Skip to content

Commit 3822a18

Browse files
authored
Merge pull request #2742 from pygame-community/ankith26-pixelarray-fix
PixelArray raise error assigning sequence to pixel
2 parents 7b0beba + 90b4cb4 commit 3822a18

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src_c/pixelarray.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,12 @@ _pxarray_ass_item(pgPixelArrayObject *array, Py_ssize_t index, PyObject *value)
13181318
if (!tmparray) {
13191319
return -1;
13201320
}
1321+
if (!pgPixelArrayObject_Check(tmparray)) {
1322+
PyErr_SetString(
1323+
PyExc_ValueError,
1324+
"cannot assign a pixel sequence to a single pixel");
1325+
return -1;
1326+
}
13211327
retval =
13221328
_array_assign_sequence(tmparray, 0, tmparray->shape[0], value);
13231329
Py_DECREF(tmparray);

test/pixelarray_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,19 @@ def test_zero_size_surface(self):
13191319
weird_surface = pygame.Surface((0, 5))
13201320
self.assertRaises(ValueError, lambda: pygame.PixelArray(weird_surface))
13211321

1322+
def test_assign_seq_to_single(self):
1323+
"""
1324+
Regression test for https://github.com/pygame-community/pygame-ce/issues/2740
1325+
This usage should ValueError and not segfault (as list is to be interpreted as
1326+
pixel sequence, and not color)
1327+
"""
1328+
test = pygame.PixelArray(pygame.Surface([800, 800]))
1329+
with self.assertRaises(ValueError):
1330+
test[400][400] = [255, 255, 0]
1331+
1332+
with self.assertRaises(ValueError):
1333+
test[400, 400] = [255, 255, 0]
1334+
13221335

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

0 commit comments

Comments
 (0)