Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 36d4da2

Browse filesBrowse files
committed
BUG: avoid negating unsigned integers in resize implementation
1 parent f15a116 commit 36d4da2
Copy full SHA for 36d4da2

File tree

Expand file treeCollapse file tree

2 files changed

+9
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+9
-1
lines changed

‎numpy/_core/fromnumeric.py

Copy file name to clipboardExpand all lines: numpy/_core/fromnumeric.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,8 @@ def resize(a, new_shape):
16071607
# First case must zero fill. The second would have repeats == 0.
16081608
return np.zeros_like(a, shape=new_shape)
16091609

1610-
repeats = -(-new_size // a.size) # ceil division
1610+
# ceiling division without negating new_size
1611+
repeats = ((new_size + a.size - 1) // a.size)
16111612
a = concatenate((a,) * repeats)[:new_size]
16121613

16131614
return reshape(a, new_shape)

‎numpy/_core/tests/test_numeric.py

Copy file name to clipboardExpand all lines: numpy/_core/tests/test_numeric.py
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ def test_negative_resize(self):
7979
with pytest.raises(ValueError, match=r"negative"):
8080
np.resize(A, new_shape=new_shape)
8181

82+
def test_unsigned_resize(self):
83+
# ensure unsigned integer sizes don't lead to underflows
84+
for dt_pair in [(np.int32, np.uint32), (np.int64, np.uint64)]:
85+
arr = np.array([[23, 95], [66, 37]])
86+
assert_array_equal(np.resize(arr, dt_pair[0](1)),
87+
np.resize(arr, dt_pair[1](1)))
88+
8289
def test_subclass(self):
8390
class MyArray(np.ndarray):
8491
__array_priority__ = 1.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.