]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix linecache.py add lazycache to __all__ and use dict.clear to clear the cache ...
author加和 <ganziqim@live.com>
Sun, 26 Jan 2020 02:07:40 +0000 (10:07 +0800)
committerCheryl Sabella <cheryl.sabella@gmail.com>
Sun, 26 Jan 2020 02:07:40 +0000 (21:07 -0500)
Lib/linecache.py
Misc/NEWS.d/next/Library/2017-12-04-10-14-23.bpo-32173.e0C5dF.rst [new file with mode: 0644]

index 3afcce1f0a14566c67e97527e95f90afba89c07f..ddd0abf2cf01d9e3a8dea236da60c02a8a627c0f 100644 (file)
@@ -10,17 +10,8 @@ import sys
 import os
 import tokenize
 
-__all__ = ["getline", "clearcache", "checkcache"]
+__all__ = ["getline", "clearcache", "checkcache", "lazycache"]
 
-def getline(filename, lineno, module_globals=None):
-    lines = getlines(filename, module_globals)
-    if 1 <= lineno <= len(lines):
-        return lines[lineno-1]
-    else:
-        return ''
-
-
-# The cache
 
 # The cache. Maps filenames to either a thunk which will provide source code,
 # or a tuple (size, mtime, lines, fullname) once loaded.
@@ -29,9 +20,17 @@ cache = {}
 
 def clearcache():
     """Clear the cache entirely."""
+    cache.clear()
 
-    global cache
-    cache = {}
+
+def getline(filename, lineno, module_globals=None):
+    """Get a line for a Python source file from the cache.
+    Update the cache if it doesn't contain an entry for this file already."""
+
+    lines = getlines(filename, module_globals)
+    if 1 <= lineno <= len(lines):
+        return lines[lineno - 1]
+    return ''
 
 
 def getlines(filename, module_globals=None):
@@ -56,11 +55,10 @@ def checkcache(filename=None):
 
     if filename is None:
         filenames = list(cache.keys())
+    elif filename in cache:
+        filenames = [filename]
     else:
-        if filename in cache:
-            filenames = [filename]
-        else:
-            return
+        return
 
     for filename in filenames:
         entry = cache[filename]
@@ -109,8 +107,10 @@ def updatecache(filename, module_globals=None):
                     # for this module.
                     return []
                 cache[filename] = (
-                    len(data), None,
-                    [line+'\n' for line in data.splitlines()], fullname
+                    len(data),
+                    None,
+                    [line + '\n' for line in data.splitlines()],
+                    fullname
                 )
                 return cache[filename][2]
 
diff --git a/Misc/NEWS.d/next/Library/2017-12-04-10-14-23.bpo-32173.e0C5dF.rst b/Misc/NEWS.d/next/Library/2017-12-04-10-14-23.bpo-32173.e0C5dF.rst
new file mode 100644 (file)
index 0000000..fc8f36f
--- /dev/null
@@ -0,0 +1,3 @@
+* Add `lazycache` function to `__all__`.
+* Use `dict.clear` to clear the cache.
+* Refactoring `getline` function and `checkcache` function.