gh-141780: Make PyModule_FromSlotsAndSpec enable GIL if needed (GH-141785)

This commit is contained in:
Petr Viktorin 2025-11-24 13:26:35 +01:00 committed by GitHub
parent 6462322840
commit bf66bce4ee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 183 additions and 22 deletions

View file

@ -2,6 +2,7 @@
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_EnableGILTransient()
#include "pycore_dict.h" // _PyDict_EnablePerThreadRefcounting()
#include "pycore_fileutils.h" // _Py_wgetcwd
#include "pycore_import.h" // _PyImport_GetNextModuleIndex()
@ -522,6 +523,22 @@ module_from_def_and_spec(
#undef COPY_COMMON_SLOT
}
#ifdef Py_GIL_DISABLED
// For modules created directly from slots (not from a def), we enable
// the GIL here (pairing `_PyEval_EnableGILTransient` with
// an immediate `_PyImport_EnableGILAndWarn`).
// For modules created from a def, the caller is responsible for this.
if (!original_def && requires_gil) {
PyThreadState *tstate = _PyThreadState_GET();
if (_PyEval_EnableGILTransient(tstate) < 0) {
goto error;
}
if (_PyImport_EnableGILAndWarn(tstate, nameobj) < 0) {
goto error;
}
}
#endif
/* By default, multi-phase init modules are expected
to work under multiple interpreters. */
if (!has_multiple_interpreters_slot) {