Re: [C++-sig] Passing an instance...
Beau Sapach wrote:
Hello everyone,
I've got a C++ object that I've created a Python type for. When I insert the object into the main module using PyModule_AddObject then I can instantiate it in Python and everything works just fine. I assumed (mostly from reading other's code) that I could use something like:
PyDict_SetItemString(MainDict,"x",pMyObject)
Up to this point there is no need to use the C API directly. Assuming you have exported your C++ type 'Type' to python, you can write (inside C++): namespace bpl = boost::python; bpl::object instance(Type(...)); bpl::dict global; global["instance"] = instance; bpl::exec_file(python_script, global, global); and the executed script can access your Type instance directly through the 'instance' variable. Caution: the 'exec_file' function has been added after 1.33.1, so you need to use a CVS snapshot. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin...
Hello everyone,
I've got a C++ object that I've created a Python type for. When I insert the object into the main module using PyModule_AddObject then I can instantiate it in Python and everything works just fine. I assumed (mostly from reading other's code) that I could use something
Hi Stefan, Yes.... I originally got a copy of this code (and the recommendation to move to the CVS version) from you via this list. When I last left boost, using your example code, the library was crashing on: bpl::object main = python::import("__main__"); as I was trying to add my instance to the main module's dictionary. Because of that I've abandoned boost for the time being and am trying to simply understand the C API better. I suppose I could look into the boost::python code to see what operator[] is doing but I was hoping a quick answer from the mailing list would point me in the right direction, with either the correct API function or telling me what I'm missing. Beau -----Original Message----- From: c++-sig-bounces@python.org [mailto:c++-sig-bounces@python.org] On Behalf Of Stefan Seefeld Sent: Friday, November 03, 2006 12:11 PM To: Development of Python/C++ integration Subject: Re: [C++-sig] Passing an instance... Beau Sapach wrote: like:
PyDict_SetItemString(MainDict,"x",pMyObject)
Up to this point there is no need to use the C API directly. Assuming you have exported your C++ type 'Type' to python, you can write (inside C++): namespace bpl = boost::python; bpl::object instance(Type(...)); bpl::dict global; global["instance"] = instance; bpl::exec_file(python_script, global, global); and the executed script can access your Type instance directly through the 'instance' variable. Caution: the 'exec_file' function has been added after 1.33.1, so you need to use a CVS snapshot. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... _______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig
participants (2)
-
Beau Sapach -
Stefan Seefeld