python as plugin language

John Roth johnroth at ameritech.net
Tue Jun 26 11:57:11 EDT 2001


"Lyle Johnson" <ljohnson at resgen.com> wrote in message
news:tjh5isq58hk8dc at corp.supernews.com...
> > could someone please enlighten me! I want to embed python in my app
> > as a scripting language... I have succeeded in extending python via
swig.
> > Not what I want, because main() has to come from my c-app.
> >
> > Then i can embed python in a c program with things #include "Python.h"
> > and Py_Initialize(), etc. but that is still not what i want, because i
> have no
> > idea how I can call C-Funktions with this approach... clear what i mean?
> > I fear not...
> >
> > From my app I want to load a plugin (a python script) and from this
script
> > I want to access functions of my app.
> >
> > Some pointers would be _very_ welcome!!
>
> If you've already learned how to develop C extension modules (using SWIG,
or
> "by hand") you've gotten past the biggest hurdle. And it sounds like
you've
> got the idea w. r. t. embedding Python in your application. I know of two
> books that do a pretty good job of explaining the "big picture" for
> embedding Python as an application-scripting language. One is "Programming
> Python", by Mark Lutz; the other is "Internet Programming with Python", by
> Aaron Watters, Guido van Rossum and James C. Ahlstrom. The latter may be
out
> of print, but if you run across it you'll probably be able to get it for
> cheap. A good general approach, though, is:
>
>  1. Develop one or more C extension modules that expose some subset of
your
> application's functionality,
>      i.e. the stuff you'd like to be able to access from a Python script.
>
>  2. Embed the Python interpreter in your application, initialize it by
> calling Py_Initialize(), etc.
>
>  3. At some selected point(s) during your application's execution, pass
the
> user's "plugin" script
>     to the embedded Python interpreter. Within that script, the user will
> presumably import your
>     extension module(s) and make application-specific calls back into your
> application.
>
> Hope this helps,
>
> Lyle

As someone who may need this in a couple of months, I'd like to ask for
a bit of clarification. There may be a real obvious answer, but how do you
get the application and the separately compiled  and linked extension module
to talk to each other when you can't pass a communication parameter?

Let's say I've got an application called "foo.exe"
In that application, I've included and initialized the interpreter, and the
user running it wants to execute a Python script named "spam.py".

Presumably, the script has these two statements (among lots of others):

import foo

    foo.bar()

On a first scan, "Programming Python" doesn't deal with this issue at all.
It deals with a lot of other stuff, but if it deals with this, I missed it.

It looks like the PyImport_ExtendInittab(struct_inittab *newtab) function
is what is needed. If I'm reading the Python/C API reference manual
correctly, it will add the module to Python's list of built-in modules,
where
it will be availible for the Python script when it wants it.

John Roth
>
>





More information about the Python-list mailing list