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/core/shape_base.py | |
parent | f49c0169f5a62d4bfa62c791e2a4e2b78ddf7517 (diff) | |
download | numpy-73d7871970a951edd48e5c40bdc7609385ce61e6.tar.gz |
MAINT: refactor design of recursive closures (#11910)
Diffstat (limited to 'numpy/core/shape_base.py')
-rw-r--r-- | numpy/core/shape_base.py | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/numpy/core/shape_base.py b/numpy/core/shape_base.py index 319c25088..3e023c87b 100644 --- a/numpy/core/shape_base.py +++ b/numpy/core/shape_base.py @@ -7,6 +7,7 @@ __all__ = ['atleast_1d', 'atleast_2d', 'atleast_3d', 'block', 'hstack', from . import numeric as _nx from .numeric import array, asanyarray, newaxis from .multiarray import normalize_axis_index +from ._internal import recursive def atleast_1d(*arys): """ @@ -435,24 +436,19 @@ def _block(arrays, max_depth, result_ndim): # ones to `a.shape` as necessary return array(a, ndmin=ndim, copy=False, subok=True) - def block_recursion(arrays, depth=0): + @recursive + def block_recursion(self, arrays, depth=0): if depth < max_depth: if len(arrays) == 0: raise ValueError('Lists cannot be empty') - arrs = [block_recursion(arr, depth+1) for arr in arrays] + arrs = [self(arr, depth+1) for arr in arrays] return _nx.concatenate(arrs, axis=-(max_depth-depth)) else: # We've 'bottomed out' - arrays is either a scalar or an array # type(arrays) is not list return atleast_nd(arrays, result_ndim) - try: - return block_recursion(arrays) - finally: - # 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: - block_recursion = None + return block_recursion(arrays) def block(arrays): |