File tree Expand file tree Collapse file tree
python/tools/recorded-call-graph-metrics Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class Foo :
2+ def __init__ (self ):
3+ self .list = []
4+
5+ def func (self , kwargs = None , result_callback = None ):
6+ self .list .append ((kwargs or {}, result_callback ))
7+
8+
9+ foo = Foo ()
10+ foo .func ()
11+
12+ """
13+ Has problematic bytecode, since to find out what method is called from instruction 16, we need
14+ to traverse the JUMP_IF_TRUE_OR_POP which requires some more sophistication.
15+
16+ Disassembly of <code object func at 0x7f98f64ee030, file "example/problem-1.py", line 5>:
17+ 6 0 LOAD_FAST 0 (self)
18+ 2 LOAD_ATTR 0 (list)
19+ 4 LOAD_METHOD 1 (append)
20+ 6 LOAD_FAST 1 (kwargs)
21+ 8 JUMP_IF_TRUE_OR_POP 12
22+ 10 BUILD_MAP 0
23+ >> 12 LOAD_FAST 2 (result_callback)
24+ 14 BUILD_TUPLE 2
25+ 16 CALL_METHOD 1
26+ """
Original file line number Diff line number Diff line change @@ -71,6 +71,14 @@ def __str__(self):
7171 return f"<MAKE_FUNCTION>(qualified_name={ self .qualified_name } )>"
7272
7373
74+ @dataclasses .dataclass (frozen = True , eq = True , order = True )
75+ class SomethingInvolvingScaryBytecodeJump (BytecodeExpr ):
76+ opname : str
77+
78+ def __str__ (self ):
79+ return "<SomethingInvolvingScaryBytecodeJump>"
80+
81+
7482def expr_that_added_elem_to_stack (
7583 instructions : List [Instruction ], start_index : int , stack_pos : int
7684):
@@ -95,10 +103,18 @@ def expr_that_added_elem_to_stack(
95103 It is assumed that if `stack_pos == 0` then the instruction you are looking for is
96104 the one at `instructions[start_index]`. This might not hold, in case of using `NOP`
97105 instructions.
106+
107+ If any jump instruction is found, `SomethingInvolvingScaryBytecodeJump` is returned
108+ immediately. (since correctly process the bytecode when faced with jumps is not as
109+ straight forward).
98110 """
99111 LOGGER .debug (f"find_inst_that_added_elem_to_stack { start_index = } { stack_pos = } " )
100112 assert stack_pos >= 0
101113 for inst in reversed (instructions [: start_index + 1 ]):
114+ # Return immediately if faced with a jump
115+ if inst .opcode in dis .hasjabs or inst .opcode in dis .hasjrel :
116+ return SomethingInvolvingScaryBytecodeJump (inst .opname )
117+
102118 if stack_pos == 0 :
103119 LOGGER .debug (f"Found it: { inst } " )
104120 found_index = instructions .index (inst )
You can’t perform that action at this time.
0 commit comments