[Python-Dev] API design question: how to extend sys.settrace()?

Nick Coghlan ncoghlan at gmail.com
Wed Sep 27 12:39:07 EDT 2017


On 27 September 2017 at 22:56, Victor Stinner <victor.stinner at gmail.com> wrote:
> Hi,
>
> In bpo-29400, it was proposed to add the ability to trace not only
> function calls but also instructions at the bytecode level. I like the
> idea, but I don't see how to extend sys.settrace() to add a new
> "trace_instructions: bool" optional (keyword-only?) parameter without
> breaking the backward compatibility. Should we add a new function
> instead?

As part of investigating the current signal safety problems in with
statements [1], I added the ability to trace lines, opcodes, both, or
neither by setting a couple of flags on a per-frame basis:
https://docs.python.org/dev/whatsnew/3.7.html#other-cpython-implementation-changes

So the idea is that if you want per-opcode tracing, your trace
function has to turn it on for each frame when handling the call
event, and you accept the responsibility of not messing up the frame
stack.

Cheers,
Nick.

[1] See https://bugs.python.org/issue29988 if you're interested in the
gory details

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list