diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2008-07-13 14:52:36 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2008-07-13 14:52:36 (GMT) |
commit | b028f509119d5ea7dd85d7f38df366ccc9d7bdb4 (patch) | |
tree | 0cef7d8095d49b149d12c1ce2d2044e4561c94e7 /Python | |
parent | 12c8660cc6f299464d61b3edd57a73dc6bcbffd7 (diff) | |
download | cpython-b028f509119d5ea7dd85d7f38df366ccc9d7bdb4.zip cpython-b028f509119d5ea7dd85d7f38df366ccc9d7bdb4.tar.gz cpython-b028f509119d5ea7dd85d7f38df366ccc9d7bdb4.tar.bz2 |
Fix issue 3221 by emitting a RuntimeWarning instead of raising SystemError when the parent module can't be found during an absolute import (likely due to non-PEP 361 aware code which sets a module level __package__ attribute)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/import.c | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/Python/import.c b/Python/import.c index b65ed0e..f0ee40a 100644 --- a/Python/import.c +++ b/Python/import.c @@ -2160,6 +2160,7 @@ get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level) static PyObject *pathstr = NULL; static PyObject *pkgstr = NULL; PyObject *pkgname, *modname, *modpath, *modules, *parent; + int orig_level = level; if (globals == NULL || !PyDict_Check(globals) || !level) return Py_None; @@ -2285,9 +2286,27 @@ get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level) modules = PyImport_GetModuleDict(); parent = PyDict_GetItemString(modules, buf); - if (parent == NULL) - PyErr_Format(PyExc_SystemError, - "Parent module '%.200s' not loaded", buf); + if (parent == NULL) { + if (orig_level < 1) { + PyObject *err_msg = PyString_FromFormat( + "Parent module '%.200s' not found " + "while handling absolute import", buf); + if (err_msg == NULL) { + return NULL; + } + if (!PyErr_WarnEx(PyExc_RuntimeWarning, + PyString_AsString(err_msg), 1)) { + *buf = '\0'; + *p_buflen = 0; + parent = Py_None; + } + Py_DECREF(err_msg); + } else { + PyErr_Format(PyExc_SystemError, + "Parent module '%.200s' not loaded, " + "cannot perform relative import", buf); + } + } return parent; /* We expect, but can't guarantee, if parent != None, that: - parent.__name__ == buf |