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

Serhiy Storchaka storchaka at gmail.com
Wed Sep 27 11:10:20 EDT 2017

27.09.17 15:56, Victor Stinner пише:
> 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?

I afraid that this change breaks an assumption in frame_setlineno() 
about the state of the stack. This can corrupt the stack if you jump 
from the instruction which is a part of Python operation. For example 
FOR_ITER expects an iterator on the stack. If you jump to the end of the 
loop from the middle of an assignment operator and skip say STORE_FAST, 
you will left an arbitrary value on the stack. This can lead to 
unpredictable consequences.

More information about the Python-Dev mailing list