1919
2020import re
2121
22- from opcode import HAVE_ARGUMENT , opname
22+ from opcode import opname
2323from typing import Optional
2424from xdis import PYTHON_VERSION_TRIPLE , get_opcode_module
2525
@@ -50,10 +50,8 @@ def next_opcode(code, offset):
5050 n = len (code )
5151 while offset < n :
5252 op = code [offset ]
53- offset += 1
54- if op >= HAVE_ARGUMENT :
55- offset += 2
56- pass
53+ yield op , offset
54+ offset = offset + 2
5755 yield op , offset
5856 pass
5957 yield - 100 , - 1000
@@ -64,37 +62,40 @@ def next_linestart(co, offset: int, count=1) -> int:
6462 code = co .co_code
6563
6664 linestarts = dict (opcode_module .findlinestarts (co ))
67- # n = len(code)
65+ n = len (code )
6866 # contains_cond_jump = False
69- for op , offset in next_opcode (code , offset ):
67+ while offset < len (code ):
7068 if offset in linestarts :
7169 count -= 1
7270 if 0 == count :
7371 return linestarts [offset ]
7472 pass
75- pass
73+ offset += 2
7674
7775 return - 1000
7876
7977
80- def stmt_contains_opcode (co , lineno , query_opcode ) -> bool :
78+ def stmt_contains_opcode (co , lineno , query_opname ) -> bool :
8179 linestarts = dict (opcode_module .findlinestarts (co ))
8280 code = co .co_code
8381 found_start = False
8482 offset = 0
83+ start_line = None
8584 for offset , start_line in list (linestarts .items ()):
8685 if start_line == lineno :
86+ print ("=" * 30 )
8787 found_start = True
8888 break
8989 pass
9090 if not found_start :
9191 return False
9292 for op , offset in next_opcode (code , offset ):
93- if - 1000 == offset or linestarts .get (offset ):
93+ linestart = linestarts .get (offset )
94+ if - 1000 == offset or linestart and linestart != start_line :
9495 return False
95- opcode = opname [op ]
96- # debug: print opcode
97- if query_opcode == opcode :
96+ op_name = opname [op ]
97+ # debug: print opname
98+ if query_opname == op_name :
9899 return True
99100 pass
100101 return False
@@ -104,7 +105,7 @@ def stmt_contains_opcode(co, lineno, query_opcode) -> bool:
104105_re_def = re .compile (_re_def_str )
105106
106107
107- def is_def_stmt (line , frame ):
108+ def is_def_stmt (line : Optional [ str ] , frame ) -> bool :
108109 """Return True if we are looking at a def statement"""
109110 # Should really also check that operand of 'LOAD_CONST' is a code object
110111 return (
0 commit comments