Skip to content

Commit cd68e87

Browse files
committed
gh-98442: Fix location info of return statement
1 parent 9be05df commit cd68e87

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

Lib/test/test_compile.py

+13
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,19 @@ def test_multiline_assert(self):
12491249
self.assertOpcodeSourcePositionIs(compiled_code, 'RAISE_VARARGS',
12501250
line=1, end_line=3, column=0, end_column=30, occurrence=1)
12511251

1252+
def test_return_inside_with(self):
1253+
snippet = """\
1254+
def f(x):
1255+
with x:
1256+
return 42
1257+
"""
1258+
compiled_code, _ = self.check_positions_against_ast(snippet)
1259+
g = {}
1260+
exec(compiled_code, g)
1261+
compiled_code = g['f'].__code__
1262+
self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
1263+
line=3, end_line=3, column=8, end_column=17)
1264+
12521265
def test_very_long_line_end_offset(self):
12531266
# Make sure we get the correct column offset for offsets
12541267
# too large to store in a byte.

Python/compile.c

+13-16
Original file line numberDiff line numberDiff line change
@@ -3194,44 +3194,41 @@ compiler_while(struct compiler *c, stmt_ty s)
31943194
static int
31953195
compiler_return(struct compiler *c, stmt_ty s)
31963196
{
3197-
location loc = LOC(s);
31983197
int preserve_tos = ((s->v.Return.value != NULL) &&
31993198
(s->v.Return.value->kind != Constant_kind));
3200-
if (c->u->u_ste->ste_type != FunctionBlock)
3201-
return compiler_error(c, loc, "'return' outside function");
3199+
if (c->u->u_ste->ste_type != FunctionBlock) {
3200+
return compiler_error(c, LOC(s), "'return' outside function");
3201+
}
32023202
if (s->v.Return.value != NULL &&
32033203
c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
32043204
{
3205-
return compiler_error(
3206-
c, loc, "'return' with value in async generator");
3205+
return compiler_error(
3206+
c, LOC(s), "'return' with value in async generator");
32073207
}
32083208

32093209
if (preserve_tos) {
32103210
VISIT(c, expr, s->v.Return.value);
32113211
} else {
32123212
/* Emit instruction with line number for return value */
32133213
if (s->v.Return.value != NULL) {
3214-
SET_LOC(c, s->v.Return.value);
3215-
loc = LOC(s->v.Return.value);
3216-
ADDOP(c, loc, NOP);
3214+
ADDOP(c, LOC(s->v.Return.value), NOP);
32173215
}
32183216
}
32193217
if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) {
3220-
SET_LOC(c, s);
3221-
loc = LOC(s);
3222-
ADDOP(c, loc, NOP);
3218+
ADDOP(c, LOC(s), NOP);
32233219
}
32243220

3225-
if (!compiler_unwind_fblock_stack(c, &loc, preserve_tos, NULL))
3221+
location loc = LOC(s);
3222+
if (!compiler_unwind_fblock_stack(c, &loc, preserve_tos, NULL)) {
32263223
return 0;
3224+
}
32273225
if (s->v.Return.value == NULL) {
3228-
ADDOP_LOAD_CONST(c, loc, Py_None);
3226+
ADDOP_LOAD_CONST(c, LOC(s), Py_None);
32293227
}
32303228
else if (!preserve_tos) {
3231-
ADDOP_LOAD_CONST(c, loc, s->v.Return.value->v.Constant.value);
3229+
ADDOP_LOAD_CONST(c, LOC(s), s->v.Return.value->v.Constant.value);
32323230
}
3233-
ADDOP(c, loc, RETURN_VALUE);
3234-
3231+
ADDOP(c, LOC(s), RETURN_VALUE);
32353232
return 1;
32363233
}
32373234

0 commit comments

Comments
 (0)