[3.10] bpo-44298: Backport #26513 to 3.10 (#26516)
* Backport 937cebc93 to 3.10
* Update importlib
diff --git a/Python/compile.c b/Python/compile.c
index 3c69ce2..4abc9f0 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -1767,7 +1767,6 @@ static int
compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
int preserve_tos)
{
- int loc;
switch (info->fb_type) {
case WHILE_LOOP:
case EXCEPTION_HANDLER:
@@ -1820,7 +1819,6 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
case WITH:
case ASYNC_WITH:
- loc = c->u->u_lineno;
SET_LOC(c, (stmt_ty)info->fb_datum);
ADDOP(c, POP_BLOCK);
if (preserve_tos) {
@@ -1835,7 +1833,10 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
ADDOP(c, YIELD_FROM);
}
ADDOP(c, POP_TOP);
- c->u->u_lineno = loc;
+ /* The exit block should appear to execute after the
+ * statement causing the unwinding, so make the unwinding
+ * instruction artificial */
+ c->u->u_lineno = -1;
return 1;
case HANDLER_CLEANUP:
@@ -2986,12 +2987,17 @@ compiler_return(struct compiler *c, stmt_ty s)
if (preserve_tos) {
VISIT(c, expr, s->v.Return.value);
} else {
- /* Emit instruction with line number for expression */
+ /* Emit instruction with line number for return value */
if (s->v.Return.value != NULL) {
SET_LOC(c, s->v.Return.value);
ADDOP(c, NOP);
}
}
+ if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) {
+ SET_LOC(c, s);
+ ADDOP(c, NOP);
+ }
+
if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
return 0;
if (s->v.Return.value == NULL) {
@@ -3010,6 +3016,8 @@ static int
compiler_break(struct compiler *c)
{
struct fblockinfo *loop = NULL;
+ /* Emit instruction with line number */
+ ADDOP(c, NOP);
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
return 0;
}
@@ -3028,6 +3036,8 @@ static int
compiler_continue(struct compiler *c)
{
struct fblockinfo *loop = NULL;
+ /* Emit instruction with line number */
+ ADDOP(c, NOP);
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
return 0;
}