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

Add warning when using negative rect dimensions in draw module #3158

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 3 additions & 1 deletion docs/reST/ref/draw.rst
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ object around the draw calls (see :func:`pygame.Surface.lock` and
:type color: Color or string (for :doc:`color_list`) or int or tuple(int, int, int, [int])
:param Rect rect: rectangle to indicate the position and dimensions of the
ellipse, the ellipse will be centered inside the rectangle and bounded
by it
by it. Negative rect dimension values are deprecated, and will raise an exception
in a future versions on pygame-ce.
:param int width: (optional) used for line thickness or to indicate that
the ellipse is to be filled (not to be confused with the width value
of the ``rect`` parameter)
Expand All @@ -296,6 +297,7 @@ object around the draw calls (see :func:`pygame.Surface.lock` and
:rtype: Rect

.. versionchangedold:: 2.0.0 Added support for keyword arguments.
.. versionchanged:: 2.5.2 Negative rect dimension values will raise a deprecation warning

.. ## pygame.draw.ellipse ##

Expand Down
23 changes: 17 additions & 6 deletions src_c/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,14 +716,25 @@ ellipse(PyObject *self, PyObject *arg, PyObject *kwargs)
return RAISE(PyExc_RuntimeError, "error locking surface");
}

if (!width ||
width >= MIN(rect->w / 2 + rect->w % 2, rect->h / 2 + rect->h % 2)) {
draw_ellipse_filled(surf, rect->x, rect->y, rect->w, rect->h, color,
drawn_area);
if (rect->w < 0 || rect->h < 0) {
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"Negative rect dimension values are deprecated and "
"have no functionality. This will cause an error in "
"a future version of pygame-ce.",
1) == -1) {
return pgRect_New4(0, 0, 0, 0);
mzivic7 marked this conversation as resolved.
Show resolved Hide resolved
}
}
else {
draw_ellipse_thickness(surf, rect->x, rect->y, rect->w, rect->h,
width - 1, color, drawn_area);
if (!width || width >= MIN(rect->w / 2 + rect->w % 2,
rect->h / 2 + rect->h % 2)) {
draw_ellipse_filled(surf, rect->x, rect->y, rect->w, rect->h,
color, drawn_area);
}
else {
draw_ellipse_thickness(surf, rect->x, rect->y, rect->w, rect->h,
width - 1, color, drawn_area);
}
}

if (!pgSurface_Unlock(surfobj)) {
Expand Down
14 changes: 14 additions & 0 deletions test/draw_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,20 @@ def test_ellipse__valid_width_values(self):
self.assertEqual(surface.get_at(pos), expected_color)
self.assertIsInstance(bounds_rect, pygame.Rect)

def test_ellipse__negative_rect_warning(self):
"""Ensures draw ellipse shows DeprecationWarning for rect with negative values"""
# Generate few faulty rects.
faulty_rects = ((10, 10, -5, 3), (10, 10, 5, -3))
with warnings.catch_warnings(record=True) as w:
for count, rect in enumerate(faulty_rects):
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
# Trigger DeprecationWarning.
self.draw_ellipse(pygame.Surface((6, 6)), (255, 255, 255), rect)
# Check if there is only one warning and is a DeprecationWarning.
self.assertEqual(len(w), count + 1)
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))

def test_ellipse__valid_rect_formats(self):
"""Ensures draw ellipse accepts different rect formats."""
pos = (1, 1)
Expand Down
Loading