[Python-Dev] cffi in stdlib

Maciej Fijalkowski fijall at gmail.com
Wed Feb 27 10:06:07 CET 2013


On Wed, Feb 27, 2013 at 9:29 AM, Ronald Oussoren <ronaldoussoren at mac.com> wrote:
>
> On 26 Feb, 2013, at 16:13, Maciej Fijalkowski <fijall at gmail.com> wrote:
>
>> Hello.
>>
>> I would like to discuss on the language summit a potential inclusion
>> of cffi[1] into stdlib.
>
> The API in general looks nice, but I do have some concens w.r.t. including cffi in the stdlib.
>
> 1. Why is cffi completely separate from ctypes, instead of layered on top of it? That is, add a utility module to ctypes that can parse C declarations and generate the right ctypes definitions.

Because ctypes API is a mess and magic. We needed a cleaner (and much
smaller) model.

>
> 2. Cffi has a dependencies on pycparser and that module and its dependencies would therefore also be added to the stdlib (even if they'd be hidden in the cffi package)

Yes. pycparser and ply.

>
> 3. Cffi basicly contains a (limited) C parser, and those are notoriously hard to get exactly right. Luckily cffi only needs to interpret declarations and not the full language, but even so this can be a risk of subtle bugs.

It seems to work.

>
> 4. And finally a technical concern: how well does cffi work with fat binaries on OSX? In particular, will the distutils support generate cached data for all architectures supported by a fat binary?

no idea.

>
> Also, after playing around with it for 5 minutes I don't quite understand how to use it. Let's say I want to wrap a function "CGPoint CGPointMake(CGFloat x, CGFloat y)". Is is possible to avoid mentioning the exact typedef for CGFloat somewhere? I tried using:
>
>    ffi.cdef("typedef ... CGFloat; typedef struct { CGFloat x; CGFloat y; } CGPoint; CGPoint CGPointMake(CGFloat x, CGFloat y);")
>
> But that results in an error when calling verify:
>
>    TypeError: field 'struct $CGPoint.x' has ctype 'struct $CGFloat' of unknown size
>
> From a first glance this doesn't seem to buy me that much w.r.t. ctypes, I still have to declare the actual type of CGFloat which is documented as "some floating point type".
>
> Ronald

typedef ... is assumed to be a struct. Copy pasting from
documentation, we might want to have a better support for that, but
what you do right now is to do:

ffi.cdef("const int mysize;")
lib = ffi.verify("const int mysize = sizeof(THE_TYPE);")
print lib.mysize

which will tell you the size (and then you can pick up which float you
want). I agree that some generic support for that would be cool.

Cheers,
fijal


More information about the Python-Dev mailing list