Skip to content

Commit b8ef17f

Browse files
authored
Merge pull request #2392 from pygame-community/ankith26-colorspace-fix
Deprecate passing out of range sequence lengths to colorspace setters
2 parents 9421000 + 464631c commit b8ef17f

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src_c/color.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,16 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure)
11441144
return -1;
11451145
}
11461146

1147+
if (PySequence_Size(value) > 4) {
1148+
if (PyErr_WarnEx(
1149+
PyExc_DeprecationWarning,
1150+
"Passing sequences of size larger than 4 is deprecated, doing "
1151+
"this will error in a future version",
1152+
1) == -1) {
1153+
return -1;
1154+
}
1155+
}
1156+
11471157
/* H */
11481158
item = PySequence_GetItem(value, 0);
11491159
if (!item || !_get_double(item, &(hsva[0])) || hsva[0] < 0 ||
@@ -1309,6 +1319,16 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure)
13091319
return -1;
13101320
}
13111321

1322+
if (PySequence_Size(value) > 4) {
1323+
if (PyErr_WarnEx(
1324+
PyExc_DeprecationWarning,
1325+
"Passing sequences of size larger than 4 is deprecated, doing "
1326+
"this will error in a future version",
1327+
1) == -1) {
1328+
return -1;
1329+
}
1330+
}
1331+
13121332
/* H */
13131333
item = PySequence_GetItem(value, 0);
13141334
if (!item || !_get_double(item, &(hsla[0])) || hsla[0] < 0 ||
@@ -1469,6 +1489,21 @@ _color_set_i1i2i3(pgColorObject *color, PyObject *value, void *closure)
14691489

14701490
DEL_ATTR_NOT_SUPPORTED_CHECK("i1i2i3", value);
14711491

1492+
if (!PySequence_Check(value) || PySequence_Size(value) < 3) {
1493+
PyErr_SetString(PyExc_ValueError, "invalid I1I2I3 value");
1494+
return -1;
1495+
}
1496+
1497+
if (PySequence_Size(value) > 3) {
1498+
if (PyErr_WarnEx(
1499+
PyExc_DeprecationWarning,
1500+
"Passing sequences of size larger than 3 is deprecated, doing "
1501+
"this will error in a future version",
1502+
1) == -1) {
1503+
return -1;
1504+
}
1505+
}
1506+
14721507
/* I1 */
14731508
item = PySequence_GetItem(value, 0);
14741509
if (!item || !_get_double(item, &(i1i2i3[0])) || i1i2i3[0] < 0 ||
@@ -1536,6 +1571,21 @@ _color_set_cmy(pgColorObject *color, PyObject *value, void *closure)
15361571

15371572
DEL_ATTR_NOT_SUPPORTED_CHECK("cmy", value);
15381573

1574+
if (!PySequence_Check(value) || PySequence_Size(value) < 3) {
1575+
PyErr_SetString(PyExc_ValueError, "invalid CMY value");
1576+
return -1;
1577+
}
1578+
1579+
if (PySequence_Size(value) > 3) {
1580+
if (PyErr_WarnEx(
1581+
PyExc_DeprecationWarning,
1582+
"Passing sequences of size larger than 3 is deprecated, doing "
1583+
"this will error in a future version",
1584+
1) == -1) {
1585+
return -1;
1586+
}
1587+
}
1588+
15391589
/* I1 */
15401590
item = PySequence_GetItem(value, 0);
15411591
if (!item || !_get_double(item, &(cmy[0])) || cmy[0] < 0 || cmy[0] > 1) {

test/color_test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,14 @@ def test_from_cmy(self):
782782
self.assertEqual(expected_cmy, cmy)
783783
self.assertEqual(expected_cmy, cmy_tuple)
784784

785+
with self.assertWarns(DeprecationWarning):
786+
self.assertEqual(
787+
expected_cmy, pygame.Color.from_cmy(0.5, 0.5, 0.5, "lel", "foo")
788+
)
789+
790+
with self.assertWarns(DeprecationWarning):
791+
self.assertEqual(expected_cmy, pygame.Color.from_cmy((0.5, 0.5, 0.5, 0.5)))
792+
785793
def test_from_hsva(self):
786794
hsva = pygame.Color.from_hsva(0, 100, 100, 100)
787795
hsva_tuple = pygame.Color.from_hsva((0, 100, 100, 100))
@@ -791,6 +799,16 @@ def test_from_hsva(self):
791799
self.assertEqual(expected_hsva, hsva)
792800
self.assertEqual(expected_hsva, hsva_tuple)
793801

802+
with self.assertWarns(DeprecationWarning):
803+
self.assertEqual(
804+
expected_hsva, pygame.Color.from_hsva(0, 100, 100, 100, "lel", "foo")
805+
)
806+
807+
with self.assertWarns(DeprecationWarning):
808+
self.assertEqual(
809+
expected_hsva, pygame.Color.from_hsva((0, 100, 100, 100, "lel"))
810+
)
811+
794812
def test_from_hsla(self):
795813
hsla = pygame.Color.from_hsla(0, 100, 100, 100)
796814
hsla_tuple = pygame.Color.from_hsla((0, 100, 100, 100))
@@ -800,6 +818,16 @@ def test_from_hsla(self):
800818
self.assertEqual(expected_hsla, hsla)
801819
self.assertEqual(expected_hsla, hsla_tuple)
802820

821+
with self.assertWarns(DeprecationWarning):
822+
self.assertEqual(
823+
expected_hsla, pygame.Color.from_hsla(0, 100, 100, 100, "lel")
824+
)
825+
826+
with self.assertWarns(DeprecationWarning):
827+
self.assertEqual(
828+
expected_hsla, pygame.Color.from_hsla((0, 100, 100, 100, "lel", "foo"))
829+
)
830+
803831
def test_from_i1i2i3(self):
804832
i1i2i3 = pygame.Color.from_i1i2i3(0, 0, 0)
805833
i1i2i3_tuple = pygame.Color.from_i1i2i3((0, 0, 0))
@@ -809,6 +837,14 @@ def test_from_i1i2i3(self):
809837
self.assertEqual(expected_i1i2i3, i1i2i3)
810838
self.assertEqual(expected_i1i2i3, i1i2i3_tuple)
811839

840+
with self.assertWarns(DeprecationWarning):
841+
self.assertEqual(
842+
expected_i1i2i3, pygame.Color.from_i1i2i3(0, 0, 0, "lel", "foo")
843+
)
844+
845+
with self.assertWarns(DeprecationWarning):
846+
self.assertEqual(expected_i1i2i3, pygame.Color.from_i1i2i3((0, 0, 0, 0)))
847+
812848
def test_normalize(self):
813849
c = pygame.Color(204, 38, 194, 55)
814850
self.assertEqual(c.r, 204)
@@ -989,6 +1025,12 @@ def test_cmy__sanity_testing_converted_should_equate_bar_rounding(self):
9891025
def test_i1i2i3__sanity_testing_converted_should_equate_bar_rounding(self):
9901026
self.colorspaces_converted_should_equate_bar_rounding("i1i2i3")
9911027

1028+
def test_colorspaces_deprecated_large_sequence(self):
1029+
c = pygame.Color("black")
1030+
for space in ("hsla", "hsva", "i1i2i3", "cmy"):
1031+
with self.assertWarns(DeprecationWarning):
1032+
setattr(c, space, (0, 0, 0, 0, "hehe 5th ignored member"))
1033+
9921034
################################################################################
9931035

9941036
def test_correct_gamma__verified_against_python_implementation(self):

0 commit comments

Comments
 (0)