1""" 2Check that the @cython.no_gc decorator disables generation of the 3tp_clear and tp_traverse slots, that is, disables cycle collection. 4""" 5 6cimport cython 7from cpython.ref cimport PyObject, Py_TYPE 8 9# Force non-gc'd PyTypeObject when safety is guaranteed by user but not provable 10 11cdef extern from *: 12 ctypedef struct PyTypeObject: 13 void (*tp_clear)(object) 14 void (*tp_traverse)(object) 15 16 17def is_tp_clear_null(obj): 18 return (<PyTypeObject*>Py_TYPE(obj)).tp_clear is NULL 19 20def is_tp_traverse_null(obj): 21 return (<PyTypeObject*>Py_TYPE(obj)).tp_traverse is NULL 22 23 24@cython.no_gc 25cdef class DisableGC: 26 """ 27 An extension type that has tp_clear and tp_traverse methods generated 28 to test that it actually clears the references to NULL. 29 30 >>> uut = DisableGC() 31 >>> is_tp_clear_null(uut) 32 True 33 >>> is_tp_traverse_null(uut) 34 True 35 """ 36 37 cdef public object requires_cleanup 38 39 def __cinit__(self): 40 self.requires_cleanup = ( 41 "Tuples to strings don't really need cleanup, cannot take part of cycles",) 42 43