[C++-sig] Executing python code from C++

Murray Cumming murrayc at murrayc.com
Sun Feb 7 14:29:53 CET 2010


On Sun, 2010-02-07 at 08:03 -0500, Stefan Seefeld wrote:
> On 02/07/2010 05:22 AM, Murray Cumming wrote:
> > In Glom, to execute arbitrary Python code, I use PyRun_String() to parse
> > the Python code, PyDict_GetItemString() and PyObject_Call() to get a
> > callable object for that code, and then PyObject_CallObject() to
> > actually execute the code and get a return value. This was the result of
> > experimentation and code archeology:
> > http://git.gnome.org/browse/glom/tree/glom/python_embed/glom_python.cc?h=glom-1-12#n269
> >
> > Is there anything in boost::python that wraps this, so I can make the
> > code nicer?
> >    
> 
> Yes. Please read the Boost.Python documentation, specifically on Embedding:
> http://www.boost.org/doc/libs/1_42_0/libs/python/doc/tutorial/doc/html/python/embedding.html

Thanks.

So, I guess I can use boost::python::exec() to call code that defines a
Python function.

But how can I get a boost::python::object for the (callable object of)
the function? In C, I'm using PyDict_GetItemString() for that.

-- 
murrayc at murrayc.com
www.murrayc.com
www.openismus.com



More information about the Cplusplus-sig mailing list