This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: sys.settrace doesn't disable tracing if a local trace function returns None
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.2, Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: nedbat Nosy List: arigo, nedbat, xdegaye
Priority: normal Keywords:

Created on 2011-05-04 01:58 by nedbat, last changed 2022-04-11 14:57 by admin.

Messages (3)
msg135089 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2011-05-04 01:58
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.

To demonstrate:

    import sys

    UPTO_LINE = 1

    def t(frame, event, arg):
        num = frame.f_lineno
        print("line %d" % num)
        if num < UPTO_LINE:
            return t

    def try_it():

    UPTO_LINE = 1

    UPTO_LINE = 13


    line 11
    line 11
    line 12
    line 13
    line 14
    line 15
    line 15

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.
msg206739 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2013-12-21 14:07
See also issue 20040.
msg285720 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2017-01-18 11:32
Confirmed.  More interestingly, nowadays (at least in 3.5) depends on this bug.  If we really clear f->f_trace when the trace function returns None, then test_pdb_until_command_for_generator() fails.  This is because incorrectly thinks there is no breakpoint in the generator function, and returns None.  This doesn't actually clear anything, and so it works anyway.

I'd suggest to fix the documentation to reflect the actual behavior of all versions from 2.3 to (at least) 3.5.
Date User Action Args
2022-04-11 14:57:16adminsetgithub: 56201
2017-01-18 11:32:14arigosetnosy: + arigo
messages: + msg285720
2013-12-21 14:07:05xdegayesetnosy: + xdegaye
messages: + msg206739
2011-05-04 02:03:44nedbatsetassignee: nedbat
2011-05-04 01:58:53nedbatcreate