Skip to content

Commit 0cf4c31

Browse files
Update for_helpers.py
1 parent 090a440 commit 0cf4c31

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

mypyc/irbuild/for_helpers.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -443,24 +443,28 @@ def make_for_loop_generator(
443443
target_type = builder.get_sequence_type(expr)
444444
for_list = ForSequence(builder, index, body_block, loop_exit, line, nested)
445445

446-
if (
447-
isinstance(expr, IndexExpr)
448-
and isinstance(expr.index, SliceExpr)
449-
and all(
446+
if isinstance(expr, IndexExpr) and isinstance(expr.index, SliceExpr):
447+
# TODO: maybe we must not apply this optimization to list type specifically
448+
# because the need to check length changes at each iteration?
449+
start = expr.index.start
450+
stop = expr.index.stop
451+
step = expr.index.step
452+
453+
if all(
450454
s is None or isinstance(constant_fold_expr(builder, s), int)
451-
for s in (expr.index.start, expr.index.stop, expr.index.step)
452-
)
453-
):
454-
for_list.init(
455-
builder.accept(expr.base),
456-
target_type,
457-
reverse=False,
458-
start=constant_fold_expr(builder, expr.index.start),
459-
stop=constant_fold_expr(builder, expr.index.stop),
460-
step=constant_fold_expr(builder, expr.index.step),
461-
)
462-
else:
463-
for_list.init(builder.accept(expr), target_type, reverse=False)
455+
for s in (start, stop, step)
456+
):
457+
for_list.init(
458+
builder.accept(expr.base),
459+
target_type,
460+
reverse=False,
461+
start=constant_fold_expr(builder, start),
462+
stop=constant_fold_expr(builder, stop),
463+
step=constant_fold_expr(builder, step),
464+
)
465+
return for_list
466+
467+
for_list.init(builder.accept(expr), target_type, reverse=False)
464468
return for_list
465469

466470
if is_dict_rprimitive(rtyp):

0 commit comments

Comments
 (0)