[C++-sig] Re: Creating an instance of a Python class in C++: PyRun_String?

Dirk Gerrits dirk at gerrits.homeip.net
Fri Nov 15 15:38:14 CET 2002

David Abrahams wrote:

> >>Does
> >>
> >>   http://www.boost.org/libs/python/doc/v2/extract.html#examples
> >>
> >>help?
> >
> >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?

> Also, if your compiler is having trouble
> with:
>     object inst = klass();
>     Base& py = python::extract(inst);
> you can always use:
>     Base& py = python::extract(inst)();
>                                            ^^
> to help it. You must be using MSVC, eh?

That compiles, thanks! I'm using Intel 6.0 though, not MSVC.

However, I'm still getting the run-time error for the extraction. I 
think I now know what is causing it though. I'm now doing this:

python::extract<Base&> extractor(res);
if (!extractor.check())
     std::cout << "Check returns false and ";

     Base& py = extractor();

catch(python::error_already_set const&)
     std::cout << "the extraction throws an exception: " << std::flush;


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 PythonDerived

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?


More information about the Cplusplus-sig mailing list