[Python-checkins] gh-104686: Fix tracing for decorated classes (#104708)

JelleZijlstra webhook-mailer at python.org
Sun May 21 19:20:27 EDT 2023


https://github.com/python/cpython/commit/cd9748409aa877d6d9905730bf68f25cf7a6a723
commit: cd9748409aa877d6d9905730bf68f25cf7a6a723
branch: main
author: Jelle Zijlstra <jelle.zijlstra at gmail.com>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2023-05-21T16:20:19-07:00
summary:

gh-104686: Fix tracing for decorated classes (#104708)

files:
M Lib/test/test_sys_settrace.py
M Python/compile.c

diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py
index 0571919066bb..5603c3cdbf3c 100644
--- a/Lib/test/test_sys_settrace.py
+++ b/Lib/test/test_sys_settrace.py
@@ -1524,6 +1524,52 @@ def __init__(self):
              (3, 'return'),
              (1, 'return')])
 
+    def test_class_creation_with_decorator(self):
+        def func():
+            def decorator(arg):
+                def _dec(c):
+                    return c
+                return _dec
+
+            @decorator(6)
+            @decorator(
+                len([8]),
+            )
+            class MyObject:
+                pass
+
+        self.run_and_compare(func, [
+            (0, 'call'),
+            (1, 'line'),
+            (6, 'line'),
+            (1, 'call'),
+            (2, 'line'),
+            (4, 'line'),
+            (4, 'return'),
+            (7, 'line'),
+            (8, 'line'),
+            (7, 'line'),
+            (1, 'call'),
+            (2, 'line'),
+            (4, 'line'),
+            (4, 'return'),
+            (10, 'line'),
+            (6, 'call'),
+            (6, 'line'),
+            (11, 'line'),
+            (11, 'return'),
+            (7, 'line'),
+            (2, 'call'),
+            (3, 'line'),
+            (3, 'return'),
+            (6, 'line'),
+            (2, 'call'),
+            (3, 'line'),
+            (3, 'return'),
+            (10, 'line'),
+            (10, 'return'),
+        ])
+
     @support.cpython_only
     def test_no_line_event_after_creating_generator(self):
         # Spurious line events before call events only show up with C tracer
diff --git a/Python/compile.c b/Python/compile.c
index cfe8224a7e27..e4dc9729b694 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -2486,6 +2486,10 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
     }
 
     /* 2. load the 'build_class' function */
+
+    // these instructions should be attributed to the class line,
+    // not a decorator line
+    loc = LOC(s);
     ADDOP(c, loc, PUSH_NULL);
     ADDOP(c, loc, LOAD_BUILD_CLASS);
 



More information about the Python-checkins mailing list