Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Include/internal/pycore_moduleobject.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,7 +30,7 @@ typedef struct{
PyObject *md_name;
bool md_token_is_def; /* if true, `md_token` is the PyModuleDef */
#ifdef Py_GIL_DISABLED
void *md_gil;
bool md_requires_gil;
#endif
Py_ssize_t md_state_size;
traverseproc md_state_traverse;
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_sys.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -1725,7 +1725,7 @@ def get_gen(): yield 1
check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit)
# module
if support.Py_GIL_DISABLED:
md_gil = 'P'
md_gil = '?'
else:
md_gil = ''
check(unittest, size('PPPP?' + md_gil + 'NPPPPP'))
Expand Down
15 changes: 8 additions & 7 deletions Objects/moduleobject.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -178,7 +178,7 @@ new_module_notrack(PyTypeObject *mt)
m->md_name = NULL;
m->md_token_is_def = false;
#ifdef Py_GIL_DISABLED
m->md_gil = Py_MOD_GIL_USED;
m->md_requires_gil = true;
#endif
m->md_state_size = 0;
m->md_state_traverse = NULL;
Expand DownExpand Up@@ -361,7 +361,7 @@ _PyModule_CreateInitialized(PyModuleDef* module, int module_api_version)
m->md_token_is_def = true;
module_copy_members_from_deflike(m, module);
#ifdef Py_GIL_DISABLED
m->md_gil = Py_MOD_GIL_USED;
m->md_requires_gil = true;
#endif
return (PyObject*)m;
}
Expand All@@ -380,7 +380,7 @@ module_from_def_and_spec(
int has_multiple_interpreters_slot = 0;
void *multiple_interpreters = (void *)0;
int has_gil_slot = 0;
void *gil_slot = Py_MOD_GIL_USED;
bool requires_gil = true;
int has_execution_slots = 0;
const char *name;
int ret;
Expand DownExpand Up@@ -474,7 +474,7 @@ module_from_def_and_spec(
name);
goto error;
}
gil_slot = cur_slot->value;
requires_gil = (cur_slot->value != Py_MOD_GIL_NOT_USED);
has_gil_slot = 1;
break;
case Py_mod_abi:
Expand DownExpand Up@@ -581,9 +581,9 @@ module_from_def_and_spec(
mod->md_token = token;
}
#ifdef Py_GIL_DISABLED
mod->md_gil = gil_slot;
mod->md_requires_gil = requires_gil;
#else
(void)gil_slot;
(void)requires_gil;
#endif
mod->md_exec = m_exec;
} else{
Expand DownExpand Up@@ -664,11 +664,12 @@ PyModule_FromSlotsAndSpec(const PyModuleDef_Slot *slots, PyObject *spec)
int
PyUnstable_Module_SetGIL(PyObject *module, void *gil)
{
bool requires_gil = (gil != Py_MOD_GIL_NOT_USED);
if (!PyModule_Check(module)){
PyErr_BadInternalCall();
return -1;
}
((PyModuleObject *)module)->md_gil = gil;
((PyModuleObject *)module)->md_requires_gil = requires_gil;
return 0;
}
#endif
Expand Down
26 changes: 14 additions & 12 deletions Python/import.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -1017,9 +1017,10 @@ struct extensions_cache_value{
_Py_ext_module_origin origin;

#ifdef Py_GIL_DISABLED
/* The module's md_gil slot, for legacy modules that are reinitialized from
m_dict rather than calling their initialization function again. */
void *md_gil;
/* The module's md_requires_gil member, for legacy modules that are
* reinitialized from m_dict rather than calling their initialization
* function again. */
bool md_requires_gil;
#endif
};

Expand DownExpand Up@@ -1350,7 +1351,7 @@ static struct extensions_cache_value *
_extensions_cache_set(PyObject *path, PyObject *name,
PyModuleDef *def, PyModInitFunction m_init,
Py_ssize_t m_index, PyObject *m_dict,
_Py_ext_module_origin origin, void *md_gil)
_Py_ext_module_origin origin, bool requires_gil)
{
struct extensions_cache_value *value = NULL;
void *key = NULL;
Expand DownExpand Up@@ -1405,11 +1406,11 @@ _extensions_cache_set(PyObject *path, PyObject *name,
/* m_dict is set by set_cached_m_dict(). */
.origin=origin,
#ifdef Py_GIL_DISABLED
.md_gil=md_gil,
.md_requires_gil=requires_gil,
#endif
};
#ifndef Py_GIL_DISABLED
(void)md_gil;
(void)requires_gil;
#endif
if (init_cached_m_dict(newvalue, m_dict) < 0){
goto finally;
Expand DownExpand Up@@ -1547,7 +1548,8 @@ _PyImport_CheckGILForModule(PyObject* module, PyObject *module_name)
}

if (!PyModule_Check(module) ||
((PyModuleObject *)module)->md_gil == Py_MOD_GIL_USED){
((PyModuleObject *)module)->md_requires_gil)
{
if (_PyEval_EnableGILPermanent(tstate)){
int warn_result = PyErr_WarnFormat(
PyExc_RuntimeWarning,
Expand DownExpand Up@@ -1725,7 +1727,7 @@ struct singlephase_global_update{
Py_ssize_t m_index;
PyObject *m_dict;
_Py_ext_module_origin origin;
void *md_gil;
bool md_requires_gil;
};

static struct extensions_cache_value *
Expand DownExpand Up@@ -1784,7 +1786,7 @@ update_global_state_for_extension(PyThreadState *tstate,
#endif
cached = _extensions_cache_set(
path, name, def, m_init, singlephase->m_index, m_dict,
singlephase->origin, singlephase->md_gil);
singlephase->origin, singlephase->md_requires_gil);
if (cached == NULL){
// XXX Ignore this error? Doing so would effectively
// mark the module as not loadable.
Expand DownExpand Up@@ -1873,7 +1875,7 @@ reload_singlephase_extension(PyThreadState *tstate,
if (def->m_base.m_copy != NULL){
// For non-core modules, fetch the GIL slot that was stored by
// import_run_extension().
((PyModuleObject *)mod)->md_gil = cached->md_gil;
((PyModuleObject *)mod)->md_requires_gil = cached->md_requires_gil;
}
#endif
/* We can't set mod->md_def if it's missing,
Expand DownExpand Up@@ -2128,7 +2130,7 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
.m_index=def->m_base.m_index,
.origin=info->origin,
#ifdef Py_GIL_DISABLED
.md_gil=((PyModuleObject *)mod)->md_gil,
.md_requires_gil=((PyModuleObject *)mod)->md_requires_gil,
#endif
};
// gh-88216: Extensions and def->m_base.m_copy can be updated
Expand DownExpand Up@@ -2323,7 +2325,7 @@ _PyImport_FixupBuiltin(PyThreadState *tstate, PyObject *mod, const char *name,
.origin=_Py_ext_module_origin_CORE,
#ifdef Py_GIL_DISABLED
/* Unused when m_dict == NULL. */
.md_gil=NULL,
.md_requires_gil=false,
#endif
};
cached = update_global_state_for_extension(
Expand Down
Loading