TST: Extract a helper function to test for reference cycles by eric-wieser · Pull Request #10891 · numpy/numpy
An example of this in use:
In [1]: from numpy.testing import assert_no_gc_cycles In [2]: def make_array_ctype(shape): ...: import ctypes ...: ct = ctypes.c_uint8 ...: for i in shape: ...: ct = i * ct ...: In [3]: assert_no_gc_cycles(make_array_ctype, (1,)) AssertionError: Reference cycles were found when calling make_array_ctype: 7 objects were collected, of which 6 are shown below: tuple object with id=2822255556536: (<class '_ctypes.Array'>,) PyCArrayType object with id=2822226500408: <class '__main__.c_ubyte_Array_1'> getset_descriptor object with id=2822252062256: <attribute '__dict__' of 'c_ubyte_Array_1' objects> getset_descriptor object with id=2822252062184: <attribute '__weakref__' of 'c_ubyte_Array_1' objects> tuple object with id=2822243712440: (<class '__main__.c_ubyte_Array_1'>, <class '_ctypes.Array'>, <class '_ctypes._CData'>, <class 'object'>) StgDict object with id=2822226211928: {'__dict__': <attribute '__dict__' of 'c_ubyte_Array_1' objects>, '__doc__': None, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'c_ubyte_Array_1' objects>, '_length_': 1, '_type_': <class 'ctypes.c_ubyte'>}
Not clear to me if this is a ctypes bug, or if it is by design