[Python-ideas] discouraging direct use of the C-API

Donald Stufft donald at stufft.io
Thu May 7 00:27:20 CEST 2015


> On May 6, 2015, at 6:16 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> 
> On Wed, 6 May 2015 13:13:57 -0400
> Donald Stufft <donald at stufft.io> wrote:
>> 
>>> On May 6, 2015, at 12:57 PM, Antoine Pitrou <solipsis-xNDA5Wrcr86sTnJN9+BGXg at public.gmane.org> wrote:
>>> 
>>> On Wed, 6 May 2015 10:23:09 -0600
>>> Eric Snow <ericsnowcurrently at gmail.com>
>>> wrote:
>>>> A big blocker to making certain sweeping changes to CPython (e.g.
>>>> ref-counting) is compatibility with the vast body of C extension
>>>> modules out there that use the C-API.  While there are certainly
>>>> drastic long-term solutions to that problem, there is one thing we can
>>>> do in the short-term that would at least get the ball rolling.  We can
>>>> put a big red note at the top of every page of the C-API docs that
>>>> encourages folks to either use CFFI or Cython.
>>> 
>>> CFFI is only useful for a small subset of stuff people use the C API for
>>> (mainly, thin wrappers around external libraries). Cython is a more
>>> reasonable suggestion in this context.
>> 
>> You can write stuff in C itself for cffi too, it’s not just for C bindings,
>> an example would be the .c’s and .h’s for padding and constant time compare
>> in the cryptography project [1].
> 
> That really doesn't change what I said.  CFFI is not appropriate to
> write e.g. actual extension classes.


What is an “actual extension class”?

> 
> Besides, we have ctypes in the standard library, it would be stupid to
> recommend CFFI and not ctypes.


Besides the fact that ctypes can only work at the ABI level which flat out
doesn’t work for a lot of C projects, but even if you’re working at the ABI
level ctypes isn’t nearly as nice to use as CFFI is. With ctypes you have to
repeat the C declarations using ctypes special snowflake API but with cffi
you just re-use the C declarations (for the most part), in most scenarios
you can simply copy/paste from the .h files or man pages or what have you.

Here’s a decent read: http://eli.thegreenplace.net/2013/03/09/python-ffi-with-ctypes-and-cffi

---
Donald Stufft
PGP: 7C6B 7C5D 5E2B 6356 A926 F04F 6E3C BCE9 3372 DCFA

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150506/08f504f7/attachment-0001.sig>


More information about the Python-ideas mailing list