diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py index 17a36eb4c348..d6757bb74743 100644 --- a/numpy/core/function_base.py +++ b/numpy/core/function_base.py @@ -5,7 +5,7 @@ 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 0fabb25886e8..8b4f5b4ac6d3 100644 --- a/numpy/core/tests/test_function_base.py +++ b/numpy/core/tests/test_function_base.py @@ -260,6 +260,42 @@ def test_subclass(self): 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': '