annotate Modules/itertoolsmodule.c @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents 2c389a0448a4
children 61fa6b55c1de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2 #include "Python.h"
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
3
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
4 /* Itertools module written and maintained
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
5 by Raymond D. Hettinger <python@rcn.com>
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6 Copyright (c) 2003 Python Software Foundation.
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 All rights reserved.
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
8 */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
9
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
10 /* dropwhile object **********************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
11
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
12 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
13 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
14 PyObject *func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
15 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
16 long start;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
17 } dropwhileobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
18
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
19 PyTypeObject dropwhile_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
20
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
21 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
22 dropwhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
23 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
24 PyObject *func, *seq;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
25 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
26 dropwhileobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
27
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
28 if (!PyArg_UnpackTuple(args, "dropwhile", 2, 2, &func, &seq))
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
29 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
30
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
31 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
32 it = PyObject_GetIter(seq);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
33 if (it == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
34 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
35
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
36 /* create dropwhileobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
37 lz = (dropwhileobject *)type->tp_alloc(type, 0);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
38 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
39 Py_DECREF(it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
40 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
41 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
42 Py_INCREF(func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
43 lz->func = func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
44 lz->it = it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
45 lz->start = 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
46
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
47 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
48 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
49
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
50 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
51 dropwhile_dealloc(dropwhileobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
52 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
53 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
54 Py_XDECREF(lz->func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
55 Py_XDECREF(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
56 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
57 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
58
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
59 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
60 dropwhile_traverse(dropwhileobject *lz, visitproc visit, void *arg)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
61 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
62 int err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
63
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
64 if (lz->it) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
65 err = visit(lz->it, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
66 if (err)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
67 return err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
68 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
69 if (lz->func) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
70 err = visit(lz->func, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
71 if (err)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
72 return err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
73 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
74 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
75 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
76
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
77 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
78 dropwhile_next(dropwhileobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
79 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
80 PyObject *item, *good;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
81 long ok;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
82
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
83 for (;;) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
84 item = PyIter_Next(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85 if (item == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
86 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
87 if (lz->start == 1)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
88 return item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
89
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
90 good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91 if (good == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
93 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
94 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
95 ok = PyObject_IsTrue(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
96 Py_DECREF(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
97 if (!ok) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
98 lz->start = 1;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99 return item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
100 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
101 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
102 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
103 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
104
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
105 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
106 dropwhile_getiter(PyObject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
107 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
108 Py_INCREF(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
109 return lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
110 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
111
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
112 PyDoc_STRVAR(dropwhile_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
113 "dropwhile(predicate, iterable) --> dropwhile object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
114 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
115 Drop items from the iterable while predicate(item) is true.\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
116 Afterwards, return every element until the iterable is exhausted.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
117
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
118 PyTypeObject dropwhile_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
119 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
120 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
121 "itertools.dropwhile", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
122 sizeof(dropwhileobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
123 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
124 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
125 (destructor)dropwhile_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
126 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
127 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
128 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
129 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
130 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
131 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
132 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
134 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
135 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
136 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
137 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
138 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
139 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
140 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
141 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
142 dropwhile_doc, /* tp_doc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
143 (traverseproc)dropwhile_traverse, /* tp_traverse */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
144 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
145 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
146 0, /* tp_weaklistoffset */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
147 (getiterfunc)dropwhile_getiter, /* tp_iter */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
148 (iternextfunc)dropwhile_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
149 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
150 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
151 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
152 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
153 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
154 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
155 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
156 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
157 0, /* tp_init */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
158 PyType_GenericAlloc, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
159 dropwhile_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
160 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
161 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
162
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
163
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
164 /* takewhile object **********************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
165
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
166 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
167 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
168 PyObject *func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
169 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
170 long stop;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
171 } takewhileobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
172
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
173 PyTypeObject takewhile_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
174
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
175 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
176 takewhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
177 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
178 PyObject *func, *seq;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
179 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
180 takewhileobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
181
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
182 if (!PyArg_UnpackTuple(args, "takewhile", 2, 2, &func, &seq))
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
183 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
184
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
185 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
186 it = PyObject_GetIter(seq);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
187 if (it == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
188 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
189
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
190 /* create takewhileobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
191 lz = (takewhileobject *)type->tp_alloc(type, 0);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
192 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
193 Py_DECREF(it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
194 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
195 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
196 Py_INCREF(func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
197 lz->func = func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
198 lz->it = it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
199 lz->stop = 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
200
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
201 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
202 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
203
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
204 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
205 takewhile_dealloc(takewhileobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
206 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
207 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
208 Py_XDECREF(lz->func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
209 Py_XDECREF(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
210 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
211 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
212
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
213 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
214 takewhile_traverse(takewhileobject *lz, visitproc visit, void *arg)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
215 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
216 int err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
217
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
218 if (lz->it) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
219 err = visit(lz->it, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
220 if (err)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
221 return err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
222 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
223 if (lz->func) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
224 err = visit(lz->func, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
225 if (err)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
226 return err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
227 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
228 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
229 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
230
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
231 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
232 takewhile_next(takewhileobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
233 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
234 PyObject *item, *good;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
235 long ok;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
236
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
237 if (lz->stop == 1)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
238 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
239
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
240 item = PyIter_Next(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
241 if (item == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
242 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
243
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
244 good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
245 if (good == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
246 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
247 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
248 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
249 ok = PyObject_IsTrue(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
250 Py_DECREF(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
251 if (ok)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
252 return item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
253 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
254 lz->stop = 1;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
255 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
256 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
257
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
258 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
259 takewhile_getiter(PyObject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
260 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
261 Py_INCREF(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
262 return lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
263 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
264
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
265 PyDoc_STRVAR(takewhile_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
266 "takewhile(predicate, iterable) --> takewhile object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
267 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
268 Return successive entries from an iterable as long as the \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
269 predicate evaluates to true for each entry.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
270
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
271 PyTypeObject takewhile_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
272 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
273 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
274 "itertools.takewhile", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
275 sizeof(takewhileobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
276 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
277 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
278 (destructor)takewhile_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
279 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
280 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
281 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
282 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
283 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
284 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
285 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
286 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
287 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
288 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
289 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
290 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
291 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
292 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
293 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
294 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
295 takewhile_doc, /* tp_doc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
296 (traverseproc)takewhile_traverse, /* tp_traverse */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
297 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
298 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
299 0, /* tp_weaklistoffset */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
300 (getiterfunc)takewhile_getiter, /* tp_iter */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
301 (iternextfunc)takewhile_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
302 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
303 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
304 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
305 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
306 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
307 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
308 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
309 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
310 0, /* tp_init */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
311 PyType_GenericAlloc, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
312 takewhile_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
313 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
314 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
315
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
316
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
317 /* islice object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
318
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
319 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
320 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
321 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
322 long next;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
323 long stop;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
324 long step;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
325 long cnt;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
326 } isliceobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
327
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
328 PyTypeObject islice_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
329
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
330 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
331 islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
332 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
333 PyObject *seq;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
334 long a1=0, a2=0, a3=0, start=0, stop=0, step=1;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
335 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
336 int numargs;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
337 isliceobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
338
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
339 numargs = PyTuple_Size(args);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
340 if (!PyArg_ParseTuple(args, "Ol|ll:islice", &seq, &a1, &a2, &a3))
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
341 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
342
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
343 if (numargs == 2) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
344 stop = a1;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
345 } else if (numargs == 3) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
346 start = a1;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
347 stop = a2;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
348 } else {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
349 start = a1;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
350 stop = a2;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
351 step = a3;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
352 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
353
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
354 if (start<0 || stop<0) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
355 PyErr_SetString(PyExc_ValueError,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
356 "Indices for islice() must be positive.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
357 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
358 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
359
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
360 if (step<1) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
361 PyErr_SetString(PyExc_ValueError,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
362 "Step must be one or larger for islice().");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
363 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
364 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
365
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
366 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
367 it = PyObject_GetIter(seq);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
368 if (it == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
369 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
370
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
371 /* create isliceobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
372 lz = (isliceobject *)type->tp_alloc(type, 0);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
373 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
374 Py_DECREF(it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
375 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
376 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
377 lz->it = it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
378 lz->next = start;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
379 lz->stop = stop;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
380 lz->step = step;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
381 lz->cnt = 0L;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
382
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
383 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
384 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
385
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
386 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
387 islice_dealloc(isliceobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
388 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
389 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
390 Py_XDECREF(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
391 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
392 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
393
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
394 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
395 islice_traverse(isliceobject *lz, visitproc visit, void *arg)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
396 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
397 if (lz->it)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
398 return visit(lz->it, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
399 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
400 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
401
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
402 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
403 islice_next(isliceobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
404 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
405 PyObject *item;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
406 long oldnext;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
407
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
408 while (lz->cnt < lz->next) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
409 item = PyIter_Next(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
410 if (item == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
411 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
412 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
413 lz->cnt++;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
414 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
415 if (lz->cnt >= lz->stop)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
416 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
417 item = PyIter_Next(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
418 if (item == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
419 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
420 lz->cnt++;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
421 oldnext = lz->next;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
422 lz->next += lz->step;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
423 if (lz->next < oldnext) /* Check for overflow */
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
424 lz->next = lz->stop;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
425 return item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
426 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
427
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
428 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
429 islice_getiter(PyObject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
430 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
431 Py_INCREF(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
432 return lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
433 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
434
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
435 PyDoc_STRVAR(islice_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
436 "islice(iterable, [start,] stop [, step]) --> islice object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
437 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
438 Return an iterator whose next() method returns selected values from an\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
439 iterable. If start is specified, will skip all preceding elements;\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
440 otherwise, start defaults to zero. Step defaults to one. If\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
441 specified as another value, step determines how many values are \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
442 skipped between successive calls. Works like a slice() on a list\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
443 but returns an iterator.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
444
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
445 PyTypeObject islice_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
446 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
447 0, /* ob_size */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
448 "itertools.islice", /* tp_name */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
449 sizeof(isliceobject), /* tp_basicsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
450 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
451 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
452 (destructor)islice_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
453 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
454 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
455 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
456 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
457 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
458 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
459 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
460 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
461 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
462 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
463 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
464 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
465 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
466 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
467 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
468 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
469 islice_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
470 (traverseproc)islice_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
471 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
472 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
473 0, /* tp_weaklistoffset */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
474 (getiterfunc)islice_getiter, /* tp_iter */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
475 (iternextfunc)islice_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
476 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
477 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
478 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
479 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
480 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
481 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
482 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
483 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
484 0, /* tp_init */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
485 PyType_GenericAlloc, /* tp_alloc */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
486 islice_new, /* tp_new */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
487 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
488 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
489
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
490
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
491 /* starmap object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
492
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
493 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
494 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
495 PyObject *func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
496 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
497 } starmapobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
498
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
499 PyTypeObject starmap_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
500
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
501 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
502 starmap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
503 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
504 PyObject *func, *seq;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
505 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
506 starmapobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
507
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
508 if (!PyArg_UnpackTuple(args, "starmap", 2, 2, &func, &seq))
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
509 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
510
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
511 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
512 it = PyObject_GetIter(seq);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
513 if (it == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
514 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
515
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
516 /* create starmapobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
517 lz = (starmapobject *)type->tp_alloc(type, 0);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
518 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
519 Py_DECREF(it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
520 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
521 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
522 Py_INCREF(func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
523 lz->func = func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
524 lz->it = it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
525
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
526 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
527 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
528
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
529 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
530 starmap_dealloc(starmapobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
531 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
532 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
533 Py_XDECREF(lz->func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
534 Py_XDECREF(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
535 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
536 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
537
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
538 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
539 starmap_traverse(starmapobject *lz, visitproc visit, void *arg)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
540 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
541 int err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
542
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
543 if (lz->it) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
544 err = visit(lz->it, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
545 if (err)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
546 return err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
547 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
548 if (lz->func) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
549 err = visit(lz->func, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
550 if (err)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
551 return err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
552 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
553 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
554 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
555
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
556 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
557 starmap_next(starmapobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
558 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
559 PyObject *args;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
560 PyObject *result;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
561
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
562 args = PyIter_Next(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
563 if (args == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
564 return NULL;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
565 if (!PyTuple_CheckExact(args)) {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
566 Py_DECREF(args);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
567 PyErr_SetString(PyExc_TypeError,
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
568 "iterator must return a tuple");
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
569 return NULL;
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
570 }
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
571 result = PyObject_Call(lz->func, args, NULL);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
572 Py_DECREF(args);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
573 return result;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
574 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
575
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
576 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
577 starmap_getiter(PyObject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
578 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
579 Py_INCREF(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
580 return lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
581 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
582
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
583 PyDoc_STRVAR(starmap_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
584 "starmap(function, sequence) --> starmap object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
585 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
586 Return an iterator whose values are returned from the function evaluated\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
587 with a argument tuple taken from the given sequence.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
588
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
589 PyTypeObject starmap_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
590 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
591 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
592 "itertools.starmap", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
593 sizeof(starmapobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
594 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
595 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
596 (destructor)starmap_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
597 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
598 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
599 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
600 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
601 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
602 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
603 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
604 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
605 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
606 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
607 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
608 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
609 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
610 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
611 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
612 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
613 starmap_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
614 (traverseproc)starmap_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
615 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
616 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
617 0, /* tp_weaklistoffset */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
618 (getiterfunc)starmap_getiter, /* tp_iter */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
619 (iternextfunc)starmap_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
620 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
621 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
622 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
623 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
624 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
625 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
626 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
627 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
628 0, /* tp_init */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
629 PyType_GenericAlloc, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
630 starmap_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
631 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
632 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
633
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
634
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
635 /* imap object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
636
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
637 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
638 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
639 PyObject *iters;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
640 PyObject *func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
641 } imapobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
642
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
643 PyTypeObject imap_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
644
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
645 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
646 imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
647 {
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
648 PyObject *it, *iters, *func;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
649 imapobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
650 int numargs, i;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
651
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
652 numargs = PyTuple_Size(args);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
653 if (numargs < 2) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
654 PyErr_SetString(PyExc_TypeError,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
655 "imap() must have at least two arguments.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
656 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
657 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
658
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
659 iters = PyTuple_New(numargs-1);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
660 if (iters == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
661 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
662
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
663 for (i=1 ; i<numargs ; i++) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
664 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
665 it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
666 if (it == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
667 Py_DECREF(iters);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
668 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
669 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
670 PyTuple_SET_ITEM(iters, i-1, it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
671 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
672
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
673 /* create imapobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
674 lz = (imapobject *)type->tp_alloc(type, 0);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
675 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
676 Py_DECREF(iters);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
677 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
678 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
679 lz->iters = iters;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
680 func = PyTuple_GET_ITEM(args, 0);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
681 Py_INCREF(func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
682 lz->func = func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
683
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
684 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
685 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
686
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
687 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
688 imap_dealloc(imapobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
689 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
690 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
691 Py_XDECREF(lz->iters);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
692 Py_XDECREF(lz->func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
693 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
694 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
695
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
696 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
697 imap_traverse(imapobject *lz, visitproc visit, void *arg)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
698 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
699 int err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
700
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
701 if (lz->iters) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
702 err = visit(lz->iters, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
703 if (err)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
704 return err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
705 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
706 if (lz->func) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
707 err = visit(lz->func, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
708 if (err)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
709 return err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
710 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
711 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
712 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
713
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
714 /*
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
715 imap() is an iterator version of __builtins__.map() except that it does
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
716 not have the None fill-in feature. That was intentionally left out for
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
717 the following reasons:
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
718
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
719 1) Itertools are designed to be easily combined and chained together.
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
720 Having all tools stop with the shortest input is a unifying principle
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
721 that makes it easier to combine finite iterators (supplying data) with
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
722 infinite iterators like count() and repeat() (for supplying sequential
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
723 or constant arguments to a function).
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
724
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
725 2) In typical use cases for combining itertools, having one finite data
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
726 supplier run out before another is likely to be an error condition which
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
727 should not pass silently by automatically supplying None.
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
728
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
729 3) The use cases for automatic None fill-in are rare -- not many functions
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
730 do something useful when a parameter suddenly switches type and becomes
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
731 None.
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
732
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
733 4) If a need does arise, it can be met by __builtins__.map() or by
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
734 writing a generator.
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
735
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
736 5) Similar toolsets in Haskell and SML do not have automatic None fill-in.
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
737 */
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
738
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
739 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
740 imap_next(imapobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
741 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
742 PyObject *val;
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
743 PyObject *argtuple;
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
744 PyObject *result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
745 int numargs, i;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
746
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
747 numargs = PyTuple_Size(lz->iters);
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
748 argtuple = PyTuple_New(numargs);
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
749 if (argtuple == NULL)
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
750 return NULL;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
751
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
752 for (i=0 ; i<numargs ; i++) {
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
753 val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i));
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
754 if (val == NULL) {
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
755 Py_DECREF(argtuple);
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
756 return NULL;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
757 }
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
758 PyTuple_SET_ITEM(argtuple, i, val);
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
759 }
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
760 if (lz->func == Py_None)
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
761 return argtuple;
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
762 result = PyObject_Call(lz->func, argtuple, NULL);
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
763 Py_DECREF(argtuple);
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
764 return result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
765 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
766
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
767 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
768 imap_getiter(PyObject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
769 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
770 Py_INCREF(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
771 return lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
772 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
773
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
774 PyDoc_STRVAR(imap_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
775 "imap(func, *iterables) --> imap object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
776 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
777 Make an iterator that computes the function using arguments from\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
778 each of the iterables. Like map() except that it returns\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
779 an iterator instead of a list and that it stops when the shortest\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
780 iterable is exhausted instead of filling in None for shorter\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
781 iterables.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
782
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
783 PyTypeObject imap_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
784 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
785 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
786 "itertools.imap", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
787 sizeof(imapobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
788 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
789 /* methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
790 (destructor)imap_dealloc, /* tp_dealloc */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
791 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
792 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
793 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
794 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
795 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
796 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
797 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
798 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
799 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
800 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
801 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
802 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
803 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
804 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
805 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
806 Py_TPFLAGS_BASETYPE, /* tp_flags */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
807 imap_doc, /* tp_doc */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
808 (traverseproc)imap_traverse, /* tp_traverse */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
809 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
810 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
811 0, /* tp_weaklistoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
812 (getiterfunc)imap_getiter, /* tp_iter */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
813 (iternextfunc)imap_next, /* tp_iternext */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
814 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
815 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
816 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
817 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
818 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
819 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
820 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
821 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
822 0, /* tp_init */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
823 PyType_GenericAlloc, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
824 imap_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
825 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
826 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
827
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
828
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
829 /* times object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
830
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
831 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
832 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
833 PyObject *obj;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
834 long cnt;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
835 } timesobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
836
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
837 PyTypeObject times_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
838
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
839 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
840 times_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
841 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
842 timesobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
843 PyObject *obj = Py_None;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
844 long cnt;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
845
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
846 if (!PyArg_ParseTuple(args, "l|O:times", &cnt, &obj))
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
847 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
848
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
849 if (cnt < 0) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
850 PyErr_SetString(PyExc_ValueError,
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
851 "count for times() cannot be negative.");
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
852 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
853 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
854
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
855 /* create timesobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
856 lz = (timesobject *)type->tp_alloc(type, 0);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
857 if (lz == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
858 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
859 lz->cnt = cnt;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
860 Py_INCREF(obj);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
861 lz->obj = obj;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
862
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
863 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
864 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
865
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
866 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
867 times_dealloc(timesobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
868 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
869 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
870 Py_XDECREF(lz->obj);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
871 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
872 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
873
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
874 static int
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
875 times_traverse(timesobject *lz, visitproc visit, void *arg)
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
876 {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
877 if (lz->obj)
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
878 return visit(lz->obj, arg);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
879 return 0;
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
880 }
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
881
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
882 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
883 times_next(timesobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
884 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
885 PyObject *obj = lz->obj;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
886
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
887 if (lz->cnt > 0) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
888 lz->cnt--;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
889 Py_INCREF(obj);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
890 return obj;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
891 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
892 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
893 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
894
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
895 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
896 times_getiter(PyObject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
897 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
898 Py_INCREF(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
899 return lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
900 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
901
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
902 PyDoc_STRVAR(times_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
903 "times(n [,obj]) --> times object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
904 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
905 Return a times object whose .next() method returns n consecutive\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
906 instances of obj (default is None).");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
907
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
908 PyTypeObject times_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
909 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
910 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
911 "itertools.times", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
912 sizeof(timesobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
913 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
914 /* methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
915 (destructor)times_dealloc, /* tp_dealloc */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
916 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
917 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
918 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
919 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
920 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
921 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
922 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
923 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
924 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
925 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
926 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
927 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
928 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
929 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
930 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
931 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
932 times_doc, /* tp_doc */
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
933 (traverseproc)times_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
934 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
935 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
936 0, /* tp_weaklistoffset */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
937 (getiterfunc)times_getiter, /* tp_iter */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
938 (iternextfunc)times_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
939 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
940 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
941 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
942 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
943 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
944 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
945 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
946 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
947 0, /* tp_init */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
948 PyType_GenericAlloc, /* tp_alloc */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
949 times_new, /* tp_new */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
950 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
951 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
952
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
953
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
954 /* ifilter object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
955
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
956 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
957 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
958 PyObject *func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
959 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
960 } ifilterobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
961
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
962 PyTypeObject ifilter_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
963
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
964 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
965 ifilter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
966 {
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
967 PyObject *func, *seq;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
968 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
969 ifilterobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
970
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
971 if (!PyArg_UnpackTuple(args, "ifilter", 2, 2, &func, &seq))
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
972 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
973
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
974 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
975 it = PyObject_GetIter(seq);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
976 if (it == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
977 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
978
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
979 /* create ifilterobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
980 lz = (ifilterobject *)type->tp_alloc(type, 0);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
981 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
982 Py_DECREF(it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
983 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
984 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
985 Py_INCREF(func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
986 lz->func = func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
987 lz->it = it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
988
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
989 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
990 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
991
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
992 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
993 ifilter_dealloc(ifilterobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
994 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
995 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
996 Py_XDECREF(lz->func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
997 Py_XDECREF(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
998 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
999 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1000
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1001 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1002 ifilter_traverse(ifilterobject *lz, visitproc visit, void *arg)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1003 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1004 int err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1005
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1006 if (lz->it) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1007 err = visit(lz->it, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1008 if (err)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1009 return err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1010 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1011 if (lz->func) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1012 err = visit(lz->func, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1013 if (err)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1014 return err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1015 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1016 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1017 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1018
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1019 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1020 ifilter_next(ifilterobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1021 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1022 PyObject *item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1023 long ok;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1024
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1025 for (;;) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1026 item = PyIter_Next(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1027 if (item == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1028 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1029
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1030 if (lz->func == Py_None) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1031 ok = PyObject_IsTrue(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1032 } else {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1033 PyObject *good;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1034 good = PyObject_CallFunctionObjArgs(lz->func,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1035 item, NULL);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1036 if (good == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1037 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1038 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1039 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1040 ok = PyObject_IsTrue(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1041 Py_DECREF(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1042 }
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1043 if (ok)
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1044 return item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1045 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1046 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1047 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1048
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1049 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1050 ifilter_getiter(PyObject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1051 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1052 Py_INCREF(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1053 return lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1054 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1055
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1056 PyDoc_STRVAR(ifilter_doc,
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1057 "ifilter(function or None, sequence) --> ifilter object\n\
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1058 \n\
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1059 Return those items of sequence for which function(item) is true.\n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1060 If function is None, return the items that are true.");
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1061
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1062 PyTypeObject ifilter_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1063 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1064 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1065 "itertools.ifilter", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1066 sizeof(ifilterobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1067 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1068 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1069 (destructor)ifilter_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1070 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1071 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1072 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1073 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1074 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1075 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1076 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1077 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1078 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1079 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1080 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1081 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1082 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1083 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1084 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1085 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1086 ifilter_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1087 (traverseproc)ifilter_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1088 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1089 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1090 0, /* tp_weaklistoffset */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1091 (getiterfunc)ifilter_getiter, /* tp_iter */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1092 (iternextfunc)ifilter_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1093 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1094 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1095 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1096 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1097 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1098 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1099 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1100 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1101 0, /* tp_init */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1102 PyType_GenericAlloc, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1103 ifilter_new, /* tp_new */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1104 PyObject_GC_Del, /* tp_free */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1105 };
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1106
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1107
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1108 /* ifilterfalse object ************************************************************/
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1109
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1110 typedef struct {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1111 PyObject_HEAD
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1112 PyObject *func;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1113 PyObject *it;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1114 } ifilterfalseobject;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1115
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1116 PyTypeObject ifilterfalse_type;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1117
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1118 static PyObject *
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1119 ifilterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1120 {
27770
2c389a0448a4 Remove unused variable.
Guido van Rossum <guido@python.org>
parents: 27769
diff changeset
1121 PyObject *func, *seq;
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1122 PyObject *it;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1123 ifilterfalseobject *lz;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1124
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1125 if (!PyArg_UnpackTuple(args, "ifilterfalse", 2, 2, &func, &seq))
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1126 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1127
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1128 /* Get iterator. */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1129 it = PyObject_GetIter(seq);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1130 if (it == NULL)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1131 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1132
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1133 /* create ifilterfalseobject structure */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1134 lz = (ifilterfalseobject *)type->tp_alloc(type, 0);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1135 if (lz == NULL) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1136 Py_DECREF(it);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1137 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1138 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1139 Py_INCREF(func);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1140 lz->func = func;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1141 lz->it = it;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1142
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1143 return (PyObject *)lz;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1144 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1145
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1146 static void
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1147 ifilterfalse_dealloc(ifilterfalseobject *lz)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1148 {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1149 PyObject_GC_UnTrack(lz);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1150 Py_XDECREF(lz->func);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1151 Py_XDECREF(lz->it);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1152 lz->ob_type->tp_free(lz);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1153 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1154
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1155 static int
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1156 ifilterfalse_traverse(ifilterfalseobject *lz, visitproc visit, void *arg)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1157 {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1158 int err;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1159
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1160 if (lz->it) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1161 err = visit(lz->it, arg);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1162 if (err)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1163 return err;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1164 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1165 if (lz->func) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1166 err = visit(lz->func, arg);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1167 if (err)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1168 return err;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1169 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1170 return 0;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1171 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1172
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1173 static PyObject *
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1174 ifilterfalse_next(ifilterfalseobject *lz)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1175 {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1176 PyObject *item;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1177 long ok;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1178
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1179 for (;;) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1180 item = PyIter_Next(lz->it);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1181 if (item == NULL)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1182 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1183
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1184 if (lz->func == Py_None) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1185 ok = PyObject_IsTrue(item);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1186 } else {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1187 PyObject *good;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1188 good = PyObject_CallFunctionObjArgs(lz->func,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1189 item, NULL);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1190 if (good == NULL) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1191 Py_DECREF(item);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1192 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1193 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1194 ok = PyObject_IsTrue(good);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1195 Py_DECREF(good);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1196 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1197 if (!ok)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1198 return item;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1199 Py_DECREF(item);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1200 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1201 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1202
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1203 static PyObject *
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1204 ifilterfalse_getiter(PyObject *lz)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1205 {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1206 Py_INCREF(lz);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1207 return lz;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1208 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1209
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1210 PyDoc_STRVAR(ifilterfalse_doc,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1211 "ifilterfalse(function or None, sequence) --> ifilterfalse object\n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1212 \n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1213 Return those items of sequence for which function(item) is false.\n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1214 If function is None, return the items that are false.");
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1215
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1216 PyTypeObject ifilterfalse_type = {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1217 PyObject_HEAD_INIT(NULL)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1218 0, /* ob_size */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1219 "itertools.ifilterfalse", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1220 sizeof(ifilterfalseobject), /* tp_basicsize */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1221 0, /* tp_itemsize */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1222 /* methods */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1223 (destructor)ifilterfalse_dealloc, /* tp_dealloc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1224 0, /* tp_print */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1225 0, /* tp_getattr */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1226 0, /* tp_setattr */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1227 0, /* tp_compare */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1228 0, /* tp_repr */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1229 0, /* tp_as_number */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1230 0, /* tp_as_sequence */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1231 0, /* tp_as_mapping */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1232 0, /* tp_hash */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1233 0, /* tp_call */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1234 0, /* tp_str */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1235 PyObject_GenericGetAttr, /* tp_getattro */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1236 0, /* tp_setattro */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1237 0, /* tp_as_buffer */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1238 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1239 Py_TPFLAGS_BASETYPE, /* tp_flags */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1240 ifilterfalse_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1241 (traverseproc)ifilterfalse_traverse, /* tp_traverse */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1242 0, /* tp_clear */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1243 0, /* tp_richcompare */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1244 0, /* tp_weaklistoffset */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1245 (getiterfunc)ifilterfalse_getiter, /* tp_iter */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1246 (iternextfunc)ifilterfalse_next, /* tp_iternext */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1247 0, /* tp_methods */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1248 0, /* tp_members */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1249 0, /* tp_getset */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1250 0, /* tp_base */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1251 0, /* tp_dict */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1252 0, /* tp_descr_get */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1253 0, /* tp_descr_set */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1254 0, /* tp_dictoffset */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1255 0, /* tp_init */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1256 PyType_GenericAlloc, /* tp_alloc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1257 ifilterfalse_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1258 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1259 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1260
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1261
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1262 /* count object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1263
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1264 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1265 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1266 long cnt;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1267 } countobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1268
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1269 PyTypeObject count_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1270
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1271 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1272 count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1273 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1274 countobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1275 long cnt = 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1276
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1277 if (!PyArg_ParseTuple(args, "|l:count", &cnt))
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1278 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1279
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1280 /* create countobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1281 lz = (countobject *)PyObject_New(countobject, &count_type);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1282 if (lz == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1283 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1284 lz->cnt = cnt;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1285
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1286 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1287 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1288
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1289 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1290 count_next(countobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1291 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1292 return PyInt_FromLong(lz->cnt++);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1293 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1294
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1295 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1296 count_getiter(PyObject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1297 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1298 Py_INCREF(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1299 return lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1300 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1301
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1302 PyDoc_STRVAR(count_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1303 "count([firstval]) --> count object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1304 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1305 Return a count object whose .next() method returns consecutive\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1306 integers starting from zero or, if specified, from firstval.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1307
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1308 PyTypeObject count_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1309 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1310 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1311 "itertools.count", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1312 sizeof(countobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1313 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1314 /* methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1315 (destructor)PyObject_Del, /* tp_dealloc */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1316 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1317 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1318 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1319 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1320 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1321 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1322 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1323 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1324 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1325 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1326 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1327 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1328 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1329 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1330 Py_TPFLAGS_DEFAULT, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1331 count_doc, /* tp_doc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1332 0, /* tp_traverse */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1333 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1334 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1335 0, /* tp_weaklistoffset */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1336 (getiterfunc)count_getiter, /* tp_iter */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1337 (iternextfunc)count_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1338 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1339 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1340 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1341 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1342 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1343 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1344 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1345 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1346 0, /* tp_init */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1347 PyType_GenericAlloc, /* tp_alloc */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1348 count_new, /* tp_new */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1349 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1350
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1351
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1352 /* izip object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1353
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1354 #include "Python.h"
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1355
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1356 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1357 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1358 long tuplesize;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1359 PyObject *ittuple; /* tuple of iterators */
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1360 PyObject *result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1361 } izipobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1362
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1363 PyTypeObject izip_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1364
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1365 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1366 izip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1367 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1368 izipobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1369 int i;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1370 PyObject *ittuple; /* tuple of iterators */
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1371 PyObject *result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1372 int tuplesize = PySequence_Length(args);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1373
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1374 if (tuplesize < 1) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1375 PyErr_SetString(PyExc_TypeError,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1376 "izip() requires at least one sequence");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1377 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1378 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1379
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1380 /* args must be a tuple */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1381 assert(PyTuple_Check(args));
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1382
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1383 /* obtain iterators */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1384 ittuple = PyTuple_New(tuplesize);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1385 if(ittuple == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1386 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1387 for (i=0; i < tuplesize; ++i) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1388 PyObject *item = PyTuple_GET_ITEM(args, i);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1389 PyObject *it = PyObject_GetIter(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1390 if (it == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1391 if (PyErr_ExceptionMatches(PyExc_TypeError))
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1392 PyErr_Format(PyExc_TypeError,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1393 "izip argument #%d must support iteration",
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1394 i+1);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1395 Py_DECREF(ittuple);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1396 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1397 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1398 PyTuple_SET_ITEM(ittuple, i, it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1399 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1400
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1401 /* create a result holder */
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1402 result = PyTuple_New(tuplesize);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1403 if (result == NULL) {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1404 Py_DECREF(ittuple);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1405 return NULL;
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1406 }
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1407 for (i=0 ; i < tuplesize ; i++) {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1408 Py_INCREF(Py_None);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1409 PyTuple_SET_ITEM(result, i, Py_None);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1410 }
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1411
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1412 /* create izipobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1413 lz = (izipobject *)type->tp_alloc(type, 0);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1414 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1415 Py_DECREF(ittuple);
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1416 Py_DECREF(result);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1417 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1418 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1419 lz->ittuple = ittuple;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1420 lz->tuplesize = tuplesize;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1421 lz->result = result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1422
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1423 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1424 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1425
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1426 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1427 izip_dealloc(izipobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1428 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1429 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1430 Py_XDECREF(lz->ittuple);
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1431 Py_XDECREF(lz->result);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1432 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1433 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1434
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1435 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1436 izip_traverse(izipobject *lz, visitproc visit, void *arg)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1437 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1438 if (lz->ittuple)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1439 return visit(lz->ittuple, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1440 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1441 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1442
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1443 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1444 izip_next(izipobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1445 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1446 int i;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1447 long tuplesize = lz->tuplesize;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1448 PyObject *result = lz->result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1449 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1450 PyObject *item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1451
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1452 if (result->ob_refcnt == 1) {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1453 for (i=0 ; i < tuplesize ; i++) {
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1454 it = PyTuple_GET_ITEM(lz->ittuple, i);
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1455 item = PyIter_Next(it);
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1456 if (item == NULL)
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1457 return NULL;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1458 Py_DECREF(PyTuple_GET_ITEM(result, i));
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1459 PyTuple_SET_ITEM(result, i, item);
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1460 }
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1461 Py_INCREF(result);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1462 } else {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1463 result = PyTuple_New(tuplesize);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1464 if (result == NULL)
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1465 return NULL;
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1466 for (i=0 ; i < tuplesize ; i++) {
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1467 it = PyTuple_GET_ITEM(lz->ittuple, i);
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1468 item = PyIter_Next(it);
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1469 if (item == NULL) {
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1470 Py_DECREF(result);
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1471 return NULL;
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1472 }
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1473 PyTuple_SET_ITEM(result, i, item);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1474 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1475 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1476 return result;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1477 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1478
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1479 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1480 izip_getiter(PyObject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1481 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1482 Py_INCREF(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1483 return lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1484 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1485
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1486 PyDoc_STRVAR(izip_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1487 "izip(iter1 [,iter2 [...]]) --> izip object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1488 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1489 Return a izip object whose .next() method returns a tuple where\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1490 the i-th element comes from the i-th iterable argument. The .next()\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1491 method continues until the shortest iterable in the argument sequence\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1492 is exhausted and then it raises StopIteration. Works like the zip()\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1493 function but consumes less memory by returning an iterator instead of\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1494 a list.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1495
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1496 PyTypeObject izip_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1497 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1498 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1499 "itertools.izip", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1500 sizeof(izipobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1501 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1502 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1503 (destructor)izip_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1504 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1505 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1506 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1507 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1508 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1509 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1510 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1511 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1512 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1513 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1514 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1515 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1516 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1517 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1518 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1519 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1520 izip_doc, /* tp_doc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1521 (traverseproc)izip_traverse, /* tp_traverse */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1522 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1523 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1524 0, /* tp_weaklistoffset */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1525 (getiterfunc)izip_getiter, /* tp_iter */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1526 (iternextfunc)izip_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1527 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1528 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1529 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1530 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1531 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1532 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1533 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1534 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1535 0, /* tp_init */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1536 PyType_GenericAlloc, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1537 izip_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1538 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1539 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1540
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1541
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1542 /* repeat object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1543
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1544 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1545 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1546 PyObject *element;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1547 } repeatobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1548
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1549 PyTypeObject repeat_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1550
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1551 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1552 repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1553 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1554 repeatobject *ro;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1555 PyObject *element;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1556
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1557 if (!PyArg_UnpackTuple(args, "repeat", 1, 1, &element))
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1558 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1559
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1560 ro = (repeatobject *)type->tp_alloc(type, 0);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1561 if (ro == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1562 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1563 Py_INCREF(element);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1564 ro->element = element;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1565 return (PyObject *)ro;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1566 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1567
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1568 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1569 repeat_dealloc(repeatobject *ro)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1570 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1571 PyObject_GC_UnTrack(ro);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1572 Py_XDECREF(ro->element);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1573 ro->ob_type->tp_free(ro);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1574 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1575
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1576 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1577 repeat_traverse(repeatobject *ro, visitproc visit, void *arg)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1578 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1579 if (ro->element)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1580 return visit(ro->element, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1581 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1582 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1583
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1584 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1585 repeat_next(repeatobject *ro)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1586 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1587 Py_INCREF(ro->element);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1588 return ro->element;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1589 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1590
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1591 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1592 repeat_getiter(PyObject *ro)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1593 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1594 Py_INCREF(ro);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1595 return ro;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1596 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1597
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1598 PyDoc_STRVAR(repeat_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1599 "repeat(element) -> create an iterator which returns the element endlessly.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1600
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1601 PyTypeObject repeat_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1602 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1603 0, /* ob_size */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1604 "itertools.repeat", /* tp_name */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1605 sizeof(repeatobject), /* tp_basicsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1606 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1607 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1608 (destructor)repeat_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1609 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1610 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1611 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1612 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1613 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1614 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1615 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1616 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1617 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1618 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1619 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1620 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1621 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1622 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1623 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1624 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1625 repeat_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1626 (traverseproc)repeat_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1627 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1628 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1629 0, /* tp_weaklistoffset */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1630 (getiterfunc)repeat_getiter, /* tp_iter */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1631 (iternextfunc)repeat_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1632 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1633 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1634 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1635 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1636 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1637 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1638 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1639 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1640 0, /* tp_init */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1641 PyType_GenericAlloc, /* tp_alloc */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1642 repeat_new, /* tp_new */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1643 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1644 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1645
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1646
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1647 /* module level code ********************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1648
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1649 PyDoc_STRVAR(module_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1650 "Functional tools for creating and using iterators.\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1651 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1652 Infinite iterators:\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1653 count([n]) --> n, n+1, n+2, ...\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1654 repeat(elem) --> elem, elem, elem, ...\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1655 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1656 Iterators terminating on the shortest input sequence:\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1657 izip(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1658 ifilter(pred, seq) --> elements of seq where pred(elem) is True\n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1659 ifilterfalse(pred, seq) --> elements of seq where pred(elem) is False\n\
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1660 islice(seq, [start,] stop [, step]) --> elements from\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1661 seq[start:stop:step]\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1662 imap(fun, p, q, ...) --> fun(p0, q0), fun(p1, q1), ...\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1663 starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1664 times(n, [obj]) --> obj, obj, ... for n times. obj defaults to None\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1665 takewhile(pred, seq) --> seq[0], seq[1], until pred fails\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1666 dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred fails\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1667 ");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1668
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1669
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1670 PyMODINIT_FUNC
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1671 inititertools(void)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1672 {
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1673 int i;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1674 PyObject *m;
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1675 char *name;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1676 PyTypeObject *typelist[] = {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1677 &dropwhile_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1678 &takewhile_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1679 &islice_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1680 &starmap_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1681 &imap_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1682 &times_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1683 &ifilter_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1684 &ifilterfalse_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1685 &count_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1686 &izip_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1687 &repeat_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1688 NULL
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1689 };
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1690
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1691 m = Py_InitModule3("itertools", NULL, module_doc);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1692
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1693 for (i=0 ; typelist[i] != NULL ; i++) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1694 if (PyType_Ready(typelist[i]) < 0)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1695 return;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1696 name = strchr(typelist[i]->tp_name, '.') + 1;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1697 if (name == NULL)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1698 return;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1699 Py_INCREF(typelist[i]);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1700 PyModule_AddObject(m, name, (PyObject *)typelist[i]);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1701 }
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1702 }