[Python-Dev] Use of Cython

Ronald Oussoren ronaldoussoren at mac.com
Mon Aug 6 09:25:12 EDT 2018



> On 5 Aug 2018, at 18:14, Nick Coghlan <ncoghlan at gmail.com> wrote:
> 
> 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.
> ====

And earlier Nick wrote:
> 1. The test case itself (what action to take, which assertions to make about it)
> 2. The C code to make the API call you want to test
> 3. The Python->C interface for the test case from 1 to pass test
> values in to the code from 2
> 4. The C->Python interface to get state of interest from 2 back to the
> test case from 1

For all of Cython, ctypes and cffi you almost never have to write (2), and hence (3) and (4), but can write that code in Python. This is at the code of making it harder to know which bits of the CPython API are used in step (2), which makes it harder to review a testcase. 

BTW. In other projects I use tests there almost all of the test code is in C, the unittest runner only calls a C function and uses the result of that function to deduce if the test passed or failed. This only works nicely for fairly simple tests (such as the example test in this thread), not for more complicated and interesting tests due to having to write more C code.

Ronald

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180806/2e041841/attachment.html>


More information about the Python-Dev mailing list