@@ -2,6 +2,7 @@ pub(crate) use decl::make_module;
2
2
3
3
#[ pymodule( name = "itertools" ) ]
4
4
mod decl {
5
+ use crate :: stdlib:: itertools:: decl:: int:: get_value;
5
6
use crate :: {
6
7
builtins:: {
7
8
int, tuple:: IntoPyTuple , PyGenericAlias , PyInt , PyIntRef , PyList , PyTuple , PyTupleRef ,
@@ -110,7 +111,9 @@ mod decl {
110
111
Ok ( ( ) )
111
112
}
112
113
}
114
+
113
115
impl SelfIter for PyItertoolsChain { }
116
+
114
117
impl IterNext for PyItertoolsChain {
115
118
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
116
119
let Some ( source) = zelf. source . read ( ) . clone ( ) else {
@@ -201,6 +204,7 @@ mod decl {
201
204
}
202
205
203
206
impl SelfIter for PyItertoolsCompress { }
207
+
204
208
impl IterNext for PyItertoolsCompress {
205
209
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
206
210
loop {
@@ -268,7 +272,9 @@ mod decl {
268
272
( zelf. class ( ) . to_owned ( ) , ( zelf. cur . read ( ) . clone ( ) , ) )
269
273
}
270
274
}
275
+
271
276
impl SelfIter for PyItertoolsCount { }
277
+
272
278
impl IterNext for PyItertoolsCount {
273
279
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
274
280
let mut cur = zelf. cur . write ( ) ;
@@ -316,7 +322,9 @@ mod decl {
316
322
317
323
#[ pyclass( with( IterNext , Iterable , Constructor ) , flags( BASETYPE ) ) ]
318
324
impl PyItertoolsCycle { }
325
+
319
326
impl SelfIter for PyItertoolsCycle { }
327
+
320
328
impl IterNext for PyItertoolsCycle {
321
329
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
322
330
let item = if let PyIterReturn :: Return ( item) = zelf. iter . next ( vm) ? {
@@ -401,6 +409,7 @@ mod decl {
401
409
}
402
410
403
411
impl SelfIter for PyItertoolsRepeat { }
412
+
404
413
impl IterNext for PyItertoolsRepeat {
405
414
fn next ( zelf : & Py < Self > , _vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
406
415
if let Some ( ref times) = zelf. times {
@@ -466,7 +475,9 @@ mod decl {
466
475
)
467
476
}
468
477
}
478
+
469
479
impl SelfIter for PyItertoolsStarmap { }
480
+
470
481
impl IterNext for PyItertoolsStarmap {
471
482
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
472
483
let obj = zelf. iterable . next ( vm) ?;
@@ -537,7 +548,9 @@ mod decl {
537
548
Ok ( ( ) )
538
549
}
539
550
}
551
+
540
552
impl SelfIter for PyItertoolsTakewhile { }
553
+
541
554
impl IterNext for PyItertoolsTakewhile {
542
555
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
543
556
if zelf. stop_flag . load ( ) {
@@ -618,7 +631,9 @@ mod decl {
618
631
Ok ( ( ) )
619
632
}
620
633
}
634
+
621
635
impl SelfIter for PyItertoolsDropwhile { }
636
+
622
637
impl IterNext for PyItertoolsDropwhile {
623
638
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
624
639
let predicate = & zelf. predicate ;
@@ -629,7 +644,7 @@ mod decl {
629
644
let obj = match iterable. next ( vm) ? {
630
645
PyIterReturn :: Return ( obj) => obj,
631
646
PyIterReturn :: StopIteration ( v) => {
632
- return Ok ( PyIterReturn :: StopIteration ( v) )
647
+ return Ok ( PyIterReturn :: StopIteration ( v) ) ;
633
648
}
634
649
} ;
635
650
let pred = predicate. clone ( ) ;
@@ -737,7 +752,9 @@ mod decl {
737
752
Ok ( PyIterReturn :: Return ( ( new_value, new_key) ) )
738
753
}
739
754
}
755
+
740
756
impl SelfIter for PyItertoolsGroupBy { }
757
+
741
758
impl IterNext for PyItertoolsGroupBy {
742
759
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
743
760
let mut state = zelf. state . lock ( ) ;
@@ -753,7 +770,7 @@ mod decl {
753
770
let ( value, new_key) = match zelf. advance ( vm) ? {
754
771
PyIterReturn :: Return ( obj) => obj,
755
772
PyIterReturn :: StopIteration ( v) => {
756
- return Ok ( PyIterReturn :: StopIteration ( v) )
773
+ return Ok ( PyIterReturn :: StopIteration ( v) ) ;
757
774
}
758
775
} ;
759
776
if !vm. bool_eq ( & new_key, & old_key) ? {
@@ -764,7 +781,7 @@ mod decl {
764
781
match zelf. advance ( vm) ? {
765
782
PyIterReturn :: Return ( obj) => obj,
766
783
PyIterReturn :: StopIteration ( v) => {
767
- return Ok ( PyIterReturn :: StopIteration ( v) )
784
+ return Ok ( PyIterReturn :: StopIteration ( v) ) ;
768
785
}
769
786
}
770
787
} ;
@@ -797,7 +814,9 @@ mod decl {
797
814
798
815
#[ pyclass( with( IterNext , Iterable ) ) ]
799
816
impl PyItertoolsGrouper { }
817
+
800
818
impl SelfIter for PyItertoolsGrouper { }
819
+
801
820
impl IterNext for PyItertoolsGrouper {
802
821
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
803
822
let old_key = {
@@ -960,6 +979,7 @@ mod decl {
960
979
}
961
980
962
981
impl SelfIter for PyItertoolsIslice { }
982
+
963
983
impl IterNext for PyItertoolsIslice {
964
984
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
965
985
while zelf. cur . load ( ) < zelf. next . load ( ) {
@@ -1033,7 +1053,9 @@ mod decl {
1033
1053
)
1034
1054
}
1035
1055
}
1056
+
1036
1057
impl SelfIter for PyItertoolsFilterFalse { }
1058
+
1037
1059
impl IterNext for PyItertoolsFilterFalse {
1038
1060
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
1039
1061
let predicate = & zelf. predicate ;
@@ -1142,6 +1164,7 @@ mod decl {
1142
1164
}
1143
1165
1144
1166
impl SelfIter for PyItertoolsAccumulate { }
1167
+
1145
1168
impl IterNext for PyItertoolsAccumulate {
1146
1169
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
1147
1170
let iterable = & zelf. iterable ;
@@ -1153,7 +1176,7 @@ mod decl {
1153
1176
None => match iterable. next ( vm) ? {
1154
1177
PyIterReturn :: Return ( obj) => obj,
1155
1178
PyIterReturn :: StopIteration ( v) => {
1156
- return Ok ( PyIterReturn :: StopIteration ( v) )
1179
+ return Ok ( PyIterReturn :: StopIteration ( v) ) ;
1157
1180
}
1158
1181
} ,
1159
1182
Some ( obj) => obj. clone ( ) ,
@@ -1162,7 +1185,7 @@ mod decl {
1162
1185
let obj = match iterable. next ( vm) ? {
1163
1186
PyIterReturn :: Return ( obj) => obj,
1164
1187
PyIterReturn :: StopIteration ( v) => {
1165
- return Ok ( PyIterReturn :: StopIteration ( v) )
1188
+ return Ok ( PyIterReturn :: StopIteration ( v) ) ;
1166
1189
}
1167
1190
} ;
1168
1191
match & zelf. binop {
@@ -1348,7 +1371,60 @@ mod decl {
1348
1371
self . cur . store ( idxs. len ( ) - 1 ) ;
1349
1372
}
1350
1373
}
1374
+
1375
+ #[ pymethod( magic) ]
1376
+ fn setstate ( zelf : PyRef < Self > , state : PyTupleRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
1377
+ let args = state. as_slice ( ) ;
1378
+ if args. len ( ) != zelf. pools . len ( ) {
1379
+ let msg = "Invalid number of arguments" . to_string ( ) ;
1380
+ return Err ( vm. new_type_error ( msg) ) ;
1381
+ }
1382
+ let mut idxs: PyRwLockWriteGuard < ' _ , Vec < usize > > = zelf. idxs . write ( ) ;
1383
+ idxs. clear ( ) ;
1384
+ for s in 0 ..args. len ( ) {
1385
+ let index = get_value ( state. get ( s) . unwrap ( ) ) . to_usize ( ) . unwrap ( ) ;
1386
+ let pool_size = zelf. pools . get ( s) . unwrap ( ) . len ( ) ;
1387
+ if pool_size == 0 {
1388
+ zelf. stop . store ( true ) ;
1389
+ return Ok ( ( ) ) ;
1390
+ }
1391
+ if index >= pool_size {
1392
+ idxs. push ( pool_size - 1 ) ;
1393
+ } else {
1394
+ idxs. push ( index) ;
1395
+ }
1396
+ }
1397
+ zelf. stop . store ( false ) ;
1398
+ Ok ( ( ) )
1399
+ }
1400
+
1401
+ #[ pymethod( magic) ]
1402
+ fn reduce ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyTupleRef {
1403
+ let class = zelf. class ( ) . to_owned ( ) ;
1404
+
1405
+ if zelf. stop . load ( ) {
1406
+ return vm. new_tuple ( ( class, ( vm. ctx . empty_tuple . clone ( ) , ) ) ) ;
1407
+ }
1408
+
1409
+ let mut pools: Vec < PyObjectRef > = Vec :: new ( ) ;
1410
+ for element in zelf. pools . iter ( ) {
1411
+ pools. push ( element. clone ( ) . into_pytuple ( vm) . into ( ) ) ;
1412
+ }
1413
+
1414
+ let mut indices: Vec < PyObjectRef > = Vec :: new ( ) ;
1415
+
1416
+ for item in & zelf. idxs . read ( ) [ ..] {
1417
+ indices. push ( vm. new_pyobj ( * item) ) ;
1418
+ }
1419
+
1420
+ vm. new_tuple ( (
1421
+ class,
1422
+ pools. clone ( ) . into_pytuple ( vm) ,
1423
+ indices. into_pytuple ( vm) ,
1424
+ ) )
1425
+ }
1351
1426
}
1427
+
1352
1428
impl SelfIter for PyItertoolsProduct { }
1353
1429
impl IterNext for PyItertoolsProduct {
1354
1430
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
@@ -1563,6 +1639,7 @@ mod decl {
1563
1639
impl PyItertoolsCombinationsWithReplacement { }
1564
1640
1565
1641
impl SelfIter for PyItertoolsCombinationsWithReplacement { }
1642
+
1566
1643
impl IterNext for PyItertoolsCombinationsWithReplacement {
1567
1644
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
1568
1645
// stop signal
@@ -1679,7 +1756,9 @@ mod decl {
1679
1756
) )
1680
1757
}
1681
1758
}
1759
+
1682
1760
impl SelfIter for PyItertoolsPermutations { }
1761
+
1683
1762
impl IterNext for PyItertoolsPermutations {
1684
1763
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
1685
1764
// stop signal
@@ -1802,7 +1881,9 @@ mod decl {
1802
1881
Ok ( ( ) )
1803
1882
}
1804
1883
}
1884
+
1805
1885
impl SelfIter for PyItertoolsZipLongest { }
1886
+
1806
1887
impl IterNext for PyItertoolsZipLongest {
1807
1888
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
1808
1889
if zelf. iterators . is_empty ( ) {
@@ -1851,7 +1932,9 @@ mod decl {
1851
1932
1852
1933
#[ pyclass( with( IterNext , Iterable , Constructor ) ) ]
1853
1934
impl PyItertoolsPairwise { }
1935
+
1854
1936
impl SelfIter for PyItertoolsPairwise { }
1937
+
1855
1938
impl IterNext for PyItertoolsPairwise {
1856
1939
fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
1857
1940
let old = match zelf. old . read ( ) . clone ( ) {
0 commit comments