[C++-sig] Determine C++ type held by object? (original) (raw)

Alex Mohr amohr at pixar.com
Tue Dec 13 22:36:27 CET 2005


extract() is awesome for getting C++ objects out of python objects. However, extract goes through the converters. I would like a way to determine whether an object is holding a particular C++ type without going through converters. Anyone know of a way to do this?

For those interested, here's why I would like it:

I am wrapping a boost::any-like object.

  1. I want 'any' objects in python that are passed as arguments to wrapped C++ functions to convert to their held type if necessary. I have this working just fine with a custom converter. Call this converter #1.

  2. I want to be able to call wrapped C++ functions that take 'any' objects with arbitrary python objects. That is, I want objects of all types to convert to 'any'. I have attempted to write this converter. Call it converter #2.

Here's the problem. In #1's convertible() function, I have to check if the python object is holding an 'any', and if so, get it out and check if the type it's holding is a match. I use extract for this.

However, #1's call to extract invokes converter #2. Converter #2 always says it can convert to 'any', and it does so by actually constructing an 'any' object holding what it was given. Unfortunately, this invokes converter #1 again during the 'any' constructor. Since #1 tries to extract from what it was passed, we get back into converter #2. To infinity and beyond.

I can actually fix this by having converter #1 set a flag during its operation indicating that it, in fact, is trying to extract. Then converter #2 checks this flag and returns 0 in convertible() if it is set. This works, but is not very satisfying.

Anyway, if I had the ability to ask if a python object was holding a particular wrapped C++ type (my 'any') without going through converters, then in converter #1 I could just check that and only convert in that case.

Thanks,

Alex



More information about the Cplusplus-sig mailing list