Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add the Py_mod_multiple_interpreters module def slot.
  • Loading branch information
ericsnowcurrently committed May 3, 2023
commit 7afb0050881e16d7b8e9f13622cb4621db916f9e
3 changes: 2 additions & 1 deletion Include/moduleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ struct PyModuleDef_Slot {

#define Py_mod_create 1
#define Py_mod_exec 2
#define Py_mod_multiple_interpreters 3

#ifndef Py_LIMITED_API
#define _Py_mod_LAST_SLOT 2
#define _Py_mod_LAST_SLOT 3
#endif

#endif /* New in 3.5 */
Expand Down
28 changes: 28 additions & 0 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_versio
PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
PyObject *nameobj;
PyObject *m = NULL;
Py_ssize_t multiple_interpreters = -1;
int has_execution_slots = 0;
const char *name;
int ret;
Expand Down Expand Up @@ -287,6 +288,16 @@ PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_versio
case Py_mod_exec:
has_execution_slots = 1;
break;
case Py_mod_multiple_interpreters:
if (multiple_interpreters >= 0) {
PyErr_Format(
PyExc_SystemError,
"module %s has more than one 'multiple interpreters' slots",
name);
goto error;
}
multiple_interpreters = (Py_ssize_t)cur_slot->value;
break;
default:
assert(cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT);
PyErr_Format(
Expand All @@ -297,6 +308,20 @@ PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_versio
}
}

/* By default, multi-phase init modules are expected
to work under multiple interpreters. */
if (multiple_interpreters < 0) {
multiple_interpreters = 1;
}
if (!multiple_interpreters) {
PyInterpreterState *interp = _PyInterpreterState_GET();
if (!_Py_IsMainInterpreter(interp)
&& _PyImport_CheckSubinterpIncompatibleExtensionAllowed(name) < 0)
{
goto error;
}
}

if (create) {
m = create(spec, def);
if (m == NULL) {
Expand Down Expand Up @@ -421,6 +446,9 @@ PyModule_ExecDef(PyObject *module, PyModuleDef *def)
return -1;
}
break;
case Py_mod_multiple_interpreters:
/* handled in PyModule_FromDefAndSpec2 */
break;
default:
PyErr_Format(
PyExc_SystemError,
Expand Down