8000 BUG: Let linspace accept input that has an array_interface. by kiwifb · Pull Request #6659 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Let linspace accept input that has an array_interface. #6659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions numpy/core/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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:
Expand Down
36 changes: 36 additions & 0 deletions numpy/core/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': '<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
Expand Down
0