[Distutils] Sharing code between C extensions

M.-A. Lemburg mal@lemburg.com
Mon Oct 21 15:04:01 2002


Anders J. Munch wrote:
> Hi all,
> 
> I have a set of C extensions that I developed while using my own
> Borland compiler Win32 Python build, statically linking the whole
> thing.  Recently I gave up on using that special build, too much
> hassle to get third-party stuff (like wxWindows) to work.
> 
> So now I'm using distutils, and I must say it's been surprisingly
> smooth sailing so far.  Good work guys!
> 
> I've hit a snag, though: My C extensions (are supposed to) share code
> and data.  But distutils insists that each extension be self-contained
> and include everything that it depends on.
> 
> So that means that some of my C extensions and library code now exists
> in multiple copies, one copy per extension.  I don't mind the memory
> used; but the duplicated static data and duplicated type objects are
> causing problems.
> 
> I guess placing the shared stuff in a DLL is an option but not one I'm
> too fond of, for various reasons.  (Or perhaps I should switch to
> Linux where shared objects behave differently? Well, some other time,
> perhaps ;-))
> 
> What I'd like to do is to have multiple C extensions in the same .pyd.
> Like, instead of writing (simplified):
>      ext_modules = [Extension('foo', source=['foo.cc']),
>                     Extension('bar', source=['bar.cc'])]
> if I could do:
>    ext_modules = [Extension(['foo','bar'], source=['foo.cc','bar.cc'])]
> and have both extensions in the same .pyd.
> 
> Hmm... come to think of it I guess I could fake it with
>    ext_modules = [Extension('foo', source=['foo.cc', 'bar.cc'])]
> and then calling initbar() from initfoo().  I'd like to find a cleaner
> solution though.

That won't work: Python uses the DLL name to determine the
init function name, so in your example it will look for
initfoo().

If you want to share data between extensions, the right
thing to do is to wrap the data in a PyCObject which you
then access using the standard Python import mechanisms.

Works great. See mxDateTime for an example on this can
be done or the interaction between socketmodule.c|h
and _ssl.c in Python itself.

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
_______________________________________________________________________
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting:                               http://www.egenix.com/
Python Software:                    http://www.egenix.com/files/python/