]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #15767: Add ModuleNotFoundError.
authorEric Snow <ericsnowcurrently@gmail.com>
Wed, 7 Sep 2016 22:42:32 +0000 (15:42 -0700)
committerEric Snow <ericsnowcurrently@gmail.com>
Wed, 7 Sep 2016 22:42:32 +0000 (15:42 -0700)
Doc/c-api/exceptions.rst
Doc/library/exceptions.rst
Include/pyerrors.h
Lib/_compat_pickle.py
Lib/test/exception_hierarchy.txt
Lib/test/test_pickle.py
Misc/NEWS
Objects/exceptions.c

index 226b61972f470653c1e273231494db11fb3fbaa2..5644410b47ed9673863a2047b257e865abd3ac7e 100644 (file)
@@ -782,6 +782,8 @@ the variables:
 +-----------------------------------------+---------------------------------+----------+
 | :c:data:`PyExc_ImportError`             | :exc:`ImportError`              |          |
 +-----------------------------------------+---------------------------------+----------+
+| :c:data:`PyExc_ModuleNotFoundError`     | :exc:`ModuleNotFoundError`      |          |
++-----------------------------------------+---------------------------------+----------+
 | :c:data:`PyExc_IndexError`              | :exc:`IndexError`               |          |
 +-----------------------------------------+---------------------------------+----------+
 | :c:data:`PyExc_InterruptedError`        | :exc:`InterruptedError`         |          |
index 1747efe2ed5eb313d5d5b7c5082dcbac0f3c23a5..a428f5165fc8d23b6131309ca0a213268e92d4eb 100644 (file)
@@ -170,8 +170,9 @@ The following exceptions are the exceptions that are usually raised.
 
 .. exception:: ImportError
 
-   Raised when an :keyword:`import` statement fails to find the module definition
-   or when a ``from ... import`` fails to find a name that is to be imported.
+   Raised when the :keyword:`import` statement has troubles trying to
+   load a module.  Also raised when the "from list" in ``from ... import``
+   has a name that cannot be found.
 
    The :attr:`name` and :attr:`path` attributes can be set using keyword-only
    arguments to the constructor. When set they represent the name of the module
@@ -181,6 +182,14 @@ The following exceptions are the exceptions that are usually raised.
    .. versionchanged:: 3.3
       Added the :attr:`name` and :attr:`path` attributes.
 
+.. exception:: ModuleNotFoundError
+
+   A subclass of :exc:`ImportError` which is raised by :keyword:`import`
+   when a module could not be located.  It is also raised when ``None``
+   is found in :data:`sys.modules`.
+
+   .. versionadded:: 3.6
+
 
 .. exception:: IndexError
 
index 35aedb7349207003ab899a55ef742a4492e54c4f..6bc3ca77617d0255b3a67d34471060bf3370bac0 100644 (file)
@@ -160,6 +160,7 @@ PyAPI_DATA(PyObject *) PyExc_EOFError;
 PyAPI_DATA(PyObject *) PyExc_FloatingPointError;
 PyAPI_DATA(PyObject *) PyExc_OSError;
 PyAPI_DATA(PyObject *) PyExc_ImportError;
+PyAPI_DATA(PyObject *) PyExc_ModuleNotFoundError;
 PyAPI_DATA(PyObject *) PyExc_IndexError;
 PyAPI_DATA(PyObject *) PyExc_KeyError;
 PyAPI_DATA(PyObject *) PyExc_KeyboardInterrupt;
index c0e0443cf2330f9cb0d9ee358f3592eaac3960f3..f68496ae639f5f880ae5f4ca0a220a29d2e354be 100644 (file)
@@ -242,3 +242,10 @@ PYTHON3_OSERROR_EXCEPTIONS = (
 
 for excname in PYTHON3_OSERROR_EXCEPTIONS:
     REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'OSError')
+
+PYTHON3_IMPORTERROR_EXCEPTIONS = (
+    'ModuleNotFoundError',
+)
+
+for excname in PYTHON3_IMPORTERROR_EXCEPTIONS:
+    REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'ImportError')
index 05137654de62610b67ceed1bbbb88a1f7a5474d8..7333b2aa6243ea87640d28f77180a758c1631a7b 100644 (file)
@@ -14,6 +14,7 @@ BaseException
       +-- BufferError
       +-- EOFError
       +-- ImportError
+           +-- ModuleNotFoundError
       +-- LookupError
       |    +-- IndexError
       |    +-- KeyError
index 05203e52cd7d170bfa37074666d411d7213698bb..e6c5d08522aa8e90ffb1d0842d6ccdc980e0581e 100644 (file)
@@ -335,6 +335,9 @@ class CompatPickleTests(unittest.TestCase):
                 if (module2, name2) == ('exceptions', 'OSError'):
                     attr = getattribute(module3, name3)
                     self.assertTrue(issubclass(attr, OSError))
+                elif (module2, name2) == ('exceptions', 'ImportError'):
+                    attr = getattribute(module3, name3)
+                    self.assertTrue(issubclass(attr, ImportError))
                 else:
                     module, name = mapping(module2, name2)
                     if module3[:1] != '_':
@@ -401,6 +404,11 @@ class CompatPickleTests(unittest.TestCase):
                 if exc is not OSError and issubclass(exc, OSError):
                     self.assertEqual(reverse_mapping('builtins', name),
                                      ('exceptions', 'OSError'))
+                elif exc is not ImportError and issubclass(exc, ImportError):
+                    self.assertEqual(reverse_mapping('builtins', name),
+                                     ('exceptions', 'ImportError'))
+                    self.assertEqual(mapping('exceptions', name),
+                                     ('exceptions', name))
                 else:
                     self.assertEqual(reverse_mapping('builtins', name),
                                      ('exceptions', name))
index edfff7b8701a6c373c98d9c2cae352800ea9f53f..26922770294b2e5110a5082cfccba2b334804586 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -8043,6 +8043,9 @@ Core and Builtins
 - Issue #18137: Detect integer overflow on precision in float.__format__()
   and complex.__format__().
 
+- Issue #15767: Introduce ModuleNotFoundError which is raised when a module
+  could not be found.
+
 - Issue #18183: Fix various unicode operations on strings with large unicode
   codepoints.
 
index 336c32c8a46d2035c0858e3d2fa4fef8da9a23e8..6fb5eb7214f63add116fad10676aa3ab84dcf699 100644 (file)
@@ -705,6 +705,13 @@ ComplexExtendsException(PyExc_Exception, ImportError,
                         "Import can't find module, or can't find name in "
                         "module.");
 
+/*
+ *    ModuleNotFoundError extends ImportError
+ */
+
+MiddlingExtendsException(PyExc_ImportError, ModuleNotFoundError, ImportError,
+                         "Module not found.");
+
 /*
  *    OSError extends Exception
  */
@@ -2469,6 +2476,7 @@ _PyExc_Init(PyObject *bltinmod)
     PRE_INIT(SystemExit)
     PRE_INIT(KeyboardInterrupt)
     PRE_INIT(ImportError)
+    PRE_INIT(ModuleNotFoundError)
     PRE_INIT(OSError)
     PRE_INIT(EOFError)
     PRE_INIT(RuntimeError)
@@ -2541,6 +2549,7 @@ _PyExc_Init(PyObject *bltinmod)
     POST_INIT(SystemExit)
     POST_INIT(KeyboardInterrupt)
     POST_INIT(ImportError)
+    POST_INIT(ModuleNotFoundError)
     POST_INIT(OSError)
     INIT_ALIAS(EnvironmentError, OSError)
     INIT_ALIAS(IOError, OSError)