Skip to content

Provide a way to convert frame.f_lasti into an instruction #9

@eric-wieser

Description

@eric-wieser

Here's the situation - you want to find the bytecode that was just executed in a frame, possibly in a trace func. Here are a pair of things you could want:

  • The current ConcreteInstr object corresponding to f_lasti:

    def get_concrete_index(concrete_bc, code_index):
        at = 0
        concrete_index = 0
        for c in concrete_bc:
            at += c.size
            if at < code_index:
                concrete_index += 1
        return concrete_index

    Which can be used as:

    concrete_bc = ConcreteBytecode.from_code(frame.code)
    ci = get_concrete_index(concrete_bc, frame.f_lasti)
    concrete_instr = concrete_bc[ci]
  • the current Instr object corresponding to ci or flasti:

    def promote_concrete_index(bc, concrete_index):
        index = None
        at = 0
        for i, b in enumerate(bc):
            if at == concrete_index:
                index = i
            if isinstance(b, bytecode.instr.BaseInstr):
                at += 1
        return index

    Used as

    bc = concrete_bc.to_bytecode()
    i = promote_concrete_index(bc, ci)
    instr = bc[concrete_bc]
    

  1. Does this code look correct for all cases?
  2. Does this make sense as a library addition? If so, how are these operations best exposed in the API?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions