summaryrefslogtreecommitdiffstats
path: root/Python/import.c
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2016-01-23 00:39:02 (GMT)
committerBrett Cannon <brett@python.org>2016-01-23 00:39:02 (GMT)
commit9fa812668faf0d2d7839845e1a0da19b87bdbc29 (patch)
tree80c2c811909e691d6ab1fc2fea7ad9ca1619bdd7 /Python/import.c
parent4b18dd339a9919e6f5fa3485c8b871ed19d9e391 (diff)
downloadcpython-9fa812668faf0d2d7839845e1a0da19b87bdbc29.zip
cpython-9fa812668faf0d2d7839845e1a0da19b87bdbc29.tar.gz
cpython-9fa812668faf0d2d7839845e1a0da19b87bdbc29.tar.bz2
Issue #18018: Raise an ImportError if a relative import is attempted
with no known parent package. Previously SystemError was raised if the parent package didn't exist (e.g., __package__ was set to ''). Thanks to Florent Xicluna and Yongzhi Pan for reporting the issue.
Diffstat (limited to 'Python/import.c')
-rw-r--r--Python/import.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/Python/import.c b/Python/import.c
index 22f9d21..8ad5b4c 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -1424,7 +1424,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
PyErr_SetString(PyExc_TypeError, "package must be a string");
goto error;
}
- else if (spec != NULL) {
+ else if (spec != NULL && spec != Py_None) {
int equal;
PyObject *parent = PyObject_GetAttrString(spec, "parent");
if (parent == NULL) {
@@ -1444,7 +1444,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
}
}
}
- else if (spec != NULL) {
+ else if (spec != NULL && spec != Py_None) {
package = PyObject_GetAttrString(spec, "parent");
if (package == NULL) {
goto error;
@@ -1491,7 +1491,12 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
}
}
- if (PyDict_GetItem(interp->modules, package) == NULL) {
+ if (PyUnicode_CompareWithASCIIString(package, "") == 0) {
+ PyErr_SetString(PyExc_ImportError,
+ "attempted relative import with no known parent package");
+ goto error;
+ }
+ else if (PyDict_GetItem(interp->modules, package) == NULL) {
PyErr_Format(PyExc_SystemError,
"Parent module %R not loaded, cannot perform relative "
"import", package);