Skip to content

Surface multiphase init #3354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 52 additions & 42 deletions src_c/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand All @@ -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);
}
Loading