diff options
author | Brett Cannon <brett@python.org> | 2012-04-15 19:24:04 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-04-15 19:24:04 (GMT) |
commit | 881535b726c595582920acc69f26b053e32b85b0 (patch) | |
tree | ca3bcfcff049f02a4dc5e1187b0a9e5d1beb489d /Python/import.c | |
parent | 27fc52877ce3cabb2ed524ab0f40f8c8e3a45c18 (diff) | |
download | cpython-881535b726c595582920acc69f26b053e32b85b0.zip cpython-881535b726c595582920acc69f26b053e32b85b0.tar.gz cpython-881535b726c595582920acc69f26b053e32b85b0.tar.bz2 |
Issue #14582: Import returns the module returned by a loader instead
of sys.modules when possible.
This is being done for two reasons. One is to gain a little bit of
performance by skipping an unnecessary dict lookup in sys.modules. But
the other (and main) reason is to be a little bit more clear in how
things should work from the perspective of import's interactions with
loaders. Otherwise loaders can easily forget to return the module even
though PEP 302 explicitly states they are expected to return the module
they loaded.
Diffstat (limited to 'Python/import.c')
-rw-r--r-- | Python/import.c | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/Python/import.c b/Python/import.c index d4f5783..e96c7e4 100644 --- a/Python/import.c +++ b/Python/import.c @@ -3019,15 +3019,22 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals, Py_DECREF(partition); if (level == 0) { - final_mod = PyDict_GetItem(interp->modules, front); - Py_DECREF(front); - if (final_mod == NULL) { - PyErr_Format(PyExc_KeyError, - "%R not in sys.modules as expected", front); + if (PyUnicode_GET_LENGTH(name) == + PyUnicode_GET_LENGTH(front)) { + final_mod = mod; } else { - Py_INCREF(final_mod); + final_mod = PyDict_GetItem(interp->modules, front); + if (final_mod == NULL) { + PyErr_Format(PyExc_KeyError, + "%R not in sys.modules as expected", front); + } + } + Py_DECREF(front); + if (final_mod == NULL) { + goto error_with_unlock; } + Py_INCREF(final_mod); } else { Py_ssize_t cut_off = PyUnicode_GetLength(name) - |