From 677b78b7e87bf165c0a06fca9c441a9896b21b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Komljenovi=C4=87?= Date: Thu, 23 Jan 2025 12:17:52 +0100 Subject: [PATCH 1/2] Move get_rect helper to c_api --- docs/reST/c_api/base.rst | 7 +++++++ src_c/_pygame.h | 2 +- src_c/base.c | 32 +++++++++++++++++++++++++++++++- src_c/include/_pygame.h | 3 +++ src_c/surface.c | 30 ++---------------------------- 5 files changed, 44 insertions(+), 30 deletions(-) diff --git a/docs/reST/c_api/base.rst b/docs/reST/c_api/base.rst index 9c20197f5f..5fbfd9f55d 100644 --- a/docs/reST/c_api/base.rst +++ b/docs/reST/c_api/base.rst @@ -177,3 +177,10 @@ C header: src_c/include/pygame.h The previous surface object, if any, is invalidated. Argument *screen* may be *NULL*. This functions is called by pygame.display.set_mode(). + +.. c:function:: PyObject* pgObject_getRectHelper(PyObject *rect, PyObject *const *args, + Py_ssize_t nargs, PyObject *kwnames, char *type) + + Return a rectangle covering the entire object. Rectangle will start at (0, 0) + with a width and height the same size as the object. You can pass keyword + arguments to be applied to the attributes of the rect before it is returned. diff --git a/src_c/_pygame.h b/src_c/_pygame.h index ad8ffc0626..86019125f9 100644 --- a/src_c/_pygame.h +++ b/src_c/_pygame.h @@ -531,7 +531,7 @@ typedef enum { #define PYGAMEAPI_PIXELARRAY_NUMSLOTS 2 #define PYGAMEAPI_COLOR_NUMSLOTS 5 #define PYGAMEAPI_MATH_NUMSLOTS 2 -#define PYGAMEAPI_BASE_NUMSLOTS 29 +#define PYGAMEAPI_BASE_NUMSLOTS 30 #define PYGAMEAPI_EVENT_NUMSLOTS 10 #define PYGAMEAPI_WINDOW_NUMSLOTS 1 #define PYGAMEAPI_GEOMETRY_NUMSLOTS 2 diff --git a/src_c/base.c b/src_c/base.c index 9c3176875c..2b60b6db4b 100644 --- a/src_c/base.c +++ b/src_c/base.c @@ -103,6 +103,9 @@ static void pgBuffer_Release(pg_buffer *); static int pgObject_GetBuffer(PyObject *, pg_buffer *, int); +static inline PyObject * +pgObject_getRectHelper(PyObject *, PyObject *const *, Py_ssize_t, + PyObject *, char *); static int pgGetArrayInterface(PyObject **, PyObject *); static int @@ -1372,6 +1375,32 @@ pgObject_GetBuffer(PyObject *obj, pg_buffer *pg_view_p, int flags) return 0; } +static inline PyObject * +pgObject_getRectHelper(PyObject *rect, PyObject *const *args, + Py_ssize_t nargs, PyObject *kwnames, char *type) +{ + if (nargs > 0) { + Py_DECREF(rect); + return PyErr_Format(PyExc_TypeError, + "get_%s only accepts keyword arguments", type); + } + + if (rect && kwnames) { + Py_ssize_t i, sequence_len; + PyObject **sequence_items; + sequence_items = PySequence_Fast_ITEMS(kwnames); + sequence_len = PyTuple_GET_SIZE(kwnames); + + for (i = 0; i < sequence_len; ++i) { + if ((PyObject_SetAttr(rect, sequence_items[i], args[i]) == -1)) { + Py_DECREF(rect); + return NULL; + } + } + } + return rect; +} + static void pgBuffer_Release(pg_buffer *pg_view_p) { @@ -2401,8 +2430,9 @@ MODINIT_DEFINE(base) c_api[26] = pg_TwoDoublesFromFastcallArgs; c_api[27] = pg_GetDefaultConvertFormat; c_api[28] = pg_SetDefaultConvertFormat; + c_api[29] = pgObject_getRectHelper; -#define FILLED_SLOTS 29 +#define FILLED_SLOTS 30 #if PYGAMEAPI_BASE_NUMSLOTS != FILLED_SLOTS #error export slot count mismatch diff --git a/src_c/include/_pygame.h b/src_c/include/_pygame.h index b3548a66ee..4eed69f277 100644 --- a/src_c/include/_pygame.h +++ b/src_c/include/_pygame.h @@ -189,6 +189,9 @@ typedef struct pg_bufferinfo_s { #define pg_SetDefaultConvertFormat \ (*(void (*)(Uint32))PYGAMEAPI_GET_SLOT(base, 28)) +#define pgObject_getRectHelper \ + (*(PyObject * (*)(PyObject *, PyObject *const *, Py_ssize_t, PyObject *, char *))PYGAMEAPI_GET_SLOT(base, 29)) + #define import_pygame_base() IMPORT_PYGAME_MODULE(base) #endif /* ~PYGAMEAPI_BASE_INTERNAL */ diff --git a/src_c/surface.c b/src_c/surface.c index d9cfc1e2b7..81b603da6c 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -2621,32 +2621,6 @@ surf_get_height(PyObject *self, PyObject *_null) return PyLong_FromLong(surf->h); } -static inline PyObject * -_get_rect_helper(PyObject *rect, PyObject *const *args, Py_ssize_t nargs, - PyObject *kwnames, char *type) -{ - if (nargs > 0) { - Py_DECREF(rect); - return PyErr_Format(PyExc_TypeError, - "get_%s only accepts keyword arguments", type); - } - - if (rect && kwnames) { - Py_ssize_t i, sequence_len; - PyObject **sequence_items; - sequence_items = PySequence_Fast_ITEMS(kwnames); - sequence_len = PyTuple_GET_SIZE(kwnames); - - for (i = 0; i < sequence_len; ++i) { - if ((PyObject_SetAttr(rect, sequence_items[i], args[i]) == -1)) { - Py_DECREF(rect); - return NULL; - } - } - } - return rect; -} - static PyObject * surf_get_rect(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) @@ -2656,7 +2630,7 @@ surf_get_rect(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *rect = pgRect_New4(0, 0, surf->w, surf->h); - return _get_rect_helper(rect, args, nargs, kwnames, "rect"); + return pgObject_getRectHelper(rect, args, nargs, kwnames, "rect"); } static PyObject * @@ -2668,7 +2642,7 @@ surf_get_frect(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *rect = pgFRect_New4(0.f, 0.f, (float)surf->w, (float)surf->h); - return _get_rect_helper(rect, args, nargs, kwnames, "frect"); + return pgObject_getRectHelper(rect, args, nargs, kwnames, "frect"); } static PyObject * From 39b68cf10e9ac1f39e95dee36419d83ba088c69c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Komljenovi=C4=87?= Date: Thu, 23 Jan 2025 12:35:16 +0100 Subject: [PATCH 2/2] Fix formatting --- src_c/base.c | 8 ++++---- src_c/include/_pygame.h | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src_c/base.c b/src_c/base.c index 2b60b6db4b..2b40bafc42 100644 --- a/src_c/base.c +++ b/src_c/base.c @@ -104,8 +104,8 @@ pgBuffer_Release(pg_buffer *); static int pgObject_GetBuffer(PyObject *, pg_buffer *, int); static inline PyObject * -pgObject_getRectHelper(PyObject *, PyObject *const *, Py_ssize_t, - PyObject *, char *); +pgObject_getRectHelper(PyObject *, PyObject *const *, Py_ssize_t, PyObject *, + char *); static int pgGetArrayInterface(PyObject **, PyObject *); static int @@ -1376,8 +1376,8 @@ pgObject_GetBuffer(PyObject *obj, pg_buffer *pg_view_p, int flags) } static inline PyObject * -pgObject_getRectHelper(PyObject *rect, PyObject *const *args, - Py_ssize_t nargs, PyObject *kwnames, char *type) +pgObject_getRectHelper(PyObject *rect, PyObject *const *args, Py_ssize_t nargs, + PyObject *kwnames, char *type) { if (nargs > 0) { Py_DECREF(rect); diff --git a/src_c/include/_pygame.h b/src_c/include/_pygame.h index 4eed69f277..41c186eddb 100644 --- a/src_c/include/_pygame.h +++ b/src_c/include/_pygame.h @@ -189,8 +189,9 @@ typedef struct pg_bufferinfo_s { #define pg_SetDefaultConvertFormat \ (*(void (*)(Uint32))PYGAMEAPI_GET_SLOT(base, 28)) -#define pgObject_getRectHelper \ - (*(PyObject * (*)(PyObject *, PyObject *const *, Py_ssize_t, PyObject *, char *))PYGAMEAPI_GET_SLOT(base, 29)) +#define pgObject_getRectHelper \ + (*(PyObject * (*)(PyObject *, PyObject *const *, Py_ssize_t, PyObject *, \ + char *)) PYGAMEAPI_GET_SLOT(base, 29)) #define import_pygame_base() IMPORT_PYGAME_MODULE(base) #endif /* ~PYGAMEAPI_BASE_INTERNAL */