Skip to content

Commit

Permalink
PixelArray raise error assigning sequence to pixel
Browse files Browse the repository at this point in the history
  • Loading branch information
ankith26 committed Apr 21, 2024
1 parent 1f8fe8d commit 90b4cb4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src_c/pixelarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,12 @@ _pxarray_ass_item(pgPixelArrayObject *array, Py_ssize_t index, PyObject *value)
if (!tmparray) {
return -1;
}
if (!pgPixelArrayObject_Check(tmparray)) {
PyErr_SetString(
PyExc_ValueError,
"cannot assign a pixel sequence to a single pixel");
return -1;
}
retval =
_array_assign_sequence(tmparray, 0, tmparray->shape[0], value);
Py_DECREF(tmparray);
Expand Down
13 changes: 13 additions & 0 deletions test/pixelarray_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,19 @@ def test_zero_size_surface(self):
weird_surface = pygame.Surface((0, 5))
self.assertRaises(ValueError, lambda: pygame.PixelArray(weird_surface))

def test_assign_seq_to_single(self):
"""
Regression test for https://github.com/pygame-community/pygame-ce/issues/2740
This usage should ValueError and not segfault (as list is to be interpreted as
pixel sequence, and not color)
"""
test = pygame.PixelArray(pygame.Surface([800, 800]))
with self.assertRaises(ValueError):
test[400][400] = [255, 255, 0]

with self.assertRaises(ValueError):
test[400, 400] = [255, 255, 0]


@unittest.skipIf(IS_PYPY, "pypy having issues")
class PixelArrayArrayInterfaceTest(unittest.TestCase, TestMixin):
Expand Down

0 comments on commit 90b4cb4

Please sign in to comment.