Skip to content

Commit

Permalink
Only check rect dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
mzivic7 committed Oct 9, 2024
1 parent ec92294 commit 5e99a6b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
6 changes: 3 additions & 3 deletions docs/reST/ref/draw.rst
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +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. Negative dimension and position values smaller than negated dimension are deprecated,
and will raise an exception in future versions on pygame-ce.
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 @@ -297,7 +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 values in rect will raise a deprecation warning
.. versionchanged:: 2.5.2 Negative rect dimension values will raise a deprecation warning

.. ## pygame.draw.ellipse ##
Expand Down
34 changes: 17 additions & 17 deletions src_c/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -706,17 +706,6 @@ ellipse(PyObject *self, PyObject *arg, PyObject *kwargs)
PG_SURF_BytesPerPixel(surf));
}

if (drawn_area[0] - drawn_area[2] < 0 || drawn_area[2] < 0 ||
drawn_area[1] - drawn_area[3] < 0 || drawn_area[3] < 0) {
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"Negative values in position and dimension rect 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);
}
}

CHECK_LOAD_COLOR(colorobj)

if (width < 0) {
Expand All @@ -727,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);
}
}
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
8 changes: 1 addition & 7 deletions test/draw_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,7 @@ def test_ellipse__valid_width_values(self):
def test_ellipse__negative_rect_warning(self):
"""Ensures draw ellipse shows DeprecationWarning for rect with negative values"""
# Generate few faulty rects.
faulty_rects = []
for i in range(4):
faulty_rect = [0, 0, 0, 0]
faulty_rect[i] = -15
if i < 2:
faulty_rect[i + 2] = -5
faulty_rects.append(faulty_rect)
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.
Expand Down

0 comments on commit 5e99a6b

Please sign in to comment.