Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set error when too many numbers of argument in pygame.Color.from_colorspace #3125

Merged
merged 3 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
63 changes: 19 additions & 44 deletions test/color_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,13 +782,9 @@ 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)))
with self.assertRaises(ValueError):
pygame.Color.from_cmy(0.5, 0.5, 0.5, "lel", "foo")
pygame.Color.from_cmy((0.5, 0.5, 0.5, 0.5))
bilhox marked this conversation as resolved.
Show resolved Hide resolved

def test_from_hsva(self):
hsva = pygame.Color.from_hsva(0, 100, 100, 100)
Expand All @@ -799,15 +795,9 @@ 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"))
)
with self.assertRaises(ValueError):
pygame.Color.from_hsva(0, 100, 100, 100, "lel", "foo")
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 +808,9 @@ 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"))
)
with self.assertRaises(ValueError):
pygame.Color.from_hsla(0, 100, 100, 100, "lel")
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 +821,9 @@ 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)))
with self.assertRaises(ValueError):
pygame.Color.from_i1i2i3(0, 0, 0, "lel", "foo")
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 +834,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 +1035,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
Loading