Skip to content

Commit 689ada7

Browse files
authoredApr 10, 2024
gh-67224: Make linecache imports relative to improve startup speed (#117501)
1 parent 630df37 commit 689ada7

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed
 

‎Lib/linecache.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
that name.
66
"""
77

8-
import sys
9-
import os
10-
118
__all__ = ["getline", "clearcache", "checkcache", "lazycache"]
129

1310

@@ -66,6 +63,11 @@ def checkcache(filename=None):
6663
size, mtime, lines, fullname = entry
6764
if mtime is None:
6865
continue # no-op for files loaded via a __loader__
66+
try:
67+
# This import can fail if the interpreter is shutting down
68+
import os
69+
except ImportError:
70+
return
6971
try:
7072
stat = os.stat(fullname)
7173
except OSError:
@@ -76,6 +78,12 @@ def checkcache(filename=None):
7678

7779

7880
def updatecache(filename, module_globals=None):
81+
# These imports are not at top level because linecache is in the critical
82+
# path of the interpreter startup and importing os and sys take a lot of time
83+
# and slow down the startup sequence.
84+
import os
85+
import sys
86+
7987
"""Update a cache entry and return its list of lines.
8088
If something's wrong, print a message, discard the cache entry,
8189
and return an empty list."""

0 commit comments

Comments
 (0)
Please sign in to comment.