Skip to content

Commit

Permalink
Issue #15767: Use ModuleNotFoundError.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed Sep 7, 2016
1 parent 096d3a2 commit 86888ae
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 284 deletions.
7 changes: 7 additions & 0 deletions Doc/c-api/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ an error value).
: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)
Expand Down
20 changes: 10 additions & 10 deletions Doc/reference/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ implement import semantics.

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.
Expand Down Expand Up @@ -167,7 +167,7 @@ arguments to the :keyword:`import` statement, or from the parameters to the
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
Expand All @@ -186,15 +186,15 @@ object.
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
associated module (as other modules may hold references to it),
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
Expand Down Expand Up @@ -288,8 +288,8 @@ the named module or not.
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
Expand All @@ -298,9 +298,9 @@ The second argument is the path entries to use for the module search. For
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,
Expand Down Expand Up @@ -887,7 +887,7 @@ import statements within that module.

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.
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ Some smaller changes made to the core Python language are:
: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
===========
Expand Down Expand Up @@ -959,6 +963,9 @@ Changes in the Python API
* 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``,
Expand Down
3 changes: 3 additions & 0 deletions Include/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);

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 *);

Expand Down
14 changes: 7 additions & 7 deletions Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,10 +943,10 @@ def _find_and_load_unlocked(name, import_):
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:
Expand Down Expand Up @@ -982,10 +982,11 @@ def _gcd_import(name, package=None, level=0):
_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.
Expand All @@ -1007,13 +1008,12 @@ def _handle_fromlist(module, fromlist, import_):
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

Expand Down
2 changes: 1 addition & 1 deletion Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def safeimport(path, forceload=0, cache={}):
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:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def test_dash_m_errors(self):
('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'),
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ def setUp(self):
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.
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_importlib/import_/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class APITest:
"""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):
Expand Down
10 changes: 5 additions & 5 deletions Lib/test/test_importlib/import_/test_fromlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ def test_module_from_package(self):
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)

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class C(builtins.object)
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:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_addpackage_import_bad_exec(self):
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")
Expand Down
4 changes: 4 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -9966,6 +9966,10 @@ C-API
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.
Expand Down
28 changes: 22 additions & 6 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -697,35 +697,45 @@ PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
#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;

if (msg == NULL) {
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;
}

kwargs = PyDict_New();
if (kwargs == 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 (PyDict_SetItemString(kwargs, "name", name) < 0) {
goto done;
}
if (PyDict_SetItemString(kwargs, "path", path) < 0) {
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);
Expand All @@ -736,6 +746,12 @@ PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
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)
{
Expand Down
3 changes: 2 additions & 1 deletion Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,8 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
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;
Expand Down

0 comments on commit 86888ae

Please sign in to comment.