diff --git a/Lib/linecache.py b/Lib/linecache.py index b97999fc1dc909c..1c32fdb87a006d8 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -5,9 +5,6 @@ that name. """ -import sys -import os - __all__ = ["getline", "clearcache", "checkcache", "lazycache"] @@ -67,15 +64,22 @@ def checkcache(filename=None): if mtime is None: continue # no-op for files loaded via a __loader__ try: + # This import can fail if the interpreter is shutting down + import os stat = os.stat(fullname) except OSError: cache.pop(filename, None) continue + except ImportError: + return if size != stat.st_size or mtime != stat.st_mtime: cache.pop(filename, None) def updatecache(filename, module_globals=None): + import os + import sys + """Update a cache entry and return its list of lines. If something's wrong, print a message, discard the cache entry, and return an empty list."""