2
Check that Cython generates a tp_clear function that actually clears object
3
references to NULL instead of None.
5
Discussed here: https://article.gmane.org/gmane.comp.python.cython.devel/14833
8
from cpython.ref cimport PyObject, Py_TYPE
10
cdef class ExtensionType:
12
Just a type which is handled by a specific C type (instead of PyObject)
13
to check that tp_clear works when the C pointer is of a type different
18
# Pull tp_clear for PyTypeObject as I did not find another way to access it
21
cdef extern from "Python.h":
22
ctypedef struct PyTypeObject:
23
void (*tp_clear)(object)
26
cdef class TpClearFixture:
28
An extension type that has a tp_clear method generated to test that it
29
actually clears the references to NULL.
31
>>> fixture = TpClearFixture()
32
>>> isinstance(fixture.extension_type, ExtensionType)
34
>>> isinstance(fixture.any_object, str)
36
>>> fixture.call_tp_clear()
37
>>> fixture.check_any_object_status()
39
>>> fixture.check_extension_type_status()
43
cdef readonly object any_object
44
cdef readonly ExtensionType extension_type
47
self.any_object = "Hello World"
48
self.extension_type = ExtensionType()
50
def call_tp_clear(self):
51
cdef PyTypeObject *pto = Py_TYPE(self)
54
def check_any_object_status(self):
55
if <void*>(self.any_object) == NULL:
57
elif self.any_object is None:
62
def check_extension_type_status(self):
63
if <void*>(self.any_object) == NULL:
65
elif self.any_object is None: