[Python-checkins] cpython: Issue #15767: Use ModuleNotFoundError.

eric.snow python-checkins at python.org
Wed Sep 7 19:56:37 EDT 2016


https://hg.python.org/cpython/rev/5fdb8c897023
changeset:   103263:5fdb8c897023
user:        Eric Snow <ericsnowcurrently at gmail.com>
date:        Wed Sep 07 16:56:15 2016 -0700
summary:
  Issue #15767: Use ModuleNotFoundError.

files:
  Doc/c-api/exceptions.rst                         |    7 +
  Doc/reference/import.rst                         |   20 +-
  Doc/whatsnew/3.6.rst                             |    7 +
  Include/pyerrors.h                               |    3 +
  Lib/importlib/_bootstrap.py                      |   14 +-
  Lib/pydoc.py                                     |    2 +-
  Lib/test/test_cmd_line_script.py                 |    2 +-
  Lib/test/test_import/__init__.py                 |   12 +
  Lib/test/test_importlib/import_/test_api.py      |    4 +
  Lib/test/test_importlib/import_/test_fromlist.py |   10 +-
  Lib/test/test_pydoc.py                           |    2 +-
  Lib/test/test_site.py                            |    2 +-
  Misc/NEWS                                        |    4 +
  Python/errors.c                                  |   38 +-
  Python/import.c                                  |    3 +-
  Python/importlib.h                               |  501 ++++-----
  16 files changed, 342 insertions(+), 289 deletions(-)


diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst
--- a/Doc/c-api/exceptions.rst
+++ b/Doc/c-api/exceptions.rst
@@ -306,6 +306,13 @@
    :mod:`warnings` module and the :option:`-W` option in the command line
    documentation.  There is no C API for warning control.
 
+.. c:function:: PyObject* PyErr_SetImportErrorSubclass(PyObject *msg, PyObject *name, PyObject *path)
+
+   Much like :c:func:`PyErr_SetImportError` but this function allows for
+   specifying a subclass of :exc:`ImportError` to raise.
+
+   .. versionadded:: 3.4
+
 
 .. c:function:: int PyErr_WarnExplicitObject(PyObject *category, PyObject *message, PyObject *filename, int lineno, PyObject *module, PyObject *registry)
 
diff --git a/Doc/reference/import.rst b/Doc/reference/import.rst
--- a/Doc/reference/import.rst
+++ b/Doc/reference/import.rst
@@ -36,7 +36,7 @@
 
 When a module is first imported, Python searches for the module and if found,
 it creates a module object [#fnmo]_, initializing it.  If the named module
-cannot be found, an :exc:`ImportError` is raised.  Python implements various
+cannot be found, an :exc:`ModuleNotFoundError` is raised.  Python implements various
 strategies to search for the named module when the import machinery is
 invoked.  These strategies can be modified and extended by using various hooks
 described in the sections below.
@@ -167,7 +167,7 @@
 This name will be used in various phases of the import search, and it may be
 the dotted path to a submodule, e.g. ``foo.bar.baz``.  In this case, Python
 first tries to import ``foo``, then ``foo.bar``, and finally ``foo.bar.baz``.
-If any of the intermediate imports fail, an :exc:`ImportError` is raised.
+If any of the intermediate imports fail, an :exc:`ModuleNotFoundError` is raised.
 
 
 The module cache
@@ -186,7 +186,7 @@
 During import, the module name is looked up in :data:`sys.modules` and if
 present, the associated value is the module satisfying the import, and the
 process completes.  However, if the value is ``None``, then an
-:exc:`ImportError` is raised.  If the module name is missing, Python will
+:exc:`ModuleNotFoundError` is raised.  If the module name is missing, Python will
 continue searching for the module.
 
 :data:`sys.modules` is writable.  Deleting a key may not destroy the
@@ -194,7 +194,7 @@
 but it will invalidate the cache entry for the named module, causing
 Python to search anew for the named module upon its next
 import. The key can also be assigned to ``None``, forcing the next import
-of the module to result in an :exc:`ImportError`.
+of the module to result in an :exc:`ModuleNotFoundError`.
 
 Beware though, as if you keep a reference to the module object,
 invalidate its cache entry in :data:`sys.modules`, and then re-import the
@@ -288,8 +288,8 @@
 If the meta path finder knows how to handle the named module, it returns a
 spec object.  If it cannot handle the named module, it returns ``None``.  If
 :data:`sys.meta_path` processing reaches the end of its list without returning
-a spec, then an :exc:`ImportError` is raised.  Any other exceptions raised
-are simply propagated up, aborting the import process.
+a spec, then a :exc:`ModuleNotFoundError` is raised.  Any other exceptions
+raised are simply propagated up, aborting the import process.
 
 The :meth:`~importlib.abc.MetaPathFinder.find_spec()` method of meta path
 finders is called with two or three arguments.  The first is the fully
@@ -298,9 +298,9 @@
 top-level modules, the second argument is ``None``, but for submodules or
 subpackages, the second argument is the value of the parent package's
 ``__path__`` attribute. If the appropriate ``__path__`` attribute cannot
-be accessed, an :exc:`ImportError` is raised.  The third argument is an
-existing module object that will be the target of loading later.  The
-import system passes in a target module only during reload.
+be accessed, an :exc:`ModuleNotFoundError` is raised.  The third argument
+is an existing module object that will be the target of loading later.
+The import system passes in a target module only during reload.
 
 The meta path may be traversed multiple times for a single import request.
 For example, assuming none of the modules involved has already been cached,
@@ -887,7 +887,7 @@
 
 To selectively prevent import of some modules from a hook early on the
 meta path (rather than disabling the standard import system entirely),
-it is sufficient to raise :exc:`ImportError` directly from
+it is sufficient to raise :exc:`ModuleNoFoundError` directly from
 :meth:`~importlib.abc.MetaPathFinder.find_spec` instead of returning
 ``None``. The latter indicates that the meta path search should continue,
 while raising an exception terminates it immediately.
diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst
--- a/Doc/whatsnew/3.6.rst
+++ b/Doc/whatsnew/3.6.rst
@@ -350,6 +350,10 @@
   :ref:`py36-traceback` for an example).
   (Contributed by Emanuel Barry in :issue:`26823`.)
 
+* Import now raises the new exception :exc:`ModuleNotFoundError`
+  (subclass of :exc:`ImportError`) when it cannot find a module.  Code
+  that current checks for ImportError (in try-except) will still work.
+
 
 New Modules
 ===========
@@ -959,6 +963,9 @@
 * When :meth:`importlib.abc.Loader.exec_module` is defined,
   :meth:`importlib.abc.Loader.create_module` must also be defined.
 
+* :c:func:`PyErr_SetImportError` now sets :exc:`TypeError` when its **msg**
+  argument is not set. Previously only ``NULL`` was returned.
+
 * The format of the ``co_lnotab`` attribute of code objects changed to support
   negative line number delta. By default, Python does not emit bytecode with
   negative line number delta. Functions using ``frame.f_lineno``,
diff --git a/Include/pyerrors.h b/Include/pyerrors.h
--- a/Include/pyerrors.h
+++ b/Include/pyerrors.h
@@ -284,6 +284,9 @@
 
 PyAPI_FUNC(PyObject *) PyErr_SetExcWithArgsKwargs(PyObject *, PyObject *,
     PyObject *);
+
+PyAPI_FUNC(PyObject *) PyErr_SetImportErrorSubclass(PyObject *, PyObject *,
+    PyObject *, PyObject *);
 PyAPI_FUNC(PyObject *) PyErr_SetImportError(PyObject *, PyObject *,
     PyObject *);
 
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -943,10 +943,10 @@
             path = parent_module.__path__
         except AttributeError:
             msg = (_ERR_MSG + '; {!r} is not a package').format(name, parent)
-            raise ImportError(msg, name=name) from None
+            raise ModuleNotFoundError(msg, name=name) from None
     spec = _find_spec(name, path)
     if spec is None:
-        raise ImportError(_ERR_MSG.format(name), name=name)
+        raise ModuleNotFoundError(_ERR_MSG.format(name), name=name)
     else:
         module = _load_unlocked(spec)
     if parent:
@@ -982,10 +982,11 @@
         _imp.release_lock()
         message = ('import of {} halted; '
                    'None in sys.modules'.format(name))
-        raise ImportError(message, name=name)
+        raise ModuleNotFoundError(message, name=name)
     _lock_unlock_module(name)
     return module
 
+
 def _handle_fromlist(module, fromlist, import_):
     """Figure out what __import__ should return.
 
@@ -1007,13 +1008,12 @@
                 from_name = '{}.{}'.format(module.__name__, x)
                 try:
                     _call_with_frames_removed(import_, from_name)
-                except ImportError as exc:
+                except ModuleNotFoundError as exc:
                     # Backwards-compatibility dictates we ignore failed
                     # imports triggered by fromlist for modules that don't
                     # exist.
-                    if str(exc).startswith(_ERR_MSG_PREFIX):
-                        if exc.name == from_name:
-                            continue
+                    if exc.name == from_name:
+                        continue
                     raise
     return module
 
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -350,7 +350,7 @@
         elif exc is SyntaxError:
             # A SyntaxError occurred before we could execute the module.
             raise ErrorDuringImport(value.filename, info)
-        elif exc is ImportError and value.name == path:
+        elif issubclass(exc, ImportError) and value.name == path:
             # No such module in the path.
             return None
         else:
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -428,7 +428,7 @@
             ('builtins.x', br'Error while finding module specification.*'
                 br'AttributeError'),
             ('builtins.x.y', br'Error while finding module specification.*'
-                br'ImportError.*No module named.*not a package'),
+                br'ModuleNotFoundError.*No module named.*not a package'),
             ('os.path', br'loader.*cannot handle'),
             ('importlib', br'No module named.*'
                 br'is a package and cannot be directly executed'),
diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -69,6 +69,18 @@
     def tearDown(self):
         unload(TESTFN)
 
+    def test_import_raises_ModuleNotFoundError(self):
+        with self.assertRaises(ModuleNotFoundError):
+            import something_that_should_not_exist_anywhere
+
+    def test_from_import_missing_module_raises_ModuleNotFoundError(self):
+        with self.assertRaises(ModuleNotFoundError):
+            from something_that_should_not_exist_anywhere import blah
+
+    def test_from_import_missing_attr_raises_ImportError(self):
+        with self.assertRaises(ImportError):
+            from importlib import something_that_should_not_exist_anywhere
+
     def test_case_sensitivity(self):
         # Brief digression to test that import is case-sensitive:  if we got
         # this far, we know for sure that "random" exists.
diff --git a/Lib/test/test_importlib/import_/test_api.py b/Lib/test/test_importlib/import_/test_api.py
--- a/Lib/test/test_importlib/import_/test_api.py
+++ b/Lib/test/test_importlib/import_/test_api.py
@@ -43,6 +43,10 @@
     """Test API-specific details for __import__ (e.g. raising the right
     exception when passing in an int for the module name)."""
 
+    def test_raises_ModuleNotFoundError(self):
+        with self.assertRaises(ModuleNotFoundError):
+            util.import_importlib('some module that does not exist')
+
     def test_name_requires_rparition(self):
         # Raise TypeError if a non-string is passed in for the module name.
         with self.assertRaises(TypeError):
diff --git a/Lib/test/test_importlib/import_/test_fromlist.py b/Lib/test/test_importlib/import_/test_fromlist.py
--- a/Lib/test/test_importlib/import_/test_fromlist.py
+++ b/Lib/test/test_importlib/import_/test_fromlist.py
@@ -73,16 +73,16 @@
                 self.assertTrue(hasattr(module, 'module'))
                 self.assertEqual(module.module.__name__, 'pkg.module')
 
-    def test_module_from_package_triggers_ImportError(self):
-        # If a submodule causes an ImportError because it tries to import
-        # a module which doesn't exist, that should let the ImportError
-        # propagate.
+    def test_module_from_package_triggers_ModuleNotFoundError(self):
+        # If a submodule causes an ModuleNotFoundError because it tries
+        # to import a module which doesn't exist, that should let the
+        # ModuleNotFoundError propagate.
         def module_code():
             import i_do_not_exist
         with util.mock_modules('pkg.__init__', 'pkg.mod',
                                module_code={'pkg.mod': module_code}) as importer:
             with util.import_state(meta_path=[importer]):
-                with self.assertRaises(ImportError) as exc:
+                with self.assertRaises(ModuleNotFoundError) as exc:
                     self.__import__('pkg', fromlist=['mod'])
                 self.assertEqual('i_do_not_exist', exc.exception.name)
 
diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py
--- a/Lib/test/test_pydoc.py
+++ b/Lib/test/test_pydoc.py
@@ -263,7 +263,7 @@
 Use help(str) for help on the str class.'''.replace('\n', os.linesep)
 
 # output pattern for module with bad imports
-badimport_pattern = "problem in %s - ImportError: No module named %r"
+badimport_pattern = "problem in %s - ModuleNotFoundError: No module named %r"
 
 expected_dynamicattribute_pattern = """
 Help on class DA in module %s:
diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
@@ -138,7 +138,7 @@
             re.escape(os.path.join(pth_dir, pth_fn)))
         # XXX: ditto previous XXX comment.
         self.assertRegex(err_out.getvalue(), 'Traceback')
-        self.assertRegex(err_out.getvalue(), 'ImportError')
+        self.assertRegex(err_out.getvalue(), 'ModuleNotFoundError')
 
     @unittest.skipIf(sys.platform == "win32", "Windows does not raise an "
                       "error for file paths containing null characters")
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9966,6 +9966,10 @@
   PyImport_ExecCodeModuleWithPathnames() (and thus by extension
   PyImport_ExecCodeModule() and PyImport_ExecCodeModuleEx()).
 
+- Issue #15767: Added PyErr_SetImportErrorSubclass().
+
+- PyErr_SetImportError() now sets TypeError when its msg argument is set.
+
 - Issue #9369: The types of `char*` arguments of PyObject_CallFunction() and
   PyObject_CallMethod() now changed to `const char*`.  Based on patches by
   Jörg Müller and Lars Buitinck.
diff --git a/Python/errors.c b/Python/errors.c
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -697,27 +697,37 @@
 #endif /* MS_WINDOWS */
 
 PyObject *
-PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
+PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
+    PyObject *name, PyObject *path)
 {
+    int issubclass;
     PyObject *kwargs, *error;
 
+    issubclass = PyObject_IsSubclass(exception, PyExc_ImportError);
+    if (issubclass < 0) {
+        return NULL;
+    }
+    else if (!issubclass) {
+        PyErr_SetString(PyExc_TypeError, "expected a subclass of ImportError");
+        return NULL;
+    }
+
     if (msg == NULL) {
+        PyErr_SetString(PyExc_TypeError, "expected a message argument");
         return NULL;
     }
 
+    if (name == NULL) {
+        name = Py_None;
+    }
+    if (path == NULL) {
+        path = Py_None;
+    }
+
     kwargs = PyDict_New();
     if (kwargs == NULL) {
         return NULL;
     }
-
-    if (name == NULL) {
-        name = Py_None;
-    }
-
-    if (path == NULL) {
-        path = Py_None;
-    }
-
     if (PyDict_SetItemString(kwargs, "name", name) < 0) {
         goto done;
     }
@@ -725,7 +735,7 @@
         goto done;
     }
 
-    error = _PyObject_FastCallDict(PyExc_ImportError, &msg, 1, kwargs);
+    error = _PyObject_FastCallDict(exception, &msg, 1, kwargs);
     if (error != NULL) {
         PyErr_SetObject((PyObject *)Py_TYPE(error), error);
         Py_DECREF(error);
@@ -736,6 +746,12 @@
     return NULL;
 }
 
+PyObject *
+PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
+{
+    return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path);
+}
+
 void
 _PyErr_BadInternalCall(const char *filename, int lineno)
 {
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -1539,7 +1539,8 @@
         PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
                                              "None in sys.modules", abs_name);
         if (msg != NULL) {
-            PyErr_SetImportError(msg, abs_name, NULL);
+            PyErr_SetImportErrorSubclass(PyExc_ModuleNotFoundError, msg,
+                    abs_name, NULL);
             Py_DECREF(msg);
         }
         mod = NULL;
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list