Skip to content

Commit 677b78b

Browse files
committed
Move get_rect helper to c_api
1 parent aac3b96 commit 677b78b

File tree

5 files changed

+44
-30
lines changed

5 files changed

+44
-30
lines changed

docs/reST/c_api/base.rst

+7
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,10 @@ C header: src_c/include/pygame.h
177177
The previous surface object, if any, is invalidated.
178178
Argument *screen* may be *NULL*.
179179
This functions is called by pygame.display.set_mode().
180+
181+
.. c:function:: PyObject* pgObject_getRectHelper(PyObject *rect, PyObject *const *args,
182+
Py_ssize_t nargs, PyObject *kwnames, char *type)
183+
184+
Return a rectangle covering the entire object. Rectangle will start at (0, 0)
185+
with a width and height the same size as the object. You can pass keyword
186+
arguments to be applied to the attributes of the rect before it is returned.

src_c/_pygame.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ typedef enum {
531531
#define PYGAMEAPI_PIXELARRAY_NUMSLOTS 2
532532
#define PYGAMEAPI_COLOR_NUMSLOTS 5
533533
#define PYGAMEAPI_MATH_NUMSLOTS 2
534-
#define PYGAMEAPI_BASE_NUMSLOTS 29
534+
#define PYGAMEAPI_BASE_NUMSLOTS 30
535535
#define PYGAMEAPI_EVENT_NUMSLOTS 10
536536
#define PYGAMEAPI_WINDOW_NUMSLOTS 1
537537
#define PYGAMEAPI_GEOMETRY_NUMSLOTS 2

src_c/base.c

+31-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ static void
103103
pgBuffer_Release(pg_buffer *);
104104
static int
105105
pgObject_GetBuffer(PyObject *, pg_buffer *, int);
106+
static inline PyObject *
107+
pgObject_getRectHelper(PyObject *, PyObject *const *, Py_ssize_t,
108+
PyObject *, char *);
106109
static int
107110
pgGetArrayInterface(PyObject **, PyObject *);
108111
static int
@@ -1372,6 +1375,32 @@ pgObject_GetBuffer(PyObject *obj, pg_buffer *pg_view_p, int flags)
13721375
return 0;
13731376
}
13741377

1378+
static inline PyObject *
1379+
pgObject_getRectHelper(PyObject *rect, PyObject *const *args,
1380+
Py_ssize_t nargs, PyObject *kwnames, char *type)
1381+
{
1382+
if (nargs > 0) {
1383+
Py_DECREF(rect);
1384+
return PyErr_Format(PyExc_TypeError,
1385+
"get_%s only accepts keyword arguments", type);
1386+
}
1387+
1388+
if (rect && kwnames) {
1389+
Py_ssize_t i, sequence_len;
1390+
PyObject **sequence_items;
1391+
sequence_items = PySequence_Fast_ITEMS(kwnames);
1392+
sequence_len = PyTuple_GET_SIZE(kwnames);
1393+
1394+
for (i = 0; i < sequence_len; ++i) {
1395+
if ((PyObject_SetAttr(rect, sequence_items[i], args[i]) == -1)) {
1396+
Py_DECREF(rect);
1397+
return NULL;
1398+
}
1399+
}
1400+
}
1401+
return rect;
1402+
}
1403+
13751404
static void
13761405
pgBuffer_Release(pg_buffer *pg_view_p)
13771406
{
@@ -2401,8 +2430,9 @@ MODINIT_DEFINE(base)
24012430
c_api[26] = pg_TwoDoublesFromFastcallArgs;
24022431
c_api[27] = pg_GetDefaultConvertFormat;
24032432
c_api[28] = pg_SetDefaultConvertFormat;
2433+
c_api[29] = pgObject_getRectHelper;
24042434

2405-
#define FILLED_SLOTS 29
2435+
#define FILLED_SLOTS 30
24062436

24072437
#if PYGAMEAPI_BASE_NUMSLOTS != FILLED_SLOTS
24082438
#error export slot count mismatch

src_c/include/_pygame.h

+3
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ typedef struct pg_bufferinfo_s {
189189
#define pg_SetDefaultConvertFormat \
190190
(*(void (*)(Uint32))PYGAMEAPI_GET_SLOT(base, 28))
191191

192+
#define pgObject_getRectHelper \
193+
(*(PyObject * (*)(PyObject *, PyObject *const *, Py_ssize_t, PyObject *, char *))PYGAMEAPI_GET_SLOT(base, 29))
194+
192195
#define import_pygame_base() IMPORT_PYGAME_MODULE(base)
193196
#endif /* ~PYGAMEAPI_BASE_INTERNAL */
194197

src_c/surface.c

+2-28
Original file line numberDiff line numberDiff line change
@@ -2621,32 +2621,6 @@ surf_get_height(PyObject *self, PyObject *_null)
26212621
return PyLong_FromLong(surf->h);
26222622
}
26232623

2624-
static inline PyObject *
2625-
_get_rect_helper(PyObject *rect, PyObject *const *args, Py_ssize_t nargs,
2626-
PyObject *kwnames, char *type)
2627-
{
2628-
if (nargs > 0) {
2629-
Py_DECREF(rect);
2630-
return PyErr_Format(PyExc_TypeError,
2631-
"get_%s only accepts keyword arguments", type);
2632-
}
2633-
2634-
if (rect && kwnames) {
2635-
Py_ssize_t i, sequence_len;
2636-
PyObject **sequence_items;
2637-
sequence_items = PySequence_Fast_ITEMS(kwnames);
2638-
sequence_len = PyTuple_GET_SIZE(kwnames);
2639-
2640-
for (i = 0; i < sequence_len; ++i) {
2641-
if ((PyObject_SetAttr(rect, sequence_items[i], args[i]) == -1)) {
2642-
Py_DECREF(rect);
2643-
return NULL;
2644-
}
2645-
}
2646-
}
2647-
return rect;
2648-
}
2649-
26502624
static PyObject *
26512625
surf_get_rect(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
26522626
PyObject *kwnames)
@@ -2656,7 +2630,7 @@ surf_get_rect(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
26562630

26572631
PyObject *rect = pgRect_New4(0, 0, surf->w, surf->h);
26582632

2659-
return _get_rect_helper(rect, args, nargs, kwnames, "rect");
2633+
return pgObject_getRectHelper(rect, args, nargs, kwnames, "rect");
26602634
}
26612635

26622636
static PyObject *
@@ -2668,7 +2642,7 @@ surf_get_frect(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
26682642

26692643
PyObject *rect = pgFRect_New4(0.f, 0.f, (float)surf->w, (float)surf->h);
26702644

2671-
return _get_rect_helper(rect, args, nargs, kwnames, "frect");
2645+
return pgObject_getRectHelper(rect, args, nargs, kwnames, "frect");
26722646
}
26732647

26742648
static PyObject *

0 commit comments

Comments
 (0)