Closed
Description
Bug report
The documentation of sys.settrace claims:
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.
Returning None inside a local trace function should therefore disable tracing. However, it doesn't:
def global_tf(*_):
return local_tf
def local_tf(f, e, _):
print(e, f)
return None
def some_function():
1
2
import sys
sys.settrace(global_tf)
some_function()Running this code produces three events within some_function (one for each line and one return), even though every invocation of the local trace function returns None.
Your environment
- CPython versions tested on: 3.11, 3.10, 3.9 (behaviour differs slightly: just one line event)
- Operating system and architecture: macOS (3.11, 3.10, 3.9), Linux (3.11)
I see three possibilities:
- This is a misunderstanding, there's something I missed.
- This is a bug in the documentation. In that case, the docs should be changed to something like "The local trace function can return a reference to another function for further tracing in that scope, or
Noneto continue tracing that scope." I could write the PR in that case. - The behaviour is a bug. In that case, I think this part of the code should be changed to set
frame.f_tracetoNonein theelsecase: https://github.com/python/cpython/blob/main/Python/sysmodule.c#L997-L1002 (thanks to @godlygeek for pointing out the place this is checked in). I could try writing the PR in that case.

