OSDir


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] Use of Cython


On 5 August 2018 at 18:06, Ronald Oussoren <ronaldoussoren at mac.com> wrote:
> I?m not sure if I understand this, ctypes and cffi are used to access C APIs
> without writing C code including the CPython API (see for example
> <https://github.com/abarnert/superhackyinternals/blob/master/internals.py>).
>
> The code code below should be mostly equivalent to the Cython example posted
> earlier:
>
> import unittest
> import ctypes
> from ctypes import pythonapi
>
> class PyObject(ctypes.Structure):
>     _fields_ = (
>         ('ob_refcnt', ctypes.c_ssize_t),
>     )
>
> pythonapi.PyList_Append.argtypes = [ctypes.py_object, ctypes.py_object]
>
> def refcount(v):
>     return PyObject.from_address(id(v)).ob_refcnt

The quoted code is what I was referring to in:
====
ctypes & cffi likely wouldn't help as much in the case, since they
don't eliminate the need to come up with custom code for parts 3 & 4,
they just let you write that logic in Python rather than C.
====

Cython has more machinery for accessing the CPython C API correctly
already built in to it, whereas ctypes has no type safety at all,
while cffi doesn't special case CPython's C API in particular.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia