[New-bugs-announce] [issue11992] sys.settrace doesn't disable tracing if a local trace function returns None
report at bugs.python.org
Wed May 4 03:58:53 CEST 2011
New submission from Ned Batchelder <nedbat at users.sourceforge.net>:
The docs say:
The trace function is invoked (with event set to 'call') whenever a new local scope is entered; it should return a reference to a local trace function to be used that scope, or None if the scope shouldn’t be traced.
The local trace function should return a reference to itself (or to another function for further tracing in that scope), or None to turn off tracing in that scope.
It's that last part that's wrong: returning None from the trace function only has an effect on the first call in a new frame. Once the trace function returns a function for a frame, returning None from subsequent calls is ignored. A "local trace function" can't turn off tracing in its scope.
UPTO_LINE = 1
def t(frame, event, arg):
num = frame.f_lineno
print("line %d" % num)
if num < UPTO_LINE:
UPTO_LINE = 1
UPTO_LINE = 13
The first call to try_it() returns None immediately, preventing tracing for the rest of the function. The second call returns None at line 13, but the rest of the function is traced anyway. This behavior is the same in all versions from 2.3 to 3.2, in fact, the 100 lines of code in sysmodule.c responsible for Python tracing functions are completely unchanged through those versions.
components: Library (Lib)
title: sys.settrace doesn't disable tracing if a local trace function returns None
versions: Python 2.7, Python 3.2
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce