diff options
author | Guido van Rossum <guido@python.org> | 2001-07-23 13:27:49 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-07-23 13:27:49 (GMT) |
commit | f48f11cd793a545251b83a6037e609a77f080c00 (patch) | |
tree | 9d8b99f5571f24298dac8375548b960a57bf0a11 | |
parent | fa712ca1ed5c04a07dca4ae6837c74509d89fa82 (diff) | |
download | cpython-f48f11cd793a545251b83a6037e609a77f080c00.zip cpython-f48f11cd793a545251b83a6037e609a77f080c00.tar.gz cpython-f48f11cd793a545251b83a6037e609a77f080c00.tar.bz2 |
SF Patch #441791, with changes: when "import foo.bar" fails with an
exception in the execution of bar, ensure that foo.bar exists.
(Previously, while sys.modules['foo.bar'] would exist, foo.bar would
only be created upon successful execution of bar. This is
inconvenient; some would say wrong. :-)
-rw-r--r-- | Python/import.c | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/Python/import.c b/Python/import.c index 7443724..456a5be 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1789,7 +1789,7 @@ static PyObject * import_submodule(PyObject *mod, char *subname, char *fullname) { PyObject *modules = PyImport_GetModuleDict(); - PyObject *m; + PyObject *m, *res = NULL; /* Require: if mod == None: subname == fullname @@ -1829,9 +1829,21 @@ import_submodule(PyObject *mod, char *subname, char *fullname) m = load_module(fullname, fp, buf, fdp->type); if (fp) fclose(fp); - if (m != NULL && mod != Py_None) { - if (PyObject_SetAttrString(mod, subname, m) < 0) { - Py_DECREF(m); + if (mod != Py_None) { + /* Irrespective of the success of this load, make a + reference to it in the parent package module. + A copy gets saved in the modules dictionary + under the full name, so get a reference from + there, if need be. (The exception is when + the load failed with a SyntaxError -- then + there's no trace in sys.modules. In that case, + of course, do nothing extra.) */ + res = m; + if (res == NULL) + res = PyDict_GetItemString(modules, fullname); + if (res != NULL && + PyObject_SetAttrString(mod, subname, res) < 0) { + Py_XDECREF(m); m = NULL; } } |