summaryrefslogtreecommitdiffstats
path: root/Lib/linecache.py
diff options
context:
space:
mode:
author加和 <ganziqim@live.com>2020-01-26 02:07:40 (GMT)
committerCheryl Sabella <cheryl.sabella@gmail.com>2020-01-26 02:07:40 (GMT)
commit4515a590a4a4c09231a66e81782f33b4bfcd5054 (patch)
treeaf683c488fe17efeedf4e4f027488b41898bc168 /Lib/linecache.py
parent8271441d8b6e1f8eae1457c437da24e775801d9f (diff)
downloadcpython-4515a590a4a4c09231a66e81782f33b4bfcd5054.zip
cpython-4515a590a4a4c09231a66e81782f33b4bfcd5054.tar.gz
cpython-4515a590a4a4c09231a66e81782f33b4bfcd5054.tar.bz2
Fix linecache.py add lazycache to __all__ and use dict.clear to clear the cache (GH-4641)
Diffstat (limited to 'Lib/linecache.py')
-rw-r--r--Lib/linecache.py36
1 files changed, 18 insertions, 18 deletions
diff --git a/Lib/linecache.py b/Lib/linecache.py
index 3afcce1..ddd0abf 100644
--- a/Lib/linecache.py
+++ b/Lib/linecache.py
@@ -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]