SWIG vs. ctypes (Was: ANN: PyEnchant 1.5.0)

R

Roy Smith

Ryan Kelly said:
* Migrated from SWIG to ctypes for the C binding:

Ryan,

I'm looking at a doing a Python wrapper for a C++ library. We've already
done a Perl wrapper for this library using SWIG (I wasn't personally
involved in that effort). I'm debating whether I should do the Python
version with SWIG or ctypes. I'd be interested to hear about your
experiences with both and why you switched.
 
S

skip

Roy> I'm debating whether I should do the Python version with SWIG or
Roy> ctypes.

Unless your C++ library exports a C api I don't think you can use ctypes to
make it available within Python.
 
C

cptnwillard

You can't use ctypes for C++, only for C-style APIs.

Diez

With some work, you can convert your C++ objects to PyObject* and then
return the latter in a function with C bindings.
 
M

Matthieu Brucher

Boost.Python is difficult to understand, far more than SWIG. It may be
faster, but I don't think it is worth it ATM.
Binding C++ classes with SWIG is really simple.

Matthieu
 
D

Diez B. Roggisch

With some work, you can convert your C++ objects to PyObject* and then
return the latter in a function with C bindings.

Or you use SWIG or SIP or Python::Boost to do that for you with less work.


Diez
 
R

rfk

 I'd be interested to hear about your
experiences with both and why you switched.

Enchant has a pretty simple C API so the binding process was
straightforward with both SWIG and ctypes. The only real trick was in
passing python functions as callbacks to the C API, for which I found
ctypes a little easier to use.

The switch wasn't motivated by any problems with SWIG, but simply
because ctypes seems to have a lot of momentum on the alternate python
implementations such as PyPy, Jython etc.

As others have noted, ctypes unfortunately doesn't support C++
bindings. There was a recent post on the PyPy blog discussing options
for C++ that you might be interested in:

http://morepypy.blogspot.com/2008/10/sprint-discussions-c-library-bindings.html


Cheers,

Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top