[C++-sig] Re: instantiating python objects within C++

Stefan Seefeld seefeld at sympatico.ca
Wed May 28 02:53:00 CEST 2003


David Abrahams wrote:

> The Boost.Python shared library has a converter registry where all of
> this information is stored.

ok, this seems really to be the key. How does this work ? I'm a bit confused,
as conceptually the lines

BOOST_PYTHON_MODULE(Sandbox)
{
   python::class_<Base> base_type("Base");
   //...
}

create a type 'Sandbox.Base', i.e. the equivalent in C++ would be
something like 'Sandbox::Base'. However, 'Sandbox' isn't a type,
so I wonder how I can access the above type object from inside
C++.
Your example at the bottom of
http://www.boost.org/libs/python/doc/tutorial/doc/derived_object_types.html
hints a bit at how to instantiate such type objects, but I fail
to see how I could do that from within a different 'scope'.

What I'm really pondering about now is how to compare types, i.e.
if I'v obtained a type from a python script, and I want to
test whether this type derives from 'Sandbox.Base':

from Sandbox import *
class Derived(Base):
   def foobar(self): pass

I should be able to take the python::object that is the
(wrapper around the) python type, and compare it with the 'base_type'
from the above snippet, i.e. doing something like

python::handle<> result(PyRun_File(fp, const_cast<char *>(file.c_str()),
				   Py_file_input,
				   globals.ptr(), globals.ptr()));
python::object derived_type = globals["Derived"];

I want to compare 'derived_type' with 'base_type', for example using

Py_IsSubclass(derived_type.ptr(), base_type.ptr())

But how can I access 'base_type' ? It's a local variable in the scope of
'BOOST_PYTHON_MODULE(Sandbox), or is it ?

Kind regards,
		Stefan





More information about the Cplusplus-sig mailing list