Skip to content

Commit 435c2cd

Browse files
authored
Merge pull request numpy#29774 from cibarbia05/fix-setbufsize-negative
BUG: np.setbufsize should raise ValueError for negative input
2 parents a49e31e + b5023db commit 435c2cd

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

numpy/_core/_ufunc_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ def setbufsize(size):
187187
8192
188188
189189
"""
190+
if size < 0:
191+
raise ValueError("buffer size must be non-negative")
190192
old = _get_extobj_dict()["bufsize"]
191193
extobj = _make_extobj(bufsize=size)
192194
_extobj_contextvar.set(extobj)

numpy/_core/tests/test_umath.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4708,6 +4708,18 @@ def test_reduceat():
47084708
np.setbufsize(ncu.UFUNC_BUFSIZE_DEFAULT)
47094709
assert_array_almost_equal(h1, h2)
47104710

4711+
def test_negative_value_raises():
4712+
with pytest.raises(ValueError, match="buffer size must be non-negative"):
4713+
np.setbufsize(-5)
4714+
4715+
old = np.getbufsize()
4716+
try:
4717+
prev = np.setbufsize(4096)
4718+
assert prev == old
4719+
assert np.getbufsize() == 4096
4720+
finally:
4721+
np.setbufsize(old)
4722+
47114723
def test_reduceat_empty():
47124724
"""Reduceat should work with empty arrays"""
47134725
indices = np.array([], 'i4')

0 commit comments

Comments
 (0)