Skip to content

Commit

Permalink
gh-67224: Make linecache imports relative to improve startup speed
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
  • Loading branch information
pablogsal committed Apr 3, 2024
1 parent 1dc1521 commit f10548f
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Lib/linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
that name.
"""

import sys
import os

__all__ = ["getline", "clearcache", "checkcache", "lazycache"]


Expand Down Expand Up @@ -67,15 +64,25 @@ 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):
# 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."""
Expand Down

0 comments on commit f10548f

Please sign in to comment.