[C++-sig] Re: Creating an instance of a Python class in C++: PyRun_String?
dave at boost-consulting.com
Fri Nov 15 13:21:27 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
>> >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));
>> >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?
> 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. Also, if your compiler is having trouble
object inst = klass();
Base& py = python::extract<Base&>(inst);
you can always use:
Base& py = python::extract<Base&>(inst)();
to help it. You must be using MSVC, eh?
dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution
More information about the Cplusplus-sig