55from types import FrameType
66from typing import Any , List
77
8+ from cg_trace .settings import DEBUG , FAIL_ON_UNKNOWN_BYTECODE
89from cg_trace .utils import better_compare_for_dataclass
910
1011LOGGER = logging .getLogger (__name__ )
@@ -155,23 +156,26 @@ def expr_that_added_elem_to_stack(
155156 immediately. (since correctly process the bytecode when faced with jumps is not as
156157 straight forward).
157158 """
158- LOGGER .debug (
159- f"find_inst_that_added_elem_to_stack start_index={ start_index } stack_pos={ stack_pos } "
160- )
159+ if DEBUG :
160+ LOGGER .debug (
161+ f"find_inst_that_added_elem_to_stack start_index={ start_index } stack_pos={ stack_pos } "
162+ )
161163 assert stack_pos >= 0
162164 for inst in reversed (instructions [: start_index + 1 ]):
163165 # Return immediately if faced with a jump
164166 if inst .opcode in dis .hasjabs or inst .opcode in dis .hasjrel :
165167 return SomethingInvolvingScaryBytecodeJump (inst .opname )
166168
167169 if stack_pos == 0 :
168- LOGGER .debug (f"Found it: { inst } " )
170+ if DEBUG :
171+ LOGGER .debug (f"Found it: { inst } " )
169172 found_index = instructions .index (inst )
170173 break
171174 old = stack_pos
172175 stack_pos -= dis .stack_effect (inst .opcode , inst .arg )
173176 new = stack_pos
174- LOGGER .debug (f"Skipping ({ old } -> { new } ) { inst } " )
177+ if DEBUG :
178+ LOGGER .debug (f"Skipping ({ old } -> { new } ) { inst } " )
175179 else :
176180 raise Exception ("inst_index_for_stack_diff failed" )
177181
@@ -181,7 +185,8 @@ def expr_that_added_elem_to_stack(
181185def expr_from_instruction (instructions : List [Instruction ], index : int ) -> BytecodeExpr :
182186 inst = instructions [index ]
183187
184- LOGGER .debug (f"expr_from_instruction: { inst } index={ index } " )
188+ if DEBUG :
189+ LOGGER .debug (f"expr_from_instruction: { inst } index={ index } " )
185190
186191 if inst .opname in ["LOAD_GLOBAL" , "LOAD_FAST" , "LOAD_NAME" , "LOAD_DEREF" ]:
187192 return BytecodeVariableName (inst .argval )
@@ -247,24 +252,23 @@ def expr_from_instruction(instructions: List[Instruction], index: int) -> Byteco
247252 # - LOAD_BUILD_CLASS: Called when constructing a class.
248253 # - IMPORT_NAME: Observed to result in a call to filename='<frozen
249254 # importlib._bootstrap>', linenum=389, funcname='parent'
250- if inst .opname not in ["LOAD_BUILD_CLASS" , "IMPORT_NAME" ] + WITH_OPNAMES :
251- LOGGER .warning (
252- f"Don't know how to handle this type of instruction: { inst .opname } "
253- )
254- # Uncomment to stop execution when encountering non-ignored unknown instruction
255- # class MyBytecodeException(BaseException):
256- # pass
257- #
258- # raise MyBytecodeException()
255+ if FAIL_ON_UNKNOWN_BYTECODE :
256+ if inst .opname not in ["LOAD_BUILD_CLASS" , "IMPORT_NAME" ] + WITH_OPNAMES :
257+ LOGGER .warning (
258+ f"Don't know how to handle this type of instruction: { inst .opname } "
259+ )
260+ raise BaseException ()
261+
259262 return BytecodeUnknown (inst .opname )
260263
261264
262265def expr_from_frame (frame : FrameType ) -> BytecodeExpr :
263266 bytecode = dis .Bytecode (frame .f_code , current_offset = frame .f_lasti )
264267
265- LOGGER .debug (
266- f"{ frame .f_code .co_filename } :{ frame .f_lineno } : bytecode: \n { bytecode .dis ()} "
267- )
268+ if DEBUG :
269+ LOGGER .debug (
270+ f"{ frame .f_code .co_filename } :{ frame .f_lineno } : bytecode: \n { bytecode .dis ()} "
271+ )
268272
269273 instructions = list (iter (bytecode ))
270274 last_instruction_index = [inst .offset for inst in instructions ].index (frame .f_lasti )
0 commit comments