From 816f3edc2ab19b38988cdd0f2371f640cee7aaad Mon Sep 17 00:00:00 2001 From: Damus2D Date: Thu, 4 Jan 2024 13:34:54 +0100 Subject: [PATCH 1/7] pygame.Rect/FRect() init no args --- buildconfig/stubs/pygame/rect.pyi | 2 ++ docs/reST/ref/rect.rst | 6 ++++++ src_c/rect_impl.h | 4 ++++ test/rect_test.py | 7 +++++++ 4 files changed, 19 insertions(+) diff --git a/buildconfig/stubs/pygame/rect.pyi b/buildconfig/stubs/pygame/rect.pyi index a0ad71d87b..b401f9a930 100644 --- a/buildconfig/stubs/pygame/rect.pyi +++ b/buildconfig/stubs/pygame/rect.pyi @@ -128,6 +128,8 @@ class _GenericRect(Collection[_N]): def __init__(self, left_top: Coordinate, width_height: Coordinate) -> None: ... @overload def __init__(self, single_arg: RectValue) -> None: ... + @overload + def __init__(self) -> None: ... def __len__(self) -> Literal[4]: ... def __iter__(self) -> Iterator[_N]: ... @overload diff --git a/docs/reST/ref/rect.rst b/docs/reST/ref/rect.rst index 984c1aeee4..7f4bcecb46 100644 --- a/docs/reST/ref/rect.rst +++ b/docs/reST/ref/rect.rst @@ -11,9 +11,11 @@ | :sg:`Rect(left, top, width, height) -> Rect` | :sg:`Rect((left, top), (width, height)) -> Rect` | :sg:`Rect(object) -> Rect` + | :sg:`Rect() -> Rect` | :sg:`FRect(left, top, width, height) -> FRect` | :sg:`FRect((left, top), (width, height)) -> FRect` | :sg:`FRect(object) -> FRect` + | :sg:`FRect() -> FRect` .. versionchanged:: 2.2 Since version 2.2 there is another class called FRect that serves the same purpose as as `Rect` but it can hold floats instead of integers. @@ -26,6 +28,10 @@ values to construct a Rect. This makes it easier to create Rects on the fly as arguments to functions. + If no arguments are given, a zero Rect will be created (x, y, w, h = 0). + This will only work when using the Rect/FRect class and not with functions + that require a Rect argument. + The Rect functions that change the position or size of a Rect return a new copy of the Rect with the affected changes. The original Rect is not modified. Some methods have an alternate "in-place" version that returns diff --git a/src_c/rect_impl.h b/src_c/rect_impl.h index b66ee4c3f4..4e44200f29 100644 --- a/src_c/rect_impl.h +++ b/src_c/rect_impl.h @@ -850,6 +850,10 @@ RectExport_dealloc(RectObject *self) static int RectExport_init(RectObject *self, PyObject *args, PyObject *kwds) { + if (PySequence_Fast_GET_SIZE(args) == 0) { + return 0; + } + InnerRect *argrect, temp; if (!(argrect = RectFromObject(args, &temp))) { diff --git a/test/rect_test.py b/test/rect_test.py index 22291fae15..01b9eeacde 100644 --- a/test/rect_test.py +++ b/test/rect_test.py @@ -2847,6 +2847,13 @@ def test_collection_abc(self): self.assertTrue(isinstance(r, Collection)) self.assertFalse(isinstance(r, Sequence)) + def test_construction_no_args(self): + r = Rect() + self.assertEqual(r.x, 0) + self.assertEqual(r.y, 0) + self.assertEqual(r.w, 0) + self.assertEqual(r.h, 0) + class FRectTypeTest(RectTypeTest): def setUp(self): From 294dc898fa707c6a70a94d1fd38b92cca4bce1f1 Mon Sep 17 00:00:00 2001 From: Damus2D Date: Thu, 4 Jan 2024 14:03:53 +0100 Subject: [PATCH 2/7] pygame.Rect/FRect() init no args DOCS FIX1 --- src_c/doc/rect_doc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src_c/doc/rect_doc.h b/src_c/doc/rect_doc.h index cfe1e2f1bd..e83a50431c 100644 --- a/src_c/doc/rect_doc.h +++ b/src_c/doc/rect_doc.h @@ -1,5 +1,5 @@ /* Auto generated file: with makeref.py . Docs go in docs/reST/ref/ . */ -#define DOC_RECT "Rect(left, top, width, height) -> Rect\nRect((left, top), (width, height)) -> Rect\nRect(object) -> Rect\nFRect(left, top, width, height) -> FRect\nFRect((left, top), (width, height)) -> FRect\nFRect(object) -> FRect\npygame object for storing rectangular coordinates" +#define DOC_RECT "Rect(left, top, width, height) -> Rect\nRect((left, top), (width, height)) -> Rect\nRect(object) -> Rect\nRect() -> Rect\nFRect(left, top, width, height) -> FRect\nFRect((left, top), (width, height)) -> FRect\nFRect(object) -> FRect\nFRect() -> FRect\npygame object for storing rectangular coordinates" #define DOC_RECT_COPY "copy() -> Rect\ncopy the rectangle" #define DOC_RECT_MOVE "move(x, y, /) -> Rect\nmoves the rectangle" #define DOC_RECT_MOVEIP "move_ip(x, y, /) -> None\nmoves the rectangle, in place" From ce6a5224888bd742cd4ed877b8ca762489b8ffd5 Mon Sep 17 00:00:00 2001 From: Damus2D Date: Thu, 4 Jan 2024 15:53:36 +0100 Subject: [PATCH 3/7] pygame.Rect/FRect() init no args PyTuple_GET_SIZE instead of PySequence_Fast_GET_SIZE --- src_c/rect_impl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src_c/rect_impl.h b/src_c/rect_impl.h index 4e44200f29..0f6cb53fb0 100644 --- a/src_c/rect_impl.h +++ b/src_c/rect_impl.h @@ -850,7 +850,7 @@ RectExport_dealloc(RectObject *self) static int RectExport_init(RectObject *self, PyObject *args, PyObject *kwds) { - if (PySequence_Fast_GET_SIZE(args) == 0) { + if (PyTuple_GET_SIZE(args) == 0) { return 0; } From bd9517e309b8891f2b0348133fc0baea2d1cb8a9 Mon Sep 17 00:00:00 2001 From: Damus2D Date: Sun, 7 Jan 2024 11:41:49 +0100 Subject: [PATCH 4/7] pygame.Rect/FRect() init no args explicitly setting fields to 0 --- src_c/rect_impl.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src_c/rect_impl.h b/src_c/rect_impl.h index 0f6cb53fb0..4230a5bf06 100644 --- a/src_c/rect_impl.h +++ b/src_c/rect_impl.h @@ -851,6 +851,10 @@ static int RectExport_init(RectObject *self, PyObject *args, PyObject *kwds) { if (PyTuple_GET_SIZE(args) == 0) { + // Setting fields explicitly to 0 to eliminate any chance of bugs happening + self->r.x = self->r.y = (PrimitiveType)0; + self->r.w = self->r.h = (PrimitiveType)0; + self->weakreflist = NULL; return 0; } From c081fc0b2d9fb54006adcb071dcb9b9d577e81f6 Mon Sep 17 00:00:00 2001 From: Damus2D Date: Sun, 7 Jan 2024 11:43:35 +0100 Subject: [PATCH 5/7] pygame.Rect/FRect() init no args forgot to run 'setup.py format' --- src_c/rect_impl.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src_c/rect_impl.h b/src_c/rect_impl.h index 4230a5bf06..69ff6096e3 100644 --- a/src_c/rect_impl.h +++ b/src_c/rect_impl.h @@ -851,7 +851,8 @@ static int RectExport_init(RectObject *self, PyObject *args, PyObject *kwds) { if (PyTuple_GET_SIZE(args) == 0) { - // Setting fields explicitly to 0 to eliminate any chance of bugs happening + // Setting fields explicitly to 0 to eliminate any chance of bugs + // happening self->r.x = self->r.y = (PrimitiveType)0; self->r.w = self->r.h = (PrimitiveType)0; self->weakreflist = NULL; From 9e530fef2bcc8425ec65fc7ed22fadb6ec4365f5 Mon Sep 17 00:00:00 2001 From: Damus2D Date: Sun, 7 Jan 2024 14:29:35 +0100 Subject: [PATCH 6/7] pygame.Rect/FRect() init no args remove unnecessary fields initialization to 0 --- src_c/rect_impl.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src_c/rect_impl.h b/src_c/rect_impl.h index 69ff6096e3..0f6cb53fb0 100644 --- a/src_c/rect_impl.h +++ b/src_c/rect_impl.h @@ -851,11 +851,6 @@ static int RectExport_init(RectObject *self, PyObject *args, PyObject *kwds) { if (PyTuple_GET_SIZE(args) == 0) { - // Setting fields explicitly to 0 to eliminate any chance of bugs - // happening - self->r.x = self->r.y = (PrimitiveType)0; - self->r.w = self->r.h = (PrimitiveType)0; - self->weakreflist = NULL; return 0; } From a0773c5f4513acc2bbdec2cfcd717f6c05f961f5 Mon Sep 17 00:00:00 2001 From: Damus2D Date: Sun, 7 Jan 2024 15:17:43 +0100 Subject: [PATCH 7/7] pygame.Rect/FRect() init no args docs change, tests for FRect and subclasses --- docs/reST/ref/rect.rst | 2 +- test/rect_test.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/reST/ref/rect.rst b/docs/reST/ref/rect.rst index 7f4bcecb46..9066d045b1 100644 --- a/docs/reST/ref/rect.rst +++ b/docs/reST/ref/rect.rst @@ -28,7 +28,7 @@ values to construct a Rect. This makes it easier to create Rects on the fly as arguments to functions. - If no arguments are given, a zero Rect will be created (x, y, w, h = 0). + If no arguments are given, a zero Rect will be created (x=0, y=0, w=0, h=0). This will only work when using the Rect/FRect class and not with functions that require a Rect argument. diff --git a/test/rect_test.py b/test/rect_test.py index 01b9eeacde..307feb8b29 100644 --- a/test/rect_test.py +++ b/test/rect_test.py @@ -3298,6 +3298,13 @@ def test_frect_subscript(self): self.assertSeqAlmostEqual5(r[3::-1], [4.8, 3.6, 2.4, 1.2]) self.assertRaises(TypeError, r.__getitem__, None) + def test_construction_no_args(self): + r = FRect() + self.assertEqual(r.x, 0.0) + self.assertEqual(r.y, 0.0) + self.assertEqual(r.w, 0.0) + self.assertEqual(r.h, 0.0) + class SubclassTest(unittest.TestCase): class MyRect(Rect): @@ -3373,6 +3380,13 @@ def test_collection_abc(self): self.assertTrue(isinstance(mr1, Collection)) self.assertFalse(isinstance(mr1, Sequence)) + def test_construction_no_args(self): + mr = self.MyRect() + self.assertEqual(mr.x, 0) + self.assertEqual(mr.y, 0) + self.assertEqual(mr.w, 0) + self.assertEqual(mr.h, 0) + if __name__ == "__main__": unittest.main()