Skip to content

Commit

Permalink
core: fix handling of tracing for RETURN_CONST
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthieuDartiailh committed Oct 31, 2023
1 parent c6b0d00 commit 9c2fbb0
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions enaml/core/code_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ def call_tracer_return_value(tracer_op: str, Instr=bc.Instr):
Instr("POP_TOP"), # obj
]

def call_tracer_return_const(tracer_op: str, const, Instr=bc.Instr):
return [
Instr("PUSH_NULL"), # obj -> null
Instr(tracer_op, '_[tracer]'), # obj -> null -> tracer
Instr("LOAD_ATTR", (False, 'return_value')), # obj -> null -> tracefunc
Instr("LOAD_CONST", const), # obj -> null -> tracefunc -> obj
Instr("CALL", 0x0001), # obj -> retval
Instr("POP_TOP"), # obj
]

elif PY311:
def call_tracer_load_attr(tracer_op: str, i_arg: int, Instr=bc.Instr):
return [ # obj
Expand Down Expand Up @@ -350,6 +360,9 @@ def call_tracer_return_value(tracer_op: str, Instr=bc.Instr):
Instr("POP_TOP"), # obj
]

def call_tracer_return_const(tracer_op: str, const, Instr=bc.Instr):
raise NotImplementedError()

else:
def call_tracer_load_attr(tracer_op: str, i_arg: int, Instr=bc.Instr):
return [ # obj
Expand Down Expand Up @@ -408,6 +421,9 @@ def call_tracer_return_value(tracer_op: str, Instr=bc.Instr):
Instr("POP_TOP"), # obj
]

def call_tracer_return_const(tracer_op: str, const, Instr=bc.Instr):
raise NotImplementedError()


def inject_tracing(bytecode, nested=False):
""" Inject tracing code into the given code list.
Expand Down Expand Up @@ -476,6 +492,8 @@ def inject_tracing(bytecode, nested=False):
inserts[idx] = call_tracer_get_iter(tracer_op)
elif i_name == "RETURN_VALUE":
inserts[idx] = call_tracer_return_value(tracer_op)
elif i_name == "RETURN_CONST":
inserts[idx] = call_tracer_return_const(tracer_op, i_arg)
elif isinstance(i_arg, CodeType):
# Inject tracing in nested code object if they use their parent
# locals.
Expand Down

0 comments on commit 9c2fbb0

Please sign in to comment.