[Cython] SWIG and Cython
Ondrej Certik
ondrej at certik.cz
Mon Jun 2 13:47:39 CEST 2008
On Fri, May 30, 2008 at 11:43 PM, Lisandro Dalcin <dalcinl at gmail.com> wrote:
> Well, In the case of wrapping large, legacy C++ libraries, then the
> choice is not easy, it depend on many factors. I'll try to elaborate
>
> For something like MPI and PETSc in case for mpi4py and petsc4py,
> which are both being converted to Cython, the option is really clear
> for me: just use Cython, do not bother with SWIG.
>
> Why use Cython? Their native API's are just C (well, MPI has a
> standard C++ API, but not much more featured than the C one). Wrapping
> a C API and providing just C-ish functions at the Python level is just
> unpythonic and painfull to use that plain C. You really need a trully
> OO Python API, if not, you Python level API is just much crap than the
> C one, hard to use, perhaps the only you alleviate is error checking.
> Building a OO API targeting Python through using Cython and calling a
> your legacy library C API is just easy, fast, convenient than using
> SWIG and next use the SWIG generated code to implement a
> better-looking, high-level, pythonic API for Python-level consumption.
>
> In the case of a large C++ API its depend on how much of that API it
> makes sense to expose to Python. Let suppose we have an API with a lot
> of classes and a lot of methods (let say 10,20,30 classes, with
> 10,10,30 methods each). Then wrapping the full API in the current
> status of Cython, that is going to be a real pain, to much manual.
> BUT, if you are going to use only a few of your classes, and only
> expose a few of your methods, or even want to expose new methods, more
> convenient to use in the Python side, then, again I will definitelly
> choose Cython.
>
> My personal experience in going from SWIG to Cython is worth to take
> into account:
>
> Implementing a really good, robust, full featured, pythonic access to
> full PETSc functionalities for petsc4py took me about two years of
> carefull writting of typemaps, implementing Python extension types BY
> HAND, and writting a log or C code inside custom typemaps, with Python
> and Numpy C API calls plus PETSc calls.
>
> Then, after the success with Cython for the mpi4py case, I started the
> process of rewritting petsc4py from scratch, working at night, let say
> from 9:00 PM to until 1:00 AM or 2:00 AM. In about 10 days, yes, JUST
> 10 DAYS of night work, I'm near to have implemented all the features
> of the previous implementation, and the new implementation is actually
> bether and more featured....
>
>
> In short, if you really, really, really want to wrapp the FULL C++
> library, then the Cython way will be harder than the SWIG way. But I
> really doubt that you really need to access the FULL C++ API. But one
> thing that you sould take for granted: the Cython way will let you
> write better-looking, more easily maintenable source code.
>
> That's all, folks!
Thanks for sharing this, it is a very useful info!
Ondrej
More information about the Cython-dev
mailing list