summaryrefslogtreecommitdiff
path: root/Doc
diff options
context:
space:
mode:
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/importlib.rst11
-rw-r--r--Doc/whatsnew/3.9.rst20
2 files changed, 28 insertions, 3 deletions
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst
index 65a685022e..8be6172d4c 100644
--- a/Doc/library/importlib.rst
+++ b/Doc/library/importlib.rst
@@ -1433,13 +1433,18 @@ an :term:`importer`.
``importlib.util.resolve_name('sys', __package__)`` without doing a
check to see if the **package** argument is needed.
- :exc:`ValueError` is raised if **name** is a relative module name but
- package is a false value (e.g. ``None`` or the empty string).
- :exc:`ValueError` is also raised a relative name would escape its containing
+ :exc:`ImportError` is raised if **name** is a relative module name but
+ **package** is a false value (e.g. ``None`` or the empty string).
+ :exc:`ImportError` is also raised a relative name would escape its containing
package (e.g. requesting ``..bacon`` from within the ``spam`` package).
.. versionadded:: 3.3
+ .. versionchanged:: 3.9
+ To improve consistency with import statements, raise
+ :exc:`ImportError` instead of :exc:`ValueError` for invalid relative
+ import attempts.
+
.. function:: find_spec(name, package=None)
Find the :term:`spec <module spec>` for a module, optionally relative to
diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst
index 273fd2b50d..61d9e745e8 100644
--- a/Doc/whatsnew/3.9.rst
+++ b/Doc/whatsnew/3.9.rst
@@ -75,6 +75,12 @@ New Features
Other Language Changes
======================
+* :func:`builtins.__import__` now raises :exc:`ImportError` instead of
+ :exc:`ValueError` as used to occur when a relative import went past
+ its top-level package.
+ (Contributed by Ngalim Siregar in :issue:`37444`.)
+
+
* Python now gets the absolute path of the script filename specified on
the command line (ex: ``python3 script.py``): the ``__file__`` attribute of
the ``__main__`` module, ``sys.argv[0]`` and ``sys.path[0]`` become an
@@ -118,6 +124,13 @@ pprint
:mod:`pprint` can now pretty-print :class:`types.SimpleNamespace`.
(Contributed by Carl Bordum Hansen in :issue:`37376`.)
+importlib
+---------
+
+To improve consistency with import statements, :func:`importlib.util.resolve_name`
+now raises :exc:`ImportError` instead of :exc:`ValueError` for invalid relative
+import attempts.
+(Contributed by Ngalim Siregar in :issue:`37444`.)
Optimizations
=============
@@ -180,4 +193,11 @@ Porting to Python 3.9
This section lists previously described changes and other bugfixes
that may require changes to your code.
+Changes in the Python API
+-------------------------
+* :func:`builtins.__import__` and :func:`importlib.util.resolve_name` now raise
+ :exc:`ImportError` where it previously raised :exc:`ValueError`. Callers
+ catching the specific exception type and supporting both Python 3.9 and
+ earlier versions will need to catch both:
+ ``except (ImportError, ValueError):``