gh-116322: Add Py_mod_gil module slot (#116882)

This PR adds the ability to enable the GIL if it was disabled at
interpreter startup, and modifies the multi-phase module initialization
path to enable the GIL when loading a module, unless that module's spec
includes a slot indicating it can run safely without the GIL.

PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went
with `Py_MOD_GIL_NOT_USED` for consistency with gh-104148.

A warning will be issued up to once per interpreter for the first
GIL-using module that is loaded. If `-v` is given, a shorter message
will be printed to stderr every time a GIL-using module is loaded
(including the first one that issues a warning).
This commit is contained in:
Brett Simmers 2024-05-03 08:30:55 -07:00 committed by GitHub
parent 3e818afb9b
commit c2627d6eea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
123 changed files with 376 additions and 62 deletions

View file

@ -6,18 +6,24 @@
#include "pyconfig.h" // Py_GIL_DISABLED
#ifndef Py_GIL_DISABLED
# define Py_LIMITED_API 0x03020000
# define Py_LIMITED_API 0x030d0000
#endif
#include <Python.h>
static PyModuleDef_Slot shared_slots[] = {
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
{0, NULL},
};
static struct PyModuleDef _testimportmultiple = {
PyModuleDef_HEAD_INIT,
"_testimportmultiple",
"_testimportmultiple doc",
-1,
NULL,
0,
NULL,
shared_slots,
NULL,
NULL,
NULL
@ -25,16 +31,16 @@ static struct PyModuleDef _testimportmultiple = {
PyMODINIT_FUNC PyInit__testimportmultiple(void)
{
return PyModule_Create(&_testimportmultiple);
return PyModuleDef_Init(&_testimportmultiple);
}
static struct PyModuleDef _foomodule = {
PyModuleDef_HEAD_INIT,
"_testimportmultiple_foo",
"_testimportmultiple_foo doc",
-1,
NULL,
0,
NULL,
shared_slots,
NULL,
NULL,
NULL
@ -42,21 +48,21 @@ static struct PyModuleDef _foomodule = {
PyMODINIT_FUNC PyInit__testimportmultiple_foo(void)
{
return PyModule_Create(&_foomodule);
return PyModuleDef_Init(&_foomodule);
}
static struct PyModuleDef _barmodule = {
PyModuleDef_HEAD_INIT,
"_testimportmultiple_bar",
"_testimportmultiple_bar doc",
-1,
NULL,
0,
NULL,
shared_slots,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC PyInit__testimportmultiple_bar(void){
return PyModule_Create(&_barmodule);
return PyModuleDef_Init(&_barmodule);
}