diff options
author | Marten van Kerkwijk <mhvk@astro.utoronto.ca> | 2014-08-27 14:46:07 -0400 |
---|---|---|
committer | Marten van Kerkwijk <mhvk@astro.utoronto.ca> | 2014-08-27 17:04:47 -0400 |
commit | e8590311a7b312711c7a4f40c1a15496e34d0ee6 (patch) | |
tree | dddb7cc85b34926dc93689bac9f48c8719e4a2cf /numpy/lib/stride_tricks.py | |
parent | 26a02cd702d9ccfc48978dcf81c80225f324bf3b (diff) | |
download | numpy-e8590311a7b312711c7a4f40c1a15496e34d0ee6.tar.gz |
Convert as_strided input to array first
Diffstat (limited to 'numpy/lib/stride_tricks.py')
-rw-r--r-- | numpy/lib/stride_tricks.py | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/numpy/lib/stride_tricks.py b/numpy/lib/stride_tricks.py index c15bc5167..b81307a65 100644 --- a/numpy/lib/stride_tricks.py +++ b/numpy/lib/stride_tricks.py @@ -23,6 +23,8 @@ class DummyArray(object): def as_strided(x, shape=None, strides=None, subok=False): """ Make an ndarray from the given array with the given shape and strides. """ + # first convert input to array, possibly keeping subclass + x = np.array(x, copy=False, subok=subok) interface = dict(x.__array_interface__) if shape is not None: interface['shape'] = tuple(shape) @@ -32,11 +34,13 @@ def as_strided(x, shape=None, strides=None, subok=False): # Make sure dtype is correct in case of custom dtype if array.dtype.kind == 'V': array.dtype = x.dtype - if subok and isinstance(x, np.ndarray) and type(x) is not type(array): + if type(x) is not type(array): + # if input was an ndarray subclass and subclasses were OK, + # then view the result as that subclass. array = array.view(type=type(x)) - # we have done something akin to a view from x, so we should let a - # possible subclass finalize (if it has it implemented) - if callable(getattr(array, '__array_finalize__', None)): + # Since we have done something akin to a view from x, we should let + # the subclass finalize (if it has it implemented, i.e., is not None). + if array.__array_finalize__: array.__array_finalize__(x) return array |