Skip to content

Commit 3b79bb4

Browse files
committed
Remove redundant overflow checks, properly handle pixel_size < 1, and implement test for invalid arguments
1 parent 78296a3 commit 3b79bb4

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src_c/transform.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,15 +4246,9 @@ surf_pixelate(PyObject *self, PyObject *args, PyObject *kwargs)
42464246
double testWidth = round((double)src->surf->w / pixel_size);
42474247
double testHeight = round((double)src->surf->h / pixel_size);
42484248

4249-
if (testWidth > INT_MAX || testWidth <= 0) {
4250-
PyErr_SetString(PyExc_OverflowError,
4251-
"Cannot scale width outside the range (0, INT_MAX]");
4252-
return NULL;
4253-
}
4254-
4255-
if (testHeight > INT_MAX || testHeight <= 0) {
4256-
PyErr_SetString(PyExc_OverflowError,
4257-
"Cannot scale height outside the range (0, INT_MAX]");
4249+
if (pixel_size < 1) {
4250+
PyErr_SetString(PyExc_ValueError,
4251+
"Pixel size must be a nonnegative integer");
42584252
return NULL;
42594253
}
42604254

test/transform_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,21 @@ def test_pixelate(self):
18921892

18931893
self.assertTrue(surfaces_have_same_pixels(square_pixelated, square_resized))
18941894

1895+
# test a variety of arguments raise an exception
1896+
for arg in (0, -1):
1897+
with self.assertRaises(ValueError, msg=f"Running with pixel_size = {arg}"):
1898+
pygame.transform.pixelate(image, arg)
1899+
1900+
for arg in (-1_000_000_000_000_000_000, 1_000_000_000_000_000_000):
1901+
with self.assertRaises(
1902+
OverflowError, msg=f"Running with pixel_size = {arg}"
1903+
):
1904+
pygame.transform.pixelate(image, arg)
1905+
1906+
for arg in ("one", 1.0, None):
1907+
with self.assertRaises(TypeError, msg=f"Running with pixel_size = {arg}"):
1908+
pygame.transform.pixelate(image, arg)
1909+
18951910

18961911
class TransformDisplayModuleTest(unittest.TestCase):
18971912
def setUp(self):

0 commit comments

Comments
 (0)