@@ -105,6 +105,15 @@ def find_function(funcname, filename):
105
105
fp .close ()
106
106
return answer
107
107
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
108
117
109
118
# Interaction prompt line will separate file and call info from code
110
119
# text using value of line_prefix string. A newline and arrow may
@@ -1048,7 +1057,7 @@ def do_longlist(self, arg):
1048
1057
filename = self .curframe .f_code .co_filename
1049
1058
breaklist = self .get_file_breaks (filename )
1050
1059
try :
1051
- lines , lineno = inspect . getsourcelines (self .curframe )
1060
+ lines , lineno = getsourcelines (self .curframe )
1052
1061
except IOError as err :
1053
1062
self .error (err )
1054
1063
return
@@ -1064,7 +1073,7 @@ def do_source(self, arg):
1064
1073
except :
1065
1074
return
1066
1075
try :
1067
- lines , lineno = inspect . getsourcelines (obj )
1076
+ lines , lineno = getsourcelines (obj )
1068
1077
except (IOError , TypeError ) as err :
1069
1078
self .error (err )
1070
1079
return
0 commit comments