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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
722
57e94eba2484 Search through the module search path.
Guido van Rossum <guido@python.org>
parents: 566
diff changeset
6 import sys
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 import os
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 from stat import *
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 def getline(filename, lineno):
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 lines = getlines(filename)
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 if 1 <= lineno <= len(lines):
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 return lines[lineno-1]
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 else:
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 return ''
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # The cache
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 cache = {} # The cache
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # Clear the cache entirely
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 def clearcache():
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 global cache
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 cache = {}
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # Get the lines for a file from the cache.
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # Update the cache if it doesn't contain an entry for this file already.
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 def getlines(filename):
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 if cache.has_key(filename):
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 return cache[filename][2]
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 else:
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 return updatecache(filename)
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 def checkcache():
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 except os.error:
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 del cache[filename]
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 continue
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 if size <> stat[ST_SIZE] or mtime <> stat[ST_MTIME]:
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 del cache[filename]
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 # Update a cache entry and return its list of lines.
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 # If something's wrong, print a message, discard the cache entry,
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 # and return an empty list.
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 lines = fp.readlines()
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 fp.close()
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 return []
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 return lines