2929 TypeAlias ,
3030 Var ,
3131)
32+ from mypy .types import LiteralType , TupleType , get_proper_type , get_proper_types
3233from mypyc .ir .ops import (
3334 ERR_NEVER ,
3435 BasicBlock ,
3738 IntOp ,
3839 LoadAddress ,
3940 LoadErrorValue ,
41+ LoadLiteral ,
4042 LoadMem ,
4143 MethodCall ,
4244 RaiseStandardError ,
@@ -171,7 +173,7 @@ def for_loop_helper_with_index(
171173 body_insts: a function that generates the body of the loop.
172174 It needs a index as parameter.
173175 """
174- assert is_sequence_rprimitive (expr_reg .type )
176+ assert is_sequence_rprimitive (expr_reg .type ), ( expr_reg , expr_reg . type )
175177 target_type = builder .get_sequence_type (expr )
176178
177179 body_block = BasicBlock ()
@@ -218,10 +220,9 @@ def sequence_from_generator_preallocate_helper(
218220 there is no condition list in the generator and only one original sequence with
219221 one index is allowed.
220222
221- e.g. (1) tuple(f(x) for x in a_list/a_tuple/a_str/a_bytes)
222- (2) list(f(x) for x in a_list/a_tuple/a_str/a_bytes)
223- (3) [f(x) for x in a_list/a_tuple/a_str/a_bytes]
224- RTuple as an original sequence is not supported yet.
223+ e.g. (1) tuple(f(x) for x in a_list/a_tuple/a_str/a_bytes/an_rtuple)
224+ (2) list(f(x) for x in a_list/a_tuple/a_str/a_bytes/an_rtuple)
225+ (3) [f(x) for x in a_list/a_tuple/a_str/a_bytes/an_rtuple]
225226
226227 Args:
227228 empty_op_llbuilder: A function that can generate an empty sequence op when
@@ -236,23 +237,41 @@ def sequence_from_generator_preallocate_helper(
236237 implementation.
237238 """
238239 if len (gen .sequences ) == 1 and len (gen .indices ) == 1 and len (gen .condlists [0 ]) == 0 :
239- rtype = builder .node_type (gen .sequences [0 ])
240- if is_sequence_rprimitive (rtype ):
241- sequence = builder .accept (gen .sequences [0 ])
242- length = get_expr_length_value (
243- builder , gen .sequences [0 ], sequence , gen .line , use_pyssize_t = True
244- )
245- target_op = empty_op_llbuilder (length , gen .line )
246-
247- def set_item (item_index : Value ) -> None :
248- e = builder .accept (gen .left_expr )
249- builder .call_c (set_item_op , [target_op , item_index , e ], gen .line )
250-
251- for_loop_helper_with_index (
252- builder , gen .indices [0 ], gen .sequences [0 ], sequence , set_item , gen .line , length
253- )
240+ line = gen .line
241+ sequence_expr = gen .sequences [0 ]
242+ rtype = builder .node_type (sequence_expr )
243+ if not (is_sequence_rprimitive (rtype ) or isinstance (rtype , RTuple )):
244+ return None
245+ sequence = builder .accept (sequence_expr )
246+ length = get_expr_length_value (builder , sequence_expr , sequence , line , use_pyssize_t = True )
247+ if isinstance (rtype , RTuple ):
248+ # If input is RTuple, box it to tuple_rprimitive for generic iteration
249+ # TODO: this can be optimized a bit better with an unrolled ForRTuple helper
250+ proper_type = get_proper_type (builder .types [sequence_expr ])
251+ assert isinstance (proper_type , TupleType ), proper_type
252+
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+ items = list (map (builder .add , get_item_ops ))
262+ sequence = builder .new_tuple (items , line )
263+
264+ target_op = empty_op_llbuilder (length , line )
265+
266+ def set_item (item_index : Value ) -> None :
267+ e = builder .accept (gen .left_expr )
268+ builder .call_c (set_item_op , [target_op , item_index , e ], line )
269+
270+ for_loop_helper_with_index (
271+ builder , gen .indices [0 ], sequence_expr , sequence , set_item , line , length
272+ )
254273
255- return target_op
274+ return target_op
256275 return None
257276
258277
@@ -805,7 +824,7 @@ class ForSequence(ForGenerator):
805824 def init (
806825 self , expr_reg : Value , target_type : RType , reverse : bool , length : Value | None = None
807826 ) -> None :
808- assert is_sequence_rprimitive (expr_reg .type ), expr_reg
827+ assert is_sequence_rprimitive (expr_reg .type ), ( expr_reg , expr_reg . type )
809828 builder = self .builder
810829 # Record a Value indicating the length of the sequence, if known at compile time.
811830 self .length = length
@@ -835,7 +854,6 @@ def init(
835854 def gen_condition (self ) -> None :
836855 builder = self .builder
837856 line = self .line
838- # TODO: Don't reload the length each time when iterating an immutable sequence?
839857 if self .reverse :
840858 # If we are iterating in reverse order, we obviously need
841859 # to check that the index is still positive. Somewhat less
@@ -1248,7 +1266,7 @@ def get_expr_length_value(
12481266 builder : IRBuilder , expr : Expression , expr_reg : Value , line : int , use_pyssize_t : bool
12491267) -> Value :
12501268 rtype = builder .node_type (expr )
1251- assert is_sequence_rprimitive (rtype ), rtype
1269+ assert is_sequence_rprimitive (rtype ) or isinstance ( rtype , RTuple ) , rtype
12521270 length = get_expr_length (expr )
12531271 if length is None :
12541272 # We cannot compute the length at compile time, so we will fetch it.
0 commit comments