Skip to content

Commit 5d9ccf3

Browse files
committed
Use a more generic implementation.
1 parent cdbcfad commit 5d9ccf3

1 file changed

Lines changed: 20 additions & 21 deletions

File tree

Python/codegen.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ static int codegen_nameop(compiler *, location, identifier, expr_context_ty);
200200
static int codegen_visit_stmt(compiler *, stmt_ty);
201201
static int codegen_visit_keyword(compiler *, keyword_ty);
202202
static int codegen_visit_expr(compiler *, expr_ty);
203+
static int codegen_visit_unused_expr(compiler *, expr_ty);
203204
static int codegen_augassign(compiler *, stmt_ty);
204205
static int codegen_annassign(compiler *, stmt_ty);
205206
static int codegen_subscript(compiler *, expr_ty);
@@ -233,8 +234,6 @@ typedef enum {
233234
ITERATOR_ON_STACK = 2,
234235
} IterStackPosition;
235236

236-
static int
237-
codegen_listcomp_impl(compiler *c, expr_ty e, bool avoid_creation);
238237
static int codegen_sync_comprehension_generator(
239238
compiler *c, location loc,
240239
asdl_comprehension_seq *generators, int gen_index,
@@ -477,6 +476,9 @@ codegen_addop_j(instr_sequence *seq, location loc,
477476
} \
478477
} while (0)
479478

479+
#define VISIT_UNUSED(C, TYPE, V) \
480+
RETURN_IF_ERROR(codegen_visit_unused_ ## TYPE((C), (V)))
481+
480482
static int
481483
codegen_call_exit_with_nones(compiler *c, location loc)
482484
{
@@ -3086,13 +3088,7 @@ codegen_stmt_expr(compiler *c, location loc, expr_ty value)
30863088
return SUCCESS;
30873089
}
30883090

3089-
if (value->kind == ListComp_kind) {
3090-
/* Optimization: Don't bother creating structures if they won't be
3091-
* used. */
3092-
return codegen_listcomp_impl(c, value, /*avoid_creation=*/true);
3093-
}
3094-
3095-
VISIT(c, expr, value);
3091+
VISIT_UNUSED(c, expr, value);
30963092
ADDOP(c, NO_LOCATION, POP_TOP); /* artificial */
30973093
return SUCCESS;
30983094
}
@@ -5110,9 +5106,6 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
51105106
if (pop_inlined_comprehension_state(c, loc, &inline_state)) {
51115107
goto error;
51125108
}
5113-
if (avoid_creation) {
5114-
ADDOP(c, loc, POP_TOP);
5115-
}
51165109
return SUCCESS;
51175110
}
51185111

@@ -5174,7 +5167,7 @@ codegen_genexp(compiler *c, expr_ty e)
51745167
}
51755168

51765169
static int
5177-
codegen_listcomp_impl(compiler *c, expr_ty e, bool avoid_creation)
5170+
codegen_listcomp(compiler *c, expr_ty e, bool avoid_creation)
51785171
{
51795172
assert(e->kind == ListComp_kind);
51805173
_Py_DECLARE_STR(anon_listcomp, "<listcomp>");
@@ -5183,12 +5176,6 @@ codegen_listcomp_impl(compiler *c, expr_ty e, bool avoid_creation)
51835176
e->v.ListComp.elt, NULL, avoid_creation);
51845177
}
51855178

5186-
static int
5187-
codegen_listcomp(compiler *c, expr_ty e)
5188-
{
5189-
return codegen_listcomp_impl(c, e, false);
5190-
}
5191-
51925179
static int
51935180
codegen_setcomp(compiler *c, expr_ty e)
51945181
{
@@ -5455,7 +5442,7 @@ codegen_with(compiler *c, stmt_ty s)
54555442
}
54565443

54575444
static int
5458-
codegen_visit_expr(compiler *c, expr_ty e)
5445+
codegen_visit_expr_impl(compiler *c, expr_ty e, bool result_is_unused)
54595446
{
54605447
if (Py_EnterRecursiveCall(" during compilation")) {
54615448
return ERROR;
@@ -5498,7 +5485,7 @@ codegen_visit_expr(compiler *c, expr_ty e)
54985485
case GeneratorExp_kind:
54995486
return codegen_genexp(c, e);
55005487
case ListComp_kind:
5501-
return codegen_listcomp(c, e);
5488+
return codegen_listcomp(c, e, result_is_unused);
55025489
case SetComp_kind:
55035490
return codegen_setcomp(c, e);
55045491
case DictComp_kind:
@@ -5608,6 +5595,18 @@ codegen_visit_expr(compiler *c, expr_ty e)
56085595
return SUCCESS;
56095596
}
56105597

5598+
static int
5599+
codegen_visit_expr(compiler *c, expr_ty e)
5600+
{
5601+
return codegen_visit_expr_impl(c, e, false);
5602+
}
5603+
5604+
static int
5605+
codegen_visit_unused_expr(compiler *c, expr_ty e)
5606+
{
5607+
return codegen_visit_expr_impl(c, e, true);
5608+
}
5609+
56115610
static bool
56125611
is_constant_slice(expr_ty s)
56135612
{

0 commit comments

Comments
 (0)