Hi Alex! On Thu, 21 Jul 2022 at 02:08, Alex Martelli via pypy-dev <pypy-dev@python.org> wrote: > credible examples, esp. one setting CFFI head-to-head against ctypes (but > comparisons with cython and the API would be fine too -- IF I could figure > out how to define completely new Python types in CFFI, which so far escapes > me).
CFFI is different from ctypes, and also different from the CPython C API, and also different from Cython. You can't always write a straightforward line-by-line comparison for any two of these four things. The main purpose of CFFI is to expose an existing C API to Python, a bit like ctypes but focusing on the level of C instead of on the details of the system's ABI. You can also write some extra C code and expose that to Python as well. That's it: you can't do things like define new Python types in C, or even write a C function that accepts an arbitrary Python object. Instead, you typically write a pythonic layer and this layer internally uses CFFI but doesn't expose it further to the rest of the program. This layer may do things like define its own Python classes to give a more object-oriented feeling to the existing C API; or it can wrap and unwrap arbitrary Python objects inside "handles" which can be passed to C and back as black boxes. You can also write regular Python functions and expose them for the C code to call. (This is generally useful, but is also the basis for how embedding works with CFFI.) CFFI is not really suited for *all* use cases. For example, writing a custom data structure that can be used to store a large number of arbitrary Python objects: while this is in theory possible, in practice you get large overheads. Or if all you want is a very compact Python class that can store two fixed-size integers. > If asking for your collaboration is too much, is there at least a way to get > your OK about reusing, with small adaptations, some of the excellent examples > you give on CFFI's docs? Sure. A bientôt, Armin _______________________________________________ pypy-dev mailing list -- pypy-dev@python.org To unsubscribe send an email to pypy-dev-le...@python.org https://mail.python.org/mailman3/lists/pypy-dev.python.org/ Member address: arch...@mail-archive.com