Python embedding/extending

Chris Heald cheald45 at hotmail.com
Thu May 22 17:40:48 EDT 2003


I have a C++ state machine that I would like to script via Python. My
current approach is to embed the Python interpreter in the C++ app and load
up the scripts as necessary, and use with Boost::Python, the Python/C API,
or maybe SWIG to interface the two, so that Python scripts modify the C++
state machine. However, I would ALSO like to keep these Python objects in
memory once created, so that they maintain their own state, and act as their
own state machines, for two reasons. First, I think it will be cleaner to
wrap an object's state in itself, rather than instanciating the object,
importing the state, and running whatever function I need. Second, I think
this will facilitate speed, since the script won't have to be parsed again,
and/or bytecode won't have to be loaded/initialized again. However, I want
to have multiple objects existing concurrently, from which I can call
methods, and then pass data to other methods. All the documentation and
examples I have run in to thus far are pretty much stateless, and don't
offer too much help in the way of maintaining a persistant state in both the
host application (easy enough) and the instanciated python objects (a little
harder).

My requirements are:
a) Embed the scripting engine in the application, so that the application is
the primary executable, and the python interpreter isn't exposed.
b) Allow C++ to instanciate and keep Python objects, as well as call the
members of those objects
c) Allow Python to interface to the C++ state machine. I'm not sure a custom
module works here as the module wouldn't necessarily have access to the
instance of the state machine I'm working with.

How would you suggest that I go about doing this? Is there a particular tool
that is best suited to this application (boost::python, swig, etc)? Is there
an easier route I can take? I've been researching ways of doing this for a
couple of weeks, and I've yet to develop a solid answer.

Thanks

Chris






More information about the Python-list mailing list