From ff307d87db35157042c10817c6c80566a414cbd6 Mon Sep 17 00:00:00 2001 From: Andrew Coffey Date: Fri, 28 Feb 2025 13:35:54 -0600 Subject: [PATCH 1/5] 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 9dde1f92ff..ba48784f57 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -4275,17 +4275,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}; @@ -4319,46 +4373,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 1daefece2206440178483310f462deab093fa950 Mon Sep 17 00:00:00 2001 From: Andrew Coffey Date: Sat, 1 Mar 2025 20:20:04 -0600 Subject: [PATCH 2/5] 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 ba48784f57..2d03edb7f7 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 e33b87aaf7748264b5dabe09f447bb0aaaddbe71 Mon Sep 17 00:00:00 2001 From: Andrew Coffey Date: Sat, 8 Mar 2025 21:55:53 -0600 Subject: [PATCH 3/5] 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; } From a0688ebe0e04c287b5ab90c19d8a8d56db70f46e Mon Sep 17 00:00:00 2001 From: Andrew Coffey Date: Sat, 24 May 2025 13:43:32 -0500 Subject: [PATCH 4/5] Replace PyModule_AddObjectRef with PyModule_Add for surface capi --- src_c/surface.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src_c/surface.c b/src_c/surface.c index 7c9cfce5e7..e542c7dbb0 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -4623,8 +4623,7 @@ exec_surface(PyObject *module) 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_XDECREF(apiobj); + if (PyModule_Add(module, PYGAMEAPI_LOCAL_ENTRY, apiobj) < 0) { return -1; } From f531d0b7626b13b08365a47c5299700db919dcc0 Mon Sep 17 00:00:00 2001 From: Andrew Coffey Date: Sun, 25 May 2025 12:59:18 -0500 Subject: [PATCH 5/5] Moved imports and PyType_Ready to exec_surface --- src_c/surface.c | 58 ++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src_c/surface.c b/src_c/surface.c index e542c7dbb0..76ff3bd785 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -4600,6 +4600,35 @@ static PyMethodDef _surface_methods[] = {{NULL, NULL, 0, NULL}}; int exec_surface(PyObject *module) { + /* imported needed apis; Do this first so if there is an error + the module is not loaded. + */ + import_pygame_base(); + if (PyErr_Occurred()) { + return -1; + } + import_pygame_color(); + if (PyErr_Occurred()) { + return -1; + } + import_pygame_rect(); + if (PyErr_Occurred()) { + return -1; + } + import_pygame_bufferproxy(); + if (PyErr_Occurred()) { + return -1; + } + _IMPORT_PYGAME_MODULE(surflock); + if (PyErr_Occurred()) { + return -1; + } + + /* type preparation */ + if (PyType_Ready(&pgSurface_Type) < 0) { + return -1; + } + PyObject *apiobj; static void *c_api[PYGAMEAPI_SURFACE_NUMSLOTS]; @@ -4658,34 +4687,5 @@ MODINIT_DEFINE(surface) NULL, NULL}; - /* imported needed apis; Do this first so if there is an error - the module is not loaded. - */ - import_pygame_base(); - if (PyErr_Occurred()) { - return NULL; - } - import_pygame_color(); - if (PyErr_Occurred()) { - return NULL; - } - import_pygame_rect(); - if (PyErr_Occurred()) { - return NULL; - } - import_pygame_bufferproxy(); - if (PyErr_Occurred()) { - return NULL; - } - _IMPORT_PYGAME_MODULE(surflock); - if (PyErr_Occurred()) { - return NULL; - } - - /* type preparation */ - if (PyType_Ready(&pgSurface_Type) < 0) { - return NULL; - } - return PyModuleDef_Init(&_module); }