Skip to content

Commit

Permalink
Merge pull request #19392 from charris/backport-19380
Browse files Browse the repository at this point in the history
BUG: Fix NULL special case in object-to-any cast code
  • Loading branch information
charris committed Jul 1, 2021
2 parents 52247be + 9b38c1c commit c1ba1dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions numpy/core/src/multiarray/dtype_transfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,11 @@ strided_to_strided_object_to_any(

while (N > 0) {
memcpy(&src_ref, src, sizeof(src_ref));
if (PyArray_Pack(data->descr, dst, src_ref) < 0) {
if (PyArray_Pack(data->descr, dst, src_ref ? src_ref : Py_None) < 0) {
return -1;
}

if (data->move_references) {
if (data->move_references && src_ref != NULL) {
Py_DECREF(src_ref);
memset(src, 0, sizeof(src_ref));
}
Expand Down
17 changes: 17 additions & 0 deletions numpy/core/tests/test_casting_unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,20 @@ def test_void_and_structured_with_subarray(self, casting):
expected = casting == "unsafe"
assert np.can_cast("V4", dtype, casting=casting) == expected
assert np.can_cast(dtype, "V4", casting=casting) == expected

@pytest.mark.parametrize("dtype", np.typecodes["All"])
def test_object_casts_NULL_None_equivalence(self, dtype):
# None to <other> casts may succeed or fail, but a NULL'ed array must
# behave the same as one filled with None's.
arr_normal = np.array([None] * 5)
arr_NULLs = np.empty_like([None] * 5)
# If the check fails (maybe it should) the test would lose its purpose:
assert arr_NULLs.tobytes() == b"\x00" * arr_NULLs.nbytes

try:
expected = arr_normal.astype(dtype)
except TypeError:
with pytest.raises(TypeError):
arr_NULLs.astype(dtype)
else:
assert_array_equal(expected, arr_NULLs.astype(dtype))

0 comments on commit c1ba1dc

Please sign in to comment.