Skip to content

Commit

Permalink
Rework uwsgi module handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lalufu committed Dec 26, 2023
1 parent b693940 commit 973fc9a
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions plugins/python/python_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,6 @@ void uwsgi_python_pthread_child(void) {
PyMethodDef uwsgi_spit_method[] = { {"uwsgi_spit", py_uwsgi_spit, METH_VARARGS, ""} };
PyMethodDef uwsgi_write_method[] = { {"uwsgi_write", py_uwsgi_write, METH_VARARGS, ""} };

PyDoc_STRVAR(uwsgi_py_doc, "uWSGI api module.");

#ifdef PYTHREE
static PyModuleDef uwsgi_module3 = {
PyModuleDef_HEAD_INIT,
"uwsgi",
uwsgi_py_doc,
-1,
NULL,
};
PyObject *init_uwsgi3(void) {
return PyModule_Create(&uwsgi_module3);
}
#endif

int uwsgi_python_init() {

char *pyversion = strchr(Py_GetVersion(), '\n');
Expand Down Expand Up @@ -313,9 +298,6 @@ int uwsgi_python_init() {
wchar_t *pname = uwsgi_calloc(sizeof(wchar_t) * (strlen(program_name)+1));
mbstowcs(pname, program_name, strlen(program_name)+1);
Py_SetProgramName(pname);
#ifdef UWSGI_PY312
PyImport_AppendInittab("uwsgi", init_uwsgi3);
#endif
#else
Py_SetProgramName(program_name);
#endif
Expand Down Expand Up @@ -678,6 +660,21 @@ void init_uwsgi_vars() {



PyDoc_STRVAR(uwsgi_py_doc, "uWSGI api module.");

#ifdef PYTHREE
static PyModuleDef uwsgi_module3 = {
PyModuleDef_HEAD_INIT,
"uwsgi",
uwsgi_py_doc,
-1,
NULL,
};
PyObject *init_uwsgi3(void) {
return PyModule_Create(&uwsgi_module3);
}
#endif

void init_uwsgi_embedded_module() {
PyObject *new_uwsgi_module, *zero;
int i;
Expand All @@ -700,11 +697,21 @@ void init_uwsgi_embedded_module() {
#ifdef PYTHREE
#ifndef UWSGI_PY312
PyImport_AppendInittab("uwsgi", init_uwsgi3);
#endif
new_uwsgi_module = PyImport_AddModule("uwsgi");
#else
#else // UWSGI_PY312
// From python 3.12 onwards, PyImport_AppendInittab() can no
// longer be called after Py_Initialize(). Dynamically
// add the module instead
PyObject *sys_modules;
PyImport_AddModule("uwsgi");
new_uwsgi_module = init_uwsgi3();
sys_modules = PyImport_GetModuleDict();
PyDict_SetItemString(sys_modules, "uwsgi", new_uwsgi_module);
Py_DECREF(new_uwsgi_module);
#endif // UWSGI_PY312
#else // PYTHREE
new_uwsgi_module = Py_InitModule3("uwsgi", NULL, uwsgi_py_doc);
#endif
#endif // PYTHREE
if (new_uwsgi_module == NULL) {
uwsgi_log("could not initialize the uwsgi python module\n");
exit(1);
Expand Down

0 comments on commit 973fc9a

Please sign in to comment.