8000 BUG: `take` casting logic with an `out=` argument by jaimefrio · Pull Request #4246 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: take casting logic with an out= argument #4246

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

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 17 additions & 1 deletion numpy/core/src/multiarray/item_selection.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis,
}
else {
int flags = NPY_ARRAY_CARRAY |
NPY_ARRAY_UPDATEIFCOPY;
NPY_ARRAY_UPDATEIFCOPY |
NPY_ARRAY_FORCECAST;

if ((PyArray_NDIM(out) != nd) ||
!PyArray_CompareLists(PyArray_DIMS(out), shape, nd)) {
Expand All @@ -104,8 +105,23 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis,
*/
flags |= NPY_ARRAY_ENSURECOPY;
}

dtype = PyArray_DESCR(self);
/*
* The out array is converted to an array obj of type dtype and after
* the take operation is finished is update-if-copy-ed back. An
* explicit check that dtype can be safely cast to the type of out
* is needed, as the constructor of obj would check the opposite
* casting, were it not disabled by the NPY_ARRAY_FORCECAST flag.
*/
if (PyArray_CanCastTypeTo(dtype, PyArray_DESCR(out),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking this check is incorrect. It was previously allowed to cast, say, float64 to int8, and ufuncs also allow such casting. The forcecast going the other way should be the only change needed as it provides a type compatible work array, which is then copied back on success or discarded on failure. The test could check that out remains unchanged when the mode is raise and an indexing error occurs.

NPY_SAFE_CASTING) == 0) {
PyErr_SetString(PyExc_TypeError,
"output cannot be safely cast to out dtype");
goto fail;
}
Py_INCREF(dtype);

obj = (PyArrayObject *)PyArray_FromArray(out, dtype, flags);
if (obj == NULL) {
goto fail;
Expand Down
13 changes: 12 additions & 1 deletion numpy/core/tests/test_item_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
from numpy.testing import *
import sys, warnings
import itertools


class TestTake(TestCase):
Expand Down Expand Up @@ -45,7 +46,6 @@ def test_simple(self):
res = ta.take(index_array, mode=mode, axis=1)
assert_(res.shape == (2,) + index_array.shape)


def test_refcounting(self):
objects = [object() for i in range(10)]
for mode in ('raise', 'clip', 'wrap'):
Expand All @@ -60,6 +60,17 @@ def test_refcounting(self):
del a
assert_(all(sys.getrefcount(o) == 3 for o in objects))

def test_casting(self):
types = ''.join((np.typecodes['AllInteger'], np.typecodes['Float']))
modes = ['raise', 'wrap', 'clip']
for from_, to_, mode in itertools.product(types, types, modes):
a = np.array([0, 1, 2], dtype=np.dtype(from_))
b = np.empty((3,), dtype=np.dtype(to_))
if np.can_cast(from_, to_):
a.take([0, 1, 2], out=b, mode=mode)
else:
assert_raises(TypeError, a.take, [0, 1, 2], out=b, mode=mode)

def test_unicode_mode(self):
d = np.arange(10)
k = b'\xc3\xa4'.decode("UTF8")
Expand Down
0