Skip to content

Fix aaline width and blend conflict #3510

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions buildconfig/stubs/pygame/draw.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def aaline(
:param int width: (optional) used for line thickness

| if width >= 1, used for line thickness (default is 1)
| if width < 1, nothing will be drawn
| if width < 1, a line of width == 1 will be drawn

:returns: a rect bounding the changed pixels, if nothing is drawn the
bounding rect's position will be the ``start_pos`` parameter value (float
Expand All @@ -521,8 +521,9 @@ def aaline(
.. versionchangedold:: 2.0.0 Added support for keyword arguments.
.. versionchanged:: 2.4.0 Removed deprecated 'blend' argument
.. versionchanged:: 2.5.0 ``blend`` argument re-added for backcompat, but will
always raise a deprecation exception when used
.. versionchanged:: 2.5.3 Added line width
do nothing different and always raise a deprecation exception when used.
.. versionchanged:: 2.5.6 Added ``width`` in place of the deprecated
``blend`` argument in a way that doesn't break backcompat too much.
"""

def aalines(
Expand Down
10 changes: 5 additions & 5 deletions src_c/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs)
static char *keywords[] = {"surface", "color", "start_pos", "end_pos",
"width", "blend", NULL};

if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "O!OOO|iO", keywords,
// blend argument is keyword only for backcompat.
// if it is passed as a positional argument, it will be handled in width
if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "O!OOO|i$O", keywords,
&pgSurface_Type, &surfobj, &colorobj,
&start, &end, &width, &blend)) {
return NULL; /* Exception already set. */
Expand Down Expand Up @@ -180,10 +182,6 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs)
return RAISE(PyExc_TypeError, "invalid end_pos argument");
}

if (width < 1) {
return pgRect_New4((int)startx, (int)starty, 0, 0);
}

if (!pgSurface_Lock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error locking surface");
}
Expand All @@ -193,6 +191,8 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs)
starty, endx, endy, width, drawn_area);
}
else {
// For all width <= 1 treat it as width == 1, this helps compat
// with the old blend argument
draw_aaline(surf, surf_clip_rect, surf_format, color, startx, starty,
endx, endy, drawn_area, 0, 0, 0);
}
Expand Down
29 changes: 15 additions & 14 deletions test/draw_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2447,19 +2447,27 @@ def test_aaline__args(self):

def test_aaline__blend_warning(self):
"""Using the blend argument should raise a DeprecationWarning"""
faulty_blend_values = [0, 1, True, False, None, "", [], type]
faulty_blend_values = [0, 1, True, False]
with warnings.catch_warnings(record=True) as w:
for count, blend in enumerate(faulty_blend_values):
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
# Trigger DeprecationWarning.
self.draw_aaline(
pygame.Surface((2, 2)), (0, 0, 0, 50), (0, 0), (2, 2), 1, blend
bounding1 = self.draw_aaline(
pygame.Surface((2, 2)), (0, 0, 0, 50), (0, 0), (2, 2), blend=blend
)
# Doesn't trigger DeprecationWarning, interepreted as width
bounding2 = self.draw_aaline(
pygame.Surface((2, 2)), (0, 0, 0, 50), (0, 0), (2, 2), blend
)
# Check if there is only one warning and is a DeprecationWarning.
self.assertEqual(len(w), count + 1)
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))

# check that the line gets drawn
self.assertEqual(bounding1, bounding2)
self.assertEqual(bounding1, (0, 0, 2, 2))

def test_aaline__kwargs(self):
"""Ensures draw aaline accepts the correct kwargs"""
surface = pygame.Surface((4, 4))
Expand Down Expand Up @@ -2681,11 +2689,10 @@ def test_aaline__valid_width_values(self):
for width in (-100, -10, -1, 0, 1, 10, 100):
surface.fill(surface_color) # Clear for each test.
kwargs["width"] = width
expected_color = line_color if width > 0 else surface_color

bounds_rect = self.draw_aaline(**kwargs)

self.assertEqual(surface.get_at(pos), expected_color)
self.assertEqual(surface.get_at(pos), line_color)
self.assertIsInstance(bounds_rect, pygame.Rect)

def test_aaline__valid_start_pos_formats(self):
Expand Down Expand Up @@ -2928,15 +2935,9 @@ def test_aaline__bounding_rect(self):
surface, line_color, start, end, thickness
)

if 0 < thickness:
# Calculating the expected_rect after the line is
# drawn (it uses what is actually drawn).
expected_rect = create_bounding_rect(
surface, surf_color, start
)
else:
# Nothing drawn.
expected_rect = pygame.Rect(start, (0, 0))
# Calculating the expected_rect after the line is
# drawn (it uses what is actually drawn).
expected_rect = create_bounding_rect(surface, surf_color, start)

self.assertEqual(
bounding_rect,
Expand Down
Loading