simple(?) Python C module question

Dan Stromberg drsalists at gmail.com
Wed Nov 17 23:37:47 EST 2010


On Wed, Nov 17, 2010 at 7:42 PM, Terry Reedy <tjreedy at udel.edu> wrote:

> On 11/17/2010 7:25 PM, Mark Crispin wrote:
>
>  Have you looked at ctypes? It's not suitable for all libraries, but
>>> it can often obviate the need to write any C code:
>>> http://docs.python.org/release/2.6.6/library/ctypes.html#module-ctypes
>>>
>>
>> Hmm. I don't think that it helps, especially as I don't really want to
>> make the consumers of this module know anything about c-client or its
>> calling conventions.
>>
>
> For the record, a C library wrapper written in Python with cytpes is nearly
> indistinguishable to users from an equivalent wrapper written in C. However,
> given that you are more comfortable with C than Python and have gotten the
> info you need, stick with that.
>

...except when you go to recompile a C extension module, if a dependency has
changed its API, the C compiler will at least output a warning at the
relevant spot.  ctypes code is likely to be rather less friendly about it -
the word "segfault" comes to mind.

I'd say that the 3 main options for C <-> Python interop are:
1) ctypes (nice for light use and if you only need python, not that well
type checked, probably the best bet if you're targeting python
implementations other than CPython alone)
2) swig (nice if you plan to expose your C API to lots of languages)
3) cython (nice if you only need python, and want to more or less freely
intermix normal python with C symbols using a very python-like syntax and
with speed near that of C)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101117/2507da7f/attachment-0001.html>


More information about the Python-list mailing list