cython
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 *:
12ctypedef struct PyTypeObject:
13void (*tp_clear)(object)
14void (*tp_traverse)(object)
15
16
17def is_tp_clear_null(obj):
18return (<PyTypeObject*>Py_TYPE(obj)).tp_clear is NULL
19
20def is_tp_traverse_null(obj):
21return (<PyTypeObject*>Py_TYPE(obj)).tp_traverse is NULL
22
23
24@cython.no_gc
25cdef class DisableGC:
26"""
27An extension type that has tp_clear and tp_traverse methods generated
28to test that it actually clears the references to NULL.
29
30>>> uut = DisableGC()
31>>> is_tp_clear_null(uut)
32True
33>>> is_tp_traverse_null(uut)
34True
35"""
36
37cdef public object requires_cleanup
38
39def __cinit__(self):
40self.requires_cleanup = (
41"Tuples to strings don't really need cleanup, cannot take part of cycles",)
42
43