-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python3.8 changes how decorators are traced #79057
Comments
When decorating a function, the sequence of lines reported to the trace function is different in Python3.8 than with previous versions $ cat -n decorator.py
1 def decorator(f):
2 return f
3
4 def f():
5 @decorator
6 @decorator
7 @decorator
8 def func():
9 pass
10
11 import sys
12 def trace(frame, event, args):
13 print(frame.f_lineno, event)
14 return trace
15
16 sys.settrace(trace)
17 f()
$ python3.7 decorator.py
4 call
5 line
6 line
7 line
1 call
2 line
2 return
1 call
2 line
2 return
1 call
2 line
2 return
7 return
$ python3.8 decorator.py
4 call
5 line
6 line
7 line
5 line
1 call
2 line
2 return
1 call
2 line
2 return
1 call
2 line
2 return
5 return Is this intentional? Will it be changed back before 3.8 ships? People are testing their projects against 3.8-dev, and reporting problems with coverage. The problems are due to these sorts of changes. |
It looks like this is caused by da8d72c Adding Serhiy to the nosy list. |
This is because the first line of the function definition was the line of the last decorator in 3.7, and it is the line of the first decorator in 3.8. $ rlwrap ./python -m dis @decorator
@decorator
@decorator
def func():
pass In 3.7: 2 0 LOAD_NAME 0 (decorator) 3 2 LOAD_NAME 0 (decorator) 4 4 LOAD_NAME 0 (decorator) In 3.8: 2 0 LOAD_NAME 0 (decorator) 3 2 LOAD_NAME 0 (decorator) 4 4 LOAD_NAME 0 (decorator) 2 6 LOAD_CONST 0 (<code object func at 0x7f7045a80100, file "<stdin>", line 2>) |
On other hand, consider the following example of multiline assignment: $ rlwrap ./python -m dis
a = [
x,
y,
] In 3.7: 2 0 LOAD_NAME 0 (x) 3 2 LOAD_NAME 1 (y) In 3.8: 2 0 LOAD_NAME 0 (x) 3 2 LOAD_NAME 1 (y) 1 4 BUILD_LIST 2 In 3.7 the line of the assignment "a = [" is not traced. In 3.8 it is traced. These all are a consequences of the same change. |
Are we sure this is the behavior we want? Now when I step through your code in the debugger, it will show me line 2, then 3, then 4, then 2 again. I can see the appeal for a multiline assignment statement, but for stacked decorators it just seems wrong. |
I think this is a correct behavior. $ cat -n multiline_assignment.py
1 x = 1
2 y = 2
3 z = [
4 x,
5 y,
6 ]
$ ./python -m trace --trace multiline_assignment.py In 3.7 the line with the assignment is missed: --- modulename: multiline_assignment, funcname: <module> In 3.8: --- modulename: multiline_assignment, funcname: <module> |
Yes, I agree that the assignment statement behavior is fine. The stacked decorator behavior is not. I understand that under the hood the two cases are very similar, but the syntax is different. Jumping back to the first decorator makes it look like the decorators are executed in order and then the first decorator runs again. There is nothing in the syntax that makes revisting the first decorator line reasonable. |
First the decorator itself is loaded. Then the function is created, decorators are called and the result is bound to the name. There is similar situation in the case of multiline call. $ cat -n multiline_call.py
1 def f(a, b):
2 return [
3 a,
4 b,
5 ]
6
7 x = f(
8 1,
9 2,
10 )
$ ./python -m trace --trace multiline_call.py In 3.7: --- modulename: multiline_call, funcname: <module> In 3.8: --- modulename: multiline_call, funcname: <module> Line 7 started the execution with loading the function f. Then arguments are evaluated on lines 1 and 2. Then line 7 continue the execution with calling the function and consuming its result. Maybe using a range of lines instead of a single line will help (as was discussed in bpo-12458). First time the single line with a decorator is executed, second time the multiline expression that starts with the same line is executed. But this may require a significant change of AST and bytecode format. |
I have never looked at the trace of a decorated object before. The 3.7 behavior treating the inner decorator line as the first line of the decorated function definition looks wrong to me. I actually expected the line pointer to move down to the def line, analogously to the following, at least until after MAKE_FUNCTION, but moving to the beginning of the statement for the rest would seem proper. >>> dis.dis("""a = f(
f(
f(
3)))""")
1 0 LOAD_NAME 0 (f) 2 2 LOAD_NAME 0 (f) 3 4 LOAD_NAME 0 (f) 4 6 LOAD_CONST 0 (3) |
This is the --trace output for some stacked decorators: $ cat -n /tmp/decdec.py
1 def decorator1(f):
2 return f
3
4 def decorator2(f):
5 return f
6
7 def decorator3(f):
8 return f
9
10 @decorator1
11 @decorator2
12 @decorator3
13 def func():
14 print("hello")
15
16 func()
$ python3.7 -m trace --trace /tmp/decdec.py
--- modulename: decdec, funcname: <module>
decdec.py(1): def decorator1(f):
decdec.py(4): def decorator2(f):
decdec.py(7): def decorator3(f):
decdec.py(10): @decorator1
decdec.py(11): @decorator2
decdec.py(12): @decorator3
--- modulename: decdec, funcname: decorator3
decdec.py(8): return f
--- modulename: decdec, funcname: decorator2
decdec.py(5): return f
--- modulename: decdec, funcname: decorator1
decdec.py(2): return f
decdec.py(16): func()
--- modulename: decdec, funcname: func
decdec.py(14): print("hello")
hello
$ python3.8 -m trace --trace /tmp/decdec.py
--- modulename: decdec, funcname: <module>
decdec.py(1): def decorator1(f):
decdec.py(4): def decorator2(f):
decdec.py(7): def decorator3(f):
decdec.py(10): @decorator1
decdec.py(11): @decorator2
decdec.py(12): @decorator3
decdec.py(10): @decorator1
--- modulename: decdec, funcname: decorator3
decdec.py(8): return f
--- modulename: decdec, funcname: decorator2
decdec.py(5): return f
--- modulename: decdec, funcname: decorator1
decdec.py(2): return f
decdec.py(16): func()
--- modulename: decdec, funcname: func
decdec.py(14): print("hello")
hello In Python3.8, "@decorator1" appears twice, as both the first and the last decorator line traced. There's no conceptual reason to show that line twice. I'd like to consider the stacked decorator case separately from the multi-line function call case. Yes, they are consequences of the same change. One change can have good effects and bad effects. We can do further work to eliminate the bad effects. |
Seems PR 9731 fixes this issue. I'll add tests later. |
See 09aaa88. The position of the AST node for decorated function and class was changed to the position of the first decorator. It was made to help inspect.getsource() for functions to include decorator lines in the result. But the position of |
Ned, please look at PR 9731. Does it fixes the issue to you? Georg, you had added the original code for patching the lineno of decorated function. Are your good to remove this patch and to move updating the first line number at the code generation stage? |
Thanks, the fix looks good to me. I made a comparison of some decorator tracing to check it out: https://gist.github.com/nedbat/d603a34136299f0c0b8e442fccadeb7d TBH, the first time I tried it, something seemed wrong, but I can't see it now, so ¯\(ツ)/¯ :) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: