From 6efa50a384f61155cd5315cd32f0f8775fe124c5 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Mon, 7 May 2012 21:41:59 +0200 Subject: Issue #14583: Fix importlib bug when a package's __init__.py would first import one of its modules then raise an error. --- Python/import.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'Python/import.c') diff --git a/Python/import.c b/Python/import.c index fd42a892a1..a8b1aa3fd1 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1633,19 +1633,20 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals, goto error_with_unlock; } + if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) { + /* No dot in module name, simple exit */ + Py_DECREF(partition); + final_mod = mod; + Py_INCREF(mod); + goto exit_with_unlock; + } + front = PyTuple_GET_ITEM(partition, 0); Py_INCREF(front); Py_DECREF(partition); if (level == 0) { - final_mod = PyDict_GetItem(interp->modules, front); - if (final_mod == NULL) { - PyErr_Format(PyExc_KeyError, - "%R not in sys.modules as expected", front); - } - else { - Py_INCREF(final_mod); - } + final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL); Py_DECREF(front); } else { @@ -1682,6 +1683,8 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals, fromlist, builtins_import, NULL); } + + exit_with_unlock: error_with_unlock: #ifdef WITH_THREAD if (_PyImport_ReleaseLock() < 0) { -- cgit v1.2.1