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

Dirk Gerrits dirk at gerrits.homeip.net
Fri Nov 15 09:05:29 CET 2002


Bjorn Pettersen wrote:

> I don't know what's happening whith your extract, but you need to define
> your previous statements in a module/namespace and feed this to
> PyRun_String, e.g.:
>
>     PyObject* mainmod = PyImport_AddModule("__main__");
>     ns = PyModule_GetDict(mainmod);
>     Py_INCREF(ns);
>     // add your definitions to the namespace
>     PyObject* x = PyRun_String("..your classdef here..",
>        Py_file_input, ns, ns);
>     Py_XDECREF(x); // it doesn't contain anything useful
>     PyObject* res = PyRun_String("PythonDerived()\n", Py_eval_input, ns,
> ns);

Thanks, this makes sense. However, the extract still won't work. The line:

Base* py = python::extract<Base*>(res);

seems to trigger a "Microsoft C++ exception: 
boost::python::error_already_set" from:

(throw_no_pointer_from_python) in extract_pointer<Ptr>::operator()() const.

Dirk







More information about the Cplusplus-sig mailing list