python as plugin language

Lyle Johnson ljohnson at resgen.com
Tue Jun 26 19:30:08 EDT 2001


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

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





More information about the Python-list mailing list