How do I wrap a C++ object to a PyObject*
I'm trying to embed Python in my C++ application, and it looks pretty easy to convert a PyObject* returned from PyRun_String using the 'extract' functionality. How do I go the other way? I.e. I want to set a variable in __main__ to an instance of my C++ class (or pass them as arguments to PyObject_CallFunctionObjArgs). With SIP there was a function called sipMapCppToSelf used like this: void set(const std::string& name, const LegalStatusCode& value) { PyObject* key = PyString_FromString(name.c_str()); PyObject* val = sipMapCppToSelf(&value, sipClass_LegalStatusCode); PyDict_SetItem(mainmodule, key, val); Py_DECREF(key); Py_DECREF(val); } I've looked through the documentation but haven't found anything obvious... -- bjorn
"Bjorn Pettersen" <BPettersen@NAREX.com> writes:
I'm trying to embed Python in my C++ application, and it looks pretty easy to convert a PyObject* returned from PyRun_String using the 'extract' functionality. How do I go the other way? I.e. I want to set a variable in __main__ to an instance of my C++ class (or pass them as arguments to PyObject_CallFunctionObjArgs). With SIP there was a function called sipMapCppToSelf used like this:
void set(const std::string& name, const LegalStatusCode& value) { PyObject* key = PyString_FromString(name.c_str()); PyObject* val = sipMapCppToSelf(&value, sipClass_LegalStatusCode); PyDict_SetItem(mainmodule, key, val); Py_DECREF(key); Py_DECREF(val); }
I've looked through the documentation but haven't found anything obvious...
Let's see. You have a wrapped C++ class somewhere? If so, you can write in __main__: import some_boost_python_module x = some_boost_python_module.some_class(some_arg) But this is trivially explained in the docs, so you must mean something else... -- David Abrahams dave@boost-consulting.com * http://www.boost-consulting.com
--- David Abrahams <dave@boost-consulting.com> wrote:
"Bjorn Pettersen" <BPettersen@NAREX.com> writes:
I've looked through the documentation but haven't found anything obvious...
Let's see. You have a wrapped C++ class somewhere? If so, you can write in __main__:
import some_boost_python_module x = some_boost_python_module.some_class(some_arg)
But this is trivially explained in the docs, so you must mean something else...
I believe he wants to do this from C++. I don't know what __main__ is, but I am guessing that his problem is solved by using a construct like this: scope().attr("key") = value; which I believe is just a shorthand for scope().attr("key") = object(value); This sets an attribute in the importing module. value can be any object that is convertible from C++ to Python, including but not limited to instances of a user-defined classes X with a corresponding class_<X> statement. Ralf __________________________________________________ Do you Yahoo!? HotJobs - Search new jobs daily now http://hotjobs.yahoo.com/
participants (3)
-
Bjorn Pettersen -
David Abrahams -
Ralf W. Grosse-Kunstleve