diff options
author | Matti Picus <matti.picus@gmail.com> | 2018-09-17 09:08:42 +0300 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2018-09-16 23:08:42 -0700 |
commit | 73d7871970a951edd48e5c40bdc7609385ce61e6 (patch) | |
tree | 7e71e96faf55f1d716417c5f057323152d97bbe6 /numpy/lib/npyio.py | |
parent | f49c0169f5a62d4bfa62c791e2a4e2b78ddf7517 (diff) | |
download | numpy-73d7871970a951edd48e5c40bdc7609385ce61e6.tar.gz |
MAINT: refactor design of recursive closures (#11910)
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r-- | numpy/lib/npyio.py | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index d8cfbf769..9a7b244ac 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -13,6 +13,7 @@ import numpy as np from . import format from ._datasource import DataSource from numpy.core.multiarray import packbits, unpackbits +from numpy.core._internal import recursive from ._iotools import ( LineSplitter, NameValidator, StringConverter, ConverterError, ConverterLockError, ConversionWarning, _is_string_like, @@ -944,7 +945,8 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, fencoding = locale.getpreferredencoding() # not to be confused with the flatten_dtype we import... - def flatten_dtype_internal(dt): + @recursive + def flatten_dtype_internal(self, dt): """Unpack a structured data-type, and produce re-packing info.""" if dt.names is None: # If the dtype is flattened, return. @@ -964,7 +966,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, packing = [] for field in dt.names: tp, bytes = dt.fields[field] - flat_dt, flat_packing = flatten_dtype_internal(tp) + flat_dt, flat_packing = self(tp) types.extend(flat_dt) # Avoid extra nesting for subarrays if tp.ndim > 0: @@ -973,7 +975,8 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, packing.append((len(flat_dt), flat_packing)) return (types, packing) - def pack_items(items, packing): + @recursive + def pack_items(self, items, packing): """Pack items into nested lists based on re-packing info.""" if packing is None: return items[0] @@ -985,7 +988,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, start = 0 ret = [] for length, subpacking in packing: - ret.append(pack_items(items[start:start+length], subpacking)) + ret.append(self(items[start:start+length], subpacking)) start += length return tuple(ret) @@ -1111,11 +1114,6 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, finally: if fown: fh.close() - # recursive closures have a cyclic reference to themselves, which - # requires gc to collect (gh-10620). To avoid this problem, for - # performance and PyPy friendliness, we break the cycle: - flatten_dtype_internal = None - pack_items = None if X is None: X = np.array([], dtype) |