Skip to content
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

PEP 695 changed how class decorators are traced #104686

Closed
nedbat opened this issue May 20, 2023 · 2 comments
Closed

PEP 695 changed how class decorators are traced #104686

nedbat opened this issue May 20, 2023 · 2 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@nedbat
Copy link
Member

nedbat commented May 20, 2023

Commit 24d8b88 ("gh-103763: Implement PEP 695 (#103764)") changed how nested class decorators are traced.

Example file nightly-20230518.py:

def decorator(arg):
    def _dec(c):
        return c
    return _dec

@decorator(6)
@decorator(
    len([8]),
)
class MyObject(
    object
):
    X = 13
a = 14

Tracing with 3.12.0a7:

% python3.12 -m trace --trace nightly-20230518.py
 --- modulename: nightly-20230518, funcname: <module>
nightly-20230518.py(1): def decorator(arg):
nightly-20230518.py(6): @decorator(6)
 --- modulename: nightly-20230518, funcname: decorator
nightly-20230518.py(2):     def _dec(c):
nightly-20230518.py(4):     return _dec
nightly-20230518.py(7): @decorator(
nightly-20230518.py(8):     len([8]),
nightly-20230518.py(7): @decorator(
 --- modulename: nightly-20230518, funcname: decorator
nightly-20230518.py(2):     def _dec(c):
nightly-20230518.py(4):     return _dec
nightly-20230518.py(10): class MyObject(            <*****
nightly-20230518.py(11):     object
nightly-20230518.py(10): class MyObject(
 --- modulename: nightly-20230518, funcname: MyObject
nightly-20230518.py(6): @decorator(6)
nightly-20230518.py(13):     X = 13
nightly-20230518.py(7): @decorator(
 --- modulename: nightly-20230518, funcname: _dec
nightly-20230518.py(3):         return c
nightly-20230518.py(6): @decorator(6)
 --- modulename: nightly-20230518, funcname: _dec
nightly-20230518.py(3):         return c
nightly-20230518.py(10): class MyObject(
nightly-20230518.py(14): a = 14
% python3.12 -c "import sys; print(sys.version)"
3.12.0a7 (main, Apr  5 2023, 05:51:58) [Clang 14.0.3 (clang-1403.0.22.14.1)]

Running with newer code:

% /usr/local/cpython/bin/python3 -m trace --trace nightly-20230518.py
 --- modulename: nightly-20230518, funcname: <module>
nightly-20230518.py(1): def decorator(arg):
nightly-20230518.py(6): @decorator(6)
 --- modulename: nightly-20230518, funcname: decorator
nightly-20230518.py(2):     def _dec(c):
nightly-20230518.py(4):     return _dec
nightly-20230518.py(7): @decorator(
nightly-20230518.py(8):     len([8]),
nightly-20230518.py(7): @decorator(
 --- modulename: nightly-20230518, funcname: decorator
nightly-20230518.py(2):     def _dec(c):
nightly-20230518.py(4):     return _dec
nightly-20230518.py(6): @decorator(6)               <*****
nightly-20230518.py(11):     object
nightly-20230518.py(10): class MyObject(
 --- modulename: nightly-20230518, funcname: MyObject
nightly-20230518.py(6): @decorator(6)
nightly-20230518.py(13):     X = 13
nightly-20230518.py(7): @decorator(
 --- modulename: nightly-20230518, funcname: _dec
nightly-20230518.py(3):         return c
nightly-20230518.py(6): @decorator(6)
 --- modulename: nightly-20230518, funcname: _dec
nightly-20230518.py(3):         return c
nightly-20230518.py(10): class MyObject(
nightly-20230518.py(14): a = 14
% /usr/local/cpython/bin/python3 -c "import sys; print(sys.version)"
3.12.0a7+ (tags/v3.12.0a7-548-g24d8b88420:24d8b88420, May 20 2023, 06:39:38) [Clang 14.0.3 (clang-1403.0.22.14.1)]

The different lines are marked with <*****.

cc: @JelleZijlstra @markshannon

Linked PRs

@nedbat nedbat added the type-bug An unexpected behavior, bug, or error label May 20, 2023
@JelleZijlstra
Copy link
Member

In the disassembly I see that the instructions for creating the class are on a different (wrong) line:

main:

  8          42 CALL                     1

  7          50 PUSH_NULL
             52 LOAD_BUILD_CLASS
             54 LOAD_CONST               3 (<code object MyObject at 0x104ea5e40, file "<dis>", line 7>)
             56 MAKE_FUNCTION            0
             58 LOAD_CONST               4 ('MyObject')

 12          60 LOAD_NAME                2 (object)

3.11:

  8          54 PRECALL                  1
             58 CALL                     1

 11          68 PUSH_NULL
             70 LOAD_BUILD_CLASS
             72 LOAD_CONST               3 (<code object MyObject at 0x10536ef50, file "<dis>", line 7>)
             74 MAKE_FUNCTION            0
             76 LOAD_CONST               4 ('MyObject')

 12          78 LOAD_NAME                2 (object)

So presumably I put the wrong location somewhere in the compiler. I'll take a look.

@JelleZijlstra
Copy link
Member

Submitted a fix in #104708, thanks @nedbat for the early report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants