Skip to content

Commit 6ebce90

Browse files
Update for_helpers.py
1 parent 379048a commit 6ebce90

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

mypyc/irbuild/for_helpers.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,24 +242,40 @@ def sequence_from_generator_preallocate_helper(
242242
rtype = builder.node_type(sequence_expr)
243243
if not (is_sequence_rprimitive(rtype) or isinstance(rtype, RTuple)):
244244
return None
245-
sequence = builder.accept(sequence_expr)
246-
length = get_expr_length_value(builder, sequence_expr, sequence, line, use_pyssize_t=True)
245+
247246
if isinstance(rtype, RTuple):
248247
# If input is RTuple, box it to tuple_rprimitive for generic iteration
249248
# TODO: this can be optimized a bit better with an unrolled ForRTuple helper
250249
proper_type = get_proper_type(builder.types[sequence_expr])
251250
assert isinstance(proper_type, TupleType), proper_type
251+
252+
# the for_loop_helper_with_index crashes for empty tuples, bail out
253+
if not proper_type.items:
254+
return None
255+
256+
proper_types = get_proper_types(proper_type.items)
257+
258+
if all(isinstance(typ, LiteralType) for typ in proper_types):
259+
get_item_ops = [LoadLiteral(typ.value, object_rprimitive) for typ in proper_types]
252260

253-
get_item_ops = [
254-
(
255-
LoadLiteral(typ.value, object_rprimitive)
256-
if isinstance(typ, LiteralType)
257-
else TupleGet(sequence, i, line)
258-
)
259-
for i, typ in enumerate(get_proper_types(proper_type.items))
260-
]
261+
else:
262+
sequence = builder.accept(sequence_expr)
263+
get_item_ops = [
264+
(
265+
LoadLiteral(typ.value, object_rprimitive)
266+
if isinstance(typ, LiteralType)
267+
else TupleGet(sequence, i, line)
268+
)
269+
for i, typ in enumerate(proper_types)
270+
]
271+
261272
items = list(map(builder.add, get_item_ops))
262273
sequence = builder.new_tuple(items, line)
274+
275+
else:
276+
sequence = builder.accept(sequence_expr)
277+
278+
length = get_expr_length_value(builder, sequence_expr, sequence, line, use_pyssize_t=True)
263279

264280
target_op = empty_op_llbuilder(length, line)
265281

0 commit comments

Comments
 (0)