Skip to content

Commit 8b8fa3d

Browse files
committed
Fix aaline width and blend conflict
1 parent 627c9de commit 8b8fa3d

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

buildconfig/stubs/pygame/draw.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ def aaline(
508508
:param int width: (optional) used for line thickness
509509
510510
| if width >= 1, used for line thickness (default is 1)
511-
| if width < 1, nothing will be drawn
511+
| if width < 1, a line of width == 1 will be drawn
512512
513513
:returns: a rect bounding the changed pixels, if nothing is drawn the
514514
bounding rect's position will be the ``start_pos`` parameter value (float
@@ -521,8 +521,9 @@ def aaline(
521521
.. versionchangedold:: 2.0.0 Added support for keyword arguments.
522522
.. versionchanged:: 2.4.0 Removed deprecated 'blend' argument
523523
.. versionchanged:: 2.5.0 ``blend`` argument re-added for backcompat, but will
524-
always raise a deprecation exception when used
525-
.. versionchanged:: 2.5.3 Added line width
524+
do nothing different and always raise a deprecation exception when used.
525+
.. versionchanged:: 2.5.6 Added ``width`` in place of the deprecated
526+
``blend`` argument in a way that doesn't break backcompat too much.
526527
"""
527528

528529
def aalines(

src_c/draw.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs)
135135
static char *keywords[] = {"surface", "color", "start_pos", "end_pos",
136136
"width", "blend", NULL};
137137

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

183-
if (width < 1) {
184-
return pgRect_New4((int)startx, (int)starty, 0, 0);
185-
}
186-
187185
if (!pgSurface_Lock(surfobj)) {
188186
return RAISE(PyExc_RuntimeError, "error locking surface");
189187
}
@@ -193,6 +191,8 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs)
193191
starty, endx, endy, width, drawn_area);
194192
}
195193
else {
194+
// For all width <= 1 treat it as width == 1, this helps compat
195+
// with the old blend argument
196196
draw_aaline(surf, surf_clip_rect, surf_format, color, startx, starty,
197197
endx, endy, drawn_area, 0, 0, 0);
198198
}

test/draw_test.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,13 +2453,21 @@ def test_aaline__blend_warning(self):
24532453
# Cause all warnings to always be triggered.
24542454
warnings.simplefilter("always")
24552455
# Trigger DeprecationWarning.
2456-
self.draw_aaline(
2457-
pygame.Surface((2, 2)), (0, 0, 0, 50), (0, 0), (2, 2), 1, blend
2456+
bounding1 = self.draw_aaline(
2457+
pygame.Surface((2, 2)), (0, 0, 0, 50), (0, 0), (2, 2), blend=blend
2458+
)
2459+
# Doesn't trigger DeprecationWarning, interepreted as width
2460+
bounding1 = self.draw_aaline(
2461+
pygame.Surface((2, 2)), (0, 0, 0, 50), (0, 0), (2, 2), blend
24582462
)
24592463
# Check if there is only one warning and is a DeprecationWarning.
24602464
self.assertEqual(len(w), count + 1)
24612465
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
24622466

2467+
# check that the line gets drawn
2468+
self.assertEqual(bounding1, bounding2)
2469+
self.assertEqual(bounding1, (0, 0, 2, 2))
2470+
24632471
def test_aaline__kwargs(self):
24642472
"""Ensures draw aaline accepts the correct kwargs"""
24652473
surface = pygame.Surface((4, 4))
@@ -2681,11 +2689,10 @@ def test_aaline__valid_width_values(self):
26812689
for width in (-100, -10, -1, 0, 1, 10, 100):
26822690
surface.fill(surface_color) # Clear for each test.
26832691
kwargs["width"] = width
2684-
expected_color = line_color if width > 0 else surface_color
26852692

26862693
bounds_rect = self.draw_aaline(**kwargs)
26872694

2688-
self.assertEqual(surface.get_at(pos), expected_color)
2695+
self.assertEqual(surface.get_at(pos), line_color)
26892696
self.assertIsInstance(bounds_rect, pygame.Rect)
26902697

26912698
def test_aaline__valid_start_pos_formats(self):
@@ -2928,15 +2935,9 @@ def test_aaline__bounding_rect(self):
29282935
surface, line_color, start, end, thickness
29292936
)
29302937

2931-
if 0 < thickness:
2932-
# Calculating the expected_rect after the line is
2933-
# drawn (it uses what is actually drawn).
2934-
expected_rect = create_bounding_rect(
2935-
surface, surf_color, start
2936-
)
2937-
else:
2938-
# Nothing drawn.
2939-
expected_rect = pygame.Rect(start, (0, 0))
2938+
# Calculating the expected_rect after the line is
2939+
# drawn (it uses what is actually drawn).
2940+
expected_rect = create_bounding_rect(surface, surf_color, start)
29402941

29412942
self.assertEqual(
29422943
bounding_rect,

0 commit comments

Comments
 (0)