Skip to content

Commit 2adb4b2

Browse files
authored
controller is not multiphase init + workaround for wasm 313t (#3137)
1 parent 9ef9855 commit 2adb4b2

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

src_c/_sdl2/controller.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,15 @@ static PyTypeObject pgController_Type = {
544544
.tp_members = controller_members,
545545
};
546546

547+
/* TODO: multiphase init
548+
#if PY_VERSION_HEX >= 0x030D0000
549+
static struct PyModuleDef_Slot mod_controller_slots[] = {
550+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
551+
{0, NULL}
552+
};
553+
#endif
554+
*/
555+
547556
MODINIT_DEFINE(controller)
548557
{
549558
PyObject *module;
@@ -553,7 +562,13 @@ MODINIT_DEFINE(controller)
553562
.m_name = "controller",
554563
.m_doc = DOC_SDL2_CONTROLLER,
555564
.m_size = -1,
556-
.m_methods = _controller_module_methods};
565+
.m_methods = _controller_module_methods,
566+
/*
567+
#if PY_VERSION_HEX >= 0x030D0000
568+
.m_slots = mod_controller_slots,
569+
#endif
570+
*/
571+
};
557572

558573
import_pygame_base();
559574
if (PyErr_Occurred()) {
@@ -566,7 +581,11 @@ MODINIT_DEFINE(controller)
566581
}
567582

568583
module = PyModule_Create(&_module);
569-
584+
#if Py_GIL_DISABLED
585+
#if (defined(__EMSCRIPTEN__) || defined(__wasi__))
586+
PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED);
587+
#endif
588+
#endif
570589
if (!module) {
571590
return NULL;
572591
}

src_c/static.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#define NO_PYGAME_C_API
22

3+
#define CONTROLLER_NOPYX
4+
35
#define PYGAMEAPI_RECT_INTERNAL
46
#define PYGAMEAPI_EVENT_INTERNAL
57
#define PYGAMEAPI_JOYSTICK_INTERNAL
@@ -150,11 +152,13 @@ PyInit_mixer(void);
150152
PyMODINIT_FUNC
151153
PyInit_system(void);
152154

155+
#if defined(CONTROLLER_NOPYX)
153156
PyMODINIT_FUNC
154157
PyInit_controller(void);
155-
158+
#else
156159
PyMODINIT_FUNC
157160
PyInit_controller_old(void);
161+
#endif
158162

159163
PyMODINIT_FUNC
160164
PyInit_transform(void);
@@ -216,48 +220,44 @@ load_submodule_mphase(const char *parent, PyObject *mdef, PyObject *spec,
216220
{
217221
char fqn[1024];
218222
snprintf(fqn, sizeof(fqn), "%s.%s", parent, alias);
219-
220223
PyObject *modules = PyImport_GetModuleDict();
221224

222225
Py_DECREF(PyObject_GetAttrString(spec, "name"));
223-
224226
PyObject_SetAttrString(spec, "name", PyUnicode_FromString(alias));
225-
226227
PyObject *pmod = PyDict_GetItemString(modules, parent);
227-
228228
PyObject *mod = PyModule_FromDefAndSpec((PyModuleDef *)mdef, spec);
229-
230229
PyDict_SetItemString(PyModule_GetDict(mod), "__package__",
231230
PyUnicode_FromString(parent));
232-
233231
// TODO SET PACKAGE
234-
235232
PyModule_ExecDef(mod, (PyModuleDef *)mdef);
236233

237-
if (!pmod) {
238-
snprintf(fqn, sizeof(fqn), "ERROR: %s.%s", parent, alias);
239-
puts(fqn);
240-
PyErr_Print();
241-
PyErr_Clear();
242-
}
243-
else {
234+
if (pmod) {
244235
PyDict_SetItemString(modules, fqn, mod);
245236
PyDict_SetItemString(PyModule_GetDict(mod), "__name__",
246237
PyUnicode_FromString(fqn));
247238
PyModule_AddObjectRef(pmod, alias, mod);
248239
Py_XDECREF(mod);
249240
}
241+
if (!pmod || PyErr_Occurred()) {
242+
snprintf(fqn, sizeof(fqn), "Error after init in : %s.%s\n", parent,
243+
alias);
244+
fputs(fqn, stderr);
245+
PyErr_Print();
246+
PyErr_Clear();
247+
}
250248
}
251249

252250
static PyObject *
253251
mod_pygame_import_cython(PyObject *self, PyObject *spec)
254252
{
255253
load_submodule_mphase("pygame._sdl2", PyInit_sdl2(), spec, "sdl2");
256254
load_submodule_mphase("pygame._sdl2", PyInit_mixer(), spec, "mixer");
255+
#if defined(CONTROLLER_NOPYX)
256+
load_submodule("pygame._sdl2", PyInit_controller(), "controller");
257+
#else
257258
load_submodule_mphase("pygame._sdl2", PyInit_controller_old(), spec,
258259
"controller_old");
259-
load_submodule_mphase("pygame._sdl2", PyInit_controller(), spec,
260-
"controller");
260+
#endif
261261
load_submodule_mphase("pygame._sdl2", PyInit_audio(), spec, "audio");
262262
load_submodule_mphase("pygame._sdl2", PyInit_video(), spec, "video");
263263

0 commit comments

Comments
 (0)