annotate Modules/itertoolsmodule.c @ 33526:175eee09cb98 legacy-trunk

Raymond reminded me to use DSU key
author Skip Montanaro <skip@pobox.com>
date Mon, 20 Sep 2004 16:43:30 +0000
parents c2c49ee727d0
children 31b08670ebdd
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
31136
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
10
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
11 /* groupby object ***********************************************************/
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
12
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
13 typedef struct {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
14 PyObject_HEAD
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
15 PyObject *it;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
16 PyObject *keyfunc;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
17 PyObject *tgtkey;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
18 PyObject *currkey;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
19 PyObject *currvalue;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
20 } groupbyobject;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
21
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
22 static PyTypeObject groupby_type;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
23 static PyObject *_grouper_create(groupbyobject *, PyObject *);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
24
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
25 static PyObject *
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
26 groupby_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
27 {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
28 static char *kwargs[] = {"iterable", "key", NULL};
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
29 groupbyobject *gbo;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
30 PyObject *it, *keyfunc = Py_None;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
31
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
32 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:groupby", kwargs,
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
33 &it, &keyfunc))
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
34 return NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
35
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
36 gbo = (groupbyobject *)type->tp_alloc(type, 0);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
37 if (gbo == NULL)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
38 return NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
39 gbo->tgtkey = NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
40 gbo->currkey = NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
41 gbo->currvalue = NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
42 gbo->keyfunc = keyfunc;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
43 Py_INCREF(keyfunc);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
44 gbo->it = PyObject_GetIter(it);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
45 if (gbo->it == NULL) {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
46 Py_DECREF(gbo);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
47 return NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
48 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
49 return (PyObject *)gbo;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
50 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
51
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
52 static void
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
53 groupby_dealloc(groupbyobject *gbo)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
54 {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
55 PyObject_GC_UnTrack(gbo);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
56 Py_XDECREF(gbo->it);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
57 Py_XDECREF(gbo->keyfunc);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
58 Py_XDECREF(gbo->tgtkey);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
59 Py_XDECREF(gbo->currkey);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
60 Py_XDECREF(gbo->currvalue);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
61 gbo->ob_type->tp_free(gbo);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
62 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
63
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
64 static int
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
65 groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
66 {
32682
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
67 Py_VISIT(gbo->it);
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
68 Py_VISIT(gbo->keyfunc);
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
69 Py_VISIT(gbo->tgtkey);
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
70 Py_VISIT(gbo->currkey);
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
71 Py_VISIT(gbo->currvalue);
31136
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
72 return 0;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
73 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
74
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
75 static PyObject *
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
76 groupby_next(groupbyobject *gbo)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
77 {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
78 PyObject *newvalue, *newkey, *r, *grouper;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
79
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
80 /* skip to next iteration group */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
81 for (;;) {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
82 if (gbo->currkey == NULL)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
83 /* pass */;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
84 else if (gbo->tgtkey == NULL)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
85 break;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
86 else {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
87 int rcmp;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
88
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
89 rcmp = PyObject_RichCompareBool(gbo->tgtkey,
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
90 gbo->currkey, Py_EQ);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
91 if (rcmp == -1)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
92 return NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
93 else if (rcmp == 0)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
94 break;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
95 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
96
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
97 newvalue = PyIter_Next(gbo->it);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
98 if (newvalue == NULL)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
99 return NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
100
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
101 if (gbo->keyfunc == Py_None) {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
102 newkey = newvalue;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
103 Py_INCREF(newvalue);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
104 } else {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
105 newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc,
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
106 newvalue, NULL);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
107 if (newkey == NULL) {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
108 Py_DECREF(newvalue);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
109 return NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
110 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
111 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
112
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
113 Py_XDECREF(gbo->currkey);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
114 gbo->currkey = newkey;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
115 Py_XDECREF(gbo->currvalue);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
116 gbo->currvalue = newvalue;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
117 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
118
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
119 Py_XDECREF(gbo->tgtkey);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
120 gbo->tgtkey = gbo->currkey;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
121 Py_INCREF(gbo->currkey);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
122
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
123 grouper = _grouper_create(gbo, gbo->tgtkey);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
124 if (grouper == NULL)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
125 return NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
126
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
127 r = PyTuple_Pack(2, gbo->currkey, grouper);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
128 Py_DECREF(grouper);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
129 return r;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
130 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
131
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
132 PyDoc_STRVAR(groupby_doc,
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
133 "groupby(iterable[, keyfunc]) -> create an iterator which returns\n\
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
134 (key, sub-iterator) grouped by each value of key(value).\n");
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
135
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
136 static PyTypeObject groupby_type = {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
137 PyObject_HEAD_INIT(NULL)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
138 0, /* ob_size */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
139 "itertools.groupby", /* tp_name */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
140 sizeof(groupbyobject), /* tp_basicsize */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
141 0, /* tp_itemsize */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
142 /* methods */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
143 (destructor)groupby_dealloc, /* tp_dealloc */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
144 0, /* tp_print */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
145 0, /* tp_getattr */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
146 0, /* tp_setattr */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
147 0, /* tp_compare */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
148 0, /* tp_repr */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
149 0, /* tp_as_number */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
150 0, /* tp_as_sequence */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
151 0, /* tp_as_mapping */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
152 0, /* tp_hash */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
153 0, /* tp_call */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
154 0, /* tp_str */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
155 PyObject_GenericGetAttr, /* tp_getattro */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
156 0, /* tp_setattro */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
157 0, /* tp_as_buffer */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
158 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
159 Py_TPFLAGS_BASETYPE, /* tp_flags */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
160 groupby_doc, /* tp_doc */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
161 (traverseproc)groupby_traverse, /* tp_traverse */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
162 0, /* tp_clear */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
163 0, /* tp_richcompare */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
164 0, /* tp_weaklistoffset */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
165 PyObject_SelfIter, /* tp_iter */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
166 (iternextfunc)groupby_next, /* tp_iternext */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
167 0, /* tp_methods */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
168 0, /* tp_members */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
169 0, /* tp_getset */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
170 0, /* tp_base */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
171 0, /* tp_dict */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
172 0, /* tp_descr_get */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
173 0, /* tp_descr_set */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
174 0, /* tp_dictoffset */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
175 0, /* tp_init */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
176 0, /* tp_alloc */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
177 groupby_new, /* tp_new */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
178 PyObject_GC_Del, /* tp_free */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
179 };
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
180
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
181
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
182 /* _grouper object (internal) ************************************************/
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
183
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
184 typedef struct {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
185 PyObject_HEAD
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
186 PyObject *parent;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
187 PyObject *tgtkey;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
188 } _grouperobject;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
189
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
190 static PyTypeObject _grouper_type;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
191
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
192 static PyObject *
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
193 _grouper_create(groupbyobject *parent, PyObject *tgtkey)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
194 {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
195 _grouperobject *igo;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
196
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
197 igo = PyObject_New(_grouperobject, &_grouper_type);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
198 if (igo == NULL)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
199 return NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
200 igo->parent = (PyObject *)parent;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
201 Py_INCREF(parent);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
202 igo->tgtkey = tgtkey;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
203 Py_INCREF(tgtkey);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
204
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
205 return (PyObject *)igo;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
206 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
207
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
208 static void
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
209 _grouper_dealloc(_grouperobject *igo)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
210 {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
211 Py_DECREF(igo->parent);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
212 Py_DECREF(igo->tgtkey);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
213 PyObject_Del(igo);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
214 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
215
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
216 static PyObject *
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
217 _grouper_next(_grouperobject *igo)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
218 {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
219 groupbyobject *gbo = (groupbyobject *)igo->parent;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
220 PyObject *newvalue, *newkey, *r;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
221 int rcmp;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
222
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
223 if (gbo->currvalue == NULL) {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
224 newvalue = PyIter_Next(gbo->it);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
225 if (newvalue == NULL)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
226 return NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
227
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
228 if (gbo->keyfunc == Py_None) {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
229 newkey = newvalue;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
230 Py_INCREF(newvalue);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
231 } else {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
232 newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc,
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
233 newvalue, NULL);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
234 if (newkey == NULL) {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
235 Py_DECREF(newvalue);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
236 return NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
237 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
238 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
239
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
240 assert(gbo->currkey == NULL);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
241 gbo->currkey = newkey;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
242 gbo->currvalue = newvalue;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
243 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
244
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
245 assert(gbo->currkey != NULL);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
246 rcmp = PyObject_RichCompareBool(igo->tgtkey, gbo->currkey, Py_EQ);
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
247 if (rcmp <= 0)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
248 /* got any error or current group is end */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
249 return NULL;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
250
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
251 r = gbo->currvalue;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
252 gbo->currvalue = NULL;
33386
c2c49ee727d0 SF patch #1020188: Use Py_CLEAR where necessary to avoid crashes
Raymond Hettinger <python@rcn.com>
parents: 32682
diff changeset
253 Py_CLEAR(gbo->currkey);
31136
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
254
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
255 return r;
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
256 }
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
257
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
258 static PyTypeObject _grouper_type = {
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
259 PyObject_HEAD_INIT(NULL)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
260 0, /* ob_size */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
261 "itertools._grouper", /* tp_name */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
262 sizeof(_grouperobject), /* tp_basicsize */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
263 0, /* tp_itemsize */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
264 /* methods */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
265 (destructor)_grouper_dealloc, /* tp_dealloc */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
266 0, /* tp_print */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
267 0, /* tp_getattr */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
268 0, /* tp_setattr */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
269 0, /* tp_compare */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
270 0, /* tp_repr */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
271 0, /* tp_as_number */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
272 0, /* tp_as_sequence */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
273 0, /* tp_as_mapping */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
274 0, /* tp_hash */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
275 0, /* tp_call */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
276 0, /* tp_str */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
277 PyObject_GenericGetAttr, /* tp_getattro */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
278 0, /* tp_setattro */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
279 0, /* tp_as_buffer */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
280 Py_TPFLAGS_DEFAULT, /* tp_flags */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
281 0, /* tp_doc */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
282 0, /* tp_traverse */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
283 0, /* tp_clear */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
284 0, /* tp_richcompare */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
285 0, /* tp_weaklistoffset */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
286 PyObject_SelfIter, /* tp_iter */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
287 (iternextfunc)_grouper_next, /* tp_iternext */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
288 0, /* tp_methods */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
289 0, /* tp_members */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
290 0, /* tp_getset */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
291 0, /* tp_base */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
292 0, /* tp_dict */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
293 0, /* tp_descr_get */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
294 0, /* tp_descr_set */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
295 0, /* tp_dictoffset */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
296 0, /* tp_init */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
297 0, /* tp_alloc */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
298 0, /* tp_new */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
299 PyObject_Del, /* tp_free */
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
300 };
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
301
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
302
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
303
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
304 /* tee object and with supporting function and objects ***************/
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
305
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
306 /* The teedataobject pre-allocates space for LINKCELLS number of objects.
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
307 To help the object fit neatly inside cache lines (space for 16 to 32
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
308 pointers), the value should be a multiple of 16 minus space for
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
309 the other structure members including PyHEAD overhead. The larger the
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
310 value, the less memory overhead per object and the less time spent
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
311 allocating/deallocating new links. The smaller the number, the less
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
312 wasted space and the more rapid freeing of older data.
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
313 */
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
314 #define LINKCELLS 57
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
315
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
316 typedef struct {
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
317 PyObject_HEAD
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
318 PyObject *it;
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
319 int numread;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
320 PyObject *nextlink;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
321 PyObject *(values[LINKCELLS]);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
322 } teedataobject;
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
323
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
324 typedef struct {
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
325 PyObject_HEAD
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
326 teedataobject *dataobj;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
327 int index;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
328 } teeobject;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
329
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
330 static PyTypeObject teedataobject_type;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
331
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
332 static PyObject *
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
333 teedataobject_new(PyObject *it)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
334 {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
335 teedataobject *tdo;
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
336
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
337 tdo = PyObject_New(teedataobject, &teedataobject_type);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
338 if (tdo == NULL)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
339 return NULL;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
340
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
341 tdo->numread = 0;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
342 tdo->nextlink = NULL;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
343 Py_INCREF(it);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
344 tdo->it = it;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
345 return (PyObject *)tdo;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
346 }
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
347
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
348 static PyObject *
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
349 teedataobject_jumplink(teedataobject *tdo)
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
350 {
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
351 if (tdo->nextlink == NULL)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
352 tdo->nextlink = teedataobject_new(tdo->it);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
353 Py_INCREF(tdo->nextlink);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
354 return tdo->nextlink;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
355 }
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
356
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
357 static PyObject *
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
358 teedataobject_getitem(teedataobject *tdo, int i)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
359 {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
360 PyObject *value;
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
361
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
362 assert(i < LINKCELLS);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
363 if (i < tdo->numread)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
364 value = tdo->values[i];
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
365 else {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
366 /* this is the lead iterator, so fetch more data */
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
367 assert(i == tdo->numread);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
368 value = PyIter_Next(tdo->it);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
369 if (value == NULL)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
370 return NULL;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
371 tdo->numread++;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
372 tdo->values[i] = value;
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
373 }
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
374 Py_INCREF(value);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
375 return value;
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
376 }
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
377
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
378 static void
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
379 teedataobject_dealloc(teedataobject *tdo)
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
380 {
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
381 int i;
30773
95fd55545cac Improvements to coding for itertools.tee():
Raymond Hettinger <python@rcn.com>
parents: 30764
diff changeset
382
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
383 for (i=0 ; i<tdo->numread ; i++)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
384 Py_DECREF(tdo->values[i]);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
385 Py_XDECREF(tdo->it);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
386 Py_XDECREF(tdo->nextlink);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
387 PyObject_Del(tdo);
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
388 }
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
389
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
390 PyDoc_STRVAR(teedataobject_doc, "Data container common to multiple tee objects.");
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
391
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
392 static PyTypeObject teedataobject_type = {
31508
71864820402d need to initialize ob_type slot at run-time, at least on cygwin
Skip Montanaro <skip@pobox.com>
parents: 31501
diff changeset
393 PyObject_HEAD_INIT(0) /* Must fill in type value later */
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
394 0, /* ob_size */
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
395 "itertools.tee_dataobject", /* tp_name */
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
396 sizeof(teedataobject), /* tp_basicsize */
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
397 0, /* tp_itemsize */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
398 /* methods */
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
399 (destructor)teedataobject_dealloc, /* tp_dealloc */
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
400 0, /* tp_print */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
401 0, /* tp_getattr */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
402 0, /* tp_setattr */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
403 0, /* tp_compare */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
404 0, /* tp_repr */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
405 0, /* tp_as_number */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
406 0, /* tp_as_sequence */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
407 0, /* tp_as_mapping */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
408 0, /* tp_hash */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
409 0, /* tp_call */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
410 0, /* tp_str */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
411 PyObject_GenericGetAttr, /* tp_getattro */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
412 0, /* tp_setattro */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
413 0, /* tp_as_buffer */
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
414 Py_TPFLAGS_DEFAULT, /* tp_flags */
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
415 teedataobject_doc, /* tp_doc */
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
416 0, /* tp_traverse */
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
417 };
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
418
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
419
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
420 static PyTypeObject tee_type;
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
421
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
422 static PyObject *
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
423 tee_next(teeobject *to)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
424 {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
425 PyObject *value, *link;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
426
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
427 if (to->index >= LINKCELLS) {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
428 link = teedataobject_jumplink(to->dataobj);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
429 Py_XDECREF(to->dataobj);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
430 to->dataobj = (teedataobject *)link;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
431 to->index = 0;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
432 }
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
433 value = teedataobject_getitem(to->dataobj, to->index);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
434 if (value == NULL)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
435 return NULL;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
436 to->index++;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
437 return value;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
438 }
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
439
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
440 static PyObject *
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
441 tee_copy(teeobject *to)
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
442 {
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
443 teeobject *newto;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
444
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
445 newto = PyObject_New(teeobject, &tee_type);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
446 if (newto == NULL)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
447 return NULL;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
448 Py_INCREF(to->dataobj);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
449 newto->dataobj = to->dataobj;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
450 newto->index = to->index;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
451 return (PyObject *)newto;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
452 }
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
453
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
454 PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator.");
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
455
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
456 static PyObject *
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
457 tee_fromiterable(PyObject *iterable)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
458 {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
459 teeobject *to;
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
460 PyObject *it = NULL;
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
461
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
462 it = PyObject_GetIter(iterable);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
463 if (it == NULL)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
464 return NULL;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
465 if (PyObject_TypeCheck(it, &tee_type)) {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
466 to = (teeobject *)tee_copy((teeobject *)it);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
467 goto done;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
468 }
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
469
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
470 to = PyObject_New(teeobject, &tee_type);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
471 if (to == NULL)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
472 goto done;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
473 to->dataobj = (teedataobject *)teedataobject_new(it);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
474 to->index = 0;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
475 done:
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
476 Py_XDECREF(it);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
477 return (PyObject *)to;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
478 }
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
479
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
480 static PyObject *
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
481 tee_new(PyTypeObject *type, PyObject *args, PyObject *kw)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
482 {
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
483 PyObject *iterable;
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
484
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
485 if (!PyArg_UnpackTuple(args, "tee", 1, 1, &iterable))
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
486 return NULL;
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
487 return tee_fromiterable(iterable);
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
488 }
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
489
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
490 static void
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
491 tee_dealloc(teeobject *to)
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
492 {
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
493 Py_XDECREF(to->dataobj);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
494 PyObject_Del(to);
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
495 }
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
496
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
497 PyDoc_STRVAR(teeobject_doc,
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
498 "Iterator wrapped to make it copyable");
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
499
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
500 static PyMethodDef tee_methods[] = {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
501 {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc},
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
502 {NULL, NULL} /* sentinel */
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
503 };
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
504
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
505 static PyTypeObject tee_type = {
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
506 PyObject_HEAD_INIT(NULL)
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
507 0, /* ob_size */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
508 "itertools.tee", /* tp_name */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
509 sizeof(teeobject), /* tp_basicsize */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
510 0, /* tp_itemsize */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
511 /* methods */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
512 (destructor)tee_dealloc, /* tp_dealloc */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
513 0, /* tp_print */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
514 0, /* tp_getattr */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
515 0, /* tp_setattr */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
516 0, /* tp_compare */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
517 0, /* tp_repr */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
518 0, /* tp_as_number */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
519 0, /* tp_as_sequence */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
520 0, /* tp_as_mapping */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
521 0, /* tp_hash */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
522 0, /* tp_call */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
523 0, /* tp_str */
30786
225d848a5f96 Minor improvements to itertools.tee():
Raymond Hettinger <python@rcn.com>
parents: 30773
diff changeset
524 0, /* tp_getattro */
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
525 0, /* tp_setattro */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
526 0, /* tp_as_buffer */
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
527 Py_TPFLAGS_DEFAULT, /* tp_flags */
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
528 teeobject_doc, /* tp_doc */
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
529 0, /* tp_traverse */
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
530 0, /* tp_clear */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
531 0, /* tp_richcompare */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
532 0, /* tp_weaklistoffset */
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
533 PyObject_SelfIter, /* tp_iter */
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
534 (iternextfunc)tee_next, /* tp_iternext */
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
535 tee_methods, /* tp_methods */
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
536 0, /* tp_members */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
537 0, /* tp_getset */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
538 0, /* tp_base */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
539 0, /* tp_dict */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
540 0, /* tp_descr_get */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
541 0, /* tp_descr_set */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
542 0, /* tp_dictoffset */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
543 0, /* tp_init */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
544 0, /* tp_alloc */
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
545 tee_new, /* tp_new */
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
546 PyObject_Del, /* tp_free */
30764
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
547 };
b948e775a79e Added itertools.tee()
Raymond Hettinger <python@rcn.com>
parents: 30240
diff changeset
548
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
549 static PyObject *
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
550 tee(PyObject *self, PyObject *args)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
551 {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
552 int i, n=2;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
553 PyObject *it, *iterable, *copyable, *result;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
554
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
555 if (!PyArg_ParseTuple(args, "O|i", &iterable, &n))
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
556 return NULL;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
557 result = PyTuple_New(n);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
558 if (result == NULL)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
559 return NULL;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
560 if (n == 0)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
561 return result;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
562 it = PyObject_GetIter(iterable);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
563 if (it == NULL) {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
564 Py_DECREF(result);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
565 return NULL;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
566 }
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
567 if (!PyObject_HasAttrString(it, "__copy__")) {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
568 copyable = tee_fromiterable(it);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
569 Py_DECREF(it);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
570 if (copyable == NULL) {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
571 Py_DECREF(result);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
572 return NULL;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
573 }
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
574 } else
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
575 copyable = it;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
576 PyTuple_SET_ITEM(result, 0, copyable);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
577 for (i=1 ; i<n ; i++) {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
578 copyable = PyObject_CallMethod(copyable, "__copy__", NULL);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
579 if (copyable == NULL) {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
580 Py_DECREF(result);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
581 return NULL;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
582 }
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
583 PyTuple_SET_ITEM(result, i, copyable);
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
584 }
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
585 return result;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
586 }
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
587
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
588 PyDoc_STRVAR(tee_doc,
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
589 "tee(iterable, n=2) --> tuple of n independent iterators.");
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
590
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
591
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
592 /* cycle object **********************************************************/
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
593
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
594 typedef struct {
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
595 PyObject_HEAD
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
596 PyObject *it;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
597 PyObject *saved;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
598 int firstpass;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
599 } cycleobject;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
600
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
601 static PyTypeObject cycle_type;
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
602
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
603 static PyObject *
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
604 cycle_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
605 {
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
606 PyObject *it;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
607 PyObject *iterable;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
608 PyObject *saved;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
609 cycleobject *lz;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
610
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
611 if (!PyArg_UnpackTuple(args, "cycle", 1, 1, &iterable))
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
612 return NULL;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
613
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
614 /* Get iterator. */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
615 it = PyObject_GetIter(iterable);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
616 if (it == NULL)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
617 return NULL;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
618
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
619 saved = PyList_New(0);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
620 if (saved == NULL) {
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
621 Py_DECREF(it);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
622 return NULL;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
623 }
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
624
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
625 /* create cycleobject structure */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
626 lz = (cycleobject *)type->tp_alloc(type, 0);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
627 if (lz == NULL) {
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
628 Py_DECREF(it);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
629 Py_DECREF(saved);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
630 return NULL;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
631 }
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
632 lz->it = it;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
633 lz->saved = saved;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
634 lz->firstpass = 0;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
635
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
636 return (PyObject *)lz;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
637 }
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
638
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
639 static void
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
640 cycle_dealloc(cycleobject *lz)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
641 {
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
642 PyObject_GC_UnTrack(lz);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
643 Py_XDECREF(lz->saved);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
644 Py_XDECREF(lz->it);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
645 lz->ob_type->tp_free(lz);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
646 }
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
647
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
648 static int
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
649 cycle_traverse(cycleobject *lz, visitproc visit, void *arg)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
650 {
32682
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
651 Py_VISIT(lz->it);
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
652 Py_VISIT(lz->saved);
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
653 return 0;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
654 }
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
655
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
656 static PyObject *
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
657 cycle_next(cycleobject *lz)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
658 {
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
659 PyObject *item;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
660 PyObject *it;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
661
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
662 while (1) {
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
663 item = PyIter_Next(lz->it);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
664 if (item != NULL) {
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
665 if (!lz->firstpass)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
666 PyList_Append(lz->saved, item);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
667 return item;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
668 }
32010
544f2172363d SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents: 31909
diff changeset
669 if (PyErr_Occurred()) {
544f2172363d SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents: 31909
diff changeset
670 if (PyErr_ExceptionMatches(PyExc_StopIteration))
544f2172363d SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents: 31909
diff changeset
671 PyErr_Clear();
544f2172363d SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents: 31909
diff changeset
672 else
544f2172363d SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents: 31909
diff changeset
673 return NULL;
544f2172363d SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents: 31909
diff changeset
674 }
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
675 if (PyList_Size(lz->saved) == 0)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
676 return NULL;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
677 it = PyObject_GetIter(lz->saved);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
678 if (it == NULL)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
679 return NULL;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
680 Py_DECREF(lz->it);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
681 lz->it = it;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
682 lz->firstpass = 1;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
683 }
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
684 }
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
685
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
686 PyDoc_STRVAR(cycle_doc,
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
687 "cycle(iterable) --> cycle object\n\
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
688 \n\
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
689 Return elements from the iterable until it is exhausted.\n\
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
690 Then repeat the sequence indefinitely.");
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
691
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
692 static PyTypeObject cycle_type = {
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
693 PyObject_HEAD_INIT(NULL)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
694 0, /* ob_size */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
695 "itertools.cycle", /* tp_name */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
696 sizeof(cycleobject), /* tp_basicsize */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
697 0, /* tp_itemsize */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
698 /* methods */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
699 (destructor)cycle_dealloc, /* tp_dealloc */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
700 0, /* tp_print */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
701 0, /* tp_getattr */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
702 0, /* tp_setattr */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
703 0, /* tp_compare */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
704 0, /* tp_repr */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
705 0, /* tp_as_number */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
706 0, /* tp_as_sequence */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
707 0, /* tp_as_mapping */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
708 0, /* tp_hash */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
709 0, /* tp_call */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
710 0, /* tp_str */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
711 PyObject_GenericGetAttr, /* tp_getattro */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
712 0, /* tp_setattro */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
713 0, /* tp_as_buffer */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
714 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
715 Py_TPFLAGS_BASETYPE, /* tp_flags */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
716 cycle_doc, /* tp_doc */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
717 (traverseproc)cycle_traverse, /* tp_traverse */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
718 0, /* tp_clear */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
719 0, /* tp_richcompare */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
720 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
721 PyObject_SelfIter, /* tp_iter */
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
722 (iternextfunc)cycle_next, /* tp_iternext */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
723 0, /* tp_methods */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
724 0, /* tp_members */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
725 0, /* tp_getset */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
726 0, /* tp_base */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
727 0, /* tp_dict */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
728 0, /* tp_descr_get */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
729 0, /* tp_descr_set */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
730 0, /* tp_dictoffset */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
731 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
732 0, /* tp_alloc */
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
733 cycle_new, /* tp_new */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
734 PyObject_GC_Del, /* tp_free */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
735 };
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
736
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
737
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
738 /* dropwhile object **********************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
739
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
740 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
741 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
742 PyObject *func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
743 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
744 long start;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
745 } dropwhileobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
746
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
747 static PyTypeObject dropwhile_type;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
748
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
749 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
750 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
751 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
752 PyObject *func, *seq;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
753 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
754 dropwhileobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
755
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
756 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
757 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
758
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
759 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
760 it = PyObject_GetIter(seq);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
761 if (it == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
762 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
763
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
764 /* create dropwhileobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
765 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
766 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
767 Py_DECREF(it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
768 return NULL;
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(func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
771 lz->func = func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
772 lz->it = it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
773 lz->start = 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
774
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
775 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
776 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
777
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
778 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
779 dropwhile_dealloc(dropwhileobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
780 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
781 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
782 Py_XDECREF(lz->func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
783 Py_XDECREF(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
784 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
785 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
786
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
787 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
788 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
789 {
32682
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
790 Py_VISIT(lz->it);
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
791 Py_VISIT(lz->func);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
792 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
793 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
794
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
795 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
796 dropwhile_next(dropwhileobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
797 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
798 PyObject *item, *good;
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
799 PyObject *it = lz->it;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
800 long ok;
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
801 PyObject *(*iternext)(PyObject *);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
802
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
803 assert(PyIter_Check(it));
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
804 iternext = *it->ob_type->tp_iternext;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
805 for (;;) {
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
806 item = iternext(it);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
807 if (item == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
808 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
809 if (lz->start == 1)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
810 return item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
811
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
812 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
813 if (good == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
814 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
815 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
816 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
817 ok = PyObject_IsTrue(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
818 Py_DECREF(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
819 if (!ok) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
820 lz->start = 1;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
821 return item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
822 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
823 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
824 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
825 }
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 PyDoc_STRVAR(dropwhile_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
828 "dropwhile(predicate, iterable) --> dropwhile object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
829 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
830 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
831 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
832
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
833 static PyTypeObject dropwhile_type = {
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
834 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
835 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
836 "itertools.dropwhile", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
837 sizeof(dropwhileobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
838 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
839 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
840 (destructor)dropwhile_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
841 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
842 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
843 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
844 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
845 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
846 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
847 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
848 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
849 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
850 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
851 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
852 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
853 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
854 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
855 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
856 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
857 dropwhile_doc, /* tp_doc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
858 (traverseproc)dropwhile_traverse, /* tp_traverse */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
859 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
860 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
861 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
862 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
863 (iternextfunc)dropwhile_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
864 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
865 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
866 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
867 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
868 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
869 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
870 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
871 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
872 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
873 0, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
874 dropwhile_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
875 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
876 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
877
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
878
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
879 /* takewhile object **********************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
880
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
881 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
882 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
883 PyObject *func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
884 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
885 long stop;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
886 } takewhileobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
887
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
888 static PyTypeObject takewhile_type;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
889
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
890 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
891 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
892 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
893 PyObject *func, *seq;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
894 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
895 takewhileobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
896
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
897 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
898 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
899
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
900 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
901 it = PyObject_GetIter(seq);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
902 if (it == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
903 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
904
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
905 /* create takewhileobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
906 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
907 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
908 Py_DECREF(it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
909 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
910 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
911 Py_INCREF(func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
912 lz->func = func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
913 lz->it = it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
914 lz->stop = 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
915
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
916 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
917 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
918
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
919 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
920 takewhile_dealloc(takewhileobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
921 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
922 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
923 Py_XDECREF(lz->func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
924 Py_XDECREF(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
925 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
926 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
927
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
928 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
929 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
930 {
32682
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
931 Py_VISIT(lz->it);
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
932 Py_VISIT(lz->func);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
933 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
934 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
935
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
936 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
937 takewhile_next(takewhileobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
938 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
939 PyObject *item, *good;
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
940 PyObject *it = lz->it;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
941 long ok;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
942
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
943 if (lz->stop == 1)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
944 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
945
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
946 assert(PyIter_Check(it));
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
947 item = (*it->ob_type->tp_iternext)(it);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
948 if (item == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
949 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
950
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
951 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
952 if (good == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
953 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
954 return NULL;
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 ok = PyObject_IsTrue(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
957 Py_DECREF(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
958 if (ok)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
959 return item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
960 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
961 lz->stop = 1;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
962 return NULL;
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
965 PyDoc_STRVAR(takewhile_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
966 "takewhile(predicate, iterable) --> takewhile object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
967 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
968 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
969 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
970
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
971 static PyTypeObject takewhile_type = {
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
972 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
973 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
974 "itertools.takewhile", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
975 sizeof(takewhileobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
976 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
977 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
978 (destructor)takewhile_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
979 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
980 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
981 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
982 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
983 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
984 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
985 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
986 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
987 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
988 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
989 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
990 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
991 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
992 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
993 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
994 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
995 takewhile_doc, /* tp_doc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
996 (traverseproc)takewhile_traverse, /* tp_traverse */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
997 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
998 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
999 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1000 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1001 (iternextfunc)takewhile_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1002 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1003 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1004 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1005 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1006 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1007 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1008 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1009 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1010 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1011 0, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1012 takewhile_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1013 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1014 };
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1017 /* islice object ************************************************************/
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 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1020 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1021 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1022 long next;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1023 long stop;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1024 long step;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1025 long cnt;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1026 } isliceobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1027
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1028 static PyTypeObject islice_type;
27570
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 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1031 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
1032 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1033 PyObject *seq;
28929
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1034 long start=0, stop=-1, step=1;
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1035 PyObject *it, *a1=NULL, *a2=NULL;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1036 int numargs;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1037 isliceobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1038
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1039 numargs = PyTuple_Size(args);
28930
18082b2d2ebd The previous made the stop argument optional.
Raymond Hettinger <python@rcn.com>
parents: 28929
diff changeset
1040 if (!PyArg_ParseTuple(args, "OO|Ol:islice", &seq, &a1, &a2, &step))
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1041 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1042
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1043 if (numargs == 2) {
28929
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1044 if (a1 != Py_None) {
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1045 stop = PyInt_AsLong(a1);
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1046 if (stop == -1) {
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1047 if (PyErr_Occurred())
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1048 PyErr_Clear();
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1049 PyErr_SetString(PyExc_ValueError,
30800
af443d820a50 Fix nits in error messages.
Raymond Hettinger <python@rcn.com>
parents: 30786
diff changeset
1050 "Stop argument must be a non-negative integer or None.");
28929
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1051 return NULL;
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1052 }
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1053 }
28930
18082b2d2ebd The previous made the stop argument optional.
Raymond Hettinger <python@rcn.com>
parents: 28929
diff changeset
1054 } else {
28929
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1055 start = PyInt_AsLong(a1);
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1056 if (start == -1 && PyErr_Occurred()) {
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1057 PyErr_Clear();
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1058 PyErr_SetString(PyExc_ValueError,
30800
af443d820a50 Fix nits in error messages.
Raymond Hettinger <python@rcn.com>
parents: 30786
diff changeset
1059 "Start argument must be a non-negative integer.");
28929
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1060 return NULL;
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1061 }
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1062 if (a2 != Py_None) {
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1063 stop = PyInt_AsLong(a2);
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1064 if (stop == -1) {
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1065 if (PyErr_Occurred())
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1066 PyErr_Clear();
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1067 PyErr_SetString(PyExc_ValueError,
30800
af443d820a50 Fix nits in error messages.
Raymond Hettinger <python@rcn.com>
parents: 30786
diff changeset
1068 "Stop argument must be a non-negative integer or None.");
28929
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1069 return NULL;
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1070 }
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1071 }
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1072 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1073
28929
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1074 if (start<0 || stop<-1) {
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1075 PyErr_SetString(PyExc_ValueError,
30800
af443d820a50 Fix nits in error messages.
Raymond Hettinger <python@rcn.com>
parents: 30786
diff changeset
1076 "Indices for islice() must be non-negative integers.");
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1077 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1078 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1079
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1080 if (step<1) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1081 PyErr_SetString(PyExc_ValueError,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1082 "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
1083 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1084 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1085
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1086 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1087 it = PyObject_GetIter(seq);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1088 if (it == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1089 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1090
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1091 /* create isliceobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1092 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
1093 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1094 Py_DECREF(it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1095 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1096 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1097 lz->it = it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1098 lz->next = start;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1099 lz->stop = stop;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1100 lz->step = step;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1101 lz->cnt = 0L;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1102
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1103 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1104 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1105
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1106 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1107 islice_dealloc(isliceobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1108 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1109 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1110 Py_XDECREF(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1111 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1112 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1113
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1114 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1115 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
1116 {
32682
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
1117 Py_VISIT(lz->it);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1118 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1119 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1120
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1121 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1122 islice_next(isliceobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1123 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1124 PyObject *item;
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
1125 PyObject *it = lz->it;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1126 long oldnext;
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1127 PyObject *(*iternext)(PyObject *);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1128
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1129 assert(PyIter_Check(it));
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1130 iternext = *it->ob_type->tp_iternext;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1131 while (lz->cnt < lz->next) {
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1132 item = iternext(it);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1133 if (item == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1134 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1135 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1136 lz->cnt++;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1137 }
28929
92a1e585abf7 SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents: 28601
diff changeset
1138 if (lz->stop != -1 && lz->cnt >= lz->stop)
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1139 return NULL;
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
1140 assert(PyIter_Check(it));
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1141 item = iternext(it);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1142 if (item == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1143 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1144 lz->cnt++;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1145 oldnext = lz->next;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1146 lz->next += lz->step;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1147 if (lz->next < oldnext) /* Check for overflow */
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1148 lz->next = lz->stop;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1149 return item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1150 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1151
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1152 PyDoc_STRVAR(islice_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1153 "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
1154 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1155 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
1156 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
1157 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
1158 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
1159 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
1160 but returns an iterator.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1161
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1162 static PyTypeObject islice_type = {
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1163 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1164 0, /* ob_size */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1165 "itertools.islice", /* tp_name */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1166 sizeof(isliceobject), /* tp_basicsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1167 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1168 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1169 (destructor)islice_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1170 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1171 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1172 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1173 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1174 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1175 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1176 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1177 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1178 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1179 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1180 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1181 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1182 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1183 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1184 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
1185 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1186 islice_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1187 (traverseproc)islice_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1188 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1189 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1190 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1191 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1192 (iternextfunc)islice_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1193 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1194 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1195 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1196 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1197 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1198 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1199 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1200 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1201 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1202 0, /* tp_alloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1203 islice_new, /* tp_new */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1204 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1205 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1206
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1207
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1208 /* starmap object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1209
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1210 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1211 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1212 PyObject *func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1213 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1214 } starmapobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1215
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1216 static PyTypeObject starmap_type;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1217
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1218 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1219 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
1220 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1221 PyObject *func, *seq;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1222 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1223 starmapobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1224
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1225 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
1226 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1227
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1228 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1229 it = PyObject_GetIter(seq);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1230 if (it == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1231 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1232
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1233 /* create starmapobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1234 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
1235 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1236 Py_DECREF(it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1237 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1238 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1239 Py_INCREF(func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1240 lz->func = func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1241 lz->it = it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1242
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1243 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1244 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1245
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1246 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1247 starmap_dealloc(starmapobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1248 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1249 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1250 Py_XDECREF(lz->func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1251 Py_XDECREF(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1252 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1253 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1254
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1255 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1256 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
1257 {
32682
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
1258 Py_VISIT(lz->it);
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
1259 Py_VISIT(lz->func);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1260 return 0;
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1263 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1264 starmap_next(starmapobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1265 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1266 PyObject *args;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1267 PyObject *result;
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
1268 PyObject *it = lz->it;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1269
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
1270 assert(PyIter_Check(it));
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
1271 args = (*it->ob_type->tp_iternext)(it);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1272 if (args == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1273 return NULL;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1274 if (!PyTuple_CheckExact(args)) {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1275 Py_DECREF(args);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1276 PyErr_SetString(PyExc_TypeError,
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1277 "iterator must return a tuple");
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1278 return NULL;
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1279 }
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1280 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
1281 Py_DECREF(args);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1282 return result;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1283 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1284
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1285 PyDoc_STRVAR(starmap_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1286 "starmap(function, sequence) --> starmap object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1287 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1288 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
1289 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
1290
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1291 static PyTypeObject starmap_type = {
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1292 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1293 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1294 "itertools.starmap", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1295 sizeof(starmapobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1296 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1297 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1298 (destructor)starmap_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1299 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1300 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1301 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1302 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1303 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1304 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1305 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1306 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1307 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1308 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1309 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1310 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1311 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1312 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1313 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
1314 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1315 starmap_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1316 (traverseproc)starmap_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1317 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1318 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1319 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1320 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1321 (iternextfunc)starmap_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1322 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1323 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1324 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1325 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1326 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1327 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1328 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1329 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1330 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1331 0, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1332 starmap_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1333 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1334 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1335
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1336
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1337 /* imap object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1338
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1339 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1340 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1341 PyObject *iters;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1342 PyObject *func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1343 } imapobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1344
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1345 static PyTypeObject imap_type;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1346
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1347 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1348 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
1349 {
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1350 PyObject *it, *iters, *func;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1351 imapobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1352 int numargs, i;
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 numargs = PyTuple_Size(args);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1355 if (numargs < 2) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1356 PyErr_SetString(PyExc_TypeError,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1357 "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
1358 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1359 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1360
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1361 iters = PyTuple_New(numargs-1);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1362 if (iters == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1363 return NULL;
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 for (i=1 ; i<numargs ; i++) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1366 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1367 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
1368 if (it == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1369 Py_DECREF(iters);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1370 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1371 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1372 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
1373 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1374
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1375 /* create imapobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1376 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
1377 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1378 Py_DECREF(iters);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1379 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1380 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1381 lz->iters = iters;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1382 func = PyTuple_GET_ITEM(args, 0);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1383 Py_INCREF(func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1384 lz->func = func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1385
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1386 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1387 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1388
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1389 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1390 imap_dealloc(imapobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1391 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1392 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1393 Py_XDECREF(lz->iters);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1394 Py_XDECREF(lz->func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1395 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1396 }
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 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1399 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
1400 {
32682
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
1401 Py_VISIT(lz->iters);
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
1402 Py_VISIT(lz->func);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1403 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1404 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1405
27747
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 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
1408 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
1409 the following reasons:
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 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
1412 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
1413 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
1414 infinite iterators like count() and repeat() (for supplying sequential
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1415 or constant arguments to a function).
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1416
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1417 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
1418 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
1419 should not pass silently by automatically supplying None.
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1420
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1421 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
1422 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
1423 None.
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1424
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1425 4) If a need does arise, it can be met by __builtins__.map() or by
29535
c64687cd5b37 Minor updates:
Raymond Hettinger <python@rcn.com>
parents: 29522
diff changeset
1426 writing: chain(iterable, repeat(None)).
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1427
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1428 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
1429 */
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1430
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1431 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1432 imap_next(imapobject *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 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
1435 PyObject *argtuple;
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1436 PyObject *result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1437 int numargs, i;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1438
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1439 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
1440 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
1441 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
1442 return NULL;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1443
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1444 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
1445 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
1446 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
1447 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
1448 return NULL;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1449 }
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1450 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
1451 }
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1452 if (lz->func == Py_None)
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1453 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
1454 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
1455 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
1456 return result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1457 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1458
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1459 PyDoc_STRVAR(imap_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1460 "imap(func, *iterables) --> imap object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1461 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1462 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
1463 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
1464 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
1465 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
1466 iterables.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1467
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1468 static PyTypeObject imap_type = {
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1469 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1470 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1471 "itertools.imap", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1472 sizeof(imapobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1473 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1474 /* methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1475 (destructor)imap_dealloc, /* tp_dealloc */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1476 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1477 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1478 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1479 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1480 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1481 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1482 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1483 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1484 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1485 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1486 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1487 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1488 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1489 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1490 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
1491 Py_TPFLAGS_BASETYPE, /* tp_flags */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1492 imap_doc, /* tp_doc */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1493 (traverseproc)imap_traverse, /* tp_traverse */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1494 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1495 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1496 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1497 PyObject_SelfIter, /* tp_iter */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1498 (iternextfunc)imap_next, /* tp_iternext */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1499 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1500 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1501 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1502 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1503 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1504 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1505 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1506 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1507 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1508 0, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1509 imap_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1510 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1511 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1512
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1513
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1514 /* chain object ************************************************************/
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1515
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1516 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1517 PyObject_HEAD
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1518 long tuplesize;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1519 long iternum; /* which iterator is active */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1520 PyObject *ittuple; /* tuple of iterators */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1521 } chainobject;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1522
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1523 static PyTypeObject chain_type;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1524
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1525 static PyObject *
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1526 chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1527 {
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1528 chainobject *lz;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1529 int tuplesize = PySequence_Length(args);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1530 int i;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1531 PyObject *ittuple;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1532
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1533 /* obtain iterators */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1534 assert(PyTuple_Check(args));
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1535 ittuple = PyTuple_New(tuplesize);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1536 if(ittuple == NULL)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1537 return NULL;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1538 for (i=0; i < tuplesize; ++i) {
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1539 PyObject *item = PyTuple_GET_ITEM(args, i);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1540 PyObject *it = PyObject_GetIter(item);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1541 if (it == NULL) {
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1542 if (PyErr_ExceptionMatches(PyExc_TypeError))
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1543 PyErr_Format(PyExc_TypeError,
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1544 "chain argument #%d must support iteration",
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1545 i+1);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1546 Py_DECREF(ittuple);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1547 return NULL;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1548 }
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1549 PyTuple_SET_ITEM(ittuple, i, it);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1550 }
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1551
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1552 /* create chainobject structure */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1553 lz = (chainobject *)type->tp_alloc(type, 0);
29522
70e649fa3fcd Add missing DECREF.
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
1554 if (lz == NULL) {
70e649fa3fcd Add missing DECREF.
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
1555 Py_DECREF(ittuple);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1556 return NULL;
29522
70e649fa3fcd Add missing DECREF.
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
1557 }
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1558
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1559 lz->ittuple = ittuple;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1560 lz->iternum = 0;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1561 lz->tuplesize = tuplesize;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1562
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1563 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1564 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1565
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1566 static void
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1567 chain_dealloc(chainobject *lz)
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1568 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1569 PyObject_GC_UnTrack(lz);
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1570 Py_XDECREF(lz->ittuple);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1571 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1572 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1573
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1574 static int
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1575 chain_traverse(chainobject *lz, visitproc visit, void *arg)
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1576 {
32682
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
1577 Py_VISIT(lz->ittuple);
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1578 return 0;
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1579 }
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1580
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1581 static PyObject *
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1582 chain_next(chainobject *lz)
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1583 {
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1584 PyObject *it;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1585 PyObject *item;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1586
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1587 while (lz->iternum < lz->tuplesize) {
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1588 it = PyTuple_GET_ITEM(lz->ittuple, lz->iternum);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1589 item = PyIter_Next(it);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1590 if (item != NULL)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1591 return item;
32010
544f2172363d SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents: 31909
diff changeset
1592 if (PyErr_Occurred()) {
544f2172363d SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents: 31909
diff changeset
1593 if (PyErr_ExceptionMatches(PyExc_StopIteration))
544f2172363d SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents: 31909
diff changeset
1594 PyErr_Clear();
544f2172363d SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents: 31909
diff changeset
1595 else
544f2172363d SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents: 31909
diff changeset
1596 return NULL;
544f2172363d SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents: 31909
diff changeset
1597 }
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1598 lz->iternum++;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1599 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1600 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1601 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1602
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1603 PyDoc_STRVAR(chain_doc,
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1604 "chain(*iterables) --> chain object\n\
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1605 \n\
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1606 Return a chain object whose .next() method returns elements from the\n\
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1607 first iterable until it is exhausted, then elements from the next\n\
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1608 iterable, until all of the iterables are exhausted.");
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1609
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1610 static PyTypeObject chain_type = {
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1611 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1612 0, /* ob_size */
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1613 "itertools.chain", /* tp_name */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1614 sizeof(chainobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1615 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1616 /* methods */
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1617 (destructor)chain_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1618 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1619 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1620 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1621 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1622 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1623 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1624 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1625 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1626 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1627 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1628 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1629 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1630 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1631 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1632 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
1633 Py_TPFLAGS_BASETYPE, /* tp_flags */
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1634 chain_doc, /* tp_doc */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1635 (traverseproc)chain_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1636 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1637 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1638 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1639 PyObject_SelfIter, /* tp_iter */
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1640 (iternextfunc)chain_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1641 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1642 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1643 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1644 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1645 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1646 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1647 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1648 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1649 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1650 0, /* tp_alloc */
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1651 chain_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1652 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1653 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1654
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1655
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1656 /* ifilter object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1657
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1658 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1659 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1660 PyObject *func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1661 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1662 } ifilterobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1663
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1664 static PyTypeObject ifilter_type;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1665
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1666 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1667 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
1668 {
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1669 PyObject *func, *seq;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1670 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1671 ifilterobject *lz;
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 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
1674 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1675
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1676 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1677 it = PyObject_GetIter(seq);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1678 if (it == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1679 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1680
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1681 /* create ifilterobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1682 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
1683 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1684 Py_DECREF(it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1685 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1686 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1687 Py_INCREF(func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1688 lz->func = func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1689 lz->it = it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1690
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1691 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1692 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1693
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1694 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1695 ifilter_dealloc(ifilterobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1696 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1697 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1698 Py_XDECREF(lz->func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1699 Py_XDECREF(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1700 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1701 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1702
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1703 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1704 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
1705 {
32682
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
1706 Py_VISIT(lz->it);
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
1707 Py_VISIT(lz->func);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1708 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1709 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1710
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1711 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1712 ifilter_next(ifilterobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1713 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1714 PyObject *item;
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
1715 PyObject *it = lz->it;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1716 long ok;
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1717 PyObject *(*iternext)(PyObject *);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1718
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1719 assert(PyIter_Check(it));
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1720 iternext = *it->ob_type->tp_iternext;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1721 for (;;) {
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1722 item = iternext(it);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1723 if (item == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1724 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1725
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1726 if (lz->func == Py_None) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1727 ok = PyObject_IsTrue(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1728 } else {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1729 PyObject *good;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1730 good = PyObject_CallFunctionObjArgs(lz->func,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1731 item, NULL);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1732 if (good == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1733 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1734 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1735 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1736 ok = PyObject_IsTrue(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1737 Py_DECREF(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1738 }
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1739 if (ok)
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1740 return item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1741 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1742 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1743 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1744
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1745 PyDoc_STRVAR(ifilter_doc,
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1746 "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
1747 \n\
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1748 Return those items of sequence for which function(item) is true.\n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1749 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
1750
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1751 static PyTypeObject ifilter_type = {
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1752 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1753 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1754 "itertools.ifilter", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1755 sizeof(ifilterobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1756 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1757 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1758 (destructor)ifilter_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1759 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1760 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1761 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1762 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1763 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1764 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1765 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1766 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1767 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1768 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1769 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1770 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1771 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1772 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1773 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
1774 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1775 ifilter_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1776 (traverseproc)ifilter_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1777 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1778 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1779 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1780 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1781 (iternextfunc)ifilter_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1782 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1783 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1784 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1785 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1786 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1787 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1788 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1789 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1790 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1791 0, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1792 ifilter_new, /* tp_new */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1793 PyObject_GC_Del, /* tp_free */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1794 };
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1795
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1796
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1797 /* ifilterfalse object ************************************************************/
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1798
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1799 typedef struct {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1800 PyObject_HEAD
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1801 PyObject *func;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1802 PyObject *it;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1803 } ifilterfalseobject;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1804
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1805 static PyTypeObject ifilterfalse_type;
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1806
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1807 static PyObject *
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1808 ifilterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1809 {
27770
2c389a0448a4 Remove unused variable.
Guido van Rossum <guido@python.org>
parents: 27769
diff changeset
1810 PyObject *func, *seq;
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1811 PyObject *it;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1812 ifilterfalseobject *lz;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1813
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1814 if (!PyArg_UnpackTuple(args, "ifilterfalse", 2, 2, &func, &seq))
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1815 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1816
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1817 /* Get iterator. */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1818 it = PyObject_GetIter(seq);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1819 if (it == NULL)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1820 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1821
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1822 /* create ifilterfalseobject structure */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1823 lz = (ifilterfalseobject *)type->tp_alloc(type, 0);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1824 if (lz == NULL) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1825 Py_DECREF(it);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1826 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1827 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1828 Py_INCREF(func);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1829 lz->func = func;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1830 lz->it = it;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1831
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1832 return (PyObject *)lz;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1833 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1834
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1835 static void
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1836 ifilterfalse_dealloc(ifilterfalseobject *lz)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1837 {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1838 PyObject_GC_UnTrack(lz);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1839 Py_XDECREF(lz->func);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1840 Py_XDECREF(lz->it);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1841 lz->ob_type->tp_free(lz);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1842 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1843
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1844 static int
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1845 ifilterfalse_traverse(ifilterfalseobject *lz, visitproc visit, void *arg)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1846 {
32682
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
1847 Py_VISIT(lz->it);
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
1848 Py_VISIT(lz->func);
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1849 return 0;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1850 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1851
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1852 static PyObject *
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1853 ifilterfalse_next(ifilterfalseobject *lz)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1854 {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1855 PyObject *item;
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
1856 PyObject *it = lz->it;
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1857 long ok;
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1858 PyObject *(*iternext)(PyObject *);
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1859
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1860 assert(PyIter_Check(it));
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1861 iternext = *it->ob_type->tp_iternext;
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1862 for (;;) {
31675
f7374e3b279a Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents: 31508
diff changeset
1863 item = iternext(it);
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1864 if (item == NULL)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1865 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1866
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1867 if (lz->func == Py_None) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1868 ok = PyObject_IsTrue(item);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1869 } else {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1870 PyObject *good;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1871 good = PyObject_CallFunctionObjArgs(lz->func,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1872 item, NULL);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1873 if (good == NULL) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1874 Py_DECREF(item);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1875 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1876 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1877 ok = PyObject_IsTrue(good);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1878 Py_DECREF(good);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1879 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1880 if (!ok)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1881 return item;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1882 Py_DECREF(item);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1883 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1884 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1885
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1886 PyDoc_STRVAR(ifilterfalse_doc,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1887 "ifilterfalse(function or None, sequence) --> ifilterfalse object\n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1888 \n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1889 Return those items of sequence for which function(item) is false.\n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1890 If function is None, return the items that are false.");
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1891
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1892 static PyTypeObject ifilterfalse_type = {
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1893 PyObject_HEAD_INIT(NULL)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1894 0, /* ob_size */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1895 "itertools.ifilterfalse", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1896 sizeof(ifilterfalseobject), /* tp_basicsize */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1897 0, /* tp_itemsize */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1898 /* methods */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1899 (destructor)ifilterfalse_dealloc, /* tp_dealloc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1900 0, /* tp_print */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1901 0, /* tp_getattr */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1902 0, /* tp_setattr */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1903 0, /* tp_compare */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1904 0, /* tp_repr */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1905 0, /* tp_as_number */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1906 0, /* tp_as_sequence */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1907 0, /* tp_as_mapping */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1908 0, /* tp_hash */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1909 0, /* tp_call */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1910 0, /* tp_str */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1911 PyObject_GenericGetAttr, /* tp_getattro */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1912 0, /* tp_setattro */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1913 0, /* tp_as_buffer */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1914 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1915 Py_TPFLAGS_BASETYPE, /* tp_flags */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1916 ifilterfalse_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1917 (traverseproc)ifilterfalse_traverse, /* tp_traverse */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1918 0, /* tp_clear */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1919 0, /* tp_richcompare */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1920 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1921 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1922 (iternextfunc)ifilterfalse_next, /* tp_iternext */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1923 0, /* tp_methods */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1924 0, /* tp_members */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1925 0, /* tp_getset */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1926 0, /* tp_base */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1927 0, /* tp_dict */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1928 0, /* tp_descr_get */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1929 0, /* tp_descr_set */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1930 0, /* tp_dictoffset */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1931 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1932 0, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1933 ifilterfalse_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1934 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1935 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1936
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1937
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1938 /* count object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1939
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1940 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1941 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1942 long cnt;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1943 } countobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1944
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1945 static PyTypeObject count_type;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1946
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1947 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1948 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
1949 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1950 countobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1951 long cnt = 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1952
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1953 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
1954 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1955
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1956 /* create countobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1957 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
1958 if (lz == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1959 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1960 lz->cnt = cnt;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1961
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1962 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1963 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1964
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1965 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1966 count_next(countobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1967 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1968 return PyInt_FromLong(lz->cnt++);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1969 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1970
31890
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
1971 static PyObject *
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
1972 count_repr(countobject *lz)
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
1973 {
31909
6b236994684a Change two instance of format strings for PyString_FromFormat() to use %ld
Brett Cannon <bcannon@gmail.com>
parents: 31890
diff changeset
1974 return PyString_FromFormat("count(%ld)", lz->cnt);
31890
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
1975 }
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
1976
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1977 PyDoc_STRVAR(count_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1978 "count([firstval]) --> count object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1979 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1980 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
1981 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
1982
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
1983 static PyTypeObject count_type = {
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1984 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1985 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1986 "itertools.count", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1987 sizeof(countobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1988 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1989 /* methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1990 (destructor)PyObject_Del, /* tp_dealloc */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1991 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1992 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1993 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1994 0, /* tp_compare */
31890
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
1995 (reprfunc)count_repr, /* tp_repr */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1996 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1997 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1998 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1999 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2000 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2001 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2002 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2003 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2004 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2005 Py_TPFLAGS_DEFAULT, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2006 count_doc, /* tp_doc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2007 0, /* tp_traverse */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2008 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2009 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2010 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
2011 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2012 (iternextfunc)count_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2013 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2014 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2015 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2016 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2017 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2018 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2019 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2020 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2021 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
2022 0, /* tp_alloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2023 count_new, /* tp_new */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2024 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2025
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2026
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2027 /* izip object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2028
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2029 #include "Python.h"
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2030
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2031 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2032 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2033 long tuplesize;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2034 PyObject *ittuple; /* tuple of iterators */
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2035 PyObject *result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2036 } izipobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2037
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
2038 static PyTypeObject izip_type;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2039
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2040 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2041 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
2042 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2043 izipobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2044 int i;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2045 PyObject *ittuple; /* tuple of iterators */
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2046 PyObject *result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2047 int tuplesize = PySequence_Length(args);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2048
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2049 /* args must be a tuple */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2050 assert(PyTuple_Check(args));
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2051
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2052 /* obtain iterators */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2053 ittuple = PyTuple_New(tuplesize);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2054 if(ittuple == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2055 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2056 for (i=0; i < tuplesize; ++i) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2057 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
2058 PyObject *it = PyObject_GetIter(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2059 if (it == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2060 if (PyErr_ExceptionMatches(PyExc_TypeError))
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2061 PyErr_Format(PyExc_TypeError,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2062 "izip argument #%d must support iteration",
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2063 i+1);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2064 Py_DECREF(ittuple);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2065 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2066 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2067 PyTuple_SET_ITEM(ittuple, i, it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2068 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2069
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2070 /* create a result holder */
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2071 result = PyTuple_New(tuplesize);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2072 if (result == NULL) {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2073 Py_DECREF(ittuple);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2074 return NULL;
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2075 }
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2076 for (i=0 ; i < tuplesize ; i++) {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2077 Py_INCREF(Py_None);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2078 PyTuple_SET_ITEM(result, i, Py_None);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2079 }
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2080
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2081 /* create izipobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2082 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
2083 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2084 Py_DECREF(ittuple);
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2085 Py_DECREF(result);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2086 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2087 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2088 lz->ittuple = ittuple;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2089 lz->tuplesize = tuplesize;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2090 lz->result = result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2091
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2092 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2093 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2094
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2095 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2096 izip_dealloc(izipobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2097 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2098 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2099 Py_XDECREF(lz->ittuple);
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2100 Py_XDECREF(lz->result);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2101 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2102 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2103
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2104 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2105 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
2106 {
32682
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
2107 Py_VISIT(lz->ittuple);
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
2108 Py_VISIT(lz->result);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2109 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2110 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2111
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2112 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2113 izip_next(izipobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2114 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2115 int i;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2116 long tuplesize = lz->tuplesize;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2117 PyObject *result = lz->result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2118 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2119 PyObject *item;
30240
f1ea311127cf For safety, replace a tuple entry before decreffing it.
Raymond Hettinger <python@rcn.com>
parents: 30238
diff changeset
2120 PyObject *olditem;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2121
30093
0ac4396b1894 Modified itertools.izip() to match the behavior of __builtin__.zip()
Raymond Hettinger <python@rcn.com>
parents: 29850
diff changeset
2122 if (tuplesize == 0)
0ac4396b1894 Modified itertools.izip() to match the behavior of __builtin__.zip()
Raymond Hettinger <python@rcn.com>
parents: 29850
diff changeset
2123 return NULL;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2124 if (result->ob_refcnt == 1) {
30238
a570bb9f133f SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents: 30093
diff changeset
2125 Py_INCREF(result);
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2126 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
2127 it = PyTuple_GET_ITEM(lz->ittuple, i);
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
2128 assert(PyIter_Check(it));
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
2129 item = (*it->ob_type->tp_iternext)(it);
30238
a570bb9f133f SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents: 30093
diff changeset
2130 if (item == NULL) {
a570bb9f133f SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents: 30093
diff changeset
2131 Py_DECREF(result);
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
2132 return NULL;
30238
a570bb9f133f SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents: 30093
diff changeset
2133 }
30240
f1ea311127cf For safety, replace a tuple entry before decreffing it.
Raymond Hettinger <python@rcn.com>
parents: 30238
diff changeset
2134 olditem = 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
2135 PyTuple_SET_ITEM(result, i, item);
30240
f1ea311127cf For safety, replace a tuple entry before decreffing it.
Raymond Hettinger <python@rcn.com>
parents: 30238
diff changeset
2136 Py_DECREF(olditem);
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2137 }
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2138 } else {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2139 result = PyTuple_New(tuplesize);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2140 if (result == NULL)
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
2141 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
2142 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
2143 it = PyTuple_GET_ITEM(lz->ittuple, i);
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
2144 assert(PyIter_Check(it));
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
2145 item = (*it->ob_type->tp_iternext)(it);
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
2146 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
2147 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
2148 return NULL;
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
2149 }
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
2150 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
2151 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2152 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2153 return result;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2154 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2155
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2156 PyDoc_STRVAR(izip_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2157 "izip(iter1 [,iter2 [...]]) --> izip object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2158 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2159 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
2160 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
2161 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
2162 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
2163 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
2164 a list.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2165
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
2166 static PyTypeObject izip_type = {
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2167 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2168 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2169 "itertools.izip", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2170 sizeof(izipobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2171 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2172 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2173 (destructor)izip_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2174 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2175 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2176 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2177 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2178 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2179 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2180 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2181 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2182 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2183 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2184 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2185 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2186 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2187 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2188 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
2189 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2190 izip_doc, /* tp_doc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2191 (traverseproc)izip_traverse, /* tp_traverse */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2192 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2193 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2194 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
2195 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2196 (iternextfunc)izip_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2197 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2198 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2199 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2200 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2201 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2202 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2203 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2204 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2205 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
2206 0, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2207 izip_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2208 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2209 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2210
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2211
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2212 /* repeat object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2213
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2214 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2215 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2216 PyObject *element;
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2217 long cnt;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2218 } repeatobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2219
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
2220 static PyTypeObject repeat_type;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2221
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2222 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2223 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
2224 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2225 repeatobject *ro;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2226 PyObject *element;
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2227 long cnt = -1;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2228
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2229 if (!PyArg_ParseTuple(args, "O|l:repeat", &element, &cnt))
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2230 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2231
28945
001a90555780 * Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents: 28930
diff changeset
2232 if (PyTuple_Size(args) == 2 && cnt < 0)
001a90555780 * Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents: 28930
diff changeset
2233 cnt = 0;
001a90555780 * Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents: 28930
diff changeset
2234
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2235 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
2236 if (ro == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2237 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2238 Py_INCREF(element);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2239 ro->element = element;
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2240 ro->cnt = cnt;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2241 return (PyObject *)ro;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2242 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2243
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2244 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2245 repeat_dealloc(repeatobject *ro)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2246 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2247 PyObject_GC_UnTrack(ro);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2248 Py_XDECREF(ro->element);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2249 ro->ob_type->tp_free(ro);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2250 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2251
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2252 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2253 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
2254 {
32682
bc4dd31c73fd Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents: 32010
diff changeset
2255 Py_VISIT(ro->element);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2256 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2257 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2258
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2259 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2260 repeat_next(repeatobject *ro)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2261 {
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2262 if (ro->cnt == 0)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2263 return NULL;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2264 if (ro->cnt > 0)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2265 ro->cnt--;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2266 Py_INCREF(ro->element);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2267 return ro->element;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2268 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2269
31890
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2270 static PyObject *
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2271 repeat_repr(repeatobject *ro)
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2272 {
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2273 PyObject *result, *objrepr;
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2274
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2275 objrepr = PyObject_Repr(ro->element);
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2276 if (objrepr == NULL)
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2277 return NULL;
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2278
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2279 if (ro->cnt == -1)
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2280 result = PyString_FromFormat("repeat(%s)",
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2281 PyString_AS_STRING(objrepr));
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2282 else
31909
6b236994684a Change two instance of format strings for PyString_FromFormat() to use %ld
Brett Cannon <bcannon@gmail.com>
parents: 31890
diff changeset
2283 result = PyString_FromFormat("repeat(%s, %ld)",
31890
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2284 PyString_AS_STRING(objrepr), ro->cnt);
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2285 Py_DECREF(objrepr);
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2286 return result;
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2287 }
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2288
31501
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2289 static int
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2290 repeat_len(repeatobject *ro)
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2291 {
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2292 if (ro->cnt == -1)
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2293 PyErr_SetString(PyExc_TypeError, "len() of unsized object");
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2294 return (int)(ro->cnt);
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2295 }
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2296
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2297 static PySequenceMethods repeat_as_sequence = {
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2298 (inquiry)repeat_len, /* sq_length */
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2299 0, /* sq_concat */
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2300 };
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2301
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2302 PyDoc_STRVAR(repeat_doc,
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2303 "repeat(element [,times]) -> create an iterator which returns the element\n\
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2304 for the specified number of times. If not specified, returns the element\n\
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2305 endlessly.");
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2306
29850
45dc33433323 SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents: 29535
diff changeset
2307 static PyTypeObject repeat_type = {
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2308 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2309 0, /* ob_size */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2310 "itertools.repeat", /* tp_name */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2311 sizeof(repeatobject), /* tp_basicsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2312 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2313 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2314 (destructor)repeat_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2315 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2316 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2317 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2318 0, /* tp_compare */
31890
848bce2215d3 Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents: 31675
diff changeset
2319 (reprfunc)repeat_repr, /* tp_repr */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2320 0, /* tp_as_number */
31501
14f860b03be2 Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents: 31136
diff changeset
2321 &repeat_as_sequence, /* tp_as_sequence */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2322 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2323 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2324 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2325 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2326 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2327 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2328 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2329 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
2330 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2331 repeat_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2332 (traverseproc)repeat_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2333 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2334 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2335 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
2336 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2337 (iternextfunc)repeat_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2338 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2339 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2340 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2341 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2342 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2343 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2344 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2345 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2346 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
2347 0, /* tp_alloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2348 repeat_new, /* tp_new */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2349 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2350 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2351
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2352
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2353 /* module level code ********************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2354
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2355 PyDoc_STRVAR(module_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2356 "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
2357 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2358 Infinite iterators:\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2359 count([n]) --> n, n+1, n+2, ...\n\
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2360 cycle(p) --> p0, p1, ... plast, p0, p1, ...\n\
28601
58db5fbd9c28 Fix docstring typo
Andrew M. Kuchling <amk@amk.ca>
parents: 28320
diff changeset
2361 repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times\n\
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2362 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2363 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
2364 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
2365 ifilter(pred, seq) --> elements of seq where pred(elem) is True\n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2366 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
2367 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
2368 seq[start:stop:step]\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2369 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
2370 starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
2371 tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n\n\
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2372 chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ... \n\
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2373 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
2374 dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred fails\n\
31136
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
2375 groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)\n\
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2376 ");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2377
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2378
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
2379 static PyMethodDef module_methods[] = {
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
2380 {"tee", (PyCFunction)tee, METH_VARARGS, tee_doc},
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
2381 {NULL, NULL} /* sentinel */
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
2382 };
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
2383
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2384 PyMODINIT_FUNC
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2385 inititertools(void)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2386 {
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2387 int i;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2388 PyObject *m;
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2389 char *name;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2390 PyTypeObject *typelist[] = {
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2391 &cycle_type,
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2392 &dropwhile_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2393 &takewhile_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2394 &islice_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2395 &starmap_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2396 &imap_type,
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2397 &chain_type,
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2398 &ifilter_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2399 &ifilterfalse_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2400 &count_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2401 &izip_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2402 &repeat_type,
31136
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
2403 &groupby_type,
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2404 NULL
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2405 };
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2406
31508
71864820402d need to initialize ob_type slot at run-time, at least on cygwin
Skip Montanaro <skip@pobox.com>
parents: 31501
diff changeset
2407 teedataobject_type.ob_type = &PyType_Type;
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
2408 m = Py_InitModule3("itertools", module_methods, module_doc);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2409
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2410 for (i=0 ; typelist[i] != NULL ; i++) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2411 if (PyType_Ready(typelist[i]) < 0)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2412 return;
29225
f3f306457b43 Fixed dotted name assertion.
Raymond Hettinger <python@rcn.com>
parents: 28945
diff changeset
2413 name = strchr(typelist[i]->tp_name, '.');
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
2414 assert (name != NULL);
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2415 Py_INCREF(typelist[i]);
29225
f3f306457b43 Fixed dotted name assertion.
Raymond Hettinger <python@rcn.com>
parents: 28945
diff changeset
2416 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
2417 }
30900
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
2418
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
2419 if (PyType_Ready(&teedataobject_type) < 0)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
2420 return;
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
2421 if (PyType_Ready(&tee_type) < 0)
3096fab2a19c Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents: 30800
diff changeset
2422 return;
31136
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
2423 if (PyType_Ready(&_grouper_type) < 0)
fde74fadf780 Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents: 30900
diff changeset
2424 return;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2425 }