summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2013-06-13 03:29:18 (GMT)
committerBrett Cannon <brett@python.org>2013-06-13 03:29:18 (GMT)
commit8f5ac5106eb24dd8bda91f25e993a90a820a2d5c (patch)
tree49b910fbfa1af58f0c017d42c84d507b338d7bb2 /Doc
parent3e9a9ae09d6fc9169b01cba3efd1ae03ab40b237 (diff)
downloadcpython-8f5ac5106eb24dd8bda91f25e993a90a820a2d5c.zip
cpython-8f5ac5106eb24dd8bda91f25e993a90a820a2d5c.tar.gz
cpython-8f5ac5106eb24dd8bda91f25e993a90a820a2d5c.tar.bz2
Issue #15767: Touch up ModuleNotFoundError usage by import.
Forgot to raise ModuleNotFoundError when None is found in sys.modules. This led to introducing the C function PyErr_SetImportErrorSubclass() to make setting ModuleNotFoundError easier. Also updated the reference docs to mention ModuleNotFoundError appropriately. Updated the docs for ModuleNotFoundError to mention the None in sys.modules case. Lastly, it was noticed that PyErr_SetImportError() was not setting an exception when returning None in one case. That issue is now fixed.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/c-api/exceptions.rst7
-rw-r--r--Doc/library/exceptions.rst3
-rw-r--r--Doc/reference/import.rst12
-rw-r--r--Doc/whatsnew/3.4.rst3
4 files changed, 18 insertions, 7 deletions
diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst
index 1bdcdd3..25e2a1c 100644
--- a/Doc/c-api/exceptions.rst
+++ b/Doc/c-api/exceptions.rst
@@ -292,6 +292,13 @@ in various ways. There is a separate error indicator for each thread.
.. versionadded:: 3.3
+.. 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:: void PyErr_SyntaxLocationEx(char *filename, int lineno, int col_offset)
diff --git a/Doc/library/exceptions.rst b/Doc/library/exceptions.rst
index 933667c..377d98a 100644
--- a/Doc/library/exceptions.rst
+++ b/Doc/library/exceptions.rst
@@ -185,7 +185,8 @@ The following exceptions are the exceptions that are usually raised.
A subclass of :exc:`ImportError` which is raised by :keyword:`import` when a
module could not be located. This includes ``from ... import`` statements as
the specific attribute being requested cannot be known a priori to be a module
- or some other type of object.
+ or some other type of object. It is also raised when ``None`` is found in
+ :data:`sys.modules`.
.. versionadded:: 3.4
diff --git a/Doc/reference/import.rst b/Doc/reference/import.rst
index b587fc9..73f5ae5 100644
--- a/Doc/reference/import.rst
+++ b/Doc/reference/import.rst
@@ -37,7 +37,7 @@ use the standard import system.
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.
@@ -168,7 +168,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
@@ -187,7 +187,7 @@ 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
@@ -195,7 +195,7 @@ 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
@@ -284,7 +284,7 @@ handle the named module or not.
If the meta path finder knows how to handle the named module, it returns a
loader 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 loader, then an :exc:`ImportError` is raised. Any other exceptions raised
+a loader, then an :exc:`ModuleNotFoundError` is raised. Any other exceptions raised
are simply propagated up, aborting the import process.
The :meth:`find_module()` method of meta path finders is called with two
@@ -647,7 +647,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:`ModuleNotFoundError` directly from
:meth:`find_module` 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.4.rst b/Doc/whatsnew/3.4.rst
index 23ccae2..1054c68 100644
--- a/Doc/whatsnew/3.4.rst
+++ b/Doc/whatsnew/3.4.rst
@@ -269,3 +269,6 @@ that may require changes to your code.
* Frozen packages no longer set ``__path__`` to a list containg the package name
but an empty list instead. Determing if a module is a package should be done
using ``hasattr(module, '__path__')``.
+
+* :c:func:`PyErr_SetImportError` now sets :exc:`TypeError` when its **msg**
+ argument is not set. Previously only ``NULL`` was returned.