python as plugin language

John Roth johnroth at ameritech.net
Wed Jun 27 00:34:20 EDT 2001


"Lyle Johnson" <ljohnson at resgen.com> wrote in message
news:tji6ftsb4dlla3 at corp.supernews.com...
> > 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?
>
> Why can't you pass a communication parameter?

You certainly can pass one - if you expect your users to muck it up
regularly. I'd rather not give them the opportunity - it's easier on both of
us.

> That is in fact the approach I
> think they described in "Internet Programming with Python", where they
refer
> to it as registering a "callback" object. Right after you initialize the
> Python interpreter, you call out to Python to create an instance of your
> callback object (let's call it "callbackObj") and then register that
object
> with your extension module, e.g.
>
>     callbackObj = MyCallbackObject()
>     myExtensionModule.setCallbackObject(callbackObj)
>
> The rest of this message is a quote from "Internet Programming with
Python",
> Chapter 12:
>
> """
> After everything has been initialized properly, the Python subsystem and
the
> main program may then communicate as follows. When the main application
> program desires a service of Python the program calls a method of the
> callback object, as in:
>
>     PyObject *dummy = PyObject_CallMethod(callbackObj, "someMethod", ...);
>
> To Python, this appears exactly as if a Python program had executed:
>
>     dummy = callbackObj.someMethod(args)
>
> The Python interpreter then performs the requested service by executing
the
> method and returns a value to the main program as the result of the method
> call.

See below. I'm not interested in the main program requesting a service of
Python.
I'm interested in Python requesting a service of the main program during
it's
servicing of a request from the main program. In other words: C calls
Python,
which in turn calls C.

> > 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.
>
> I'm looking at "Programming Python", 1st ed.; I don't know how much he
> changed for the 2nd ed. But in Chapter 15, "Embedding Python", my copy has
> an extended case study entitled "Embedding User-Coded Validations", which
> should be useful reading. It's followed up by a section entitled "Other
> Approaches: Registering Callable Objects", which describes the approach I
> mentioned earlier.

All the cases you cite miss the essential point - how does the Python
code call a function in the C language program it's embedded in? It's
relatively
easy for the C program to call a pure Python function - it checks its
parameters,
calculates a result, and returns it.

I'm more interested in being able to expose a complete object model, with
real, working functions and data. In other words, what you can do with
Python
in a Web browser under Windows using the Active Scripting interface,
although that's a specialized, Windows only implementation.

Callback functions, as mentioned in "Programming Python, 2nd Edition"
Chapter 20,
page 1162, do exactly the opposite. They allow the C program to call Python
functions
asynchronously. I know what that does, and that's not what I want to do. I'm
perfectly
happy with the C program calling the Python function synchronously - the
function
just needs access to objects and methods that are part of the C program.

I think the functions I need are in the Python/C API, but I've never seen
those functions
referenced in any example, or in any of the books.

John Roth





More information about the Python-list mailing list