diff --git a/src_c/surface.c b/src_c/surface.c index ada724a9a3..76ff3bd785 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -52,10 +52,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 */ @@ -4597,69 +4597,53 @@ 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; - static void *c_api[PYGAMEAPI_SURFACE_NUMSLOTS]; - - static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT, - "surface", - DOC_SURFACE, - -1, - _surface_methods, - NULL, - NULL, - 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; + return -1; } import_pygame_color(); if (PyErr_Occurred()) { - return NULL; + return -1; } import_pygame_rect(); if (PyErr_Occurred()) { - return NULL; + return -1; } import_pygame_bufferproxy(); if (PyErr_Occurred()) { - return NULL; + return -1; } _IMPORT_PYGAME_MODULE(surflock); if (PyErr_Occurred()) { - return NULL; + return -1; } /* type preparation */ if (PyType_Ready(&pgSurface_Type) < 0) { - return NULL; + return -1; } - /* create the module */ - module = PyModule_Create(&_module); - if (module == NULL) { - return NULL; - } + PyObject *apiobj; + static void *c_api[PYGAMEAPI_SURFACE_NUMSLOTS]; + if (pg_warn_simd_at_runtime_but_uncompiled() < 0) { - Py_DECREF(module); - return NULL; + return -1; } + if (PyModule_AddObjectRef(module, "SurfaceType", (PyObject *)&pgSurface_Type)) { - Py_DECREF(module); - return NULL; + return -1; } if (PyModule_AddObjectRef(module, "Surface", (PyObject *)&pgSurface_Type)) { - Py_DECREF(module); - return NULL; + return -1; } /* export the c api */ @@ -4668,14 +4652,40 @@ MODINIT_DEFINE(surface) 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; + if (PyModule_Add(module, PYGAMEAPI_LOCAL_ENTRY, apiobj) < 0) { + return -1; } + if (PyModule_AddObjectRef(module, "_dict", pgSurface_Type.tp_dict)) { - Py_DECREF(module); - return NULL; + return -1; } - return module; + + 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, + 0, + _surface_methods, + surf_slots, + NULL, + NULL, + NULL}; + + return PyModuleDef_Init(&_module); }