summaryrefslogtreecommitdiffstats
path: root/Lib/linecache.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1992-03-27 15:12:43 (GMT)
committerGuido van Rossum <guido@python.org>1992-03-27 15:12:43 (GMT)
commitc341c62e81e3f869b9b75fb6b1213a04388406b7 (patch)
tree21fded4a41e479eee832a576817576e5c884b724 /Lib/linecache.py
parentb5a40dcadebb5dfd01d2a9c3a885124af31adf56 (diff)
downloadcpython-c341c62e81e3f869b9b75fb6b1213a04388406b7.zip
cpython-c341c62e81e3f869b9b75fb6b1213a04388406b7.tar.gz
cpython-c341c62e81e3f869b9b75fb6b1213a04388406b7.tar.bz2
Search through the module search path.
Add a warning to the top that this is the case.
Diffstat (limited to 'Lib/linecache.py')
-rw-r--r--Lib/linecache.py41
1 files changed, 30 insertions, 11 deletions
diff --git a/Lib/linecache.py b/Lib/linecache.py
index 4355f19..b981869 100644
--- a/Lib/linecache.py
+++ b/Lib/linecache.py
@@ -1,5 +1,9 @@
# Cache lines from files.
+# This is intended to read lines from modules imported -- hence if a filename
+# is not found, it will look down the module search path for a file by
+# that name.
+import sys
import os
from stat import *
@@ -34,12 +38,13 @@ def getlines(filename):
# Discard cache entries that are out of date.
-# (This is not checked upon each call
+# (This is not checked upon each call!)
def checkcache():
for filename in cache.keys():
- size, mtime, lines = cache[filename]
- try: stat = os.stat(filename)
+ size, mtime, lines, fullname = cache[filename]
+ try:
+ stat = os.stat(fullname)
except os.error:
del cache[filename]
continue
@@ -52,20 +57,34 @@ def checkcache():
# and return an empty list.
def updatecache(filename):
- try: del cache[filename]
- except KeyError: pass
- try: stat = os.stat(filename)
+ if cache.has_key(filename):
+ del cache[filename]
+ if filename[0] + filename[-1] == '<>':
+ return []
+ fullname = filename
+ try:
+ stat = os.stat(fullname)
except os.error, msg:
- if filename[0] + filename[-1] <> '<>':
+ # Try looking through the module search path
+ basename = os.path.split(filename)[1]
+ for dirname in sys.path:
+ fullname = os.path.join(dirname, basename)
+ try:
+ stat = os.stat(fullname)
+ break
+ except os.error:
+ pass
+ else:
+ # No luck
print '*** Cannot stat', filename, ':', msg
- return []
+ return []
try:
- fp = open(filename, 'r')
+ fp = open(fullname, 'r')
lines = fp.readlines()
fp.close()
except IOError, msg:
- print '*** Cannot open', filename, ':', msg
+ print '*** Cannot open', fullname, ':', msg
return []
size, mtime = stat[ST_SIZE], stat[ST_MTIME]
- cache[filename] = size, mtime, lines
+ cache[filename] = size, mtime, lines, fullname
return lines