Skip to content

Commit 5ed2b5a

Browse files
committed
Fix source finding if the given frame is a module-level frame.
1 parent e38de85 commit 5ed2b5a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Lib/pdb.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ def find_function(funcname, filename):
105105
fp.close()
106106
return answer
107107

108+
def getsourcelines(obj):
109+
lines, lineno = inspect.findsource(obj)
110+
if inspect.isframe(obj) and lineno == 0 and \
111+
obj.f_globals is obj.f_locals:
112+
# must be a module frame: do not try to cut a block out of it
113+
return lines, 0
114+
elif inspect.ismodule(obj):
115+
return lines, 0
116+
return inspect.getblock(lines[lineno:]), lineno+1
108117

109118
# Interaction prompt line will separate file and call info from code
110119
# text using value of line_prefix string. A newline and arrow may
@@ -1048,7 +1057,7 @@ def do_longlist(self, arg):
10481057
filename = self.curframe.f_code.co_filename
10491058
breaklist = self.get_file_breaks(filename)
10501059
try:
1051-
lines, lineno = inspect.getsourcelines(self.curframe)
1060+
lines, lineno = getsourcelines(self.curframe)
10521061
except IOError as err:
10531062
self.error(err)
10541063
return
@@ -1064,7 +1073,7 @@ def do_source(self, arg):
10641073
except:
10651074
return
10661075
try:
1067-
lines, lineno = inspect.getsourcelines(obj)
1076+
lines, lineno = getsourcelines(obj)
10681077
except (IOError, TypeError) as err:
10691078
self.error(err)
10701079
return

0 commit comments

Comments
 (0)