diff --git a/buildconfig/stubs/pygame/draw.pyi b/buildconfig/stubs/pygame/draw.pyi index c3c88aca81..50f57741c5 100644 --- a/buildconfig/stubs/pygame/draw.pyi +++ b/buildconfig/stubs/pygame/draw.pyi @@ -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 @@ -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( diff --git a/src_c/draw.c b/src_c/draw.c index a5667b8c94..7f2745c940 100644 --- a/src_c/draw.c +++ b/src_c/draw.c @@ -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. */ @@ -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"); } @@ -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); } diff --git a/test/draw_test.py b/test/draw_test.py index cb72452fdf..82099e51d8 100644 --- a/test/draw_test.py +++ b/test/draw_test.py @@ -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)) @@ -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): @@ -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,