Extension module interdependencies

Anders J. Munch andersjm at dancontrol.dk
Mon Feb 24 04:20:50 EST 2003


"Mark Rowe" <bdash at gmx.net> wrote:
> Hello,
> 
> I'm building a package with the layout:
> 
>     pkg/
>           __init__.py
>           foo.so
>           bar.so
> 
> where foo.so and bar.so are extension modules written in C.  In bar.so, 
> several custom types are defined.  A method in foo.so creates instances 
> of these custom types by way of a newBarObject function defined in 
> bar.so.  Problems arise when building the package using distutils.  
> foo.so needs to be linked against bar.so, as it uses methods defined in 
> bar.so, but I am unsure how to get distutils to do this for me.

Been there - didn't find a fully satisfactory solution.

There are three directions to go.

1) Pass your C objects around packaged as PyCObject-s.  Whenever you
need to invoke a method on a BarObject from foo, bar must provide a
wrapper function that takes a PyCObject argument and calls the method.

This is cumbersome and intrusive, but The Right Way.

2) Move the implementation out into separate files and include them
with both modules. Like this:
 
     pkg/
           __init__.py
           foo.so (compiled from foo.c, foo_impl.c and bar_impl.c)
           bar.so (compiled from bar.c, foo_impl.c and bar_impl.c)

The code duplication is unfortunate, but AFAIK benign on
".so"-platforms, where duplication is eliminated on loading.
".dll"-platforms are another story, as duplication is retained at
runtime.

3) The hack that I did to avoid code duplication in .dll's.  I'll get
into that on request.

- Anders






More information about the Python-list mailing list