◐ Shell
clean mode source ↗

Message 410643 - Python tracker

In Python 3.11, unused generator comprehensions cause trace events with f_lineno set to None.

---- %< -------------------------------------------------
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
        if lineno is not None:
            line = linecache.getline(__file__, lineno).rstrip()
        else:
            line = "<none>"
        print("{} {!s:4}: {}".format(event[:4], lineno, line))
    return trace

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

(i for i in [1])
------------------------------------------------------

$ python3.10 /tmp/runbpo.py
3.10.2 (main, Jan 15 2022, 05:51:59) [Clang 12.0.0 (clang-1200.0.32.29)]
call 17  : (i for i in [1])
exce 17  : (i for i in [1])
retu 17  : (i for i in [1])

$ python3.11 /tmp/runbpo.py
3.11.0a4 (main, Jan 14 2022, 18:14:29) [Clang 12.0.0 (clang-1200.0.32.29)]
call None: <none>
exce None: <none>
retu None: <none>