summaryrefslogtreecommitdiffstats
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorNeil Schemenauer <nascheme@enme.ucalgary.ca>2002-03-23 23:51:04 (GMT)
committerNeil Schemenauer <nascheme@enme.ucalgary.ca>2002-03-23 23:51:04 (GMT)
commitf06f8530f1d2b844d560d7420998780637f3184b (patch)
tree07d09a032c7f6ad214b86f85e6555d8b3d519a0c /Lib/inspect.py
parentdbab3e3178d8819a839ad08571f995376edcba00 (diff)
downloadcpython-f06f8530f1d2b844d560d7420998780637f3184b.zip
cpython-f06f8530f1d2b844d560d7420998780637f3184b.tar.gz
cpython-f06f8530f1d2b844d560d7420998780637f3184b.tar.bz2
Use linecache for loading source code. Closes SF patch 490374.
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py12
1 files changed, 5 insertions, 7 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 9e2d23d..e4f6b57 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -27,7 +27,7 @@ Here are some of the useful functions provided by this module:
__author__ = 'Ka-Ping Yee <ping@lfw.org>'
__date__ = '1 Jan 2001'
-import sys, os, types, string, re, dis, imp, tokenize
+import sys, os, types, string, re, dis, imp, tokenize, linecache
# ----------------------------------------------------------- type-checking
def ismodule(object):
@@ -381,12 +381,10 @@ def findsource(object):
or code object. The source code is returned as a list of all the lines
in the file and the line number indexes a line in that list. An IOError
is raised if the source code cannot be retrieved."""
- try:
- file = open(getsourcefile(object))
- except (TypeError, IOError):
+ file = getsourcefile(object) or getfile(object)
+ lines = linecache.getlines(file)
+ if not lines:
raise IOError, 'could not get source code'
- lines = file.readlines()
- file.close()
if ismodule(object):
return lines, 0
@@ -706,7 +704,7 @@ def getframeinfo(frame, context=1):
if not isframe(frame):
raise TypeError, 'arg is not a frame or traceback object'
- filename = getsourcefile(frame)
+ filename = getsourcefile(frame) or getfile(frame)
lineno = getlineno(frame)
if context > 0:
start = lineno - 1 - context//2