Skip to content

Commit b3721c5

Browse files
committed
BUG: Fix PyArray_ZeroContiguousBuffer (resize) with struct dtypes
We allow the structured dtype to return NULL for the zero fill function to indicate that a simple memset is sufficient. Also simplifies error handling a bit. The get_fill_zero_loop function must clean up on error and not return references if returns a `NULL` loop.
1 parent be296e2 commit b3721c5

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

numpy/_core/src/multiarray/refcount.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,23 @@ PyArray_ZeroContiguousBuffer(
8383
if (get_fill_zero_loop(
8484
NULL, descr, aligned, descr->elsize, &(zero_info.func),
8585
&(zero_info.auxdata), &flags_unused) < 0) {
86-
goto fail;
86+
return -1;
8787
}
8888
}
8989
else {
90+
assert(zero_info.func == NULL);
91+
}
92+
if (zero_info.func == NULL) {
9093
/* the multiply here should never overflow, since we already
9194
checked if the new array size doesn't overflow */
9295
memset(data, 0, size*stride);
93-
NPY_traverse_info_xfree(&zero_info);
9496
return 0;
9597
}
9698

9799
int res = zero_info.func(
98100
NULL, descr, data, size, stride, zero_info.auxdata);
99101
NPY_traverse_info_xfree(&zero_info);
100102
return res;
101-
102-
fail:
103-
NPY_traverse_info_xfree(&zero_info);
104-
return -1;
105103
}
106104

107105

numpy/_core/tests/test_multiarray.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9174,6 +9174,12 @@ def test_resize(self):
91749174
d.resize(150)
91759175
assert_(old < sys.getsizeof(d))
91769176

9177+
@pytest.mark.parametrize("dtype", ["u4,f4", "u4,O"])
9178+
def test_resize_structured(self, dtype):
9179+
a = np.array([(0, 0.0) for i in range(5)], dtype=dtype)
9180+
a.resize(1000)
9181+
assert_array_equal(a, np.zeros(1000, dtype=dtype))
9182+
91779183
def test_error(self):
91789184
d = np.ones(100)
91799185
assert_raises(TypeError, d.__sizeof__, "a")

0 commit comments

Comments
 (0)