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