diff options
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 1b698b517..715ef203b 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -8007,6 +8007,35 @@ class TestCTypes(object): break_cycles() assert_(arr_ref() is None, "unknowable whether ctypes pointer holds a reference") + def test_ctypes_as_parameter_holds_reference(self): + arr = np.array([None]).copy() + + arr_ref = weakref.ref(arr) + + ctypes_ptr = arr.ctypes._as_parameter_ + + # `ctypes_ptr` should hold onto `arr` + del arr + break_cycles() + assert_(arr_ref() is not None, "ctypes pointer did not hold onto a reference") + + # but when the `ctypes_ptr` object dies, so should `arr` + del ctypes_ptr + break_cycles() + assert_(arr_ref() is None, "unknowable whether ctypes pointer holds a reference") + + def test_ctypes_data_as_no_circular_reference(self): + # check array reference count based on the buffer the array points to + data = b'\x00' * 128 + ref_count = sys.getrefcount(data) + + arr = np.frombuffer(data) + ctypes_ptr = arr.ctypes.data_as(ctypes.c_void_p) + + del arr, ctypes_ptr + # Do not call gc before checking circular reference + assert_(sys.getrefcount(data) == ref_count, "Found ctypes pointer circular reference") + class TestWritebackIfCopy(object): # all these tests use the WRITEBACKIFCOPY mechanism |