[C++-sig] Re: Injecting python code to a class
David Abrahams
dave at boost-consulting.com
Mon Mar 29 05:11:58 CEST 2004
Raoul Gough <RaoulGough at yahoo.co.uk> writes:
>> http://cvs.sourceforge.net/viewcvs.py/cctbx/boost_adaptbx/boost/python.py?rev=1.1&view=auto
>>
>> All my extension module imports go through a Python layer to work
>> around exception handling problems with certain versions of gcc, and
>> because I add all kinds of stuff from Python anyway. Therefore the
>> injector doesn't add any extra complications. If you don't want the
>> Python layer I'd write the extra functions in C++ using the
>> boost::python::object facilities. Just loop over your array in C++
>> and append str(element) to a boost::python::list.
>
> Yes, doing it with a loop in C++ is certainly an option, but I was
> hoping to avoid this becuase of the simplicity of the Python
> solution. The C++ code would have to rely heavily on Python facilities
> like list and str, as you suggest, since client code might have
> provided Python __str__ or __repr__ methods for the container
> elements.
>
> I had a quick look at the injector code you mentioned, but that seems
> to rely on Python code as well. Not that that's a bad thing, but I'm
> looking for a solution to embed within the C++ extension module code,
> which was my original problem - how to insert some Python code as a
> method from within the C++ code.
Can't you use PyRun_String or similar to evaluate some Python code
from C++?
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list