From 64783262a563914eafc9407a8095d811075663f0 Mon Sep 17 00:00:00 2001 From: Andrew Coffey Date: Fri, 28 Feb 2025 13:35:54 -0600 Subject: [PATCH 1/3] Surface is now multi-phase initialization --- src_c/surface.c | 105 +++++++++++++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 46 deletions(-) diff --git a/src_c/surface.c b/src_c/surface.c index 9424452359..fac4543ec7 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -4174,17 +4174,71 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj, static PyMethodDef _surface_methods[] = {{NULL, NULL, 0, NULL}}; -MODINIT_DEFINE(surface) +int +exec_surface(PyObject *module) { - PyObject *module, *apiobj; + PyObject *apiobj; static void *c_api[PYGAMEAPI_SURFACE_NUMSLOTS]; + if (pg_warn_simd_at_runtime_but_uncompiled() < 0) { + Py_DECREF(module); + return -1; + } + + Py_INCREF(&pgSurface_Type); + if (PyModule_AddObjectRef(module, "SurfaceType", + (PyObject *)&pgSurface_Type)) { + Py_DECREF(module); + return -1; + } + + Py_INCREF(&pgSurface_Type); + if (PyModule_AddObjectRef(module, "Surface", + (PyObject *)&pgSurface_Type)) { + Py_DECREF(module); + return -1; + } + + /* export the c api */ + c_api[0] = &pgSurface_Type; + c_api[1] = pgSurface_New2; + c_api[2] = pgSurface_Blit; + c_api[3] = pgSurface_SetSurface; + apiobj = encapsulate_api(c_api, "surface"); + if (PyModule_AddObjectRef(module, PYGAMEAPI_LOCAL_ENTRY, apiobj)) { + Py_DECREF(module); + return -1; + } + + Py_XINCREF(pgSurface_Type.tp_dict); + if (PyModule_AddObjectRef(module, "_dict", pgSurface_Type.tp_dict)) { + Py_DECREF(module); + return -1; + } + + return 0; +} + +MODINIT_DEFINE(surface) +{ + static PyModuleDef_Slot surf_slots[] = { + {Py_mod_exec, &exec_surface}, +#if PY_VERSION_HEX >= 0x030c0000 + {Py_mod_multiple_interpreters, + Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED}, // TODO: see if this can + // be supported later +#endif +#if PY_VERSION_HEX >= 0x030d0000 + {Py_mod_gil, Py_MOD_GIL_USED}, // TODO: support this later +#endif + {0, NULL}}; + static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT, "surface", DOC_SURFACE, - -1, + 0, _surface_methods, - NULL, + surf_slots, NULL, NULL, NULL}; @@ -4218,46 +4272,5 @@ MODINIT_DEFINE(surface) return NULL; } - /* create the module */ - module = PyModule_Create(&_module); - if (module == NULL) { - return NULL; - } - if (pg_warn_simd_at_runtime_but_uncompiled() < 0) { - Py_DECREF(module); - return NULL; - } - Py_INCREF(&pgSurface_Type); - if (PyModule_AddObject(module, "SurfaceType", - (PyObject *)&pgSurface_Type)) { - Py_DECREF(&pgSurface_Type); - Py_DECREF(module); - return NULL; - } - - Py_INCREF(&pgSurface_Type); - if (PyModule_AddObject(module, "Surface", (PyObject *)&pgSurface_Type)) { - Py_DECREF(&pgSurface_Type); - Py_DECREF(module); - return NULL; - } - - /* export the c api */ - c_api[0] = &pgSurface_Type; - c_api[1] = pgSurface_New2; - c_api[2] = pgSurface_Blit; - c_api[3] = pgSurface_SetSurface; - apiobj = encapsulate_api(c_api, "surface"); - if (PyModule_AddObject(module, PYGAMEAPI_LOCAL_ENTRY, apiobj)) { - Py_XDECREF(apiobj); - Py_DECREF(module); - return NULL; - } - Py_XINCREF(pgSurface_Type.tp_dict); - if (PyModule_AddObject(module, "_dict", pgSurface_Type.tp_dict)) { - Py_XDECREF(pgSurface_Type.tp_dict); - Py_DECREF(module); - return NULL; - } - return module; + return PyModuleDef_Init(&_module); } From 68bcd86fff0323dac0edd087ccc2e1e103cb5fa7 Mon Sep 17 00:00:00 2001 From: Andrew Coffey Date: Sat, 1 Mar 2025 20:20:04 -0600 Subject: [PATCH 2/3] m_size actually zero now --- src_c/surface.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src_c/surface.c b/src_c/surface.c index fac4543ec7..83d92ba605 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -53,10 +53,10 @@ typedef enum { } SurfViewKind; /* To avoid problems with non-const Py_buffer format field */ -static char FormatUint8[] = "B"; -static char FormatUint16[] = "=H"; -static char FormatUint24[] = "3x"; -static char FormatUint32[] = "=I"; +#define FormatUint8 "B" +#define FormatUint16 "=H" +#define FormatUint24 "3x" +#define FormatUint32 "=I" typedef struct pg_bufferinternal_s { PyObject *consumer_ref; /* A weak reference to a bufferproxy object */ From 5b610195eb63438f1b254cd3964faf9dc56a3b05 Mon Sep 17 00:00:00 2001 From: Andrew Coffey Date: Sat, 8 Mar 2025 21:55:53 -0600 Subject: [PATCH 3/3] Removed unnecessary module decrefs and strong refs in exec_surface --- src_c/surface.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src_c/surface.c b/src_c/surface.c index 2d03edb7f7..14b4511c5f 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -4282,21 +4282,16 @@ exec_surface(PyObject *module) static void *c_api[PYGAMEAPI_SURFACE_NUMSLOTS]; if (pg_warn_simd_at_runtime_but_uncompiled() < 0) { - Py_DECREF(module); return -1; } - Py_INCREF(&pgSurface_Type); if (PyModule_AddObjectRef(module, "SurfaceType", (PyObject *)&pgSurface_Type)) { - Py_DECREF(module); return -1; } - Py_INCREF(&pgSurface_Type); if (PyModule_AddObjectRef(module, "Surface", (PyObject *)&pgSurface_Type)) { - Py_DECREF(module); return -1; } @@ -4307,13 +4302,10 @@ exec_surface(PyObject *module) c_api[3] = pgSurface_SetSurface; apiobj = encapsulate_api(c_api, "surface"); if (PyModule_AddObjectRef(module, PYGAMEAPI_LOCAL_ENTRY, apiobj)) { - Py_DECREF(module); return -1; } - Py_XINCREF(pgSurface_Type.tp_dict); if (PyModule_AddObjectRef(module, "_dict", pgSurface_Type.tp_dict)) { - Py_DECREF(module); return -1; }