diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2016-03-24 23:40:59 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2016-03-24 23:40:59 (GMT) |
commit | 4f17426437a5bdf458f8628936c3e37909fca448 (patch) | |
tree | 3fca87d2576307f66a63f7152fe3a26893a31140 /Lib/importlib/_bootstrap.py | |
parent | e091d32a7ac514a415161043c4a70e1765363c5a (diff) | |
download | cpython-4f17426437a5bdf458f8628936c3e37909fca448.zip cpython-4f17426437a5bdf458f8628936c3e37909fca448.tar.gz cpython-4f17426437a5bdf458f8628936c3e37909fca448.tar.bz2 |
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).
Diffstat (limited to 'Lib/importlib/_bootstrap.py')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 880c493..fa99f56 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -878,13 +878,20 @@ def _find_spec_legacy(finder, name, path): 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 |