Skip to content

Commit 931758e

Browse files
committed
BUG: Make bool(void_scalar) and void_scalar.astype(bool) consistent
Caused by void scalars decaying to 1d uint8 arrays before casting - `getitem` is dangerous for intermediate results Works towards #9847
1 parent e64699d commit 931758e

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

‎doc/release/1.14.0-notes.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ This is for pytest compatibility in the case of duplicate test file names in
128128
the different directories. As a result, ``run_module_suite`` no longer works,
129129
i.e., ``python <path-to-test-file>`` results in an error.
130130

131+
``.astype(bool)`` on unstructured void arrays now calls ``bool`` on each element
132+
--------------------------------------------------------------------------------
133+
On Python 2, ``void_array.astype(bool)`` would always return an array of
134+
``True``, unless the dtype is ``V0``. On Python 3, this operation would usually
135+
crash. Going forwards, `astype` matches the behavior of ``bool(np.void)``,
136+
considering a buffer of all zeros as false, and anything else as true.
137+
Checks for ``V0`` can still be done with ``arr.dtype.itemsize == 0``.
138+
131139
``MaskedArray.squeeze`` never returns ``np.ma.masked``
132140
------------------------------------------------------
133141
``np.squeeze`` is documented as returning a view, but the masked variant would

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,12 +1561,12 @@ static void
15611561
PyArrayObject *aip = vaip;
15621562

15631563
npy_intp i;
1564-
PyObject *temp = NULL, *new;
15651564
int skip = PyArray_DESCR(aip)->elsize;
15661565
int oskip = @oskip@;
15671566

15681567
for (i = 0; i < n; i++, ip+=skip, op+=oskip) {
1569-
temp = @from@_getitem(ip, aip);
1568+
PyObject *new;
1569+
PyObject *temp = PyArray_Scalar(ip, PyArray_DESCR(aip), (PyObject *)aip);
15701570
if (temp == NULL) {
15711571
return;
15721572
}
@@ -1621,12 +1621,11 @@ static void
16211621
PyArrayObject *aip = vaip;
16221622

16231623
npy_intp i;
1624-
PyObject *temp = NULL;
16251624
int skip = PyArray_DESCR(aip)->elsize;
16261625
int oskip = @oskip@;
16271626

16281627
for (i = 0; i < n; i++, ip+=skip, op+=oskip) {
1629-
temp = @from@_getitem(ip, aip);
1628+
PyObject *temp = PyArray_Scalar(ip, PyArray_DESCR(aip), (PyObject *)aip);
16301629
if (temp == NULL) {
16311630
return;
16321631
}

‎numpy/core/tests/test_multiarray.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,37 @@ def test_count_nonzero_unaligned(self):
12031203
a[:o] = False
12041204
assert_equal(np.count_nonzero(a), builtins.sum(a.tolist()))
12051205

1206+
def _test_cast_from_flexible(self, dtype):
1207+
# empty string -> false
1208+
for n in range(3):
1209+
v = np.array(b'', (dtype, n))
1210+
assert_equal(bool(v), False)
1211+
assert_equal(bool(v[()]), False)
1212+
assert_equal(v.astype(bool), False)
1213+
assert_(isinstance(v.astype(bool), np.ndarray))
1214+
assert_(v[()].astype(bool) is np.False_)
1215+
1216+
# anything else -> true
1217+
for n in range(1, 4):
1218+
for val in [b'a', b'0', b' ']:
1219+
v = np.array(val, (dtype, n))
1220+
assert_equal(bool(v), True)
1221+
assert_equal(bool(v[()]), True)
1222+
assert_equal(v.astype(bool), True)
1223+
assert_(isinstance(v.astype(bool), np.ndarray))
1224+
assert_(v[()].astype(bool) is np.True_)
1225+
1226+
def test_cast_from_void(self):
1227+
self._test_cast_from_flexible(np.void)
1228+
1229+
@dec.knownfailureif(True, "See gh-9847")
1230+
def test_cast_from_unicode(self):
1231+
self._test_cast_from_flexible(np.unicode_)
1232+
1233+
@dec.knownfailureif(True, "See gh-9847")
1234+
def test_cast_from_bytes(self):
1235+
self._test_cast_from_flexible(np.bytes_)
1236+
12061237

12071238
class TestZeroSizeFlexible(object):
12081239
@staticmethod

0 commit comments

Comments
 (0)