Python: CG trace: Handle BUILD_TUPLE

This commit is contained in:
Rasmus Wriedt Larsen
2020-07-21 23:05:49 +02:00
parent 0d05d96b50
commit 8c8656ccca
2 changed files with 30 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
def foo():
print("foo")
def bar():
print("bar")
(foo, bar)[0]()

View File

@@ -54,6 +54,19 @@ class BytecodeSubscript(BytecodeExpr):
return f"{self.object}[{self.key}]"
@dataclasses.dataclass(frozen=True, eq=True, order=True)
class BytecodeTuple(BytecodeExpr):
elements: List[BytecodeExpr]
def __str__(self):
elements_formatted = (
", ".join(str(e) for e in self.elements)
if len(self.elements) > 1
else f"{self.elements[0]},"
)
return f"({elements_formatted})"
@dataclasses.dataclass(frozen=True, eq=True, order=True)
class BytecodeCall(BytecodeExpr):
function: BytecodeExpr
@@ -161,6 +174,14 @@ def expr_from_instruction(instructions: List[Instruction], index: int) -> Byteco
obj_expr = expr_that_added_elem_to_stack(instructions, index - 1, 1)
return BytecodeSubscript(key=key_expr, object=obj_expr)
elif inst.opname in ["BUILD_TUPLE"]:
elements = []
for i in range(inst.arg):
element_expr = expr_that_added_elem_to_stack(instructions, index - 1, i)
elements.append(element_expr)
elements.reverse()
return BytecodeTuple(elements=elements)
# https://docs.python.org/3/library/dis.html#opcode-CALL_FUNCTION
elif inst.opname in [
"CALL_FUNCTION",