[New-bugs-announce] [issue46234] 3.11: Tracing of decorators now visits the decorator line before the decorator function

Ned Batchelder report at bugs.python.org
Sun Jan 2 18:14:57 EST 2022


New submission from Ned Batchelder <ned at nedbatchelder.com>:

Sometime after 3.11.0a3, tracing of decorators changed so that each decorator
line is revisited as the decorator is invoked.  Is this intentional?

---< dectrace.py >-----------------------------------
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

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

def decorator(func):
    return func

def doit():
    @decorator
    @decorator
    @decorator
    def func(x):
        return x + 1

    print(func(1))

doit()
-----------------------------------------------------

Running it on 3.10, 3.11.0a3, and latest 3.11.  The last run has the new line
hightlighted with <<<<<:

$ python3.10 dectrace.py
3.10.1 (main, Dec 14 2021, 08:30:13) [Clang 12.0.0 (clang-1200.0.32.29)]
call 16: def doit():
line 17:     @decorator
line 18:     @decorator
line 19:     @decorator
line 20:     def func(x):
call 13: def decorator(func):
line 14:     return func
retu 14:     return func
call 13: def decorator(func):
line 14:     return func
retu 14:     return func
call 13: def decorator(func):
line 14:     return func
retu 14:     return func
line 23:     print(func(1))
call 17:     @decorator
line 21:         return x + 1
retu 21:         return x + 1
2
retu 23:     print(func(1))

$ python3.11 dectrace.py
3.11.0a3 (main, Dec  9 2021, 12:22:18) [Clang 12.0.0 (clang-1200.0.32.29)]
call 16: def doit():
line 17:     @decorator
line 18:     @decorator
line 19:     @decorator
line 20:     def func(x):
call 13: def decorator(func):
line 14:     return func
retu 14:     return func
call 13: def decorator(func):
line 14:     return func
retu 14:     return func
call 13: def decorator(func):
line 14:     return func
retu 14:     return func
line 23:     print(func(1))
call 17:     @decorator
line 21:         return x + 1
retu 21:         return x + 1
2
retu 23:     print(func(1))

$ /usr/local/cpython/bin/python3.11 dectrace.py
3.11.0a3+ (heads/main:a82baed0e9, Jan  2 2022, 08:12:01) [Clang 12.0.0 (clang-1200.0.32.29)]
call 16: def doit():
line 17:     @decorator
line 18:     @decorator
line 19:     @decorator
line 20:     def func(x):
line 19:     @decorator             <<<<<
call 13: def decorator(func):
line 14:     return func
retu 14:     return func
line 18:     @decorator             <<<<<
call 13: def decorator(func):
line 14:     return func
retu 14:     return func
line 17:     @decorator             <<<<<
call 13: def decorator(func):
line 14:     return func
retu 14:     return func
line 20:     def func(x):
line 23:     print(func(1))
call 17:     @decorator
line 21:         return x + 1
retu 21:         return x + 1
2
retu 23:     print(func(1))


(this might or might not be related to https://bugs.python.org/issue37971)

----------
components: Interpreter Core
keywords: 3.11regression
messages: 409539
nosy: Mark.Shannon, nedbat
priority: normal
severity: normal
status: open
title: 3.11: Tracing of decorators now visits the decorator line before the decorator function
type: behavior
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46234>
_______________________________________


More information about the New-bugs-announce mailing list