| 1 |
"""Cache lines from Python source files. |
| 2 |
|
| 3 |
This is intended to read lines from modules imported -- hence if a filename |
| 4 |
is not found, it will look down the module search path for a file by |
| 5 |
that name. |
| 6 |
""" |
| 7 |
|
| 8 |
import functools |
| 9 |
import sys |
| 10 |
import os |
| 11 |
import tokenize |
| 12 |
|
| 13 |
__all__ = ["getline", "clearcache", "checkcache", "lazycache"] |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
cache = {} |
| 19 |
|
| 20 |
|
| 21 |
def clearcache(): |
| 22 |
"""Clear the cache entirely.""" |
| 23 |
cache.clear() |
| 24 |
|
| 25 |
|
| 26 |
def getline(filename, lineno, module_globals=None): |
| 27 |
"""Get a line for a Python source file from the cache. |
| 28 |
Update the cache if it doesn't contain an entry for this file already.""" |
| 29 |
|
| 30 |
lines = getlines(filename, module_globals) |
| 31 |
if 1 <= lineno <= len(lines): |
| 32 |
return lines[lineno - 1] |
| 33 |
return '' |
| 34 |
|
| 35 |
|
| 36 |
def getlines(filename, module_globals=None): |
| 37 |
"""Get the lines for a Python source file from the cache. |
| 38 |
Update the cache if it doesn't contain an entry for this file already.""" |
| 39 |
|
| 40 |
if filename in cache: |
| 41 |
entry = cache[filename] |
| 42 |
if len(entry) != 1: |
| 43 |
return cache[filename][2] |
| 44 |
|
| 45 |
try: |
| 46 |
return updatecache(filename, module_globals) |
| 47 |
except MemoryError: |
| 48 |
clearcache() |
| 49 |
return [] |
| 50 |
|
| 51 |
|
| 52 |
def checkcache(filename=None): |
| 53 |
"""Discard cache entries that are out of date. |
| 54 |
(This is not checked upon each call!)""" |
| 55 |
|
| 56 |
if filename is None: |
| 57 |
filenames = cache.copy().keys() |
| 58 |
elif filename in cache: |
| 59 |
filenames = [filename] |
| 60 |
else: |
| 61 |
return |
| 62 |
|
| 63 |
for filename in filenames: |
| 64 |
|
| 65 |
entry = cache.get(filename) |
| 66 |
if entry is None or len(entry) == 1: |
| 67 |
|
| 68 |
continue |
| 69 |
size, mtime, lines, fullname = entry |
| 70 |
if mtime is None: |
| 71 |
continue |
| 72 |
try: |
| 73 |
stat = os.stat(fullname) |
| 74 |
except OSError: |
| 75 |
cache.pop(filename, None) |
| 76 |
continue |
| 77 |
if size != stat.st_size or mtime != stat.st_mtime: |
| 78 |
cache.pop(filename, None) |
| 79 |
|
| 80 |
|
| 81 |
def updatecache(filename, module_globals=None): |
| 82 |
"""Update a cache entry and return its list of lines. |
| 83 |
If something's wrong, print a message, discard the cache entry, |
| 84 |
and return an empty list.""" |
| 85 |
|
| 86 |
if filename in cache: |
| 87 |
if len(cache[filename]) != 1: |
| 88 |
cache.pop(filename, None) |
| 89 |
if not filename or (filename.startswith('<') and filename.endswith('>')): |
| 90 |
return [] |
| 91 |
|
| 92 |
fullname = filename |
| 93 |
try: |
| 94 |
stat = os.stat(fullname) |
| 95 |
except OSError: |
| 96 |
basename = filename |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
if lazycache(filename, module_globals): |
| 101 |
try: |
| 102 |
data = cache[filename][0]() |
| 103 |
except (ImportError, OSError): |
| 104 |
pass |
| 105 |
else: |
| 106 |
if data is None: |
| 107 |
|
| 108 |
|
| 109 |
return [] |
| 110 |
cache[filename] = ( |
| 111 |
len(data), |
| 112 |
None, |
| 113 |
[line + '\n' for line in data.splitlines()], |
| 114 |
fullname |
| 115 |
) |
| 116 |
return cache[filename][2] |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
if os.path.isabs(filename): |
| 121 |
return [] |
| 122 |
|
| 123 |
for dirname in sys.path: |
| 124 |
try: |
| 125 |
fullname = os.path.join(dirname, basename) |
| 126 |
except (TypeError, AttributeError): |
| 127 |
|
| 128 |
continue |
| 129 |
try: |
| 130 |
stat = os.stat(fullname) |
| 131 |
break |
| 132 |
except OSError: |
| 133 |
pass |
| 134 |
else: |
| 135 |
return [] |
| 136 |
try: |
| 137 |
with tokenize.open(fullname) as fp: |
| 138 |
lines = fp.readlines() |
| 139 |
except (OSError, UnicodeDecodeError, SyntaxError): |
| 140 |
return [] |
| 141 |
if lines and not lines[-1].endswith('\n'): |
| 142 |
lines[-1] += '\n' |
| 143 |
size, mtime = stat.st_size, stat.st_mtime |
| 144 |
cache[filename] = size, mtime, lines, fullname |
| 145 |
return lines |
| 146 |
|
| 147 |
|
| 148 |
def lazycache(filename, module_globals): |
| 149 |
"""Seed the cache for filename with module_globals. |
| 150 |
|
| 151 |
The module loader will be asked for the source only when getlines is |
| 152 |
called, not immediately. |
| 153 |
|
| 154 |
If there is an entry in the cache already, it is not altered. |
| 155 |
|
| 156 |
:return: True if a lazy load is registered in the cache, |
| 157 |
otherwise False. To register such a load a module loader with a |
| 158 |
get_source method must be found, the filename must be a cacheable |
| 159 |
filename, and the filename must not be already cached. |
| 160 |
""" |
| 161 |
if filename in cache: |
| 162 |
if len(cache[filename]) == 1: |
| 163 |
return True |
| 164 |
else: |
| 165 |
return False |
| 166 |
if not filename or (filename.startswith('<') and filename.endswith('>')): |
| 167 |
return False |
| 168 |
|
| 169 |
if module_globals and '__name__' in module_globals: |
| 170 |
spec = module_globals.get('__spec__') |
| 171 |
name = getattr(spec, 'name', None) or module_globals['__name__'] |
| 172 |
loader = getattr(spec, 'loader', None) |
| 173 |
if loader is None: |
| 174 |
loader = module_globals.get('__loader__') |
| 175 |
get_source = getattr(loader, 'get_source', None) |
| 176 |
|
| 177 |
if name and get_source: |
| 178 |
get_lines = functools.partial(get_source, name) |
| 179 |
cache[filename] = (get_lines,) |
| 180 |
return True |
| 181 |
return False |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
def _register_code(code, string, name): |
| 186 |
cache[code] = ( |
| 187 |
len(string), |
| 188 |
None, |
| 189 |
[line + '\n' for line in string.splitlines()], |
| 190 |
name) |