Wrapping a C library in Python

David M. Cooke cookedm+news at physics.mcmaster.ca
Fri Nov 19 15:42:34 EST 2004


John Hunter <jdhunter at ace.bsd.uchicago.edu> writes:
>>>>>> "Michael" == Michael Loritsch <loritsch at gmail.com> writes:
>     Michael> I recommend both boost::python
>     Michael> (http://www.boost.org/libs/python/doc/index.html) and
>     Michael> ctypes (http://starship.python.net/crew/theller/ctypes/),
>     Michael> but for very different reasons.
>
>     Michael> If wrapped code speed and production of a binary is a
>     Michael> goal in creating your python extensions, use
>     Michael> boost::python.  The price you pay for creating an fast
>     Michael> binary python extension is coding your translation from
>     Michael> python in C/C++ in C/C++ (I suppose you could get around
>     Michael> this by creating a boost::python module, and then
>     Michael> wrapping it with a python module to do the type
>     Michael> translation).  And in boost::python, C++ to python
>     Michael> exception translation is supported.
>
> I don't fully agree with this.  I've been working on a wrapper for
> antigrain, a C++ library that makes heavy use of templates.  I started
> off using pyste (a boost::python generator) and boost.  It worked
> reasonably well - pyste is not being actively maintained right but you
> can usually work around the limitations by writing boost code where
> you need to.  I was reasonably happy, until I saw my *.so files
> ballooning.  After wrapping a small fraction of the library, and
> having instantiated only a few of the many templates I ultimately
> wanted, my extension files were at 20MB, which is *much larger* than
> the agg library or moderately sophisticated applications built around
> it.  And I still had *a lot* left to expose!

Did you strip the extension modules (run 'strip' on the .so file)? I
know, there's nothing in distutils that will do that automatically. I
just did this on an extension module of mine using boost::python.
Before stripping, it was about 1.3 MB, afterwards, 50 kB.

The problem is the symbol table is kept by default, and it seems with
GNU C++ (at least) that can get *huge* when templates are involved.

Compile times are pain still.

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca



More information about the Python-list mailing list