Mercurial > cpython-lru_cache-weakref
annotate Lib/linecache.py @ 17322:bd110eadc19a legacy-trunk
One-liner critical fix from Jim Fulton: Eric's string-method crusade
got the order backwards in a line (for .find()).
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Thu, 05 Apr 2001 22:26:23 +0000 |
| parents | 32755b456369 |
| children | 2a94bf235daa |
| rev | line source |
|---|---|
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
1 """Cache lines from files. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
2 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
3 This is intended to read lines from modules imported -- hence if a filename |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
4 is not found, it will look down the module search path for a file by |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
5 that name. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
6 """ |
| 566 | 7 |
|
722
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
8 import sys |
| 566 | 9 import os |
| 10 from stat import * | |
| 11 | |
|
16198
32755b456369
a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents:
15543
diff
changeset
|
12 __all__ = ["getline","clearcache","checkcache"] |
|
32755b456369
a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents:
15543
diff
changeset
|
13 |
| 566 | 14 def getline(filename, lineno): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
15 lines = getlines(filename) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
16 if 1 <= lineno <= len(lines): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
17 return lines[lineno-1] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
18 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
19 return '' |
| 566 | 20 |
| 21 | |
| 22 # The cache | |
| 23 | |
| 24 cache = {} # The cache | |
| 25 | |
| 26 | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
27 def clearcache(): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
28 """Clear the cache entirely.""" |
| 566 | 29 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
30 global cache |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
31 cache = {} |
| 566 | 32 |
| 33 | |
| 34 def getlines(filename): | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
35 """Get the lines for a file from the cache. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
36 Update the cache if it doesn't contain an entry for this file already.""" |
| 566 | 37 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
38 if cache.has_key(filename): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
39 return cache[filename][2] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
40 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
41 return updatecache(filename) |
| 566 | 42 |
| 43 | |
| 44 def checkcache(): | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
45 """Discard cache entries that are out of date. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
46 (This is not checked upon each call!)""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
47 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
48 for filename in cache.keys(): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
49 size, mtime, lines, fullname = cache[filename] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
50 try: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
51 stat = os.stat(fullname) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
52 except os.error: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
53 del cache[filename] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
54 continue |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
55 if size != stat[ST_SIZE] or mtime != stat[ST_MTIME]: |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
56 del cache[filename] |
| 566 | 57 |
| 58 | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
59 def updatecache(filename): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
60 """Update a cache entry and return its list of lines. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
61 If something's wrong, print a message, discard the cache entry, |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
62 and return an empty list.""" |
| 566 | 63 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
64 if cache.has_key(filename): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
65 del cache[filename] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
66 if not filename or filename[0] + filename[-1] == '<>': |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
67 return [] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
68 fullname = filename |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
69 try: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
70 stat = os.stat(fullname) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
71 except os.error, msg: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
72 # Try looking through the module search path |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
73 basename = os.path.split(filename)[1] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
74 for dirname in sys.path: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
75 fullname = os.path.join(dirname, basename) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
76 try: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
77 stat = os.stat(fullname) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
78 break |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
79 except os.error: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
80 pass |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
81 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
82 # No luck |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
83 ## print '*** Cannot stat', filename, ':', msg |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
84 return [] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
85 try: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
86 fp = open(fullname, 'r') |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
87 lines = fp.readlines() |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
88 fp.close() |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
89 except IOError, msg: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
90 ## print '*** Cannot open', fullname, ':', msg |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
91 return [] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
92 size, mtime = stat[ST_SIZE], stat[ST_MTIME] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
93 cache[filename] = size, mtime, lines, fullname |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
94 return lines |
