Skip to content

Commit 5d4aab0

Browse files
committed
Rework uwsgi module handling
This partially reverts some of the work done in 1881628. This removes some code reordering done in the above commit to work around the fact that, starting in 3.12, `PyImport_AppendInittab()` could no longer be called after `Py_Initialize()`. This patch reverts this reordering, and takes a different approach to add the `uwsgi` python module to the plugin. This is mainly done to support the https://pypi.org/project/pyuwsgi/ use case where the uwsgi server itself is a python module, and hence the interpreter will always be initialized before uwsgi even starts. It's also a bit cleaner, there's less code getting rearranged to support 3.12.
1 parent b693940 commit 5d4aab0

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

plugins/python/python_plugin.c

+27-20
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,6 @@ void uwsgi_python_pthread_child(void) {
233233
PyMethodDef uwsgi_spit_method[] = { {"uwsgi_spit", py_uwsgi_spit, METH_VARARGS, ""} };
234234
PyMethodDef uwsgi_write_method[] = { {"uwsgi_write", py_uwsgi_write, METH_VARARGS, ""} };
235235

236-
PyDoc_STRVAR(uwsgi_py_doc, "uWSGI api module.");
237-
238-
#ifdef PYTHREE
239-
static PyModuleDef uwsgi_module3 = {
240-
PyModuleDef_HEAD_INIT,
241-
"uwsgi",
242-
uwsgi_py_doc,
243-
-1,
244-
NULL,
245-
};
246-
PyObject *init_uwsgi3(void) {
247-
return PyModule_Create(&uwsgi_module3);
248-
}
249-
#endif
250-
251236
int uwsgi_python_init() {
252237

253238
char *pyversion = strchr(Py_GetVersion(), '\n');
@@ -313,9 +298,6 @@ int uwsgi_python_init() {
313298
wchar_t *pname = uwsgi_calloc(sizeof(wchar_t) * (strlen(program_name)+1));
314299
mbstowcs(pname, program_name, strlen(program_name)+1);
315300
Py_SetProgramName(pname);
316-
#ifdef UWSGI_PY312
317-
PyImport_AppendInittab("uwsgi", init_uwsgi3);
318-
#endif
319301
#else
320302
Py_SetProgramName(program_name);
321303
#endif
@@ -678,6 +660,21 @@ void init_uwsgi_vars() {
678660

679661

680662

663+
PyDoc_STRVAR(uwsgi_py_doc, "uWSGI api module.");
664+
665+
#ifdef PYTHREE
666+
static PyModuleDef uwsgi_module3 = {
667+
PyModuleDef_HEAD_INIT,
668+
"uwsgi",
669+
uwsgi_py_doc,
670+
-1,
671+
NULL,
672+
};
673+
PyObject *init_uwsgi3(void) {
674+
return PyModule_Create(&uwsgi_module3);
675+
}
676+
#endif
677+
681678
void init_uwsgi_embedded_module() {
682679
PyObject *new_uwsgi_module, *zero;
683680
int i;
@@ -698,10 +695,20 @@ void init_uwsgi_embedded_module() {
698695

699696

700697
#ifdef PYTHREE
701-
#ifndef UWSGI_PY312
698+
# ifndef UWSGI_PY312
702699
PyImport_AppendInittab("uwsgi", init_uwsgi3);
703-
#endif
704700
new_uwsgi_module = PyImport_AddModule("uwsgi");
701+
# else
702+
// From python 3.12 onwards, PyImport_AppendInittab() can no
703+
// longer be called after Py_Initialize(). Instead, dynamically
704+
// add the module instead
705+
PyObject *sys_modules;
706+
PyImport_AddModule("uwsgi");
707+
new_uwsgi_module = init_uwsgi3();
708+
sys_modules = PyImport_GetModuleDict();
709+
PyDict_SetItemString(sys_modules, "uwsgi", new_uwsgi_module);
710+
Py_DECREF(new_uwsgi_module);
711+
# endif
705712
#else
706713
new_uwsgi_module = Py_InitModule3("uwsgi", NULL, uwsgi_py_doc);
707714
#endif

0 commit comments

Comments
 (0)