[C++-sig] Passing a C++ instance to a Python script

Stefan Seefeld seefeld at sympatico.ca
Tue Feb 6 20:11:24 CET 2007


Francis Moreau wrote:
> Hello,
> 
> I need to pass a C++ instance to a Python function, then the Python function 
> can call back the C++ instance object. I found this following sample that do 
> it well when the script is embedded into the C++ code within the 
> PyRun_String function. I need to have something similar but with the 
> PyRun_String content in a .py file. As a matter of fact, when I am trying to 
> move the PyRun_String content into a .py file the line "object 
> foo(main_module.attr("foo"));" fails since the foo function is unknown from 
> the main dictionary.

Have a look at the test code in boost/libs/python/test/exec.cpp
(http://boost.cvs.sourceforge.net/boost/boost/libs/python/test/exec.cpp?revision=1.5.2.1&view=markup&pathrev=RC_1_34_0)

I think that contains all you need.

HTH,
		Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list