Mercurial > cpython-lru_cache-weakref
annotate Lib/linecache.py @ 15842:f6cd36498a50 legacy-trunk
Document extensions to .pth files.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Sat, 13 Jan 2001 09:54:41 +0000 |
| parents | 785155e2cfca |
| children | 32755b456369 |
| 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 | |
| 12 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
|
13 lines = getlines(filename) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
14 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
|
15 return lines[lineno-1] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
16 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
17 return '' |
| 566 | 18 |
| 19 | |
| 20 # The cache | |
| 21 | |
| 22 cache = {} # The cache | |
| 23 | |
| 24 | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
25 def clearcache(): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
26 """Clear the cache entirely.""" |
| 566 | 27 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
28 global cache |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
29 cache = {} |
| 566 | 30 |
| 31 | |
| 32 def getlines(filename): | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
33 """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
|
34 Update the cache if it doesn't contain an entry for this file already.""" |
| 566 | 35 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
36 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
|
37 return cache[filename][2] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
38 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
39 return updatecache(filename) |
| 566 | 40 |
| 41 | |
| 42 def checkcache(): | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
43 """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
|
44 (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
|
45 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
46 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
|
47 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
|
48 try: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
49 stat = os.stat(fullname) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
50 except os.error: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
51 del cache[filename] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
52 continue |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
53 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
|
54 del cache[filename] |
| 566 | 55 |
| 56 | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
57 def updatecache(filename): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
58 """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
|
59 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
|
60 and return an empty list.""" |
| 566 | 61 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
62 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
|
63 del cache[filename] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
64 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
|
65 return [] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
66 fullname = filename |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
67 try: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
68 stat = os.stat(fullname) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
69 except os.error, msg: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
70 # 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
|
71 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
|
72 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
|
73 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
|
74 try: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
75 stat = os.stat(fullname) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
76 break |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
77 except os.error: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
78 pass |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
79 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
80 # No luck |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
81 ## 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
|
82 return [] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
83 try: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
84 fp = open(fullname, 'r') |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
85 lines = fp.readlines() |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
86 fp.close() |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
87 except IOError, msg: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
88 ## 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
|
89 return [] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
90 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
|
91 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
|
92 return lines |
