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.

Author nedbat
Recipients Mark.Shannon, nedbat
Date 2021-07-05.22:29:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1625524178.46.0.642583594704.issue44570@roundup.psfhosted.org>
In-reply-to
Content
(from https://github.com/nedbat/coveragepy/issues/1184)

This code runs the return statement on line 17 twice.  The second time, there is a "line" event and then a "return" event for that line.  But the first time, there is only a "return" event:

--- 8< -------------------------------
import linecache, sys

def trace(frame, event, arg):
    # The weird globals here is to avoid a NameError on shutdown...
    if frame.f_code.co_filename == globals().get("__file__"):
        lineno = frame.f_lineno
        print("{} {}: {}".format(event[:4], lineno, linecache.getline(__file__, lineno).rstrip()))
    return trace

def foo(x):
    if x:
        try:
            1/(x - 1)
        except ZeroDivisionError:
            pass

    return x

def test_foo():
    for i in range(2):
        foo(i)

print(sys.version)
sys.settrace(trace)
test_foo()
-----------------------------------------

I get this output with 3.10.0b3:

3.10.0b3 (default, Jun 18 2021, 06:43:38) [Clang 12.0.0 (clang-1200.0.32.29)]
call 19: def test_foo():
line 20:     for i in range(2):
line 21:         foo(i)
call 10: def foo(x):
line 11:     if x:
retu 17:     return x
line 20:     for i in range(2):
line 21:         foo(i)
call 10: def foo(x):
line 11:     if x:
line 12:         try:
line 13:             1/(x - 1)
exce 13:             1/(x - 1)
line 14:         except ZeroDivisionError:
line 15:             pass
line 17:     return x
retu 17:     return x
line 20:     for i in range(2):
retu 20:     for i in range(2):


Shouldn't there be a "line" event for both invocations?
History
Date User Action Args
2021-07-05 22:29:38nedbatsetrecipients: + nedbat, Mark.Shannon
2021-07-05 22:29:38nedbatsetmessageid: <1625524178.46.0.642583594704.issue44570@roundup.psfhosted.org>
2021-07-05 22:29:38nedbatlinkissue44570 messages
2021-07-05 22:29:38nedbatcreate