Skip to content

Commit 1e4af72

Browse files
Check the module returned by import_find_extension() to ensure single-phase init.
1 parent a8c9468 commit 1e4af72

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

Python/import.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,9 +1549,14 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
15491549
{
15501550
PyModuleDef *def = NULL;
15511551
PyObject *mod = import_find_extension(tstate, name, name);
1552-
if (mod || _PyErr_Occurred(tstate)) {
1552+
if (mod != NULL) {
1553+
assert(!_PyErr_Occurred(tstate));
1554+
assert(is_singlephase(_PyModule_GetDef(mod)));
15531555
return mod;
15541556
}
1557+
else if (_PyErr_Occurred(tstate)) {
1558+
return NULL;
1559+
}
15551560

15561561
struct _inittab *found = NULL;
15571562
for (struct _inittab *p = INITTAB; p->name != NULL; p++) {
@@ -3922,20 +3927,23 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
39223927
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
39233928
{
39243929
PyObject *mod = NULL;
3930+
PyThreadState *tstate = _PyThreadState_GET();
39253931

39263932
struct _Py_ext_module_loader_info info;
39273933
if (_Py_ext_module_loader_info_init_from_spec(&info, spec) < 0) {
39283934
return NULL;
39293935
}
39303936

3931-
PyThreadState *tstate = _PyThreadState_GET();
39323937
mod = import_find_extension(tstate, info.name, info.filename);
3933-
if (mod != NULL || _PyErr_Occurred(tstate)) {
3934-
assert(mod == NULL || !_PyErr_Occurred(tstate));
3938+
if (mod != NULL) {
3939+
assert(!_PyErr_Occurred(tstate));
3940+
assert(is_singlephase(_PyModule_GetDef(mod)));
39353941
goto finally;
39363942
}
3937-
3938-
/* Is multi-phase init or this is the first time being loaded. */
3943+
else if (_PyErr_Occurred(tstate)) {
3944+
goto finally;
3945+
}
3946+
/* Otherwise it must be multi-phase init or the first time it's loaded. */
39393947

39403948
if (PySys_Audit("import", "OOOOO", info.name, info.filename,
39413949
Py_None, Py_None, Py_None) < 0)

0 commit comments

Comments
 (0)