2015-03-18 13:16:50 +00:00
|
|
|
"""Cache lines from Python source files.
|
2000-02-04 15:10:34 +00: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 14:54:42 +00:00
|
|
|
|
2015-03-04 23:07:57 +00:00
|
|
|
import functools
|
1992-03-27 15:12:43 +00:00
|
|
|
import sys
|
1992-01-10 14:54:42 +00:00
|
|
|
import os
|
2009-03-24 22:30:15 +00:00
|
|
|
import tokenize
|
1992-01-10 14:54:42 +00:00
|
|
|
|
2020-01-26 02:07:40 +00:00
|
|
|
__all__ = ["getline", "clearcache", "checkcache", "lazycache"]
|
2001-01-24 06:27:27 +00:00
|
|
|
|
1992-01-10 14:54:42 +00:00
|
|
|
|
2015-03-04 23:07:57 +00: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 14:54:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def clearcache():
|
2000-02-04 15:10:34 +00:00
|
|
|
"""Clear the cache entirely."""
|
2020-01-26 02:07:40 +00:00
|
|
|
cache.clear()
|
1992-01-10 14:54:42 +00:00
|
|
|
|
2020-01-26 02:07:40 +00: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 14:54:42 +00:00
|
|
|
|
|
|
|
|
2006-04-21 10:40:58 +00:00
|
|
|
def getlines(filename, module_globals=None):
|
2015-03-18 13:16:50 +00:00
|
|
|
"""Get the lines for a Python source file from the cache.
|
2000-02-04 15:10:34 +00:00
|
|
|
Update the cache if it doesn't contain an entry for this file already."""
|
1992-01-10 14:54:42 +00:00
|
|
|
|
2002-06-01 14:18:47 +00:00
|
|
|
if filename in cache:
|
2015-03-04 23:07:57 +00:00
|
|
|
entry = cache[filename]
|
2015-04-01 13:56:13 +00:00
|
|
|
if len(entry) != 1:
|
|
|
|
return cache[filename][2]
|
2015-04-01 13:54:05 +00:00
|
|
|
|
|
|
|
try:
|
2006-04-21 10:40:58 +00:00
|
|
|
return updatecache(filename, module_globals)
|
2015-04-01 13:54:05 +00:00
|
|
|
except MemoryError:
|
|
|
|
clearcache()
|
|
|
|
return []
|
1992-01-10 14:54:42 +00:00
|
|
|
|
|
|
|
|
2004-10-26 09:16:42 +00:00
|
|
|
def checkcache(filename=None):
|
2000-02-04 15:10:34 +00:00
|
|
|
"""Discard cache entries that are out of date.
|
|
|
|
(This is not checked upon each call!)"""
|
1992-01-10 14:54:42 +00:00
|
|
|
|
2004-10-26 09:16:42 +00:00
|
|
|
if filename is None:
|
2007-02-26 22:21:25 +00:00
|
|
|
filenames = list(cache.keys())
|
2020-01-26 02:07:40 +00:00
|
|
|
elif filename in cache:
|
|
|
|
filenames = [filename]
|
2004-10-26 09:16:42 +00:00
|
|
|
else:
|
2020-01-26 02:07:40 +00:00
|
|
|
return
|
2004-10-26 09:16:42 +00:00
|
|
|
|
|
|
|
for filename in filenames:
|
2015-03-04 23:07:57 +00:00
|
|
|
entry = cache[filename]
|
|
|
|
if len(entry) == 1:
|
|
|
|
# lazy cache entry, leave it lazy.
|
|
|
|
continue
|
|
|
|
size, mtime, lines, fullname = entry
|
2006-04-21 10:40:58 +00:00
|
|
|
if mtime is None:
|
|
|
|
continue # no-op for files loaded via a __loader__
|
2000-02-04 15:10:34 +00:00
|
|
|
try:
|
|
|
|
stat = os.stat(fullname)
|
2012-12-18 20:02:39 +00:00
|
|
|
except OSError:
|
2020-05-13 22:41:57 +00:00
|
|
|
cache.pop(filename, None)
|
2000-02-04 15:10:34 +00:00
|
|
|
continue
|
2002-06-01 19:51:15 +00:00
|
|
|
if size != stat.st_size or mtime != stat.st_mtime:
|
2020-05-13 22:41:57 +00:00
|
|
|
cache.pop(filename, None)
|
1992-01-10 14:54:42 +00:00
|
|
|
|
|
|
|
|
2006-04-21 10:40:58 +00:00
|
|
|
def updatecache(filename, module_globals=None):
|
2000-02-04 15:10:34 +00: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."""
|
|
|
|
|
2002-06-01 14:18:47 +00:00
|
|
|
if filename in cache:
|
2015-03-04 23:07:57 +00:00
|
|
|
if len(cache[filename]) != 1:
|
2020-05-13 22:41:57 +00:00
|
|
|
cache.pop(filename, None)
|
2010-05-21 21:45:06 +00:00
|
|
|
if not filename or (filename.startswith('<') and filename.endswith('>')):
|
2000-02-04 15:10:34 +00:00
|
|
|
return []
|
2006-04-21 10:40:58 +00:00
|
|
|
|
2000-02-04 15:10:34 +00:00
|
|
|
fullname = filename
|
|
|
|
try:
|
|
|
|
stat = os.stat(fullname)
|
2010-05-21 21:45:06 +00:00
|
|
|
except OSError:
|
2009-05-05 08:31:54 +00:00
|
|
|
basename = filename
|
2006-04-21 10:40:58 +00:00
|
|
|
|
2015-03-04 23:07:57 +00: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-26 02:07:40 +00:00
|
|
|
len(data),
|
|
|
|
None,
|
|
|
|
[line + '\n' for line in data.splitlines()],
|
|
|
|
fullname
|
2015-03-04 23:07:57 +00:00
|
|
|
)
|
|
|
|
return cache[filename][2]
|
2006-04-21 10:40:58 +00:00
|
|
|
|
2009-05-05 08:31:54 +00: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 10:40:58 +00:00
|
|
|
|
2000-02-04 15:10:34 +00:00
|
|
|
for dirname in sys.path:
|
|
|
|
try:
|
2001-05-29 04:27:01 +00:00
|
|
|
fullname = os.path.join(dirname, basename)
|
|
|
|
except (TypeError, AttributeError):
|
|
|
|
# Not sufficiently string-like to do anything useful with.
|
2010-05-21 21:45:06 +00:00
|
|
|
continue
|
|
|
|
try:
|
|
|
|
stat = os.stat(fullname)
|
|
|
|
break
|
2012-12-18 20:02:39 +00:00
|
|
|
except OSError:
|
2000-02-04 15:10:34 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
return []
|
2010-05-21 21:45:06 +00:00
|
|
|
try:
|
2010-11-09 01:08:59 +00:00
|
|
|
with tokenize.open(fullname) as fp:
|
2010-05-21 21:45:06 +00:00
|
|
|
lines = fp.readlines()
|
2022-06-30 09:59:33 +00:00
|
|
|
except (OSError, UnicodeDecodeError, SyntaxError):
|
2010-09-29 01:30:45 +00:00
|
|
|
return []
|
2010-05-21 21:45:06 +00:00
|
|
|
if lines and not lines[-1].endswith('\n'):
|
|
|
|
lines[-1] += '\n'
|
2002-06-01 19:51:15 +00:00
|
|
|
size, mtime = stat.st_size, stat.st_mtime
|
2000-02-04 15:10:34 +00:00
|
|
|
cache[filename] = size, mtime, lines, fullname
|
|
|
|
return lines
|
2015-03-04 23:07:57 +00: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 23:13:48 +00:00
|
|
|
get_source method must be found, the filename must be a cacheable
|
2015-03-04 23:07:57 +00: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-07 02:45:56 +00:00
|
|
|
if module_globals and '__name__' in module_globals:
|
2024-02-20 18:18:16 +00: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 23:07:57 +00:00
|
|
|
get_source = getattr(loader, 'get_source', None)
|
|
|
|
|
|
|
|
if name and get_source:
|
|
|
|
get_lines = functools.partial(get_source, name)
|
|
|
|
cache[filename] = (get_lines,)
|
|
|
|
return True
|
|
|
|
return False
|