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

David Abrahams dave at boost-consulting.com
Fri Nov 15 13:09:03 CET 2002

Dirk Gerrits <dirk at gerrits.homeip.net> writes:

> David Abrahams wrote:
>> Dirk Gerrits  writes:
>> >Dirk Gerrits wrote:
>> >
>> >
>> >>Firstly, I don't know which arguments to feed into PyRun_String. The
>> >>python.org docs didn't really help me that much: I don't have any
>> >>dictionaries of globals and locals, and what exactly is a start
>> >>token?
>> >
>> >Upon further RTFMing on start tokens, I tried this:
>> >
>> >// Create and use an instance of the Python derived class
>> >Base* py = python::extract(
>> >     PyRun_String("PythonDerived()\n", Py_eval_input, 0, 0));
>> >py->f();
>> >
>> >This gives an access violation in find_instance_impl which is called
>> >by extract_pointer::extract_pointer(PyObject* obj) as far as I
>> >can tell. And the Base& version wouldn't compile as I said.
>> >
>> >What (else) am I doing wrong?
>> 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?
> I don't really see how the example would compile BTW, because value is
> declared a non-const member function and it is called through the
> const reference x.

Oops! Good point, fixed.
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

More information about the Cplusplus-sig mailing list