[C++-sig] Re: Creating an instance of a Python class in C++: PyRun_String?
dave at boost-consulting.com
Sun Nov 17 00:55:16 CET 2002
Dirk Gerrits <dirk at gerrits.homeip.net> writes:
> David Abrahams wrote:
>> >> http://www.boost.org/libs/python/doc/v2/extract.html#examples
>> >I looked at that one, as I said in my initial post. However the
>> >example creates a new python::object instance of a wrapped C++
>> >class. Not of a Python class. Is there some way to create a class_
>> >object from a Python class that I'm not aware of?
>> No. However, if you can arrange to pass the class object from Python
>> to C++ in an object instance, you can avoid all of the problems you're
>> having with PyRun_String.
> This sounds very interesting! I don't know how to do it though. I'm
> very, very new to the Python/C API, if you hadn't guessed so
> already. ;) Can you give me some pointers on this one?
Urm, all I meant was:
void f(object class_object) // wrap this function
object instance = class_object(param1, param2, ...);
Base& = python::extract<Base&>(instance);
Pass the class from Python to the wrapped version of f().
> Check returns false and the extraction throws an exception:
> TypeError: No registered converter was able to extract a C++
> reference to type class Base from this Python object of type
> This reminded me of something I read in a post by Nicolas Lelong. He
> calls python::converter::initialize_builtin_converters() just after
> Py_Initialize(). So I tried that, but the linker complained:
> unresolved external symbol "void __cdecl
> Any idea why that might be?
Because it's not an exported symbol. He was probably linking
Boost.Python's sources directly into his application, or was using
You could try something like:
Which will call that function.
When we get embedding support officially set up, you won't have to
resort to this sort of hack anymore.
dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution
More information about the Cplusplus-sig