Skip to content

Commit cd97484

Browse files
gh-104686: Fix tracing for decorated classes (#104708)
1 parent 64d1b44 commit cd97484

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,52 @@ def __init__(self):
15241524
(3, 'return'),
15251525
(1, 'return')])
15261526

1527+
def test_class_creation_with_decorator(self):
1528+
def func():
1529+
def decorator(arg):
1530+
def _dec(c):
1531+
return c
1532+
return _dec
1533+
1534+
@decorator(6)
1535+
@decorator(
1536+
len([8]),
1537+
)
1538+
class MyObject:
1539+
pass
1540+
1541+
self.run_and_compare(func, [
1542+
(0, 'call'),
1543+
(1, 'line'),
1544+
(6, 'line'),
1545+
(1, 'call'),
1546+
(2, 'line'),
1547+
(4, 'line'),
1548+
(4, 'return'),
1549+
(7, 'line'),
1550+
(8, 'line'),
1551+
(7, 'line'),
1552+
(1, 'call'),
1553+
(2, 'line'),
1554+
(4, 'line'),
1555+
(4, 'return'),
1556+
(10, 'line'),
1557+
(6, 'call'),
1558+
(6, 'line'),
1559+
(11, 'line'),
1560+
(11, 'return'),
1561+
(7, 'line'),
1562+
(2, 'call'),
1563+
(3, 'line'),
1564+
(3, 'return'),
1565+
(6, 'line'),
1566+
(2, 'call'),
1567+
(3, 'line'),
1568+
(3, 'return'),
1569+
(10, 'line'),
1570+
(10, 'return'),
1571+
])
1572+
15271573
@support.cpython_only
15281574
def test_no_line_event_after_creating_generator(self):
15291575
# Spurious line events before call events only show up with C tracer

Python/compile.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,6 +2486,10 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
24862486
}
24872487

24882488
/* 2. load the 'build_class' function */
2489+
2490+
// these instructions should be attributed to the class line,
2491+
// not a decorator line
2492+
loc = LOC(s);
24892493
ADDOP(c, loc, PUSH_NULL);
24902494
ADDOP(c, loc, LOAD_BUILD_CLASS);
24912495

0 commit comments

Comments
 (0)