Skip to content

Commit f10548f

Browse files
committed
gh-67224: Make linecache imports relative to improve startup speed
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
1 parent 1dc1521 commit f10548f

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Lib/linecache.py

Lines changed: 10 additions & 3 deletions
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

@@ -67,15 +64,25 @@ def checkcache(filename=None):
6764
if mtime is None:
6865
continue # no-op for files loaded via a __loader__
6966
try:
67+
# This import can fail if the interpreter is shutting down
68+
import os
7069
stat = os.stat(fullname)
7170
except OSError:
7271
cache.pop(filename, None)
7372
continue
73+
except ImportError:
74+
return
7475
if size != stat.st_size or mtime != stat.st_mtime:
7576
cache.pop(filename, None)
7677

7778

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

0 commit comments

Comments
 (0)