-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
/
itertoolsmodule.c
3959 lines (3478 loc) · 107 KB
/
itertoolsmodule.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_typeobject.h" // _PyType_GetModuleState()
#include "pycore_object.h" // _PyObject_GC_TRACK()
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include <stddef.h> // offsetof()
/* Itertools module written and maintained
by Raymond D. Hettinger <python@rcn.com>
*/
typedef struct {
PyTypeObject *accumulate_type;
PyTypeObject *batched_type;
PyTypeObject *chain_type;
PyTypeObject *combinations_type;
PyTypeObject *compress_type;
PyTypeObject *count_type;
PyTypeObject *cwr_type;
PyTypeObject *cycle_type;
PyTypeObject *dropwhile_type;
PyTypeObject *filterfalse_type;
PyTypeObject *groupby_type;
PyTypeObject *_grouper_type;
PyTypeObject *islice_type;
PyTypeObject *pairwise_type;
PyTypeObject *permutations_type;
PyTypeObject *product_type;
PyTypeObject *repeat_type;
PyTypeObject *starmap_type;
PyTypeObject *takewhile_type;
PyTypeObject *tee_type;
PyTypeObject *teedataobject_type;
PyTypeObject *ziplongest_type;
} itertools_state;
static inline itertools_state *
get_module_state(PyObject *mod)
{
void *state = _PyModule_GetState(mod);
assert(state != NULL);
return (itertools_state *)state;
}
static inline itertools_state *
get_module_state_by_cls(PyTypeObject *cls)
{
void *state = _PyType_GetModuleState(cls);
assert(state != NULL);
return (itertools_state *)state;
}
static struct PyModuleDef itertoolsmodule;
static inline itertools_state *
find_state_by_type(PyTypeObject *tp)
{
PyObject *mod = PyType_GetModuleByDef(tp, &itertoolsmodule);
assert(mod != NULL);
return get_module_state(mod);
}
/*[clinic input]
module itertools
class itertools.groupby "groupbyobject *" "clinic_state()->groupby_type"
class itertools._grouper "_grouperobject *" "clinic_state()->_grouper_type"
class itertools.teedataobject "teedataobject *" "clinic_state()->teedataobject_type"
class itertools._tee "teeobject *" "clinic_state()->tee_type"
class itertools.batched "batchedobject *" "clinic_state()->batched_type"
class itertools.cycle "cycleobject *" "clinic_state()->cycle_type"
class itertools.dropwhile "dropwhileobject *" "clinic_state()->dropwhile_type"
class itertools.takewhile "takewhileobject *" "clinic_state()->takewhile_type"
class itertools.starmap "starmapobject *" "clinic_state()->starmap_type"
class itertools.chain "chainobject *" "clinic_state()->chain_type"
class itertools.combinations "combinationsobject *" "clinic_state()->combinations_type"
class itertools.combinations_with_replacement "cwr_object *" "clinic_state()->cwr_type"
class itertools.permutations "permutationsobject *" "clinic_state()->permutations_type"
class itertools.accumulate "accumulateobject *" "clinic_state()->accumulate_type"
class itertools.compress "compressobject *" "clinic_state()->compress_type"
class itertools.filterfalse "filterfalseobject *" "clinic_state()->filterfalse_type"
class itertools.count "countobject *" "clinic_state()->count_type"
class itertools.pairwise "pairwiseobject *" "clinic_state()->pairwise_type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=aa48fe4de9d4080f]*/
#define clinic_state() (find_state_by_type(type))
#define clinic_state_by_cls() (get_module_state_by_cls(base_tp))
#include "clinic/itertoolsmodule.c.h"
#undef clinic_state_by_cls
#undef clinic_state
/* batched object ************************************************************/
typedef struct {
PyObject_HEAD
PyObject *it;
Py_ssize_t batch_size;
bool strict;
} batchedobject;
/*[clinic input]
@classmethod
itertools.batched.__new__ as batched_new
iterable: object
n: Py_ssize_t
*
strict: bool = False
Batch data into tuples of length n. The last batch may be shorter than n.
Loops over the input iterable and accumulates data into tuples
up to size n. The input is consumed lazily, just enough to
fill a batch. The result is yielded as soon as a batch is full
or when the input iterable is exhausted.
>>> for batch in batched('ABCDEFG', 3):
... print(batch)
...
('A', 'B', 'C')
('D', 'E', 'F')
('G',)
If "strict" is True, raises a ValueError if the final batch is shorter
than n.
[clinic start generated code]*/
static PyObject *
batched_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t n,
int strict)
/*[clinic end generated code: output=c6de11b061529d3e input=7814b47e222f5467]*/
{
PyObject *it;
batchedobject *bo;
if (n < 1) {
/* We could define the n==0 case to return an empty iterator
but that is at odds with the idea that batching should
never throw-away input data.
*/
PyErr_SetString(PyExc_ValueError, "n must be at least one");
return NULL;
}
it = PyObject_GetIter(iterable);
if (it == NULL) {
return NULL;
}
/* create batchedobject structure */
bo = (batchedobject *)type->tp_alloc(type, 0);
if (bo == NULL) {
Py_DECREF(it);
return NULL;
}
bo->batch_size = n;
bo->it = it;
bo->strict = (bool) strict;
return (PyObject *)bo;
}
static void
batched_dealloc(batchedobject *bo)
{
PyTypeObject *tp = Py_TYPE(bo);
PyObject_GC_UnTrack(bo);
Py_XDECREF(bo->it);
tp->tp_free(bo);
Py_DECREF(tp);
}
static int
batched_traverse(batchedobject *bo, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(bo));
Py_VISIT(bo->it);
return 0;
}
static PyObject *
batched_next(batchedobject *bo)
{
Py_ssize_t i;
Py_ssize_t n = bo->batch_size;
PyObject *it = bo->it;
PyObject *item;
PyObject *result;
if (it == NULL) {
return NULL;
}
result = PyTuple_New(n);
if (result == NULL) {
return NULL;
}
iternextfunc iternext = *Py_TYPE(it)->tp_iternext;
PyObject **items = _PyTuple_ITEMS(result);
for (i=0 ; i < n ; i++) {
item = iternext(it);
if (item == NULL) {
goto null_item;
}
items[i] = item;
}
return result;
null_item:
if (PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
/* Input raised an exception other than StopIteration */
Py_CLEAR(bo->it);
Py_DECREF(result);
return NULL;
}
PyErr_Clear();
}
if (i == 0) {
Py_CLEAR(bo->it);
Py_DECREF(result);
return NULL;
}
if (bo->strict) {
Py_CLEAR(bo->it);
Py_DECREF(result);
PyErr_SetString(PyExc_ValueError, "batched(): incomplete batch");
return NULL;
}
_PyTuple_Resize(&result, i);
return result;
}
static PyType_Slot batched_slots[] = {
{Py_tp_dealloc, batched_dealloc},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_doc, (void *)batched_new__doc__},
{Py_tp_traverse, batched_traverse},
{Py_tp_iter, PyObject_SelfIter},
{Py_tp_iternext, batched_next},
{Py_tp_alloc, PyType_GenericAlloc},
{Py_tp_new, batched_new},
{Py_tp_free, PyObject_GC_Del},
{0, NULL},
};
static PyType_Spec batched_spec = {
.name = "itertools.batched",
.basicsize = sizeof(batchedobject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = batched_slots,
};
/* pairwise object ***********************************************************/
typedef struct {
PyObject_HEAD
PyObject *it;
PyObject *old;
PyObject *result;
} pairwiseobject;
/*[clinic input]
@classmethod
itertools.pairwise.__new__ as pairwise_new
iterable: object
/
Return an iterator of overlapping pairs taken from the input iterator.
s -> (s0,s1), (s1,s2), (s2, s3), ...
[clinic start generated code]*/
static PyObject *
pairwise_new_impl(PyTypeObject *type, PyObject *iterable)
/*[clinic end generated code: output=9f0267062d384456 input=6e7c3cddb431a8d6]*/
{
PyObject *it;
pairwiseobject *po;
it = PyObject_GetIter(iterable);
if (it == NULL) {
return NULL;
}
po = (pairwiseobject *)type->tp_alloc(type, 0);
if (po == NULL) {
Py_DECREF(it);
return NULL;
}
po->it = it;
po->old = NULL;
po->result = PyTuple_Pack(2, Py_None, Py_None);
if (po->result == NULL) {
Py_DECREF(po);
return NULL;
}
return (PyObject *)po;
}
static void
pairwise_dealloc(pairwiseobject *po)
{
PyTypeObject *tp = Py_TYPE(po);
PyObject_GC_UnTrack(po);
Py_XDECREF(po->it);
Py_XDECREF(po->old);
Py_XDECREF(po->result);
tp->tp_free(po);
Py_DECREF(tp);
}
static int
pairwise_traverse(pairwiseobject *po, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(po));
Py_VISIT(po->it);
Py_VISIT(po->old);
Py_VISIT(po->result);
return 0;
}
static PyObject *
pairwise_next(pairwiseobject *po)
{
PyObject *it = po->it;
PyObject *old = po->old;
PyObject *new, *result;
if (it == NULL) {
return NULL;
}
if (old == NULL) {
old = (*Py_TYPE(it)->tp_iternext)(it);
Py_XSETREF(po->old, old);
if (old == NULL) {
Py_CLEAR(po->it);
return NULL;
}
it = po->it;
if (it == NULL) {
Py_CLEAR(po->old);
return NULL;
}
}
Py_INCREF(old);
new = (*Py_TYPE(it)->tp_iternext)(it);
if (new == NULL) {
Py_CLEAR(po->it);
Py_CLEAR(po->old);
Py_DECREF(old);
return NULL;
}
result = po->result;
if (Py_REFCNT(result) == 1) {
Py_INCREF(result);
PyObject *last_old = PyTuple_GET_ITEM(result, 0);
PyObject *last_new = PyTuple_GET_ITEM(result, 1);
PyTuple_SET_ITEM(result, 0, Py_NewRef(old));
PyTuple_SET_ITEM(result, 1, Py_NewRef(new));
Py_DECREF(last_old);
Py_DECREF(last_new);
// bpo-42536: The GC may have untracked this result tuple. Since we're
// recycling it, make sure it's tracked again:
if (!_PyObject_GC_IS_TRACKED(result)) {
_PyObject_GC_TRACK(result);
}
}
else {
result = PyTuple_New(2);
if (result != NULL) {
PyTuple_SET_ITEM(result, 0, Py_NewRef(old));
PyTuple_SET_ITEM(result, 1, Py_NewRef(new));
}
}
Py_XSETREF(po->old, new);
Py_DECREF(old);
return result;
}
static PyType_Slot pairwise_slots[] = {
{Py_tp_dealloc, pairwise_dealloc},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_doc, (void *)pairwise_new__doc__},
{Py_tp_traverse, pairwise_traverse},
{Py_tp_iter, PyObject_SelfIter},
{Py_tp_iternext, pairwise_next},
{Py_tp_alloc, PyType_GenericAlloc},
{Py_tp_new, pairwise_new},
{Py_tp_free, PyObject_GC_Del},
{0, NULL},
};
static PyType_Spec pairwise_spec = {
.name = "itertools.pairwise",
.basicsize = sizeof(pairwiseobject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = pairwise_slots,
};
/* groupby object ************************************************************/
typedef struct {
PyObject_HEAD
PyObject *it;
PyObject *keyfunc;
PyObject *tgtkey;
PyObject *currkey;
PyObject *currvalue;
const void *currgrouper; /* borrowed reference */
itertools_state *state;
} groupbyobject;
static PyObject *_grouper_create(groupbyobject *, PyObject *);
/*[clinic input]
@classmethod
itertools.groupby.__new__
iterable as it: object
Elements to divide into groups according to the key function.
key as keyfunc: object = None
A function for computing the group category for each element.
If the key function is not specified or is None, the element itself
is used for grouping.
make an iterator that returns consecutive keys and groups from the iterable
[clinic start generated code]*/
static PyObject *
itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc)
/*[clinic end generated code: output=cbb1ae3a90fd4141 input=6b3d123e87ff65a1]*/
{
groupbyobject *gbo;
gbo = (groupbyobject *)type->tp_alloc(type, 0);
if (gbo == NULL)
return NULL;
gbo->tgtkey = NULL;
gbo->currkey = NULL;
gbo->currvalue = NULL;
gbo->keyfunc = Py_NewRef(keyfunc);
gbo->it = PyObject_GetIter(it);
if (gbo->it == NULL) {
Py_DECREF(gbo);
return NULL;
}
gbo->state = find_state_by_type(type);
return (PyObject *)gbo;
}
static void
groupby_dealloc(groupbyobject *gbo)
{
PyTypeObject *tp = Py_TYPE(gbo);
PyObject_GC_UnTrack(gbo);
Py_XDECREF(gbo->it);
Py_XDECREF(gbo->keyfunc);
Py_XDECREF(gbo->tgtkey);
Py_XDECREF(gbo->currkey);
Py_XDECREF(gbo->currvalue);
tp->tp_free(gbo);
Py_DECREF(tp);
}
static int
groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(gbo));
Py_VISIT(gbo->it);
Py_VISIT(gbo->keyfunc);
Py_VISIT(gbo->tgtkey);
Py_VISIT(gbo->currkey);
Py_VISIT(gbo->currvalue);
return 0;
}
Py_LOCAL_INLINE(int)
groupby_step(groupbyobject *gbo)
{
PyObject *newvalue, *newkey, *oldvalue;
newvalue = PyIter_Next(gbo->it);
if (newvalue == NULL)
return -1;
if (gbo->keyfunc == Py_None) {
newkey = Py_NewRef(newvalue);
} else {
newkey = PyObject_CallOneArg(gbo->keyfunc, newvalue);
if (newkey == NULL) {
Py_DECREF(newvalue);
return -1;
}
}
oldvalue = gbo->currvalue;
gbo->currvalue = newvalue;
Py_XSETREF(gbo->currkey, newkey);
Py_XDECREF(oldvalue);
return 0;
}
static PyObject *
groupby_next(groupbyobject *gbo)
{
PyObject *r, *grouper;
gbo->currgrouper = NULL;
/* skip to next iteration group */
for (;;) {
if (gbo->currkey == NULL)
/* pass */;
else if (gbo->tgtkey == NULL)
break;
else {
int rcmp;
rcmp = PyObject_RichCompareBool(gbo->tgtkey, gbo->currkey, Py_EQ);
if (rcmp == -1)
return NULL;
else if (rcmp == 0)
break;
}
if (groupby_step(gbo) < 0)
return NULL;
}
Py_INCREF(gbo->currkey);
Py_XSETREF(gbo->tgtkey, gbo->currkey);
grouper = _grouper_create(gbo, gbo->tgtkey);
if (grouper == NULL)
return NULL;
r = PyTuple_Pack(2, gbo->currkey, grouper);
Py_DECREF(grouper);
return r;
}
static PyType_Slot groupby_slots[] = {
{Py_tp_dealloc, groupby_dealloc},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_doc, (void *)itertools_groupby__doc__},
{Py_tp_traverse, groupby_traverse},
{Py_tp_iter, PyObject_SelfIter},
{Py_tp_iternext, groupby_next},
{Py_tp_new, itertools_groupby},
{Py_tp_free, PyObject_GC_Del},
{0, NULL},
};
static PyType_Spec groupby_spec = {
.name = "itertools.groupby",
.basicsize= sizeof(groupbyobject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = groupby_slots,
};
/* _grouper object (internal) ************************************************/
typedef struct {
PyObject_HEAD
PyObject *parent;
PyObject *tgtkey;
} _grouperobject;
/*[clinic input]
@classmethod
itertools._grouper.__new__
parent: object(subclass_of='clinic_state_by_cls()->groupby_type')
tgtkey: object
/
[clinic start generated code]*/
static PyObject *
itertools__grouper_impl(PyTypeObject *type, PyObject *parent,
PyObject *tgtkey)
/*[clinic end generated code: output=462efb1cdebb5914 input=afe05eb477118f12]*/
{
return _grouper_create((groupbyobject*) parent, tgtkey);
}
static PyObject *
_grouper_create(groupbyobject *parent, PyObject *tgtkey)
{
itertools_state *state = parent->state;
_grouperobject *igo = PyObject_GC_New(_grouperobject, state->_grouper_type);
if (igo == NULL)
return NULL;
igo->parent = Py_NewRef(parent);
igo->tgtkey = Py_NewRef(tgtkey);
parent->currgrouper = igo; /* borrowed reference */
PyObject_GC_Track(igo);
return (PyObject *)igo;
}
static void
_grouper_dealloc(_grouperobject *igo)
{
PyTypeObject *tp = Py_TYPE(igo);
PyObject_GC_UnTrack(igo);
Py_DECREF(igo->parent);
Py_DECREF(igo->tgtkey);
PyObject_GC_Del(igo);
Py_DECREF(tp);
}
static int
_grouper_traverse(_grouperobject *igo, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(igo));
Py_VISIT(igo->parent);
Py_VISIT(igo->tgtkey);
return 0;
}
static PyObject *
_grouper_next(_grouperobject *igo)
{
groupbyobject *gbo = (groupbyobject *)igo->parent;
PyObject *r;
int rcmp;
if (gbo->currgrouper != igo)
return NULL;
if (gbo->currvalue == NULL) {
if (groupby_step(gbo) < 0)
return NULL;
}
assert(gbo->currkey != NULL);
rcmp = PyObject_RichCompareBool(igo->tgtkey, gbo->currkey, Py_EQ);
if (rcmp <= 0)
/* got any error or current group is end */
return NULL;
r = gbo->currvalue;
gbo->currvalue = NULL;
Py_CLEAR(gbo->currkey);
return r;
}
static PyType_Slot _grouper_slots[] = {
{Py_tp_dealloc, _grouper_dealloc},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_traverse, _grouper_traverse},
{Py_tp_iter, PyObject_SelfIter},
{Py_tp_iternext, _grouper_next},
{Py_tp_new, itertools__grouper},
{Py_tp_free, PyObject_GC_Del},
{0, NULL},
};
static PyType_Spec _grouper_spec = {
.name = "itertools._grouper",
.basicsize = sizeof(_grouperobject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = _grouper_slots,
};
/* tee object and with supporting function and objects ***********************/
/* The teedataobject pre-allocates space for LINKCELLS number of objects.
To help the object fit neatly inside cache lines (space for 16 to 32
pointers), the value should be a multiple of 16 minus space for
the other structure members including PyHEAD overhead. The larger the
value, the less memory overhead per object and the less time spent
allocating/deallocating new links. The smaller the number, the less
wasted space and the more rapid freeing of older data.
*/
#define LINKCELLS 57
typedef struct {
PyObject_HEAD
PyObject *it;
int numread; /* 0 <= numread <= LINKCELLS */
int running;
PyObject *nextlink;
PyObject *(values[LINKCELLS]);
} teedataobject;
typedef struct {
PyObject_HEAD
teedataobject *dataobj;
int index; /* 0 <= index <= LINKCELLS */
PyObject *weakreflist;
itertools_state *state;
} teeobject;
static PyObject *
teedataobject_newinternal(itertools_state *state, PyObject *it)
{
teedataobject *tdo;
tdo = PyObject_GC_New(teedataobject, state->teedataobject_type);
if (tdo == NULL)
return NULL;
tdo->running = 0;
tdo->numread = 0;
tdo->nextlink = NULL;
tdo->it = Py_NewRef(it);
PyObject_GC_Track(tdo);
return (PyObject *)tdo;
}
static PyObject *
teedataobject_jumplink(itertools_state *state, teedataobject *tdo)
{
if (tdo->nextlink == NULL)
tdo->nextlink = teedataobject_newinternal(state, tdo->it);
return Py_XNewRef(tdo->nextlink);
}
static PyObject *
teedataobject_getitem(teedataobject *tdo, int i)
{
PyObject *value;
assert(i < LINKCELLS);
if (i < tdo->numread)
value = tdo->values[i];
else {
/* this is the lead iterator, so fetch more data */
assert(i == tdo->numread);
if (tdo->running) {
PyErr_SetString(PyExc_RuntimeError,
"cannot re-enter the tee iterator");
return NULL;
}
tdo->running = 1;
value = PyIter_Next(tdo->it);
tdo->running = 0;
if (value == NULL)
return NULL;
tdo->numread++;
tdo->values[i] = value;
}
return Py_NewRef(value);
}
static int
teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg)
{
int i;
Py_VISIT(Py_TYPE(tdo));
Py_VISIT(tdo->it);
for (i = 0; i < tdo->numread; i++)
Py_VISIT(tdo->values[i]);
Py_VISIT(tdo->nextlink);
return 0;
}
static void
teedataobject_safe_decref(PyObject *obj)
{
while (obj && Py_REFCNT(obj) == 1) {
PyObject *nextlink = ((teedataobject *)obj)->nextlink;
((teedataobject *)obj)->nextlink = NULL;
Py_SETREF(obj, nextlink);
}
Py_XDECREF(obj);
}
static int
teedataobject_clear(teedataobject *tdo)
{
int i;
PyObject *tmp;
Py_CLEAR(tdo->it);
for (i=0 ; i<tdo->numread ; i++)
Py_CLEAR(tdo->values[i]);
tmp = tdo->nextlink;
tdo->nextlink = NULL;
teedataobject_safe_decref(tmp);
return 0;
}
static void
teedataobject_dealloc(teedataobject *tdo)
{
PyTypeObject *tp = Py_TYPE(tdo);
PyObject_GC_UnTrack(tdo);
teedataobject_clear(tdo);
PyObject_GC_Del(tdo);
Py_DECREF(tp);
}
/*[clinic input]
@classmethod
itertools.teedataobject.__new__
iterable as it: object
values: object(subclass_of='&PyList_Type')
next: object
/
Data container common to multiple tee objects.
[clinic start generated code]*/
static PyObject *
itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
PyObject *values, PyObject *next)
/*[clinic end generated code: output=3343ceb07e08df5e input=be60f2fabd2b72ba]*/
{
teedataobject *tdo;
Py_ssize_t i, len;
itertools_state *state = get_module_state_by_cls(type);
assert(type == state->teedataobject_type);
tdo = (teedataobject *)teedataobject_newinternal(state, it);
if (!tdo)
return NULL;
len = PyList_GET_SIZE(values);
if (len > LINKCELLS)
goto err;
for (i=0; i<len; i++) {
tdo->values[i] = PyList_GET_ITEM(values, i);
Py_INCREF(tdo->values[i]);
}
/* len <= LINKCELLS < INT_MAX */
tdo->numread = Py_SAFE_DOWNCAST(len, Py_ssize_t, int);
if (len == LINKCELLS) {
if (next != Py_None) {
if (!Py_IS_TYPE(next, state->teedataobject_type))
goto err;
assert(tdo->nextlink == NULL);
tdo->nextlink = Py_NewRef(next);
}
} else {
if (next != Py_None)
goto err; /* shouldn't have a next if we are not full */
}
return (PyObject*)tdo;
err:
Py_XDECREF(tdo);
PyErr_SetString(PyExc_ValueError, "Invalid arguments");
return NULL;
}
static PyType_Slot teedataobject_slots[] = {
{Py_tp_dealloc, teedataobject_dealloc},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_doc, (void *)itertools_teedataobject__doc__},
{Py_tp_traverse, teedataobject_traverse},
{Py_tp_clear, teedataobject_clear},
{Py_tp_new, itertools_teedataobject},
{Py_tp_free, PyObject_GC_Del},
{0, NULL},
};
static PyType_Spec teedataobject_spec = {
.name = "itertools._tee_dataobject",
.basicsize = sizeof(teedataobject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = teedataobject_slots,
};
static PyObject *
tee_next(teeobject *to)
{
PyObject *value, *link;
if (to->index >= LINKCELLS) {
link = teedataobject_jumplink(to->state, to->dataobj);
if (link == NULL)
return NULL;
Py_SETREF(to->dataobj, (teedataobject *)link);
to->index = 0;
}
value = teedataobject_getitem(to->dataobj, to->index);
if (value == NULL)
return NULL;
to->index++;
return value;
}
static int
tee_traverse(teeobject *to, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(to));
Py_VISIT((PyObject *)to->dataobj);
return 0;
}
static PyObject *
tee_copy(teeobject *to, PyObject *Py_UNUSED(ignored))
{
teeobject *newto;
newto = PyObject_GC_New(teeobject, Py_TYPE(to));
if (newto == NULL)
return NULL;
newto->dataobj = (teedataobject*)Py_NewRef(to->dataobj);
newto->index = to->index;
newto->weakreflist = NULL;
newto->state = to->state;
PyObject_GC_Track(newto);
return (PyObject *)newto;
}
PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator.");
static PyObject *
tee_fromiterable(itertools_state *state, PyObject *iterable)
{
teeobject *to;
PyObject *it;
it = PyObject_GetIter(iterable);
if (it == NULL)
return NULL;
if (PyObject_TypeCheck(it, state->tee_type)) {
to = (teeobject *)tee_copy((teeobject *)it, NULL);
goto done;
}
PyObject *dataobj = teedataobject_newinternal(state, it);
if (!dataobj) {
to = NULL;
goto done;
}
to = PyObject_GC_New(teeobject, state->tee_type);
if (to == NULL) {
Py_DECREF(dataobj);
goto done;
}
to->dataobj = (teedataobject *)dataobj;
to->index = 0;
to->weakreflist = NULL;
to->state = state;
PyObject_GC_Track(to);
done:
Py_DECREF(it);
return (PyObject *)to;
}
/*[clinic input]
@classmethod
itertools._tee.__new__
iterable: object
/
Iterator wrapped to make it copyable.
[clinic start generated code]*/
static PyObject *
itertools__tee_impl(PyTypeObject *type, PyObject *iterable)
/*[clinic end generated code: output=b02d3fd26c810c3f input=adc0779d2afe37a2]*/
{
itertools_state *state = get_module_state_by_cls(type);
return tee_fromiterable(state, iterable);
}
static int
tee_clear(teeobject *to)
{
if (to->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) to);
Py_CLEAR(to->dataobj);
return 0;
}
static void
tee_dealloc(teeobject *to)
{
PyTypeObject *tp = Py_TYPE(to);
PyObject_GC_UnTrack(to);
tee_clear(to);
PyObject_GC_Del(to);
Py_DECREF(tp);
}
static PyMethodDef tee_methods[] = {
{"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc},
{NULL, NULL} /* sentinel */
};
static PyMemberDef tee_members[] = {
{"__weaklistoffset__", Py_T_PYSSIZET, offsetof(teeobject, weakreflist), Py_READONLY},