As of 3.11, code objects have a new co_positions()
method that yields tuples of the form (lineno_start, lineno_end, char_start, char_end)
for each bytecode instruction.
Combined with setting f_trace_opcodes
to True
on a frame, trace functions can theoretically track coverage on a character level.
There are a bunch of issues though, shown in part in the code:
- Some instructions correctly cover a wide range of code:
JUMP_*
instructions from branching code basically seems to span the entire length of the jump, i.e. from theif
to the end of the indented block.MAKE_FUNCTION
covers the entire function definition. These issues seem to be easy to resolve on first glance, because you can just ignore the corresponding opcodes.