@@ -1550,25 +1550,9 @@ _PyImport_CheckGILForModule(PyObject* module, PyObject *module_name)
15501550 if (!PyModule_Check (module ) ||
15511551 ((PyModuleObject * )module )-> md_requires_gil )
15521552 {
1553- if (_PyEval_EnableGILPermanent (tstate )) {
1554- int warn_result = PyErr_WarnFormat (
1555- PyExc_RuntimeWarning ,
1556- 1 ,
1557- "The global interpreter lock (GIL) has been enabled to load "
1558- "module '%U', which has not declared that it can run safely "
1559- "without the GIL. To override this behavior and keep the GIL "
1560- "disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0." ,
1561- module_name
1562- );
1563- if (warn_result < 0 ) {
1564- return warn_result ;
1565- }
1566- }
1567-
1568- const PyConfig * config = _PyInterpreterState_GetConfig (tstate -> interp );
1569- if (config -> enable_gil == _PyConfig_GIL_DEFAULT && config -> verbose ) {
1570- PySys_FormatStderr ("# loading module '%U', which requires the GIL\n" ,
1571- module_name );
1553+ assert (((PyModuleObject * )module )-> md_token_is_def );
1554+ if (_PyImport_EnableGILAndWarn (tstate , module_name ) < 0 ) {
1555+ return -1 ;
15721556 }
15731557 }
15741558 else {
@@ -1577,6 +1561,27 @@ _PyImport_CheckGILForModule(PyObject* module, PyObject *module_name)
15771561
15781562 return 0 ;
15791563}
1564+
1565+ int
1566+ _PyImport_EnableGILAndWarn (PyThreadState * tstate , PyObject * module_name ) {
1567+ if (_PyEval_EnableGILPermanent (tstate )) {
1568+ return PyErr_WarnFormat (
1569+ PyExc_RuntimeWarning ,
1570+ 1 ,
1571+ "The global interpreter lock (GIL) has been enabled to load "
1572+ "module '%U', which has not declared that it can run safely "
1573+ "without the GIL. To override this behavior and keep the GIL "
1574+ "disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0." ,
1575+ module_name
1576+ );
1577+ }
1578+ const PyConfig * config = _PyInterpreterState_GetConfig (tstate -> interp );
1579+ if (config -> enable_gil == _PyConfig_GIL_DEFAULT && config -> verbose ) {
1580+ PySys_FormatStderr ("# loading module '%U', which requires the GIL\n" ,
1581+ module_name );
1582+ }
1583+ return 0 ;
1584+ }
15801585#endif
15811586
15821587static PyThreadState *
@@ -4785,6 +4790,8 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
47854790 _PyImport_GetModuleExportHooks (& info , fp , & p0 , & ex0 );
47864791 if (ex0 ) {
47874792 mod = import_run_modexport (tstate , ex0 , & info , spec );
4793+ // Modules created from slots handle GIL enablement (Py_mod_gil slot)
4794+ // when they're created.
47884795 goto cleanup ;
47894796 }
47904797 if (p0 == NULL ) {
0 commit comments