10
10
11
11
12
12
class TryExceptInfo (object ):
13
-
14
13
def __init__ (self , try_line , ignore = False ):
15
14
"""
16
15
:param try_line:
@@ -54,7 +53,6 @@ def __str__(self):
54
53
55
54
56
55
class ReturnInfo (object ):
57
-
58
56
def __init__ (self , return_line ):
59
57
self .return_line = return_line
60
58
@@ -119,7 +117,6 @@ def collect_return_info(co, use_func_first_line=False):
119
117
if sys .version_info [:2 ] <= (3 , 9 ):
120
118
121
119
class _TargetInfo (object ):
122
-
123
120
def __init__ (self , except_end_instruction , jump_if_not_exc_instruction = None ):
124
121
self .except_end_instruction = except_end_instruction
125
122
self .jump_if_not_exc_instruction = jump_if_not_exc_instruction
@@ -138,7 +135,7 @@ def __str__(self):
138
135
139
136
def _get_except_target_info (instructions , exception_end_instruction_index , offset_to_instruction_idx ):
140
137
next_3 = [
141
- j_instruction .opname for j_instruction in instructions [exception_end_instruction_index : exception_end_instruction_index + 3 ]
138
+ j_instruction .opname for j_instruction in instructions [exception_end_instruction_index : exception_end_instruction_index + 3 ]
142
139
]
143
140
# print('next_3:', [(j_instruction.opname, j_instruction.argval) for j_instruction in instructions[exception_end_instruction_index:exception_end_instruction_index + 3]])
144
141
if next_3 == ["POP_TOP" , "POP_TOP" , "POP_TOP" ]: # try..except without checking exception.
@@ -178,7 +175,7 @@ def _get_except_target_info(instructions, exception_end_instruction_index, offse
178
175
return _TargetInfo (except_end_instruction )
179
176
180
177
elif next_3 and next_3 [0 ] == "DUP_TOP" : # try..except AssertionError.
181
- iter_in = instructions [exception_end_instruction_index + 1 :]
178
+ iter_in = instructions [exception_end_instruction_index + 1 :]
182
179
for j , jump_if_not_exc_instruction in enumerate (iter_in ):
183
180
if jump_if_not_exc_instruction .opname == "JUMP_IF_NOT_EXC_MATCH" :
184
181
# Python 3.9
@@ -213,7 +210,7 @@ def collect_try_except_info(co, use_func_first_line=False):
213
210
214
211
try_except_info_lst = []
215
212
216
- op_offset_to_line = dict ((k , v ) for (k , v ) in dis .findlinestarts (co ) if v is not None )
213
+ op_offset_to_line = dict ((k , v ) for (k , v ) in dis .findlinestarts (co ) if v is not None )
217
214
218
215
offset_to_instruction_idx = {}
219
216
@@ -262,7 +259,7 @@ def collect_try_except_info(co, use_func_first_line=False):
262
259
try_except_info .except_end_line = _get_line (op_offset_to_line , except_end_instruction .offset , firstlineno , search = True )
263
260
try_except_info_lst .append (try_except_info )
264
261
265
- for raise_instruction in instructions [i : offset_to_instruction_idx [try_except_info .except_end_bytecode_offset ]]:
262
+ for raise_instruction in instructions [i : offset_to_instruction_idx [try_except_info .except_end_bytecode_offset ]]:
266
263
if raise_instruction .opname == "RAISE_VARARGS" :
267
264
if raise_instruction .argval == 0 :
268
265
try_except_info .raise_lines_in_except .append (
@@ -274,7 +271,6 @@ def collect_try_except_info(co, use_func_first_line=False):
274
271
elif sys .version_info [:2 ] == (3 , 10 ):
275
272
276
273
class _TargetInfo (object ):
277
-
278
274
def __init__ (self , except_end_instruction , jump_if_not_exc_instruction = None ):
279
275
self .except_end_instruction = except_end_instruction
280
276
self .jump_if_not_exc_instruction = jump_if_not_exc_instruction
@@ -293,21 +289,21 @@ def __str__(self):
293
289
294
290
def _get_except_target_info (instructions , exception_end_instruction_index , offset_to_instruction_idx ):
295
291
next_3 = [
296
- j_instruction .opname for j_instruction in instructions [exception_end_instruction_index : exception_end_instruction_index + 3 ]
292
+ j_instruction .opname for j_instruction in instructions [exception_end_instruction_index : exception_end_instruction_index + 3 ]
297
293
]
298
294
# print('next_3:', [(j_instruction.opname, j_instruction.argval) for j_instruction in instructions[exception_end_instruction_index:exception_end_instruction_index + 3]])
299
295
if next_3 == ["POP_TOP" , "POP_TOP" , "POP_TOP" ]: # try..except without checking exception.
300
296
# Previously there was a jump which was able to point where the exception would end. This
301
297
# is no longer true, now a bare except doesn't really have any indication in the bytecode
302
298
# where the end would be expected if the exception wasn't raised, so, we just blindly
303
299
# search for a POP_EXCEPT from the current position.
304
- for pop_except_instruction in instructions [exception_end_instruction_index + 3 :]:
300
+ for pop_except_instruction in instructions [exception_end_instruction_index + 3 :]:
305
301
if pop_except_instruction .opname == "POP_EXCEPT" :
306
302
except_end_instruction = pop_except_instruction
307
303
return _TargetInfo (except_end_instruction )
308
304
309
305
elif next_3 and next_3 [0 ] == "DUP_TOP" : # try..except AssertionError.
310
- iter_in = instructions [exception_end_instruction_index + 1 :]
306
+ iter_in = instructions [exception_end_instruction_index + 1 :]
311
307
for jump_if_not_exc_instruction in iter_in :
312
308
if jump_if_not_exc_instruction .opname == "JUMP_IF_NOT_EXC_MATCH" :
313
309
# Python 3.9
@@ -332,7 +328,7 @@ def collect_try_except_info(co, use_func_first_line=False):
332
328
333
329
try_except_info_lst = []
334
330
335
- op_offset_to_line = dict ((k , v ) for (k , v ) in dis .findlinestarts (co ) if v is not None )
331
+ op_offset_to_line = dict ((k , v ) for (k , v ) in dis .findlinestarts (co ) if v is not None )
336
332
337
333
offset_to_instruction_idx = {}
338
334
@@ -386,15 +382,15 @@ def collect_try_except_info(co, use_func_first_line=False):
386
382
except_end_line = - 1
387
383
start_i = offset_to_instruction_idx [try_except_info .except_bytecode_offset ]
388
384
end_i = offset_to_instruction_idx [except_end_instruction .offset ]
389
- for instruction in instructions [start_i : end_i + 1 ]:
385
+ for instruction in instructions [start_i : end_i + 1 ]:
390
386
found_at_line = op_offset_to_line .get (instruction .offset )
391
387
if found_at_line is not None and found_at_line > except_end_line :
392
388
except_end_line = found_at_line
393
389
try_except_info .except_end_line = except_end_line - firstlineno
394
390
395
391
try_except_info_lst .append (try_except_info )
396
392
397
- for raise_instruction in instructions [i : offset_to_instruction_idx [try_except_info .except_end_bytecode_offset ]]:
393
+ for raise_instruction in instructions [i : offset_to_instruction_idx [try_except_info .except_end_bytecode_offset ]]:
398
394
if raise_instruction .opname == "RAISE_VARARGS" :
399
395
if raise_instruction .argval == 0 :
400
396
try_except_info .raise_lines_in_except .append (
@@ -413,11 +409,11 @@ def collect_try_except_info(co, use_func_first_line=False):
413
409
"""
414
410
return []
415
411
412
+
416
413
import ast as ast_module
417
414
418
415
419
416
class _Visitor (ast_module .NodeVisitor ):
420
-
421
417
def __init__ (self ):
422
418
self .try_except_infos = []
423
419
self ._stack = []
@@ -489,7 +485,6 @@ def collect_try_except_info_from_contents(contents, filename="<unknown>"):
489
485
490
486
491
487
class _MsgPart (object ):
492
-
493
488
def __init__ (self , line , tok ):
494
489
assert line >= 0
495
490
self .line = line
@@ -528,13 +523,12 @@ def add_to_line_to_contents(cls, obj, line_to_contents, line=None):
528
523
529
524
530
525
class _Disassembler (object ):
531
-
532
526
def __init__ (self , co , firstlineno , level = 0 ):
533
527
self .co = co
534
528
self .firstlineno = firstlineno
535
529
self .level = level
536
530
self .instructions = list (iter_instructions (co ))
537
- op_offset_to_line = self .op_offset_to_line = dict ((k , v ) for (k , v ) in dis .findlinestarts (co ) if v is not None )
531
+ op_offset_to_line = self .op_offset_to_line = dict ((k , v ) for (k , v ) in dis .findlinestarts (co ) if v is not None )
538
532
539
533
# Update offsets so that all offsets have the line index (and update it based on
540
534
# the passed firstlineno).
@@ -671,7 +665,7 @@ def _lookahead(self):
671
665
else :
672
666
return None # This is odd
673
667
674
- del self .instructions [delta : delta + next_instruction .argval + 1 ] # +1 = BUILD_TUPLE
668
+ del self .instructions [delta : delta + next_instruction .argval + 1 ] # +1 = BUILD_TUPLE
675
669
676
670
found = iter (found [delta :])
677
671
0 commit comments