| 
									
										
										
										
											2015-03-18 14:16:50 +01: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-26 10:07:40 +08:00
										 |  |  | __all__ = ["getline", "clearcache", "checkcache", "lazycache"] | 
					
						
							| 
									
										
										
										
											2001-01-24 06:27:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-01-10 14:54:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-05 12:07:57 +13:00
										 |  |  | # The cache. Maps filenames to either a thunk which will provide source code, | 
					
						
							|  |  |  | # or a tuple (size, mtime, lines, fullname) once loaded. | 
					
						
							|  |  |  | cache = {} | 
					
						
							| 
									
										
										
										
											2025-03-11 00:11:24 +01:00
										 |  |  | _interactive_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 10:07:40 +08:00
										 |  |  |     cache.clear() | 
					
						
							| 
									
										
										
										
											1992-01-10 14:54:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-26 10:07:40 +08: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 14:16:50 +01: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-05 12:07:57 +13:00
										 |  |  |         entry = cache[filename] | 
					
						
							| 
									
										
										
										
											2015-04-01 16:56:13 +03:00
										 |  |  |         if len(entry) != 1: | 
					
						
							|  |  |  |             return cache[filename][2] | 
					
						
							| 
									
										
										
										
											2015-04-01 16:54:05 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  |         return updatecache(filename, module_globals) | 
					
						
							| 
									
										
										
										
											2015-04-01 16:54:05 +03:00
										 |  |  |     except MemoryError: | 
					
						
							|  |  |  |         clearcache() | 
					
						
							|  |  |  |         return [] | 
					
						
							| 
									
										
										
										
											1992-01-10 14:54:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-11 00:11:24 +01:00
										 |  |  | def _getline_from_code(filename, lineno): | 
					
						
							|  |  |  |     lines = _getlines_from_code(filename) | 
					
						
							|  |  |  |     if 1 <= lineno <= len(lines): | 
					
						
							|  |  |  |         return lines[lineno - 1] | 
					
						
							|  |  |  |     return '' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-11 23:43:07 +00:00
										 |  |  | def _make_key(code): | 
					
						
							|  |  |  |     return (code.co_filename, code.co_qualname, code.co_firstlineno) | 
					
						
							| 
									
										
										
										
											2025-03-11 00:11:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | def _getlines_from_code(code): | 
					
						
							| 
									
										
										
										
											2025-03-11 23:43:07 +00:00
										 |  |  |     code_id = _make_key(code) | 
					
						
							| 
									
										
										
										
											2025-03-11 00:11:24 +01:00
										 |  |  |     if code_id in _interactive_cache: | 
					
						
							|  |  |  |         entry = _interactive_cache[code_id] | 
					
						
							|  |  |  |         if len(entry) != 1: | 
					
						
							|  |  |  |             return _interactive_cache[code_id][2] | 
					
						
							|  |  |  |     return [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							| 
									
										
										
										
											2024-12-10 09:06:26 +01:00
										 |  |  |         # get keys atomically | 
					
						
							|  |  |  |         filenames = cache.copy().keys() | 
					
						
							| 
									
										
										
										
											2004-10-26 09:16:42 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2024-12-10 09:06:26 +01:00
										 |  |  |         filenames = [filename] | 
					
						
							| 
									
										
										
										
											2004-10-26 09:16:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for filename in filenames: | 
					
						
							| 
									
										
										
										
											2024-12-10 09:06:26 +01:00
										 |  |  |         try: | 
					
						
							|  |  |  |             entry = cache[filename] | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-05 12:07:57 +13:00
										 |  |  |         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__ | 
					
						
							| 
									
										
										
										
											2024-04-10 20:09:25 +01:00
										 |  |  |         try: | 
					
						
							|  |  |  |             # This import can fail if the interpreter is shutting down | 
					
						
							|  |  |  |             import os | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2000-02-04 15:10:34 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             stat = os.stat(fullname) | 
					
						
							| 
									
										
										
										
											2024-07-27 12:54:49 +02:00
										 |  |  |         except (OSError, ValueError): | 
					
						
							| 
									
										
										
										
											2020-05-13 17:41:57 -05: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 17:41:57 -05: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."""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-16 15:37:18 -07:00
										 |  |  |     # These imports are not at top level because linecache is in the critical | 
					
						
							|  |  |  |     # path of the interpreter startup and importing os and sys take a lot of time | 
					
						
							|  |  |  |     # and slows down the startup sequence. | 
					
						
							| 
									
										
										
										
											2025-03-11 00:11:24 +01:00
										 |  |  |     try: | 
					
						
							|  |  |  |         import os | 
					
						
							|  |  |  |         import sys | 
					
						
							|  |  |  |         import tokenize | 
					
						
							|  |  |  |     except ImportError: | 
					
						
							|  |  |  |         # These import can fail if the interpreter is shutting down | 
					
						
							|  |  |  |         return [] | 
					
						
							| 
									
										
										
										
											2023-10-26 15:17:28 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-01 14:18:47 +00:00
										 |  |  |     if filename in cache: | 
					
						
							| 
									
										
										
										
											2015-03-05 12:07:57 +13:00
										 |  |  |         if len(cache[filename]) != 1: | 
					
						
							| 
									
										
										
										
											2020-05-13 17:41:57 -05: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-05 12:07:57 +13: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 10:07:40 +08:00
										 |  |  |                     len(data), | 
					
						
							|  |  |  |                     None, | 
					
						
							|  |  |  |                     [line + '\n' for line in data.splitlines()], | 
					
						
							|  |  |  |                     fullname | 
					
						
							| 
									
										
										
										
											2015-03-05 12:07:57 +13: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 | 
					
						
							| 
									
										
										
										
											2024-07-27 12:54:49 +02:00
										 |  |  |             except (OSError, ValueError): | 
					
						
							| 
									
										
										
										
											2000-02-04 15:10:34 +00:00
										 |  |  |                 pass | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return [] | 
					
						
							| 
									
										
										
										
											2024-07-27 12:54:49 +02:00
										 |  |  |     except ValueError:  # may be raised by os.stat() | 
					
						
							|  |  |  |         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 10:18:18 +01:00
										 |  |  |     except (OSError, UnicodeDecodeError, SyntaxError): | 
					
						
							| 
									
										
										
										
											2010-09-29 01:30:45 +00:00
										 |  |  |         return [] | 
					
						
							| 
									
										
										
										
											2024-03-18 16:13:02 +01:00
										 |  |  |     if not lines: | 
					
						
							|  |  |  |         lines = ['\n'] | 
					
						
							|  |  |  |     elif not lines[-1].endswith('\n'): | 
					
						
							| 
									
										
										
										
											2010-05-21 21:45:06 +00:00
										 |  |  |         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-05 12:07:57 +13: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-07 01:13:48 +02:00
										 |  |  |         get_source method must be found, the filename must be a cacheable | 
					
						
							| 
									
										
										
										
											2015-03-05 12:07:57 +13: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 18:45:56 -08:00
										 |  |  |     if module_globals and '__name__' in module_globals: | 
					
						
							| 
									
										
										
										
											2024-02-20 11:47:41 -05: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-05 12:07:57 +13:00
										 |  |  |         get_source = getattr(loader, 'get_source', None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if name and get_source: | 
					
						
							| 
									
										
										
										
											2023-10-26 15:17:28 +09:00
										 |  |  |             def get_lines(name=name, *args, **kwargs): | 
					
						
							|  |  |  |                 return get_source(name, *args, **kwargs) | 
					
						
							| 
									
										
										
										
											2015-03-05 12:07:57 +13:00
										 |  |  |             cache[filename] = (get_lines,) | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  |     return False | 
					
						
							| 
									
										
										
										
											2023-10-13 11:25:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | def _register_code(code, string, name): | 
					
						
							| 
									
										
										
										
											2025-03-11 00:11:24 +01:00
										 |  |  |     entry = (len(string), | 
					
						
							|  |  |  |              None, | 
					
						
							|  |  |  |              [line + '\n' for line in string.splitlines()], | 
					
						
							|  |  |  |              name) | 
					
						
							|  |  |  |     stack = [code] | 
					
						
							|  |  |  |     while stack: | 
					
						
							|  |  |  |         code = stack.pop() | 
					
						
							|  |  |  |         for const in code.co_consts: | 
					
						
							|  |  |  |             if isinstance(const, type(code)): | 
					
						
							|  |  |  |                 stack.append(const) | 
					
						
							| 
									
										
										
										
											2025-03-11 23:43:07 +00:00
										 |  |  |         _interactive_cache[_make_key(code)] = entry |