Fix linecache.py add lazycache to __all__ and use dict.clear to clear the cache (GH-4641)
This commit is contained in:
parent
8271441d8b
commit
4515a590a4
|
@ -10,17 +10,8 @@ import sys
|
||||||
import os
|
import os
|
||||||
import tokenize
|
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,
|
# The cache. Maps filenames to either a thunk which will provide source code,
|
||||||
# or a tuple (size, mtime, lines, fullname) once loaded.
|
# or a tuple (size, mtime, lines, fullname) once loaded.
|
||||||
|
@ -29,9 +20,17 @@ cache = {}
|
||||||
|
|
||||||
def clearcache():
|
def clearcache():
|
||||||
"""Clear the cache entirely."""
|
"""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):
|
def getlines(filename, module_globals=None):
|
||||||
|
@ -56,8 +55,7 @@ def checkcache(filename=None):
|
||||||
|
|
||||||
if filename is None:
|
if filename is None:
|
||||||
filenames = list(cache.keys())
|
filenames = list(cache.keys())
|
||||||
else:
|
elif filename in cache:
|
||||||
if filename in cache:
|
|
||||||
filenames = [filename]
|
filenames = [filename]
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
@ -109,8 +107,10 @@ def updatecache(filename, module_globals=None):
|
||||||
# for this module.
|
# for this module.
|
||||||
return []
|
return []
|
||||||
cache[filename] = (
|
cache[filename] = (
|
||||||
len(data), None,
|
len(data),
|
||||||
[line+'\n' for line in data.splitlines()], fullname
|
None,
|
||||||
|
[line + '\n' for line in data.splitlines()],
|
||||||
|
fullname
|
||||||
)
|
)
|
||||||
return cache[filename][2]
|
return cache[filename][2]
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
* Add `lazycache` function to `__all__`.
|
||||||
|
* Use `dict.clear` to clear the cache.
|
||||||
|
* Refactoring `getline` function and `checkcache` function.
|
Loading…
Reference in New Issue