Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c8656c

Browse files
committed
Python: CG trace: Handle BUILD_TUPLE
1 parent 0d05d96 commit 8c8656c

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def foo():
2+
print("foo")
3+
4+
5+
def bar():
6+
print("bar")
7+
8+
9+
(foo, bar)[0]()

python/tools/recorded-call-graph-metrics/src/cg_trace/bytecode_reconstructor.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ def __str__(self):
5454
return f"{self.object}[{self.key}]"
5555

5656

57+
@dataclasses.dataclass(frozen=True, eq=True, order=True)
58+
class BytecodeTuple(BytecodeExpr):
59+
elements: List[BytecodeExpr]
60+
61+
def __str__(self):
62+
elements_formatted = (
63+
", ".join(str(e) for e in self.elements)
64+
if len(self.elements) > 1
65+
else f"{self.elements[0]},"
66+
)
67+
return f"({elements_formatted})"
68+
69+
5770
@dataclasses.dataclass(frozen=True, eq=True, order=True)
5871
class BytecodeCall(BytecodeExpr):
5972
function: BytecodeExpr
@@ -161,6 +174,14 @@ def expr_from_instruction(instructions: List[Instruction], index: int) -> Byteco
161174
obj_expr = expr_that_added_elem_to_stack(instructions, index - 1, 1)
162175
return BytecodeSubscript(key=key_expr, object=obj_expr)
163176

177+
elif inst.opname in ["BUILD_TUPLE"]:
178+
elements = []
179+
for i in range(inst.arg):
180+
element_expr = expr_that_added_elem_to_stack(instructions, index - 1, i)
181+
elements.append(element_expr)
182+
elements.reverse()
183+
return BytecodeTuple(elements=elements)
184+
164185
# https://docs.python.org/3/library/dis.html#opcode-CALL_FUNCTION
165186
elif inst.opname in [
166187
"CALL_FUNCTION",

0 commit comments

Comments
 (0)