Skip to content

PEP 695 changed how class decorators are traced #104686

Description

@nedbat

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on May 20, 2023
  2. JelleZijlstra commented on May 20, 2023

    @JelleZijlstra
    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.

  3. added a commit that references this issue on May 20, 2023
  4. JelleZijlstra commented on May 20, 2023

    @JelleZijlstra
    Member

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

  5. added a commit that references this issue on May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions