Skip to content

Commit

Permalink
Set error when too many numbers of argument in ``pygame.Color.from_co…
Browse files Browse the repository at this point in the history
…lorspace`` (#3125)
  • Loading branch information
bilhox authored Sep 29, 2024
1 parent c00b777 commit fd47664
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 99 deletions.
63 changes: 8 additions & 55 deletions src_c/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -1008,21 +1008,12 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure)

DEL_ATTR_NOT_SUPPORTED_CHECK("hsva", value);

if (!PySequence_Check(value) || PySequence_Size(value) < 3) {
if (!PySequence_Check(value) || PySequence_Size(value) < 3 ||
PySequence_Size(value) > 4) {
PyErr_SetString(PyExc_ValueError, "invalid HSVA value");
return -1;
}

if (PySequence_Size(value) > 4) {
if (PyErr_WarnEx(
PyExc_DeprecationWarning,
"Passing sequences of size larger than 4 is deprecated, doing "
"this will error in a future version",
1) == -1) {
return -1;
}
}

/* H */
item = PySequence_GetItem(value, 0);
if (!item || !_get_double(item, &(hsva[0])) || hsva[0] < 0 ||
Expand Down Expand Up @@ -1183,21 +1174,12 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure)

DEL_ATTR_NOT_SUPPORTED_CHECK("hsla", value);

if (!PySequence_Check(value) || PySequence_Size(value) < 3) {
if (!PySequence_Check(value) || PySequence_Size(value) < 3 ||
PySequence_Size(value) > 4) {
PyErr_SetString(PyExc_ValueError, "invalid HSLA value");
return -1;
}

if (PySequence_Size(value) > 4) {
if (PyErr_WarnEx(
PyExc_DeprecationWarning,
"Passing sequences of size larger than 4 is deprecated, doing "
"this will error in a future version",
1) == -1) {
return -1;
}
}

/* H */
item = PySequence_GetItem(value, 0);
if (!item || !_get_double(item, &(hsla[0])) || hsla[0] < 0 ||
Expand Down Expand Up @@ -1358,21 +1340,11 @@ _color_set_i1i2i3(pgColorObject *color, PyObject *value, void *closure)

DEL_ATTR_NOT_SUPPORTED_CHECK("i1i2i3", value);

if (!PySequence_Check(value) || PySequence_Size(value) < 3) {
if (!PySequence_Check(value) || PySequence_Size(value) != 3) {
PyErr_SetString(PyExc_ValueError, "invalid I1I2I3 value");
return -1;
}

if (PySequence_Size(value) > 3) {
if (PyErr_WarnEx(
PyExc_DeprecationWarning,
"Passing sequences of size larger than 3 is deprecated, doing "
"this will error in a future version",
1) == -1) {
return -1;
}
}

/* I1 */
item = PySequence_GetItem(value, 0);
if (!item || !_get_double(item, &(i1i2i3[0])) || i1i2i3[0] < 0 ||
Expand Down Expand Up @@ -1440,21 +1412,11 @@ _color_set_cmy(pgColorObject *color, PyObject *value, void *closure)

DEL_ATTR_NOT_SUPPORTED_CHECK("cmy", value);

if (!PySequence_Check(value) || PySequence_Size(value) < 3) {
if (!PySequence_Check(value) || PySequence_Size(value) != 3) {
PyErr_SetString(PyExc_ValueError, "invalid CMY value");
return -1;
}

if (PySequence_Size(value) > 3) {
if (PyErr_WarnEx(
PyExc_DeprecationWarning,
"Passing sequences of size larger than 3 is deprecated, doing "
"this will error in a future version",
1) == -1) {
return -1;
}
}

/* I1 */
item = PySequence_GetItem(value, 0);
if (!item || !_get_double(item, &(cmy[0])) || cmy[0] < 0 || cmy[0] > 1) {
Expand Down Expand Up @@ -1510,21 +1472,12 @@ _color_set_normalized(pgColorObject *color, PyObject *value, void *closure)

DEL_ATTR_NOT_SUPPORTED_CHECK("normalized", value);

if (!PySequence_Check(value) || PySequence_Size(value) < 3) {
if (!PySequence_Check(value) || PySequence_Size(value) < 3 ||
PySequence_Size(value) > 4) {
PyErr_SetString(PyExc_ValueError, "invalid normalized value");
return -1;
}

if (PySequence_Size(value) > 4) {
if (PyErr_WarnEx(
PyExc_DeprecationWarning,
"Passing sequences of size larger than 4 is deprecated, doing "
"this will error in a future version",
1) == -1) {
return -1;
}
}

item = PySequence_GetItem(value, 0);
if (!item || !_get_double(item, &(frgba[0])) || frgba[0] < 0.0 ||
frgba[0] > 1.0) {
Expand Down
73 changes: 29 additions & 44 deletions test/color_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,13 +782,12 @@ def test_from_cmy(self):
self.assertEqual(expected_cmy, cmy)
self.assertEqual(expected_cmy, cmy_tuple)

with self.assertWarns(DeprecationWarning):
self.assertEqual(
expected_cmy, pygame.Color.from_cmy(0.5, 0.5, 0.5, "lel", "foo")
)

with self.assertWarns(DeprecationWarning):
self.assertEqual(expected_cmy, pygame.Color.from_cmy((0.5, 0.5, 0.5, 0.5)))
self.assertRaises(
ValueError, lambda: pygame.Color.from_cmy(0.5, 0.5, 0.5, "lel", "foo")
)
self.assertRaises(
ValueError, lambda: pygame.Color.from_cmy((0.5, 0.5, 0.5, 0.5))
)

def test_from_hsva(self):
hsva = pygame.Color.from_hsva(0, 100, 100, 100)
Expand All @@ -799,15 +798,12 @@ def test_from_hsva(self):
self.assertEqual(expected_hsva, hsva)
self.assertEqual(expected_hsva, hsva_tuple)

with self.assertWarns(DeprecationWarning):
self.assertEqual(
expected_hsva, pygame.Color.from_hsva(0, 100, 100, 100, "lel", "foo")
)

with self.assertWarns(DeprecationWarning):
self.assertEqual(
expected_hsva, pygame.Color.from_hsva((0, 100, 100, 100, "lel"))
)
self.assertRaises(
ValueError, lambda: pygame.Color.from_hsva(0, 100, 100, 100, "lel", "foo")
)
self.assertRaises(
ValueError, lambda: pygame.Color.from_hsva((0, 100, 100, 100, "lel"))
)

def test_from_hsla(self):
hsla = pygame.Color.from_hsla(0, 100, 100, 100)
Expand All @@ -818,15 +814,12 @@ def test_from_hsla(self):
self.assertEqual(expected_hsla, hsla)
self.assertEqual(expected_hsla, hsla_tuple)

with self.assertWarns(DeprecationWarning):
self.assertEqual(
expected_hsla, pygame.Color.from_hsla(0, 100, 100, 100, "lel")
)

with self.assertWarns(DeprecationWarning):
self.assertEqual(
expected_hsla, pygame.Color.from_hsla((0, 100, 100, 100, "lel", "foo"))
)
self.assertRaises(
ValueError, lambda: pygame.Color.from_hsla(0, 100, 100, 100, "lel")
)
self.assertRaises(
ValueError, lambda: pygame.Color.from_hsla((0, 100, 100, 100, "lel", "foo"))
)

def test_from_i1i2i3(self):
i1i2i3 = pygame.Color.from_i1i2i3(0, 0, 0)
Expand All @@ -837,13 +830,10 @@ def test_from_i1i2i3(self):
self.assertEqual(expected_i1i2i3, i1i2i3)
self.assertEqual(expected_i1i2i3, i1i2i3_tuple)

with self.assertWarns(DeprecationWarning):
self.assertEqual(
expected_i1i2i3, pygame.Color.from_i1i2i3(0, 0, 0, "lel", "foo")
)

with self.assertWarns(DeprecationWarning):
self.assertEqual(expected_i1i2i3, pygame.Color.from_i1i2i3((0, 0, 0, 0)))
self.assertRaises(
ValueError, lambda: pygame.Color.from_i1i2i3(0, 0, 0, "lel", "foo")
)
self.assertRaises(ValueError, lambda: pygame.Color.from_i1i2i3((0, 0, 0, 0)))

def test_from_normalized(self):
normal = pygame.Color.from_normalized(1, 1, 1, 1)
Expand All @@ -854,16 +844,9 @@ def test_from_normalized(self):
self.assertEqual(expected_normal, normal)
self.assertEqual(expected_normal, normal_tuple)

with self.assertWarns(DeprecationWarning):
self.assertEqual(
expected_normal, pygame.Color.from_normalized(1, 1, 1, 1, "lel")
)

with self.assertWarns(DeprecationWarning):
self.assertEqual(
expected_normal,
pygame.Color.from_normalized((1, 1, 1, 1, "lel", "foo")),
)
self.assertRaises(
ValueError, lambda: pygame.Color.from_normalized(1, 1, 1, 1, "lel")
)

def test_normalize(self):
c = pygame.Color(204, 38, 194, 55)
Expand Down Expand Up @@ -1062,8 +1045,10 @@ def test_normalized__sanity_testing_converted_should_equate_bar_rounding(self):
def test_colorspaces_deprecated_large_sequence(self):
c = pygame.Color("black")
for space in ("hsla", "hsva", "i1i2i3", "cmy", "normalized"):
with self.assertWarns(DeprecationWarning):
setattr(c, space, (0, 0, 0, 0, "hehe 5th ignored member"))
with self.assertRaises(ValueError):
setattr(
c, space, (0, 0, 0, 0, "HAHAHAHAHAHA, don't ignore 5th member :)")
)

################################################################################

Expand Down

0 comments on commit fd47664

Please sign in to comment.