8000 BUG: Errors thrown by 0d arrays in setitem are silenced and replaced · numpy/numpy@8e26a5d · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e26a5d

Browse files
committed
BUG: Errors thrown by 0d arrays in setitem are silenced and replaced
Fixes gh-8461
1 parent 8899616 commit 8e26a5d

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

numpy/core/src/multiarray/arraytypes.c.src

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636
#include <limits.h>
3737
#include <assert.h>
3838

39+
/* check for sequences, but ignore the types numpy considers scalars */
40+
static NPY_INLINE npy_bool
41+
PySequence_NoString_Check(PyObject *op) {
42+
return
43+
PySequence_Check(op) &&
44+
!PyString_Check(op) &&
45+
!PyUnicode_Check(op) &&
46+
!PyArray_IsZeroDim(op);
47+
}
48+
3949
/*
4050
*****************************************************************************
4151
** PYTHON TYPES TO C TYPES **
@@ -223,8 +233,7 @@ static int
223233
if (PyErr_Occurred()) {
224234
PyObject *type, *value, *traceback;
225235
PyErr_Fetch(&type, &value, &traceback);
226-
if (PySequence_Check(op) && !PyString_Check(op) &&
227-
!PyUnicode_Check(op)) {
236+
if (PySequence_NoString_Check(op)) {
228237
PyErr_SetString(PyExc_ValueError,
229238
"setting an array element with a sequence.");
230239
Py_DECREF(type);
@@ -461,7 +470,7 @@ UNICODE_setitem(PyObject *op, void *ov, void *vap)
461470
return convert_to_scalar_and_retry(op, ov, vap, UNICODE_setitem);
462471
}
463472

464-
if (!PyBytes_Check(op) && !PyUnicode_Check(op) && PySequence_Check(op)) {
473+
if (PySequence_NoString_Check(op)) {
465474
PyErr_SetString(PyExc_ValueError,
466475
"setting an array element with a sequence");
467476
return -1;
@@ -552,7 +561,7 @@ STRING_setitem(PyObject *op, void *ov, void *vap)
552561
return convert_to_scalar_and_retry(op, ov, vap, STRING_setitem);
553562
}
554563

555-
if (!PyBytes_Check(op) && !PyUnicode_Check(op) && PySequence_Check(op)) {
564+
if (PySequence_NoString_Check(op)) {
556565
PyErr_SetString(PyExc_ValueError,
557566
"setting an array element with a sequence");
558567
return -1;

numpy/ma/tests/test_core.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
__author__ = "Pierre GF Gerard-Marchant"
1010

11+
import sys
1112
import warnings
1213
import pickle
1314
import operator
@@ -20,7 +21,7 @@
2021
import numpy.core.fromnumeric as fromnumeric
2122
import numpy.core.umath as umath
2223
from numpy.testing import (
23-
run_module_suite, assert_raises, assert_warns, suppress_warnings
24+
run_module_suite, assert_raises, assert_warns, suppress_warnings, dec
2425
)
2526
from numpy import ndarray
2627
from numpy.compat import asbytes, asbytes_nested
@@ -4836,6 +4837,28 @@ def test_immutable(self):
48364837
assert_raises(ValueError, operator.setitem, view.data, (), 1)
48374838
assert_raises(ValueError, operator.setitem, view.mask, (), False)
48384839

4840+
@dec.knownfailureif(sys.version_info.major == 2, "See gh-9751")
4841+
def test_coercion_int(self):
4842+
a_i = np.zeros((), int)
4843+
assert_raises(MaskError, operator.setitem, a_i, (), np.ma.masked)
4844+
4845+
def test_coercion_float(self):
4846+
a_f = np.zeros((), float)
4847+
assert_warns(UserWarning, operator.setitem, a_f, (), np.ma.masked)
4848+
assert_(np.isnan(a_f[()]))
4849+
4850+
@dec.knownfailureif(True, "See gh-9750")
4851+
def test_coercion_unicode(self):
4852+
a_u = np.zeros((), 'U10')
4853+
a_u[()] = np.ma.masked
4854+
assert_equal(a_u[()], u'--')
4855+
4856+
@dec.knownfailureif(True, "See gh-9750")
4857+
def test_coercion_bytes(self):
4858+
a_b = np.zeros((), 'S10')
4859+
a_b[()] = np.ma.masked
4860+
assert_equal(a_b[()], b'--')
4861+
48394862

48404863
def test_masked_array():
48414864
a = np.ma.array([0, 1, 2, 3], mask=[0, 0, 1, 0])

0 commit comments

Comments
 (0)
0