Python: CG trace: Handle BUILD_LIST

This commit is contained in:
Rasmus Wriedt Larsen
2020-07-21 23:08:33 +02:00
parent 8c8656ccca
commit 9bff615fad
2 changed files with 25 additions and 2 deletions

View File

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

View File

@@ -67,6 +67,19 @@ class BytecodeTuple(BytecodeExpr):
return f"({elements_formatted})"
@dataclasses.dataclass(frozen=True, eq=True, order=True)
class BytecodeList(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
@@ -174,13 +187,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"]:
elif inst.opname in ["BUILD_TUPLE", "BUILD_LIST"]:
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)
klass = {"BUILD_TUPLE": BytecodeTuple, "BUILD_LIST": BytecodeList}[inst.opname]
return klass(elements=elements)
# https://docs.python.org/3/library/dis.html#opcode-CALL_FUNCTION
elif inst.opname in [