/src/cpython/Modules/itertoolsmodule.c
Line | Count | Source |
1 | | #include "Python.h" |
2 | | #include "pycore_call.h" // _PyObject_CallNoArgs() |
3 | | #include "pycore_ceval.h" // _PyEval_GetBuiltin() |
4 | | #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION() |
5 | | #include "pycore_long.h" // _PyLong_GetZero() |
6 | | #include "pycore_moduleobject.h" // _PyModule_GetState() |
7 | | #include "pycore_typeobject.h" // _PyType_GetModuleState() |
8 | | #include "pycore_object.h" // _PyObject_GC_TRACK() |
9 | | #include "pycore_tuple.h" // _PyTuple_ITEMS() |
10 | | |
11 | | #include <stddef.h> // offsetof() |
12 | | |
13 | | /* Itertools module written and maintained |
14 | | by Raymond D. Hettinger <python@rcn.com> |
15 | | */ |
16 | | |
17 | | typedef struct { |
18 | | PyTypeObject *accumulate_type; |
19 | | PyTypeObject *batched_type; |
20 | | PyTypeObject *chain_type; |
21 | | PyTypeObject *combinations_type; |
22 | | PyTypeObject *compress_type; |
23 | | PyTypeObject *count_type; |
24 | | PyTypeObject *cwr_type; |
25 | | PyTypeObject *cycle_type; |
26 | | PyTypeObject *dropwhile_type; |
27 | | PyTypeObject *filterfalse_type; |
28 | | PyTypeObject *groupby_type; |
29 | | PyTypeObject *_grouper_type; |
30 | | PyTypeObject *islice_type; |
31 | | PyTypeObject *pairwise_type; |
32 | | PyTypeObject *permutations_type; |
33 | | PyTypeObject *product_type; |
34 | | PyTypeObject *repeat_type; |
35 | | PyTypeObject *starmap_type; |
36 | | PyTypeObject *takewhile_type; |
37 | | PyTypeObject *tee_type; |
38 | | PyTypeObject *teedataobject_type; |
39 | | PyTypeObject *ziplongest_type; |
40 | | } itertools_state; |
41 | | |
42 | | static inline itertools_state * |
43 | | get_module_state(PyObject *mod) |
44 | 17.6k | { |
45 | 17.6k | void *state = _PyModule_GetState(mod); |
46 | 17.6k | assert(state != NULL); |
47 | 17.6k | return (itertools_state *)state; |
48 | 17.6k | } |
49 | | |
50 | | static inline itertools_state * |
51 | | get_module_state_by_cls(PyTypeObject *cls) |
52 | 0 | { |
53 | 0 | void *state = _PyType_GetModuleState(cls); |
54 | 0 | assert(state != NULL); |
55 | 0 | return (itertools_state *)state; |
56 | 0 | } |
57 | | |
58 | | static struct PyModuleDef itertoolsmodule; |
59 | | |
60 | | static inline itertools_state * |
61 | | find_state_by_type(PyTypeObject *tp) |
62 | 15.6k | { |
63 | 15.6k | PyObject *mod = PyType_GetModuleByDef(tp, &itertoolsmodule); |
64 | 15.6k | assert(mod != NULL); |
65 | 15.6k | return get_module_state(mod); |
66 | 15.6k | } |
67 | | |
68 | | // The generated code (clinic/itertoolsmodule.c.h) needs the object types |
69 | | // of the type slots implemented with Argument Clinic. |
70 | | typedef struct chainobject chainobject; |
71 | | typedef struct productobject productobject; |
72 | | typedef struct combinationsobject combinationsobject; |
73 | | typedef struct cwrobject cwrobject; |
74 | | typedef struct permutationsobject permutationsobject; |
75 | | typedef struct accumulateobject accumulateobject; |
76 | | typedef struct ziplongestobject ziplongestobject; |
77 | | |
78 | | /*[clinic input] |
79 | | module itertools |
80 | | class itertools.groupby "groupbyobject *" "clinic_state()->groupby_type" |
81 | | class itertools._grouper "_grouperobject *" "clinic_state()->_grouper_type" |
82 | | class itertools.teedataobject "teedataobject *" "clinic_state()->teedataobject_type" |
83 | | class itertools._tee "teeobject *" "clinic_state()->tee_type" |
84 | | class itertools.batched "batchedobject *" "clinic_state()->batched_type" |
85 | | class itertools.cycle "cycleobject *" "clinic_state()->cycle_type" |
86 | | class itertools.dropwhile "dropwhileobject *" "clinic_state()->dropwhile_type" |
87 | | class itertools.takewhile "takewhileobject *" "clinic_state()->takewhile_type" |
88 | | class itertools.starmap "starmapobject *" "clinic_state()->starmap_type" |
89 | | class itertools.chain "chainobject *" "clinic_state()->chain_type" |
90 | | class itertools.product "productobject *" "clinic_state()->product_type" |
91 | | class itertools.combinations "combinationsobject *" "clinic_state()->combinations_type" |
92 | | class itertools.combinations_with_replacement "cwrobject *" "clinic_state()->cwr_type" |
93 | | class itertools.permutations "permutationsobject *" "clinic_state()->permutations_type" |
94 | | class itertools.accumulate "accumulateobject *" "clinic_state()->accumulate_type" |
95 | | class itertools.zip_longest "ziplongestobject *" "clinic_state()->ziplongest_type" |
96 | | class itertools.compress "compressobject *" "clinic_state()->compress_type" |
97 | | class itertools.filterfalse "filterfalseobject *" "clinic_state()->filterfalse_type" |
98 | | class itertools.count "countobject *" "clinic_state()->count_type" |
99 | | class itertools.pairwise "pairwiseobject *" "clinic_state()->pairwise_type" |
100 | | [clinic start generated code]*/ |
101 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=1086fb2d308d75d2]*/ |
102 | | |
103 | 0 | #define clinic_state() (find_state_by_type(type)) |
104 | 0 | #define clinic_state_by_cls() (get_module_state_by_cls(base_tp)) |
105 | | #include "clinic/itertoolsmodule.c.h" |
106 | | #undef clinic_state_by_cls |
107 | | #undef clinic_state |
108 | | |
109 | | |
110 | | /* batched object ************************************************************/ |
111 | | |
112 | | typedef struct { |
113 | | PyObject_HEAD |
114 | | PyObject *it; |
115 | | Py_ssize_t batch_size; |
116 | | bool strict; |
117 | | } batchedobject; |
118 | | |
119 | 0 | #define batchedobject_CAST(op) ((batchedobject *)(op)) |
120 | | |
121 | | /*[clinic input] |
122 | | @permit_long_summary |
123 | | @vectorcall |
124 | | @classmethod |
125 | | itertools.batched.__new__ as batched_new |
126 | | iterable: object |
127 | | n: Py_ssize_t |
128 | | * |
129 | | strict: bool = False |
130 | | |
131 | | Batch data into tuples of length n. The last batch may be shorter than n. |
132 | | |
133 | | Loops over the input iterable and accumulates data into tuples |
134 | | up to size n. The input is consumed lazily, just enough to |
135 | | fill a batch. The result is yielded as soon as a batch is full |
136 | | or when the input iterable is exhausted. |
137 | | |
138 | | >>> for batch in batched('ABCDEFG', 3): |
139 | | ... print(batch) |
140 | | ... |
141 | | ('A', 'B', 'C') |
142 | | ('D', 'E', 'F') |
143 | | ('G',) |
144 | | |
145 | | If "strict" is True, raises a ValueError if the final batch is shorter |
146 | | than n. |
147 | | |
148 | | [clinic start generated code]*/ |
149 | | |
150 | | static PyObject * |
151 | | batched_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t n, |
152 | | int strict) |
153 | | /*[clinic end generated code: output=c6de11b061529d3e input=e225f892d3eee347]*/ |
154 | 0 | { |
155 | 0 | PyObject *it; |
156 | 0 | batchedobject *bo; |
157 | |
|
158 | 0 | if (n < 1) { |
159 | | /* We could define the n==0 case to return an empty iterator |
160 | | but that is at odds with the idea that batching should |
161 | | never throw-away input data. |
162 | | */ |
163 | 0 | PyErr_SetString(PyExc_ValueError, "n must be at least one"); |
164 | 0 | return NULL; |
165 | 0 | } |
166 | 0 | it = PyObject_GetIter(iterable); |
167 | 0 | if (it == NULL) { |
168 | 0 | return NULL; |
169 | 0 | } |
170 | | |
171 | | /* create batchedobject structure */ |
172 | 0 | bo = (batchedobject *)type->tp_alloc(type, 0); |
173 | 0 | if (bo == NULL) { |
174 | 0 | Py_DECREF(it); |
175 | 0 | return NULL; |
176 | 0 | } |
177 | 0 | bo->batch_size = n; |
178 | 0 | bo->it = it; |
179 | 0 | bo->strict = (bool) strict; |
180 | 0 | return (PyObject *)bo; |
181 | 0 | } |
182 | | |
183 | | static void |
184 | | batched_dealloc(PyObject *op) |
185 | 0 | { |
186 | 0 | batchedobject *bo = batchedobject_CAST(op); |
187 | 0 | PyTypeObject *tp = Py_TYPE(bo); |
188 | 0 | PyObject_GC_UnTrack(bo); |
189 | 0 | Py_XDECREF(bo->it); |
190 | 0 | tp->tp_free(bo); |
191 | 0 | Py_DECREF(tp); |
192 | 0 | } |
193 | | |
194 | | static int |
195 | | batched_traverse(PyObject *op, visitproc visit, void *arg) |
196 | 0 | { |
197 | 0 | batchedobject *bo = batchedobject_CAST(op); |
198 | 0 | Py_VISIT(Py_TYPE(bo)); |
199 | 0 | Py_VISIT(bo->it); |
200 | 0 | return 0; |
201 | 0 | } |
202 | | |
203 | | static PyObject * |
204 | | batched_next(PyObject *op) |
205 | 0 | { |
206 | 0 | batchedobject *bo = batchedobject_CAST(op); |
207 | 0 | Py_ssize_t i; |
208 | 0 | Py_ssize_t n = FT_ATOMIC_LOAD_SSIZE_RELAXED(bo->batch_size); |
209 | 0 | PyObject *it = bo->it; |
210 | 0 | PyObject *item; |
211 | 0 | PyObject *result; |
212 | |
|
213 | 0 | if (n < 0) { |
214 | 0 | return NULL; |
215 | 0 | } |
216 | 0 | result = PyTuple_New(n); |
217 | 0 | if (result == NULL) { |
218 | 0 | return NULL; |
219 | 0 | } |
220 | 0 | iternextfunc iternext = *Py_TYPE(it)->tp_iternext; |
221 | 0 | PyObject **items = _PyTuple_ITEMS(result); |
222 | 0 | for (i=0 ; i < n ; i++) { |
223 | 0 | item = iternext(it); |
224 | 0 | if (item == NULL) { |
225 | 0 | goto null_item; |
226 | 0 | } |
227 | 0 | items[i] = item; |
228 | 0 | } |
229 | 0 | return result; |
230 | | |
231 | 0 | null_item: |
232 | 0 | if (PyErr_Occurred()) { |
233 | 0 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) { |
234 | | /* Input raised an exception other than StopIteration */ |
235 | 0 | FT_ATOMIC_STORE_SSIZE_RELAXED(bo->batch_size, -1); |
236 | 0 | #ifndef Py_GIL_DISABLED |
237 | 0 | Py_CLEAR(bo->it); |
238 | 0 | #endif |
239 | 0 | Py_DECREF(result); |
240 | 0 | return NULL; |
241 | 0 | } |
242 | 0 | PyErr_Clear(); |
243 | 0 | } |
244 | 0 | if (i == 0) { |
245 | 0 | FT_ATOMIC_STORE_SSIZE_RELAXED(bo->batch_size, -1); |
246 | 0 | #ifndef Py_GIL_DISABLED |
247 | 0 | Py_CLEAR(bo->it); |
248 | 0 | #endif |
249 | 0 | Py_DECREF(result); |
250 | 0 | return NULL; |
251 | 0 | } |
252 | 0 | if (bo->strict) { |
253 | 0 | FT_ATOMIC_STORE_SSIZE_RELAXED(bo->batch_size, -1); |
254 | 0 | #ifndef Py_GIL_DISABLED |
255 | 0 | Py_CLEAR(bo->it); |
256 | 0 | #endif |
257 | 0 | Py_DECREF(result); |
258 | 0 | PyErr_SetString(PyExc_ValueError, "batched(): incomplete batch"); |
259 | 0 | return NULL; |
260 | 0 | } |
261 | 0 | _PyTuple_Resize(&result, i); |
262 | 0 | return result; |
263 | 0 | } |
264 | | |
265 | | static PyType_Slot batched_slots[] = { |
266 | | {Py_tp_dealloc, batched_dealloc}, |
267 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
268 | | {Py_tp_doc, (void *)batched_new__doc__}, |
269 | | {Py_tp_traverse, batched_traverse}, |
270 | | {Py_tp_iter, PyObject_SelfIter}, |
271 | | {Py_tp_iternext, batched_next}, |
272 | | {Py_tp_alloc, PyType_GenericAlloc}, |
273 | | {Py_tp_new, batched_new}, |
274 | | {Py_tp_vectorcall, batched_vectorcall}, |
275 | | {Py_tp_free, PyObject_GC_Del}, |
276 | | {0, NULL}, |
277 | | }; |
278 | | |
279 | | static PyType_Spec batched_spec = { |
280 | | .name = "itertools.batched", |
281 | | .basicsize = sizeof(batchedobject), |
282 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
283 | | Py_TPFLAGS_IMMUTABLETYPE), |
284 | | .slots = batched_slots, |
285 | | }; |
286 | | |
287 | | |
288 | | /* pairwise object ***********************************************************/ |
289 | | |
290 | | typedef struct { |
291 | | PyObject_HEAD |
292 | | PyObject *it; |
293 | | PyObject *old; |
294 | | PyObject *result; |
295 | | } pairwiseobject; |
296 | | |
297 | 28 | #define pairwiseobject_CAST(op) ((pairwiseobject *)(op)) |
298 | | |
299 | | /*[clinic input] |
300 | | @vectorcall |
301 | | @classmethod |
302 | | itertools.pairwise.__new__ as pairwise_new |
303 | | iterable: object |
304 | | / |
305 | | Return an iterator of overlapping pairs taken from the input iterator. |
306 | | |
307 | | s -> (s0,s1), (s1,s2), (s2, s3), ... |
308 | | |
309 | | [clinic start generated code]*/ |
310 | | |
311 | | static PyObject * |
312 | | pairwise_new_impl(PyTypeObject *type, PyObject *iterable) |
313 | | /*[clinic end generated code: output=9f0267062d384456 input=32624f8712dcf0a3]*/ |
314 | 4 | { |
315 | 4 | PyObject *it; |
316 | 4 | pairwiseobject *po; |
317 | | |
318 | 4 | it = PyObject_GetIter(iterable); |
319 | 4 | if (it == NULL) { |
320 | 0 | return NULL; |
321 | 0 | } |
322 | 4 | po = (pairwiseobject *)type->tp_alloc(type, 0); |
323 | 4 | if (po == NULL) { |
324 | 0 | Py_DECREF(it); |
325 | 0 | return NULL; |
326 | 0 | } |
327 | 4 | po->it = it; |
328 | 4 | po->old = NULL; |
329 | 4 | po->result = _PyTuple_FromPairSteal(Py_None, Py_None); |
330 | 4 | if (po->result == NULL) { |
331 | 0 | Py_DECREF(po); |
332 | 0 | return NULL; |
333 | 0 | } |
334 | 4 | return (PyObject *)po; |
335 | 4 | } |
336 | | |
337 | | static void |
338 | | pairwise_dealloc(PyObject *op) |
339 | 4 | { |
340 | 4 | pairwiseobject *po = pairwiseobject_CAST(op); |
341 | 4 | PyTypeObject *tp = Py_TYPE(po); |
342 | 4 | PyObject_GC_UnTrack(po); |
343 | 4 | Py_XDECREF(po->it); |
344 | 4 | Py_XDECREF(po->old); |
345 | 4 | Py_XDECREF(po->result); |
346 | 4 | tp->tp_free(po); |
347 | 4 | Py_DECREF(tp); |
348 | 4 | } |
349 | | |
350 | | static int |
351 | | pairwise_traverse(PyObject *op, visitproc visit, void *arg) |
352 | 0 | { |
353 | 0 | pairwiseobject *po = pairwiseobject_CAST(op); |
354 | 0 | Py_VISIT(Py_TYPE(po)); |
355 | 0 | Py_VISIT(po->it); |
356 | 0 | Py_VISIT(po->old); |
357 | 0 | Py_VISIT(po->result); |
358 | 0 | return 0; |
359 | 0 | } |
360 | | |
361 | | static PyObject * |
362 | | pairwise_next(PyObject *op) |
363 | 24 | { |
364 | 24 | pairwiseobject *po = pairwiseobject_CAST(op); |
365 | 24 | PyObject *it = po->it; |
366 | 24 | PyObject *old = po->old; |
367 | 24 | PyObject *new, *result; |
368 | | |
369 | 24 | if (it == NULL) { |
370 | 0 | return NULL; |
371 | 0 | } |
372 | 24 | if (old == NULL) { |
373 | 4 | old = (*Py_TYPE(it)->tp_iternext)(it); |
374 | 4 | Py_XSETREF(po->old, old); |
375 | 4 | if (old == NULL) { |
376 | 0 | Py_CLEAR(po->it); |
377 | 0 | return NULL; |
378 | 0 | } |
379 | 4 | it = po->it; |
380 | 4 | if (it == NULL) { |
381 | 0 | Py_CLEAR(po->old); |
382 | 0 | return NULL; |
383 | 0 | } |
384 | 4 | } |
385 | 24 | Py_INCREF(old); |
386 | 24 | new = (*Py_TYPE(it)->tp_iternext)(it); |
387 | 24 | if (new == NULL) { |
388 | 4 | Py_CLEAR(po->it); |
389 | 4 | Py_CLEAR(po->old); |
390 | 4 | Py_DECREF(old); |
391 | 4 | return NULL; |
392 | 4 | } |
393 | | |
394 | 20 | result = po->result; |
395 | 20 | if (_PyObject_IsUniquelyReferenced(result)) { |
396 | 20 | Py_INCREF(result); |
397 | 20 | PyObject *last_old = PyTuple_GET_ITEM(result, 0); |
398 | 20 | PyObject *last_new = PyTuple_GET_ITEM(result, 1); |
399 | 20 | PyTuple_SET_ITEM(result, 0, Py_NewRef(old)); |
400 | 20 | PyTuple_SET_ITEM(result, 1, Py_NewRef(new)); |
401 | 20 | Py_DECREF(last_old); |
402 | 20 | Py_DECREF(last_new); |
403 | | // bpo-42536: The GC may have untracked this result tuple. Since we're |
404 | | // recycling it, make sure it's tracked again: |
405 | 20 | _PyTuple_Recycle(result); |
406 | 20 | } |
407 | 0 | else { |
408 | 0 | result = _PyTuple_FromPair(old, new); |
409 | 0 | } |
410 | | |
411 | 20 | Py_XSETREF(po->old, new); |
412 | 20 | Py_DECREF(old); |
413 | 20 | return result; |
414 | 24 | } |
415 | | |
416 | | static PyType_Slot pairwise_slots[] = { |
417 | | {Py_tp_dealloc, pairwise_dealloc}, |
418 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
419 | | {Py_tp_doc, (void *)pairwise_new__doc__}, |
420 | | {Py_tp_traverse, pairwise_traverse}, |
421 | | {Py_tp_iter, PyObject_SelfIter}, |
422 | | {Py_tp_iternext, pairwise_next}, |
423 | | {Py_tp_alloc, PyType_GenericAlloc}, |
424 | | {Py_tp_new, pairwise_new}, |
425 | | {Py_tp_vectorcall, pairwise_vectorcall}, |
426 | | {Py_tp_free, PyObject_GC_Del}, |
427 | | {0, NULL}, |
428 | | }; |
429 | | |
430 | | static PyType_Spec pairwise_spec = { |
431 | | .name = "itertools.pairwise", |
432 | | .basicsize = sizeof(pairwiseobject), |
433 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
434 | | Py_TPFLAGS_IMMUTABLETYPE), |
435 | | .slots = pairwise_slots, |
436 | | }; |
437 | | |
438 | | |
439 | | /* groupby object ************************************************************/ |
440 | | |
441 | | typedef struct { |
442 | | PyObject_HEAD |
443 | | PyObject *it; |
444 | | PyObject *keyfunc; |
445 | | PyObject *tgtkey; |
446 | | PyObject *currkey; |
447 | | PyObject *currvalue; |
448 | | const void *currgrouper; /* borrowed reference */ |
449 | | itertools_state *state; |
450 | | } groupbyobject; |
451 | | |
452 | 0 | #define groupbyobject_CAST(op) ((groupbyobject *)(op)) |
453 | | |
454 | | static PyObject *_grouper_create(groupbyobject *, PyObject *); |
455 | | |
456 | | /*[clinic input] |
457 | | @permit_long_summary |
458 | | @vectorcall |
459 | | @classmethod |
460 | | itertools.groupby.__new__ |
461 | | |
462 | | iterable as it: object |
463 | | Elements to divide into groups according to the key function. |
464 | | key as keyfunc: object = None |
465 | | A function for computing the group category for each element. |
466 | | If the key function is not specified or is None, the element itself |
467 | | is used for grouping. |
468 | | |
469 | | make an iterator that returns consecutive keys and groups from the iterable |
470 | | [clinic start generated code]*/ |
471 | | |
472 | | static PyObject * |
473 | | itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc) |
474 | | /*[clinic end generated code: output=cbb1ae3a90fd4141 input=83700c6814fea634]*/ |
475 | 0 | { |
476 | 0 | groupbyobject *gbo; |
477 | |
|
478 | 0 | gbo = (groupbyobject *)type->tp_alloc(type, 0); |
479 | 0 | if (gbo == NULL) |
480 | 0 | return NULL; |
481 | 0 | gbo->tgtkey = NULL; |
482 | 0 | gbo->currkey = NULL; |
483 | 0 | gbo->currvalue = NULL; |
484 | 0 | gbo->keyfunc = Py_NewRef(keyfunc); |
485 | 0 | gbo->it = PyObject_GetIter(it); |
486 | 0 | if (gbo->it == NULL) { |
487 | 0 | Py_DECREF(gbo); |
488 | 0 | return NULL; |
489 | 0 | } |
490 | 0 | gbo->state = find_state_by_type(type); |
491 | 0 | return (PyObject *)gbo; |
492 | 0 | } |
493 | | |
494 | | static void |
495 | | groupby_dealloc(PyObject *op) |
496 | 0 | { |
497 | 0 | groupbyobject *gbo = groupbyobject_CAST(op); |
498 | 0 | PyTypeObject *tp = Py_TYPE(gbo); |
499 | 0 | PyObject_GC_UnTrack(gbo); |
500 | 0 | Py_XDECREF(gbo->it); |
501 | 0 | Py_XDECREF(gbo->keyfunc); |
502 | 0 | Py_XDECREF(gbo->tgtkey); |
503 | 0 | Py_XDECREF(gbo->currkey); |
504 | 0 | Py_XDECREF(gbo->currvalue); |
505 | 0 | tp->tp_free(gbo); |
506 | 0 | Py_DECREF(tp); |
507 | 0 | } |
508 | | |
509 | | static int |
510 | | groupby_traverse(PyObject *op, visitproc visit, void *arg) |
511 | 0 | { |
512 | 0 | groupbyobject *gbo = groupbyobject_CAST(op); |
513 | 0 | Py_VISIT(Py_TYPE(gbo)); |
514 | 0 | Py_VISIT(gbo->it); |
515 | 0 | Py_VISIT(gbo->keyfunc); |
516 | 0 | Py_VISIT(gbo->tgtkey); |
517 | 0 | Py_VISIT(gbo->currkey); |
518 | 0 | Py_VISIT(gbo->currvalue); |
519 | 0 | return 0; |
520 | 0 | } |
521 | | |
522 | | Py_LOCAL_INLINE(int) |
523 | | groupby_step(groupbyobject *gbo) |
524 | 0 | { |
525 | 0 | PyObject *newvalue, *newkey, *oldvalue; |
526 | |
|
527 | 0 | newvalue = PyIter_Next(gbo->it); |
528 | 0 | if (newvalue == NULL) |
529 | 0 | return -1; |
530 | | |
531 | 0 | if (gbo->keyfunc == Py_None) { |
532 | 0 | newkey = Py_NewRef(newvalue); |
533 | 0 | } else { |
534 | 0 | newkey = PyObject_CallOneArg(gbo->keyfunc, newvalue); |
535 | 0 | if (newkey == NULL) { |
536 | 0 | Py_DECREF(newvalue); |
537 | 0 | return -1; |
538 | 0 | } |
539 | 0 | } |
540 | | |
541 | 0 | oldvalue = gbo->currvalue; |
542 | 0 | gbo->currvalue = newvalue; |
543 | 0 | Py_XSETREF(gbo->currkey, newkey); |
544 | 0 | Py_XDECREF(oldvalue); |
545 | 0 | return 0; |
546 | 0 | } |
547 | | |
548 | | static PyObject * |
549 | | groupby_next(PyObject *op) |
550 | 0 | { |
551 | 0 | PyObject *grouper; |
552 | 0 | groupbyobject *gbo = groupbyobject_CAST(op); |
553 | |
|
554 | 0 | gbo->currgrouper = NULL; |
555 | | /* skip to next iteration group */ |
556 | 0 | for (;;) { |
557 | 0 | if (gbo->currkey == NULL) |
558 | 0 | /* pass */; |
559 | 0 | else if (gbo->tgtkey == NULL) |
560 | 0 | break; |
561 | 0 | else { |
562 | | /* A user-defined __eq__ can re-enter groupby and advance the iterator, |
563 | | mutating gbo->tgtkey / gbo->currkey while we are comparing them. |
564 | | Take local snapshots and hold strong references so INCREF/DECREF |
565 | | apply to the same objects even under re-entrancy. */ |
566 | 0 | PyObject *tgtkey = gbo->tgtkey; |
567 | 0 | PyObject *currkey = gbo->currkey; |
568 | |
|
569 | 0 | Py_INCREF(tgtkey); |
570 | 0 | Py_INCREF(currkey); |
571 | 0 | int rcmp = PyObject_RichCompareBool(tgtkey, currkey, Py_EQ); |
572 | 0 | Py_DECREF(tgtkey); |
573 | 0 | Py_DECREF(currkey); |
574 | |
|
575 | 0 | if (rcmp == -1) |
576 | 0 | return NULL; |
577 | 0 | else if (rcmp == 0) |
578 | 0 | break; |
579 | 0 | } |
580 | | |
581 | 0 | if (groupby_step(gbo) < 0) |
582 | 0 | return NULL; |
583 | 0 | } |
584 | 0 | Py_INCREF(gbo->currkey); |
585 | 0 | Py_XSETREF(gbo->tgtkey, gbo->currkey); |
586 | |
|
587 | 0 | grouper = _grouper_create(gbo, gbo->tgtkey); |
588 | 0 | if (grouper == NULL) |
589 | 0 | return NULL; |
590 | | |
591 | 0 | return _PyTuple_FromPairSteal(Py_NewRef(gbo->currkey), grouper); |
592 | 0 | } |
593 | | |
594 | | static PyType_Slot groupby_slots[] = { |
595 | | {Py_tp_dealloc, groupby_dealloc}, |
596 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
597 | | {Py_tp_doc, (void *)itertools_groupby__doc__}, |
598 | | {Py_tp_traverse, groupby_traverse}, |
599 | | {Py_tp_iter, PyObject_SelfIter}, |
600 | | {Py_tp_iternext, groupby_next}, |
601 | | {Py_tp_new, itertools_groupby}, |
602 | | {Py_tp_vectorcall, itertools_groupby_vectorcall}, |
603 | | {Py_tp_free, PyObject_GC_Del}, |
604 | | {0, NULL}, |
605 | | }; |
606 | | |
607 | | static PyType_Spec groupby_spec = { |
608 | | .name = "itertools.groupby", |
609 | | .basicsize= sizeof(groupbyobject), |
610 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
611 | | Py_TPFLAGS_IMMUTABLETYPE), |
612 | | .slots = groupby_slots, |
613 | | }; |
614 | | |
615 | | /* _grouper object (internal) ************************************************/ |
616 | | |
617 | | typedef struct { |
618 | | PyObject_HEAD |
619 | | PyObject *parent; |
620 | | PyObject *tgtkey; |
621 | | } _grouperobject; |
622 | | |
623 | 0 | #define _grouperobject_CAST(op) ((_grouperobject *)(op)) |
624 | | |
625 | | /*[clinic input] |
626 | | @classmethod |
627 | | itertools._grouper.__new__ |
628 | | |
629 | | parent: object(subclass_of='clinic_state_by_cls()->groupby_type') |
630 | | tgtkey: object |
631 | | / |
632 | | [clinic start generated code]*/ |
633 | | |
634 | | static PyObject * |
635 | | itertools__grouper_impl(PyTypeObject *type, PyObject *parent, |
636 | | PyObject *tgtkey) |
637 | | /*[clinic end generated code: output=462efb1cdebb5914 input=afe05eb477118f12]*/ |
638 | 0 | { |
639 | 0 | return _grouper_create(groupbyobject_CAST(parent), tgtkey); |
640 | 0 | } |
641 | | |
642 | | static PyObject * |
643 | | _grouper_create(groupbyobject *parent, PyObject *tgtkey) |
644 | 0 | { |
645 | 0 | itertools_state *state = parent->state; |
646 | 0 | _grouperobject *igo = PyObject_GC_New(_grouperobject, state->_grouper_type); |
647 | 0 | if (igo == NULL) |
648 | 0 | return NULL; |
649 | 0 | igo->parent = Py_NewRef(parent); |
650 | 0 | igo->tgtkey = Py_NewRef(tgtkey); |
651 | 0 | parent->currgrouper = igo; /* borrowed reference */ |
652 | |
|
653 | 0 | PyObject_GC_Track(igo); |
654 | 0 | return (PyObject *)igo; |
655 | 0 | } |
656 | | |
657 | | static void |
658 | | _grouper_dealloc(PyObject *op) |
659 | 0 | { |
660 | 0 | _grouperobject *igo = _grouperobject_CAST(op); |
661 | 0 | PyTypeObject *tp = Py_TYPE(igo); |
662 | 0 | PyObject_GC_UnTrack(igo); |
663 | 0 | Py_DECREF(igo->parent); |
664 | 0 | Py_DECREF(igo->tgtkey); |
665 | 0 | PyObject_GC_Del(igo); |
666 | 0 | Py_DECREF(tp); |
667 | 0 | } |
668 | | |
669 | | static int |
670 | | _grouper_traverse(PyObject *op, visitproc visit, void *arg) |
671 | 0 | { |
672 | 0 | _grouperobject *igo = _grouperobject_CAST(op); |
673 | 0 | Py_VISIT(Py_TYPE(igo)); |
674 | 0 | Py_VISIT(igo->parent); |
675 | 0 | Py_VISIT(igo->tgtkey); |
676 | 0 | return 0; |
677 | 0 | } |
678 | | |
679 | | static PyObject * |
680 | | _grouper_next(PyObject *op) |
681 | 0 | { |
682 | 0 | _grouperobject *igo = _grouperobject_CAST(op); |
683 | 0 | groupbyobject *gbo = groupbyobject_CAST(igo->parent); |
684 | 0 | PyObject *r; |
685 | 0 | int rcmp; |
686 | |
|
687 | 0 | if (gbo->currgrouper != igo) |
688 | 0 | return NULL; |
689 | 0 | if (gbo->currvalue == NULL) { |
690 | 0 | if (groupby_step(gbo) < 0) |
691 | 0 | return NULL; |
692 | 0 | } |
693 | | |
694 | 0 | assert(gbo->currkey != NULL); |
695 | | /* A user-defined __eq__ can re-enter the grouper and advance the iterator, |
696 | | mutating gbo->currkey while we are comparing them. |
697 | | Take local snapshots and hold strong references so INCREF/DECREF |
698 | | apply to the same objects even under re-entrancy. */ |
699 | 0 | PyObject *tgtkey = Py_NewRef(igo->tgtkey); |
700 | 0 | PyObject *currkey = Py_NewRef(gbo->currkey); |
701 | 0 | rcmp = PyObject_RichCompareBool(tgtkey, currkey, Py_EQ); |
702 | 0 | Py_DECREF(tgtkey); |
703 | 0 | Py_DECREF(currkey); |
704 | |
|
705 | 0 | if (rcmp <= 0) |
706 | | /* got any error or current group is end */ |
707 | 0 | return NULL; |
708 | | |
709 | 0 | r = gbo->currvalue; |
710 | 0 | gbo->currvalue = NULL; |
711 | 0 | Py_CLEAR(gbo->currkey); |
712 | |
|
713 | 0 | return r; |
714 | 0 | } |
715 | | |
716 | | static PyType_Slot _grouper_slots[] = { |
717 | | {Py_tp_dealloc, _grouper_dealloc}, |
718 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
719 | | {Py_tp_traverse, _grouper_traverse}, |
720 | | {Py_tp_iter, PyObject_SelfIter}, |
721 | | {Py_tp_iternext, _grouper_next}, |
722 | | {Py_tp_new, itertools__grouper}, |
723 | | {Py_tp_free, PyObject_GC_Del}, |
724 | | {0, NULL}, |
725 | | }; |
726 | | |
727 | | static PyType_Spec _grouper_spec = { |
728 | | .name = "itertools._grouper", |
729 | | .basicsize = sizeof(_grouperobject), |
730 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
731 | | Py_TPFLAGS_IMMUTABLETYPE), |
732 | | .slots = _grouper_slots, |
733 | | }; |
734 | | |
735 | | |
736 | | /* tee object and with supporting function and objects ***********************/ |
737 | | |
738 | | /* The teedataobject pre-allocates space for LINKCELLS number of objects. |
739 | | To help the object fit neatly inside cache lines (space for 16 to 32 |
740 | | pointers), the value should be a multiple of 16 minus space for |
741 | | the other structure members including PyHEAD overhead. The larger the |
742 | | value, the less memory overhead per object and the less time spent |
743 | | allocating/deallocating new links. The smaller the number, the less |
744 | | wasted space and the more rapid freeing of older data. |
745 | | */ |
746 | 0 | #define LINKCELLS 57 |
747 | | |
748 | | typedef struct { |
749 | | PyObject_HEAD |
750 | | PyObject *it; |
751 | | int numread; /* 0 <= numread <= LINKCELLS */ |
752 | | int running; |
753 | | PyObject *nextlink; |
754 | | PyObject *(values[LINKCELLS]); |
755 | | } teedataobject; |
756 | | |
757 | 0 | #define teedataobject_CAST(op) ((teedataobject *)(op)) |
758 | | |
759 | | typedef struct { |
760 | | PyObject_HEAD |
761 | | teedataobject *dataobj; |
762 | | int index; /* 0 <= index <= LINKCELLS */ |
763 | | PyObject *weakreflist; |
764 | | itertools_state *state; |
765 | | } teeobject; |
766 | | |
767 | 0 | #define teeobject_CAST(op) ((teeobject *)(op)) |
768 | | |
769 | | static PyObject * |
770 | | teedataobject_newinternal(itertools_state *state, PyObject *it) |
771 | 0 | { |
772 | 0 | teedataobject *tdo; |
773 | |
|
774 | 0 | tdo = PyObject_GC_New(teedataobject, state->teedataobject_type); |
775 | 0 | if (tdo == NULL) |
776 | 0 | return NULL; |
777 | | |
778 | 0 | tdo->running = 0; |
779 | 0 | tdo->numread = 0; |
780 | 0 | tdo->nextlink = NULL; |
781 | 0 | tdo->it = Py_NewRef(it); |
782 | 0 | PyObject_GC_Track(tdo); |
783 | 0 | return (PyObject *)tdo; |
784 | 0 | } |
785 | | |
786 | | static PyObject * |
787 | | teedataobject_jumplink(itertools_state *state, teedataobject *tdo) |
788 | 0 | { |
789 | 0 | PyObject *link; |
790 | 0 | Py_BEGIN_CRITICAL_SECTION(tdo); |
791 | 0 | if (tdo->nextlink == NULL) |
792 | 0 | tdo->nextlink = teedataobject_newinternal(state, tdo->it); |
793 | 0 | link = Py_XNewRef(tdo->nextlink); |
794 | 0 | Py_END_CRITICAL_SECTION(); |
795 | 0 | return link; |
796 | 0 | } |
797 | | |
798 | | static PyObject * |
799 | | teedataobject_getitem_lock_held(teedataobject *tdo, int i) |
800 | 0 | { |
801 | 0 | PyObject *value; |
802 | |
|
803 | 0 | assert(i < LINKCELLS); |
804 | 0 | if (i < tdo->numread) |
805 | 0 | value = tdo->values[i]; |
806 | 0 | else { |
807 | | /* this is the lead iterator, so fetch more data */ |
808 | 0 | assert(i == tdo->numread); |
809 | 0 | if (tdo->running) { |
810 | 0 | PyErr_SetString(PyExc_RuntimeError, |
811 | 0 | "cannot re-enter the tee iterator"); |
812 | 0 | return NULL; |
813 | 0 | } |
814 | 0 | tdo->running = 1; |
815 | 0 | value = PyIter_Next(tdo->it); |
816 | 0 | tdo->running = 0; |
817 | 0 | if (value == NULL) |
818 | 0 | return NULL; |
819 | 0 | tdo->numread++; |
820 | 0 | tdo->values[i] = value; |
821 | 0 | } |
822 | 0 | return Py_NewRef(value); |
823 | 0 | } |
824 | | |
825 | | static PyObject * |
826 | | teedataobject_getitem(teedataobject *tdo, int i) |
827 | 0 | { |
828 | 0 | PyObject *result; |
829 | 0 | Py_BEGIN_CRITICAL_SECTION(tdo); |
830 | 0 | result = teedataobject_getitem_lock_held(tdo, i); |
831 | 0 | Py_END_CRITICAL_SECTION(); |
832 | 0 | return result; |
833 | 0 | } |
834 | | |
835 | | static int |
836 | | teedataobject_traverse(PyObject *op, visitproc visit, void * arg) |
837 | 0 | { |
838 | 0 | int i; |
839 | 0 | teedataobject *tdo = teedataobject_CAST(op); |
840 | |
|
841 | 0 | Py_VISIT(Py_TYPE(tdo)); |
842 | 0 | Py_VISIT(tdo->it); |
843 | 0 | for (i = 0; i < tdo->numread; i++) |
844 | 0 | Py_VISIT(tdo->values[i]); |
845 | 0 | Py_VISIT(tdo->nextlink); |
846 | 0 | return 0; |
847 | 0 | } |
848 | | |
849 | | static void |
850 | | teedataobject_safe_decref(PyObject *obj) |
851 | 0 | { |
852 | 0 | while (obj && _PyObject_IsUniquelyReferenced(obj)) { |
853 | 0 | teedataobject *tmp = teedataobject_CAST(obj); |
854 | 0 | PyObject *nextlink; |
855 | 0 | Py_BEGIN_CRITICAL_SECTION(obj); |
856 | 0 | nextlink = tmp->nextlink; |
857 | 0 | tmp->nextlink = NULL; |
858 | 0 | Py_END_CRITICAL_SECTION(); |
859 | 0 | Py_SETREF(obj, nextlink); |
860 | 0 | } |
861 | 0 | Py_XDECREF(obj); |
862 | 0 | } |
863 | | |
864 | | static int |
865 | | teedataobject_clear(PyObject *op) |
866 | 0 | { |
867 | 0 | int i; |
868 | 0 | PyObject *tmp; |
869 | 0 | teedataobject *tdo = teedataobject_CAST(op); |
870 | |
|
871 | 0 | Py_BEGIN_CRITICAL_SECTION(op); |
872 | 0 | Py_CLEAR(tdo->it); |
873 | 0 | for (i=0 ; i<tdo->numread ; i++) |
874 | 0 | Py_CLEAR(tdo->values[i]); |
875 | 0 | tmp = tdo->nextlink; |
876 | 0 | tdo->nextlink = NULL; |
877 | 0 | Py_END_CRITICAL_SECTION(); |
878 | 0 | teedataobject_safe_decref(tmp); |
879 | 0 | return 0; |
880 | 0 | } |
881 | | |
882 | | static void |
883 | | teedataobject_dealloc(PyObject *op) |
884 | 0 | { |
885 | 0 | PyTypeObject *tp = Py_TYPE(op); |
886 | 0 | PyObject_GC_UnTrack(op); |
887 | 0 | (void)teedataobject_clear(op); |
888 | 0 | PyObject_GC_Del(op); |
889 | 0 | Py_DECREF(tp); |
890 | 0 | } |
891 | | |
892 | | /*[clinic input] |
893 | | @vectorcall |
894 | | @classmethod |
895 | | itertools.teedataobject.__new__ |
896 | | iterable as it: object |
897 | | values: object(subclass_of='&PyList_Type') |
898 | | next: object |
899 | | / |
900 | | Data container common to multiple tee objects. |
901 | | [clinic start generated code]*/ |
902 | | |
903 | | static PyObject * |
904 | | itertools_teedataobject_impl(PyTypeObject *type, PyObject *it, |
905 | | PyObject *values, PyObject *next) |
906 | | /*[clinic end generated code: output=3343ceb07e08df5e input=02647670a03714c0]*/ |
907 | 0 | { |
908 | 0 | teedataobject *tdo; |
909 | 0 | Py_ssize_t i, len; |
910 | |
|
911 | 0 | itertools_state *state = get_module_state_by_cls(type); |
912 | 0 | assert(type == state->teedataobject_type); |
913 | |
|
914 | 0 | tdo = (teedataobject *)teedataobject_newinternal(state, it); |
915 | 0 | if (!tdo) |
916 | 0 | return NULL; |
917 | | |
918 | 0 | len = PyList_GET_SIZE(values); |
919 | 0 | if (len > LINKCELLS) |
920 | 0 | goto err; |
921 | 0 | for (i=0; i<len; i++) { |
922 | 0 | tdo->values[i] = PyList_GET_ITEM(values, i); |
923 | 0 | Py_INCREF(tdo->values[i]); |
924 | 0 | } |
925 | | /* len <= LINKCELLS < INT_MAX */ |
926 | 0 | tdo->numread = Py_SAFE_DOWNCAST(len, Py_ssize_t, int); |
927 | |
|
928 | 0 | if (len == LINKCELLS) { |
929 | 0 | if (next != Py_None) { |
930 | 0 | if (!Py_IS_TYPE(next, state->teedataobject_type)) |
931 | 0 | goto err; |
932 | 0 | assert(tdo->nextlink == NULL); |
933 | 0 | tdo->nextlink = Py_NewRef(next); |
934 | 0 | } |
935 | 0 | } else { |
936 | 0 | if (next != Py_None) |
937 | 0 | goto err; /* shouldn't have a next if we are not full */ |
938 | 0 | } |
939 | 0 | return (PyObject*)tdo; |
940 | | |
941 | 0 | err: |
942 | 0 | Py_XDECREF(tdo); |
943 | 0 | PyErr_SetString(PyExc_ValueError, "Invalid arguments"); |
944 | 0 | return NULL; |
945 | 0 | } |
946 | | |
947 | | static PyType_Slot teedataobject_slots[] = { |
948 | | {Py_tp_dealloc, teedataobject_dealloc}, |
949 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
950 | | {Py_tp_doc, (void *)itertools_teedataobject__doc__}, |
951 | | {Py_tp_traverse, teedataobject_traverse}, |
952 | | {Py_tp_clear, teedataobject_clear}, |
953 | | {Py_tp_new, itertools_teedataobject}, |
954 | | {Py_tp_vectorcall, itertools_teedataobject_vectorcall}, |
955 | | {Py_tp_free, PyObject_GC_Del}, |
956 | | {0, NULL}, |
957 | | }; |
958 | | |
959 | | static PyType_Spec teedataobject_spec = { |
960 | | .name = "itertools._tee_dataobject", |
961 | | .basicsize = sizeof(teedataobject), |
962 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
963 | | Py_TPFLAGS_IMMUTABLETYPE), |
964 | | .slots = teedataobject_slots, |
965 | | }; |
966 | | |
967 | | |
968 | | static PyObject * |
969 | | tee_next(PyObject *op) |
970 | 0 | { |
971 | 0 | teeobject *to = teeobject_CAST(op); |
972 | 0 | PyObject *value; |
973 | |
|
974 | 0 | #ifndef Py_GIL_DISABLED |
975 | | /* The GIL already serializes access, so keep the simple path without the |
976 | | snapshot and revalidation that the free-threaded build needs. */ |
977 | 0 | if (to->index >= LINKCELLS) { |
978 | 0 | PyObject *link = teedataobject_jumplink(to->state, to->dataobj); |
979 | 0 | if (link == NULL) { |
980 | 0 | return NULL; |
981 | 0 | } |
982 | 0 | Py_SETREF(to->dataobj, (teedataobject *)link); |
983 | 0 | to->index = 0; |
984 | 0 | } |
985 | 0 | value = teedataobject_getitem(to->dataobj, to->index); |
986 | 0 | if (value == NULL) { |
987 | 0 | return NULL; |
988 | 0 | } |
989 | 0 | to->index++; |
990 | 0 | return value; |
991 | | #else |
992 | | for (;;) { |
993 | | teedataobject *dataobj; |
994 | | int index; |
995 | | |
996 | | /* Snapshot the branch position (strong ref to the shared data object) |
997 | | under the tee lock; the data object is locked separately, not nested, |
998 | | then the advance is revalidated. */ |
999 | | Py_BEGIN_CRITICAL_SECTION(op); |
1000 | | dataobj = (teedataobject *)Py_NewRef((PyObject *)to->dataobj); |
1001 | | index = to->index; |
1002 | | Py_END_CRITICAL_SECTION(); |
1003 | | |
1004 | | if (index < LINKCELLS) { |
1005 | | value = teedataobject_getitem(dataobj, index); |
1006 | | if (value != NULL) { |
1007 | | Py_BEGIN_CRITICAL_SECTION(op); |
1008 | | if (to->dataobj == dataobj && to->index == index) { |
1009 | | to->index = index + 1; |
1010 | | } |
1011 | | Py_END_CRITICAL_SECTION(); |
1012 | | } |
1013 | | Py_DECREF(dataobj); |
1014 | | return value; |
1015 | | } |
1016 | | |
1017 | | PyObject *link = teedataobject_jumplink(to->state, dataobj); |
1018 | | if (link == NULL) { |
1019 | | Py_DECREF(dataobj); |
1020 | | return NULL; |
1021 | | } |
1022 | | Py_BEGIN_CRITICAL_SECTION(op); |
1023 | | if (to->dataobj == dataobj) { |
1024 | | Py_SETREF(to->dataobj, (teedataobject *)link); |
1025 | | to->index = 0; |
1026 | | link = NULL; |
1027 | | } |
1028 | | Py_END_CRITICAL_SECTION(); |
1029 | | Py_XDECREF(link); |
1030 | | Py_DECREF(dataobj); |
1031 | | } |
1032 | | #endif |
1033 | 0 | } |
1034 | | |
1035 | | static int |
1036 | | tee_traverse(PyObject *op, visitproc visit, void *arg) |
1037 | 0 | { |
1038 | 0 | teeobject *to = teeobject_CAST(op); |
1039 | 0 | Py_VISIT(Py_TYPE(to)); |
1040 | 0 | Py_VISIT((PyObject *)to->dataobj); |
1041 | 0 | return 0; |
1042 | 0 | } |
1043 | | |
1044 | | static teeobject * |
1045 | | tee_copy_impl(teeobject *to) |
1046 | 0 | { |
1047 | 0 | teeobject *newto = PyObject_GC_New(teeobject, Py_TYPE(to)); |
1048 | 0 | if (newto == NULL) { |
1049 | 0 | return NULL; |
1050 | 0 | } |
1051 | 0 | Py_BEGIN_CRITICAL_SECTION(to); |
1052 | 0 | newto->dataobj = (teedataobject *)Py_NewRef(to->dataobj); |
1053 | 0 | newto->index = to->index; |
1054 | 0 | Py_END_CRITICAL_SECTION(); |
1055 | 0 | newto->weakreflist = NULL; |
1056 | 0 | newto->state = to->state; |
1057 | 0 | PyObject_GC_Track(newto); |
1058 | 0 | return newto; |
1059 | 0 | } |
1060 | | |
1061 | | static inline PyObject * |
1062 | | tee_copy(PyObject *op, PyObject *Py_UNUSED(ignored)) |
1063 | 0 | { |
1064 | 0 | teeobject *to = teeobject_CAST(op); |
1065 | 0 | return (PyObject *)tee_copy_impl(to); |
1066 | 0 | } |
1067 | | |
1068 | | PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator."); |
1069 | | |
1070 | | static PyObject * |
1071 | | tee_fromiterable(itertools_state *state, PyObject *iterable) |
1072 | 0 | { |
1073 | 0 | teeobject *to; |
1074 | 0 | PyObject *it; |
1075 | |
|
1076 | 0 | it = PyObject_GetIter(iterable); |
1077 | 0 | if (it == NULL) |
1078 | 0 | return NULL; |
1079 | 0 | if (PyObject_TypeCheck(it, state->tee_type)) { |
1080 | 0 | to = tee_copy_impl((teeobject *)it); // 'it' can be fast casted |
1081 | 0 | goto done; |
1082 | 0 | } |
1083 | | |
1084 | 0 | PyObject *dataobj = teedataobject_newinternal(state, it); |
1085 | 0 | if (!dataobj) { |
1086 | 0 | to = NULL; |
1087 | 0 | goto done; |
1088 | 0 | } |
1089 | 0 | to = PyObject_GC_New(teeobject, state->tee_type); |
1090 | 0 | if (to == NULL) { |
1091 | 0 | Py_DECREF(dataobj); |
1092 | 0 | goto done; |
1093 | 0 | } |
1094 | 0 | to->dataobj = (teedataobject *)dataobj; |
1095 | 0 | to->index = 0; |
1096 | 0 | to->weakreflist = NULL; |
1097 | 0 | to->state = state; |
1098 | 0 | PyObject_GC_Track(to); |
1099 | 0 | done: |
1100 | 0 | Py_DECREF(it); |
1101 | 0 | return (PyObject *)to; |
1102 | 0 | } |
1103 | | |
1104 | | /*[clinic input] |
1105 | | @classmethod |
1106 | | itertools._tee.__new__ |
1107 | | iterable: object |
1108 | | / |
1109 | | Iterator wrapped to make it copyable. |
1110 | | [clinic start generated code]*/ |
1111 | | |
1112 | | static PyObject * |
1113 | | itertools__tee_impl(PyTypeObject *type, PyObject *iterable) |
1114 | | /*[clinic end generated code: output=b02d3fd26c810c3f input=adc0779d2afe37a2]*/ |
1115 | 0 | { |
1116 | 0 | itertools_state *state = get_module_state_by_cls(type); |
1117 | 0 | return tee_fromiterable(state, iterable); |
1118 | 0 | } |
1119 | | |
1120 | | static int |
1121 | | tee_clear(PyObject *op) |
1122 | 0 | { |
1123 | 0 | teeobject *to = teeobject_CAST(op); |
1124 | 0 | if (to->weakreflist != NULL) |
1125 | 0 | PyObject_ClearWeakRefs(op); |
1126 | 0 | Py_CLEAR(to->dataobj); |
1127 | 0 | return 0; |
1128 | 0 | } |
1129 | | |
1130 | | static void |
1131 | | tee_dealloc(PyObject *op) |
1132 | 0 | { |
1133 | 0 | PyTypeObject *tp = Py_TYPE(op); |
1134 | 0 | PyObject_GC_UnTrack(op); |
1135 | 0 | (void)tee_clear(op); |
1136 | 0 | PyObject_GC_Del(op); |
1137 | 0 | Py_DECREF(tp); |
1138 | 0 | } |
1139 | | |
1140 | | static PyMethodDef tee_methods[] = { |
1141 | | {"__copy__", tee_copy, METH_NOARGS, teecopy_doc}, |
1142 | | {NULL, NULL} /* sentinel */ |
1143 | | }; |
1144 | | |
1145 | | static PyMemberDef tee_members[] = { |
1146 | | {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(teeobject, weakreflist), Py_READONLY}, |
1147 | | {NULL}, |
1148 | | }; |
1149 | | |
1150 | | static PyType_Slot tee_slots[] = { |
1151 | | {Py_tp_dealloc, tee_dealloc}, |
1152 | | {Py_tp_doc, (void *)itertools__tee__doc__}, |
1153 | | {Py_tp_traverse, tee_traverse}, |
1154 | | {Py_tp_clear, tee_clear}, |
1155 | | {Py_tp_iter, PyObject_SelfIter}, |
1156 | | {Py_tp_iternext, tee_next}, |
1157 | | {Py_tp_methods, tee_methods}, |
1158 | | {Py_tp_members, tee_members}, |
1159 | | {Py_tp_new, itertools__tee}, |
1160 | | {Py_tp_free, PyObject_GC_Del}, |
1161 | | {0, NULL}, |
1162 | | }; |
1163 | | |
1164 | | static PyType_Spec tee_spec = { |
1165 | | .name = "itertools._tee", |
1166 | | .basicsize = sizeof(teeobject), |
1167 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
1168 | | Py_TPFLAGS_IMMUTABLETYPE), |
1169 | | .slots = tee_slots, |
1170 | | }; |
1171 | | |
1172 | | /*[clinic input] |
1173 | | itertools.tee |
1174 | | iterable: object |
1175 | | n: Py_ssize_t(allow_negative=False) = 2 |
1176 | | / |
1177 | | Returns a tuple of n independent iterators. |
1178 | | [clinic start generated code]*/ |
1179 | | |
1180 | | static PyObject * |
1181 | | itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n) |
1182 | | /*[clinic end generated code: output=1c64519cd859c2f0 input=0f72d78e655f45cb]*/ |
1183 | 0 | { |
1184 | 0 | Py_ssize_t i; |
1185 | 0 | PyObject *it, *to, *result; |
1186 | |
|
1187 | 0 | result = PyTuple_New(n); |
1188 | 0 | if (result == NULL) |
1189 | 0 | return NULL; |
1190 | 0 | if (n == 0) |
1191 | 0 | return result; |
1192 | 0 | it = PyObject_GetIter(iterable); |
1193 | 0 | if (it == NULL) { |
1194 | 0 | Py_DECREF(result); |
1195 | 0 | return NULL; |
1196 | 0 | } |
1197 | | |
1198 | 0 | itertools_state *state = get_module_state(module); |
1199 | 0 | to = tee_fromiterable(state, it); |
1200 | 0 | Py_DECREF(it); |
1201 | 0 | if (to == NULL) { |
1202 | 0 | Py_DECREF(result); |
1203 | 0 | return NULL; |
1204 | 0 | } |
1205 | | |
1206 | 0 | PyTuple_SET_ITEM(result, 0, to); |
1207 | 0 | for (i = 1; i < n; i++) { |
1208 | 0 | to = tee_copy(to, NULL); |
1209 | 0 | if (to == NULL) { |
1210 | 0 | Py_DECREF(result); |
1211 | 0 | return NULL; |
1212 | 0 | } |
1213 | 0 | PyTuple_SET_ITEM(result, i, to); |
1214 | 0 | } |
1215 | 0 | return result; |
1216 | 0 | } |
1217 | | |
1218 | | |
1219 | | /* cycle object **************************************************************/ |
1220 | | |
1221 | | typedef struct { |
1222 | | PyObject_HEAD |
1223 | | PyObject *it; |
1224 | | PyObject *saved; |
1225 | | Py_ssize_t index; |
1226 | | } cycleobject; |
1227 | | |
1228 | 0 | #define cycleobject_CAST(op) ((cycleobject *)(op)) |
1229 | | |
1230 | | /*[clinic input] |
1231 | | @permit_long_summary |
1232 | | @vectorcall |
1233 | | @classmethod |
1234 | | itertools.cycle.__new__ |
1235 | | iterable: object |
1236 | | / |
1237 | | Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely. |
1238 | | [clinic start generated code]*/ |
1239 | | |
1240 | | static PyObject * |
1241 | | itertools_cycle_impl(PyTypeObject *type, PyObject *iterable) |
1242 | | /*[clinic end generated code: output=f60e5ec17a45b35c input=183a6615ee9f1440]*/ |
1243 | 0 | { |
1244 | 0 | PyObject *it; |
1245 | 0 | PyObject *saved; |
1246 | 0 | cycleobject *lz; |
1247 | | |
1248 | | /* Get iterator. */ |
1249 | 0 | it = PyObject_GetIter(iterable); |
1250 | 0 | if (it == NULL) |
1251 | 0 | return NULL; |
1252 | | |
1253 | 0 | saved = PyList_New(0); |
1254 | 0 | if (saved == NULL) { |
1255 | 0 | Py_DECREF(it); |
1256 | 0 | return NULL; |
1257 | 0 | } |
1258 | | |
1259 | | /* create cycleobject structure */ |
1260 | 0 | lz = (cycleobject *)type->tp_alloc(type, 0); |
1261 | 0 | if (lz == NULL) { |
1262 | 0 | Py_DECREF(it); |
1263 | 0 | Py_DECREF(saved); |
1264 | 0 | return NULL; |
1265 | 0 | } |
1266 | 0 | lz->it = it; |
1267 | 0 | lz->saved = saved; |
1268 | 0 | lz->index = -1; |
1269 | |
|
1270 | 0 | return (PyObject *)lz; |
1271 | 0 | } |
1272 | | |
1273 | | static void |
1274 | | cycle_dealloc(PyObject *op) |
1275 | 0 | { |
1276 | 0 | cycleobject *lz = cycleobject_CAST(op); |
1277 | 0 | PyTypeObject *tp = Py_TYPE(lz); |
1278 | 0 | PyObject_GC_UnTrack(lz); |
1279 | 0 | Py_XDECREF(lz->it); |
1280 | 0 | Py_XDECREF(lz->saved); |
1281 | 0 | tp->tp_free(lz); |
1282 | 0 | Py_DECREF(tp); |
1283 | 0 | } |
1284 | | |
1285 | | static int |
1286 | | cycle_traverse(PyObject *op, visitproc visit, void *arg) |
1287 | 0 | { |
1288 | 0 | cycleobject *lz = cycleobject_CAST(op); |
1289 | 0 | Py_VISIT(Py_TYPE(lz)); |
1290 | 0 | Py_VISIT(lz->it); |
1291 | 0 | Py_VISIT(lz->saved); |
1292 | 0 | return 0; |
1293 | 0 | } |
1294 | | |
1295 | | static PyObject * |
1296 | | cycle_next(PyObject *op) |
1297 | 0 | { |
1298 | 0 | cycleobject *lz = cycleobject_CAST(op); |
1299 | 0 | PyObject *item; |
1300 | |
|
1301 | 0 | Py_ssize_t index = FT_ATOMIC_LOAD_SSIZE_RELAXED(lz->index); |
1302 | |
|
1303 | 0 | if (index < 0) { |
1304 | 0 | item = PyIter_Next(lz->it); |
1305 | 0 | if (item != NULL) { |
1306 | 0 | if (PyList_Append(lz->saved, item)) { |
1307 | 0 | Py_DECREF(item); |
1308 | 0 | return NULL; |
1309 | 0 | } |
1310 | 0 | return item; |
1311 | 0 | } |
1312 | | /* Note: StopIteration is already cleared by PyIter_Next() */ |
1313 | 0 | if (PyErr_Occurred()) |
1314 | 0 | return NULL; |
1315 | 0 | index = 0; |
1316 | 0 | FT_ATOMIC_STORE_SSIZE_RELAXED(lz->index, 0); |
1317 | 0 | #ifndef Py_GIL_DISABLED |
1318 | 0 | Py_CLEAR(lz->it); |
1319 | 0 | #endif |
1320 | 0 | } |
1321 | 0 | if (PyList_GET_SIZE(lz->saved) == 0) |
1322 | 0 | return NULL; |
1323 | 0 | item = PyList_GetItemRef(lz->saved, index); |
1324 | 0 | assert(item); |
1325 | 0 | index++; |
1326 | 0 | if (index >= PyList_GET_SIZE(lz->saved)) { |
1327 | 0 | index = 0; |
1328 | 0 | } |
1329 | 0 | FT_ATOMIC_STORE_SSIZE_RELAXED(lz->index, index); |
1330 | 0 | return item; |
1331 | 0 | } |
1332 | | |
1333 | | static PyType_Slot cycle_slots[] = { |
1334 | | {Py_tp_dealloc, cycle_dealloc}, |
1335 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
1336 | | {Py_tp_doc, (void *)itertools_cycle__doc__}, |
1337 | | {Py_tp_traverse, cycle_traverse}, |
1338 | | {Py_tp_iter, PyObject_SelfIter}, |
1339 | | {Py_tp_iternext, cycle_next}, |
1340 | | {Py_tp_new, itertools_cycle}, |
1341 | | {Py_tp_vectorcall, itertools_cycle_vectorcall}, |
1342 | | {Py_tp_free, PyObject_GC_Del}, |
1343 | | {0, NULL}, |
1344 | | }; |
1345 | | |
1346 | | static PyType_Spec cycle_spec = { |
1347 | | .name = "itertools.cycle", |
1348 | | .basicsize = sizeof(cycleobject), |
1349 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
1350 | | Py_TPFLAGS_IMMUTABLETYPE), |
1351 | | .slots = cycle_slots, |
1352 | | }; |
1353 | | |
1354 | | |
1355 | | /* dropwhile object **********************************************************/ |
1356 | | |
1357 | | typedef struct { |
1358 | | PyObject_HEAD |
1359 | | PyObject *func; |
1360 | | PyObject *it; |
1361 | | long start; |
1362 | | } dropwhileobject; |
1363 | | |
1364 | 0 | #define dropwhileobject_CAST(op) ((dropwhileobject *)(op)) |
1365 | | |
1366 | | /*[clinic input] |
1367 | | @vectorcall |
1368 | | @classmethod |
1369 | | itertools.dropwhile.__new__ |
1370 | | predicate as func: object |
1371 | | iterable as seq: object |
1372 | | / |
1373 | | Drop items from the iterable while predicate(item) is true. |
1374 | | |
1375 | | Afterwards, return every element until the iterable is exhausted. |
1376 | | [clinic start generated code]*/ |
1377 | | |
1378 | | static PyObject * |
1379 | | itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq) |
1380 | | /*[clinic end generated code: output=92f9d0d89af149e4 input=38ed294cc91d3aa8]*/ |
1381 | 0 | { |
1382 | 0 | PyObject *it; |
1383 | 0 | dropwhileobject *lz; |
1384 | | |
1385 | | /* Get iterator. */ |
1386 | 0 | it = PyObject_GetIter(seq); |
1387 | 0 | if (it == NULL) |
1388 | 0 | return NULL; |
1389 | | |
1390 | | /* create dropwhileobject structure */ |
1391 | 0 | lz = (dropwhileobject *)type->tp_alloc(type, 0); |
1392 | 0 | if (lz == NULL) { |
1393 | 0 | Py_DECREF(it); |
1394 | 0 | return NULL; |
1395 | 0 | } |
1396 | 0 | lz->func = Py_NewRef(func); |
1397 | 0 | lz->it = it; |
1398 | 0 | lz->start = 0; |
1399 | |
|
1400 | 0 | return (PyObject *)lz; |
1401 | 0 | } |
1402 | | |
1403 | | static void |
1404 | | dropwhile_dealloc(PyObject *op) |
1405 | 0 | { |
1406 | 0 | dropwhileobject *lz = dropwhileobject_CAST(op); |
1407 | 0 | PyTypeObject *tp = Py_TYPE(lz); |
1408 | 0 | PyObject_GC_UnTrack(lz); |
1409 | 0 | Py_XDECREF(lz->func); |
1410 | 0 | Py_XDECREF(lz->it); |
1411 | 0 | tp->tp_free(lz); |
1412 | 0 | Py_DECREF(tp); |
1413 | 0 | } |
1414 | | |
1415 | | static int |
1416 | | dropwhile_traverse(PyObject *op, visitproc visit, void *arg) |
1417 | 0 | { |
1418 | 0 | dropwhileobject *lz = dropwhileobject_CAST(op); |
1419 | 0 | Py_VISIT(Py_TYPE(lz)); |
1420 | 0 | Py_VISIT(lz->it); |
1421 | 0 | Py_VISIT(lz->func); |
1422 | 0 | return 0; |
1423 | 0 | } |
1424 | | |
1425 | | static PyObject * |
1426 | | dropwhile_next(PyObject *op) |
1427 | 0 | { |
1428 | 0 | dropwhileobject *lz = dropwhileobject_CAST(op); |
1429 | 0 | PyObject *item, *good; |
1430 | 0 | PyObject *it = lz->it; |
1431 | 0 | long ok; |
1432 | 0 | PyObject *(*iternext)(PyObject *); |
1433 | |
|
1434 | 0 | iternext = *Py_TYPE(it)->tp_iternext; |
1435 | 0 | for (;;) { |
1436 | 0 | item = iternext(it); |
1437 | 0 | if (item == NULL) |
1438 | 0 | return NULL; |
1439 | 0 | if (lz->start == 1) |
1440 | 0 | return item; |
1441 | | |
1442 | 0 | good = PyObject_CallOneArg(lz->func, item); |
1443 | 0 | if (good == NULL) { |
1444 | 0 | Py_DECREF(item); |
1445 | 0 | return NULL; |
1446 | 0 | } |
1447 | 0 | ok = PyObject_IsTrue(good); |
1448 | 0 | Py_DECREF(good); |
1449 | 0 | if (ok == 0) { |
1450 | 0 | lz->start = 1; |
1451 | 0 | return item; |
1452 | 0 | } |
1453 | 0 | Py_DECREF(item); |
1454 | 0 | if (ok < 0) |
1455 | 0 | return NULL; |
1456 | 0 | } |
1457 | 0 | } |
1458 | | |
1459 | | static PyType_Slot dropwhile_slots[] = { |
1460 | | {Py_tp_dealloc, dropwhile_dealloc}, |
1461 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
1462 | | {Py_tp_doc, (void *)itertools_dropwhile__doc__}, |
1463 | | {Py_tp_traverse, dropwhile_traverse}, |
1464 | | {Py_tp_iter, PyObject_SelfIter}, |
1465 | | {Py_tp_iternext, dropwhile_next}, |
1466 | | {Py_tp_new, itertools_dropwhile}, |
1467 | | {Py_tp_vectorcall, itertools_dropwhile_vectorcall}, |
1468 | | {Py_tp_free, PyObject_GC_Del}, |
1469 | | {0, NULL}, |
1470 | | }; |
1471 | | |
1472 | | static PyType_Spec dropwhile_spec = { |
1473 | | .name = "itertools.dropwhile", |
1474 | | .basicsize = sizeof(dropwhileobject), |
1475 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
1476 | | Py_TPFLAGS_IMMUTABLETYPE), |
1477 | | .slots = dropwhile_slots, |
1478 | | }; |
1479 | | |
1480 | | |
1481 | | /* takewhile object **********************************************************/ |
1482 | | |
1483 | | typedef struct { |
1484 | | PyObject_HEAD |
1485 | | PyObject *func; |
1486 | | PyObject *it; |
1487 | | long stop; |
1488 | | } takewhileobject; |
1489 | | |
1490 | 0 | #define takewhileobject_CAST(op) ((takewhileobject *)(op)) |
1491 | | |
1492 | | /*[clinic input] |
1493 | | @permit_long_summary |
1494 | | @vectorcall |
1495 | | @classmethod |
1496 | | itertools.takewhile.__new__ |
1497 | | predicate as func: object |
1498 | | iterable as seq: object |
1499 | | / |
1500 | | Return successive entries from an iterable as long as the predicate evaluates to true for each entry. |
1501 | | [clinic start generated code]*/ |
1502 | | |
1503 | | static PyObject * |
1504 | | itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq) |
1505 | | /*[clinic end generated code: output=bb179ea7864e2ef6 input=168fa0aa716792be]*/ |
1506 | 0 | { |
1507 | 0 | PyObject *it; |
1508 | 0 | takewhileobject *lz; |
1509 | | |
1510 | | /* Get iterator. */ |
1511 | 0 | it = PyObject_GetIter(seq); |
1512 | 0 | if (it == NULL) |
1513 | 0 | return NULL; |
1514 | | |
1515 | | /* create takewhileobject structure */ |
1516 | 0 | lz = (takewhileobject *)type->tp_alloc(type, 0); |
1517 | 0 | if (lz == NULL) { |
1518 | 0 | Py_DECREF(it); |
1519 | 0 | return NULL; |
1520 | 0 | } |
1521 | 0 | lz->func = Py_NewRef(func); |
1522 | 0 | lz->it = it; |
1523 | 0 | lz->stop = 0; |
1524 | |
|
1525 | 0 | return (PyObject *)lz; |
1526 | 0 | } |
1527 | | |
1528 | | static void |
1529 | | takewhile_dealloc(PyObject *op) |
1530 | 0 | { |
1531 | 0 | takewhileobject *lz = takewhileobject_CAST(op); |
1532 | 0 | PyTypeObject *tp = Py_TYPE(lz); |
1533 | 0 | PyObject_GC_UnTrack(lz); |
1534 | 0 | Py_XDECREF(lz->func); |
1535 | 0 | Py_XDECREF(lz->it); |
1536 | 0 | tp->tp_free(lz); |
1537 | 0 | Py_DECREF(tp); |
1538 | 0 | } |
1539 | | |
1540 | | static int |
1541 | | takewhile_traverse(PyObject *op, visitproc visit, void *arg) |
1542 | 0 | { |
1543 | 0 | takewhileobject *lz = takewhileobject_CAST(op); |
1544 | 0 | Py_VISIT(Py_TYPE(lz)); |
1545 | 0 | Py_VISIT(lz->it); |
1546 | 0 | Py_VISIT(lz->func); |
1547 | 0 | return 0; |
1548 | 0 | } |
1549 | | |
1550 | | static PyObject * |
1551 | | takewhile_next(PyObject *op) |
1552 | 0 | { |
1553 | 0 | takewhileobject *lz = takewhileobject_CAST(op); |
1554 | 0 | PyObject *item, *good; |
1555 | 0 | PyObject *it = lz->it; |
1556 | 0 | long ok; |
1557 | |
|
1558 | 0 | if (lz->stop == 1) |
1559 | 0 | return NULL; |
1560 | | |
1561 | 0 | item = (*Py_TYPE(it)->tp_iternext)(it); |
1562 | 0 | if (item == NULL) |
1563 | 0 | return NULL; |
1564 | | |
1565 | 0 | good = PyObject_CallOneArg(lz->func, item); |
1566 | 0 | if (good == NULL) { |
1567 | 0 | Py_DECREF(item); |
1568 | 0 | return NULL; |
1569 | 0 | } |
1570 | 0 | ok = PyObject_IsTrue(good); |
1571 | 0 | Py_DECREF(good); |
1572 | 0 | if (ok > 0) |
1573 | 0 | return item; |
1574 | 0 | Py_DECREF(item); |
1575 | 0 | if (ok == 0) |
1576 | 0 | lz->stop = 1; |
1577 | 0 | return NULL; |
1578 | 0 | } |
1579 | | |
1580 | | static PyType_Slot takewhile_slots[] = { |
1581 | | {Py_tp_dealloc, takewhile_dealloc}, |
1582 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
1583 | | {Py_tp_doc, (void *)itertools_takewhile__doc__}, |
1584 | | {Py_tp_traverse, takewhile_traverse}, |
1585 | | {Py_tp_iter, PyObject_SelfIter}, |
1586 | | {Py_tp_iternext, takewhile_next}, |
1587 | | {Py_tp_new, itertools_takewhile}, |
1588 | | {Py_tp_vectorcall, itertools_takewhile_vectorcall}, |
1589 | | {Py_tp_free, PyObject_GC_Del}, |
1590 | | {0, NULL}, |
1591 | | }; |
1592 | | |
1593 | | static PyType_Spec takewhile_spec = { |
1594 | | .name = "itertools.takewhile", |
1595 | | .basicsize = sizeof(takewhileobject), |
1596 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
1597 | | Py_TPFLAGS_IMMUTABLETYPE), |
1598 | | .slots = takewhile_slots, |
1599 | | }; |
1600 | | |
1601 | | |
1602 | | /* islice object *************************************************************/ |
1603 | | |
1604 | | typedef struct { |
1605 | | PyObject_HEAD |
1606 | | PyObject *it; |
1607 | | Py_ssize_t next; |
1608 | | Py_ssize_t stop; |
1609 | | Py_ssize_t step; |
1610 | | Py_ssize_t cnt; |
1611 | | } isliceobject; |
1612 | | |
1613 | 0 | #define isliceobject_CAST(op) ((isliceobject *)(op)) |
1614 | | |
1615 | | static PyObject * |
1616 | | islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
1617 | 0 | { |
1618 | 0 | PyObject *seq; |
1619 | 0 | Py_ssize_t start=0, stop=-1, step=1; |
1620 | 0 | PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL; |
1621 | 0 | Py_ssize_t numargs; |
1622 | 0 | isliceobject *lz; |
1623 | |
|
1624 | 0 | itertools_state *st = find_state_by_type(type); |
1625 | 0 | PyTypeObject *islice_type = st->islice_type; |
1626 | 0 | if ((type == islice_type || type->tp_init == islice_type->tp_init) && |
1627 | 0 | !_PyArg_NoKeywords("islice", kwds)) |
1628 | 0 | return NULL; |
1629 | | |
1630 | 0 | if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3)) |
1631 | 0 | return NULL; |
1632 | | |
1633 | 0 | numargs = PyTuple_Size(args); |
1634 | 0 | if (numargs == 2) { |
1635 | 0 | if (a1 != Py_None) { |
1636 | 0 | stop = PyNumber_AsSsize_t(a1, PyExc_OverflowError); |
1637 | 0 | if (stop == -1) { |
1638 | 0 | if (PyErr_Occurred()) |
1639 | 0 | PyErr_Clear(); |
1640 | 0 | PyErr_SetString(PyExc_ValueError, |
1641 | 0 | "Stop argument for islice() must be None or " |
1642 | 0 | "an integer: 0 <= x <= sys.maxsize."); |
1643 | 0 | return NULL; |
1644 | 0 | } |
1645 | 0 | } |
1646 | 0 | } else { |
1647 | 0 | if (a1 != Py_None) |
1648 | 0 | start = PyNumber_AsSsize_t(a1, PyExc_OverflowError); |
1649 | 0 | if (start == -1 && PyErr_Occurred()) |
1650 | 0 | PyErr_Clear(); |
1651 | 0 | if (a2 != Py_None) { |
1652 | 0 | stop = PyNumber_AsSsize_t(a2, PyExc_OverflowError); |
1653 | 0 | if (stop == -1) { |
1654 | 0 | if (PyErr_Occurred()) |
1655 | 0 | PyErr_Clear(); |
1656 | 0 | PyErr_SetString(PyExc_ValueError, |
1657 | 0 | "Stop argument for islice() must be None or " |
1658 | 0 | "an integer: 0 <= x <= sys.maxsize."); |
1659 | 0 | return NULL; |
1660 | 0 | } |
1661 | 0 | } |
1662 | 0 | } |
1663 | 0 | if (start<0 || stop<-1) { |
1664 | 0 | PyErr_SetString(PyExc_ValueError, |
1665 | 0 | "Indices for islice() must be None or " |
1666 | 0 | "an integer: 0 <= x <= sys.maxsize."); |
1667 | 0 | return NULL; |
1668 | 0 | } |
1669 | | |
1670 | 0 | if (a3 != NULL) { |
1671 | 0 | if (a3 != Py_None) |
1672 | 0 | step = PyNumber_AsSsize_t(a3, PyExc_OverflowError); |
1673 | 0 | if (step == -1 && PyErr_Occurred()) |
1674 | 0 | PyErr_Clear(); |
1675 | 0 | } |
1676 | 0 | if (step<1) { |
1677 | 0 | PyErr_SetString(PyExc_ValueError, |
1678 | 0 | "Step for islice() must be a positive integer or None."); |
1679 | 0 | return NULL; |
1680 | 0 | } |
1681 | | |
1682 | | /* Get iterator. */ |
1683 | 0 | it = PyObject_GetIter(seq); |
1684 | 0 | if (it == NULL) |
1685 | 0 | return NULL; |
1686 | | |
1687 | | /* create isliceobject structure */ |
1688 | 0 | lz = (isliceobject *)type->tp_alloc(type, 0); |
1689 | 0 | if (lz == NULL) { |
1690 | 0 | Py_DECREF(it); |
1691 | 0 | return NULL; |
1692 | 0 | } |
1693 | 0 | lz->it = it; |
1694 | 0 | lz->next = start; |
1695 | 0 | lz->stop = stop; |
1696 | 0 | lz->step = step; |
1697 | 0 | lz->cnt = 0L; |
1698 | |
|
1699 | 0 | return (PyObject *)lz; |
1700 | 0 | } |
1701 | | |
1702 | | static void |
1703 | | islice_dealloc(PyObject *op) |
1704 | 0 | { |
1705 | 0 | isliceobject *lz = isliceobject_CAST(op); |
1706 | 0 | PyTypeObject *tp = Py_TYPE(lz); |
1707 | 0 | PyObject_GC_UnTrack(lz); |
1708 | 0 | Py_XDECREF(lz->it); |
1709 | 0 | tp->tp_free(lz); |
1710 | 0 | Py_DECREF(tp); |
1711 | 0 | } |
1712 | | |
1713 | | static int |
1714 | | islice_traverse(PyObject *op, visitproc visit, void *arg) |
1715 | 0 | { |
1716 | 0 | isliceobject *lz = isliceobject_CAST(op); |
1717 | 0 | Py_VISIT(Py_TYPE(lz)); |
1718 | 0 | Py_VISIT(lz->it); |
1719 | 0 | return 0; |
1720 | 0 | } |
1721 | | |
1722 | | static PyObject * |
1723 | | islice_next(PyObject *op) |
1724 | 0 | { |
1725 | 0 | isliceobject *lz = isliceobject_CAST(op); |
1726 | 0 | PyObject *item; |
1727 | 0 | PyObject *it = lz->it; |
1728 | 0 | Py_ssize_t stop = lz->stop; |
1729 | 0 | Py_ssize_t oldnext; |
1730 | 0 | PyObject *(*iternext)(PyObject *); |
1731 | |
|
1732 | 0 | if (it == NULL) |
1733 | 0 | return NULL; |
1734 | | |
1735 | 0 | iternext = *Py_TYPE(it)->tp_iternext; |
1736 | 0 | while (lz->cnt < lz->next) { |
1737 | 0 | item = iternext(it); |
1738 | 0 | if (item == NULL) |
1739 | 0 | goto empty; |
1740 | 0 | Py_DECREF(item); |
1741 | 0 | lz->cnt++; |
1742 | 0 | } |
1743 | 0 | if (stop != -1 && lz->cnt >= stop) |
1744 | 0 | goto empty; |
1745 | 0 | item = iternext(it); |
1746 | 0 | if (item == NULL) |
1747 | 0 | goto empty; |
1748 | 0 | lz->cnt++; |
1749 | 0 | oldnext = lz->next; |
1750 | | /* The (size_t) cast below avoids the danger of undefined |
1751 | | behaviour from signed integer overflow. */ |
1752 | 0 | lz->next += (size_t)lz->step; |
1753 | 0 | if (lz->next < oldnext || (stop != -1 && lz->next > stop)) |
1754 | 0 | lz->next = stop; |
1755 | 0 | return item; |
1756 | | |
1757 | 0 | empty: |
1758 | 0 | Py_CLEAR(lz->it); |
1759 | 0 | return NULL; |
1760 | 0 | } |
1761 | | |
1762 | | PyDoc_STRVAR(islice_doc, |
1763 | | "islice(iterable, stop) --> islice object\n\ |
1764 | | islice(iterable, start, stop[, step]) --> islice object\n\ |
1765 | | \n\ |
1766 | | Return an iterator whose next() method returns selected values from an\n\ |
1767 | | iterable. If start is specified, will skip all preceding elements;\n\ |
1768 | | otherwise, start defaults to zero. Step defaults to one. If\n\ |
1769 | | specified as another value, step determines how many values are\n\ |
1770 | | skipped between successive calls. Works like a slice() on a list\n\ |
1771 | | but returns an iterator."); |
1772 | | |
1773 | | static PyType_Slot islice_slots[] = { |
1774 | | {Py_tp_dealloc, islice_dealloc}, |
1775 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
1776 | | {Py_tp_doc, (void *)islice_doc}, |
1777 | | {Py_tp_traverse, islice_traverse}, |
1778 | | {Py_tp_iter, PyObject_SelfIter}, |
1779 | | {Py_tp_iternext, islice_next}, |
1780 | | {Py_tp_new, islice_new}, |
1781 | | {Py_tp_free, PyObject_GC_Del}, |
1782 | | {0, NULL}, |
1783 | | }; |
1784 | | |
1785 | | static PyType_Spec islice_spec = { |
1786 | | .name = "itertools.islice", |
1787 | | .basicsize = sizeof(isliceobject), |
1788 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
1789 | | Py_TPFLAGS_IMMUTABLETYPE), |
1790 | | .slots = islice_slots, |
1791 | | }; |
1792 | | |
1793 | | |
1794 | | /* starmap object ************************************************************/ |
1795 | | |
1796 | | typedef struct { |
1797 | | PyObject_HEAD |
1798 | | PyObject *func; |
1799 | | PyObject *it; |
1800 | | } starmapobject; |
1801 | | |
1802 | 0 | #define starmapobject_CAST(op) ((starmapobject *)(op)) |
1803 | | |
1804 | | /*[clinic input] |
1805 | | @permit_long_summary |
1806 | | @vectorcall |
1807 | | @classmethod |
1808 | | itertools.starmap.__new__ |
1809 | | function as func: object |
1810 | | iterable as seq: object |
1811 | | / |
1812 | | Return an iterator whose values are returned from the function evaluated with an argument tuple taken from the given sequence. |
1813 | | [clinic start generated code]*/ |
1814 | | |
1815 | | static PyObject * |
1816 | | itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq) |
1817 | | /*[clinic end generated code: output=79eeb81d452c6e8d input=a7986310dcdb9694]*/ |
1818 | 0 | { |
1819 | 0 | PyObject *it; |
1820 | 0 | starmapobject *lz; |
1821 | | |
1822 | | /* Get iterator. */ |
1823 | 0 | it = PyObject_GetIter(seq); |
1824 | 0 | if (it == NULL) |
1825 | 0 | return NULL; |
1826 | | |
1827 | | /* create starmapobject structure */ |
1828 | 0 | lz = (starmapobject *)type->tp_alloc(type, 0); |
1829 | 0 | if (lz == NULL) { |
1830 | 0 | Py_DECREF(it); |
1831 | 0 | return NULL; |
1832 | 0 | } |
1833 | 0 | lz->func = Py_NewRef(func); |
1834 | 0 | lz->it = it; |
1835 | |
|
1836 | 0 | return (PyObject *)lz; |
1837 | 0 | } |
1838 | | |
1839 | | static void |
1840 | | starmap_dealloc(PyObject *op) |
1841 | 0 | { |
1842 | 0 | starmapobject *lz = starmapobject_CAST(op); |
1843 | 0 | PyTypeObject *tp = Py_TYPE(lz); |
1844 | 0 | PyObject_GC_UnTrack(lz); |
1845 | 0 | Py_XDECREF(lz->func); |
1846 | 0 | Py_XDECREF(lz->it); |
1847 | 0 | tp->tp_free(lz); |
1848 | 0 | Py_DECREF(tp); |
1849 | 0 | } |
1850 | | |
1851 | | static int |
1852 | | starmap_traverse(PyObject *op, visitproc visit, void *arg) |
1853 | 0 | { |
1854 | 0 | starmapobject *lz = starmapobject_CAST(op); |
1855 | 0 | Py_VISIT(Py_TYPE(lz)); |
1856 | 0 | Py_VISIT(lz->it); |
1857 | 0 | Py_VISIT(lz->func); |
1858 | 0 | return 0; |
1859 | 0 | } |
1860 | | |
1861 | | static PyObject * |
1862 | | starmap_next(PyObject *op) |
1863 | 0 | { |
1864 | 0 | starmapobject *lz = starmapobject_CAST(op); |
1865 | 0 | PyObject *args; |
1866 | 0 | PyObject *result; |
1867 | 0 | PyObject *it = lz->it; |
1868 | |
|
1869 | 0 | args = (*Py_TYPE(it)->tp_iternext)(it); |
1870 | 0 | if (args == NULL) |
1871 | 0 | return NULL; |
1872 | 0 | if (!PyTuple_CheckExact(args)) { |
1873 | 0 | PyObject *newargs = PySequence_Tuple(args); |
1874 | 0 | Py_DECREF(args); |
1875 | 0 | if (newargs == NULL) |
1876 | 0 | return NULL; |
1877 | 0 | args = newargs; |
1878 | 0 | } |
1879 | 0 | result = PyObject_Call(lz->func, args, NULL); |
1880 | 0 | Py_DECREF(args); |
1881 | 0 | return result; |
1882 | 0 | } |
1883 | | |
1884 | | static PyType_Slot starmap_slots[] = { |
1885 | | {Py_tp_dealloc, starmap_dealloc}, |
1886 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
1887 | | {Py_tp_doc, (void *)itertools_starmap__doc__}, |
1888 | | {Py_tp_traverse, starmap_traverse}, |
1889 | | {Py_tp_iter, PyObject_SelfIter}, |
1890 | | {Py_tp_iternext, starmap_next}, |
1891 | | {Py_tp_new, itertools_starmap}, |
1892 | | {Py_tp_vectorcall, itertools_starmap_vectorcall}, |
1893 | | {Py_tp_free, PyObject_GC_Del}, |
1894 | | {0, NULL}, |
1895 | | }; |
1896 | | |
1897 | | static PyType_Spec starmap_spec = { |
1898 | | .name = "itertools.starmap", |
1899 | | .basicsize = sizeof(starmapobject), |
1900 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
1901 | | Py_TPFLAGS_IMMUTABLETYPE), |
1902 | | .slots = starmap_slots, |
1903 | | }; |
1904 | | |
1905 | | |
1906 | | /* chain object **************************************************************/ |
1907 | | |
1908 | | struct chainobject { |
1909 | | PyObject_HEAD |
1910 | | PyObject *source; /* Iterator over input iterables */ |
1911 | | PyObject *active; /* Currently running input iterator */ |
1912 | | }; |
1913 | | |
1914 | 15.7k | #define chainobject_CAST(op) ((chainobject *)(op)) |
1915 | | |
1916 | | static PyObject * |
1917 | | chain_new_internal(PyTypeObject *type, PyObject *source) |
1918 | 15.7k | { |
1919 | 15.7k | chainobject *lz; |
1920 | | |
1921 | 15.7k | lz = (chainobject *)type->tp_alloc(type, 0); |
1922 | 15.7k | if (lz == NULL) { |
1923 | 0 | Py_DECREF(source); |
1924 | 0 | return NULL; |
1925 | 0 | } |
1926 | | |
1927 | 15.7k | lz->source = source; |
1928 | 15.7k | lz->active = NULL; |
1929 | 15.7k | return (PyObject *)lz; |
1930 | 15.7k | } |
1931 | | |
1932 | | static PyObject * |
1933 | | chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
1934 | 15.6k | { |
1935 | 15.6k | PyObject *source; |
1936 | | |
1937 | 15.6k | itertools_state *state = find_state_by_type(type); |
1938 | 15.6k | PyTypeObject *chain_type = state->chain_type; |
1939 | 15.6k | if ((type == chain_type || type->tp_init == chain_type->tp_init) && |
1940 | 15.6k | !_PyArg_NoKeywords("chain", kwds)) |
1941 | 0 | return NULL; |
1942 | | |
1943 | 15.6k | source = PyObject_GetIter(args); |
1944 | 15.6k | if (source == NULL) |
1945 | 0 | return NULL; |
1946 | | |
1947 | 15.6k | return chain_new_internal(type, source); |
1948 | 15.6k | } |
1949 | | |
1950 | | /*[clinic input] |
1951 | | @permit_long_summary |
1952 | | @classmethod |
1953 | | itertools.chain.from_iterable |
1954 | | iterable as arg: object |
1955 | | / |
1956 | | Alternative chain() constructor taking a single iterable argument that evaluates lazily. |
1957 | | [clinic start generated code]*/ |
1958 | | |
1959 | | static PyObject * |
1960 | | itertools_chain_from_iterable_impl(PyTypeObject *type, PyObject *arg) |
1961 | | /*[clinic end generated code: output=3d7ea7d46b9e43f5 input=a9bf8227221c75b3]*/ |
1962 | 148 | { |
1963 | 148 | PyObject *source; |
1964 | | |
1965 | 148 | source = PyObject_GetIter(arg); |
1966 | 148 | if (source == NULL) |
1967 | 0 | return NULL; |
1968 | | |
1969 | 148 | return chain_new_internal(type, source); |
1970 | 148 | } |
1971 | | |
1972 | | static void |
1973 | | chain_dealloc(PyObject *op) |
1974 | 15.7k | { |
1975 | 15.7k | chainobject *lz = chainobject_CAST(op); |
1976 | 15.7k | PyTypeObject *tp = Py_TYPE(lz); |
1977 | 15.7k | PyObject_GC_UnTrack(lz); |
1978 | 15.7k | Py_XDECREF(lz->active); |
1979 | 15.7k | Py_XDECREF(lz->source); |
1980 | 15.7k | tp->tp_free(lz); |
1981 | 15.7k | Py_DECREF(tp); |
1982 | 15.7k | } |
1983 | | |
1984 | | static int |
1985 | | chain_traverse(PyObject *op, visitproc visit, void *arg) |
1986 | 16 | { |
1987 | 16 | chainobject *lz = chainobject_CAST(op); |
1988 | 16 | Py_VISIT(Py_TYPE(lz)); |
1989 | 16 | Py_VISIT(lz->source); |
1990 | 16 | Py_VISIT(lz->active); |
1991 | 16 | return 0; |
1992 | 16 | } |
1993 | | |
1994 | | /*[clinic input] |
1995 | | @critical_section |
1996 | | itertools.chain.__next__ as chain_next |
1997 | | [clinic start generated code]*/ |
1998 | | |
1999 | | static PyObject * |
2000 | | chain_next_impl(chainobject *self) |
2001 | | /*[clinic end generated code: output=91e716f45def7fa9 input=e17098e9dc88706b]*/ |
2002 | 102k | { |
2003 | 102k | chainobject *lz = self; |
2004 | 102k | PyObject *item; |
2005 | | |
2006 | | /* lz->source is the iterator of iterables. If it's NULL, we've already |
2007 | | * consumed them all. lz->active is the current iterator. If it's NULL, |
2008 | | * we should grab a new one from lz->source. */ |
2009 | 134k | while (lz->source != NULL) { |
2010 | 134k | if (lz->active == NULL) { |
2011 | 47.0k | PyObject *iterable = PyIter_Next(lz->source); |
2012 | 47.0k | if (iterable == NULL) { |
2013 | 15.7k | Py_CLEAR(lz->source); |
2014 | 15.7k | return NULL; /* no more input sources */ |
2015 | 15.7k | } |
2016 | 31.2k | lz->active = PyObject_GetIter(iterable); |
2017 | 31.2k | Py_DECREF(iterable); |
2018 | 31.2k | if (lz->active == NULL) { |
2019 | 0 | Py_CLEAR(lz->source); |
2020 | 0 | return NULL; /* input not iterable */ |
2021 | 0 | } |
2022 | 31.2k | } |
2023 | 118k | item = (*Py_TYPE(lz->active)->tp_iternext)(lz->active); |
2024 | 118k | if (item != NULL) |
2025 | 87.1k | return item; |
2026 | 31.2k | if (PyErr_Occurred()) { |
2027 | 0 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
2028 | 0 | PyErr_Clear(); |
2029 | 0 | else |
2030 | 0 | return NULL; /* input raised an exception */ |
2031 | 0 | } |
2032 | | /* lz->active is consumed, try with the next iterable. */ |
2033 | 31.2k | Py_CLEAR(lz->active); |
2034 | 31.2k | } |
2035 | | /* Everything had been consumed already. */ |
2036 | 0 | return NULL; |
2037 | 102k | } |
2038 | | |
2039 | | PyDoc_STRVAR(chain_doc, |
2040 | | "chain(*iterables)\n\ |
2041 | | --\n\ |
2042 | | \n\ |
2043 | | Return a chain object whose .__next__() method returns elements from the\n\ |
2044 | | first iterable until it is exhausted, then elements from the next\n\ |
2045 | | iterable, until all of the iterables are exhausted."); |
2046 | | |
2047 | | PyDoc_STRVAR(chain_class_getitem_doc, |
2048 | | "chain is generic over the type of its contents.\n\ |
2049 | | This is the union of the types of the input iterable contents."); |
2050 | | |
2051 | | static PyMethodDef chain_methods[] = { |
2052 | | ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF |
2053 | | {"__class_getitem__", Py_GenericAlias, |
2054 | | METH_O|METH_CLASS, chain_class_getitem_doc}, |
2055 | | {NULL, NULL} /* sentinel */ |
2056 | | }; |
2057 | | |
2058 | | static PyType_Slot chain_slots[] = { |
2059 | | {Py_tp_dealloc, chain_dealloc}, |
2060 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
2061 | | {Py_tp_doc, (void *)chain_doc}, |
2062 | | {Py_tp_traverse, chain_traverse}, |
2063 | | {Py_tp_iter, PyObject_SelfIter}, |
2064 | | {Py_tp_iternext, chain_next}, |
2065 | | {Py_tp_methods, chain_methods}, |
2066 | | {Py_tp_new, chain_new}, |
2067 | | {Py_tp_free, PyObject_GC_Del}, |
2068 | | {0, NULL}, |
2069 | | }; |
2070 | | |
2071 | | static PyType_Spec chain_spec = { |
2072 | | .name = "itertools.chain", |
2073 | | .basicsize = sizeof(chainobject), |
2074 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
2075 | | Py_TPFLAGS_IMMUTABLETYPE), |
2076 | | .slots = chain_slots, |
2077 | | }; |
2078 | | |
2079 | | |
2080 | | /* product object ************************************************************/ |
2081 | | |
2082 | | struct productobject { |
2083 | | PyObject_HEAD |
2084 | | PyObject *pools; /* tuple of pool tuples */ |
2085 | | Py_ssize_t *indices; /* one index per pool */ |
2086 | | PyObject *result; /* most recently returned result tuple */ |
2087 | | int stopped; /* set to 1 when the iterator is exhausted */ |
2088 | | }; |
2089 | | |
2090 | 198 | #define productobject_CAST(op) ((productobject *)(op)) |
2091 | | |
2092 | | static PyObject * |
2093 | | product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
2094 | 198 | { |
2095 | 198 | productobject *lz; |
2096 | 198 | Py_ssize_t nargs, npools, repeat=1; |
2097 | 198 | PyObject *pools = NULL; |
2098 | 198 | Py_ssize_t *indices = NULL; |
2099 | 198 | Py_ssize_t i; |
2100 | | |
2101 | 198 | if (kwds != NULL) { |
2102 | 0 | char *kwlist[] = {"repeat", 0}; |
2103 | 0 | PyObject *tmpargs = PyTuple_New(0); |
2104 | 0 | if (tmpargs == NULL) |
2105 | 0 | return NULL; |
2106 | 0 | if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product", |
2107 | 0 | kwlist, &repeat)) { |
2108 | 0 | Py_DECREF(tmpargs); |
2109 | 0 | return NULL; |
2110 | 0 | } |
2111 | 0 | Py_DECREF(tmpargs); |
2112 | 0 | if (repeat < 0) { |
2113 | 0 | PyErr_SetString(PyExc_ValueError, |
2114 | 0 | "repeat argument cannot be negative"); |
2115 | 0 | return NULL; |
2116 | 0 | } |
2117 | 0 | } |
2118 | | |
2119 | 198 | assert(PyTuple_CheckExact(args)); |
2120 | 198 | if (repeat == 0) { |
2121 | 0 | nargs = 0; |
2122 | 198 | } else { |
2123 | 198 | nargs = PyTuple_GET_SIZE(args); |
2124 | 198 | if ((size_t)nargs > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)/repeat) { |
2125 | 0 | PyErr_SetString(PyExc_OverflowError, "repeat argument too large"); |
2126 | 0 | return NULL; |
2127 | 0 | } |
2128 | 198 | } |
2129 | 198 | npools = nargs * repeat; |
2130 | | |
2131 | 198 | indices = PyMem_New(Py_ssize_t, npools); |
2132 | 198 | if (indices == NULL) { |
2133 | 0 | PyErr_NoMemory(); |
2134 | 0 | goto error; |
2135 | 0 | } |
2136 | | |
2137 | 198 | pools = PyTuple_New(npools); |
2138 | 198 | if (pools == NULL) |
2139 | 0 | goto error; |
2140 | | |
2141 | 504 | for (i=0; i < nargs ; ++i) { |
2142 | 306 | PyObject *item = PyTuple_GET_ITEM(args, i); |
2143 | 306 | PyObject *pool = PySequence_Tuple(item); |
2144 | 306 | if (pool == NULL) |
2145 | 0 | goto error; |
2146 | 306 | PyTuple_SET_ITEM(pools, i, pool); |
2147 | 306 | indices[i] = 0; |
2148 | 306 | } |
2149 | 198 | for ( ; i < npools; ++i) { |
2150 | 0 | PyObject *pool = PyTuple_GET_ITEM(pools, i - nargs); |
2151 | 0 | Py_INCREF(pool); |
2152 | 0 | PyTuple_SET_ITEM(pools, i, pool); |
2153 | 0 | indices[i] = 0; |
2154 | 0 | } |
2155 | | |
2156 | | /* create productobject structure */ |
2157 | 198 | lz = (productobject *)type->tp_alloc(type, 0); |
2158 | 198 | if (lz == NULL) |
2159 | 0 | goto error; |
2160 | | |
2161 | 198 | lz->pools = pools; |
2162 | 198 | lz->indices = indices; |
2163 | 198 | lz->result = NULL; |
2164 | 198 | lz->stopped = 0; |
2165 | | |
2166 | 198 | return (PyObject *)lz; |
2167 | | |
2168 | 0 | error: |
2169 | 0 | if (indices != NULL) |
2170 | 0 | PyMem_Free(indices); |
2171 | 0 | Py_XDECREF(pools); |
2172 | 0 | return NULL; |
2173 | 198 | } |
2174 | | |
2175 | | static void |
2176 | | product_dealloc(PyObject *op) |
2177 | 198 | { |
2178 | 198 | productobject *lz = productobject_CAST(op); |
2179 | 198 | PyTypeObject *tp = Py_TYPE(lz); |
2180 | 198 | PyObject_GC_UnTrack(lz); |
2181 | 198 | Py_XDECREF(lz->pools); |
2182 | 198 | Py_XDECREF(lz->result); |
2183 | 198 | PyMem_Free(lz->indices); |
2184 | 198 | tp->tp_free(lz); |
2185 | 198 | Py_DECREF(tp); |
2186 | 198 | } |
2187 | | |
2188 | | static PyObject * |
2189 | | product_sizeof(PyObject *op, PyObject *Py_UNUSED(ignored)) |
2190 | 0 | { |
2191 | 0 | productobject *lz = productobject_CAST(op); |
2192 | 0 | size_t res = _PyObject_SIZE(Py_TYPE(lz)); |
2193 | 0 | res += (size_t)PyTuple_GET_SIZE(lz->pools) * sizeof(Py_ssize_t); |
2194 | 0 | return PyLong_FromSize_t(res); |
2195 | 0 | } |
2196 | | |
2197 | | PyDoc_STRVAR(sizeof_doc, "Returns size in memory, in bytes."); |
2198 | | |
2199 | | static int |
2200 | | product_traverse(PyObject *op, visitproc visit, void *arg) |
2201 | 0 | { |
2202 | 0 | productobject *lz = productobject_CAST(op); |
2203 | 0 | Py_VISIT(Py_TYPE(lz)); |
2204 | 0 | Py_VISIT(lz->pools); |
2205 | 0 | Py_VISIT(lz->result); |
2206 | 0 | return 0; |
2207 | 0 | } |
2208 | | |
2209 | | /*[clinic input] |
2210 | | @critical_section |
2211 | | itertools.product.__next__ as product_next |
2212 | | [clinic start generated code]*/ |
2213 | | |
2214 | | static PyObject * |
2215 | | product_next_impl(productobject *self) |
2216 | | /*[clinic end generated code: output=d20b8571608c2043 input=54266fe7ed4a857a]*/ |
2217 | 810 | { |
2218 | 810 | productobject *lz = self; |
2219 | 810 | PyObject *pool; |
2220 | 810 | PyObject *elem; |
2221 | 810 | PyObject *oldelem; |
2222 | 810 | PyObject *pools = lz->pools; |
2223 | 810 | PyObject *result = lz->result; |
2224 | 810 | Py_ssize_t npools = PyTuple_GET_SIZE(pools); |
2225 | 810 | Py_ssize_t i; |
2226 | | |
2227 | 810 | if (lz->stopped) |
2228 | 0 | return NULL; |
2229 | | |
2230 | 810 | if (result == NULL) { |
2231 | | /* On the first pass, return an initial tuple filled with the |
2232 | | first element from each pool. */ |
2233 | 198 | result = PyTuple_New(npools); |
2234 | 198 | if (result == NULL) |
2235 | 0 | goto empty; |
2236 | 198 | lz->result = result; |
2237 | 504 | for (i=0; i < npools; i++) { |
2238 | 306 | pool = PyTuple_GET_ITEM(pools, i); |
2239 | 306 | if (PyTuple_GET_SIZE(pool) == 0) |
2240 | 0 | goto empty; |
2241 | 306 | elem = PyTuple_GET_ITEM(pool, 0); |
2242 | 306 | Py_INCREF(elem); |
2243 | 306 | PyTuple_SET_ITEM(result, i, elem); |
2244 | 306 | } |
2245 | 612 | } else { |
2246 | 612 | Py_ssize_t *indices = lz->indices; |
2247 | | |
2248 | | /* Copy the previous result tuple or re-use it if available */ |
2249 | 612 | if (!_PyObject_IsUniquelyReferenced(result)) { |
2250 | 612 | PyObject *old_result = result; |
2251 | 612 | result = PyTuple_FromArray(_PyTuple_ITEMS(old_result), npools); |
2252 | 612 | if (result == NULL) |
2253 | 0 | goto empty; |
2254 | 612 | lz->result = result; |
2255 | 612 | Py_DECREF(old_result); |
2256 | 612 | } |
2257 | | // bpo-42536: The GC may have untracked this result tuple. Since we're |
2258 | | // recycling it, make sure it's tracked again: |
2259 | 0 | else { |
2260 | 0 | _PyTuple_Recycle(result); |
2261 | 0 | } |
2262 | | /* Now, we've got the only copy so we can update it in-place */ |
2263 | 612 | assert (npools==0 || Py_REFCNT(result) == 1); |
2264 | | |
2265 | | /* Update the pool indices right-to-left. Only advance to the |
2266 | | next pool when the previous one rolls-over */ |
2267 | 1.02k | for (i=npools-1 ; i >= 0 ; i--) { |
2268 | 828 | pool = PyTuple_GET_ITEM(pools, i); |
2269 | 828 | indices[i]++; |
2270 | 828 | if (indices[i] == PyTuple_GET_SIZE(pool)) { |
2271 | | /* Roll-over and advance to next pool */ |
2272 | 414 | indices[i] = 0; |
2273 | 414 | elem = PyTuple_GET_ITEM(pool, 0); |
2274 | 414 | Py_INCREF(elem); |
2275 | 414 | oldelem = PyTuple_GET_ITEM(result, i); |
2276 | 414 | PyTuple_SET_ITEM(result, i, elem); |
2277 | 414 | Py_DECREF(oldelem); |
2278 | 414 | } else { |
2279 | | /* No rollover. Just increment and stop here. */ |
2280 | 414 | elem = PyTuple_GET_ITEM(pool, indices[i]); |
2281 | 414 | Py_INCREF(elem); |
2282 | 414 | oldelem = PyTuple_GET_ITEM(result, i); |
2283 | 414 | PyTuple_SET_ITEM(result, i, elem); |
2284 | 414 | Py_DECREF(oldelem); |
2285 | 414 | break; |
2286 | 414 | } |
2287 | 828 | } |
2288 | | |
2289 | | /* If i is negative, then the indices have all rolled-over |
2290 | | and we're done. */ |
2291 | 612 | if (i < 0) |
2292 | 198 | goto empty; |
2293 | 612 | } |
2294 | | |
2295 | 612 | return Py_NewRef(result); |
2296 | | |
2297 | 198 | empty: |
2298 | 198 | lz->stopped = 1; |
2299 | 198 | return NULL; |
2300 | 810 | } |
2301 | | |
2302 | | static PyMethodDef product_methods[] = { |
2303 | | {"__sizeof__", product_sizeof, METH_NOARGS, sizeof_doc}, |
2304 | | {NULL, NULL} /* sentinel */ |
2305 | | }; |
2306 | | |
2307 | | PyDoc_STRVAR(product_doc, |
2308 | | "product(*iterables, repeat=1)\n\ |
2309 | | --\n\ |
2310 | | \n\ |
2311 | | Cartesian product of input iterables. Equivalent to nested for-loops.\n\n\ |
2312 | | For example, product(A, B) returns the same as: ((x,y) for x in A for y in B).\n\ |
2313 | | The leftmost iterators are in the outermost for-loop, so the output tuples\n\ |
2314 | | cycle in a manner similar to an odometer (with the rightmost element changing\n\ |
2315 | | on every iteration).\n\n\ |
2316 | | To compute the product of an iterable with itself, specify the number\n\ |
2317 | | of repetitions with the optional repeat keyword argument. For example,\n\ |
2318 | | product(A, repeat=4) means the same as product(A, A, A, A).\n\n\ |
2319 | | product('ab', range(3)) --> ('a',0) ('a',1) ('a',2) ('b',0) ('b',1) ('b',2)\n\ |
2320 | | product((0,1), (0,1), (0,1)) --> (0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) ..."); |
2321 | | |
2322 | | static PyType_Slot product_slots[] = { |
2323 | | {Py_tp_dealloc, product_dealloc}, |
2324 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
2325 | | {Py_tp_doc, (void *)product_doc}, |
2326 | | {Py_tp_traverse, product_traverse}, |
2327 | | {Py_tp_iter, PyObject_SelfIter}, |
2328 | | {Py_tp_iternext, product_next}, |
2329 | | {Py_tp_methods, product_methods}, |
2330 | | {Py_tp_new, product_new}, |
2331 | | {Py_tp_free, PyObject_GC_Del}, |
2332 | | {0, NULL}, |
2333 | | }; |
2334 | | |
2335 | | static PyType_Spec product_spec = { |
2336 | | .name = "itertools.product", |
2337 | | .basicsize = sizeof(productobject), |
2338 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
2339 | | Py_TPFLAGS_IMMUTABLETYPE), |
2340 | | .slots = product_slots, |
2341 | | }; |
2342 | | |
2343 | | |
2344 | | /* combinations object *******************************************************/ |
2345 | | |
2346 | | struct combinationsobject { |
2347 | | PyObject_HEAD |
2348 | | PyObject *pool; /* input converted to a tuple */ |
2349 | | Py_ssize_t *indices; /* one index per result element */ |
2350 | | PyObject *result; /* most recently returned result tuple */ |
2351 | | Py_ssize_t r; /* size of result tuple */ |
2352 | | int stopped; /* set to 1 when the iterator is exhausted */ |
2353 | | }; |
2354 | | |
2355 | 0 | #define combinationsobject_CAST(op) ((combinationsobject *)(op)) |
2356 | | |
2357 | | /*[clinic input] |
2358 | | @vectorcall |
2359 | | @classmethod |
2360 | | itertools.combinations.__new__ |
2361 | | iterable: object |
2362 | | r: Py_ssize_t(allow_negative=False) |
2363 | | Return successive r-length combinations of elements in the iterable. |
2364 | | |
2365 | | combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3) |
2366 | | [clinic start generated code]*/ |
2367 | | |
2368 | | static PyObject * |
2369 | | itertools_combinations_impl(PyTypeObject *type, PyObject *iterable, |
2370 | | Py_ssize_t r) |
2371 | | /*[clinic end generated code: output=87a689b39c40039c input=119eb0d9196abfe3]*/ |
2372 | 0 | { |
2373 | 0 | combinationsobject *co; |
2374 | 0 | Py_ssize_t n; |
2375 | 0 | PyObject *pool = NULL; |
2376 | 0 | Py_ssize_t *indices = NULL; |
2377 | 0 | Py_ssize_t i; |
2378 | |
|
2379 | 0 | pool = PySequence_Tuple(iterable); |
2380 | 0 | if (pool == NULL) |
2381 | 0 | goto error; |
2382 | 0 | n = PyTuple_GET_SIZE(pool); |
2383 | |
|
2384 | 0 | indices = PyMem_New(Py_ssize_t, r); |
2385 | 0 | if (indices == NULL) { |
2386 | 0 | PyErr_NoMemory(); |
2387 | 0 | goto error; |
2388 | 0 | } |
2389 | | |
2390 | 0 | for (i=0 ; i<r ; i++) |
2391 | 0 | indices[i] = i; |
2392 | | |
2393 | | /* create combinationsobject structure */ |
2394 | 0 | co = (combinationsobject *)type->tp_alloc(type, 0); |
2395 | 0 | if (co == NULL) |
2396 | 0 | goto error; |
2397 | | |
2398 | 0 | co->pool = pool; |
2399 | 0 | co->indices = indices; |
2400 | 0 | co->result = NULL; |
2401 | 0 | co->r = r; |
2402 | 0 | co->stopped = r > n ? 1 : 0; |
2403 | |
|
2404 | 0 | return (PyObject *)co; |
2405 | | |
2406 | 0 | error: |
2407 | 0 | if (indices != NULL) |
2408 | 0 | PyMem_Free(indices); |
2409 | 0 | Py_XDECREF(pool); |
2410 | 0 | return NULL; |
2411 | 0 | } |
2412 | | |
2413 | | static void |
2414 | | combinations_dealloc(PyObject *op) |
2415 | 0 | { |
2416 | 0 | combinationsobject *co = combinationsobject_CAST(op); |
2417 | 0 | PyTypeObject *tp = Py_TYPE(co); |
2418 | 0 | PyObject_GC_UnTrack(co); |
2419 | 0 | Py_XDECREF(co->pool); |
2420 | 0 | Py_XDECREF(co->result); |
2421 | 0 | PyMem_Free(co->indices); |
2422 | 0 | tp->tp_free(co); |
2423 | 0 | Py_DECREF(tp); |
2424 | 0 | } |
2425 | | |
2426 | | static PyObject * |
2427 | | combinations_sizeof(PyObject *op, PyObject *Py_UNUSED(args)) |
2428 | 0 | { |
2429 | 0 | combinationsobject *co = combinationsobject_CAST(op); |
2430 | 0 | size_t res = _PyObject_SIZE(Py_TYPE(co)); |
2431 | 0 | res += (size_t)co->r * sizeof(Py_ssize_t); |
2432 | 0 | return PyLong_FromSize_t(res); |
2433 | 0 | } |
2434 | | |
2435 | | static int |
2436 | | combinations_traverse(PyObject *op, visitproc visit, void *arg) |
2437 | 0 | { |
2438 | 0 | combinationsobject *co = combinationsobject_CAST(op); |
2439 | 0 | Py_VISIT(Py_TYPE(co)); |
2440 | 0 | Py_VISIT(co->pool); |
2441 | 0 | Py_VISIT(co->result); |
2442 | 0 | return 0; |
2443 | 0 | } |
2444 | | |
2445 | | /*[clinic input] |
2446 | | @critical_section |
2447 | | itertools.combinations.__next__ as combinations_next |
2448 | | [clinic start generated code]*/ |
2449 | | |
2450 | | static PyObject * |
2451 | | combinations_next_impl(combinationsobject *self) |
2452 | | /*[clinic end generated code: output=19965c1a1f4b8f2a input=93000f6ec48380d7]*/ |
2453 | 0 | { |
2454 | 0 | combinationsobject *co = self; |
2455 | 0 | PyObject *elem; |
2456 | 0 | PyObject *oldelem; |
2457 | 0 | PyObject *pool = co->pool; |
2458 | 0 | Py_ssize_t *indices = co->indices; |
2459 | 0 | PyObject *result = co->result; |
2460 | 0 | Py_ssize_t n = PyTuple_GET_SIZE(pool); |
2461 | 0 | Py_ssize_t r = co->r; |
2462 | 0 | Py_ssize_t i, j, index; |
2463 | |
|
2464 | 0 | if (co->stopped) |
2465 | 0 | return NULL; |
2466 | | |
2467 | 0 | if (result == NULL) { |
2468 | | /* On the first pass, initialize result tuple using the indices */ |
2469 | 0 | result = PyTuple_New(r); |
2470 | 0 | if (result == NULL) |
2471 | 0 | goto empty; |
2472 | 0 | co->result = result; |
2473 | 0 | for (i=0; i<r ; i++) { |
2474 | 0 | index = indices[i]; |
2475 | 0 | elem = PyTuple_GET_ITEM(pool, index); |
2476 | 0 | Py_INCREF(elem); |
2477 | 0 | PyTuple_SET_ITEM(result, i, elem); |
2478 | 0 | } |
2479 | 0 | } else { |
2480 | | /* Copy the previous result tuple or re-use it if available */ |
2481 | 0 | if (!_PyObject_IsUniquelyReferenced(result)) { |
2482 | 0 | PyObject *old_result = result; |
2483 | 0 | result = PyTuple_FromArray(_PyTuple_ITEMS(old_result), r); |
2484 | 0 | if (result == NULL) |
2485 | 0 | goto empty; |
2486 | 0 | co->result = result; |
2487 | 0 | Py_DECREF(old_result); |
2488 | 0 | } |
2489 | | // bpo-42536: The GC may have untracked this result tuple. Since we're |
2490 | | // recycling it, make sure it's tracked again: |
2491 | 0 | else { |
2492 | 0 | _PyTuple_Recycle(result); |
2493 | 0 | } |
2494 | | /* Now, we've got the only copy so we can update it in-place |
2495 | | * CPython's empty tuple is a singleton and cached in |
2496 | | * PyTuple's freelist. |
2497 | | */ |
2498 | 0 | assert(r == 0 || Py_REFCNT(result) == 1); |
2499 | | |
2500 | | /* Scan indices right-to-left until finding one that is not |
2501 | | at its maximum (i + n - r). */ |
2502 | 0 | for (i=r-1 ; i >= 0 && indices[i] == i+n-r ; i--) |
2503 | 0 | ; |
2504 | | |
2505 | | /* If i is negative, then the indices are all at |
2506 | | their maximum value and we're done. */ |
2507 | 0 | if (i < 0) |
2508 | 0 | goto empty; |
2509 | | |
2510 | | /* Increment the current index which we know is not at its |
2511 | | maximum. Then move back to the right setting each index |
2512 | | to its lowest possible value (one higher than the index |
2513 | | to its left -- this maintains the sort order invariant). */ |
2514 | 0 | indices[i]++; |
2515 | 0 | for (j=i+1 ; j<r ; j++) |
2516 | 0 | indices[j] = indices[j-1] + 1; |
2517 | | |
2518 | | /* Update the result tuple for the new indices |
2519 | | starting with i, the leftmost index that changed */ |
2520 | 0 | for ( ; i<r ; i++) { |
2521 | 0 | index = indices[i]; |
2522 | 0 | elem = PyTuple_GET_ITEM(pool, index); |
2523 | 0 | Py_INCREF(elem); |
2524 | 0 | oldelem = PyTuple_GET_ITEM(result, i); |
2525 | 0 | PyTuple_SET_ITEM(result, i, elem); |
2526 | 0 | Py_DECREF(oldelem); |
2527 | 0 | } |
2528 | 0 | } |
2529 | | |
2530 | 0 | return Py_NewRef(result); |
2531 | | |
2532 | 0 | empty: |
2533 | 0 | co->stopped = 1; |
2534 | 0 | return NULL; |
2535 | 0 | } |
2536 | | |
2537 | | static PyMethodDef combinations_methods[] = { |
2538 | | {"__sizeof__", combinations_sizeof, METH_NOARGS, sizeof_doc}, |
2539 | | {NULL, NULL} /* sentinel */ |
2540 | | }; |
2541 | | |
2542 | | static PyType_Slot combinations_slots[] = { |
2543 | | {Py_tp_dealloc, combinations_dealloc}, |
2544 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
2545 | | {Py_tp_doc, (void *)itertools_combinations__doc__}, |
2546 | | {Py_tp_traverse, combinations_traverse}, |
2547 | | {Py_tp_iter, PyObject_SelfIter}, |
2548 | | {Py_tp_iternext, combinations_next}, |
2549 | | {Py_tp_methods, combinations_methods}, |
2550 | | {Py_tp_new, itertools_combinations}, |
2551 | | {Py_tp_vectorcall, itertools_combinations_vectorcall}, |
2552 | | {Py_tp_free, PyObject_GC_Del}, |
2553 | | {0, NULL}, |
2554 | | }; |
2555 | | |
2556 | | static PyType_Spec combinations_spec = { |
2557 | | .name = "itertools.combinations", |
2558 | | .basicsize = sizeof(combinationsobject), |
2559 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
2560 | | Py_TPFLAGS_IMMUTABLETYPE), |
2561 | | .slots = combinations_slots, |
2562 | | }; |
2563 | | |
2564 | | |
2565 | | /* combinations with replacement object **************************************/ |
2566 | | |
2567 | | /* Equivalent to: |
2568 | | |
2569 | | def combinations_with_replacement(iterable, r): |
2570 | | "combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC" |
2571 | | # number items returned: (n+r-1)! / r! / (n-1)! |
2572 | | pool = tuple(iterable) |
2573 | | n = len(pool) |
2574 | | indices = [0] * r |
2575 | | yield tuple(pool[i] for i in indices) |
2576 | | while 1: |
2577 | | for i in reversed(range(r)): |
2578 | | if indices[i] != n - 1: |
2579 | | break |
2580 | | else: |
2581 | | return |
2582 | | indices[i:] = [indices[i] + 1] * (r - i) |
2583 | | yield tuple(pool[i] for i in indices) |
2584 | | |
2585 | | def combinations_with_replacement2(iterable, r): |
2586 | | 'Alternate version that filters from product()' |
2587 | | pool = tuple(iterable) |
2588 | | n = len(pool) |
2589 | | for indices in product(range(n), repeat=r): |
2590 | | if sorted(indices) == list(indices): |
2591 | | yield tuple(pool[i] for i in indices) |
2592 | | */ |
2593 | | struct cwrobject { |
2594 | | PyObject_HEAD |
2595 | | PyObject *pool; /* input converted to a tuple */ |
2596 | | Py_ssize_t *indices; /* one index per result element */ |
2597 | | PyObject *result; /* most recently returned result tuple */ |
2598 | | Py_ssize_t r; /* size of result tuple */ |
2599 | | int stopped; /* set to 1 when the cwr iterator is exhausted */ |
2600 | | }; |
2601 | | |
2602 | 0 | #define cwrobject_CAST(op) ((cwrobject *)(op)) |
2603 | | |
2604 | | /*[clinic input] |
2605 | | @permit_long_summary |
2606 | | @permit_long_docstring_body |
2607 | | @vectorcall |
2608 | | @classmethod |
2609 | | itertools.combinations_with_replacement.__new__ |
2610 | | iterable: object |
2611 | | r: Py_ssize_t(allow_negative=False) |
2612 | | Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats. |
2613 | | |
2614 | | combinations_with_replacement('ABC', 2) --> ('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C') |
2615 | | [clinic start generated code]*/ |
2616 | | |
2617 | | static PyObject * |
2618 | | itertools_combinations_with_replacement_impl(PyTypeObject *type, |
2619 | | PyObject *iterable, |
2620 | | Py_ssize_t r) |
2621 | | /*[clinic end generated code: output=48b26856d4e659ca input=8bd0382a7253de95]*/ |
2622 | 0 | { |
2623 | 0 | cwrobject *co; |
2624 | 0 | Py_ssize_t n; |
2625 | 0 | PyObject *pool = NULL; |
2626 | 0 | Py_ssize_t *indices = NULL; |
2627 | 0 | Py_ssize_t i; |
2628 | |
|
2629 | 0 | pool = PySequence_Tuple(iterable); |
2630 | 0 | if (pool == NULL) |
2631 | 0 | goto error; |
2632 | 0 | n = PyTuple_GET_SIZE(pool); |
2633 | |
|
2634 | 0 | indices = PyMem_New(Py_ssize_t, r); |
2635 | 0 | if (indices == NULL) { |
2636 | 0 | PyErr_NoMemory(); |
2637 | 0 | goto error; |
2638 | 0 | } |
2639 | | |
2640 | 0 | for (i=0 ; i<r ; i++) |
2641 | 0 | indices[i] = 0; |
2642 | | |
2643 | | /* create cwrobject structure */ |
2644 | 0 | co = (cwrobject *)type->tp_alloc(type, 0); |
2645 | 0 | if (co == NULL) |
2646 | 0 | goto error; |
2647 | | |
2648 | 0 | co->pool = pool; |
2649 | 0 | co->indices = indices; |
2650 | 0 | co->result = NULL; |
2651 | 0 | co->r = r; |
2652 | 0 | co->stopped = !n && r; |
2653 | |
|
2654 | 0 | return (PyObject *)co; |
2655 | | |
2656 | 0 | error: |
2657 | 0 | if (indices != NULL) |
2658 | 0 | PyMem_Free(indices); |
2659 | 0 | Py_XDECREF(pool); |
2660 | 0 | return NULL; |
2661 | 0 | } |
2662 | | |
2663 | | static void |
2664 | | cwr_dealloc(PyObject *op) |
2665 | 0 | { |
2666 | 0 | cwrobject *co = cwrobject_CAST(op); |
2667 | 0 | PyTypeObject *tp = Py_TYPE(co); |
2668 | 0 | PyObject_GC_UnTrack(co); |
2669 | 0 | Py_XDECREF(co->pool); |
2670 | 0 | Py_XDECREF(co->result); |
2671 | 0 | PyMem_Free(co->indices); |
2672 | 0 | tp->tp_free(co); |
2673 | 0 | Py_DECREF(tp); |
2674 | 0 | } |
2675 | | |
2676 | | static PyObject * |
2677 | | cwr_sizeof(PyObject *op, PyObject *Py_UNUSED(args)) |
2678 | 0 | { |
2679 | 0 | cwrobject *co = cwrobject_CAST(op); |
2680 | 0 | size_t res = _PyObject_SIZE(Py_TYPE(co)); |
2681 | 0 | res += (size_t)co->r * sizeof(Py_ssize_t); |
2682 | 0 | return PyLong_FromSize_t(res); |
2683 | 0 | } |
2684 | | |
2685 | | static int |
2686 | | cwr_traverse(PyObject *op, visitproc visit, void *arg) |
2687 | 0 | { |
2688 | 0 | cwrobject *co = cwrobject_CAST(op); |
2689 | 0 | Py_VISIT(Py_TYPE(co)); |
2690 | 0 | Py_VISIT(co->pool); |
2691 | 0 | Py_VISIT(co->result); |
2692 | 0 | return 0; |
2693 | 0 | } |
2694 | | |
2695 | | /*[clinic input] |
2696 | | @critical_section |
2697 | | itertools.combinations_with_replacement.__next__ as cwr_next |
2698 | | [clinic start generated code]*/ |
2699 | | |
2700 | | static PyObject * |
2701 | | cwr_next_impl(cwrobject *self) |
2702 | | /*[clinic end generated code: output=760ddcfbf905ee37 input=d9ed46a28a5bb29a]*/ |
2703 | 0 | { |
2704 | 0 | cwrobject *co = self; |
2705 | 0 | PyObject *elem; |
2706 | 0 | PyObject *oldelem; |
2707 | 0 | PyObject *pool = co->pool; |
2708 | 0 | Py_ssize_t *indices = co->indices; |
2709 | 0 | PyObject *result = co->result; |
2710 | 0 | Py_ssize_t n = PyTuple_GET_SIZE(pool); |
2711 | 0 | Py_ssize_t r = co->r; |
2712 | 0 | Py_ssize_t i, index; |
2713 | |
|
2714 | 0 | if (co->stopped) |
2715 | 0 | return NULL; |
2716 | | |
2717 | 0 | if (result == NULL) { |
2718 | | /* On the first pass, initialize result tuple with pool[0] */ |
2719 | 0 | result = PyTuple_New(r); |
2720 | 0 | if (result == NULL) |
2721 | 0 | goto empty; |
2722 | 0 | co->result = result; |
2723 | 0 | if (n > 0) { |
2724 | 0 | elem = PyTuple_GET_ITEM(pool, 0); |
2725 | 0 | for (i=0; i<r ; i++) { |
2726 | 0 | assert(indices[i] == 0); |
2727 | 0 | Py_INCREF(elem); |
2728 | 0 | PyTuple_SET_ITEM(result, i, elem); |
2729 | 0 | } |
2730 | 0 | } |
2731 | 0 | } else { |
2732 | | /* Copy the previous result tuple or re-use it if available */ |
2733 | 0 | if (!_PyObject_IsUniquelyReferenced(result)) { |
2734 | 0 | PyObject *old_result = result; |
2735 | 0 | result = PyTuple_FromArray(_PyTuple_ITEMS(old_result), r); |
2736 | 0 | if (result == NULL) |
2737 | 0 | goto empty; |
2738 | 0 | co->result = result; |
2739 | 0 | Py_DECREF(old_result); |
2740 | 0 | } |
2741 | | // bpo-42536: The GC may have untracked this result tuple. Since we're |
2742 | | // recycling it, make sure it's tracked again: |
2743 | 0 | else { |
2744 | 0 | _PyTuple_Recycle(result); |
2745 | 0 | } |
2746 | | /* Now, we've got the only copy so we can update it in-place CPython's |
2747 | | empty tuple is a singleton and cached in PyTuple's freelist. */ |
2748 | 0 | assert(r == 0 || Py_REFCNT(result) == 1); |
2749 | | |
2750 | | /* Scan indices right-to-left until finding one that is not |
2751 | | * at its maximum (n-1). */ |
2752 | 0 | for (i=r-1 ; i >= 0 && indices[i] == n-1; i--) |
2753 | 0 | ; |
2754 | | |
2755 | | /* If i is negative, then the indices are all at |
2756 | | their maximum value and we're done. */ |
2757 | 0 | if (i < 0) |
2758 | 0 | goto empty; |
2759 | | |
2760 | | /* Increment the current index which we know is not at its |
2761 | | maximum. Then set all to the right to the same value. */ |
2762 | 0 | index = indices[i] + 1; |
2763 | 0 | assert(index < n); |
2764 | 0 | elem = PyTuple_GET_ITEM(pool, index); |
2765 | 0 | for ( ; i<r ; i++) { |
2766 | 0 | indices[i] = index; |
2767 | 0 | Py_INCREF(elem); |
2768 | 0 | oldelem = PyTuple_GET_ITEM(result, i); |
2769 | 0 | PyTuple_SET_ITEM(result, i, elem); |
2770 | 0 | Py_DECREF(oldelem); |
2771 | 0 | } |
2772 | 0 | } |
2773 | | |
2774 | 0 | return Py_NewRef(result); |
2775 | | |
2776 | 0 | empty: |
2777 | 0 | co->stopped = 1; |
2778 | 0 | return NULL; |
2779 | 0 | } |
2780 | | |
2781 | | static PyMethodDef cwr_methods[] = { |
2782 | | {"__sizeof__", cwr_sizeof, METH_NOARGS, sizeof_doc}, |
2783 | | {NULL, NULL} /* sentinel */ |
2784 | | }; |
2785 | | |
2786 | | static PyType_Slot cwr_slots[] = { |
2787 | | {Py_tp_dealloc, cwr_dealloc}, |
2788 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
2789 | | {Py_tp_doc, (void *)itertools_combinations_with_replacement__doc__}, |
2790 | | {Py_tp_traverse, cwr_traverse}, |
2791 | | {Py_tp_iter, PyObject_SelfIter}, |
2792 | | {Py_tp_iternext, cwr_next}, |
2793 | | {Py_tp_methods, cwr_methods}, |
2794 | | {Py_tp_new, itertools_combinations_with_replacement}, |
2795 | | {Py_tp_vectorcall, itertools_combinations_with_replacement_vectorcall}, |
2796 | | {Py_tp_free, PyObject_GC_Del}, |
2797 | | {0, NULL}, |
2798 | | }; |
2799 | | |
2800 | | static PyType_Spec cwr_spec = { |
2801 | | .name = "itertools.combinations_with_replacement", |
2802 | | .basicsize = sizeof(cwrobject), |
2803 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
2804 | | Py_TPFLAGS_IMMUTABLETYPE), |
2805 | | .slots = cwr_slots, |
2806 | | }; |
2807 | | |
2808 | | |
2809 | | /* permutations object ******************************************************** |
2810 | | |
2811 | | def permutations(iterable, r=None): |
2812 | | # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC |
2813 | | # permutations(range(3)) --> 012 021 102 120 201 210 |
2814 | | pool = tuple(iterable) |
2815 | | n = len(pool) |
2816 | | r = n if r is None else r |
2817 | | if r > n: |
2818 | | return |
2819 | | indices = list(range(n)) |
2820 | | cycles = list(range(n, n-r, -1)) |
2821 | | yield tuple(pool[i] for i in indices[:r]) |
2822 | | while n: |
2823 | | for i in reversed(range(r)): |
2824 | | cycles[i] -= 1 |
2825 | | if cycles[i] == 0: |
2826 | | indices[i:] = indices[i+1:] + indices[i:i+1] |
2827 | | cycles[i] = n - i |
2828 | | else: |
2829 | | j = cycles[i] |
2830 | | indices[i], indices[-j] = indices[-j], indices[i] |
2831 | | yield tuple(pool[i] for i in indices[:r]) |
2832 | | break |
2833 | | else: |
2834 | | return |
2835 | | */ |
2836 | | |
2837 | | struct permutationsobject { |
2838 | | PyObject_HEAD |
2839 | | PyObject *pool; /* input converted to a tuple */ |
2840 | | Py_ssize_t *indices; /* one index per element in the pool */ |
2841 | | Py_ssize_t *cycles; /* one rollover counter per element in the result */ |
2842 | | PyObject *result; /* most recently returned result tuple */ |
2843 | | Py_ssize_t r; /* size of result tuple */ |
2844 | | int stopped; /* set to 1 when the iterator is exhausted */ |
2845 | | }; |
2846 | | |
2847 | 144 | #define permutationsobject_CAST(op) ((permutationsobject *)(op)) |
2848 | | |
2849 | | /*[clinic input] |
2850 | | @vectorcall |
2851 | | @classmethod |
2852 | | itertools.permutations.__new__ |
2853 | | iterable: object |
2854 | | r as robj: object = None |
2855 | | Return successive r-length permutations of elements in the iterable. |
2856 | | |
2857 | | permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1) |
2858 | | [clinic start generated code]*/ |
2859 | | |
2860 | | static PyObject * |
2861 | | itertools_permutations_impl(PyTypeObject *type, PyObject *iterable, |
2862 | | PyObject *robj) |
2863 | | /*[clinic end generated code: output=296a72fa76d620ea input=3679c9c6e757d17d]*/ |
2864 | 144 | { |
2865 | 144 | permutationsobject *po; |
2866 | 144 | Py_ssize_t n; |
2867 | 144 | Py_ssize_t r; |
2868 | 144 | PyObject *pool = NULL; |
2869 | 144 | Py_ssize_t *indices = NULL; |
2870 | 144 | Py_ssize_t *cycles = NULL; |
2871 | 144 | Py_ssize_t i; |
2872 | | |
2873 | 144 | pool = PySequence_Tuple(iterable); |
2874 | 144 | if (pool == NULL) |
2875 | 0 | goto error; |
2876 | 144 | n = PyTuple_GET_SIZE(pool); |
2877 | | |
2878 | 144 | r = n; |
2879 | 144 | if (robj != Py_None) { |
2880 | 0 | if (!PyLong_Check(robj)) { |
2881 | 0 | PyErr_SetString(PyExc_TypeError, "Expected int as r"); |
2882 | 0 | goto error; |
2883 | 0 | } |
2884 | 0 | r = PyLong_AsSsize_t(robj); |
2885 | 0 | if (r == -1 && PyErr_Occurred()) |
2886 | 0 | goto error; |
2887 | 0 | } |
2888 | 144 | if (r < 0) { |
2889 | 0 | PyErr_SetString(PyExc_ValueError, "r must be non-negative"); |
2890 | 0 | goto error; |
2891 | 0 | } |
2892 | | |
2893 | 144 | indices = PyMem_New(Py_ssize_t, n); |
2894 | 144 | cycles = PyMem_New(Py_ssize_t, r); |
2895 | 144 | if (indices == NULL || cycles == NULL) { |
2896 | 0 | PyErr_NoMemory(); |
2897 | 0 | goto error; |
2898 | 0 | } |
2899 | | |
2900 | 342 | for (i=0 ; i<n ; i++) |
2901 | 198 | indices[i] = i; |
2902 | 342 | for (i=0 ; i<r ; i++) |
2903 | 198 | cycles[i] = n - i; |
2904 | | |
2905 | | /* create permutationsobject structure */ |
2906 | 144 | po = (permutationsobject *)type->tp_alloc(type, 0); |
2907 | 144 | if (po == NULL) |
2908 | 0 | goto error; |
2909 | | |
2910 | 144 | po->pool = pool; |
2911 | 144 | po->indices = indices; |
2912 | 144 | po->cycles = cycles; |
2913 | 144 | po->result = NULL; |
2914 | 144 | po->r = r; |
2915 | 144 | po->stopped = r > n ? 1 : 0; |
2916 | | |
2917 | 144 | return (PyObject *)po; |
2918 | | |
2919 | 0 | error: |
2920 | 0 | if (indices != NULL) |
2921 | 0 | PyMem_Free(indices); |
2922 | 0 | if (cycles != NULL) |
2923 | 0 | PyMem_Free(cycles); |
2924 | 0 | Py_XDECREF(pool); |
2925 | 0 | return NULL; |
2926 | 144 | } |
2927 | | |
2928 | | static void |
2929 | | permutations_dealloc(PyObject *op) |
2930 | 144 | { |
2931 | 144 | permutationsobject *po = permutationsobject_CAST(op); |
2932 | 144 | PyTypeObject *tp = Py_TYPE(po); |
2933 | 144 | PyObject_GC_UnTrack(po); |
2934 | 144 | Py_XDECREF(po->pool); |
2935 | 144 | Py_XDECREF(po->result); |
2936 | 144 | PyMem_Free(po->indices); |
2937 | 144 | PyMem_Free(po->cycles); |
2938 | 144 | tp->tp_free(po); |
2939 | 144 | Py_DECREF(tp); |
2940 | 144 | } |
2941 | | |
2942 | | static PyObject * |
2943 | | permutations_sizeof(PyObject *op, PyObject *Py_UNUSED(args)) |
2944 | 0 | { |
2945 | 0 | permutationsobject *po = permutationsobject_CAST(op); |
2946 | 0 | size_t res = _PyObject_SIZE(Py_TYPE(po)); |
2947 | 0 | res += (size_t)PyTuple_GET_SIZE(po->pool) * sizeof(Py_ssize_t); |
2948 | 0 | res += (size_t)po->r * sizeof(Py_ssize_t); |
2949 | 0 | return PyLong_FromSize_t(res); |
2950 | 0 | } |
2951 | | |
2952 | | static int |
2953 | | permutations_traverse(PyObject *op, visitproc visit, void *arg) |
2954 | 0 | { |
2955 | 0 | permutationsobject *po = permutationsobject_CAST(op); |
2956 | 0 | Py_VISIT(Py_TYPE(po)); |
2957 | 0 | Py_VISIT(po->pool); |
2958 | 0 | Py_VISIT(po->result); |
2959 | 0 | return 0; |
2960 | 0 | } |
2961 | | |
2962 | | /*[clinic input] |
2963 | | @critical_section |
2964 | | itertools.permutations.__next__ as permutations_next |
2965 | | [clinic start generated code]*/ |
2966 | | |
2967 | | static PyObject * |
2968 | | permutations_next_impl(permutationsobject *self) |
2969 | | /*[clinic end generated code: output=17d8db4465bd3ee5 input=d4c1a1eed76a35e0]*/ |
2970 | 342 | { |
2971 | 342 | permutationsobject *po = self; |
2972 | 342 | PyObject *elem; |
2973 | 342 | PyObject *oldelem; |
2974 | 342 | PyObject *pool = po->pool; |
2975 | 342 | Py_ssize_t *indices = po->indices; |
2976 | 342 | Py_ssize_t *cycles = po->cycles; |
2977 | 342 | PyObject *result = po->result; |
2978 | 342 | Py_ssize_t n = PyTuple_GET_SIZE(pool); |
2979 | 342 | Py_ssize_t r = po->r; |
2980 | 342 | Py_ssize_t i, j, k, index; |
2981 | | |
2982 | 342 | if (po->stopped) |
2983 | 0 | return NULL; |
2984 | | |
2985 | 342 | if (result == NULL) { |
2986 | | /* On the first pass, initialize result tuple using the indices */ |
2987 | 144 | result = PyTuple_New(r); |
2988 | 144 | if (result == NULL) |
2989 | 0 | goto empty; |
2990 | 144 | po->result = result; |
2991 | 342 | for (i=0; i<r ; i++) { |
2992 | 198 | index = indices[i]; |
2993 | 198 | elem = PyTuple_GET_ITEM(pool, index); |
2994 | 198 | Py_INCREF(elem); |
2995 | 198 | PyTuple_SET_ITEM(result, i, elem); |
2996 | 198 | } |
2997 | 198 | } else { |
2998 | 198 | if (n == 0) |
2999 | 0 | goto empty; |
3000 | | |
3001 | | /* Copy the previous result tuple or re-use it if available */ |
3002 | 198 | if (!_PyObject_IsUniquelyReferenced(result)) { |
3003 | 198 | PyObject *old_result = result; |
3004 | 198 | result = PyTuple_FromArray(_PyTuple_ITEMS(old_result), r); |
3005 | 198 | if (result == NULL) |
3006 | 0 | goto empty; |
3007 | 198 | po->result = result; |
3008 | 198 | Py_DECREF(old_result); |
3009 | 198 | } |
3010 | | // bpo-42536: The GC may have untracked this result tuple. Since we're |
3011 | | // recycling it, make sure it's tracked again: |
3012 | 0 | else { |
3013 | 0 | _PyTuple_Recycle(result); |
3014 | 0 | } |
3015 | | /* Now, we've got the only copy so we can update it in-place */ |
3016 | 198 | assert(r == 0 || Py_REFCNT(result) == 1); |
3017 | | |
3018 | | /* Decrement rightmost cycle, moving leftward upon zero rollover */ |
3019 | 450 | for (i=r-1 ; i>=0 ; i--) { |
3020 | 306 | cycles[i] -= 1; |
3021 | 306 | if (cycles[i] == 0) { |
3022 | | /* rotatation: indices[i:] = indices[i+1:] + indices[i:i+1] */ |
3023 | 252 | index = indices[i]; |
3024 | 306 | for (j=i ; j<n-1 ; j++) |
3025 | 54 | indices[j] = indices[j+1]; |
3026 | 252 | indices[n-1] = index; |
3027 | 252 | cycles[i] = n - i; |
3028 | 252 | } else { |
3029 | 54 | j = cycles[i]; |
3030 | 54 | index = indices[i]; |
3031 | 54 | indices[i] = indices[n-j]; |
3032 | 54 | indices[n-j] = index; |
3033 | | |
3034 | 162 | for (k=i; k<r ; k++) { |
3035 | | /* start with i, the leftmost element that changed */ |
3036 | | /* yield tuple(pool[k] for k in indices[:r]) */ |
3037 | 108 | index = indices[k]; |
3038 | 108 | elem = PyTuple_GET_ITEM(pool, index); |
3039 | 108 | Py_INCREF(elem); |
3040 | 108 | oldelem = PyTuple_GET_ITEM(result, k); |
3041 | 108 | PyTuple_SET_ITEM(result, k, elem); |
3042 | 108 | Py_DECREF(oldelem); |
3043 | 108 | } |
3044 | 54 | break; |
3045 | 54 | } |
3046 | 306 | } |
3047 | | /* If i is negative, then the cycles have all |
3048 | | rolled-over and we're done. */ |
3049 | 198 | if (i < 0) |
3050 | 144 | goto empty; |
3051 | 198 | } |
3052 | 198 | return Py_NewRef(result); |
3053 | | |
3054 | 144 | empty: |
3055 | 144 | po->stopped = 1; |
3056 | 144 | return NULL; |
3057 | 342 | } |
3058 | | |
3059 | | static PyMethodDef permuations_methods[] = { |
3060 | | {"__sizeof__", permutations_sizeof, METH_NOARGS, sizeof_doc}, |
3061 | | {NULL, NULL} /* sentinel */ |
3062 | | }; |
3063 | | |
3064 | | static PyType_Slot permutations_slots[] = { |
3065 | | {Py_tp_dealloc, permutations_dealloc}, |
3066 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
3067 | | {Py_tp_doc, (void *)itertools_permutations__doc__}, |
3068 | | {Py_tp_traverse, permutations_traverse}, |
3069 | | {Py_tp_iter, PyObject_SelfIter}, |
3070 | | {Py_tp_iternext, permutations_next}, |
3071 | | {Py_tp_methods, permuations_methods}, |
3072 | | {Py_tp_new, itertools_permutations}, |
3073 | | {Py_tp_vectorcall, itertools_permutations_vectorcall}, |
3074 | | {Py_tp_free, PyObject_GC_Del}, |
3075 | | {0, NULL}, |
3076 | | }; |
3077 | | |
3078 | | static PyType_Spec permutations_spec = { |
3079 | | .name = "itertools.permutations", |
3080 | | .basicsize = sizeof(permutationsobject), |
3081 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
3082 | | Py_TPFLAGS_IMMUTABLETYPE), |
3083 | | .slots = permutations_slots, |
3084 | | }; |
3085 | | |
3086 | | |
3087 | | /* accumulate object ********************************************************/ |
3088 | | |
3089 | | struct accumulateobject { |
3090 | | PyObject_HEAD |
3091 | | PyObject *total; |
3092 | | PyObject *it; |
3093 | | PyObject *binop; |
3094 | | PyObject *initial; |
3095 | | itertools_state *state; |
3096 | | }; |
3097 | | |
3098 | 0 | #define accumulateobject_CAST(op) ((accumulateobject *)(op)) |
3099 | | |
3100 | | /*[clinic input] |
3101 | | @vectorcall |
3102 | | @classmethod |
3103 | | itertools.accumulate.__new__ |
3104 | | iterable: object |
3105 | | func as binop: object = None |
3106 | | * |
3107 | | initial: object = None |
3108 | | Return series of accumulated sums (or other binary function results). |
3109 | | [clinic start generated code]*/ |
3110 | | |
3111 | | static PyObject * |
3112 | | itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable, |
3113 | | PyObject *binop, PyObject *initial) |
3114 | | /*[clinic end generated code: output=66da2650627128f8 input=9c7edffb7110edc9]*/ |
3115 | 0 | { |
3116 | 0 | PyObject *it; |
3117 | 0 | accumulateobject *lz; |
3118 | | |
3119 | | /* Get iterator. */ |
3120 | 0 | it = PyObject_GetIter(iterable); |
3121 | 0 | if (it == NULL) |
3122 | 0 | return NULL; |
3123 | | |
3124 | | /* create accumulateobject structure */ |
3125 | 0 | lz = (accumulateobject *)type->tp_alloc(type, 0); |
3126 | 0 | if (lz == NULL) { |
3127 | 0 | Py_DECREF(it); |
3128 | 0 | return NULL; |
3129 | 0 | } |
3130 | | |
3131 | 0 | if (binop != Py_None) { |
3132 | 0 | lz->binop = Py_XNewRef(binop); |
3133 | 0 | } |
3134 | 0 | lz->total = NULL; |
3135 | 0 | lz->it = it; |
3136 | 0 | lz->initial = Py_XNewRef(initial); |
3137 | 0 | lz->state = find_state_by_type(type); |
3138 | 0 | return (PyObject *)lz; |
3139 | 0 | } |
3140 | | |
3141 | | static void |
3142 | | accumulate_dealloc(PyObject *op) |
3143 | 0 | { |
3144 | 0 | accumulateobject *lz = accumulateobject_CAST(op); |
3145 | 0 | PyTypeObject *tp = Py_TYPE(lz); |
3146 | 0 | PyObject_GC_UnTrack(lz); |
3147 | 0 | Py_XDECREF(lz->binop); |
3148 | 0 | Py_XDECREF(lz->total); |
3149 | 0 | Py_XDECREF(lz->it); |
3150 | 0 | Py_XDECREF(lz->initial); |
3151 | 0 | tp->tp_free(lz); |
3152 | 0 | Py_DECREF(tp); |
3153 | 0 | } |
3154 | | |
3155 | | static int |
3156 | | accumulate_traverse(PyObject *op, visitproc visit, void *arg) |
3157 | 0 | { |
3158 | 0 | accumulateobject *lz = accumulateobject_CAST(op); |
3159 | 0 | Py_VISIT(Py_TYPE(lz)); |
3160 | 0 | Py_VISIT(lz->binop); |
3161 | 0 | Py_VISIT(lz->it); |
3162 | 0 | Py_VISIT(lz->total); |
3163 | 0 | Py_VISIT(lz->initial); |
3164 | 0 | return 0; |
3165 | 0 | } |
3166 | | |
3167 | | /*[clinic input] |
3168 | | @critical_section |
3169 | | itertools.accumulate.__next__ as accumulate_next |
3170 | | [clinic start generated code]*/ |
3171 | | |
3172 | | static PyObject * |
3173 | | accumulate_next_impl(accumulateobject *self) |
3174 | | /*[clinic end generated code: output=30d3f9b40248d8a6 input=758a144b7cfb1ef4]*/ |
3175 | 0 | { |
3176 | 0 | accumulateobject *lz = self; |
3177 | 0 | PyObject *val, *newtotal; |
3178 | |
|
3179 | 0 | if (lz->initial != Py_None) { |
3180 | 0 | lz->total = lz->initial; |
3181 | 0 | lz->initial = Py_NewRef(Py_None); |
3182 | 0 | return Py_NewRef(lz->total); |
3183 | 0 | } |
3184 | 0 | val = (*Py_TYPE(lz->it)->tp_iternext)(lz->it); |
3185 | 0 | if (val == NULL) |
3186 | 0 | return NULL; |
3187 | | |
3188 | 0 | if (lz->total == NULL) { |
3189 | 0 | lz->total = Py_NewRef(val); |
3190 | 0 | return lz->total; |
3191 | 0 | } |
3192 | | |
3193 | 0 | if (lz->binop == NULL) |
3194 | 0 | newtotal = PyNumber_Add(lz->total, val); |
3195 | 0 | else |
3196 | 0 | newtotal = PyObject_CallFunctionObjArgs(lz->binop, lz->total, val, NULL); |
3197 | 0 | Py_DECREF(val); |
3198 | 0 | if (newtotal == NULL) |
3199 | 0 | return NULL; |
3200 | | |
3201 | 0 | Py_INCREF(newtotal); |
3202 | 0 | Py_SETREF(lz->total, newtotal); |
3203 | 0 | return newtotal; |
3204 | 0 | } |
3205 | | |
3206 | | static PyType_Slot accumulate_slots[] = { |
3207 | | {Py_tp_dealloc, accumulate_dealloc}, |
3208 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
3209 | | {Py_tp_doc, (void *)itertools_accumulate__doc__}, |
3210 | | {Py_tp_traverse, accumulate_traverse}, |
3211 | | {Py_tp_iter, PyObject_SelfIter}, |
3212 | | {Py_tp_iternext, accumulate_next}, |
3213 | | {Py_tp_new, itertools_accumulate}, |
3214 | | {Py_tp_vectorcall, itertools_accumulate_vectorcall}, |
3215 | | {Py_tp_free, PyObject_GC_Del}, |
3216 | | {0, NULL}, |
3217 | | }; |
3218 | | |
3219 | | static PyType_Spec accumulate_spec = { |
3220 | | .name = "itertools.accumulate", |
3221 | | .basicsize = sizeof(accumulateobject), |
3222 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
3223 | | Py_TPFLAGS_IMMUTABLETYPE), |
3224 | | .slots = accumulate_slots, |
3225 | | }; |
3226 | | |
3227 | | |
3228 | | /* compress object ************************************************************/ |
3229 | | |
3230 | | /* Equivalent to: |
3231 | | |
3232 | | def compress(data, selectors): |
3233 | | "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F" |
3234 | | return (d for d, s in zip(data, selectors) if s) |
3235 | | */ |
3236 | | |
3237 | | typedef struct { |
3238 | | PyObject_HEAD |
3239 | | PyObject *data; |
3240 | | PyObject *selectors; |
3241 | | } compressobject; |
3242 | | |
3243 | 0 | #define compressobject_CAST(op) ((compressobject *)(op)) |
3244 | | |
3245 | | /*[clinic input] |
3246 | | @vectorcall |
3247 | | @classmethod |
3248 | | itertools.compress.__new__ |
3249 | | data as seq1: object |
3250 | | selectors as seq2: object |
3251 | | Return data elements corresponding to true selector elements. |
3252 | | |
3253 | | Forms a shorter iterator from selected data elements using the selectors |
3254 | | to choose the data elements. |
3255 | | [clinic start generated code]*/ |
3256 | | |
3257 | | static PyObject * |
3258 | | itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2) |
3259 | | /*[clinic end generated code: output=7e67157212ed09e0 input=a886cf13635c9780]*/ |
3260 | 0 | { |
3261 | 0 | PyObject *data=NULL, *selectors=NULL; |
3262 | 0 | compressobject *lz; |
3263 | |
|
3264 | 0 | data = PyObject_GetIter(seq1); |
3265 | 0 | if (data == NULL) |
3266 | 0 | goto fail; |
3267 | 0 | selectors = PyObject_GetIter(seq2); |
3268 | 0 | if (selectors == NULL) |
3269 | 0 | goto fail; |
3270 | | |
3271 | | /* create compressobject structure */ |
3272 | 0 | lz = (compressobject *)type->tp_alloc(type, 0); |
3273 | 0 | if (lz == NULL) |
3274 | 0 | goto fail; |
3275 | 0 | lz->data = data; |
3276 | 0 | lz->selectors = selectors; |
3277 | 0 | return (PyObject *)lz; |
3278 | | |
3279 | 0 | fail: |
3280 | 0 | Py_XDECREF(data); |
3281 | 0 | Py_XDECREF(selectors); |
3282 | 0 | return NULL; |
3283 | 0 | } |
3284 | | |
3285 | | static void |
3286 | | compress_dealloc(PyObject *op) |
3287 | 0 | { |
3288 | 0 | compressobject *lz = compressobject_CAST(op); |
3289 | 0 | PyTypeObject *tp = Py_TYPE(lz); |
3290 | 0 | PyObject_GC_UnTrack(lz); |
3291 | 0 | Py_XDECREF(lz->data); |
3292 | 0 | Py_XDECREF(lz->selectors); |
3293 | 0 | tp->tp_free(lz); |
3294 | 0 | Py_DECREF(tp); |
3295 | 0 | } |
3296 | | |
3297 | | static int |
3298 | | compress_traverse(PyObject *op, visitproc visit, void *arg) |
3299 | 0 | { |
3300 | 0 | compressobject *lz = compressobject_CAST(op); |
3301 | 0 | Py_VISIT(Py_TYPE(lz)); |
3302 | 0 | Py_VISIT(lz->data); |
3303 | 0 | Py_VISIT(lz->selectors); |
3304 | 0 | return 0; |
3305 | 0 | } |
3306 | | |
3307 | | static PyObject * |
3308 | | compress_next(PyObject *op) |
3309 | 0 | { |
3310 | 0 | compressobject *lz = compressobject_CAST(op); |
3311 | 0 | PyObject *data = lz->data, *selectors = lz->selectors; |
3312 | 0 | PyObject *datum, *selector; |
3313 | 0 | PyObject *(*datanext)(PyObject *) = *Py_TYPE(data)->tp_iternext; |
3314 | 0 | PyObject *(*selectornext)(PyObject *) = *Py_TYPE(selectors)->tp_iternext; |
3315 | 0 | int ok; |
3316 | |
|
3317 | 0 | while (1) { |
3318 | | /* Steps: get datum, get selector, evaluate selector. |
3319 | | Order is important (to match the pure python version |
3320 | | in terms of which input gets a chance to raise an |
3321 | | exception first). |
3322 | | */ |
3323 | |
|
3324 | 0 | datum = datanext(data); |
3325 | 0 | if (datum == NULL) |
3326 | 0 | return NULL; |
3327 | | |
3328 | 0 | selector = selectornext(selectors); |
3329 | 0 | if (selector == NULL) { |
3330 | 0 | Py_DECREF(datum); |
3331 | 0 | return NULL; |
3332 | 0 | } |
3333 | | |
3334 | 0 | ok = PyObject_IsTrue(selector); |
3335 | 0 | Py_DECREF(selector); |
3336 | 0 | if (ok > 0) |
3337 | 0 | return datum; |
3338 | 0 | Py_DECREF(datum); |
3339 | 0 | if (ok < 0) |
3340 | 0 | return NULL; |
3341 | 0 | } |
3342 | 0 | } |
3343 | | |
3344 | | static PyType_Slot compress_slots[] = { |
3345 | | {Py_tp_dealloc, compress_dealloc}, |
3346 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
3347 | | {Py_tp_doc, (void *)itertools_compress__doc__}, |
3348 | | {Py_tp_traverse, compress_traverse}, |
3349 | | {Py_tp_iter, PyObject_SelfIter}, |
3350 | | {Py_tp_iternext, compress_next}, |
3351 | | {Py_tp_new, itertools_compress}, |
3352 | | {Py_tp_vectorcall, itertools_compress_vectorcall}, |
3353 | | {Py_tp_free, PyObject_GC_Del}, |
3354 | | {0, NULL}, |
3355 | | }; |
3356 | | |
3357 | | static PyType_Spec compress_spec = { |
3358 | | .name = "itertools.compress", |
3359 | | .basicsize = sizeof(compressobject), |
3360 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
3361 | | Py_TPFLAGS_IMMUTABLETYPE), |
3362 | | .slots = compress_slots, |
3363 | | }; |
3364 | | |
3365 | | |
3366 | | /* filterfalse object ************************************************************/ |
3367 | | |
3368 | | typedef struct { |
3369 | | PyObject_HEAD |
3370 | | PyObject *func; |
3371 | | PyObject *it; |
3372 | | } filterfalseobject; |
3373 | | |
3374 | 552 | #define filterfalseobject_CAST(op) ((filterfalseobject *)(op)) |
3375 | | |
3376 | | /*[clinic input] |
3377 | | @vectorcall |
3378 | | @classmethod |
3379 | | itertools.filterfalse.__new__ |
3380 | | function as func: object |
3381 | | iterable as seq: object |
3382 | | / |
3383 | | Return those items of iterable for which function(item) is false. |
3384 | | |
3385 | | If function is None, return the items that are false. |
3386 | | [clinic start generated code]*/ |
3387 | | |
3388 | | static PyObject * |
3389 | | itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq) |
3390 | | /*[clinic end generated code: output=55f87eab9fc0484e input=a80f7d973f6efc81]*/ |
3391 | 84 | { |
3392 | 84 | PyObject *it; |
3393 | 84 | filterfalseobject *lz; |
3394 | | |
3395 | | /* Get iterator. */ |
3396 | 84 | it = PyObject_GetIter(seq); |
3397 | 84 | if (it == NULL) |
3398 | 0 | return NULL; |
3399 | | |
3400 | | /* create filterfalseobject structure */ |
3401 | 84 | lz = (filterfalseobject *)type->tp_alloc(type, 0); |
3402 | 84 | if (lz == NULL) { |
3403 | 0 | Py_DECREF(it); |
3404 | 0 | return NULL; |
3405 | 0 | } |
3406 | 84 | lz->func = Py_NewRef(func); |
3407 | 84 | lz->it = it; |
3408 | | |
3409 | 84 | return (PyObject *)lz; |
3410 | 84 | } |
3411 | | |
3412 | | static void |
3413 | | filterfalse_dealloc(PyObject *op) |
3414 | 84 | { |
3415 | 84 | filterfalseobject *lz = filterfalseobject_CAST(op); |
3416 | 84 | PyTypeObject *tp = Py_TYPE(lz); |
3417 | 84 | PyObject_GC_UnTrack(lz); |
3418 | 84 | Py_XDECREF(lz->func); |
3419 | 84 | Py_XDECREF(lz->it); |
3420 | 84 | tp->tp_free(lz); |
3421 | 84 | Py_DECREF(tp); |
3422 | 84 | } |
3423 | | |
3424 | | static int |
3425 | | filterfalse_traverse(PyObject *op, visitproc visit, void *arg) |
3426 | 0 | { |
3427 | 0 | filterfalseobject *lz = filterfalseobject_CAST(op); |
3428 | 0 | Py_VISIT(Py_TYPE(lz)); |
3429 | 0 | Py_VISIT(lz->it); |
3430 | 0 | Py_VISIT(lz->func); |
3431 | 0 | return 0; |
3432 | 0 | } |
3433 | | |
3434 | | static PyObject * |
3435 | | filterfalse_next(PyObject *op) |
3436 | 468 | { |
3437 | 468 | filterfalseobject *lz = filterfalseobject_CAST(op); |
3438 | 468 | PyObject *item; |
3439 | 468 | PyObject *it = lz->it; |
3440 | 468 | long ok; |
3441 | 468 | PyObject *(*iternext)(PyObject *); |
3442 | | |
3443 | 468 | iternext = *Py_TYPE(it)->tp_iternext; |
3444 | 492 | for (;;) { |
3445 | 492 | item = iternext(it); |
3446 | 492 | if (item == NULL) |
3447 | 84 | return NULL; |
3448 | | |
3449 | 408 | if (lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type) { |
3450 | 0 | ok = PyObject_IsTrue(item); |
3451 | 408 | } else { |
3452 | 408 | PyObject *good; |
3453 | 408 | good = PyObject_CallOneArg(lz->func, item); |
3454 | 408 | if (good == NULL) { |
3455 | 0 | Py_DECREF(item); |
3456 | 0 | return NULL; |
3457 | 0 | } |
3458 | 408 | ok = PyObject_IsTrue(good); |
3459 | 408 | Py_DECREF(good); |
3460 | 408 | } |
3461 | 408 | if (ok == 0) |
3462 | 384 | return item; |
3463 | 24 | Py_DECREF(item); |
3464 | 24 | if (ok < 0) |
3465 | 0 | return NULL; |
3466 | 24 | } |
3467 | 468 | } |
3468 | | |
3469 | | static PyType_Slot filterfalse_slots[] = { |
3470 | | {Py_tp_dealloc, filterfalse_dealloc}, |
3471 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
3472 | | {Py_tp_doc, (void *)itertools_filterfalse__doc__}, |
3473 | | {Py_tp_traverse, filterfalse_traverse}, |
3474 | | {Py_tp_iter, PyObject_SelfIter}, |
3475 | | {Py_tp_iternext, filterfalse_next}, |
3476 | | {Py_tp_new, itertools_filterfalse}, |
3477 | | {Py_tp_vectorcall, itertools_filterfalse_vectorcall}, |
3478 | | {Py_tp_free, PyObject_GC_Del}, |
3479 | | {0, NULL}, |
3480 | | }; |
3481 | | |
3482 | | static PyType_Spec filterfalse_spec = { |
3483 | | .name = "itertools.filterfalse", |
3484 | | .basicsize = sizeof(filterfalseobject), |
3485 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
3486 | | Py_TPFLAGS_IMMUTABLETYPE), |
3487 | | .slots = filterfalse_slots, |
3488 | | }; |
3489 | | |
3490 | | |
3491 | | /* count object ************************************************************/ |
3492 | | |
3493 | | typedef struct { |
3494 | | PyObject_HEAD |
3495 | | Py_ssize_t cnt; |
3496 | | PyObject *long_cnt; |
3497 | | PyObject *long_step; |
3498 | | } countobject; |
3499 | | |
3500 | 4.90k | #define countobject_CAST(op) ((countobject *)(op)) |
3501 | | |
3502 | | /* Counting logic and invariants: |
3503 | | |
3504 | | fast_mode: when cnt an integer < PY_SSIZE_T_MAX and no step is specified. |
3505 | | |
3506 | | assert(long_cnt == NULL && long_step==PyLong(1)); |
3507 | | Advances with: cnt += 1 |
3508 | | When count hits PY_SSIZE_T_MAX, switch to slow_mode. |
3509 | | |
3510 | | slow_mode: when cnt == PY_SSIZE_T_MAX, step is not int(1), or cnt is a float. |
3511 | | |
3512 | | assert(cnt == PY_SSIZE_T_MAX && long_cnt != NULL && long_step != NULL); |
3513 | | All counting is done with python objects (no overflows or underflows). |
3514 | | Advances with: long_cnt += long_step |
3515 | | Step may be zero -- effectively a slow version of repeat(cnt). |
3516 | | Either long_cnt or long_step may be a float, Fraction, or Decimal. |
3517 | | */ |
3518 | | |
3519 | | /*[clinic input] |
3520 | | @permit_long_summary |
3521 | | @vectorcall |
3522 | | @classmethod |
3523 | | itertools.count.__new__ |
3524 | | start as long_cnt: object(c_default="NULL") = 0 |
3525 | | step as long_step: object(c_default="NULL") = 1 |
3526 | | Return a count object whose .__next__() method returns consecutive values. |
3527 | | |
3528 | | Equivalent to: |
3529 | | def count(firstval=0, step=1): |
3530 | | x = firstval |
3531 | | while 1: |
3532 | | yield x |
3533 | | x += step |
3534 | | [clinic start generated code]*/ |
3535 | | |
3536 | | static PyObject * |
3537 | | itertools_count_impl(PyTypeObject *type, PyObject *long_cnt, |
3538 | | PyObject *long_step) |
3539 | | /*[clinic end generated code: output=09a9250aebd00b1c input=b8a57119ba1cdd54]*/ |
3540 | 26 | { |
3541 | 26 | countobject *lz; |
3542 | 26 | int fast_mode; |
3543 | 26 | Py_ssize_t cnt = 0; |
3544 | 26 | long step; |
3545 | | |
3546 | 26 | if ((long_cnt != NULL && !PyNumber_Check(long_cnt)) || |
3547 | 26 | (long_step != NULL && !PyNumber_Check(long_step))) { |
3548 | 0 | PyErr_SetString(PyExc_TypeError, "a number is required"); |
3549 | 0 | return NULL; |
3550 | 0 | } |
3551 | | |
3552 | 26 | fast_mode = (long_cnt == NULL || PyLong_Check(long_cnt)) && |
3553 | 26 | (long_step == NULL || PyLong_Check(long_step)); |
3554 | | |
3555 | | /* If not specified, start defaults to 0 */ |
3556 | 26 | if (long_cnt != NULL) { |
3557 | 8 | if (fast_mode) { |
3558 | 8 | assert(PyLong_Check(long_cnt)); |
3559 | 8 | cnt = PyLong_AsSsize_t(long_cnt); |
3560 | 8 | if (cnt == -1 && PyErr_Occurred()) { |
3561 | 0 | PyErr_Clear(); |
3562 | 0 | fast_mode = 0; |
3563 | 0 | } |
3564 | 8 | } |
3565 | 18 | } else { |
3566 | 18 | cnt = 0; |
3567 | 18 | long_cnt = _PyLong_GetZero(); |
3568 | 18 | } |
3569 | 26 | Py_INCREF(long_cnt); |
3570 | | |
3571 | | /* If not specified, step defaults to 1 */ |
3572 | 26 | if (long_step == NULL) { |
3573 | 26 | long_step = _PyLong_GetOne(); |
3574 | 26 | } |
3575 | 26 | Py_INCREF(long_step); |
3576 | | |
3577 | 26 | assert(long_cnt != NULL && long_step != NULL); |
3578 | | |
3579 | | /* Fast mode only works when the step is 1 */ |
3580 | 26 | if (fast_mode) { |
3581 | 26 | assert(PyLong_Check(long_step)); |
3582 | 26 | step = PyLong_AsLong(long_step); |
3583 | 26 | if (step != 1) { |
3584 | 0 | fast_mode = 0; |
3585 | 0 | if (step == -1 && PyErr_Occurred()) |
3586 | 0 | PyErr_Clear(); |
3587 | 0 | } |
3588 | 26 | } |
3589 | | |
3590 | 26 | if (fast_mode) |
3591 | 26 | Py_CLEAR(long_cnt); |
3592 | 0 | else |
3593 | 0 | cnt = PY_SSIZE_T_MAX; |
3594 | | |
3595 | 26 | assert((long_cnt == NULL && fast_mode) || |
3596 | 26 | (cnt == PY_SSIZE_T_MAX && long_cnt != NULL && !fast_mode)); |
3597 | 26 | assert(!fast_mode || |
3598 | 26 | (PyLong_Check(long_step) && PyLong_AS_LONG(long_step) == 1)); |
3599 | | |
3600 | | /* create countobject structure */ |
3601 | 26 | lz = (countobject *)type->tp_alloc(type, 0); |
3602 | 26 | if (lz == NULL) { |
3603 | 0 | Py_XDECREF(long_cnt); |
3604 | 0 | Py_DECREF(long_step); |
3605 | 0 | return NULL; |
3606 | 0 | } |
3607 | 26 | lz->cnt = cnt; |
3608 | 26 | lz->long_cnt = long_cnt; |
3609 | 26 | lz->long_step = long_step; |
3610 | | |
3611 | 26 | return (PyObject *)lz; |
3612 | 26 | } |
3613 | | |
3614 | | static void |
3615 | | count_dealloc(PyObject *op) |
3616 | 0 | { |
3617 | 0 | countobject *lz = countobject_CAST(op); |
3618 | 0 | PyTypeObject *tp = Py_TYPE(lz); |
3619 | 0 | PyObject_GC_UnTrack(lz); |
3620 | 0 | Py_XDECREF(lz->long_cnt); |
3621 | 0 | Py_XDECREF(lz->long_step); |
3622 | 0 | tp->tp_free(lz); |
3623 | 0 | Py_DECREF(tp); |
3624 | 0 | } |
3625 | | |
3626 | | static int |
3627 | | count_traverse(PyObject *op, visitproc visit, void *arg) |
3628 | 402 | { |
3629 | 402 | countobject *lz = countobject_CAST(op); |
3630 | 402 | Py_VISIT(Py_TYPE(lz)); |
3631 | 402 | Py_VISIT(lz->long_cnt); |
3632 | 402 | Py_VISIT(lz->long_step); |
3633 | 402 | return 0; |
3634 | 402 | } |
3635 | | |
3636 | | static PyObject * |
3637 | | count_nextlong(countobject *lz) |
3638 | 0 | { |
3639 | 0 | if (lz->long_cnt == NULL) { |
3640 | | /* Switch to slow_mode */ |
3641 | 0 | lz->long_cnt = PyLong_FromSsize_t(PY_SSIZE_T_MAX); |
3642 | 0 | if (lz->long_cnt == NULL) { |
3643 | 0 | return NULL; |
3644 | 0 | } |
3645 | 0 | } |
3646 | 0 | assert(lz->cnt == PY_SSIZE_T_MAX && lz->long_cnt != NULL); |
3647 | | |
3648 | | // We hold one reference to "result" (a.k.a. the old value of |
3649 | | // lz->long_cnt); we'll either return it or keep it in lz->long_cnt. |
3650 | 0 | PyObject *result = lz->long_cnt; |
3651 | |
|
3652 | 0 | PyObject *stepped_up = PyNumber_Add(result, lz->long_step); |
3653 | 0 | if (stepped_up == NULL) { |
3654 | 0 | return NULL; |
3655 | 0 | } |
3656 | 0 | lz->long_cnt = stepped_up; |
3657 | |
|
3658 | 0 | return result; |
3659 | 0 | } |
3660 | | |
3661 | | static PyObject * |
3662 | | count_next(PyObject *op) |
3663 | 4.50k | { |
3664 | 4.50k | countobject *lz = countobject_CAST(op); |
3665 | 4.50k | #ifndef Py_GIL_DISABLED |
3666 | 4.50k | if (lz->cnt == PY_SSIZE_T_MAX) |
3667 | 0 | return count_nextlong(lz); |
3668 | 4.50k | return PyLong_FromSsize_t(lz->cnt++); |
3669 | | #else |
3670 | | // free-threading version |
3671 | | // fast mode uses compare-exchange loop |
3672 | | // slow mode uses a critical section |
3673 | | PyObject *returned; |
3674 | | Py_ssize_t cnt; |
3675 | | |
3676 | | cnt = _Py_atomic_load_ssize_relaxed(&lz->cnt); |
3677 | | for (;;) { |
3678 | | if (cnt == PY_SSIZE_T_MAX) { |
3679 | | Py_BEGIN_CRITICAL_SECTION(lz); |
3680 | | returned = count_nextlong(lz); |
3681 | | Py_END_CRITICAL_SECTION(); |
3682 | | return returned; |
3683 | | } |
3684 | | if (_Py_atomic_compare_exchange_ssize(&lz->cnt, &cnt, cnt + 1)) { |
3685 | | return PyLong_FromSsize_t(cnt); |
3686 | | } |
3687 | | } |
3688 | | #endif |
3689 | 4.50k | } |
3690 | | |
3691 | | static PyObject * |
3692 | | count_repr(PyObject *op) |
3693 | 0 | { |
3694 | 0 | countobject *lz = countobject_CAST(op); |
3695 | 0 | if (lz->long_cnt == NULL) { |
3696 | 0 | Py_ssize_t cnt = FT_ATOMIC_LOAD_SSIZE_RELAXED(lz->cnt); |
3697 | 0 | return PyUnicode_FromFormat("%s(%zd)", |
3698 | 0 | _PyType_Name(Py_TYPE(lz)), cnt); |
3699 | 0 | } |
3700 | | |
3701 | 0 | if (PyLong_Check(lz->long_step)) { |
3702 | 0 | long step = PyLong_AsLong(lz->long_step); |
3703 | 0 | if (step == -1 && PyErr_Occurred()) { |
3704 | 0 | PyErr_Clear(); |
3705 | 0 | } |
3706 | 0 | if (step == 1) { |
3707 | | /* Don't display step when it is an integer equal to 1 */ |
3708 | 0 | return PyUnicode_FromFormat("%s(%R)", |
3709 | 0 | _PyType_Name(Py_TYPE(lz)), |
3710 | 0 | lz->long_cnt); |
3711 | 0 | } |
3712 | 0 | } |
3713 | 0 | return PyUnicode_FromFormat("%s(%R, %R)", |
3714 | 0 | _PyType_Name(Py_TYPE(lz)), |
3715 | 0 | lz->long_cnt, lz->long_step); |
3716 | 0 | } |
3717 | | |
3718 | | static PyType_Slot count_slots[] = { |
3719 | | {Py_tp_dealloc, count_dealloc}, |
3720 | | {Py_tp_repr, count_repr}, |
3721 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
3722 | | {Py_tp_doc, (void *)itertools_count__doc__}, |
3723 | | {Py_tp_traverse, count_traverse}, |
3724 | | {Py_tp_iter, PyObject_SelfIter}, |
3725 | | {Py_tp_iternext, count_next}, |
3726 | | {Py_tp_new, itertools_count}, |
3727 | | {Py_tp_vectorcall, itertools_count_vectorcall}, |
3728 | | {Py_tp_free, PyObject_GC_Del}, |
3729 | | {0, NULL}, |
3730 | | }; |
3731 | | |
3732 | | static PyType_Spec count_spec = { |
3733 | | .name = "itertools.count", |
3734 | | .basicsize = sizeof(countobject), |
3735 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
3736 | | Py_TPFLAGS_IMMUTABLETYPE), |
3737 | | .slots = count_slots, |
3738 | | }; |
3739 | | |
3740 | | |
3741 | | /* repeat object ************************************************************/ |
3742 | | |
3743 | | typedef struct { |
3744 | | PyObject_HEAD |
3745 | | PyObject *element; |
3746 | | Py_ssize_t cnt; |
3747 | | } repeatobject; |
3748 | | |
3749 | 45.0k | #define repeatobject_CAST(op) ((repeatobject *)(op)) |
3750 | | |
3751 | | static PyObject * |
3752 | | repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
3753 | 4.50k | { |
3754 | 4.50k | repeatobject *ro; |
3755 | 4.50k | PyObject *element; |
3756 | 4.50k | Py_ssize_t cnt = -1, n_args; |
3757 | 4.50k | static char *kwargs[] = {"object", "times", NULL}; |
3758 | | |
3759 | 4.50k | n_args = PyTuple_GET_SIZE(args); |
3760 | 4.50k | if (kwds != NULL) |
3761 | 0 | n_args += PyDict_GET_SIZE(kwds); |
3762 | 4.50k | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs, |
3763 | 4.50k | &element, &cnt)) |
3764 | 0 | return NULL; |
3765 | | /* Does user supply times argument? */ |
3766 | 4.50k | if (n_args == 2 && cnt < 0) |
3767 | 0 | cnt = 0; |
3768 | | |
3769 | 4.50k | ro = (repeatobject *)type->tp_alloc(type, 0); |
3770 | 4.50k | if (ro == NULL) |
3771 | 0 | return NULL; |
3772 | 4.50k | ro->element = Py_NewRef(element); |
3773 | 4.50k | ro->cnt = cnt; |
3774 | 4.50k | return (PyObject *)ro; |
3775 | 4.50k | } |
3776 | | |
3777 | | static void |
3778 | | repeat_dealloc(PyObject *op) |
3779 | 4.50k | { |
3780 | 4.50k | repeatobject *ro = repeatobject_CAST(op); |
3781 | 4.50k | PyTypeObject *tp = Py_TYPE(ro); |
3782 | 4.50k | PyObject_GC_UnTrack(ro); |
3783 | 4.50k | Py_XDECREF(ro->element); |
3784 | 4.50k | tp->tp_free(ro); |
3785 | 4.50k | Py_DECREF(tp); |
3786 | 4.50k | } |
3787 | | |
3788 | | static int |
3789 | | repeat_traverse(PyObject *op, visitproc visit, void *arg) |
3790 | 0 | { |
3791 | 0 | repeatobject *ro = repeatobject_CAST(op); |
3792 | 0 | Py_VISIT(Py_TYPE(ro)); |
3793 | 0 | Py_VISIT(ro->element); |
3794 | 0 | return 0; |
3795 | 0 | } |
3796 | | |
3797 | | static PyObject * |
3798 | | repeat_next(PyObject *op) |
3799 | 40.5k | { |
3800 | 40.5k | repeatobject *ro = repeatobject_CAST(op); |
3801 | 40.5k | Py_ssize_t cnt = FT_ATOMIC_LOAD_SSIZE_RELAXED(ro->cnt); |
3802 | 40.5k | if (cnt == 0) { |
3803 | 4.50k | return NULL; |
3804 | 4.50k | } |
3805 | 36.0k | if (cnt > 0) { |
3806 | 36.0k | cnt--; |
3807 | 36.0k | FT_ATOMIC_STORE_SSIZE_RELAXED(ro->cnt, cnt); |
3808 | 36.0k | } |
3809 | 36.0k | return Py_NewRef(ro->element); |
3810 | 40.5k | } |
3811 | | |
3812 | | static PyObject * |
3813 | | repeat_repr(PyObject *op) |
3814 | 0 | { |
3815 | 0 | repeatobject *ro = repeatobject_CAST(op); |
3816 | 0 | if (ro->cnt == -1) |
3817 | 0 | return PyUnicode_FromFormat("%s(%R)", |
3818 | 0 | _PyType_Name(Py_TYPE(ro)), ro->element); |
3819 | 0 | else |
3820 | 0 | return PyUnicode_FromFormat("%s(%R, %zd)", |
3821 | 0 | _PyType_Name(Py_TYPE(ro)), ro->element, |
3822 | 0 | ro->cnt); |
3823 | 0 | } |
3824 | | |
3825 | | static PyObject * |
3826 | | repeat_len(PyObject *op, PyObject *Py_UNUSED(args)) |
3827 | 0 | { |
3828 | 0 | repeatobject *ro = repeatobject_CAST(op); |
3829 | 0 | if (ro->cnt == -1) { |
3830 | 0 | PyErr_SetString(PyExc_TypeError, "len() of unsized object"); |
3831 | 0 | return NULL; |
3832 | 0 | } |
3833 | 0 | return PyLong_FromSize_t(ro->cnt); |
3834 | 0 | } |
3835 | | |
3836 | | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
3837 | | |
3838 | | static PyMethodDef repeat_methods[] = { |
3839 | | {"__length_hint__", repeat_len, METH_NOARGS, length_hint_doc}, |
3840 | | {NULL, NULL} /* sentinel */ |
3841 | | }; |
3842 | | |
3843 | | PyDoc_STRVAR(repeat_doc, |
3844 | | "repeat(object [,times]) -> create an iterator which returns the object\n\ |
3845 | | for the specified number of times. If not specified, returns the object\n\ |
3846 | | endlessly."); |
3847 | | |
3848 | | static PyType_Slot repeat_slots[] = { |
3849 | | {Py_tp_dealloc, repeat_dealloc}, |
3850 | | {Py_tp_repr, repeat_repr}, |
3851 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
3852 | | {Py_tp_doc, (void *)repeat_doc}, |
3853 | | {Py_tp_traverse, repeat_traverse}, |
3854 | | {Py_tp_iter, PyObject_SelfIter}, |
3855 | | {Py_tp_iternext, repeat_next}, |
3856 | | {Py_tp_methods, repeat_methods}, |
3857 | | {Py_tp_new, repeat_new}, |
3858 | | {Py_tp_free, PyObject_GC_Del}, |
3859 | | {0, NULL}, |
3860 | | }; |
3861 | | |
3862 | | static PyType_Spec repeat_spec = { |
3863 | | .name = "itertools.repeat", |
3864 | | .basicsize = sizeof(repeatobject), |
3865 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
3866 | | Py_TPFLAGS_IMMUTABLETYPE), |
3867 | | .slots = repeat_slots, |
3868 | | }; |
3869 | | |
3870 | | |
3871 | | /* ziplongest object *********************************************************/ |
3872 | | |
3873 | | struct ziplongestobject { |
3874 | | PyObject_HEAD |
3875 | | Py_ssize_t tuplesize; |
3876 | | Py_ssize_t numactive; |
3877 | | PyObject *ittuple; /* tuple of iterators */ |
3878 | | PyObject *result; |
3879 | | PyObject *fillvalue; |
3880 | | }; |
3881 | | |
3882 | 0 | #define ziplongestobject_CAST(op) ((ziplongestobject *)(op)) |
3883 | | |
3884 | | static PyObject * |
3885 | | zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
3886 | 0 | { |
3887 | 0 | ziplongestobject *lz; |
3888 | 0 | Py_ssize_t i; |
3889 | 0 | PyObject *ittuple; /* tuple of iterators */ |
3890 | 0 | PyObject *result; |
3891 | 0 | PyObject *fillvalue = Py_None; |
3892 | 0 | Py_ssize_t tuplesize; |
3893 | |
|
3894 | 0 | if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) { |
3895 | 0 | fillvalue = NULL; |
3896 | 0 | if (PyDict_GET_SIZE(kwds) == 1) { |
3897 | 0 | fillvalue = PyDict_GetItemWithError(kwds, &_Py_ID(fillvalue)); |
3898 | 0 | } |
3899 | 0 | if (fillvalue == NULL) { |
3900 | 0 | if (!PyErr_Occurred()) { |
3901 | 0 | PyErr_SetString(PyExc_TypeError, |
3902 | 0 | "zip_longest() got an unexpected keyword argument"); |
3903 | 0 | } |
3904 | 0 | return NULL; |
3905 | 0 | } |
3906 | 0 | } |
3907 | | |
3908 | | /* args must be a tuple */ |
3909 | 0 | assert(PyTuple_Check(args)); |
3910 | 0 | tuplesize = PyTuple_GET_SIZE(args); |
3911 | | |
3912 | | /* obtain iterators */ |
3913 | 0 | ittuple = PyTuple_New(tuplesize); |
3914 | 0 | if (ittuple == NULL) |
3915 | 0 | return NULL; |
3916 | 0 | for (i=0; i < tuplesize; i++) { |
3917 | 0 | PyObject *item = PyTuple_GET_ITEM(args, i); |
3918 | 0 | PyObject *it = PyObject_GetIter(item); |
3919 | 0 | if (it == NULL) { |
3920 | 0 | Py_DECREF(ittuple); |
3921 | 0 | return NULL; |
3922 | 0 | } |
3923 | 0 | PyTuple_SET_ITEM(ittuple, i, it); |
3924 | 0 | } |
3925 | | |
3926 | | /* create a result holder */ |
3927 | 0 | result = PyTuple_New(tuplesize); |
3928 | 0 | if (result == NULL) { |
3929 | 0 | Py_DECREF(ittuple); |
3930 | 0 | return NULL; |
3931 | 0 | } |
3932 | 0 | for (i=0 ; i < tuplesize ; i++) { |
3933 | 0 | Py_INCREF(Py_None); |
3934 | 0 | PyTuple_SET_ITEM(result, i, Py_None); |
3935 | 0 | } |
3936 | | |
3937 | | /* create ziplongestobject structure */ |
3938 | 0 | lz = (ziplongestobject *)type->tp_alloc(type, 0); |
3939 | 0 | if (lz == NULL) { |
3940 | 0 | Py_DECREF(ittuple); |
3941 | 0 | Py_DECREF(result); |
3942 | 0 | return NULL; |
3943 | 0 | } |
3944 | 0 | lz->ittuple = ittuple; |
3945 | 0 | lz->tuplesize = tuplesize; |
3946 | 0 | lz->numactive = tuplesize; |
3947 | 0 | lz->result = result; |
3948 | 0 | lz->fillvalue = Py_NewRef(fillvalue); |
3949 | 0 | return (PyObject *)lz; |
3950 | 0 | } |
3951 | | |
3952 | | static void |
3953 | | zip_longest_dealloc(PyObject *op) |
3954 | 0 | { |
3955 | 0 | ziplongestobject *lz = ziplongestobject_CAST(op); |
3956 | 0 | PyTypeObject *tp = Py_TYPE(lz); |
3957 | 0 | PyObject_GC_UnTrack(lz); |
3958 | 0 | Py_XDECREF(lz->ittuple); |
3959 | 0 | Py_XDECREF(lz->result); |
3960 | 0 | Py_XDECREF(lz->fillvalue); |
3961 | 0 | tp->tp_free(lz); |
3962 | 0 | Py_DECREF(tp); |
3963 | 0 | } |
3964 | | |
3965 | | static int |
3966 | | zip_longest_traverse(PyObject *op, visitproc visit, void *arg) |
3967 | 0 | { |
3968 | 0 | ziplongestobject *lz = ziplongestobject_CAST(op); |
3969 | 0 | Py_VISIT(Py_TYPE(lz)); |
3970 | 0 | Py_VISIT(lz->ittuple); |
3971 | 0 | Py_VISIT(lz->result); |
3972 | 0 | Py_VISIT(lz->fillvalue); |
3973 | 0 | return 0; |
3974 | 0 | } |
3975 | | |
3976 | | /*[clinic input] |
3977 | | @critical_section |
3978 | | itertools.zip_longest.__next__ as zip_longest_next |
3979 | | [clinic start generated code]*/ |
3980 | | |
3981 | | static PyObject * |
3982 | | zip_longest_next_impl(ziplongestobject *self) |
3983 | | /*[clinic end generated code: output=11bc756cf896c576 input=5b1e3b52098852a9]*/ |
3984 | 0 | { |
3985 | 0 | ziplongestobject *lz = self; |
3986 | 0 | Py_ssize_t i; |
3987 | 0 | Py_ssize_t tuplesize = lz->tuplesize; |
3988 | 0 | PyObject *result = lz->result; |
3989 | 0 | PyObject *it; |
3990 | 0 | PyObject *item; |
3991 | 0 | PyObject *olditem; |
3992 | |
|
3993 | 0 | if (tuplesize == 0) |
3994 | 0 | return NULL; |
3995 | 0 | if (lz->numactive == 0) |
3996 | 0 | return NULL; |
3997 | 0 | if (_PyObject_IsUniquelyReferenced(result)) { |
3998 | 0 | Py_INCREF(result); |
3999 | 0 | for (i=0 ; i < tuplesize ; i++) { |
4000 | 0 | it = PyTuple_GET_ITEM(lz->ittuple, i); |
4001 | 0 | if (it == NULL) { |
4002 | 0 | item = Py_NewRef(lz->fillvalue); |
4003 | 0 | } else { |
4004 | 0 | item = PyIter_Next(it); |
4005 | 0 | if (item == NULL) { |
4006 | 0 | lz->numactive -= 1; |
4007 | 0 | if (lz->numactive == 0 || PyErr_Occurred()) { |
4008 | 0 | lz->numactive = 0; |
4009 | 0 | Py_DECREF(result); |
4010 | 0 | return NULL; |
4011 | 0 | } else { |
4012 | 0 | item = Py_NewRef(lz->fillvalue); |
4013 | 0 | PyTuple_SET_ITEM(lz->ittuple, i, NULL); |
4014 | 0 | Py_DECREF(it); |
4015 | 0 | } |
4016 | 0 | } |
4017 | 0 | } |
4018 | 0 | olditem = PyTuple_GET_ITEM(result, i); |
4019 | 0 | PyTuple_SET_ITEM(result, i, item); |
4020 | 0 | Py_DECREF(olditem); |
4021 | 0 | } |
4022 | | // bpo-42536: The GC may have untracked this result tuple. Since we're |
4023 | | // recycling it, make sure it's tracked again: |
4024 | 0 | _PyTuple_Recycle(result); |
4025 | 0 | } else { |
4026 | 0 | result = PyTuple_New(tuplesize); |
4027 | 0 | if (result == NULL) |
4028 | 0 | return NULL; |
4029 | 0 | for (i=0 ; i < tuplesize ; i++) { |
4030 | 0 | it = PyTuple_GET_ITEM(lz->ittuple, i); |
4031 | 0 | if (it == NULL) { |
4032 | 0 | item = Py_NewRef(lz->fillvalue); |
4033 | 0 | } else { |
4034 | 0 | item = PyIter_Next(it); |
4035 | 0 | if (item == NULL) { |
4036 | 0 | lz->numactive -= 1; |
4037 | 0 | if (lz->numactive == 0 || PyErr_Occurred()) { |
4038 | 0 | lz->numactive = 0; |
4039 | 0 | Py_DECREF(result); |
4040 | 0 | return NULL; |
4041 | 0 | } else { |
4042 | 0 | item = Py_NewRef(lz->fillvalue); |
4043 | 0 | PyTuple_SET_ITEM(lz->ittuple, i, NULL); |
4044 | 0 | Py_DECREF(it); |
4045 | 0 | } |
4046 | 0 | } |
4047 | 0 | } |
4048 | 0 | PyTuple_SET_ITEM(result, i, item); |
4049 | 0 | } |
4050 | 0 | } |
4051 | 0 | return result; |
4052 | 0 | } |
4053 | | |
4054 | | PyDoc_STRVAR(zip_longest_doc, |
4055 | | "zip_longest(*iterables, fillvalue=None)\n\ |
4056 | | --\n\ |
4057 | | \n\ |
4058 | | Return a zip_longest object whose .__next__() method returns a tuple where\n\ |
4059 | | the i-th element comes from the i-th iterable argument. The .__next__()\n\ |
4060 | | method continues until the longest iterable in the argument sequence\n\ |
4061 | | is exhausted and then it raises StopIteration. When the shorter iterables\n\ |
4062 | | are exhausted, the fillvalue is substituted in their place. The fillvalue\n\ |
4063 | | defaults to None or can be specified by a keyword argument.\n\ |
4064 | | "); |
4065 | | |
4066 | | static PyType_Slot ziplongest_slots[] = { |
4067 | | {Py_tp_dealloc, zip_longest_dealloc}, |
4068 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
4069 | | {Py_tp_doc, (void *)zip_longest_doc}, |
4070 | | {Py_tp_traverse, zip_longest_traverse}, |
4071 | | {Py_tp_iter, PyObject_SelfIter}, |
4072 | | {Py_tp_iternext, zip_longest_next}, |
4073 | | {Py_tp_new, zip_longest_new}, |
4074 | | {Py_tp_free, PyObject_GC_Del}, |
4075 | | {0, NULL}, |
4076 | | }; |
4077 | | |
4078 | | static PyType_Spec ziplongest_spec = { |
4079 | | .name = "itertools.zip_longest", |
4080 | | .basicsize = sizeof(ziplongestobject), |
4081 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | |
4082 | | Py_TPFLAGS_IMMUTABLETYPE), |
4083 | | .slots = ziplongest_slots, |
4084 | | }; |
4085 | | |
4086 | | |
4087 | | /* module level code ********************************************************/ |
4088 | | |
4089 | | PyDoc_STRVAR(module_doc, |
4090 | | "Functional tools for creating and using iterators.\n\ |
4091 | | \n\ |
4092 | | Infinite iterators:\n\ |
4093 | | count(start=0, step=1) --> start, start+step, start+2*step, ...\n\ |
4094 | | cycle(p) --> p0, p1, ... plast, p0, p1, ...\n\ |
4095 | | repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times\n\ |
4096 | | \n\ |
4097 | | Iterators terminating on the shortest input sequence:\n\ |
4098 | | accumulate(p[, func]) --> p0, p0+p1, p0+p1+p2\n\ |
4099 | | batched(p, n) --> [p0, p1, ..., p_n-1], [p_n, p_n+1, ..., p_2n-1], ...\n\ |
4100 | | chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ...\n\ |
4101 | | chain.from_iterable([p, q, ...]) --> p0, p1, ... plast, q0, q1, ...\n\ |
4102 | | compress(data, selectors) --> (d[0] if s[0]), (d[1] if s[1]), ...\n\ |
4103 | | dropwhile(predicate, seq) --> seq[n], seq[n+1], starting when predicate fails\n\ |
4104 | | groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)\n\ |
4105 | | filterfalse(predicate, seq) --> elements of seq where predicate(elem) is False\n\ |
4106 | | islice(seq, [start,] stop [, step]) --> elements from\n\ |
4107 | | seq[start:stop:step]\n\ |
4108 | | pairwise(s) --> (s[0],s[1]), (s[1],s[2]), (s[2], s[3]), ...\n\ |
4109 | | starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\ |
4110 | | tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n\n\ |
4111 | | takewhile(predicate, seq) --> seq[0], seq[1], until predicate fails\n\ |
4112 | | zip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ...\n\ |
4113 | | \n\ |
4114 | | Combinatoric generators:\n\ |
4115 | | product(p, q, ... [repeat=1]) --> cartesian product\n\ |
4116 | | permutations(p[, r])\n\ |
4117 | | combinations(p, r)\n\ |
4118 | | combinations_with_replacement(p, r)\n\ |
4119 | | "); |
4120 | | |
4121 | | static int |
4122 | | itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg) |
4123 | 2.01k | { |
4124 | 2.01k | itertools_state *state = get_module_state(mod); |
4125 | 2.01k | Py_VISIT(state->accumulate_type); |
4126 | 2.01k | Py_VISIT(state->batched_type); |
4127 | 2.01k | Py_VISIT(state->chain_type); |
4128 | 2.01k | Py_VISIT(state->combinations_type); |
4129 | 2.01k | Py_VISIT(state->compress_type); |
4130 | 2.01k | Py_VISIT(state->count_type); |
4131 | 2.01k | Py_VISIT(state->cwr_type); |
4132 | 2.01k | Py_VISIT(state->cycle_type); |
4133 | 2.01k | Py_VISIT(state->dropwhile_type); |
4134 | 2.01k | Py_VISIT(state->filterfalse_type); |
4135 | 2.01k | Py_VISIT(state->groupby_type); |
4136 | 2.01k | Py_VISIT(state->_grouper_type); |
4137 | 2.01k | Py_VISIT(state->islice_type); |
4138 | 2.01k | Py_VISIT(state->pairwise_type); |
4139 | 2.01k | Py_VISIT(state->permutations_type); |
4140 | 2.01k | Py_VISIT(state->product_type); |
4141 | 2.01k | Py_VISIT(state->repeat_type); |
4142 | 2.01k | Py_VISIT(state->starmap_type); |
4143 | 2.01k | Py_VISIT(state->takewhile_type); |
4144 | 2.01k | Py_VISIT(state->tee_type); |
4145 | 2.01k | Py_VISIT(state->teedataobject_type); |
4146 | 2.01k | Py_VISIT(state->ziplongest_type); |
4147 | 2.01k | return 0; |
4148 | 2.01k | } |
4149 | | |
4150 | | static int |
4151 | | itertoolsmodule_clear(PyObject *mod) |
4152 | 0 | { |
4153 | 0 | itertools_state *state = get_module_state(mod); |
4154 | 0 | Py_CLEAR(state->accumulate_type); |
4155 | 0 | Py_CLEAR(state->batched_type); |
4156 | 0 | Py_CLEAR(state->chain_type); |
4157 | 0 | Py_CLEAR(state->combinations_type); |
4158 | 0 | Py_CLEAR(state->compress_type); |
4159 | 0 | Py_CLEAR(state->count_type); |
4160 | 0 | Py_CLEAR(state->cwr_type); |
4161 | 0 | Py_CLEAR(state->cycle_type); |
4162 | 0 | Py_CLEAR(state->dropwhile_type); |
4163 | 0 | Py_CLEAR(state->filterfalse_type); |
4164 | 0 | Py_CLEAR(state->groupby_type); |
4165 | 0 | Py_CLEAR(state->_grouper_type); |
4166 | 0 | Py_CLEAR(state->islice_type); |
4167 | 0 | Py_CLEAR(state->pairwise_type); |
4168 | 0 | Py_CLEAR(state->permutations_type); |
4169 | 0 | Py_CLEAR(state->product_type); |
4170 | 0 | Py_CLEAR(state->repeat_type); |
4171 | 0 | Py_CLEAR(state->starmap_type); |
4172 | 0 | Py_CLEAR(state->takewhile_type); |
4173 | 0 | Py_CLEAR(state->tee_type); |
4174 | 0 | Py_CLEAR(state->teedataobject_type); |
4175 | 0 | Py_CLEAR(state->ziplongest_type); |
4176 | 0 | return 0; |
4177 | 0 | } |
4178 | | |
4179 | | static void |
4180 | | itertoolsmodule_free(void *mod) |
4181 | 0 | { |
4182 | 0 | (void)itertoolsmodule_clear((PyObject *)mod); |
4183 | 0 | } |
4184 | | |
4185 | 682 | #define ADD_TYPE(module, type, spec) \ |
4186 | 682 | do { \ |
4187 | 682 | type = (PyTypeObject *)PyType_FromModuleAndSpec(module, spec, NULL); \ |
4188 | 682 | if (type == NULL) { \ |
4189 | 0 | return -1; \ |
4190 | 0 | } \ |
4191 | 682 | if (PyModule_AddType(module, type) < 0) { \ |
4192 | 0 | return -1; \ |
4193 | 0 | } \ |
4194 | 682 | } while (0) |
4195 | | |
4196 | | static int |
4197 | | itertoolsmodule_exec(PyObject *mod) |
4198 | 31 | { |
4199 | 31 | itertools_state *state = get_module_state(mod); |
4200 | 31 | ADD_TYPE(mod, state->accumulate_type, &accumulate_spec); |
4201 | 31 | ADD_TYPE(mod, state->batched_type, &batched_spec); |
4202 | 31 | ADD_TYPE(mod, state->chain_type, &chain_spec); |
4203 | 31 | ADD_TYPE(mod, state->combinations_type, &combinations_spec); |
4204 | 31 | ADD_TYPE(mod, state->compress_type, &compress_spec); |
4205 | 31 | ADD_TYPE(mod, state->count_type, &count_spec); |
4206 | 31 | ADD_TYPE(mod, state->cwr_type, &cwr_spec); |
4207 | 31 | ADD_TYPE(mod, state->cycle_type, &cycle_spec); |
4208 | 31 | ADD_TYPE(mod, state->dropwhile_type, &dropwhile_spec); |
4209 | 31 | ADD_TYPE(mod, state->filterfalse_type, &filterfalse_spec); |
4210 | 31 | ADD_TYPE(mod, state->groupby_type, &groupby_spec); |
4211 | 31 | ADD_TYPE(mod, state->_grouper_type, &_grouper_spec); |
4212 | 31 | ADD_TYPE(mod, state->islice_type, &islice_spec); |
4213 | 31 | ADD_TYPE(mod, state->pairwise_type, &pairwise_spec); |
4214 | 31 | ADD_TYPE(mod, state->permutations_type, &permutations_spec); |
4215 | 31 | ADD_TYPE(mod, state->product_type, &product_spec); |
4216 | 31 | ADD_TYPE(mod, state->repeat_type, &repeat_spec); |
4217 | 31 | ADD_TYPE(mod, state->starmap_type, &starmap_spec); |
4218 | 31 | ADD_TYPE(mod, state->takewhile_type, &takewhile_spec); |
4219 | 31 | ADD_TYPE(mod, state->tee_type, &tee_spec); |
4220 | 31 | ADD_TYPE(mod, state->teedataobject_type, &teedataobject_spec); |
4221 | 31 | ADD_TYPE(mod, state->ziplongest_type, &ziplongest_spec); |
4222 | | |
4223 | 31 | Py_SET_TYPE(state->teedataobject_type, &PyType_Type); |
4224 | 31 | return 0; |
4225 | 31 | } |
4226 | | |
4227 | | static struct PyModuleDef_Slot itertoolsmodule_slots[] = { |
4228 | | _Py_ABI_SLOT, |
4229 | | {Py_mod_exec, itertoolsmodule_exec}, |
4230 | | {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, |
4231 | | {Py_mod_gil, Py_MOD_GIL_NOT_USED}, |
4232 | | {0, NULL} |
4233 | | }; |
4234 | | |
4235 | | static PyMethodDef module_methods[] = { |
4236 | | ITERTOOLS_TEE_METHODDEF |
4237 | | {NULL, NULL} /* sentinel */ |
4238 | | }; |
4239 | | |
4240 | | |
4241 | | static struct PyModuleDef itertoolsmodule = { |
4242 | | .m_base = PyModuleDef_HEAD_INIT, |
4243 | | .m_name = "itertools", |
4244 | | .m_doc = module_doc, |
4245 | | .m_size = sizeof(itertools_state), |
4246 | | .m_methods = module_methods, |
4247 | | .m_slots = itertoolsmodule_slots, |
4248 | | .m_traverse = itertoolsmodule_traverse, |
4249 | | .m_clear = itertoolsmodule_clear, |
4250 | | .m_free = itertoolsmodule_free, |
4251 | | }; |
4252 | | |
4253 | | PyMODINIT_FUNC |
4254 | | PyInit_itertools(void) |
4255 | 31 | { |
4256 | 31 | return PyModuleDef_Init(&itertoolsmodule); |
4257 | 31 | } |