| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-05 12:07:57 +13: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-29 16:59:43 +00:00
										 |  |  | __all__ = ["getline", "clearcache", "checkcache"] | 
					
						
							| 
									
										
										
										
											2001-01-24 06:27:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | def getline(filename, lineno, module_globals=None): | 
					
						
							|  |  |  |     lines = getlines(filename, module_globals) | 
					
						
							| 
									
										
										
										
											2000-02-04 15:10:34 +00:00
										 |  |  |     if 1 <= lineno <= len(lines): | 
					
						
							|  |  |  |         return lines[lineno-1] | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return '' | 
					
						
							| 
									
										
										
										
											1992-01-10 14:54:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # The cache | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 = {} | 
					
						
							| 
									
										
										
										
											1992-01-10 14:54:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def clearcache(): | 
					
						
							| 
									
										
										
										
											2000-02-04 15:10:34 +00:00
										 |  |  |     """Clear the cache entirely.""" | 
					
						
							| 
									
										
										
										
											1992-01-10 14:54:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-04 15:10:34 +00:00
										 |  |  |     global cache | 
					
						
							|  |  |  |     cache = {} | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							| 
									
										
										
										
											2004-10-26 09:16:42 +00:00
										 |  |  |     else: | 
					
						
							|  |  |  |         if filename in cache: | 
					
						
							|  |  |  |             filenames = [filename] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for filename in filenames: | 
					
						
							| 
									
										
										
										
											2015-03-05 12:07:57 +13: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 22:02:39 +02:00
										 |  |  |         except OSError: | 
					
						
							| 
									
										
										
										
											2000-02-04 15:10:34 +00:00
										 |  |  |             del cache[filename] | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2002-06-01 19:51:15 +00:00
										 |  |  |         if size != stat.st_size or mtime != stat.st_mtime: | 
					
						
							| 
									
										
										
										
											2000-02-04 15:10:34 +00:00
										 |  |  |             del cache[filename] | 
					
						
							| 
									
										
										
										
											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-05 12:07:57 +13:00
										 |  |  |         if len(cache[filename]) != 1: | 
					
						
							|  |  |  |             del cache[filename] | 
					
						
							| 
									
										
										
										
											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] = ( | 
					
						
							|  |  |  |                     len(data), None, | 
					
						
							|  |  |  |                     [line+'\n' for line in data.splitlines()], fullname | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 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 22:02:39 +02: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() | 
					
						
							| 
									
										
										
										
											2012-12-25 16:47:37 +02:00
										 |  |  |     except OSError: | 
					
						
							| 
									
										
										
										
											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-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 | 
					
						
							|  |  |  |         get_source method must be found, the filename must be a cachable | 
					
						
							|  |  |  |         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 | 
					
						
							|  |  |  |     if module_globals and '__loader__' in module_globals: | 
					
						
							|  |  |  |         name = module_globals.get('__name__') | 
					
						
							|  |  |  |         loader = module_globals['__loader__'] | 
					
						
							|  |  |  |         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 |