Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 46d31ce

Browse files
authored
Merge pull request #26869 from charris/backport-26849
BUG: Mismatched allocation domains in ``PyArray_FillWithScalar``
2 parents c8c1e4e + d83e5ce commit 46d31ce

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

numpy/_core/src/multiarray/convert.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,9 @@ PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj)
408408
char *value = (char *)value_buffer_stack;
409409
PyArray_Descr *descr = PyArray_DESCR(arr);
410410

411-
if ((size_t)descr->elsize > sizeof(value_buffer_stack)) {
411+
if (PyDataType_ELSIZE(descr) > sizeof(value_buffer_stack)) {
412412
/* We need a large temporary buffer... */
413-
value_buffer_heap = PyObject_Calloc(1, descr->elsize);
413+
value_buffer_heap = PyMem_Calloc(1, PyDataType_ELSIZE(descr));
414414
if (value_buffer_heap == NULL) {
415415
PyErr_NoMemory();
416416
return -1;

numpy/_core/tests/test_multiarray.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,18 @@ def test_fill_readonly(self):
425425
with pytest.raises(ValueError, match=".*read-only"):
426426
a.fill(0)
427427

428+
def test_fill_subarrays(self):
429+
# NOTE:
430+
# This is also a regression test for a crash with PYTHONMALLOC=debug
431+
432+
dtype = np.dtype("2<i8, 2<i8, 2<i8")
433+
data = ([1, 2], [3, 4], [5, 6])
434+
435+
arr = np.empty(1, dtype=dtype)
436+
arr.fill(data)
437+
438+
assert_equal(arr, np.array(data, dtype=dtype))
439+
428440

429441
class TestArrayConstruction:
430442
def test_array(self):
@@ -3509,17 +3521,17 @@ def test_put(self):
35093521
bad_array = [1, 2, 3]
35103522
assert_raises(TypeError, np.put, bad_array, [0, 2], 5)
35113523

3512-
# when calling np.put, make sure an
3513-
# IndexError is raised if the
3524+
# when calling np.put, make sure an
3525+
# IndexError is raised if the
35143526
# array is empty
35153527
empty_array = np.asarray(list())
3516-
with pytest.raises(IndexError,
3528+
with pytest.raises(IndexError,
35173529
match="cannot replace elements of an empty array"):
35183530
np.put(empty_array, 1, 1, mode="wrap")
3519-
with pytest.raises(IndexError,
3531+
with pytest.raises(IndexError,
35203532
match="cannot replace elements of an empty array"):
35213533
np.put(empty_array, 1, 1, mode="clip")
3522-
3534+
35233535

35243536
def test_ravel(self):
35253537
a = np.array([[0, 1], [2, 3]])

0 commit comments

Comments
 (0)