On 6/27/08, Brad Aagaard <[EMAIL PROTECTED]> wrote: > I saw your message to petsc-dev regarding using cython for petsc4py. Are > you switching from swig to cython?
Yes, > I have been using Pyrex/pyrexembed to > interface C++ and Python and find it very cumbersome and considerable work. You are completelly right. In the current status of Cython/Pyrex, wrapping C++ API's is cumbersome, too much manual. > I was planning to switch to swig, so I would be interested to learn how > cython compares with swig. Well, Cython is not too much different that Pyrex. If you need to wrap a large C++ API, the SWIG is surelly the right tool. Iff you learn how typemaps work, then you will be able to generate really good, pythonic wrappers. In the very particular case of petsc4py, the situation is not quite the same. PETSc API is a C API. Then I used SWIG to generate an extension module accessing that C API. But as PETSc has an OO design, what I really need to do have is a pythonic, fully object oriented wrapper, whith classes and all that. Then I had to write all those class hierarchies in pure Python code, making inside methods the calls to the SWIG generated wrappers. Additionally, as I need petsc4py to be completelly interoperable with third party code (I mean, I need to be able to access the actual C-level Mat handle form the Py-level Mat instance, to pass it to others C/C++/Fortran codes), I ended up writing from scratch Python type object in C, implementing very low level stuff, and completelly bypassing the 'this' machinery in SWIG with custom typemaps. As you can imagine such implementation is curbersome to understand for others, and it is a bit hard to maintain and extend. Then I decided to switch to Cython. Then, the same source (a bunch of pxi and pyx files) generates the high-level, pythonic, fully object oriented API, and also make the C calls to PETSc routines. And all this at C speed! no bytecode involved! Hope I was clear enough. In short, my final coment would be: (1) If you have to wrap C API's, use Cython/Pyrex. (2) If you have to wrap short C++ API's (let say 2 or 3 clases with each haveing between 5 to 10 methods), then perhaps use Cython is still a good idea, (3) If you have to wrap a large C++ API, with lots of classes and methods, and perhaps templates, then use SWIG. Finally, regarding (3), that would be my opinion until some champion can manage to write a decent C++ parser in order to automatize the C++ API --> Python API conversion. Once this feature is available, I'll surelly wrap with Cython. I have no idea how long we'll have to wait for this, the task is not trivial!! > > Thanks, > Brad > -- Lisandro Dalcín --------------- Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC) Instituto de Desarrollo Tecnológico para la Industria Química (INTEC) Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET) PTLC - Güemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
