Mercurial > cpython-lru_cache-weakref
diff Python/import.c @ 76329:005fd1fe31ab
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.
| author | Brett Cannon <brett@python.org> |
|---|---|
| date | Sun, 15 Apr 2012 15:24:04 -0400 |
| parents | c9fb9f5e16e7 |
| children | d777f854a66e |
line wrap: on
line diff
--- a/Python/import.c Sun Apr 15 14:15:31 2012 -0400 +++ b/Python/import.c Sun Apr 15 15:24:04 2012 -0400 @@ -3019,15 +3019,22 @@ Py_DECREF(partition); if (level == 0) { - final_mod = PyDict_GetItem(interp->modules, front); + if (PyUnicode_GET_LENGTH(name) == + PyUnicode_GET_LENGTH(front)) { + final_mod = mod; + } + else { + 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) { - PyErr_Format(PyExc_KeyError, - "%R not in sys.modules as expected", front); + goto error_with_unlock; } - else { - Py_INCREF(final_mod); - } + Py_INCREF(final_mod); } else { Py_ssize_t cut_off = PyUnicode_GetLength(name) -
