[pyicu-dev] Why SWIG?
Andi Vajda
vajda at osafoundation.org
Mon Nov 21 16:27:37 PST 2005
On Fri, 18 Nov 2005, Jim Fulton wrote:
> Andi Vajda wrote:
>>
> ...
>
>> No, I don't know Pyrex, I barely know about it.
>> Would Pyrex make it as trivial to wrap all this C++ code ?
>
> No. Is that a goal? SWIG makes it trivial to create really bad
> Python bindings to things. With a bit of work -- meaning fancy
> .i files, you can begin to make nicer interfaces. The nicer
> the Python api, the bigger the .i file. I don't really think
> that non-trivial .i files are maintainable. I went through this
> a few years ago with DCOracle. (The first version of DCOracle
> was SWIG based.)
>
> ...
>
>> A few months ago, someone recommended I take a look at SIP (in the context
>> of PyLucene): http://www.river-bank.demon.co.uk/docs/sip/
>> I haven't looked at this too closely yet either.
>
> Ah. I'm not a C++ hacker, but I have the impression that there are
> some really nice options for C++ wrapping. I'm not really familiar with
> them. It's possible that one of the clever C++ binding tools could be
> better than Pyrex.
I took a look at Pyrex today. Very cool when you want to write C code from
scratch against the Python C API. I have a bunch of code that could use this
in another project.
For PyICU, though (and PyLucene), it seems that using Pyrex would let me write
all the C++ wrapping boilerplate in Python instead of C++. SWIG, however,
generates it for me. The lesser boilerplate I have to write is hidden in SWIG
type handlers which are reused for every API using these types.
>> Could you sum up the pros and cons of Pyrex over SWIG ? (educate me)
>
> - No pointers. :)
Well, the C++ pointer is hidden in the wrapped python class, as with SWIG, and
you still have to match python's ref counting with the wrapped C++ library's
ownership policies.
> - With Pyrex, you write C extensions using an extended form of Python.
> It knows about Python and can automate a lot of book keeping that you
> would have to yourself with SWIG.
Dunno. I don't do bookkeeping in SWIG, it does it for me. To make the APIs I
wrap happier with Python, in other words, to make them more pythonic, I use a
combination of clever SWIG type handler tricks which allow me to handle groups
of arguments together and make many arguments optional. I also use %extend
constructs which allow me to write a better wrapper for an API that just
requires it (a not too common occurrence).
PyICU is really work in progress but PyLucene's python API is quite nice, all
done with SWIG.
> Honestly, I've never used Pyrex myself (I haven't needed to wrap anything
> in a while), but I've heard great things about it. Martijn Faassen is using
> it with great success to wrap libxml/libxslt:
Yes, pyrex looks very promising for replacing a bunch C code I've been writing
for a while now. But for wrapping a large C++ library like ICU, SWIG seems be
doing a very nice job (notwithstanding the bugs we recently discovered).
Andi..
More information about the pyicu-dev
mailing list