8000 MAINT: Improve memory usage in PEP3118 format parsing by eric-wieser · Pull Request #11535 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Improve memory usage in PEP3118 format parsing #11535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions numpy/core/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,46 +444,46 @@ def _view_is_safe(oldtype, newtype):
}
_pep3118_standard_typechars = ''.join(_pep3118_standard_map.keys())

def _dtype_from_pep3118(spec):

class Stream(object):
def __init__(self, s):
self.s = s
self.byteorder = '@'
class _Stream(object):
def __init__(self, s):
self.s = s
self.byteorder = '@'

def advance(self, n):
res = self.s[:n]
self.s = self.s[n:]
return res
def advance(self, n):
res = self.s[:n]
self.s = self.s[n:]
return res

def consume(self, c):
if self.s[:len(c)] == c:
self.advance(len(c))
return True
return False

def consume_until(self, c):
if callable(c):
i = 0
while i < len(self.s) and not c(self.s[i]):
i = i + 1
return self.advance(i)
else:
i = self.s.index(c)
res = self.advance(i)
self.advance(len(c))
return res
def consume(self, c):
if self.s[:len(c)] == c:
self.advance(len(c))
return True
return False

@property
def next(self):
return self.s[0]
def consume_until(self, c):
if callable(c):
i = 0
while i < len(self.s) and not c(self.s[i]):
i = i + 1
return self.advance(i)
else:
i = self.s.index(c)
res = self.advance(i)
self.advance(len(c))
return res

def __bool__(self):
return bool(self.s)
__nonzero__ = __bool__
@property
def next(self):
return self.s[0]

stream = Stream(spec)
def __bool__(self):
return bool(self.s)
__nonzero__ = __bool__


def _dtype_from_pep3118(spec):
stream = _Stream(spec)
dtype, align = __dtype_from_pep3118(stream, is_subdtype=False)
return dtype

Expand Down
20 changes: 20 additions & 0 deletions numpy/tests/test_ctypeslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,23 @@ def check(x):
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
0