Mercurial > cpython-lru_cache-weakref
annotate Lib/linecache.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | c5cb3a53933c |
| children | b6f52c1ef6da |
| rev | line source |
|---|---|
| 566 | 1 # Cache lines from files. |
|
722
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
2 # This is intended to read lines from modules imported -- hence if a filename |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
3 # is not found, it will look down the module search path for a file by |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
4 # that name. |
| 566 | 5 |
|
722
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
6 import sys |
| 566 | 7 import os |
| 8 from stat import * | |
| 9 | |
| 10 def getline(filename, lineno): | |
| 11 lines = getlines(filename) | |
| 12 if 1 <= lineno <= len(lines): | |
| 13 return lines[lineno-1] | |
| 14 else: | |
| 15 return '' | |
| 16 | |
| 17 | |
| 18 # The cache | |
| 19 | |
| 20 cache = {} # The cache | |
| 21 | |
| 22 | |
| 23 # Clear the cache entirely | |
| 24 | |
| 25 def clearcache(): | |
| 26 global cache | |
| 27 cache = {} | |
| 28 | |
| 29 | |
| 30 # Get the lines for a file from the cache. | |
| 31 # Update the cache if it doesn't contain an entry for this file already. | |
| 32 | |
| 33 def getlines(filename): | |
| 34 if cache.has_key(filename): | |
| 35 return cache[filename][2] | |
| 36 else: | |
| 37 return updatecache(filename) | |
| 38 | |
| 39 | |
| 40 # Discard cache entries that are out of date. | |
|
722
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
41 # (This is not checked upon each call!) |
| 566 | 42 |
| 43 def checkcache(): | |
| 44 for filename in cache.keys(): | |
|
722
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
45 size, mtime, lines, fullname = cache[filename] |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
46 try: |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
47 stat = os.stat(fullname) |
| 566 | 48 except os.error: |
| 49 del cache[filename] | |
| 50 continue | |
| 51 if size <> stat[ST_SIZE] or mtime <> stat[ST_MTIME]: | |
| 52 del cache[filename] | |
| 53 | |
| 54 | |
| 55 # Update a cache entry and return its list of lines. | |
| 56 # If something's wrong, print a message, discard the cache entry, | |
| 57 # and return an empty list. | |
| 58 | |
| 59 def updatecache(filename): | |
|
722
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
60 if cache.has_key(filename): |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
61 del cache[filename] |
|
1810
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
722
diff
changeset
|
62 if not filename or filename[0] + filename[-1] == '<>': |
|
722
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
63 return [] |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
64 fullname = filename |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
65 try: |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
66 stat = os.stat(fullname) |
| 566 | 67 except os.error, msg: |
|
722
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
68 # Try looking through the module search path |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
69 basename = os.path.split(filename)[1] |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
70 for dirname in sys.path: |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
71 fullname = os.path.join(dirname, basename) |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
72 try: |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
73 stat = os.stat(fullname) |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
74 break |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
75 except os.error: |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
76 pass |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
77 else: |
|
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
78 # No luck |
|
2954
c5cb3a53933c
don't print Cannot open/stat messages
Guido van Rossum <guido@python.org>
parents:
1810
diff
changeset
|
79 ## print '*** Cannot stat', filename, ':', msg |
|
722
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
80 return [] |
| 566 | 81 try: |
|
722
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
82 fp = open(fullname, 'r') |
| 566 | 83 lines = fp.readlines() |
| 84 fp.close() | |
| 85 except IOError, msg: | |
|
2954
c5cb3a53933c
don't print Cannot open/stat messages
Guido van Rossum <guido@python.org>
parents:
1810
diff
changeset
|
86 ## print '*** Cannot open', fullname, ':', msg |
| 566 | 87 return [] |
| 88 size, mtime = stat[ST_SIZE], stat[ST_MTIME] | |
|
722
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
89 cache[filename] = size, mtime, lines, fullname |
| 566 | 90 return lines |
