Efficient data flow between orthogonal C extensions?

Bernhard Herzog bh at intevation.de
Thu Jun 5 14:58:54 CEST 2003


mlh at furu.idi.ntnu.no (Magnus Lie Hetland) writes:

> Somewhat similar to the approach in the standard docs, then, except
> that you don't #include the .h-file of each extension...? (I'll look
> into the source.)

pstokenize #includes filter.h which contains a declaration of the struct
type and various function pointer types.

> Maybe I'm missing an important part of the functionality here... Is it
> possible to only keep a generic version of these declarations, and to
> store the specific implementations as PyCObjects? Is that what you do
> in Sketch?
> 
> Basically, what I would like to do is have all C components totally
> ignorant of each other and only receive info about the "outside world"
> from the Python interpreter (probably in the form of PyCObjects).

Well, if you want to access them at a lower level than Python's abstract
object API with the PyObject_* functions then you'll have to define at
least an application specific C Api. 

One way to go about this that I can think of right now and that
minimizes dependencies would be to assume that all objects that
implement this interface have a pointer to a struct with function
pointers as their first struct element after PyObject_HEAD. e.g.

typedef struct {
    void (*do_something)(PyObject * self, int some_arg);
    /* ... */
} FooMethods;

typedef struct {
    PyObject_HEAD
    FooMethods * methods;
   /* ... */
}
FooObject;


and then the API functions become simply

void
FooDoSomething(PyObject * obj, int some_arg)
{

    /* check that obj is indeed a FooObject left as an exercise for the
     * reader :) but one could use PyObject_IsInstance, assuming all
     * types are derived from the same base type which probably requires
     * new style classes 
     */
   
    ((FooObject*)obj)->methods->do_something(obj, some_arg);
}


The FooMethods struct would work more or less like the type object and
perhaps there is a way to put these C-level methods into the type object
somehow. I don't there being a clean way for this but perhaps this is
possible now with new style classes.

The approach taken in Sketch's stream filters is a bit similar to the
FooMethods approach outlined above but doesn't go as far because there
is only one object type for all filter objects and the instances only
differ in which function pointers and filter specific data they have.

> >PyGTK also does somethng like this.
> 
> OK. Thanks.

PyGTK also has pygtk specific header files, though.

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/




More information about the Python-list mailing list