diff options
author | Qiming Sun <osirpt.sun@gmail.com> | 2019-09-14 15:56:12 -0700 |
---|---|---|
committer | Qiming Sun <osirpt.sun@gmail.com> | 2019-09-14 15:56:12 -0700 |
commit | e2fa2ce5c06f902b7a50e1a7d34794cce0f076b7 (patch) | |
tree | de0083e2bfa00bf02e772fce4dff3decd10a0b00 /numpy/core/tests | |
parent | ce24b2e585635f62c7187e0c5eaa675bb6a94e84 (diff) | |
download | numpy-e2fa2ce5c06f902b7a50e1a7d34794cce0f076b7.tar.gz |
TEST: Add tests for .ctypes.data_as
Diffstat (limited to 'numpy/core/tests')
-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 |