[C++-sig] How do I detect a Python subclass (with 3rd party pointer) ?

English, Mark Mark.English at rbccm.com
Wed Sep 26 12:21:41 CEST 2007


If I expose a class through Boost Python, is there a way to discover that the parameter to a wrapped function
is an instance of a sub-class defined in Python, rather than an instance of the actual exposed class ?
Some kind of lookup in the converter::registry ?
Some kind of attribute added to exposed classes by a visitor ?
Something else ?

For example:
// Start cpp
#include <smart_ptr.h>
class ExampleBase{};
class Example1 : public ExampleBase {};
class Example2 : public ExampleBase {};
//...
class ExampleN : public ExampleBase {};

void testFunction(boost::python::object objTest)
{
  // Some kind of check for exposed classes here
}

BOOST_PYTHON_MODULE(test)
{
  class_<Example, smart_ptr<Example> >("Example");
  class_<Example1, bases<Example>, smart_ptr<Example1> >("Example1");
  class_<Example2, bases<Example>, smart_ptr<Example2> >("Example2");
  //...
  class_<ExampleN, bases<Example>, smart_ptr<ExampleN> >("ExampleN");

  def("testFunction", &testFunction);
}

// End of cpp
#Start of py
import test
class PyDerived(test.Example2): pass
class External(object): pass
ex2 = test.Example2()
pyd = PyDerived()
ext = External()
test.testFunction(ex2) #Handled by straight C++ conversion
test.testFunction(pyd) #<- Want C++ code to realise this is a Python derived class
test.testFunction(ext) #<- Want C++ code to realise this is a Python class unrelated to C++ code
#End of py
______________________________________________________________________

This email is intended only for the use of the individual(s) to whom it is addressed and may be privileged and confidential.
Unauthorised use or disclosure is prohibited.If you receive This e-mail in error, please advise immediately and delete the original message.
This message may have been altered without your or our knowledge and the sender does not accept any liability for any errors or omissions in the message.



More information about the Cplusplus-sig mailing list