[Python-checkins] cpython: Fix bug in __import__ during Python shutdown

victor.stinner python-checkins at python.org
Thu Mar 24 19:41:55 EDT 2016


https://hg.python.org/cpython/rev/14c56f697a85
changeset:   100727:14c56f697a85
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Fri Mar 25 00:40:59 2016 +0100
summary:
  Fix bug in __import__ during Python shutdown

Issue #26637: The importlib module now emits an ImportError rather than a
TypeError if __import__() is tried during the Python shutdown process but
sys.path is already cleared (set to None).

files:
  Lib/importlib/_bootstrap.py |   11 +-
  Misc/NEWS                   |    5 +
  Python/importlib.h          |  884 ++++++++++++-----------
  3 files changed, 459 insertions(+), 441 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -878,13 +878,20 @@
 
 def _find_spec(name, path, target=None):
     """Find a module's loader."""
-    if sys.meta_path is not None and not sys.meta_path:
+    meta_path = sys.meta_path
+    if meta_path is None:
+        # PyImport_Cleanup() is running or has been called.
+        raise ImportError("sys.meta_path is None, Python is likely "
+                          "shutting down")
+
+    if not meta_path:
         _warnings.warn('sys.meta_path is empty', ImportWarning)
+
     # We check sys.modules here for the reload case.  While a passed-in
     # target will usually indicate a reload there is no guarantee, whereas
     # sys.modules provides one.
     is_reload = name in sys.modules
-    for finder in sys.meta_path:
+    for finder in meta_path:
         with _ImportLockContext():
             try:
                 find_spec = finder.find_spec
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -232,6 +232,11 @@
 Library
 -------
 
+- Issue #26637: The :mod:`importlib` module now emits an :exc:`ImportError`
+  rather than a :exc:`TypeError` if :func:`__import__` is tried during the
+  Python shutdown process but :data:`sys.path` is already cleared (set to
+  ``None``).
+
 - Issue #21925: :func:`warnings.formatwarning` now catches exceptions when
   calling :func;`linecache.getline` and
   :func:`tracemalloc.get_object_traceback` to be able to log
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