]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-67224: Make linecache imports relative to improve startup speed (#117501)
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Wed, 10 Apr 2024 19:09:25 +0000 (20:09 +0100)
committerGitHub <noreply@github.com>
Wed, 10 Apr 2024 19:09:25 +0000 (20:09 +0100)
Lib/linecache.py

index b97999fc1dc909ceb203d687ab054cc9fe833f7c..d1113b108dc5e442ce0e155a93e2f41a6fc4be23 100644 (file)
@@ -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"]
 
 
@@ -66,6 +63,11 @@ def checkcache(filename=None):
         size, mtime, lines, fullname = entry
         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:
@@ -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."""