From 73d7871970a951edd48e5c40bdc7609385ce61e6 Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Mon, 17 Sep 2018 09:08:42 +0300 Subject: MAINT: refactor design of recursive closures (#11910) --- numpy/core/shape_base.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'numpy/core/shape_base.py') 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): -- cgit v1.2.1