8000 BUG: Enforce integer limitation in concatenate by seberg · Pull Request #29231 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Enforce integer limitation in concatenate #29231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions numpy/_core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,17 @@ PyArray_ConcatenateInto(PyObject *op,
}

/* Convert the input list into arrays */
narrays = PySequence_Size(op);
if (narrays < 0) {
Py_ssize_t narrays_true = PySequence_Size(op);
if (narrays_true < 0) {
return NULL;
}
else if (narrays_true > NPY_MAX_INT) {
PyErr_Format(PyExc_ValueError,
"concatenate() only supports up to %d arrays but got %zd.",
NPY_MAX_INT, narrays_true);
return NULL;
}
narrays = (int)narrays_true;
arrays = PyArray_malloc(narrays * sizeof(arrays[0]));
if (arrays == NULL) {
PyErr_NoMemory();
Expand Down
14 changes: 14 additions & 0 deletions numpy/_core/tests/test_shape_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import pytest

import numpy as np
Expand Down Expand Up @@ -29,6 +31,7 @@
assert_raises,
assert_raises_regex,
)
from numpy.testing._private.utils import requires_memory


class TestAtleast1d:
Expand Down Expand Up @@ -290,6 +293,17 @@ def test_exceptions(self):
# No arrays to concatenate raises ValueError
assert_raises(ValueError, concatenate, ())

@pytest.mark.slow
@pytest.mark.skipif(sys.maxsize < 2**32, reason="only problematic on 64bit platforms")
@requires_memory(2 * np.iinfo(np.intc).max)
def test_huge_list_error(self):
a = np.array([1])
max_int = np.iinfo(np.intc).max
arrs = (a,) * (max_int + 1)
msg = fr"concatenate\(\) only supports up to {max_int} arrays but got {max_int + 1}."
with pytest.raises(ValueError, match=msg):
np.concatenate(arrs)

def test_concatenate_axis_None(self):
a = np.arange(4, dtype=np.float64).reshape((2, 2))
b = list(range(3))
Expand Down
Loading
0