@@ -1419,22 +1419,29 @@ def call_zip(self, tx: "InstructionTranslator", *args, **kwargs):
1419
1419
]
1420
1420
return variables .ZipVariable (args , strict = strict , mutable_local = MutableLocal ())
1421
1421
1422
- def call_enumerate (self , tx : "InstructionTranslator" , * args ):
1423
- if len (args ) == 1 :
1422
+ _call_enumerate_polyfill = _polyfill_call_impl ("enumerate" )
1423
+
1424
+ def call_enumerate (self , tx : "InstructionTranslator" , iterable , start = _SENTINEL ):
1425
+ if start is self ._SENTINEL :
1424
1426
start = 0
1425
1427
else :
1426
- assert len (args ) == 2
1427
- assert isinstance (args [1 ], variables .ConstantVariable )
1428
- start = args [1 ].as_python_constant ()
1429
- if args [0 ].has_unpack_var_sequence (tx ):
1430
- items = [
1431
- variables .TupleVariable (
1432
- [variables .ConstantVariable .create (idx ), var ],
1433
- )
1434
- for idx , var in enumerate (args [0 ].unpack_var_sequence (tx ), start )
1435
- ]
1436
- return variables .TupleVariable (items )
1437
- # could have an iterable version
1428
+ assert isinstance (start , variables .ConstantVariable )
1429
+ start = start .as_python_constant ()
1430
+
1431
+ if iterable .has_unpack_var_sequence (tx ):
1432
+ return variables .EnumerateVariable (
1433
+ iterable .unpack_var_sequence (tx ),
1434
+ start ,
1435
+ mutable_local = MutableLocal (),
1436
+ )
1437
+ elif isinstance (iterable , variables .IteratorVariable ):
1438
+ return variables .EnumerateVariable (
1439
+ iterable , start , mutable_local = MutableLocal ()
1440
+ )
1441
+
1442
+ return self ._call_enumerate_polyfill (
1443
+ tx , iterable , variables .ConstantVariable .create (start )
1444
+ )
1438
1445
1439
1446
def call_len (self , tx : "InstructionTranslator" , * args , ** kwargs ):
1440
1447
return args [0 ].call_method (tx , "__len__" , args [1 :], kwargs )
0 commit comments