linecache.py 6.1 KB

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