]> granicus.if.org Git - python/commitdiff
Issue #18018: Raise an ImportError if a relative import is attempted
authorBrett Cannon <brett@python.org>
Sat, 23 Jan 2016 00:39:02 +0000 (16:39 -0800)
committerBrett Cannon <brett@python.org>
Sat, 23 Jan 2016 00:39:02 +0000 (16:39 -0800)
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.

Doc/whatsnew/3.6.rst
Lib/test/test_importlib/import_/test_relative_imports.py
Misc/NEWS
Python/import.c

index 5872cc0ee0fd44223611f19b1c24c20bc056b360..158dad3efe4a48e1e34805cfd493e0fb909a0617 100644 (file)
@@ -278,6 +278,10 @@ Changes in the Python API
   to ``__spec__.parent`` then :exc:`ImportWarning` is raised.
   (Contributed by Brett Cannon in :issue:`25791`.)
 
+* When a relative import is performed and no parent package is known, then
+  :exc:`ImportError` will be raised. Previously, :exc:`SystemError` could be
+  raised. (Contribute by Brett Cannon in :issue:`18018`.)
+
 
 Changes in the C API
 --------------------
index 0409f22b4d9a097b7cd1688f44b7e77d93dd5fb4..1cad6b360a2be0f6921580d511f3375bffaf8055 100644 (file)
@@ -213,6 +213,11 @@ class RelativeImports:
             with self.assertRaises(KeyError):
                 self.__import__('sys', level=1)
 
+    def test_relative_import_no_package(self):
+        with self.assertRaises(ImportError):
+            self.__import__('a', {'__package__': '', '__spec__': None},
+                            level=1)
+
 
 (Frozen_RelativeImports,
  Source_RelativeImports
index 165b428c513cad8d1bc4ad81fbd248a5ef96e742..676f2cac8357b5af6f039e9b10395c436bec95fc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ Release date: tba
 Core and Builtins
 -----------------
 
+- Issue #18018: Import raises ImportError instead of SystemError if a relative
+  import is attempted without a known parent package.
+
 - Issue #25843: When compiling code, don't merge constants if they are equal
   but have a different types. For example, ``f1, f2 = lambda: 1, lambda: 1.0``
   is now correctly compiled to two different functions: ``f1()`` returns ``1``
index 22f9d2127ad58669ddc7b1a8db60d7a796fcfcfc..8ad5b4c1674327d7d6eb7571ca539dde59b756cd 100644 (file)
@@ -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);