2015-03-18 10:16:50 -03:00
|
|
|
"""Cache lines from Python source files.
|
2000-02-04 11:10:34 -04:00
|
|
|
|
|
|
|
This is intended to read lines from modules imported -- hence if a filename
|
|
|
|
is not found, it will look down the module search path for a file by
|
|
|
|
that name.
|
|
|
|
"""
|
1992-01-10 10:54:42 -04:00
|
|
|
|
1992-03-27 11:12:43 -04:00
|
|
|
import sys
|
1992-01-10 10:54:42 -04:00
|
|
|
import os
|
|
|
|
|
2020-01-25 22:07:40 -04:00
|
|
|
__all__ = ["getline", "clearcache", "checkcache", "lazycache"]
|
2001-01-24 02:27:27 -04:00
|
|
|
|
1992-01-10 10:54:42 -04:00
|
|
|
|
2015-03-04 19:07:57 -04:00
|
|
|
# The cache. Maps filenames to either a thunk which will provide source code,
|
|
|
|
# or a tuple (size, mtime, lines, fullname) once loaded.
|
|
|
|
cache = {}
|
1992-01-10 10:54:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
def clearcache():
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Clear the cache entirely."""
|
2020-01-25 22:07:40 -04:00
|
|
|
cache.clear()
|
1992-01-10 10:54:42 -04:00
|
|
|
|
2020-01-25 22:07:40 -04:00
|
|
|
|
|
|
|
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 ''
|
1992-01-10 10:54:42 -04:00
|
|
|
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
def getlines(filename, module_globals=None):
|
2015-03-18 10:16:50 -03:00
|
|
|
"""Get the lines for a Python source file from the cache.
|
2000-02-04 11:10:34 -04:00
|
|
|
Update the cache if it doesn't contain an entry for this file already."""
|
1992-01-10 10:54:42 -04:00
|
|
|
|
2002-06-01 11:18:47 -03:00
|
|
|
if filename in cache:
|
2015-03-04 19:07:57 -04:00
|
|
|
entry = cache[filename]
|
2015-04-01 10:56:13 -03:00
|
|
|
if len(entry) != 1:
|
|
|
|
return cache[filename][2]
|
2015-04-01 10:54:05 -03:00
|
|
|
|
|
|
|
try:
|
2006-04-21 07:40:58 -03:00
|
|
|
return updatecache(filename, module_globals)
|
2015-04-01 10:54:05 -03:00
|
|
|
except MemoryError:
|
|
|
|
clearcache()
|
|
|
|
return []
|
1992-01-10 10:54:42 -04:00
|
|
|
|
|
|
|
|
2004-10-26 06:16:42 -03:00
|
|
|
def checkcache(filename=None):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Discard cache entries that are out of date.
|
|
|
|
(This is not checked upon each call!)"""
|
1992-01-10 10:54:42 -04:00
|
|
|
|
2004-10-26 06:16:42 -03:00
|
|
|
if filename is None:
|
2007-02-26 18:21:25 -04:00
|
|
|
filenames = list(cache.keys())
|
2020-01-25 22:07:40 -04:00
|
|
|
elif filename in cache:
|
|
|
|
filenames = [filename]
|
2004-10-26 06:16:42 -03:00
|
|
|
else:
|
2020-01-25 22:07:40 -04:00
|
|
|
return
|
2004-10-26 06:16:42 -03:00
|
|
|
|
|
|
|
for filename in filenames:
|
2015-03-04 19:07:57 -04:00
|
|
|
entry = cache[filename]
|
|
|
|
if len(entry) == 1:
|
|
|
|
# lazy cache entry, leave it lazy.
|
|
|
|
continue
|
|
|
|
size, mtime, lines, fullname = entry
|
2006-04-21 07:40:58 -03:00
|
|
|
if mtime is None:
|
|
|
|
continue # no-op for files loaded via a __loader__
|
2000-02-04 11:10:34 -04:00
|
|
|
try:
|
|
|
|
stat = os.stat(fullname)
|
2012-12-18 16:02:39 -04:00
|
|
|
except OSError:
|
2020-05-13 19:41:57 -03:00
|
|
|
cache.pop(filename, None)
|
2000-02-04 11:10:34 -04:00
|
|
|
continue
|
2002-06-01 16:51:15 -03:00
|
|
|
if size != stat.st_size or mtime != stat.st_mtime:
|
2020-05-13 19:41:57 -03:00
|
|
|
cache.pop(filename, None)
|
1992-01-10 10:54:42 -04:00
|
|
|
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
def updatecache(filename, module_globals=None):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""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."""
|
|
|
|
|
2023-10-26 03:17:28 -03:00
|
|
|
import tokenize
|
|
|
|
|
2002-06-01 11:18:47 -03:00
|
|
|
if filename in cache:
|
2015-03-04 19:07:57 -04:00
|
|
|
if len(cache[filename]) != 1:
|
2020-05-13 19:41:57 -03:00
|
|
|
cache.pop(filename, None)
|
2010-05-21 18:45:06 -03:00
|
|
|
if not filename or (filename.startswith('<') and filename.endswith('>')):
|
2000-02-04 11:10:34 -04:00
|
|
|
return []
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2000-02-04 11:10:34 -04:00
|
|
|
fullname = filename
|
|
|
|
try:
|
|
|
|
stat = os.stat(fullname)
|
2010-05-21 18:45:06 -03:00
|
|
|
except OSError:
|
2009-05-05 05:31:54 -03:00
|
|
|
basename = filename
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2015-03-04 19:07:57 -04:00
|
|
|
# Realise a lazy loader based lookup if there is one
|
|
|
|
# otherwise try to lookup right now.
|
|
|
|
if lazycache(filename, module_globals):
|
|
|
|
try:
|
|
|
|
data = cache[filename][0]()
|
|
|
|
except (ImportError, OSError):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if data is None:
|
|
|
|
# No luck, the PEP302 loader cannot find the source
|
|
|
|
# for this module.
|
|
|
|
return []
|
|
|
|
cache[filename] = (
|
2020-01-25 22:07:40 -04:00
|
|
|
len(data),
|
|
|
|
None,
|
|
|
|
[line + '\n' for line in data.splitlines()],
|
|
|
|
fullname
|
2015-03-04 19:07:57 -04:00
|
|
|
)
|
|
|
|
return cache[filename][2]
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2009-05-05 05:31:54 -03:00
|
|
|
# Try looking through the module search path, which is only useful
|
|
|
|
# when handling a relative filename.
|
|
|
|
if os.path.isabs(filename):
|
|
|
|
return []
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2000-02-04 11:10:34 -04:00
|
|
|
for dirname in sys.path:
|
|
|
|
try:
|
2001-05-29 01:27:01 -03:00
|
|
|
fullname = os.path.join(dirname, basename)
|
|
|
|
except (TypeError, AttributeError):
|
|
|
|
# Not sufficiently string-like to do anything useful with.
|
2010-05-21 18:45:06 -03:00
|
|
|
continue
|
|
|
|
try:
|
|
|
|
stat = os.stat(fullname)
|
|
|
|
break
|
2012-12-18 16:02:39 -04:00
|
|
|
except OSError:
|
2000-02-04 11:10:34 -04:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
return []
|
2010-05-21 18:45:06 -03:00
|
|
|
try:
|
2010-11-08 21:08:59 -04:00
|
|
|
with tokenize.open(fullname) as fp:
|
2010-05-21 18:45:06 -03:00
|
|
|
lines = fp.readlines()
|
2022-06-30 06:18:18 -03:00
|
|
|
except (OSError, UnicodeDecodeError, SyntaxError):
|
2010-09-28 22:30:45 -03:00
|
|
|
return []
|
2024-03-18 12:13:02 -03:00
|
|
|
if not lines:
|
|
|
|
lines = ['\n']
|
|
|
|
elif not lines[-1].endswith('\n'):
|
2010-05-21 18:45:06 -03:00
|
|
|
lines[-1] += '\n'
|
2002-06-01 16:51:15 -03:00
|
|
|
size, mtime = stat.st_size, stat.st_mtime
|
2000-02-04 11:10:34 -04:00
|
|
|
cache[filename] = size, mtime, lines, fullname
|
|
|
|
return lines
|
2015-03-04 19:07:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
def lazycache(filename, module_globals):
|
|
|
|
"""Seed the cache for filename with module_globals.
|
|
|
|
|
|
|
|
The module loader will be asked for the source only when getlines is
|
|
|
|
called, not immediately.
|
|
|
|
|
|
|
|
If there is an entry in the cache already, it is not altered.
|
|
|
|
|
|
|
|
:return: True if a lazy load is registered in the cache,
|
|
|
|
otherwise False. To register such a load a module loader with a
|
2021-10-06 20:13:48 -03:00
|
|
|
get_source method must be found, the filename must be a cacheable
|
2015-03-04 19:07:57 -04:00
|
|
|
filename, and the filename must not be already cached.
|
|
|
|
"""
|
|
|
|
if filename in cache:
|
|
|
|
if len(cache[filename]) == 1:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
if not filename or (filename.startswith('<') and filename.endswith('>')):
|
|
|
|
return False
|
|
|
|
# Try for a __loader__, if available
|
2020-11-06 22:45:56 -04:00
|
|
|
if module_globals and '__name__' in module_globals:
|
2024-02-20 12:47:41 -04:00
|
|
|
spec = module_globals.get('__spec__')
|
|
|
|
name = getattr(spec, 'name', None) or module_globals['__name__']
|
|
|
|
loader = getattr(spec, 'loader', None)
|
|
|
|
if loader is None:
|
|
|
|
loader = module_globals.get('__loader__')
|
2015-03-04 19:07:57 -04:00
|
|
|
get_source = getattr(loader, 'get_source', None)
|
|
|
|
|
|
|
|
if name and get_source:
|
2023-10-26 03:17:28 -03:00
|
|
|
def get_lines(name=name, *args, **kwargs):
|
|
|
|
return get_source(name, *args, **kwargs)
|
2015-03-04 19:07:57 -04:00
|
|
|
cache[filename] = (get_lines,)
|
|
|
|
return True
|
|
|
|
return False
|
2023-10-13 06:25:37 -03:00
|
|
|
|
2023-10-26 03:17:28 -03:00
|
|
|
|
2023-10-13 06:25:37 -03:00
|
|
|
def _register_code(code, string, name):
|
|
|
|
cache[code] = (
|
|
|
|
len(string),
|
|
|
|
None,
|
|
|
|
[line + '\n' for line in string.splitlines()],
|
|
|
|
name)
|