Skip to content

Commit a1a8828

Browse files
authored
gh-98461: Fix location of RETURN_VALUE in async generator bytecode. compiler_jump_if no longer needs a pointer to the loc. (GH-98494)
1 parent 8aa1e99 commit a1a8828

File tree

2 files changed

+44
-57
lines changed

2 files changed

+44
-57
lines changed

Lib/test/test_compile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ def test_multiline_async_generator_expression(self):
12831283
self.assertOpcodeSourcePositionIs(compiled_code, 'YIELD_VALUE',
12841284
line=1, end_line=2, column=1, end_column=8, occurrence=2)
12851285
self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
1286-
line=6, end_line=6, column=23, end_column=30, occurrence=1)
1286+
line=1, end_line=6, column=0, end_column=32, occurrence=1)
12871287

12881288
def test_multiline_list_comprehension(self):
12891289
snippet = """\

Python/compile.c

+43-56
Original file line numberDiff line numberDiff line change
@@ -2873,13 +2873,14 @@ static int compiler_addcompare(struct compiler *c, location loc,
28732873

28742874

28752875
static int
2876-
compiler_jump_if(struct compiler *c, location *ploc,
2876+
compiler_jump_if(struct compiler *c, location loc,
28772877
expr_ty e, jump_target_label next, int cond)
28782878
{
28792879
switch (e->kind) {
28802880
case UnaryOp_kind:
2881-
if (e->v.UnaryOp.op == Not)
2882-
return compiler_jump_if(c, ploc, e->v.UnaryOp.operand, next, !cond);
2881+
if (e->v.UnaryOp.op == Not) {
2882+
return compiler_jump_if(c, loc, e->v.UnaryOp.operand, next, !cond);
2883+
}
28832884
/* fallback to general implementation */
28842885
break;
28852886
case BoolOp_kind: {
@@ -2893,11 +2894,13 @@ compiler_jump_if(struct compiler *c, location *ploc,
28932894
next2 = new_next2;
28942895
}
28952896
for (i = 0; i < n; ++i) {
2896-
if (!compiler_jump_if(c, ploc, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2897+
if (!compiler_jump_if(c, loc, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) {
28972898
return 0;
2899+
}
28982900
}
2899-
if (!compiler_jump_if(c, ploc, (expr_ty)asdl_seq_GET(s, n), next, cond))
2901+
if (!compiler_jump_if(c, loc, (expr_ty)asdl_seq_GET(s, n), next, cond)) {
29002902
return 0;
2903+
}
29012904
if (!SAME_LABEL(next2, next)) {
29022905
USE_LABEL(c, next2);
29032906
}
@@ -2906,45 +2909,46 @@ compiler_jump_if(struct compiler *c, location *ploc,
29062909
case IfExp_kind: {
29072910
NEW_JUMP_TARGET_LABEL(c, end);
29082911
NEW_JUMP_TARGET_LABEL(c, next2);
2909-
if (!compiler_jump_if(c, ploc, e->v.IfExp.test, next2, 0))
2912+
if (!compiler_jump_if(c, loc, e->v.IfExp.test, next2, 0)) {
29102913
return 0;
2911-
if (!compiler_jump_if(c, ploc, e->v.IfExp.body, next, cond))
2914+
}
2915+
if (!compiler_jump_if(c, loc, e->v.IfExp.body, next, cond)) {
29122916
return 0;
2917+
}
29132918
ADDOP_JUMP(c, NO_LOCATION, JUMP, end);
29142919

29152920
USE_LABEL(c, next2);
2916-
if (!compiler_jump_if(c, ploc, e->v.IfExp.orelse, next, cond))
2921+
if (!compiler_jump_if(c, loc, e->v.IfExp.orelse, next, cond)) {
29172922
return 0;
2923+
}
29182924

29192925
USE_LABEL(c, end);
29202926
return 1;
29212927
}
29222928
case Compare_kind: {
2923-
SET_LOC(c, e);
2924-
*ploc = LOC(e);
2925-
Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2929+
Py_ssize_t n = asdl_seq_LEN(e->v.Compare.ops) - 1;
29262930
if (n > 0) {
29272931
if (!check_compare(c, e)) {
29282932
return 0;
29292933
}
29302934
NEW_JUMP_TARGET_LABEL(c, cleanup);
29312935
VISIT(c, expr, e->v.Compare.left);
2932-
for (i = 0; i < n; i++) {
2936+
for (Py_ssize_t i = 0; i < n; i++) {
29332937
VISIT(c, expr,
29342938
(expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2935-
ADDOP_I(c, *ploc, SWAP, 2);
2936-
ADDOP_I(c, *ploc, COPY, 2);
2937-
ADDOP_COMPARE(c, *ploc, asdl_seq_GET(e->v.Compare.ops, i));
2938-
ADDOP_JUMP(c, *ploc, POP_JUMP_IF_FALSE, cleanup);
2939+
ADDOP_I(c, LOC(e), SWAP, 2);
2940+
ADDOP_I(c, LOC(e), COPY, 2);
2941+
ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, i));
2942+
ADDOP_JUMP(c, LOC(e), POP_JUMP_IF_FALSE, cleanup);
29392943
}
29402944
VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
2941-
ADDOP_COMPARE(c, *ploc, asdl_seq_GET(e->v.Compare.ops, n));
2942-
ADDOP_JUMP(c, *ploc, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2945+
ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, n));
2946+
ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
29432947
NEW_JUMP_TARGET_LABEL(c, end);
29442948
ADDOP_JUMP(c, NO_LOCATION, JUMP, end);
29452949

29462950
USE_LABEL(c, cleanup);
2947-
ADDOP(c, *ploc, POP_TOP);
2951+
ADDOP(c, LOC(e), POP_TOP);
29482952
if (!cond) {
29492953
ADDOP_JUMP(c, NO_LOCATION, JUMP, next);
29502954
}
@@ -2973,8 +2977,7 @@ compiler_ifexp(struct compiler *c, expr_ty e)
29732977
NEW_JUMP_TARGET_LABEL(c, end);
29742978
NEW_JUMP_TARGET_LABEL(c, next);
29752979

2976-
location loc = LOC(e);
2977-
if (!compiler_jump_if(c, &loc, e->v.IfExp.test, next, 0)) {
2980+
if (!compiler_jump_if(c, LOC(e), e->v.IfExp.test, next, 0)) {
29782981
return 0;
29792982
}
29802983
VISIT(c, expr, e->v.IfExp.body);
@@ -3059,8 +3062,7 @@ compiler_if(struct compiler *c, stmt_ty s)
30593062
else {
30603063
next = end;
30613064
}
3062-
location loc = LOC(s);
3063-
if (!compiler_jump_if(c, &loc, s->v.If.test, next, 0)) {
3065+
if (!compiler_jump_if(c, LOC(s), s->v.If.test, next, 0)) {
30643066
return 0;
30653067
}
30663068
VISIT_SEQ(c, stmt, s->v.If.body);
@@ -3167,25 +3169,22 @@ compiler_async_for(struct compiler *c, stmt_ty s)
31673169
static int
31683170
compiler_while(struct compiler *c, stmt_ty s)
31693171
{
3170-
location loc = LOC(s);
31713172
NEW_JUMP_TARGET_LABEL(c, loop);
31723173
NEW_JUMP_TARGET_LABEL(c, body);
31733174
NEW_JUMP_TARGET_LABEL(c, end);
31743175
NEW_JUMP_TARGET_LABEL(c, anchor);
31753176

31763177
USE_LABEL(c, loop);
3177-
if (!compiler_push_fblock(c, loc, WHILE_LOOP, loop, end, NULL)) {
3178+
if (!compiler_push_fblock(c, LOC(s), WHILE_LOOP, loop, end, NULL)) {
31783179
return 0;
31793180
}
3180-
if (!compiler_jump_if(c, &loc, s->v.While.test, anchor, 0)) {
3181+
if (!compiler_jump_if(c, LOC(s), s->v.While.test, anchor, 0)) {
31813182
return 0;
31823183
}
31833184

31843185
USE_LABEL(c, body);
31853186
VISIT_SEQ(c, stmt, s->v.While.body);
3186-
SET_LOC(c, s);
3187-
loc = LOC(s);
3188-
if (!compiler_jump_if(c, &loc, s->v.While.test, body, 1)) {
3187+
if (!compiler_jump_if(c, LOC(s), s->v.While.test, body, 1)) {
31893188
return 0;
31903189
}
31913190

@@ -3986,8 +3985,7 @@ compiler_assert(struct compiler *c, stmt_ty s)
39863985
return 1;
39873986
}
39883987
NEW_JUMP_TARGET_LABEL(c, end);
3989-
location loc = LOC(s);
3990-
if (!compiler_jump_if(c, &loc, s->v.Assert.test, end, 1)) {
3988+
if (!compiler_jump_if(c, LOC(s), s->v.Assert.test, end, 1)) {
39913989
return 0;
39923990
}
39933991
ADDOP(c, LOC(s), LOAD_ASSERTION_ERROR);
@@ -4017,18 +4015,13 @@ compiler_stmt_expr(struct compiler *c, location loc, expr_ty value)
40174015
}
40184016

40194017
VISIT(c, expr, value);
4020-
/* Mark POP_TOP as artificial */
4021-
UNSET_LOC(c);
4022-
ADDOP(c, NO_LOCATION, POP_TOP);
4018+
ADDOP(c, NO_LOCATION, POP_TOP); /* artificial */
40234019
return 1;
40244020
}
40254021

40264022
static int
40274023
compiler_visit_stmt(struct compiler *c, stmt_ty s)
40284024
{
4029-
Py_ssize_t i, n;
4030-
/* Always assign a lineno to the next instruction for a stmt. */
4031-
SET_LOC(c, s);
40324025

40334026
switch (s->kind) {
40344027
case FunctionDef_kind:
@@ -4042,12 +4035,11 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
40424035
break;
40434036
case Assign_kind:
40444037
{
4045-
n = asdl_seq_LEN(s->v.Assign.targets);
4038+
Py_ssize_t n = asdl_seq_LEN(s->v.Assign.targets);
40464039
VISIT(c, expr, s->v.Assign.value);
4047-
location loc = LOC(s);
4048-
for (i = 0; i < n; i++) {
4040+
for (Py_ssize_t i = 0; i < n; i++) {
40494041
if (i < n - 1) {
4050-
ADDOP_I(c, loc, COPY, 1);
4042+
ADDOP_I(c, LOC(s), COPY, 1);
40514043
}
40524044
VISIT(c, expr,
40534045
(expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
@@ -4068,7 +4060,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
40684060
return compiler_match(c, s);
40694061
case Raise_kind:
40704062
{
4071-
n = 0;
4063+
Py_ssize_t n = 0;
40724064
if (s->v.Raise.exc) {
40734065
VISIT(c, expr, s->v.Raise.exc);
40744066
n++;
@@ -4077,8 +4069,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
40774069
n++;
40784070
}
40794071
}
4080-
location loc = LOC(s);
4081-
ADDOP_I(c, loc, RAISE_VARARGS, (int)n);
4072+
ADDOP_I(c, LOC(s), RAISE_VARARGS, (int)n);
40824073
break;
40834074
}
40844075
case Try_kind:
@@ -4096,24 +4087,20 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
40964087
break;
40974088
case Expr_kind:
40984089
{
4099-
location loc = LOC(s);
4100-
return compiler_stmt_expr(c, loc, s->v.Expr.value);
4090+
return compiler_stmt_expr(c, LOC(s), s->v.Expr.value);
41014091
}
41024092
case Pass_kind:
41034093
{
4104-
location loc = LOC(s);
4105-
ADDOP(c, loc, NOP);
4094+
ADDOP(c, LOC(s), NOP);
41064095
break;
41074096
}
41084097
case Break_kind:
41094098
{
4110-
location loc = LOC(s);
4111-
return compiler_break(c, loc);
4099+
return compiler_break(c, LOC(s));
41124100
}
41134101
case Continue_kind:
41144102
{
4115-
location loc = LOC(s);
4116-
return compiler_continue(c, loc);
4103+
return compiler_continue(c, LOC(s));
41174104
}
41184105
case With_kind:
41194106
return compiler_with(c, s, 0);
@@ -5275,7 +5262,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,
52755262
Py_ssize_t n = asdl_seq_LEN(gen->ifs);
52765263
for (Py_ssize_t i = 0; i < n; i++) {
52775264
expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
5278-
if (!compiler_jump_if(c, &loc, e, if_cleanup, 0)) {
5265+
if (!compiler_jump_if(c, loc, e, if_cleanup, 0)) {
52795266
return 0;
52805267
}
52815268
}
@@ -5374,7 +5361,7 @@ compiler_async_comprehension_generator(struct compiler *c, location loc,
53745361
Py_ssize_t n = asdl_seq_LEN(gen->ifs);
53755362
for (Py_ssize_t i = 0; i < n; i++) {
53765363
expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
5377-
if (!compiler_jump_if(c, &loc, e, if_cleanup, 0)) {
5364+
if (!compiler_jump_if(c, loc, e, if_cleanup, 0)) {
53785365
return 0;
53795366
}
53805367
}
@@ -7109,7 +7096,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
71097096
// NOTE: Returning macros are safe again.
71107097
if (m->guard) {
71117098
RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0));
7112-
RETURN_IF_FALSE(compiler_jump_if(c, &loc, m->guard, pc->fail_pop[0], 0));
7099+
RETURN_IF_FALSE(compiler_jump_if(c, loc, m->guard, pc->fail_pop[0], 0));
71137100
}
71147101
// Success! Pop the subject off, we're done with it:
71157102
if (i != cases - has_default - 1) {
@@ -7138,7 +7125,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
71387125
ADDOP(c, loc, NOP);
71397126
}
71407127
if (m->guard) {
7141-
RETURN_IF_FALSE(compiler_jump_if(c, &loc, m->guard, end, 0));
7128+
RETURN_IF_FALSE(compiler_jump_if(c, loc, m->guard, end, 0));
71427129
}
71437130
VISIT_SEQ(c, stmt, m->body);
71447131
}

0 commit comments

Comments
 (0)