Python embedding/extending

Alex Martelli aleaxit at yahoo.com
Fri May 23 05:29:48 EDT 2003


Chris Heald wrote:
   ...
> However, I want to have multiple objects existing concurrently, from which
> I can call methods, and then pass data to other methods. All the

OK, but I do not think I understand how that would be a problem.

> 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).

What's hard about the latter?  E.g., once, from your application, you
have obtained the PyObject* that indicates a Python class, you call it
with the appropriate arguments and thus obtain the PyObject* that
indicates the new Python instance, with its state and all, ready for
you to call methods on, etc etc.  What's "pretty much stateless" about
this?  I suspect I must be missing something.


> 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.

*blink* now I KNOW I must be missing something.  If a Python-accessible
module "won't necessarily have access to the instance of the state
machine", what WILL have that access?


> 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)?

Boost should be OK but then so should Swig, the bare Python C API, or
anything else really -- I don't understand what's special about your
requirements, that would or might influence the choice among them -- i.e.
it seems to me that the choice would be taken on the basis of exactly
the same requirements as for any other embedding/extending case.

> 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.

Perhaps the best way to finesse the communication problems that I think
must be plaguing us here would be for you to build an absolutely simple
'toy' version of an application that shares just the very minimum barest
possible subset of structure with your real one -- it has a state machine
and embeds Python as you'd like to embed it, with Python objects having
their own state and the like.  If you can reproduce what you think are the
problems you're facing in an absolutely lilliputian scale, tiny enough
to post here, then maybe we would be enabled to understand your situation
and offer helpful advice.  But as it stands I have the feeling that I do
not understand your problem at all, just about... this makes it hard to 
help!


Alex





More information about the Python-list mailing list