Skip to content

Commit e56d54e

Browse files
authored
Mark instructions at end of class scope as artificial. (GH-24222)
1 parent 4c94d74 commit e56d54e

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,24 @@ def func():
956956
(2, 'line'),
957957
(2, 'return')])
958958

959+
def test_implicit_return_in_class(self):
960+
961+
def func():
962+
class A:
963+
if 3 < 9:
964+
a = 1
965+
else:
966+
a = 2
967+
968+
self.run_and_compare(func,
969+
[(0, 'call'),
970+
(1, 'line'),
971+
(1, 'call'),
972+
(1, 'line'),
973+
(2, 'line'),
974+
(3, 'line'),
975+
(3, 'return'),
976+
(1, 'return')])
959977

960978
class SkipLineEventsTraceTestCase(TraceTestCase):
961979
"""Repeat the trace tests, but with per-line events skipped"""

Python/compile.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,8 @@ compiler_class(struct compiler *c, stmt_ty s)
23712371
compiler_exit_scope(c);
23722372
return 0;
23732373
}
2374+
/* The following code is artificial */
2375+
c->u->u_lineno = -1;
23742376
/* Return __classcell__ if it is referenced, otherwise return None */
23752377
if (c->u->u_ste->ste_needs_class_closure) {
23762378
/* Store __classcell__ into class namespace & return it */

0 commit comments

Comments
 (0)