Skip to content

Commit ff307d8

Browse files
committed
Surface is now multi-phase initialization
1 parent 2fb2e04 commit ff307d8

File tree

1 file changed

+59
-46
lines changed

1 file changed

+59
-46
lines changed

src_c/surface.c

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4275,17 +4275,71 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
42754275

42764276
static PyMethodDef _surface_methods[] = {{NULL, NULL, 0, NULL}};
42774277

4278-
MODINIT_DEFINE(surface)
4278+
int
4279+
exec_surface(PyObject *module)
42794280
{
4280-
PyObject *module, *apiobj;
4281+
PyObject *apiobj;
42814282
static void *c_api[PYGAMEAPI_SURFACE_NUMSLOTS];
42824283

4284+
if (pg_warn_simd_at_runtime_but_uncompiled() < 0) {
4285+
Py_DECREF(module);
4286+
return -1;
4287+
}
4288+
4289+
Py_INCREF(&pgSurface_Type);
4290+
if (PyModule_AddObjectRef(module, "SurfaceType",
4291+
(PyObject *)&pgSurface_Type)) {
4292+
Py_DECREF(module);
4293+
return -1;
4294+
}
4295+
4296+
Py_INCREF(&pgSurface_Type);
4297+
if (PyModule_AddObjectRef(module, "Surface",
4298+
(PyObject *)&pgSurface_Type)) {
4299+
Py_DECREF(module);
4300+
return -1;
4301+
}
4302+
4303+
/* export the c api */
4304+
c_api[0] = &pgSurface_Type;
4305+
c_api[1] = pgSurface_New2;
4306+
c_api[2] = pgSurface_Blit;
4307+
c_api[3] = pgSurface_SetSurface;
4308+
apiobj = encapsulate_api(c_api, "surface");
4309+
if (PyModule_AddObjectRef(module, PYGAMEAPI_LOCAL_ENTRY, apiobj)) {
4310+
Py_DECREF(module);
4311+
return -1;
4312+
}
4313+
4314+
Py_XINCREF(pgSurface_Type.tp_dict);
4315+
if (PyModule_AddObjectRef(module, "_dict", pgSurface_Type.tp_dict)) {
4316+
Py_DECREF(module);
4317+
return -1;
4318+
}
4319+
4320+
return 0;
4321+
}
4322+
4323+
MODINIT_DEFINE(surface)
4324+
{
4325+
static PyModuleDef_Slot surf_slots[] = {
4326+
{Py_mod_exec, &exec_surface},
4327+
#if PY_VERSION_HEX >= 0x030c0000
4328+
{Py_mod_multiple_interpreters,
4329+
Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED}, // TODO: see if this can
4330+
// be supported later
4331+
#endif
4332+
#if PY_VERSION_HEX >= 0x030d0000
4333+
{Py_mod_gil, Py_MOD_GIL_USED}, // TODO: support this later
4334+
#endif
4335+
{0, NULL}};
4336+
42834337
static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
42844338
"surface",
42854339
DOC_SURFACE,
4286-
-1,
4340+
0,
42874341
_surface_methods,
4288-
NULL,
4342+
surf_slots,
42894343
NULL,
42904344
NULL,
42914345
NULL};
@@ -4319,46 +4373,5 @@ MODINIT_DEFINE(surface)
43194373
return NULL;
43204374
}
43214375

4322-
/* create the module */
4323-
module = PyModule_Create(&_module);
4324-
if (module == NULL) {
4325-
return NULL;
4326-
}
4327-
if (pg_warn_simd_at_runtime_but_uncompiled() < 0) {
4328-
Py_DECREF(module);
4329-
return NULL;
4330-
}
4331-
Py_INCREF(&pgSurface_Type);
4332-
if (PyModule_AddObject(module, "SurfaceType",
4333-
(PyObject *)&pgSurface_Type)) {
4334-
Py_DECREF(&pgSurface_Type);
4335-
Py_DECREF(module);
4336-
return NULL;
4337-
}
4338-
4339-
Py_INCREF(&pgSurface_Type);
4340-
if (PyModule_AddObject(module, "Surface", (PyObject *)&pgSurface_Type)) {
4341-
Py_DECREF(&pgSurface_Type);
4342-
Py_DECREF(module);
4343-
return NULL;
4344-
}
4345-
4346-
/* export the c api */
4347-
c_api[0] = &pgSurface_Type;
4348-
c_api[1] = pgSurface_New2;
4349-
c_api[2] = pgSurface_Blit;
4350-
c_api[3] = pgSurface_SetSurface;
4351-
apiobj = encapsulate_api(c_api, "surface");
4352-
if (PyModule_AddObject(module, PYGAMEAPI_LOCAL_ENTRY, apiobj)) {
4353-
Py_XDECREF(apiobj);
4354-
Py_DECREF(module);
4355-
return NULL;
4356-
}
4357-
Py_XINCREF(pgSurface_Type.tp_dict);
4358-
if (PyModule_AddObject(module, "_dict", pgSurface_Type.tp_dict)) {
4359-
Py_XDECREF(pgSurface_Type.tp_dict);
4360-
Py_DECREF(module);
4361-
return NULL;
4362-
}
4363-
return module;
4376+
return PyModuleDef_Init(&_module);
43644377
}

0 commit comments

Comments
 (0)