Skip to content

Commit a93e57f

Browse files
authored
Merge pull request #3354 from oddbookworm/surface-multiphase-init
Surface multiphase init
2 parents 88834fa + ec44f2a commit a93e57f

File tree

1 file changed

+52
-42
lines changed

1 file changed

+52
-42
lines changed

src_c/surface.c

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ typedef enum {
5252
} SurfViewKind;
5353

5454
/* To avoid problems with non-const Py_buffer format field */
55-
static char FormatUint8[] = "B";
56-
static char FormatUint16[] = "=H";
57-
static char FormatUint24[] = "3x";
58-
static char FormatUint32[] = "=I";
55+
#define FormatUint8 "B"
56+
#define FormatUint16 "=H"
57+
#define FormatUint24 "3x"
58+
#define FormatUint32 "=I"
5959

6060
typedef struct pg_bufferinternal_s {
6161
PyObject *consumer_ref; /* A weak reference to a bufferproxy object */
@@ -4596,69 +4596,53 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
45964596

45974597
static PyMethodDef _surface_methods[] = {{NULL, NULL, 0, NULL}};
45984598

4599-
MODINIT_DEFINE(surface)
4599+
int
4600+
exec_surface(PyObject *module)
46004601
{
4601-
PyObject *module, *apiobj;
4602-
static void *c_api[PYGAMEAPI_SURFACE_NUMSLOTS];
4603-
4604-
static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
4605-
"surface",
4606-
DOC_SURFACE,
4607-
-1,
4608-
_surface_methods,
4609-
NULL,
4610-
NULL,
4611-
NULL,
4612-
NULL};
4613-
46144602
/* imported needed apis; Do this first so if there is an error
46154603
the module is not loaded.
46164604
*/
46174605
import_pygame_base();
46184606
if (PyErr_Occurred()) {
4619-
return NULL;
4607+
return -1;
46204608
}
46214609
import_pygame_color();
46224610
if (PyErr_Occurred()) {
4623-
return NULL;
4611+
return -1;
46244612
}
46254613
import_pygame_rect();
46264614
if (PyErr_Occurred()) {
4627-
return NULL;
4615+
return -1;
46284616
}
46294617
import_pygame_bufferproxy();
46304618
if (PyErr_Occurred()) {
4631-
return NULL;
4619+
return -1;
46324620
}
46334621
_IMPORT_PYGAME_MODULE(surflock);
46344622
if (PyErr_Occurred()) {
4635-
return NULL;
4623+
return -1;
46364624
}
46374625

46384626
/* type preparation */
46394627
if (PyType_Ready(&pgSurface_Type) < 0) {
4640-
return NULL;
4628+
return -1;
46414629
}
46424630

4643-
/* create the module */
4644-
module = PyModule_Create(&_module);
4645-
if (module == NULL) {
4646-
return NULL;
4647-
}
4631+
PyObject *apiobj;
4632+
static void *c_api[PYGAMEAPI_SURFACE_NUMSLOTS];
4633+
46484634
if (pg_warn_simd_at_runtime_but_uncompiled() < 0) {
4649-
Py_DECREF(module);
4650-
return NULL;
4635+
return -1;
46514636
}
4637+
46524638
if (PyModule_AddObjectRef(module, "SurfaceType",
46534639
(PyObject *)&pgSurface_Type)) {
4654-
Py_DECREF(module);
4655-
return NULL;
4640+
return -1;
46564641
}
46574642

46584643
if (PyModule_AddObjectRef(module, "Surface",
46594644
(PyObject *)&pgSurface_Type)) {
4660-
Py_DECREF(module);
4661-
return NULL;
4645+
return -1;
46624646
}
46634647

46644648
/* export the c api */
@@ -4667,14 +4651,40 @@ MODINIT_DEFINE(surface)
46674651
c_api[2] = pgSurface_Blit;
46684652
c_api[3] = pgSurface_SetSurface;
46694653
apiobj = encapsulate_api(c_api, "surface");
4670-
if (PyModule_AddObject(module, PYGAMEAPI_LOCAL_ENTRY, apiobj)) {
4671-
Py_XDECREF(apiobj);
4672-
Py_DECREF(module);
4673-
return NULL;
4654+
if (PyModule_Add(module, PYGAMEAPI_LOCAL_ENTRY, apiobj) < 0) {
4655+
return -1;
46744656
}
4657+
46754658
if (PyModule_AddObjectRef(module, "_dict", pgSurface_Type.tp_dict)) {
4676-
Py_DECREF(module);
4677-
return NULL;
4659+
return -1;
46784660
}
4679-
return module;
4661+
4662+
return 0;
4663+
}
4664+
4665+
MODINIT_DEFINE(surface)
4666+
{
4667+
static PyModuleDef_Slot surf_slots[] = {
4668+
{Py_mod_exec, &exec_surface},
4669+
#if PY_VERSION_HEX >= 0x030c0000
4670+
{Py_mod_multiple_interpreters,
4671+
Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED}, // TODO: see if this can
4672+
// be supported later
4673+
#endif
4674+
#if PY_VERSION_HEX >= 0x030d0000
4675+
{Py_mod_gil, Py_MOD_GIL_USED}, // TODO: support this later
4676+
#endif
4677+
{0, NULL}};
4678+
4679+
static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
4680+
"surface",
4681+
DOC_SURFACE,
4682+
0,
4683+
_surface_methods,
4684+
surf_slots,
4685+
NULL,
4686+
NULL,
4687+
NULL};
4688+
4689+
return PyModuleDef_Init(&_module);
46804690
}

0 commit comments

Comments
 (0)