Skip to content

Commit cea0585

Browse files
authored
[3.10] bpo-44298: Backport #26513 to 3.10 (#26516)
* Backport 937cebc to 3.10 * Update importlib
1 parent 84d80f5 commit cea0585

File tree

4 files changed

+2908
-2853
lines changed

4 files changed

+2908
-2853
lines changed

Lib/test/test_sys_settrace.py

+46
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,52 @@ def func():
995995
(5, 'line'),
996996
(5, 'return')])
997997

998+
def test_early_exit_with(self):
999+
1000+
class C:
1001+
def __enter__(self):
1002+
return self
1003+
def __exit__(*args):
1004+
pass
1005+
1006+
def func_break():
1007+
for i in (1,2):
1008+
with C():
1009+
break
1010+
pass
1011+
1012+
def func_return():
1013+
with C():
1014+
return
1015+
1016+
self.run_and_compare(func_break,
1017+
[(0, 'call'),
1018+
(1, 'line'),
1019+
(2, 'line'),
1020+
(-5, 'call'),
1021+
(-4, 'line'),
1022+
(-4, 'return'),
1023+
(3, 'line'),
1024+
(2, 'line'),
1025+
(-3, 'call'),
1026+
(-2, 'line'),
1027+
(-2, 'return'),
1028+
(4, 'line'),
1029+
(4, 'return')])
1030+
1031+
self.run_and_compare(func_return,
1032+
[(0, 'call'),
1033+
(1, 'line'),
1034+
(-11, 'call'),
1035+
(-10, 'line'),
1036+
(-10, 'return'),
1037+
(2, 'line'),
1038+
(1, 'line'),
1039+
(-9, 'call'),
1040+
(-8, 'line'),
1041+
(-8, 'return'),
1042+
(1, 'return')])
1043+
9981044

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

Python/compile.c

+14-4
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,6 @@ static int
17671767
compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
17681768
int preserve_tos)
17691769
{
1770-
int loc;
17711770
switch (info->fb_type) {
17721771
case WHILE_LOOP:
17731772
case EXCEPTION_HANDLER:
@@ -1820,7 +1819,6 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
18201819

18211820
case WITH:
18221821
case ASYNC_WITH:
1823-
loc = c->u->u_lineno;
18241822
SET_LOC(c, (stmt_ty)info->fb_datum);
18251823
ADDOP(c, POP_BLOCK);
18261824
if (preserve_tos) {
@@ -1835,7 +1833,10 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
18351833
ADDOP(c, YIELD_FROM);
18361834
}
18371835
ADDOP(c, POP_TOP);
1838-
c->u->u_lineno = loc;
1836+
/* The exit block should appear to execute after the
1837+
* statement causing the unwinding, so make the unwinding
1838+
* instruction artificial */
1839+
c->u->u_lineno = -1;
18391840
return 1;
18401841

18411842
case HANDLER_CLEANUP:
@@ -2986,12 +2987,17 @@ compiler_return(struct compiler *c, stmt_ty s)
29862987
if (preserve_tos) {
29872988
VISIT(c, expr, s->v.Return.value);
29882989
} else {
2989-
/* Emit instruction with line number for expression */
2990+
/* Emit instruction with line number for return value */
29902991
if (s->v.Return.value != NULL) {
29912992
SET_LOC(c, s->v.Return.value);
29922993
ADDOP(c, NOP);
29932994
}
29942995
}
2996+
if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) {
2997+
SET_LOC(c, s);
2998+
ADDOP(c, NOP);
2999+
}
3000+
29953001
if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
29963002
return 0;
29973003
if (s->v.Return.value == NULL) {
@@ -3010,6 +3016,8 @@ static int
30103016
compiler_break(struct compiler *c)
30113017
{
30123018
struct fblockinfo *loop = NULL;
3019+
/* Emit instruction with line number */
3020+
ADDOP(c, NOP);
30133021
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
30143022
return 0;
30153023
}
@@ -3028,6 +3036,8 @@ static int
30283036
compiler_continue(struct compiler *c)
30293037
{
30303038
struct fblockinfo *loop = NULL;
3039+
/* Emit instruction with line number */
3040+
ADDOP(c, NOP);
30313041
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
30323042
return 0;
30333043
}

0 commit comments

Comments
 (0)