summaryrefslogtreecommitdiff
path: root/numpy/tests
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2018-07-09 06:11:40 +0100
committerEric Wieser <wieser.eric@gmail.com>2018-07-23 06:55:39 -0700
commite0fb54f5751e93bcb36eccc00681a9afc35e1c37 (patch)
tree904cea6452cb5651e429db327417a834f813f8cb /numpy/tests
parenta52634d7ea38f74ba286580f96c35190b2b3b549 (diff)
downloadnumpy-e0fb54f5751e93bcb36eccc00681a9afc35e1c37.tar.gz
MAINT: Improve memory usage in PEP3118 format parsing
Previously a local `Stream` class would be defined every time a format needed parsing. Classes in cpython create reference cycles, which create load on the GC. This may or may not resolve gh-6511
Diffstat (limited to 'numpy/tests')
-rw-r--r--numpy/tests/test_ctypeslib.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/numpy/tests/test_ctypeslib.py b/numpy/tests/test_ctypeslib.py
index 75ce9c8ca..62793a9d6 100644
--- a/numpy/tests/test_ctypeslib.py
+++ b/numpy/tests/test_ctypeslib.py
@@ -170,3 +170,23 @@ class TestAsArray(object):
check(as_array(pointer(c_array), shape=()))
check(as_array(pointer(c_array[0]), shape=(2,)))
check(as_array(pointer(c_array[0][0]), shape=(2, 3)))
+
+ def test_reference_cycles(self):
+ # related to gh-6511
+ import ctypes
+
+ # create array to work with
+ # don't use int/long to avoid running into bpo-10746
+ N = 100
+ a = np.arange(N, dtype=np.short)
+
+ # get pointer to array
+ pnt = np.ctypeslib.as_ctypes(a)
+
+ with np.testing.assert_no_gc_cycles():
+ # decay the array above to a pointer to its first element
+ newpnt = ctypes.cast(pnt, ctypes.POINTER(ctypes.c_short))
+ # and construct an array using this data
+ b = np.ctypeslib.as_array(newpnt, (N,))
+ # now delete both, which should cleanup both objects
+ del newpnt, b