Python-C Integration: Ctypes, CFFI, or Binary Module Creation

Basically, I want to make Python call functions written in C.

So (as far as I know) my options are:

  • CTypes / CFFI
    • Create a DLL / SO / DyLib containing the C functions and access them using CTypes or CFFI . CFFI seems to be faster with the only drawback of declaring all function signatures in python.
    • Pros:
      • No need to adapt to my C functions. All type-translations are done in Python.
    • Minuses:
      • Performance?
  • Python binary module
    • Write Python interface to C, convert my C module to python binary module
    • Pros:
      • Performance?
    • Minuses:
      • All type conversions are performed in C. Using [SIP] [3], this can be automated.

Is converting a C module to a python binary module faster?

Are both solutions for sending python callbacks to C functions supported?

Is SIP a good option for creating a python interface? Are there any other options?

Are there any other features in any of them?

+6
source share
1 answer

I was just looking at the old list of options that I posted related to this: http://stromberg.dnsalias.org/~strombrg/speeding-python/

If you are only targeting CPython (2.x or 3.x), I would probably go for Cython.

If you want to be able to work on Pypy, CFFI may be good; I haven't tried it yet, but it's great. This is not quite like ctypes, although - ctypes is a higher level of ABI, while CFFI is more of an API level - which is nice.

If you want to be able to run on Jython, a subprocess is probably the best choice.

+1
source

Source: https://habr.com/ru/post/949867/


All Articles