diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/function_base.py | 7 | ||||
-rw-r--r-- | numpy/core/tests/test_function_base.py | 36 |
2 files changed, 40 insertions, 3 deletions
diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py index 17a36eb4c..d6757bb74 100644 --- a/numpy/core/function_base.py +++ b/numpy/core/function_base.py @@ -5,7 +5,7 @@ import operator from . import numeric as _nx from .numeric import (result_type, NaN, shares_memory, MAY_SHARE_BOUNDS, - TooHardError) + TooHardError,asanyarray) __all__ = ['logspace', 'linspace', 'geomspace'] @@ -104,8 +104,9 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None): div = (num - 1) if endpoint else num # Convert float/complex array scalars to float, gh-3504 - start = start * 1.0 - stop = stop * 1.0 + # and make sure one can use variables that have an __array_interface__, gh-6634 + start = asanyarray(start) * 1.0 + stop = asanyarray(stop) * 1.0 dt = result_type(start, stop, float(num)) if dtype is None: diff --git a/numpy/core/tests/test_function_base.py b/numpy/core/tests/test_function_base.py index 9b20c4ff5..1b563dde2 100644 --- a/numpy/core/tests/test_function_base.py +++ b/numpy/core/tests/test_function_base.py @@ -262,6 +262,42 @@ class TestLinspace(TestCase): assert type(ls) is PhysicalQuantity2 assert_equal(ls, linspace(0.0, 1.0, 1)) + def test_array_interface(self): + # Regression test for https://github.com/numpy/numpy/pull/6659 + # Ensure that start/stop can be objects that implement + # __array_interface__ and are convertible to numeric scalars + + class Arrayish(object): + """ + A generic object that supports the __array_interface__ and hence + can in principle be converted to a numeric scalar, but is not + otherwise recognized as numeric, but also happens to support + multiplication by floats. + + Data should be an object that implements the buffer interface, + and contains at least 4 bytes. + """ + + def __init__(self, data): + self._data = data + + @property + def __array_interface__(self): + # Ideally should be `'shape': ()` but the current interface + # does not allow that + return {'shape': (1,), 'typestr': '<i4', 'data': self._data, + 'version': 3} + + def __mul__(self, other): + # For the purposes of this test any multiplication is an + # identity operation :) + return self + + one = Arrayish(array(1, dtype='<i4')) + five = Arrayish(array(5, dtype='<i4')) + + assert_equal(linspace(one, five), linspace(1, 5)) + def test_denormal_numbers(self): # Regression test for gh-5437. Will probably fail when compiled # with ICC, which flushes denormals to zero |