https://github.com/python/cpython/commit/689ada79150f28b0053fa6c1fb646b75ab2cc200
commit: 689ada79150f28b0053fa6c1fb646b75ab2cc200
branch: main
author: Pablo Galindo Salgado <[email protected]>
committer: pablogsal <[email protected]>
date: 2024-04-10T20:09:25+01:00
summary:

gh-67224: Make linecache imports relative to improve startup speed (#117501)

files:
M Lib/linecache.py

diff --git a/Lib/linecache.py b/Lib/linecache.py
index b97999fc1dc909..d1113b108dc5e4 100644
--- a/Lib/linecache.py
+++ b/Lib/linecache.py
@@ -5,9 +5,6 @@
 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."""

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to