summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2024-04-10 19:09:25 (GMT)
committerGitHub <noreply@github.com>2024-04-10 19:09:25 (GMT)
commit689ada79150f28b0053fa6c1fb646b75ab2cc200 (patch)
treeb8026a7f1997e2af9213dc3721434900fa05582e
parent630df37116b1c5b381984c547ef9d23792ceb464 (diff)
downloadcpython-689ada79150f28b0053fa6c1fb646b75ab2cc200.zip
cpython-689ada79150f28b0053fa6c1fb646b75ab2cc200.tar.gz
cpython-689ada79150f28b0053fa6c1fb646b75ab2cc200.tar.bz2
gh-67224: Make linecache imports relative to improve startup speed (#117501)
-rw-r--r--Lib/linecache.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/Lib/linecache.py b/Lib/linecache.py
index b97999f..d1113b1 100644
--- a/Lib/linecache.py
+++ b/Lib/linecache.py
@@ -5,9 +5,6 @@ is not found, it will look down the module search path for a file by
that name.
"""
-import sys
-import os
-
__all__ = ["getline", "clearcache", "checkcache", "lazycache"]
@@ -67,6 +64,11 @@ 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
+ except ImportError:
+ return
+ try:
stat = os.stat(fullname)
except OSError:
cache.pop(filename, None)
@@ -76,6 +78,12 @@ def checkcache(filename=None):
def updatecache(filename, module_globals=None):
+ # These imports are not at top level because linecache is in the critical
+ # path of the interpreter startup and importing os and sys take a lot of time
+ # and slow down the startup sequence.
+ 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."""