@@ -97,17 +97,47 @@ class Restart(Exception):
97
97
__all__ = ["run" , "pm" , "Pdb" , "runeval" , "runctx" , "runcall" , "set_trace" ,
98
98
"post_mortem" , "help" ]
99
99
100
+
101
+ def find_first_executable_line (code ):
102
+ """ Try to find the first executable line of the code object.
103
+
104
+ Equivalently, find the line number of the instruction that's
105
+ after RESUME
106
+
107
+ Return code.co_firstlineno if no executable line is found.
108
+ """
109
+ prev = None
110
+ for instr in dis .get_instructions (code ):
111
+ if prev is not None and prev .opname == 'RESUME' :
112
+ if instr .positions .lineno is not None :
113
+ return instr .positions .lineno
114
+ return code .co_firstlineno
115
+ prev = instr
116
+ return code .co_firstlineno
117
+
100
118
def find_function (funcname , filename ):
101
119
cre = re .compile (r'def\s+%s\s*[(]' % re .escape (funcname ))
102
120
try :
103
121
fp = tokenize .open (filename )
104
122
except OSError :
105
123
return None
124
+ funcdef = ""
125
+ funcstart = None
106
126
# consumer of this info expects the first line to be 1
107
127
with fp :
108
128
for lineno , line in enumerate (fp , start = 1 ):
109
129
if cre .match (line ):
110
- return funcname , filename , lineno
130
+ funcstart , funcdef = lineno , line
131
+ elif funcdef :
132
+ funcdef += line
133
+
134
+ if funcdef :
135
+ try :
136
+ funccode = compile (funcdef , filename , 'exec' ).co_consts [0 ]
137
+ except SyntaxError :
138
+ continue
139
+ lineno_offset = find_first_executable_line (funccode )
140
+ return funcname , filename , funcstart + lineno_offset - 1
111
141
return None
112
142
113
143
def lasti2lineno (code , lasti ):
@@ -975,7 +1005,7 @@ def do_break(self, arg, temporary = 0):
975
1005
#use co_name to identify the bkpt (function names
976
1006
#could be aliased, but co_name is invariant)
977
1007
funcname = code .co_name
978
- lineno = self . _find_first_executable_line (code )
1008
+ lineno = find_first_executable_line (code )
979
1009
filename = code .co_filename
980
1010
except :
981
1011
# last thing to try
@@ -1078,23 +1108,6 @@ def checkline(self, filename, lineno):
1078
1108
return 0
1079
1109
return lineno
1080
1110
1081
- def _find_first_executable_line (self , code ):
1082
- """ Try to find the first executable line of the code object.
1083
-
1084
- Equivalently, find the line number of the instruction that's
1085
- after RESUME
1086
-
1087
- Return code.co_firstlineno if no executable line is found.
1088
- """
1089
- prev = None
1090
- for instr in dis .get_instructions (code ):
1091
- if prev is not None and prev .opname == 'RESUME' :
1092
- if instr .positions .lineno is not None :
1093
- return instr .positions .lineno
1094
- return code .co_firstlineno
1095
- prev = instr
1096
- return code .co_firstlineno
1097
-
1098
1111
def do_enable (self , arg ):
1099
1112
"""enable bpnumber [bpnumber ...]
1100
1113
0 commit comments