Skip to content
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

Move get_rect helper to c_api #3303

Open
wants to merge 2 commits 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: 7 additions & 0 deletions docs/reST/c_api/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion src_c/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion src_c/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src_c/include/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ 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 */

Expand Down
30 changes: 2 additions & 28 deletions src_c/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 *
Expand All @@ -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 *
Expand Down
Loading