8000 gh-98461: Fix location of RETURN_VALUE in async generator bytecode. c… · iritkatriel/cpython@ae7c59d · GitHub
[go: up one dir, main page]

Skip to content

Commit ae7c59d

Browse files
committed
pythongh-98461: Fix location of RETURN_VALUE in async generator bytecode. compiler_jump_if no longer needs a pointer to the loc
1 parent 4ec9ed8 commit ae7c59d

File tree

2 files changed

+44
-57
lines changed

2 files changed

+44
-57
lines changed

Lib/test/test_compile.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,13 +2864,14 @@ static int compiler_addcompare(struct compiler *c, location loc,
28642864

28652865

28662866
static int
2867-
compiler_jump_if(struct compiler *c, location *ploc,
2867+
compiler_jump_if(struct compiler *c, location loc,
28682868
expr_ty e, jump_target_label next, int cond)
28692869
{
28702870
switch (e->kind) {
28712871
case UnaryOp_kind:
2872-
if (e->v.UnaryOp.op == Not)
2873-
return compiler_jump_if(c, ploc, e->v.UnaryOp.operand, next, !cond);
2872+
if (e->v.UnaryOp.op == Not) {
2873+
return compiler_jump_if(c, loc, e->v.UnaryOp.operand, next, !cond);
2874+
}
28742875
/* fallback to general implementation */
28752876
break;
28762877
case BoolOp_kind: {
@@ -2884,11 +2885,13 @@ compiler_jump_if(struct compiler *c, location *ploc,
28842885
next2 = new_next2;
28852886
}
28862887
for (i = 0; i < n; ++i) {
2887-
if (!compiler_jump_if(c, ploc, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2888+
if (!compiler_jump_if(c, loc, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) {
28882889
return 0;
2890+
}
28892891
}
2890-
if (!compiler_jump_if(c, ploc, (expr_ty)asdl_seq_GET(s, n), next, cond))
2892+
if (!compiler_jump_if(c, loc, (expr_ty)asdl_seq_GET(s, n), next, cond)) {
28912893
return 0;
2894+
}
28922895
if (!SAME_LABEL(next2, next)) {
28932896
USE_LABEL(c, next2);
28942897
}
@@ -2897,45 +2900,46 @@ compiler_jump_if(struct compiler *c, location *ploc,
28972900
case IfExp_kind: {
28982901
NEW_JUMP_TARGET_LABEL(c, end);
28992902
NEW_JUMP_TARGET_LABEL(c, next2);
2900-
if (!compiler_jump_if(c, ploc, e->v.IfExp.test, next2, 0))
2903+
if (!compiler_jump_if(c, loc, e->v.IfExp.test, next2, 0)) {
29012904
return 0;
2902-
if (!compiler_jump_if(c, ploc, e->v.IfExp.body, next, cond))
2905+
}
2906+
if (!compiler_jump_if(c, loc, e->v.IfExp.body, next, cond)) {
29032907
return 0;
2908+
}
29042909
ADDOP_JUMP(c, NO_LOCATION, JUMP, end);
29052910

29062911
USE_LABEL(c, next2);
2907-
if (!compiler_jump_if(c, ploc, e->v.IfExp.orelse, next, cond))
2912+
if (!compiler_jump_if(c, loc, e->v.IfExp.orelse, next, cond)) {
29082913
return 0;
2914+
}
29092915

29102916
USE_LABEL(c, end);
29112917
return 1;
29122918
}
29132919
case Compare_kind: {
2914-
SET_LOC(c, e);
2915-
*ploc = LOC(e);
2916-
Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2920+
Py_ssize_t n = asdl_seq_LEN(e->v.Compare.ops) - 1;
29172921
if (n > 0) {
29182922
if (!check_compare(c, e)) {
29192923
return 0;
29202924
}
29212925
NEW_JUMP_TARGET_LABEL(c, cleanup);
29222926
VISIT(c, expr, e->v.Compare.left);
2923-
for (i = 0; i < n; i++) {
2927+
for (Py_ssize_t i = 0; i < n; i++) {
29242928
VISIT(c, expr,
29252929
(expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2926-
ADDOP_I(c, *ploc, SWAP, 2);
2927-
ADDOP_I(c, *ploc, COPY, 2);
2928-
ADDOP_COMPARE(c, *ploc, asdl_seq_GET(e->v.Compare.ops, i));
2929-
ADDOP_JUMP(c, *ploc, POP_JUMP_IF_FALSE, cleanup);
2930+
ADDOP_I(c, LOC(e), SWAP, 2);
2931+
ADDOP_I(c, LOC(e), COPY, 2);
2932+
ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, i));
2933+
ADDOP_JUMP(c, LOC(e), POP_JUMP_IF_FALSE, cleanup);
29302934
}
29312935
VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
2932-
ADDOP_COMPARE(c, *ploc, asdl_seq_GET(e->v.Compare.ops, n));
2933-
ADDOP_JUMP(c, *ploc, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2936+
ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, n));
2937+
ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
29342938
NEW_JUMP_TARGET_LABEL(c, end);
29352939
ADDOP_JUMP(c, NO_LOCATION, JUMP, end);
29362940

29372941
USE_LABEL(c, cleanup);
2938-
ADDOP(c, *ploc, POP_TOP);
2942+
ADDOP(c, LOC(e), POP_TOP);
29392943
if (!cond) {
29402944
ADDOP_JUMP(c, NO_LOCATION, JUMP, next);
29412945
}
@@ -2964,8 +2968,7 @@ compiler_ifexp(struct compiler *c, expr_ty e)
29642968
NEW_JUMP_TARGET_LABEL(c, end);
29652969
NEW_JUMP_TARGET_LABEL(c, next);
29662970

2967-
location loc = LOC(e);
2968-
if (!compiler_jump_if(c, &loc, e->v.IfExp.test, next, 0)) {
2971+
if (!compiler_jump_if(c, LOC(e), e->v.IfExp.test, next, 0)) {
29692972
return 0;
29702973
}
29712974
VISIT(c, expr, e->v.IfExp.body);
@@ -3050,8 +3053,7 @@ compiler_if(struct compiler *c, stmt_ty s)
30503053
else {
30513054
next = end;
30523055
}
3053-
location loc = LOC(s);
3054-
if (!compiler_jump_if(c, &loc, s->v.If.test, next, 0)) {
3056+
if (!compiler_jump_if(c, LOC(s), s->v.If.test, next, 0)) {
30553057
return 0;
30563058
}
30573059
VISIT_SEQ(c, stmt, s->v.If.body);
@@ -3158,25 +3160,22 @@ compiler_async_for(struct compiler *c, stmt_ty s)
31583160
static int
31593161
compiler_while(struct compiler *c, stmt_ty s)
31603162
{
3161-
location loc = LOC(s);
31623163
NEW_JUMP_TARGET_LABEL(c, loop);
31633164
NEW_JUMP_TARGET_LABEL(c, body);
31643165
NEW_JUMP_TARGET_LABEL(c, end);
31653166
NEW_JUMP_TARGET_LABEL(c, anchor);
31663167

31673168
USE_LABEL(c, loop);
3168-
if (!compiler_push_fblock(c, loc, WHILE_LOOP, loop, end, NULL)) {
3169+
if (!compiler_push_fblock(c, LOC(s), WHILE_LOOP, loop, end, NULL)) {
31693170
return 0;
31703171
}
3171-
if (!compiler_jump_if(c, &loc, s->v.While.test, anchor, 0)) {
3172+
if (!compiler_jump_if(c, LOC(s), s->v.While.test, anchor, 0)) {
31723173
return 0;
31733174
}
31743175

31753176
USE_LABEL(c, body);
31763177
VISIT_SEQ(c, stmt, s->v.While.body);
3177-
SET_LOC(c, s);
3178-
loc = LOC(s);
3179-
if (!compiler_jump_if(c, &loc, s->v.While.test, body, 1)) {
3178+
if (!compiler_jump_if(c, LOC(s), s->v.While.test, body, 1)) {
31803179
return 0;
31813180
}
31823181

@@ -3977,8 +3976,7 @@ compiler_assert(struct compiler *c, stmt_ty s)
39773976
return 1;
39783977
}
39793978
NEW_JUMP_TARGET_LABEL(c, end);
3980-
location loc = LOC(s);
3981-
if (!compiler_jump_if(c, &loc, s->v.Assert.test, end, 1)) {
3979+
if (!compiler_jump_if(c, LOC(s), s->v.Assert.test, end, 1)) {
39823980
return 0;
39833981
}
39843982
ADDOP(c, LOC(s), LOAD_ASSERTION_ERROR);
@@ -4008,18 +4006,13 @@ compiler_stmt_expr(struct compiler *c, location loc, expr_ty value)
40084006
}
40094007

40104008
VISIT(c, expr, value);
4011-
/* Mark POP_TOP as artificial */
4012-
UNSET_LOC(c);
4013-
ADDOP(c, NO_LOCATION, POP_TOP);
4009+
ADDOP(c, NO_LOCATION, POP_TOP); /* artificial */
40144010
return 1;
40154011
}
40164012

40174013
static int
40184014
compiler_visit_stmt(struct compiler *c, stmt_ty s)
40194015
{
4020-
Py_ssize_t i, n;
4021-
/* Always assign a lineno to the next instruction for a stmt. */
4022-
SET_LOC(c, s);
40234016

40244017
switch (s->kind) {
40254018
case FunctionDef_kind:
@@ -4033,12 +4026,11 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
40334026
break;
40344027
case Assign_kind:
40354028
{
4036-
n = asdl_seq_LEN(s->v.Assign.targets);
4029+
Py_ssize_t n = asdl_seq_LEN(s->v.Assign.targets);
40374030
VISIT(c, expr, s->v.Assign.value);
4038-
location loc = LOC(s);
4039-
for (i = 0; i < n; i++) {
4031+
for (Py_ssize_t i = 0; i < n; i++) {
40404032
if (i < n - 1) {
4041-
ADDOP_I(c, loc, COPY, 1);
4033+
ADDOP_I(c, LOC(s), COPY, 1);
40424034
}
40434035
VISIT(c, expr,
40444036
(expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
@@ -4059,7 +4051,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
40594051
return compiler_match(c, s);
40604052
case Raise_kind:
40614053
{
4062-
n = 0;
4054+
Py_ssize_t n = 0;
40634055
if (s->v.Raise.exc) {
40644056
VISIT(c, expr, s->v.Raise.exc);
40654057
n++;
@@ -4068,8 +4060,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
40684060
n++;
40694061
}
40704062
}
4071-
location loc = LOC(s);
4072-
ADDOP_I(c, loc, RAISE_VARARGS, (int)n);
4063+
ADDOP_I(c, LOC(s), RAISE_VARARGS, (int)n);
40734064
break;
40744065
}
40754066
case Try_kind:
@@ -4087,24 +4078,20 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
40874078
break;
40884079
case Expr_kind:
40894080
{
4090-
location loc = LOC(s);
4091-
return compiler_stmt_expr(c, loc, s->v.Expr.value);
4081+
return compiler_stmt_expr(c, LOC(s), s->v.Expr.value);
40924082
}
40934083
case Pass_kind:
40944084
{
4095-
location loc = LOC(s);
4096-
ADDOP(c, loc, NOP);
4085+
ADDOP(c, LOC(s), NOP);
40974086
break;
40984087
}
40994088
case Break_kind:
41004089
{
4101-
location loc = LOC(s);
4102-
return compiler_break(c, loc);
4090+
return compiler_break(c, LOC(s));
41034091
}
41044092
case Continue_kind:
41054093
{
4106-
location loc = LOC(s);
4107-
return compiler_continue(c, loc);
4094+
return compiler_continue(c, LOC(s));
41084095
}
41094096
case With_kind:
41104097
return compiler_with(c, s, 0);
@@ -5266,7 +5253,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,
52665253
Py_ssize_t n = asdl_seq_LEN(gen->ifs);
52675254
for (Py_ssize_t i = 0; i < n; i++) {
52685255
expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
5269-
if (!compiler_jump_if(c, &loc, e, if_cleanup, 0)) {
5256+
if (!compiler_jump_if(c, loc, e, if_cleanup, 0)) {
52705257
return 0;
52715258
}
52725259
}
@@ -5365,7 +5352,7 @@ compiler_async_comprehension_generator(struct compiler *c, location loc,
53655352
Py_ssize_t n = asdl_seq_LEN(gen->ifs);
53665353
for (Py_ssize_t i = 0; i < n; i++) {
53675354
expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
5368-
if (!compiler_jump_if(c, &loc, e, if_cleanup, 0)) {
5355+
if (!compiler_jump_if(c, loc, e, if_cleanup, 0)) {
53695356
return 0;
53705357
}
53715358
}
@@ -7100,7 +7087,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
71007087
// NOTE: Returning macros are safe again.
71017088
if (m->guard) {
71027089
RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0));
7103-
RETURN_IF_FALSE(compiler_jump_if(c, &loc, m->guard, pc->fail_pop[0], 0));
7090+
RETURN_IF_FALSE(compiler_jump_if(c, loc, m->guard, pc->fail_pop[0], 0));
71047091
}
71057092
// Success! Pop the subject off, we're done with it:
71067093
if (i != cases - has_default - 1) {
@@ -7129,7 +7116,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
71297116
ADDOP(c, loc, NOP);
71307117
}
71317118
if (m->guard) {
7132-
RETURN_IF_FALSE(compiler_jump_if(c, &loc, m->guard, end, 0));
7119+
RETURN_IF_FALSE(compiler_jump_if(c, loc, m->guard, end, 0));
71337120
}
71347121
VISIT_SEQ(c, stmt, m->body);
71357122
}

0 commit comments

Comments
 (0)
0