summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorQiming Sun <osirpt.sun@gmail.com>2019-09-15 18:23:50 -0700
committerQiming Sun <osirpt.sun@gmail.com>2019-09-15 18:23:50 -0700
commit7cc8bfb32f8f26d47cae4b342c009ab13cfb313b (patch)
tree1b24085ee0915e487fe3db149176aad4c9106b86 /numpy
parente2fa2ce5c06f902b7a50e1a7d34794cce0f076b7 (diff)
downloadnumpy-7cc8bfb32f8f26d47cae4b342c009ab13cfb313b.tar.gz
MAINT: Remove _get_void_ptr function
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/_internal.py21
1 files changed, 4 insertions, 17 deletions
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py
index 28cbcc810..7b8ab9215 100644
--- a/numpy/core/_internal.py
+++ b/numpy/core/_internal.py
@@ -271,29 +271,12 @@ class _unsafe_first_element_pointer(object):
return i
-def _get_void_ptr(arr):
- """
- Get a `ctypes.c_void_p` to arr.data, that keeps a reference to the array
- """
- import numpy as np
- # convert to a 0d array that has a data pointer referrign to the start
- # of arr. This holds a reference to arr.
- simple_arr = np.asarray(_unsafe_first_element_pointer(arr))
-
- # create a `char[0]` using the same memory.
- c_arr = (ctypes.c_char * 0).from_buffer(simple_arr)
-
- # finally cast to void*
- return ctypes.cast(ctypes.pointer(c_arr), ctypes.c_void_p)
-
-
class _ctypes(object):
def __init__(self, array, ptr=None):
self._arr = array
if ctypes:
self._ctypes = ctypes
- # get a void pointer to the buffer, which keeps the array alive
self._data = self._ctypes.c_void_p(ptr)
else:
# fake a pointer-like object that holds onto the reference
@@ -316,6 +299,10 @@ class _ctypes(object):
The returned pointer will keep a reference to the array.
"""
+ # _ctypes.cast function causes a circular reference of self._data in
+ # self._data._objects. Attributes of self._data cannot be released
+ # until gc.collect is called. Make a copy of the pointer first then let
+ # it hold the array reference.
ptr = self._ctypes.cast(self._data, obj)
ptr._arr = self._arr
return ptr