blob: c3f2c3fdca4340c4f6781b264d39a47c2e0bb19a [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Cache lines from files.
2
3This is intended to read lines from modules imported -- hence if a filename
4is not found, it will look down the module search path for a file by
5that name.
6"""
Guido van Rossum921c8241992-01-10 14:54:42 +00007
Guido van Rossumc341c621992-03-27 15:12:43 +00008import sys
Guido van Rossum921c8241992-01-10 14:54:42 +00009import os
Benjamin Peterson9b8d24b2009-03-24 22:30:15 +000010import tokenize
Guido van Rossum921c8241992-01-10 14:54:42 +000011
Jeremy Hylton97b2e842003-06-29 16:59:43 +000012__all__ = ["getline", "clearcache", "checkcache"]
Skip Montanaro17ab1232001-01-24 06:27:27 +000013
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014def getline(filename, lineno, module_globals=None):
15 lines = getlines(filename, module_globals)
Guido van Rossum54f22ed2000-02-04 15:10:34 +000016 if 1 <= lineno <= len(lines):
17 return lines[lineno-1]
18 else:
19 return ''
Guido van Rossum921c8241992-01-10 14:54:42 +000020
21
22# The cache
23
24cache = {} # The cache
25
26
Guido van Rossum921c8241992-01-10 14:54:42 +000027def clearcache():
Guido van Rossum54f22ed2000-02-04 15:10:34 +000028 """Clear the cache entirely."""
Guido van Rossum921c8241992-01-10 14:54:42 +000029
Guido van Rossum54f22ed2000-02-04 15:10:34 +000030 global cache
31 cache = {}
Guido van Rossum921c8241992-01-10 14:54:42 +000032
Guido van Rossum921c8241992-01-10 14:54:42 +000033
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034def getlines(filename, module_globals=None):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000035 """Get the lines for a file from the cache.
36 Update the cache if it doesn't contain an entry for this file already."""
Guido van Rossum921c8241992-01-10 14:54:42 +000037
Raymond Hettinger54f02222002-06-01 14:18:47 +000038 if filename in cache:
Guido van Rossum54f22ed2000-02-04 15:10:34 +000039 return cache[filename][2]
40 else:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041 return updatecache(filename, module_globals)
Guido van Rossum921c8241992-01-10 14:54:42 +000042
Guido van Rossum921c8241992-01-10 14:54:42 +000043
Hye-Shik Chang182ac852004-10-26 09:16:42 +000044def checkcache(filename=None):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000045 """Discard cache entries that are out of date.
46 (This is not checked upon each call!)"""
Guido van Rossum921c8241992-01-10 14:54:42 +000047
Hye-Shik Chang182ac852004-10-26 09:16:42 +000048 if filename is None:
Guido van Rossumf5433482007-02-26 22:21:25 +000049 filenames = list(cache.keys())
Hye-Shik Chang182ac852004-10-26 09:16:42 +000050 else:
51 if filename in cache:
52 filenames = [filename]
53 else:
54 return
55
56 for filename in filenames:
Guido van Rossum54f22ed2000-02-04 15:10:34 +000057 size, mtime, lines, fullname = cache[filename]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058 if mtime is None:
59 continue # no-op for files loaded via a __loader__
Guido van Rossum54f22ed2000-02-04 15:10:34 +000060 try:
61 stat = os.stat(fullname)
62 except os.error:
63 del cache[filename]
64 continue
Raymond Hettinger32200ae2002-06-01 19:51:15 +000065 if size != stat.st_size or mtime != stat.st_mtime:
Guido van Rossum54f22ed2000-02-04 15:10:34 +000066 del cache[filename]
Guido van Rossum921c8241992-01-10 14:54:42 +000067
Guido van Rossum921c8241992-01-10 14:54:42 +000068
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000069def updatecache(filename, module_globals=None):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000070 """Update a cache entry and return its list of lines.
71 If something's wrong, print a message, discard the cache entry,
72 and return an empty list."""
73
Raymond Hettinger54f02222002-06-01 14:18:47 +000074 if filename in cache:
Guido van Rossum54f22ed2000-02-04 15:10:34 +000075 del cache[filename]
Benjamin Petersonaada7b82010-05-21 21:45:06 +000076 if not filename or (filename.startswith('<') and filename.endswith('>')):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000077 return []
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078
Guido van Rossum54f22ed2000-02-04 15:10:34 +000079 fullname = filename
80 try:
81 stat = os.stat(fullname)
Benjamin Petersonaada7b82010-05-21 21:45:06 +000082 except OSError:
Georg Brandl991f9202009-05-05 08:31:54 +000083 basename = filename
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084
85 # Try for a __loader__, if available
86 if module_globals and '__loader__' in module_globals:
87 name = module_globals.get('__name__')
88 loader = module_globals['__loader__']
89 get_source = getattr(loader, 'get_source', None)
90
91 if name and get_source:
Nick Coghlanf088e5e2008-12-14 11:50:48 +000092 try:
93 data = get_source(name)
94 except (ImportError, IOError):
95 pass
96 else:
97 if data is None:
98 # No luck, the PEP302 loader cannot find the source
99 # for this module.
100 return []
101 cache[filename] = (
102 len(data), None,
103 [line+'\n' for line in data.splitlines()], fullname
104 )
105 return cache[filename][2]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000106
Georg Brandl991f9202009-05-05 08:31:54 +0000107 # Try looking through the module search path, which is only useful
108 # when handling a relative filename.
109 if os.path.isabs(filename):
110 return []
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000111
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000112 for dirname in sys.path:
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000113 try:
Tim Peters12f21ae2001-05-29 04:27:01 +0000114 fullname = os.path.join(dirname, basename)
115 except (TypeError, AttributeError):
116 # Not sufficiently string-like to do anything useful with.
Benjamin Petersonaada7b82010-05-21 21:45:06 +0000117 continue
118 try:
119 stat = os.stat(fullname)
120 break
121 except os.error:
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000122 pass
123 else:
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000124 return []
Benjamin Petersonaada7b82010-05-21 21:45:06 +0000125 try:
Victor Stinner58c07522010-11-09 01:08:59 +0000126 with tokenize.open(fullname) as fp:
Benjamin Petersonaada7b82010-05-21 21:45:06 +0000127 lines = fp.readlines()
128 except IOError:
Victor Stinner41a64a52010-09-29 01:30:45 +0000129 return []
Benjamin Petersonaada7b82010-05-21 21:45:06 +0000130 if lines and not lines[-1].endswith('\n'):
131 lines[-1] += '\n'
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000132 size, mtime = stat.st_size, stat.st_mtime
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000133 cache[filename] = size, mtime, lines, fullname
134 return lines