Mercurial > cpython-lru_cache-weakref
annotate Modules/itertoolsmodule.c @ 44825:ef859cd754a7 legacy-trunk
Fixed a wrong assumption in configure.in and Include/pyport.h. The is finite function is not called isfinite() but finite(). Sorry, my fault. :)
| author | Christian Heimes <christian@cheimes.de> |
|---|---|
| date | Sun, 20 Jan 2008 14:28:28 +0000 |
| parents | d0f7963cf8b0 |
| children | 3cc57fd87d0e |
| 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" |
|
33779
c66e6c9ffc2d
some platforms still need offsetof() from structmember.h
Fred Drake <fdrake@acm.org>
parents:
33777
diff
changeset
|
3 #include "structmember.h" |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
4 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
5 /* Itertools module written and maintained |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
6 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
|
7 Copyright (c) 2003 Python Software Foundation. |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
8 All rights reserved. |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
9 */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
10 |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
11 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
12 /* groupby object ***********************************************************/ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
13 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
14 typedef struct { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
15 PyObject_HEAD |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
16 PyObject *it; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
17 PyObject *keyfunc; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
18 PyObject *tgtkey; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
19 PyObject *currkey; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
20 PyObject *currvalue; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
21 } groupbyobject; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
22 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
23 static PyTypeObject groupby_type; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
24 static PyObject *_grouper_create(groupbyobject *, PyObject *); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
25 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
26 static PyObject * |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
27 groupby_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
28 { |
| 36684 | 29 static char *kwargs[] = {"iterable", "key", NULL}; |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
30 groupbyobject *gbo; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
31 PyObject *it, *keyfunc = Py_None; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
32 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
33 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:groupby", kwargs, |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
34 &it, &keyfunc)) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
35 return NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
36 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
37 gbo = (groupbyobject *)type->tp_alloc(type, 0); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
38 if (gbo == NULL) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
39 return NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
40 gbo->tgtkey = NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
41 gbo->currkey = NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
42 gbo->currvalue = NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
43 gbo->keyfunc = keyfunc; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
44 Py_INCREF(keyfunc); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
45 gbo->it = PyObject_GetIter(it); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
46 if (gbo->it == NULL) { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
47 Py_DECREF(gbo); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
48 return NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
49 } |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
50 return (PyObject *)gbo; |
|
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 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
53 static void |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
54 groupby_dealloc(groupbyobject *gbo) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
55 { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
56 PyObject_GC_UnTrack(gbo); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
57 Py_XDECREF(gbo->it); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
58 Py_XDECREF(gbo->keyfunc); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
59 Py_XDECREF(gbo->tgtkey); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
60 Py_XDECREF(gbo->currkey); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
61 Py_XDECREF(gbo->currvalue); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
62 Py_TYPE(gbo)->tp_free(gbo); |
|
31136
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 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
65 static int |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
66 groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
67 { |
|
32682
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
68 Py_VISIT(gbo->it); |
|
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
69 Py_VISIT(gbo->keyfunc); |
|
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
70 Py_VISIT(gbo->tgtkey); |
|
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
71 Py_VISIT(gbo->currkey); |
|
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
72 Py_VISIT(gbo->currvalue); |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
73 return 0; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
74 } |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
75 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
76 static PyObject * |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
77 groupby_next(groupbyobject *gbo) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
78 { |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
79 PyObject *newvalue, *newkey, *r, *grouper, *tmp; |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
80 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
81 /* skip to next iteration group */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
82 for (;;) { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
83 if (gbo->currkey == NULL) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
84 /* pass */; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
85 else if (gbo->tgtkey == NULL) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
86 break; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
87 else { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
88 int rcmp; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
89 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
90 rcmp = PyObject_RichCompareBool(gbo->tgtkey, |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
91 gbo->currkey, Py_EQ); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
92 if (rcmp == -1) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
93 return NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
94 else if (rcmp == 0) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
95 break; |
|
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 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
98 newvalue = PyIter_Next(gbo->it); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
99 if (newvalue == NULL) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
100 return NULL; |
|
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 if (gbo->keyfunc == Py_None) { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
103 newkey = newvalue; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
104 Py_INCREF(newvalue); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
105 } else { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
106 newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
107 newvalue, NULL); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
108 if (newkey == NULL) { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
109 Py_DECREF(newvalue); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
110 return NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
111 } |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
112 } |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
113 |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
114 tmp = gbo->currkey; |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
115 gbo->currkey = newkey; |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
116 Py_XDECREF(tmp); |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
117 |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
118 tmp = gbo->currvalue; |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
119 gbo->currvalue = newvalue; |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
120 Py_XDECREF(tmp); |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
121 } |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
122 |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
123 Py_INCREF(gbo->currkey); |
|
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
124 tmp = gbo->tgtkey; |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
125 gbo->tgtkey = gbo->currkey; |
|
33616
31b08670ebdd
* Increase test coverage.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
126 Py_XDECREF(tmp); |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
127 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
128 grouper = _grouper_create(gbo, gbo->tgtkey); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
129 if (grouper == NULL) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
130 return NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
131 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
132 r = PyTuple_Pack(2, gbo->currkey, grouper); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
133 Py_DECREF(grouper); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
134 return r; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
135 } |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
136 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
137 PyDoc_STRVAR(groupby_doc, |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
138 "groupby(iterable[, keyfunc]) -> create an iterator which returns\n\ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
139 (key, sub-iterator) grouped by each value of key(value).\n"); |
|
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 static PyTypeObject groupby_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
142 PyVarObject_HEAD_INIT(NULL, 0) |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
143 "itertools.groupby", /* tp_name */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
144 sizeof(groupbyobject), /* tp_basicsize */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
145 0, /* tp_itemsize */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
146 /* methods */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
147 (destructor)groupby_dealloc, /* tp_dealloc */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
148 0, /* tp_print */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
149 0, /* tp_getattr */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
150 0, /* tp_setattr */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
151 0, /* tp_compare */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
152 0, /* tp_repr */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
153 0, /* tp_as_number */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
154 0, /* tp_as_sequence */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
155 0, /* tp_as_mapping */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
156 0, /* tp_hash */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
157 0, /* tp_call */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
158 0, /* tp_str */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
159 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
160 0, /* tp_setattro */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
161 0, /* tp_as_buffer */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
162 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
163 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
164 groupby_doc, /* tp_doc */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
165 (traverseproc)groupby_traverse, /* tp_traverse */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
166 0, /* tp_clear */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
167 0, /* tp_richcompare */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
168 0, /* tp_weaklistoffset */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
169 PyObject_SelfIter, /* tp_iter */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
170 (iternextfunc)groupby_next, /* tp_iternext */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
171 0, /* tp_methods */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
172 0, /* tp_members */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
173 0, /* tp_getset */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
174 0, /* tp_base */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
175 0, /* tp_dict */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
176 0, /* tp_descr_get */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
177 0, /* tp_descr_set */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
178 0, /* tp_dictoffset */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
179 0, /* tp_init */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
180 0, /* tp_alloc */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
181 groupby_new, /* tp_new */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
182 PyObject_GC_Del, /* tp_free */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
183 }; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
184 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
185 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
186 /* _grouper object (internal) ************************************************/ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
187 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
188 typedef struct { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
189 PyObject_HEAD |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
190 PyObject *parent; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
191 PyObject *tgtkey; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
192 } _grouperobject; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
193 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
194 static PyTypeObject _grouper_type; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
195 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
196 static PyObject * |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
197 _grouper_create(groupbyobject *parent, PyObject *tgtkey) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
198 { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
199 _grouperobject *igo; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
200 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
201 igo = PyObject_New(_grouperobject, &_grouper_type); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
202 if (igo == NULL) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
203 return NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
204 igo->parent = (PyObject *)parent; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
205 Py_INCREF(parent); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
206 igo->tgtkey = tgtkey; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
207 Py_INCREF(tgtkey); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
208 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
209 return (PyObject *)igo; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
210 } |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
211 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
212 static void |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
213 _grouper_dealloc(_grouperobject *igo) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
214 { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
215 Py_DECREF(igo->parent); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
216 Py_DECREF(igo->tgtkey); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
217 PyObject_Del(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 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
220 static PyObject * |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
221 _grouper_next(_grouperobject *igo) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
222 { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
223 groupbyobject *gbo = (groupbyobject *)igo->parent; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
224 PyObject *newvalue, *newkey, *r; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
225 int rcmp; |
|
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 if (gbo->currvalue == NULL) { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
228 newvalue = PyIter_Next(gbo->it); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
229 if (newvalue == NULL) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
230 return NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
231 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
232 if (gbo->keyfunc == Py_None) { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
233 newkey = newvalue; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
234 Py_INCREF(newvalue); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
235 } else { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
236 newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
237 newvalue, NULL); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
238 if (newkey == NULL) { |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
239 Py_DECREF(newvalue); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
240 return NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
241 } |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
242 } |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
243 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
244 assert(gbo->currkey == NULL); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
245 gbo->currkey = newkey; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
246 gbo->currvalue = newvalue; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
247 } |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
248 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
249 assert(gbo->currkey != NULL); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
250 rcmp = PyObject_RichCompareBool(igo->tgtkey, gbo->currkey, Py_EQ); |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
251 if (rcmp <= 0) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
252 /* got any error or current group is end */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
253 return NULL; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
254 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
255 r = gbo->currvalue; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
256 gbo->currvalue = NULL; |
|
33386
c2c49ee727d0
SF patch #1020188: Use Py_CLEAR where necessary to avoid crashes
Raymond Hettinger <python@rcn.com>
parents:
32682
diff
changeset
|
257 Py_CLEAR(gbo->currkey); |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
258 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
259 return r; |
|
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 static PyTypeObject _grouper_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
263 PyVarObject_HEAD_INIT(NULL, 0) |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
264 "itertools._grouper", /* tp_name */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
265 sizeof(_grouperobject), /* tp_basicsize */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
266 0, /* tp_itemsize */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
267 /* methods */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
268 (destructor)_grouper_dealloc, /* tp_dealloc */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
269 0, /* tp_print */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
270 0, /* tp_getattr */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
271 0, /* tp_setattr */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
272 0, /* tp_compare */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
273 0, /* tp_repr */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
274 0, /* tp_as_number */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
275 0, /* tp_as_sequence */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
276 0, /* tp_as_mapping */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
277 0, /* tp_hash */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
278 0, /* tp_call */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
279 0, /* tp_str */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
280 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
281 0, /* tp_setattro */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
282 0, /* tp_as_buffer */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
283 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
284 0, /* tp_doc */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
285 0, /* tp_traverse */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
286 0, /* tp_clear */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
287 0, /* tp_richcompare */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
288 0, /* tp_weaklistoffset */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
289 PyObject_SelfIter, /* tp_iter */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
290 (iternextfunc)_grouper_next, /* tp_iternext */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
291 0, /* tp_methods */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
292 0, /* tp_members */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
293 0, /* tp_getset */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
294 0, /* tp_base */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
295 0, /* tp_dict */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
296 0, /* tp_descr_get */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
297 0, /* tp_descr_set */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
298 0, /* tp_dictoffset */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
299 0, /* tp_init */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
300 0, /* tp_alloc */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
301 0, /* tp_new */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
302 PyObject_Del, /* tp_free */ |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
303 }; |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
304 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
305 |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
306 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
307 /* tee object and with supporting function and objects ***************/ |
| 30764 | 308 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
309 /* 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
|
310 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
|
311 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
|
312 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
|
313 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
|
314 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
|
315 wasted space and the more rapid freeing of older data. |
| 30764 | 316 */ |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
317 #define LINKCELLS 57 |
| 30764 | 318 |
| 319 typedef struct { | |
| 320 PyObject_HEAD | |
| 321 PyObject *it; | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
322 int numread; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
323 PyObject *nextlink; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
324 PyObject *(values[LINKCELLS]); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
325 } teedataobject; |
| 30764 | 326 |
| 327 typedef struct { | |
| 328 PyObject_HEAD | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
329 teedataobject *dataobj; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
330 int index; |
|
33777
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33646
diff
changeset
|
331 PyObject *weakreflist; |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
332 } teeobject; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
333 |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
334 static PyTypeObject teedataobject_type; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
335 |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
336 static PyObject * |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
337 teedataobject_new(PyObject *it) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
338 { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
339 teedataobject *tdo; |
| 30764 | 340 |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
341 tdo = PyObject_GC_New(teedataobject, &teedataobject_type); |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
342 if (tdo == NULL) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
343 return NULL; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
344 |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
345 tdo->numread = 0; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
346 tdo->nextlink = NULL; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
347 Py_INCREF(it); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
348 tdo->it = it; |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
349 PyObject_GC_Track(tdo); |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
350 return (PyObject *)tdo; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
351 } |
| 30764 | 352 |
| 353 static PyObject * | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
354 teedataobject_jumplink(teedataobject *tdo) |
| 30764 | 355 { |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
356 if (tdo->nextlink == NULL) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
357 tdo->nextlink = teedataobject_new(tdo->it); |
|
39144
e9773fc27e9b
nextlink can be NULL if teedataobject_new fails, so use XINCREF.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38420
diff
changeset
|
358 Py_XINCREF(tdo->nextlink); |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
359 return tdo->nextlink; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
360 } |
| 30764 | 361 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
362 static PyObject * |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
363 teedataobject_getitem(teedataobject *tdo, int i) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
364 { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
365 PyObject *value; |
| 30764 | 366 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
367 assert(i < LINKCELLS); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
368 if (i < tdo->numread) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
369 value = tdo->values[i]; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
370 else { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
371 /* 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
|
372 assert(i == tdo->numread); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
373 value = PyIter_Next(tdo->it); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
374 if (value == NULL) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
375 return NULL; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
376 tdo->numread++; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
377 tdo->values[i] = value; |
| 30764 | 378 } |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
379 Py_INCREF(value); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
380 return value; |
| 30764 | 381 } |
| 382 | |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
383 static int |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
384 teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg) |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
385 { |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
386 int i; |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
387 Py_VISIT(tdo->it); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
388 for (i = 0; i < tdo->numread; i++) |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
389 Py_VISIT(tdo->values[i]); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
390 Py_VISIT(tdo->nextlink); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
391 return 0; |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
392 } |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
393 |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
394 static int |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
395 teedataobject_clear(teedataobject *tdo) |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
396 { |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
397 int i; |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
398 Py_CLEAR(tdo->it); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
399 for (i=0 ; i<tdo->numread ; i++) |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
400 Py_CLEAR(tdo->values[i]); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
401 Py_CLEAR(tdo->nextlink); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
402 return 0; |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
403 } |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
404 |
| 30764 | 405 static void |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
406 teedataobject_dealloc(teedataobject *tdo) |
| 30764 | 407 { |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
408 PyObject_GC_UnTrack(tdo); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
409 teedataobject_clear(tdo); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
410 PyObject_GC_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 = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
416 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
417 "itertools.tee_dataobject", /* tp_name */ |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
418 sizeof(teedataobject), /* tp_basicsize */ |
| 30764 | 419 0, /* tp_itemsize */ |
| 420 /* methods */ | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
421 (destructor)teedataobject_dealloc, /* tp_dealloc */ |
| 30764 | 422 0, /* tp_print */ |
| 423 0, /* tp_getattr */ | |
| 424 0, /* tp_setattr */ | |
| 425 0, /* tp_compare */ | |
| 426 0, /* tp_repr */ | |
| 427 0, /* tp_as_number */ | |
| 428 0, /* tp_as_sequence */ | |
| 429 0, /* tp_as_mapping */ | |
| 430 0, /* tp_hash */ | |
| 431 0, /* tp_call */ | |
| 432 0, /* tp_str */ | |
| 433 PyObject_GenericGetAttr, /* tp_getattro */ | |
| 434 0, /* tp_setattro */ | |
| 435 0, /* tp_as_buffer */ | |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
436 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
437 teedataobject_doc, /* tp_doc */ |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
438 (traverseproc)teedataobject_traverse, /* tp_traverse */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
439 (inquiry)teedataobject_clear, /* tp_clear */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
440 0, /* tp_richcompare */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
441 0, /* tp_weaklistoffset */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
442 0, /* tp_iter */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
443 0, /* tp_iternext */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
444 0, /* tp_methods */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
445 0, /* tp_members */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
446 0, /* tp_getset */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
447 0, /* tp_base */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
448 0, /* tp_dict */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
449 0, /* tp_descr_get */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
450 0, /* tp_descr_set */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
451 0, /* tp_dictoffset */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
452 0, /* tp_init */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
453 0, /* tp_alloc */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
454 0, /* tp_new */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
455 PyObject_GC_Del, /* tp_free */ |
| 30764 | 456 }; |
| 457 | |
| 458 | |
| 459 static PyTypeObject tee_type; | |
| 460 | |
| 461 static PyObject * | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
462 tee_next(teeobject *to) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
463 { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
464 PyObject *value, *link; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
465 |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
466 if (to->index >= LINKCELLS) { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
467 link = teedataobject_jumplink(to->dataobj); |
|
39144
e9773fc27e9b
nextlink can be NULL if teedataobject_new fails, so use XINCREF.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38420
diff
changeset
|
468 Py_DECREF(to->dataobj); |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
469 to->dataobj = (teedataobject *)link; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
470 to->index = 0; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
471 } |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
472 value = teedataobject_getitem(to->dataobj, to->index); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
473 if (value == NULL) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
474 return NULL; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
475 to->index++; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
476 return value; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
477 } |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
478 |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
479 static int |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
480 tee_traverse(teeobject *to, visitproc visit, void *arg) |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
481 { |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
482 Py_VISIT((PyObject *)to->dataobj); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
483 return 0; |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
484 } |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
485 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
486 static PyObject * |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
487 tee_copy(teeobject *to) |
| 30764 | 488 { |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
489 teeobject *newto; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
490 |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
491 newto = PyObject_GC_New(teeobject, &tee_type); |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
492 if (newto == NULL) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
493 return NULL; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
494 Py_INCREF(to->dataobj); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
495 newto->dataobj = to->dataobj; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
496 newto->index = to->index; |
|
33777
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33646
diff
changeset
|
497 newto->weakreflist = NULL; |
|
37757
e1388aba9896
Add missing PyObject_GC_Track call, causing *some* itertools.tee objects to
Thomas Wouters <thomas@python.org>
parents:
37248
diff
changeset
|
498 PyObject_GC_Track(newto); |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
499 return (PyObject *)newto; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
500 } |
|
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 PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator."); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
503 |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
504 static PyObject * |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
505 tee_fromiterable(PyObject *iterable) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
506 { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
507 teeobject *to; |
| 30764 | 508 PyObject *it = NULL; |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
509 |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
510 it = PyObject_GetIter(iterable); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
511 if (it == NULL) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
512 return NULL; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
513 if (PyObject_TypeCheck(it, &tee_type)) { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
514 to = (teeobject *)tee_copy((teeobject *)it); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
515 goto done; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
516 } |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
517 |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
518 to = PyObject_GC_New(teeobject, &tee_type); |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
519 if (to == NULL) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
520 goto done; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
521 to->dataobj = (teedataobject *)teedataobject_new(it); |
|
39144
e9773fc27e9b
nextlink can be NULL if teedataobject_new fails, so use XINCREF.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38420
diff
changeset
|
522 if (!to->dataobj) { |
|
e9773fc27e9b
nextlink can be NULL if teedataobject_new fails, so use XINCREF.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38420
diff
changeset
|
523 PyObject_GC_Del(to); |
|
e9773fc27e9b
nextlink can be NULL if teedataobject_new fails, so use XINCREF.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38420
diff
changeset
|
524 to = NULL; |
|
e9773fc27e9b
nextlink can be NULL if teedataobject_new fails, so use XINCREF.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38420
diff
changeset
|
525 goto done; |
|
e9773fc27e9b
nextlink can be NULL if teedataobject_new fails, so use XINCREF.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38420
diff
changeset
|
526 } |
|
e9773fc27e9b
nextlink can be NULL if teedataobject_new fails, so use XINCREF.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38420
diff
changeset
|
527 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
528 to->index = 0; |
|
33777
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33646
diff
changeset
|
529 to->weakreflist = NULL; |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
530 PyObject_GC_Track(to); |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
531 done: |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
532 Py_XDECREF(it); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
533 return (PyObject *)to; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
534 } |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
535 |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
536 static PyObject * |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
537 tee_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
538 { |
| 30764 | 539 PyObject *iterable; |
| 540 | |
| 541 if (!PyArg_UnpackTuple(args, "tee", 1, 1, &iterable)) | |
| 542 return NULL; | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
543 return tee_fromiterable(iterable); |
| 30764 | 544 } |
| 545 | |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
546 static int |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
547 tee_clear(teeobject *to) |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
548 { |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
549 if (to->weakreflist != NULL) |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
550 PyObject_ClearWeakRefs((PyObject *) to); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
551 Py_CLEAR(to->dataobj); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
552 return 0; |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
553 } |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
554 |
| 30764 | 555 static void |
| 556 tee_dealloc(teeobject *to) | |
| 557 { | |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
558 PyObject_GC_UnTrack(to); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
559 tee_clear(to); |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
560 PyObject_GC_Del(to); |
| 30764 | 561 } |
| 562 | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
563 PyDoc_STRVAR(teeobject_doc, |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
564 "Iterator wrapped to make it copyable"); |
| 30764 | 565 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
566 static PyMethodDef tee_methods[] = { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
567 {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc}, |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
568 {NULL, NULL} /* sentinel */ |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
569 }; |
| 30764 | 570 |
| 571 static PyTypeObject tee_type = { | |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
572 PyVarObject_HEAD_INIT(NULL, 0) |
| 30764 | 573 "itertools.tee", /* tp_name */ |
| 574 sizeof(teeobject), /* tp_basicsize */ | |
| 575 0, /* tp_itemsize */ | |
| 576 /* methods */ | |
| 577 (destructor)tee_dealloc, /* tp_dealloc */ | |
| 578 0, /* tp_print */ | |
| 579 0, /* tp_getattr */ | |
| 580 0, /* tp_setattr */ | |
| 581 0, /* tp_compare */ | |
| 582 0, /* tp_repr */ | |
| 583 0, /* tp_as_number */ | |
| 584 0, /* tp_as_sequence */ | |
| 585 0, /* tp_as_mapping */ | |
| 586 0, /* tp_hash */ | |
| 587 0, /* tp_call */ | |
| 588 0, /* tp_str */ | |
|
30786
225d848a5f96
Minor improvements to itertools.tee():
Raymond Hettinger <python@rcn.com>
parents:
30773
diff
changeset
|
589 0, /* tp_getattro */ |
| 30764 | 590 0, /* tp_setattro */ |
| 591 0, /* tp_as_buffer */ | |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
592 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
593 teeobject_doc, /* tp_doc */ |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
594 (traverseproc)tee_traverse, /* tp_traverse */ |
|
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
595 (inquiry)tee_clear, /* tp_clear */ |
| 30764 | 596 0, /* tp_richcompare */ |
|
33777
e4b7c923ed5f
Fix and test weak referencing of itertools.tee objects.
Raymond Hettinger <python@rcn.com>
parents:
33646
diff
changeset
|
597 offsetof(teeobject, weakreflist), /* tp_weaklistoffset */ |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
598 PyObject_SelfIter, /* tp_iter */ |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
599 (iternextfunc)tee_next, /* tp_iternext */ |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
600 tee_methods, /* tp_methods */ |
| 30764 | 601 0, /* tp_members */ |
| 602 0, /* tp_getset */ | |
| 603 0, /* tp_base */ | |
| 604 0, /* tp_dict */ | |
| 605 0, /* tp_descr_get */ | |
| 606 0, /* tp_descr_set */ | |
| 607 0, /* tp_dictoffset */ | |
| 608 0, /* tp_init */ | |
| 609 0, /* tp_alloc */ | |
| 610 tee_new, /* tp_new */ | |
|
37248
00e9d59c23b4
Make itertools.tee and its internal teedataobject participate in GC. This
Thomas Wouters <thomas@python.org>
parents:
36684
diff
changeset
|
611 PyObject_GC_Del, /* tp_free */ |
| 30764 | 612 }; |
| 613 | |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
614 static PyObject * |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
615 tee(PyObject *self, PyObject *args) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
616 { |
|
39720
1352ad9afcc0
Bug #1550714: fix SystemError from itertools.tee on negative value for n.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39144
diff
changeset
|
617 Py_ssize_t i, n=2; |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
618 PyObject *it, *iterable, *copyable, *result; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
619 |
|
39720
1352ad9afcc0
Bug #1550714: fix SystemError from itertools.tee on negative value for n.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39144
diff
changeset
|
620 if (!PyArg_ParseTuple(args, "O|n", &iterable, &n)) |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
621 return NULL; |
|
39720
1352ad9afcc0
Bug #1550714: fix SystemError from itertools.tee on negative value for n.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39144
diff
changeset
|
622 if (n < 0) { |
|
1352ad9afcc0
Bug #1550714: fix SystemError from itertools.tee on negative value for n.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39144
diff
changeset
|
623 PyErr_SetString(PyExc_ValueError, "n must be >= 0"); |
|
1352ad9afcc0
Bug #1550714: fix SystemError from itertools.tee on negative value for n.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39144
diff
changeset
|
624 return NULL; |
|
1352ad9afcc0
Bug #1550714: fix SystemError from itertools.tee on negative value for n.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39144
diff
changeset
|
625 } |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
626 result = PyTuple_New(n); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
627 if (result == NULL) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
628 return NULL; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
629 if (n == 0) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
630 return result; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
631 it = PyObject_GetIter(iterable); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
632 if (it == NULL) { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
633 Py_DECREF(result); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
634 return NULL; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
635 } |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
636 if (!PyObject_HasAttrString(it, "__copy__")) { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
637 copyable = tee_fromiterable(it); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
638 Py_DECREF(it); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
639 if (copyable == NULL) { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
640 Py_DECREF(result); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
641 return NULL; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
642 } |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
643 } else |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
644 copyable = it; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
645 PyTuple_SET_ITEM(result, 0, copyable); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
646 for (i=1 ; i<n ; i++) { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
647 copyable = PyObject_CallMethod(copyable, "__copy__", NULL); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
648 if (copyable == NULL) { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
649 Py_DECREF(result); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
650 return NULL; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
651 } |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
652 PyTuple_SET_ITEM(result, i, copyable); |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
653 } |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
654 return result; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
655 } |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
656 |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
657 PyDoc_STRVAR(tee_doc, |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
658 "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
|
659 |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
660 |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
661 /* cycle object **********************************************************/ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
662 |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
663 typedef struct { |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
664 PyObject_HEAD |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
665 PyObject *it; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
666 PyObject *saved; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
667 int firstpass; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
668 } cycleobject; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
669 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
670 static PyTypeObject cycle_type; |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
671 |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
672 static PyObject * |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
673 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
|
674 { |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
675 PyObject *it; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
676 PyObject *iterable; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
677 PyObject *saved; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
678 cycleobject *lz; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
679 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
680 if (type == &cycle_type && !_PyArg_NoKeywords("cycle()", kwds)) |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
681 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
682 |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
683 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
|
684 return NULL; |
|
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 /* Get iterator. */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
687 it = PyObject_GetIter(iterable); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
688 if (it == NULL) |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
689 return NULL; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
690 |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
691 saved = PyList_New(0); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
692 if (saved == NULL) { |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
693 Py_DECREF(it); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
694 return NULL; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
695 } |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
696 |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
697 /* create cycleobject structure */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
698 lz = (cycleobject *)type->tp_alloc(type, 0); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
699 if (lz == NULL) { |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
700 Py_DECREF(it); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
701 Py_DECREF(saved); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
702 return NULL; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
703 } |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
704 lz->it = it; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
705 lz->saved = saved; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
706 lz->firstpass = 0; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
707 |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
708 return (PyObject *)lz; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
709 } |
|
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 static void |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
712 cycle_dealloc(cycleobject *lz) |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
713 { |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
714 PyObject_GC_UnTrack(lz); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
715 Py_XDECREF(lz->saved); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
716 Py_XDECREF(lz->it); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
717 Py_TYPE(lz)->tp_free(lz); |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
718 } |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
719 |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
720 static int |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
721 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
|
722 { |
|
32682
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
723 Py_VISIT(lz->it); |
|
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
724 Py_VISIT(lz->saved); |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
725 return 0; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
726 } |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
727 |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
728 static PyObject * |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
729 cycle_next(cycleobject *lz) |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
730 { |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
731 PyObject *item; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
732 PyObject *it; |
|
33646
3d8d78217599
Replace structure member before decreffing.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
733 PyObject *tmp; |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
734 |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
735 while (1) { |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
736 item = PyIter_Next(lz->it); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
737 if (item != NULL) { |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
738 if (!lz->firstpass) |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
739 PyList_Append(lz->saved, item); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
740 return item; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
741 } |
|
32010
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31909
diff
changeset
|
742 if (PyErr_Occurred()) { |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31909
diff
changeset
|
743 if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31909
diff
changeset
|
744 PyErr_Clear(); |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31909
diff
changeset
|
745 else |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31909
diff
changeset
|
746 return NULL; |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31909
diff
changeset
|
747 } |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
748 if (PyList_Size(lz->saved) == 0) |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
749 return NULL; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
750 it = PyObject_GetIter(lz->saved); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
751 if (it == NULL) |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
752 return NULL; |
|
33646
3d8d78217599
Replace structure member before decreffing.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
753 tmp = lz->it; |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
754 lz->it = it; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
755 lz->firstpass = 1; |
|
33646
3d8d78217599
Replace structure member before decreffing.
Raymond Hettinger <python@rcn.com>
parents:
33616
diff
changeset
|
756 Py_DECREF(tmp); |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
757 } |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
758 } |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
759 |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
760 PyDoc_STRVAR(cycle_doc, |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
761 "cycle(iterable) --> cycle object\n\ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
762 \n\ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
763 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
|
764 Then repeat the sequence indefinitely."); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
765 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
766 static PyTypeObject cycle_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
767 PyVarObject_HEAD_INIT(NULL, 0) |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
768 "itertools.cycle", /* tp_name */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
769 sizeof(cycleobject), /* tp_basicsize */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
770 0, /* tp_itemsize */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
771 /* methods */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
772 (destructor)cycle_dealloc, /* tp_dealloc */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
773 0, /* tp_print */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
774 0, /* tp_getattr */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
775 0, /* tp_setattr */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
776 0, /* tp_compare */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
777 0, /* tp_repr */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
778 0, /* tp_as_number */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
779 0, /* tp_as_sequence */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
780 0, /* tp_as_mapping */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
781 0, /* tp_hash */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
782 0, /* tp_call */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
783 0, /* tp_str */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
784 PyObject_GenericGetAttr, /* tp_getattro */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
785 0, /* tp_setattro */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
786 0, /* tp_as_buffer */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
787 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
788 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
789 cycle_doc, /* tp_doc */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
790 (traverseproc)cycle_traverse, /* tp_traverse */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
791 0, /* tp_clear */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
792 0, /* tp_richcompare */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
793 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
794 PyObject_SelfIter, /* tp_iter */ |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
795 (iternextfunc)cycle_next, /* tp_iternext */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
796 0, /* tp_methods */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
797 0, /* tp_members */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
798 0, /* tp_getset */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
799 0, /* tp_base */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
800 0, /* tp_dict */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
801 0, /* tp_descr_get */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
802 0, /* tp_descr_set */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
803 0, /* tp_dictoffset */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
804 0, /* tp_init */ |
|
29242
d72ad07c739e
PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents:
29225
diff
changeset
|
805 0, /* tp_alloc */ |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
806 cycle_new, /* tp_new */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
807 PyObject_GC_Del, /* tp_free */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
808 }; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
809 |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
810 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
811 /* dropwhile object **********************************************************/ |
|
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 typedef struct { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
814 PyObject_HEAD |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
815 PyObject *func; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
816 PyObject *it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
817 long start; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
818 } dropwhileobject; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
819 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
820 static PyTypeObject dropwhile_type; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
821 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
822 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
823 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
|
824 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
825 PyObject *func, *seq; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
826 PyObject *it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
827 dropwhileobject *lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
828 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
829 if (type == &dropwhile_type && !_PyArg_NoKeywords("dropwhile()", kwds)) |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
830 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
831 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
832 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
|
833 return NULL; |
|
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 /* Get iterator. */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
836 it = PyObject_GetIter(seq); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
837 if (it == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
838 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
839 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
840 /* create dropwhileobject structure */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
841 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
|
842 if (lz == NULL) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
843 Py_DECREF(it); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
844 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
845 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
846 Py_INCREF(func); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
847 lz->func = func; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
848 lz->it = it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
849 lz->start = 0; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
850 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
851 return (PyObject *)lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
852 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
853 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
854 static void |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
855 dropwhile_dealloc(dropwhileobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
856 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
857 PyObject_GC_UnTrack(lz); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
858 Py_XDECREF(lz->func); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
859 Py_XDECREF(lz->it); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
860 Py_TYPE(lz)->tp_free(lz); |
|
27570
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 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
863 static int |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
864 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
|
865 { |
|
32682
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
866 Py_VISIT(lz->it); |
|
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
867 Py_VISIT(lz->func); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
868 return 0; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
869 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
870 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
871 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
872 dropwhile_next(dropwhileobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
873 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
874 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
|
875 PyObject *it = lz->it; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
876 long ok; |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
877 PyObject *(*iternext)(PyObject *); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
878 |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
879 assert(PyIter_Check(it)); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
880 iternext = *Py_TYPE(it)->tp_iternext; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
881 for (;;) { |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
882 item = iternext(it); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
883 if (item == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
884 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
885 if (lz->start == 1) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
886 return item; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
887 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
888 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
|
889 if (good == NULL) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
890 Py_DECREF(item); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
891 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
892 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
893 ok = PyObject_IsTrue(good); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
894 Py_DECREF(good); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
895 if (!ok) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
896 lz->start = 1; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
897 return item; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
898 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
899 Py_DECREF(item); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
900 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
901 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
902 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
903 PyDoc_STRVAR(dropwhile_doc, |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
904 "dropwhile(predicate, iterable) --> dropwhile object\n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
905 \n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
906 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
|
907 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
|
908 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
909 static PyTypeObject dropwhile_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
910 PyVarObject_HEAD_INIT(NULL, 0) |
| 27769 | 911 "itertools.dropwhile", /* tp_name */ |
| 912 sizeof(dropwhileobject), /* tp_basicsize */ | |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
913 0, /* tp_itemsize */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
914 /* methods */ |
| 27769 | 915 (destructor)dropwhile_dealloc, /* tp_dealloc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
916 0, /* tp_print */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
917 0, /* tp_getattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
918 0, /* tp_setattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
919 0, /* tp_compare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
920 0, /* tp_repr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
921 0, /* tp_as_number */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
922 0, /* tp_as_sequence */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
923 0, /* tp_as_mapping */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
924 0, /* tp_hash */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
925 0, /* tp_call */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
926 0, /* tp_str */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
927 PyObject_GenericGetAttr, /* tp_getattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
928 0, /* tp_setattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
929 0, /* tp_as_buffer */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
930 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
931 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 27769 | 932 dropwhile_doc, /* tp_doc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
933 (traverseproc)dropwhile_traverse, /* tp_traverse */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
934 0, /* tp_clear */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
935 0, /* tp_richcompare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
936 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
937 PyObject_SelfIter, /* tp_iter */ |
| 27769 | 938 (iternextfunc)dropwhile_next, /* tp_iternext */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
939 0, /* tp_methods */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
940 0, /* tp_members */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
941 0, /* tp_getset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
942 0, /* tp_base */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
943 0, /* tp_dict */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
944 0, /* tp_descr_get */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
945 0, /* tp_descr_set */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
946 0, /* tp_dictoffset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
947 0, /* tp_init */ |
|
29242
d72ad07c739e
PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents:
29225
diff
changeset
|
948 0, /* tp_alloc */ |
| 27769 | 949 dropwhile_new, /* tp_new */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
950 PyObject_GC_Del, /* tp_free */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
951 }; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
952 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
953 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
954 /* takewhile object **********************************************************/ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
955 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
956 typedef struct { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
957 PyObject_HEAD |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
958 PyObject *func; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
959 PyObject *it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
960 long stop; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
961 } takewhileobject; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
962 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
963 static PyTypeObject takewhile_type; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
964 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
965 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
966 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
|
967 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
968 PyObject *func, *seq; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
969 PyObject *it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
970 takewhileobject *lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
971 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
972 if (type == &takewhile_type && !_PyArg_NoKeywords("takewhile()", kwds)) |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
973 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
974 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
975 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
|
976 return NULL; |
|
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 /* Get iterator. */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
979 it = PyObject_GetIter(seq); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
980 if (it == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
981 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
982 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
983 /* create takewhileobject structure */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
984 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
|
985 if (lz == NULL) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
986 Py_DECREF(it); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
987 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
988 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
989 Py_INCREF(func); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
990 lz->func = func; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
991 lz->it = it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
992 lz->stop = 0; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
993 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
994 return (PyObject *)lz; |
|
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 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
997 static void |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
998 takewhile_dealloc(takewhileobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
999 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1000 PyObject_GC_UnTrack(lz); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1001 Py_XDECREF(lz->func); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1002 Py_XDECREF(lz->it); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
1003 Py_TYPE(lz)->tp_free(lz); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1004 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1005 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1006 static int |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1007 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
|
1008 { |
|
32682
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
1009 Py_VISIT(lz->it); |
|
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
1010 Py_VISIT(lz->func); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1011 return 0; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1012 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1013 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1014 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1015 takewhile_next(takewhileobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1016 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1017 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
|
1018 PyObject *it = lz->it; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1019 long ok; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1020 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1021 if (lz->stop == 1) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1022 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1023 |
|
28117
9b4eed4bb3e2
Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents:
28024
diff
changeset
|
1024 assert(PyIter_Check(it)); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
1025 item = (*Py_TYPE(it)->tp_iternext)(it); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1026 if (item == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1027 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1028 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1029 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
|
1030 if (good == NULL) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1031 Py_DECREF(item); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1032 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1033 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1034 ok = PyObject_IsTrue(good); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1035 Py_DECREF(good); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1036 if (ok) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1037 return item; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1038 Py_DECREF(item); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1039 lz->stop = 1; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1040 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1041 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1042 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1043 PyDoc_STRVAR(takewhile_doc, |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1044 "takewhile(predicate, iterable) --> takewhile object\n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1045 \n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1046 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
|
1047 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
|
1048 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1049 static PyTypeObject takewhile_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
1050 PyVarObject_HEAD_INIT(NULL, 0) |
| 27769 | 1051 "itertools.takewhile", /* tp_name */ |
| 1052 sizeof(takewhileobject), /* tp_basicsize */ | |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1053 0, /* tp_itemsize */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1054 /* methods */ |
| 27769 | 1055 (destructor)takewhile_dealloc, /* tp_dealloc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1056 0, /* tp_print */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1057 0, /* tp_getattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1058 0, /* tp_setattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1059 0, /* tp_compare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1060 0, /* tp_repr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1061 0, /* tp_as_number */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1062 0, /* tp_as_sequence */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1063 0, /* tp_as_mapping */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1064 0, /* tp_hash */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1065 0, /* tp_call */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1066 0, /* tp_str */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1067 PyObject_GenericGetAttr, /* tp_getattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1068 0, /* tp_setattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1069 0, /* tp_as_buffer */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1070 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
|
1071 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 27769 | 1072 takewhile_doc, /* tp_doc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1073 (traverseproc)takewhile_traverse, /* tp_traverse */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1074 0, /* tp_clear */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1075 0, /* tp_richcompare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1076 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
1077 PyObject_SelfIter, /* tp_iter */ |
| 27769 | 1078 (iternextfunc)takewhile_next, /* tp_iternext */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1079 0, /* tp_methods */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1080 0, /* tp_members */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1081 0, /* tp_getset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1082 0, /* tp_base */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1083 0, /* tp_dict */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1084 0, /* tp_descr_get */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1085 0, /* tp_descr_set */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1086 0, /* tp_dictoffset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1087 0, /* tp_init */ |
|
29242
d72ad07c739e
PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents:
29225
diff
changeset
|
1088 0, /* tp_alloc */ |
| 27769 | 1089 takewhile_new, /* tp_new */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1090 PyObject_GC_Del, /* tp_free */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1091 }; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1092 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1093 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1094 /* islice object ************************************************************/ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1095 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1096 typedef struct { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1097 PyObject_HEAD |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1098 PyObject *it; |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1099 Py_ssize_t next; |
|
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1100 Py_ssize_t stop; |
|
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1101 Py_ssize_t step; |
|
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1102 Py_ssize_t cnt; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1103 } isliceobject; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1104 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1105 static PyTypeObject islice_type; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1106 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1107 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1108 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
|
1109 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1110 PyObject *seq; |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1111 Py_ssize_t start=0, stop=-1, step=1; |
|
34108
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1112 PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL; |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
1113 Py_ssize_t numargs; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1114 isliceobject *lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1115 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1116 if (type == &islice_type && !_PyArg_NoKeywords("islice()", kwds)) |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
1117 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
1118 |
|
34108
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1119 if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3)) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1120 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1121 |
|
34108
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1122 numargs = PyTuple_Size(args); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1123 if (numargs == 2) { |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1124 if (a1 != Py_None) { |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1125 stop = PyInt_AsSsize_t(a1); |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1126 if (stop == -1) { |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1127 if (PyErr_Occurred()) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1128 PyErr_Clear(); |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1129 PyErr_SetString(PyExc_ValueError, |
|
34108
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1130 "Stop argument for islice() 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
|
1131 return NULL; |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1132 } |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1133 } |
|
28930
18082b2d2ebd
The previous made the stop argument optional.
Raymond Hettinger <python@rcn.com>
parents:
28929
diff
changeset
|
1134 } else { |
|
34108
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1135 if (a1 != Py_None) |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1136 start = PyInt_AsSsize_t(a1); |
|
34108
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1137 if (start == -1 && PyErr_Occurred()) |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1138 PyErr_Clear(); |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1139 if (a2 != Py_None) { |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1140 stop = PyInt_AsSsize_t(a2); |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1141 if (stop == -1) { |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1142 if (PyErr_Occurred()) |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1143 PyErr_Clear(); |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1144 PyErr_SetString(PyExc_ValueError, |
|
34108
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1145 "Stop argument for islice() 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
|
1146 return NULL; |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1147 } |
|
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1148 } |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1149 } |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1150 if (start<0 || stop<-1) { |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1151 PyErr_SetString(PyExc_ValueError, |
|
34108
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1152 "Indices for islice() must be non-negative integers or None."); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1153 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1154 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1155 |
|
34108
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1156 if (a3 != NULL) { |
|
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1157 if (a3 != Py_None) |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1158 step = PyInt_AsSsize_t(a3); |
|
34108
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1159 if (step == -1 && PyErr_Occurred()) |
|
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1160 PyErr_Clear(); |
|
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1161 } |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1162 if (step<1) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1163 PyErr_SetString(PyExc_ValueError, |
|
34108
1d1d6377acb1
Added optional None arguments to itertools.islice().
Raymond Hettinger <python@rcn.com>
parents:
33779
diff
changeset
|
1164 "Step for islice() must be a positive integer or None."); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1165 return NULL; |
|
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 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1168 /* Get iterator. */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1169 it = PyObject_GetIter(seq); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1170 if (it == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1171 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1172 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1173 /* create isliceobject structure */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1174 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
|
1175 if (lz == NULL) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1176 Py_DECREF(it); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1177 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1178 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1179 lz->it = it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1180 lz->next = start; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1181 lz->stop = stop; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1182 lz->step = step; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1183 lz->cnt = 0L; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1184 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1185 return (PyObject *)lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1186 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1187 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1188 static void |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1189 islice_dealloc(isliceobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1190 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1191 PyObject_GC_UnTrack(lz); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1192 Py_XDECREF(lz->it); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
1193 Py_TYPE(lz)->tp_free(lz); |
|
27570
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 static int |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1197 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
|
1198 { |
|
32682
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
1199 Py_VISIT(lz->it); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1200 return 0; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1201 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1202 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1203 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1204 islice_next(isliceobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1205 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1206 PyObject *item; |
|
28117
9b4eed4bb3e2
Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents:
28024
diff
changeset
|
1207 PyObject *it = lz->it; |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1208 Py_ssize_t oldnext; |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
1209 PyObject *(*iternext)(PyObject *); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1210 |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
1211 assert(PyIter_Check(it)); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
1212 iternext = *Py_TYPE(it)->tp_iternext; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1213 while (lz->cnt < lz->next) { |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
1214 item = iternext(it); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1215 if (item == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1216 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1217 Py_DECREF(item); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1218 lz->cnt++; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1219 } |
|
28929
92a1e585abf7
SF bug #730685: itertools.islice stop argument is not optional
Raymond Hettinger <python@rcn.com>
parents:
28601
diff
changeset
|
1220 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
|
1221 return NULL; |
|
28117
9b4eed4bb3e2
Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents:
28024
diff
changeset
|
1222 assert(PyIter_Check(it)); |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
1223 item = iternext(it); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1224 if (item == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1225 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1226 lz->cnt++; |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1227 oldnext = lz->next; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1228 lz->next += lz->step; |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1229 if (lz->next < oldnext) /* Check for overflow */ |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1230 lz->next = lz->stop; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1231 return item; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1232 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1233 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1234 PyDoc_STRVAR(islice_doc, |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1235 "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
|
1236 \n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1237 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
|
1238 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
|
1239 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
|
1240 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
|
1241 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
|
1242 but returns an iterator."); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1243 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1244 static PyTypeObject islice_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
1245 PyVarObject_HEAD_INIT(NULL, 0) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1246 "itertools.islice", /* tp_name */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1247 sizeof(isliceobject), /* tp_basicsize */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1248 0, /* tp_itemsize */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1249 /* methods */ |
| 27769 | 1250 (destructor)islice_dealloc, /* tp_dealloc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1251 0, /* tp_print */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1252 0, /* tp_getattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1253 0, /* tp_setattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1254 0, /* tp_compare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1255 0, /* tp_repr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1256 0, /* tp_as_number */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1257 0, /* tp_as_sequence */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1258 0, /* tp_as_mapping */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1259 0, /* tp_hash */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1260 0, /* tp_call */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1261 0, /* tp_str */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1262 PyObject_GenericGetAttr, /* tp_getattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1263 0, /* tp_setattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1264 0, /* tp_as_buffer */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1265 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
|
1266 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 27769 | 1267 islice_doc, /* tp_doc */ |
| 1268 (traverseproc)islice_traverse, /* tp_traverse */ | |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1269 0, /* tp_clear */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1270 0, /* tp_richcompare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1271 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
1272 PyObject_SelfIter, /* tp_iter */ |
| 27769 | 1273 (iternextfunc)islice_next, /* tp_iternext */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1274 0, /* tp_methods */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1275 0, /* tp_members */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1276 0, /* tp_getset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1277 0, /* tp_base */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1278 0, /* tp_dict */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1279 0, /* tp_descr_get */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1280 0, /* tp_descr_set */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1281 0, /* tp_dictoffset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1282 0, /* tp_init */ |
|
29242
d72ad07c739e
PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents:
29225
diff
changeset
|
1283 0, /* tp_alloc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1284 islice_new, /* tp_new */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1285 PyObject_GC_Del, /* tp_free */ |
|
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 |
|
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 /* starmap object ************************************************************/ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1290 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1291 typedef struct { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1292 PyObject_HEAD |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1293 PyObject *func; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1294 PyObject *it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1295 } starmapobject; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1296 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1297 static PyTypeObject starmap_type; |
|
27570
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 PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1300 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
|
1301 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1302 PyObject *func, *seq; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1303 PyObject *it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1304 starmapobject *lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1305 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1306 if (type == &starmap_type && !_PyArg_NoKeywords("starmap()", kwds)) |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
1307 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
1308 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1309 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
|
1310 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1311 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1312 /* Get iterator. */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1313 it = PyObject_GetIter(seq); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1314 if (it == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1315 return NULL; |
|
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 /* create starmapobject structure */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1318 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
|
1319 if (lz == NULL) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1320 Py_DECREF(it); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1321 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1322 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1323 Py_INCREF(func); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1324 lz->func = func; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1325 lz->it = it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1326 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1327 return (PyObject *)lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1328 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1329 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1330 static void |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1331 starmap_dealloc(starmapobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1332 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1333 PyObject_GC_UnTrack(lz); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1334 Py_XDECREF(lz->func); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1335 Py_XDECREF(lz->it); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
1336 Py_TYPE(lz)->tp_free(lz); |
|
27570
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 static int |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1340 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
|
1341 { |
|
32682
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
1342 Py_VISIT(lz->it); |
|
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
1343 Py_VISIT(lz->func); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1344 return 0; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1345 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1346 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1347 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1348 starmap_next(starmapobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1349 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1350 PyObject *args; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1351 PyObject *result; |
|
28117
9b4eed4bb3e2
Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents:
28024
diff
changeset
|
1352 PyObject *it = lz->it; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1353 |
|
28117
9b4eed4bb3e2
Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents:
28024
diff
changeset
|
1354 assert(PyIter_Check(it)); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
1355 args = (*Py_TYPE(it)->tp_iternext)(it); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1356 if (args == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1357 return NULL; |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1358 if (!PyTuple_CheckExact(args)) { |
|
44717
d0f7963cf8b0
Make starmap() match its pure python definition and accept any itertable input (not just tuples).
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1359 PyObject *newargs = PySequence_Tuple(args); |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1360 Py_DECREF(args); |
|
44717
d0f7963cf8b0
Make starmap() match its pure python definition and accept any itertable input (not just tuples).
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1361 if (newargs == NULL) |
|
d0f7963cf8b0
Make starmap() match its pure python definition and accept any itertable input (not just tuples).
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1362 return NULL; |
|
d0f7963cf8b0
Make starmap() match its pure python definition and accept any itertable input (not just tuples).
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1363 args = newargs; |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1364 } |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1365 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
|
1366 Py_DECREF(args); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1367 return result; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1368 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1369 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1370 PyDoc_STRVAR(starmap_doc, |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1371 "starmap(function, sequence) --> starmap object\n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1372 \n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1373 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
|
1374 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
|
1375 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1376 static PyTypeObject starmap_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
1377 PyVarObject_HEAD_INIT(NULL, 0) |
| 27769 | 1378 "itertools.starmap", /* tp_name */ |
| 1379 sizeof(starmapobject), /* tp_basicsize */ | |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1380 0, /* tp_itemsize */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1381 /* methods */ |
| 27769 | 1382 (destructor)starmap_dealloc, /* tp_dealloc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1383 0, /* tp_print */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1384 0, /* tp_getattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1385 0, /* tp_setattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1386 0, /* tp_compare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1387 0, /* tp_repr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1388 0, /* tp_as_number */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1389 0, /* tp_as_sequence */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1390 0, /* tp_as_mapping */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1391 0, /* tp_hash */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1392 0, /* tp_call */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1393 0, /* tp_str */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1394 PyObject_GenericGetAttr, /* tp_getattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1395 0, /* tp_setattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1396 0, /* tp_as_buffer */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1397 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
|
1398 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 27769 | 1399 starmap_doc, /* tp_doc */ |
| 1400 (traverseproc)starmap_traverse, /* tp_traverse */ | |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1401 0, /* tp_clear */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1402 0, /* tp_richcompare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1403 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
1404 PyObject_SelfIter, /* tp_iter */ |
| 27769 | 1405 (iternextfunc)starmap_next, /* tp_iternext */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1406 0, /* tp_methods */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1407 0, /* tp_members */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1408 0, /* tp_getset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1409 0, /* tp_base */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1410 0, /* tp_dict */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1411 0, /* tp_descr_get */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1412 0, /* tp_descr_set */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1413 0, /* tp_dictoffset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1414 0, /* tp_init */ |
|
29242
d72ad07c739e
PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents:
29225
diff
changeset
|
1415 0, /* tp_alloc */ |
| 27769 | 1416 starmap_new, /* tp_new */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1417 PyObject_GC_Del, /* tp_free */ |
|
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 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1420 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1421 /* imap object ************************************************************/ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1422 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1423 typedef struct { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1424 PyObject_HEAD |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1425 PyObject *iters; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1426 PyObject *func; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1427 } imapobject; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1428 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1429 static PyTypeObject imap_type; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1430 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1431 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1432 imap_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
|
1433 { |
|
27748
59ae41e04ffb
* Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents:
27747
diff
changeset
|
1434 PyObject *it, *iters, *func; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1435 imapobject *lz; |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
1436 Py_ssize_t numargs, i; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1437 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1438 if (type == &imap_type && !_PyArg_NoKeywords("imap()", kwds)) |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
1439 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
1440 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1441 numargs = PyTuple_Size(args); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1442 if (numargs < 2) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1443 PyErr_SetString(PyExc_TypeError, |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1444 "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
|
1445 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1446 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1447 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1448 iters = PyTuple_New(numargs-1); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1449 if (iters == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1450 return NULL; |
|
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 for (i=1 ; i<numargs ; i++) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1453 /* Get iterator. */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1454 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
|
1455 if (it == NULL) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1456 Py_DECREF(iters); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1457 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1458 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1459 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
|
1460 } |
|
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 /* create imapobject structure */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1463 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
|
1464 if (lz == NULL) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1465 Py_DECREF(iters); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1466 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1467 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1468 lz->iters = iters; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1469 func = PyTuple_GET_ITEM(args, 0); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1470 Py_INCREF(func); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1471 lz->func = func; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1472 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1473 return (PyObject *)lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1474 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1475 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1476 static void |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1477 imap_dealloc(imapobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1478 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1479 PyObject_GC_UnTrack(lz); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1480 Py_XDECREF(lz->iters); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1481 Py_XDECREF(lz->func); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
1482 Py_TYPE(lz)->tp_free(lz); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1483 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1484 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1485 static int |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1486 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
|
1487 { |
|
32682
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
1488 Py_VISIT(lz->iters); |
|
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
1489 Py_VISIT(lz->func); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1490 return 0; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1491 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1492 |
|
27747
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 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
|
1495 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
|
1496 the following reasons: |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1497 |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1498 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
|
1499 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
|
1500 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
|
1501 infinite iterators like count() and repeat() (for supplying sequential |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1502 or constant arguments to a function). |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1503 |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1504 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
|
1505 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
|
1506 should not pass silently by automatically supplying None. |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1507 |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1508 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
|
1509 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
|
1510 None. |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1511 |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1512 4) If a need does arise, it can be met by __builtins__.map() or by |
| 29535 | 1513 writing: chain(iterable, repeat(None)). |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1514 |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1515 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
|
1516 */ |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1517 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1518 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1519 imap_next(imapobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1520 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1521 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
|
1522 PyObject *argtuple; |
|
59ae41e04ffb
* Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents:
27747
diff
changeset
|
1523 PyObject *result; |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
1524 Py_ssize_t numargs, i; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1525 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1526 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
|
1527 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
|
1528 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
|
1529 return NULL; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1530 |
|
27748
59ae41e04ffb
* Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents:
27747
diff
changeset
|
1531 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
|
1532 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
|
1533 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
|
1534 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
|
1535 return NULL; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1536 } |
|
27748
59ae41e04ffb
* Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents:
27747
diff
changeset
|
1537 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
|
1538 } |
|
59ae41e04ffb
* Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents:
27747
diff
changeset
|
1539 if (lz->func == Py_None) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1540 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
|
1541 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
|
1542 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
|
1543 return result; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1544 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1545 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1546 PyDoc_STRVAR(imap_doc, |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1547 "imap(func, *iterables) --> imap object\n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1548 \n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1549 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
|
1550 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
|
1551 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
|
1552 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
|
1553 iterables."); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1554 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1555 static PyTypeObject imap_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
1556 PyVarObject_HEAD_INIT(NULL, 0) |
| 27769 | 1557 "itertools.imap", /* tp_name */ |
| 1558 sizeof(imapobject), /* tp_basicsize */ | |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1559 0, /* tp_itemsize */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1560 /* methods */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1561 (destructor)imap_dealloc, /* tp_dealloc */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1562 0, /* tp_print */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1563 0, /* tp_getattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1564 0, /* tp_setattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1565 0, /* tp_compare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1566 0, /* tp_repr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1567 0, /* tp_as_number */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1568 0, /* tp_as_sequence */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1569 0, /* tp_as_mapping */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1570 0, /* tp_hash */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1571 0, /* tp_call */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1572 0, /* tp_str */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1573 PyObject_GenericGetAttr, /* tp_getattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1574 0, /* tp_setattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1575 0, /* tp_as_buffer */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1576 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
|
1577 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1578 imap_doc, /* tp_doc */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1579 (traverseproc)imap_traverse, /* tp_traverse */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1580 0, /* tp_clear */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1581 0, /* tp_richcompare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1582 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
1583 PyObject_SelfIter, /* tp_iter */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1584 (iternextfunc)imap_next, /* tp_iternext */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1585 0, /* tp_methods */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1586 0, /* tp_members */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1587 0, /* tp_getset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1588 0, /* tp_base */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1589 0, /* tp_dict */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1590 0, /* tp_descr_get */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1591 0, /* tp_descr_set */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1592 0, /* tp_dictoffset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1593 0, /* tp_init */ |
|
29242
d72ad07c739e
PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents:
29225
diff
changeset
|
1594 0, /* tp_alloc */ |
| 27769 | 1595 imap_new, /* tp_new */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1596 PyObject_GC_Del, /* tp_free */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1597 }; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1598 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1599 |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1600 /* chain object ************************************************************/ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1601 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1602 typedef struct { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1603 PyObject_HEAD |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1604 Py_ssize_t tuplesize; |
|
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1605 Py_ssize_t iternum; /* which iterator is active */ |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1606 PyObject *ittuple; /* tuple of iterators */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1607 } chainobject; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1608 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1609 static PyTypeObject chain_type; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1610 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1611 static PyObject * |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1612 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
|
1613 { |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1614 chainobject *lz; |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
1615 Py_ssize_t tuplesize = PySequence_Length(args); |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
1616 Py_ssize_t i; |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1617 PyObject *ittuple; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1618 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1619 if (type == &chain_type && !_PyArg_NoKeywords("chain()", kwds)) |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
1620 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
1621 |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1622 /* obtain iterators */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1623 assert(PyTuple_Check(args)); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1624 ittuple = PyTuple_New(tuplesize); |
|
38420
499cf7c2f777
Fix compiler warning (and whitespace) on Mac OS 10.4. (A lot of this code looked duplicated, I wonder if a utility function could help reduce the duplication here.)
Neal Norwitz <nnorwitz@gmail.com>
parents:
38341
diff
changeset
|
1625 if (ittuple == NULL) |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1626 return NULL; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1627 for (i=0; i < tuplesize; ++i) { |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1628 PyObject *item = PyTuple_GET_ITEM(args, i); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1629 PyObject *it = PyObject_GetIter(item); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1630 if (it == NULL) { |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1631 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1632 PyErr_Format(PyExc_TypeError, |
|
38420
499cf7c2f777
Fix compiler warning (and whitespace) on Mac OS 10.4. (A lot of this code looked duplicated, I wonder if a utility function could help reduce the duplication here.)
Neal Norwitz <nnorwitz@gmail.com>
parents:
38341
diff
changeset
|
1633 "chain argument #%zd must support iteration", |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1634 i+1); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1635 Py_DECREF(ittuple); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1636 return NULL; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1637 } |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1638 PyTuple_SET_ITEM(ittuple, i, it); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1639 } |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1640 |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1641 /* create chainobject structure */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1642 lz = (chainobject *)type->tp_alloc(type, 0); |
| 29522 | 1643 if (lz == NULL) { |
| 1644 Py_DECREF(ittuple); | |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1645 return NULL; |
| 29522 | 1646 } |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1647 |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1648 lz->ittuple = ittuple; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1649 lz->iternum = 0; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1650 lz->tuplesize = tuplesize; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1651 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1652 return (PyObject *)lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1653 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1654 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1655 static void |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1656 chain_dealloc(chainobject *lz) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1657 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1658 PyObject_GC_UnTrack(lz); |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1659 Py_XDECREF(lz->ittuple); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
1660 Py_TYPE(lz)->tp_free(lz); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1661 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1662 |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1663 static int |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1664 chain_traverse(chainobject *lz, visitproc visit, void *arg) |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1665 { |
|
32682
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
1666 Py_VISIT(lz->ittuple); |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1667 return 0; |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1668 } |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
1669 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1670 static PyObject * |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1671 chain_next(chainobject *lz) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1672 { |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1673 PyObject *it; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1674 PyObject *item; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1675 |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1676 while (lz->iternum < lz->tuplesize) { |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1677 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
|
1678 item = PyIter_Next(it); |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1679 if (item != NULL) |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1680 return item; |
|
32010
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31909
diff
changeset
|
1681 if (PyErr_Occurred()) { |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31909
diff
changeset
|
1682 if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31909
diff
changeset
|
1683 PyErr_Clear(); |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31909
diff
changeset
|
1684 else |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31909
diff
changeset
|
1685 return NULL; |
|
544f2172363d
SF #950057: itertools.chain doesn't "process" exceptions as they occur
Raymond Hettinger <python@rcn.com>
parents:
31909
diff
changeset
|
1686 } |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1687 lz->iternum++; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1688 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1689 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1690 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1691 |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1692 PyDoc_STRVAR(chain_doc, |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1693 "chain(*iterables) --> chain object\n\ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1694 \n\ |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1695 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
|
1696 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
|
1697 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
|
1698 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1699 static PyTypeObject chain_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
1700 PyVarObject_HEAD_INIT(NULL, 0) |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1701 "itertools.chain", /* tp_name */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1702 sizeof(chainobject), /* tp_basicsize */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1703 0, /* tp_itemsize */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1704 /* methods */ |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1705 (destructor)chain_dealloc, /* tp_dealloc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1706 0, /* tp_print */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1707 0, /* tp_getattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1708 0, /* tp_setattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1709 0, /* tp_compare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1710 0, /* tp_repr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1711 0, /* tp_as_number */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1712 0, /* tp_as_sequence */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1713 0, /* tp_as_mapping */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1714 0, /* tp_hash */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1715 0, /* tp_call */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1716 0, /* tp_str */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1717 PyObject_GenericGetAttr, /* tp_getattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1718 0, /* tp_setattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1719 0, /* tp_as_buffer */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1720 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
|
1721 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1722 chain_doc, /* tp_doc */ |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1723 (traverseproc)chain_traverse, /* tp_traverse */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1724 0, /* tp_clear */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1725 0, /* tp_richcompare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1726 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
1727 PyObject_SelfIter, /* tp_iter */ |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1728 (iternextfunc)chain_next, /* tp_iternext */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1729 0, /* tp_methods */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1730 0, /* tp_members */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1731 0, /* tp_getset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1732 0, /* tp_base */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1733 0, /* tp_dict */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1734 0, /* tp_descr_get */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1735 0, /* tp_descr_set */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1736 0, /* tp_dictoffset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1737 0, /* tp_init */ |
|
29242
d72ad07c739e
PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents:
29225
diff
changeset
|
1738 0, /* tp_alloc */ |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
1739 chain_new, /* tp_new */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1740 PyObject_GC_Del, /* tp_free */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1741 }; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1742 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1743 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1744 /* ifilter object ************************************************************/ |
|
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 typedef struct { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1747 PyObject_HEAD |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1748 PyObject *func; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1749 PyObject *it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1750 } ifilterobject; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1751 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1752 static PyTypeObject ifilter_type; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1753 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1754 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1755 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
|
1756 { |
| 27769 | 1757 PyObject *func, *seq; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1758 PyObject *it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1759 ifilterobject *lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1760 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1761 if (type == &ifilter_type && !_PyArg_NoKeywords("ifilter()", kwds)) |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
1762 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
1763 |
| 27769 | 1764 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
|
1765 return NULL; |
|
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 /* Get iterator. */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1768 it = PyObject_GetIter(seq); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1769 if (it == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1770 return NULL; |
|
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 /* create ifilterobject structure */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1773 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
|
1774 if (lz == NULL) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1775 Py_DECREF(it); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1776 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1777 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1778 Py_INCREF(func); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1779 lz->func = func; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1780 lz->it = it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1781 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1782 return (PyObject *)lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1783 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1784 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1785 static void |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1786 ifilter_dealloc(ifilterobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1787 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1788 PyObject_GC_UnTrack(lz); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1789 Py_XDECREF(lz->func); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1790 Py_XDECREF(lz->it); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
1791 Py_TYPE(lz)->tp_free(lz); |
|
27570
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 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1794 static int |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1795 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
|
1796 { |
|
32682
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
1797 Py_VISIT(lz->it); |
|
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
1798 Py_VISIT(lz->func); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1799 return 0; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1800 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1801 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1802 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1803 ifilter_next(ifilterobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1804 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1805 PyObject *item; |
|
28117
9b4eed4bb3e2
Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents:
28024
diff
changeset
|
1806 PyObject *it = lz->it; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1807 long ok; |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
1808 PyObject *(*iternext)(PyObject *); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1809 |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
1810 assert(PyIter_Check(it)); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
1811 iternext = *Py_TYPE(it)->tp_iternext; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1812 for (;;) { |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
1813 item = iternext(it); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1814 if (item == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1815 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1816 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1817 if (lz->func == Py_None) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1818 ok = PyObject_IsTrue(item); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1819 } else { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1820 PyObject *good; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1821 good = PyObject_CallFunctionObjArgs(lz->func, |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1822 item, NULL); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1823 if (good == NULL) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1824 Py_DECREF(item); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1825 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1826 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1827 ok = PyObject_IsTrue(good); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1828 Py_DECREF(good); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1829 } |
| 27769 | 1830 if (ok) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1831 return item; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1832 Py_DECREF(item); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1833 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1834 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1835 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1836 PyDoc_STRVAR(ifilter_doc, |
| 27769 | 1837 "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
|
1838 \n\ |
| 27769 | 1839 Return those items of sequence for which function(item) is true.\n\ |
| 1840 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
|
1841 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1842 static PyTypeObject ifilter_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
1843 PyVarObject_HEAD_INIT(NULL, 0) |
| 27769 | 1844 "itertools.ifilter", /* tp_name */ |
| 1845 sizeof(ifilterobject), /* tp_basicsize */ | |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1846 0, /* tp_itemsize */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1847 /* methods */ |
| 27769 | 1848 (destructor)ifilter_dealloc, /* tp_dealloc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1849 0, /* tp_print */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1850 0, /* tp_getattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1851 0, /* tp_setattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1852 0, /* tp_compare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1853 0, /* tp_repr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1854 0, /* tp_as_number */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1855 0, /* tp_as_sequence */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1856 0, /* tp_as_mapping */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1857 0, /* tp_hash */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1858 0, /* tp_call */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1859 0, /* tp_str */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1860 PyObject_GenericGetAttr, /* tp_getattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1861 0, /* tp_setattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1862 0, /* tp_as_buffer */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1863 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
|
1864 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 27769 | 1865 ifilter_doc, /* tp_doc */ |
| 1866 (traverseproc)ifilter_traverse, /* tp_traverse */ | |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1867 0, /* tp_clear */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1868 0, /* tp_richcompare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1869 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
1870 PyObject_SelfIter, /* tp_iter */ |
| 27769 | 1871 (iternextfunc)ifilter_next, /* tp_iternext */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1872 0, /* tp_methods */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1873 0, /* tp_members */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1874 0, /* tp_getset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1875 0, /* tp_base */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1876 0, /* tp_dict */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1877 0, /* tp_descr_get */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1878 0, /* tp_descr_set */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1879 0, /* tp_dictoffset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1880 0, /* tp_init */ |
|
29242
d72ad07c739e
PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents:
29225
diff
changeset
|
1881 0, /* tp_alloc */ |
| 27769 | 1882 ifilter_new, /* tp_new */ |
| 1883 PyObject_GC_Del, /* tp_free */ | |
| 1884 }; | |
| 1885 | |
| 1886 | |
| 1887 /* ifilterfalse object ************************************************************/ | |
| 1888 | |
| 1889 typedef struct { | |
| 1890 PyObject_HEAD | |
| 1891 PyObject *func; | |
| 1892 PyObject *it; | |
| 1893 } ifilterfalseobject; | |
| 1894 | |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1895 static PyTypeObject ifilterfalse_type; |
| 27769 | 1896 |
| 1897 static PyObject * | |
| 1898 ifilterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
| 1899 { | |
|
27770
2c389a0448a4
Remove unused variable.
Guido van Rossum <guido@python.org>
parents:
27769
diff
changeset
|
1900 PyObject *func, *seq; |
| 27769 | 1901 PyObject *it; |
| 1902 ifilterfalseobject *lz; | |
| 1903 | |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1904 if (type == &ifilterfalse_type && |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
1905 !_PyArg_NoKeywords("ifilterfalse()", kwds)) |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
1906 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
1907 |
| 27769 | 1908 if (!PyArg_UnpackTuple(args, "ifilterfalse", 2, 2, &func, &seq)) |
| 1909 return NULL; | |
| 1910 | |
| 1911 /* Get iterator. */ | |
| 1912 it = PyObject_GetIter(seq); | |
| 1913 if (it == NULL) | |
| 1914 return NULL; | |
| 1915 | |
| 1916 /* create ifilterfalseobject structure */ | |
| 1917 lz = (ifilterfalseobject *)type->tp_alloc(type, 0); | |
| 1918 if (lz == NULL) { | |
| 1919 Py_DECREF(it); | |
| 1920 return NULL; | |
| 1921 } | |
| 1922 Py_INCREF(func); | |
| 1923 lz->func = func; | |
| 1924 lz->it = it; | |
| 1925 | |
| 1926 return (PyObject *)lz; | |
| 1927 } | |
| 1928 | |
| 1929 static void | |
| 1930 ifilterfalse_dealloc(ifilterfalseobject *lz) | |
| 1931 { | |
| 1932 PyObject_GC_UnTrack(lz); | |
| 1933 Py_XDECREF(lz->func); | |
| 1934 Py_XDECREF(lz->it); | |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
1935 Py_TYPE(lz)->tp_free(lz); |
| 27769 | 1936 } |
| 1937 | |
| 1938 static int | |
| 1939 ifilterfalse_traverse(ifilterfalseobject *lz, visitproc visit, void *arg) | |
| 1940 { | |
|
32682
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
1941 Py_VISIT(lz->it); |
|
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
1942 Py_VISIT(lz->func); |
| 27769 | 1943 return 0; |
| 1944 } | |
| 1945 | |
| 1946 static PyObject * | |
| 1947 ifilterfalse_next(ifilterfalseobject *lz) | |
| 1948 { | |
| 1949 PyObject *item; | |
|
28117
9b4eed4bb3e2
Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents:
28024
diff
changeset
|
1950 PyObject *it = lz->it; |
| 27769 | 1951 long ok; |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
1952 PyObject *(*iternext)(PyObject *); |
| 27769 | 1953 |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
1954 assert(PyIter_Check(it)); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
1955 iternext = *Py_TYPE(it)->tp_iternext; |
| 27769 | 1956 for (;;) { |
|
31675
f7374e3b279a
Speedup the inner loops for dropwhile(), islice(), ifilter(), and
Raymond Hettinger <python@rcn.com>
parents:
31508
diff
changeset
|
1957 item = iternext(it); |
| 27769 | 1958 if (item == NULL) |
| 1959 return NULL; | |
| 1960 | |
| 1961 if (lz->func == Py_None) { | |
| 1962 ok = PyObject_IsTrue(item); | |
| 1963 } else { | |
| 1964 PyObject *good; | |
| 1965 good = PyObject_CallFunctionObjArgs(lz->func, | |
| 1966 item, NULL); | |
| 1967 if (good == NULL) { | |
| 1968 Py_DECREF(item); | |
| 1969 return NULL; | |
| 1970 } | |
| 1971 ok = PyObject_IsTrue(good); | |
| 1972 Py_DECREF(good); | |
| 1973 } | |
| 1974 if (!ok) | |
| 1975 return item; | |
| 1976 Py_DECREF(item); | |
| 1977 } | |
| 1978 } | |
| 1979 | |
| 1980 PyDoc_STRVAR(ifilterfalse_doc, | |
| 1981 "ifilterfalse(function or None, sequence) --> ifilterfalse object\n\ | |
| 1982 \n\ | |
| 1983 Return those items of sequence for which function(item) is false.\n\ | |
| 1984 If function is None, return the items that are false."); | |
| 1985 | |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
1986 static PyTypeObject ifilterfalse_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
1987 PyVarObject_HEAD_INIT(NULL, 0) |
| 27769 | 1988 "itertools.ifilterfalse", /* tp_name */ |
| 1989 sizeof(ifilterfalseobject), /* tp_basicsize */ | |
| 1990 0, /* tp_itemsize */ | |
| 1991 /* methods */ | |
| 1992 (destructor)ifilterfalse_dealloc, /* tp_dealloc */ | |
| 1993 0, /* tp_print */ | |
| 1994 0, /* tp_getattr */ | |
| 1995 0, /* tp_setattr */ | |
| 1996 0, /* tp_compare */ | |
| 1997 0, /* tp_repr */ | |
| 1998 0, /* tp_as_number */ | |
| 1999 0, /* tp_as_sequence */ | |
| 2000 0, /* tp_as_mapping */ | |
| 2001 0, /* tp_hash */ | |
| 2002 0, /* tp_call */ | |
| 2003 0, /* tp_str */ | |
| 2004 PyObject_GenericGetAttr, /* tp_getattro */ | |
| 2005 0, /* tp_setattro */ | |
| 2006 0, /* tp_as_buffer */ | |
| 2007 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | | |
| 2008 Py_TPFLAGS_BASETYPE, /* tp_flags */ | |
| 2009 ifilterfalse_doc, /* tp_doc */ | |
| 2010 (traverseproc)ifilterfalse_traverse, /* tp_traverse */ | |
| 2011 0, /* tp_clear */ | |
| 2012 0, /* tp_richcompare */ | |
| 2013 0, /* tp_weaklistoffset */ | |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
2014 PyObject_SelfIter, /* tp_iter */ |
| 27769 | 2015 (iternextfunc)ifilterfalse_next, /* tp_iternext */ |
| 2016 0, /* tp_methods */ | |
| 2017 0, /* tp_members */ | |
| 2018 0, /* tp_getset */ | |
| 2019 0, /* tp_base */ | |
| 2020 0, /* tp_dict */ | |
| 2021 0, /* tp_descr_get */ | |
| 2022 0, /* tp_descr_set */ | |
| 2023 0, /* tp_dictoffset */ | |
| 2024 0, /* tp_init */ | |
|
29242
d72ad07c739e
PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents:
29225
diff
changeset
|
2025 0, /* tp_alloc */ |
| 27769 | 2026 ifilterfalse_new, /* tp_new */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2027 PyObject_GC_Del, /* tp_free */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2028 }; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2029 |
|
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 /* count object ************************************************************/ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2032 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2033 typedef struct { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2034 PyObject_HEAD |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
2035 Py_ssize_t cnt; |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2036 PyObject *long_cnt; /* Arbitrarily large count when cnt >= PY_SSIZE_T_MAX */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2037 } countobject; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2038 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
2039 static PyTypeObject count_type; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2040 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2041 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2042 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
|
2043 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2044 countobject *lz; |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
2045 Py_ssize_t cnt = 0; |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2046 PyObject *cnt_arg = NULL; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2047 PyObject *long_cnt = NULL; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2048 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
2049 if (type == &count_type && !_PyArg_NoKeywords("count()", kwds)) |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
2050 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
2051 |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2052 if (!PyArg_UnpackTuple(args, "count", 0, 1, &cnt_arg)) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2053 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2054 |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2055 if (cnt_arg != NULL) { |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2056 cnt = PyInt_AsSsize_t(cnt_arg); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2057 if (cnt == -1 && PyErr_Occurred()) { |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2058 PyErr_Clear(); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2059 if (!PyLong_Check(cnt_arg)) { |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2060 PyErr_SetString(PyExc_TypeError, "an integer is required"); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2061 return NULL; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2062 } |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2063 long_cnt = cnt_arg; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2064 Py_INCREF(long_cnt); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2065 cnt = PY_SSIZE_T_MAX; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2066 } |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2067 } |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2068 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2069 /* create countobject structure */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2070 lz = (countobject *)PyObject_New(countobject, &count_type); |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2071 if (lz == NULL) { |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2072 Py_XDECREF(long_cnt); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2073 return NULL; |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2074 } |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2075 lz->cnt = cnt; |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2076 lz->long_cnt = long_cnt; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2077 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2078 return (PyObject *)lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2079 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2080 |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2081 static void |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2082 count_dealloc(countobject *lz) |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2083 { |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2084 Py_XDECREF(lz->long_cnt); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2085 PyObject_Del(lz); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2086 } |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2087 |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2088 static PyObject * |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2089 count_nextlong(countobject *lz) |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2090 { |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2091 static PyObject *one = NULL; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2092 PyObject *cnt; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2093 PyObject *stepped_up; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2094 |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2095 if (lz->long_cnt == NULL) { |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2096 lz->long_cnt = PyInt_FromSsize_t(PY_SSIZE_T_MAX); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2097 if (lz->long_cnt == NULL) |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2098 return NULL; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2099 } |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2100 if (one == NULL) { |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2101 one = PyInt_FromLong(1); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2102 if (one == NULL) |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2103 return NULL; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2104 } |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2105 cnt = lz->long_cnt; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2106 assert(cnt != NULL); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2107 stepped_up = PyNumber_Add(cnt, one); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2108 if (stepped_up == NULL) |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2109 return NULL; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2110 lz->long_cnt = stepped_up; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2111 return cnt; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2112 } |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2113 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2114 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2115 count_next(countobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2116 { |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2117 if (lz->cnt == PY_SSIZE_T_MAX) |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2118 return count_nextlong(lz); |
|
39866
69530aa21b8a
* regression bug, count_next was coercing a Py_ssize_t to an unsigned Py_size_t
Jack Diederich <jackdied@gmail.com>
parents:
39720
diff
changeset
|
2119 return PyInt_FromSsize_t(lz->cnt++); |
|
27570
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 |
|
31890
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2122 static PyObject * |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2123 count_repr(countobject *lz) |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2124 { |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2125 PyObject *cnt_repr; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2126 PyObject *result; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2127 |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2128 if (lz->cnt != PY_SSIZE_T_MAX) |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2129 return PyString_FromFormat("count(%zd)", lz->cnt); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2130 |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2131 cnt_repr = PyObject_Repr(lz->long_cnt); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2132 if (cnt_repr == NULL) |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2133 return NULL; |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2134 result = PyString_FromFormat("count(%s)", PyString_AS_STRING(cnt_repr)); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2135 Py_DECREF(cnt_repr); |
|
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2136 return result; |
|
31890
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2137 } |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2138 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2139 PyDoc_STRVAR(count_doc, |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2140 "count([firstval]) --> count object\n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2141 \n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2142 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
|
2143 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
|
2144 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
2145 static PyTypeObject count_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
2146 PyVarObject_HEAD_INIT(NULL, 0) |
| 27769 | 2147 "itertools.count", /* tp_name */ |
| 2148 sizeof(countobject), /* tp_basicsize */ | |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2149 0, /* tp_itemsize */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2150 /* methods */ |
|
43458
333b052e9da9
itertools.count() no longer limited to sys.maxint.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
2151 (destructor)count_dealloc, /* tp_dealloc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2152 0, /* tp_print */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2153 0, /* tp_getattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2154 0, /* tp_setattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2155 0, /* tp_compare */ |
|
31890
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2156 (reprfunc)count_repr, /* tp_repr */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2157 0, /* tp_as_number */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2158 0, /* tp_as_sequence */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2159 0, /* tp_as_mapping */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2160 0, /* tp_hash */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2161 0, /* tp_call */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2162 0, /* tp_str */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2163 PyObject_GenericGetAttr, /* tp_getattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2164 0, /* tp_setattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2165 0, /* tp_as_buffer */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2166 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 27769 | 2167 count_doc, /* tp_doc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2168 0, /* tp_traverse */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2169 0, /* tp_clear */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2170 0, /* tp_richcompare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2171 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
2172 PyObject_SelfIter, /* tp_iter */ |
| 27769 | 2173 (iternextfunc)count_next, /* tp_iternext */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2174 0, /* tp_methods */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2175 0, /* tp_members */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2176 0, /* tp_getset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2177 0, /* tp_base */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2178 0, /* tp_dict */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2179 0, /* tp_descr_get */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2180 0, /* tp_descr_set */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2181 0, /* tp_dictoffset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2182 0, /* tp_init */ |
|
29242
d72ad07c739e
PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents:
29225
diff
changeset
|
2183 0, /* tp_alloc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2184 count_new, /* tp_new */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2185 }; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2186 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2187 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2188 /* izip object ************************************************************/ |
|
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 #include "Python.h" |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2191 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2192 typedef struct { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2193 PyObject_HEAD |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
2194 Py_ssize_t tuplesize; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2195 PyObject *ittuple; /* tuple of iterators */ |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2196 PyObject *result; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2197 } izipobject; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2198 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
2199 static PyTypeObject izip_type; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2200 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2201 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2202 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
|
2203 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2204 izipobject *lz; |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
2205 Py_ssize_t i; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2206 PyObject *ittuple; /* tuple of iterators */ |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2207 PyObject *result; |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
2208 Py_ssize_t tuplesize = PySequence_Length(args); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2209 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
2210 if (type == &izip_type && !_PyArg_NoKeywords("izip()", kwds)) |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
2211 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
2212 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2213 /* args must be a tuple */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2214 assert(PyTuple_Check(args)); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2215 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2216 /* obtain iterators */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2217 ittuple = PyTuple_New(tuplesize); |
|
38420
499cf7c2f777
Fix compiler warning (and whitespace) on Mac OS 10.4. (A lot of this code looked duplicated, I wonder if a utility function could help reduce the duplication here.)
Neal Norwitz <nnorwitz@gmail.com>
parents:
38341
diff
changeset
|
2218 if (ittuple == NULL) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2219 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2220 for (i=0; i < tuplesize; ++i) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2221 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
|
2222 PyObject *it = PyObject_GetIter(item); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2223 if (it == NULL) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2224 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2225 PyErr_Format(PyExc_TypeError, |
|
38420
499cf7c2f777
Fix compiler warning (and whitespace) on Mac OS 10.4. (A lot of this code looked duplicated, I wonder if a utility function could help reduce the duplication here.)
Neal Norwitz <nnorwitz@gmail.com>
parents:
38341
diff
changeset
|
2226 "izip argument #%zd must support iteration", |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2227 i+1); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2228 Py_DECREF(ittuple); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2229 return NULL; |
|
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 PyTuple_SET_ITEM(ittuple, i, it); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2232 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2233 |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2234 /* create a result holder */ |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2235 result = PyTuple_New(tuplesize); |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2236 if (result == NULL) { |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2237 Py_DECREF(ittuple); |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2238 return NULL; |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2239 } |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2240 for (i=0 ; i < tuplesize ; i++) { |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2241 Py_INCREF(Py_None); |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2242 PyTuple_SET_ITEM(result, i, Py_None); |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2243 } |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2244 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2245 /* create izipobject structure */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2246 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
|
2247 if (lz == NULL) { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2248 Py_DECREF(ittuple); |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2249 Py_DECREF(result); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2250 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2251 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2252 lz->ittuple = ittuple; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2253 lz->tuplesize = tuplesize; |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2254 lz->result = result; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2255 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2256 return (PyObject *)lz; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2257 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2258 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2259 static void |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2260 izip_dealloc(izipobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2261 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2262 PyObject_GC_UnTrack(lz); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2263 Py_XDECREF(lz->ittuple); |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2264 Py_XDECREF(lz->result); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
2265 Py_TYPE(lz)->tp_free(lz); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2266 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2267 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2268 static int |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2269 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
|
2270 { |
|
32682
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
2271 Py_VISIT(lz->ittuple); |
|
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
2272 Py_VISIT(lz->result); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2273 return 0; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2274 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2275 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2276 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2277 izip_next(izipobject *lz) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2278 { |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
2279 Py_ssize_t i; |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
2280 Py_ssize_t tuplesize = lz->tuplesize; |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2281 PyObject *result = lz->result; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2282 PyObject *it; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2283 PyObject *item; |
|
30240
f1ea311127cf
For safety, replace a tuple entry before decreffing it.
Raymond Hettinger <python@rcn.com>
parents:
30238
diff
changeset
|
2284 PyObject *olditem; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2285 |
|
30093
0ac4396b1894
Modified itertools.izip() to match the behavior of __builtin__.zip()
Raymond Hettinger <python@rcn.com>
parents:
29850
diff
changeset
|
2286 if (tuplesize == 0) |
|
0ac4396b1894
Modified itertools.izip() to match the behavior of __builtin__.zip()
Raymond Hettinger <python@rcn.com>
parents:
29850
diff
changeset
|
2287 return NULL; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
2288 if (Py_REFCNT(result) == 1) { |
|
30238
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
2289 Py_INCREF(result); |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2290 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
|
2291 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
|
2292 assert(PyIter_Check(it)); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
2293 item = (*Py_TYPE(it)->tp_iternext)(it); |
|
30238
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
2294 if (item == NULL) { |
|
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
2295 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
|
2296 return NULL; |
|
30238
a570bb9f133f
SF bug #793826: using itertools.izip to mutate tuples
Raymond Hettinger <python@rcn.com>
parents:
30093
diff
changeset
|
2297 } |
|
30240
f1ea311127cf
For safety, replace a tuple entry before decreffing it.
Raymond Hettinger <python@rcn.com>
parents:
30238
diff
changeset
|
2298 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
|
2299 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
|
2300 Py_DECREF(olditem); |
|
27747
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2301 } |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2302 } else { |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2303 result = PyTuple_New(tuplesize); |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2304 if (result == NULL) |
|
941de791b17a
SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents:
27570
diff
changeset
|
2305 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
|
2306 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
|
2307 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
|
2308 assert(PyIter_Check(it)); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
2309 item = (*Py_TYPE(it)->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
|
2310 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
|
2311 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
|
2312 return NULL; |
|
59ae41e04ffb
* Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents:
27747
diff
changeset
|
2313 } |
|
59ae41e04ffb
* Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents:
27747
diff
changeset
|
2314 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
|
2315 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2316 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2317 return result; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2318 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2319 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2320 PyDoc_STRVAR(izip_doc, |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2321 "izip(iter1 [,iter2 [...]]) --> izip object\n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2322 \n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2323 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
|
2324 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
|
2325 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
|
2326 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
|
2327 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
|
2328 a list."); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2329 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
2330 static PyTypeObject izip_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
2331 PyVarObject_HEAD_INIT(NULL, 0) |
| 27769 | 2332 "itertools.izip", /* tp_name */ |
| 2333 sizeof(izipobject), /* tp_basicsize */ | |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2334 0, /* tp_itemsize */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2335 /* methods */ |
| 27769 | 2336 (destructor)izip_dealloc, /* tp_dealloc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2337 0, /* tp_print */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2338 0, /* tp_getattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2339 0, /* tp_setattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2340 0, /* tp_compare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2341 0, /* tp_repr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2342 0, /* tp_as_number */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2343 0, /* tp_as_sequence */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2344 0, /* tp_as_mapping */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2345 0, /* tp_hash */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2346 0, /* tp_call */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2347 0, /* tp_str */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2348 PyObject_GenericGetAttr, /* tp_getattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2349 0, /* tp_setattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2350 0, /* tp_as_buffer */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2351 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
|
2352 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 27769 | 2353 izip_doc, /* tp_doc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2354 (traverseproc)izip_traverse, /* tp_traverse */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2355 0, /* tp_clear */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2356 0, /* tp_richcompare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2357 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
2358 PyObject_SelfIter, /* tp_iter */ |
| 27769 | 2359 (iternextfunc)izip_next, /* tp_iternext */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2360 0, /* tp_methods */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2361 0, /* tp_members */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2362 0, /* tp_getset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2363 0, /* tp_base */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2364 0, /* tp_dict */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2365 0, /* tp_descr_get */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2366 0, /* tp_descr_set */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2367 0, /* tp_dictoffset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2368 0, /* tp_init */ |
|
29242
d72ad07c739e
PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents:
29225
diff
changeset
|
2369 0, /* tp_alloc */ |
| 27769 | 2370 izip_new, /* tp_new */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2371 PyObject_GC_Del, /* tp_free */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2372 }; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2373 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2374 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2375 /* repeat object ************************************************************/ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2376 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2377 typedef struct { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2378 PyObject_HEAD |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2379 PyObject *element; |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
2380 Py_ssize_t cnt; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2381 } repeatobject; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2382 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
2383 static PyTypeObject repeat_type; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2384 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2385 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2386 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
|
2387 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2388 repeatobject *ro; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2389 PyObject *element; |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
2390 Py_ssize_t cnt = -1; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2391 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
39866
diff
changeset
|
2392 if (type == &repeat_type && !_PyArg_NoKeywords("repeat()", kwds)) |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
2393 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34108
diff
changeset
|
2394 |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
2395 if (!PyArg_ParseTuple(args, "O|n:repeat", &element, &cnt)) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2396 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2397 |
|
28945
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
2398 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
|
2399 cnt = 0; |
|
001a90555780
* Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents:
28930
diff
changeset
|
2400 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2401 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
|
2402 if (ro == NULL) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2403 return NULL; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2404 Py_INCREF(element); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2405 ro->element = element; |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
2406 ro->cnt = cnt; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2407 return (PyObject *)ro; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2408 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2409 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2410 static void |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2411 repeat_dealloc(repeatobject *ro) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2412 { |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2413 PyObject_GC_UnTrack(ro); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2414 Py_XDECREF(ro->element); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
2415 Py_TYPE(ro)->tp_free(ro); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2416 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2417 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2418 static int |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2419 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
|
2420 { |
|
32682
bc4dd31c73fd
Exercise Jim's VISIT macro.
Raymond Hettinger <python@rcn.com>
parents:
32010
diff
changeset
|
2421 Py_VISIT(ro->element); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2422 return 0; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2423 } |
|
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 static PyObject * |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2426 repeat_next(repeatobject *ro) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2427 { |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
2428 if (ro->cnt == 0) |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
2429 return NULL; |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
2430 if (ro->cnt > 0) |
|
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
2431 ro->cnt--; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2432 Py_INCREF(ro->element); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2433 return ro->element; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2434 } |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2435 |
|
31890
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2436 static PyObject * |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2437 repeat_repr(repeatobject *ro) |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2438 { |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2439 PyObject *result, *objrepr; |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2440 |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2441 objrepr = PyObject_Repr(ro->element); |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2442 if (objrepr == NULL) |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2443 return NULL; |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2444 |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2445 if (ro->cnt == -1) |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2446 result = PyString_FromFormat("repeat(%s)", |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2447 PyString_AS_STRING(objrepr)); |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2448 else |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
2449 result = PyString_FromFormat("repeat(%s, %zd)", |
|
31890
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2450 PyString_AS_STRING(objrepr), ro->cnt); |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2451 Py_DECREF(objrepr); |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2452 return result; |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2453 } |
|
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2454 |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
2455 static PyObject * |
|
31501
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
2456 repeat_len(repeatobject *ro) |
|
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
2457 { |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
2458 if (ro->cnt == -1) { |
|
31501
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
2459 PyErr_SetString(PyExc_TypeError, "len() of unsized object"); |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
2460 return NULL; |
|
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
2461 } |
|
38341
a654d61d36d5
use Py_ssize_t in places that may need it
Jack Diederich <jackdied@gmail.com>
parents:
37757
diff
changeset
|
2462 return PyInt_FromSize_t(ro->cnt); |
|
31501
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
2463 } |
|
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
2464 |
|
36484
20f75d5e79e9
Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents:
36324
diff
changeset
|
2465 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
2466 |
|
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
2467 static PyMethodDef repeat_methods[] = { |
|
36484
20f75d5e79e9
Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents:
36324
diff
changeset
|
2468 {"__length_hint__", (PyCFunction)repeat_len, METH_NOARGS, length_hint_doc}, |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
2469 {NULL, NULL} /* sentinel */ |
|
31501
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
2470 }; |
|
14f860b03be2
Give itertools.repeat() a length method.
Raymond Hettinger <python@rcn.com>
parents:
31136
diff
changeset
|
2471 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2472 PyDoc_STRVAR(repeat_doc, |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
2473 "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
|
2474 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
|
2475 endlessly."); |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2476 |
|
29850
45dc33433323
SF patch #770521: make itertools type declarations static
Raymond Hettinger <python@rcn.com>
parents:
29535
diff
changeset
|
2477 static PyTypeObject repeat_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
2478 PyVarObject_HEAD_INIT(NULL, 0) |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2479 "itertools.repeat", /* tp_name */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2480 sizeof(repeatobject), /* tp_basicsize */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2481 0, /* tp_itemsize */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2482 /* methods */ |
| 27769 | 2483 (destructor)repeat_dealloc, /* tp_dealloc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2484 0, /* tp_print */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2485 0, /* tp_getattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2486 0, /* tp_setattr */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2487 0, /* tp_compare */ |
|
31890
848bce2215d3
Provide more information representations of repeat() and count().
Raymond Hettinger <python@rcn.com>
parents:
31675
diff
changeset
|
2488 (reprfunc)repeat_repr, /* tp_repr */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2489 0, /* tp_as_number */ |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
2490 0, /* tp_as_sequence */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2491 0, /* tp_as_mapping */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2492 0, /* tp_hash */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2493 0, /* tp_call */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2494 0, /* tp_str */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2495 PyObject_GenericGetAttr, /* tp_getattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2496 0, /* tp_setattro */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2497 0, /* tp_as_buffer */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2498 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
|
2499 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 27769 | 2500 repeat_doc, /* tp_doc */ |
| 2501 (traverseproc)repeat_traverse, /* tp_traverse */ | |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2502 0, /* tp_clear */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2503 0, /* tp_richcompare */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2504 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
2505 PyObject_SelfIter, /* tp_iter */ |
| 27769 | 2506 (iternextfunc)repeat_next, /* tp_iternext */ |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
2507 repeat_methods, /* tp_methods */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2508 0, /* tp_members */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2509 0, /* tp_getset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2510 0, /* tp_base */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2511 0, /* tp_dict */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2512 0, /* tp_descr_get */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2513 0, /* tp_descr_set */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2514 0, /* tp_dictoffset */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2515 0, /* tp_init */ |
|
29242
d72ad07c739e
PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents:
29225
diff
changeset
|
2516 0, /* tp_alloc */ |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2517 repeat_new, /* tp_new */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2518 PyObject_GC_Del, /* tp_free */ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2519 }; |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2520 |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2521 /* iziplongest object ************************************************************/ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2522 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2523 #include "Python.h" |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2524 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2525 typedef struct { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2526 PyObject_HEAD |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2527 Py_ssize_t tuplesize; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2528 Py_ssize_t numactive; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2529 PyObject *ittuple; /* tuple of iterators */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2530 PyObject *result; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2531 PyObject *fillvalue; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2532 } iziplongestobject; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2533 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2534 static PyTypeObject iziplongest_type; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2535 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2536 static PyObject * |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2537 izip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2538 { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2539 iziplongestobject *lz; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2540 Py_ssize_t i; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2541 PyObject *ittuple; /* tuple of iterators */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2542 PyObject *result; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2543 PyObject *fillvalue = Py_None; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2544 Py_ssize_t tuplesize = PySequence_Length(args); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2545 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2546 if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_Size(kwds) > 0) { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2547 fillvalue = PyDict_GetItemString(kwds, "fillvalue"); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2548 if (fillvalue == NULL || PyDict_Size(kwds) > 1) { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2549 PyErr_SetString(PyExc_TypeError, |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2550 "izip_longest() got an unexpected keyword argument"); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2551 return NULL; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2552 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2553 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2554 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2555 /* args must be a tuple */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2556 assert(PyTuple_Check(args)); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2557 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2558 /* obtain iterators */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2559 ittuple = PyTuple_New(tuplesize); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2560 if (ittuple == NULL) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2561 return NULL; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2562 for (i=0; i < tuplesize; ++i) { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2563 PyObject *item = PyTuple_GET_ITEM(args, i); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2564 PyObject *it = PyObject_GetIter(item); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2565 if (it == NULL) { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2566 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2567 PyErr_Format(PyExc_TypeError, |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2568 "izip_longest argument #%zd must support iteration", |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2569 i+1); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2570 Py_DECREF(ittuple); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2571 return NULL; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2572 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2573 PyTuple_SET_ITEM(ittuple, i, it); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2574 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2575 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2576 /* create a result holder */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2577 result = PyTuple_New(tuplesize); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2578 if (result == NULL) { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2579 Py_DECREF(ittuple); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2580 return NULL; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2581 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2582 for (i=0 ; i < tuplesize ; i++) { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2583 Py_INCREF(Py_None); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2584 PyTuple_SET_ITEM(result, i, Py_None); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2585 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2586 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2587 /* create iziplongestobject structure */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2588 lz = (iziplongestobject *)type->tp_alloc(type, 0); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2589 if (lz == NULL) { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2590 Py_DECREF(ittuple); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2591 Py_DECREF(result); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2592 return NULL; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2593 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2594 lz->ittuple = ittuple; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2595 lz->tuplesize = tuplesize; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2596 lz->numactive = tuplesize; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2597 lz->result = result; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2598 Py_INCREF(fillvalue); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2599 lz->fillvalue = fillvalue; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2600 return (PyObject *)lz; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2601 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2602 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2603 static void |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2604 izip_longest_dealloc(iziplongestobject *lz) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2605 { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2606 PyObject_GC_UnTrack(lz); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2607 Py_XDECREF(lz->ittuple); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2608 Py_XDECREF(lz->result); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2609 Py_XDECREF(lz->fillvalue); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
2610 Py_TYPE(lz)->tp_free(lz); |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2611 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2612 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2613 static int |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2614 izip_longest_traverse(iziplongestobject *lz, visitproc visit, void *arg) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2615 { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2616 Py_VISIT(lz->ittuple); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2617 Py_VISIT(lz->result); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2618 Py_VISIT(lz->fillvalue); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2619 return 0; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2620 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2621 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2622 static PyObject * |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2623 izip_longest_next(iziplongestobject *lz) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2624 { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2625 Py_ssize_t i; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2626 Py_ssize_t tuplesize = lz->tuplesize; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2627 PyObject *result = lz->result; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2628 PyObject *it; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2629 PyObject *item; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2630 PyObject *olditem; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2631 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2632 if (tuplesize == 0) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2633 return NULL; |
|
40878
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2634 if (lz->numactive == 0) |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2635 return NULL; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
2636 if (Py_REFCNT(result) == 1) { |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2637 Py_INCREF(result); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2638 for (i=0 ; i < tuplesize ; i++) { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2639 it = PyTuple_GET_ITEM(lz->ittuple, i); |
|
40878
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2640 if (it == NULL) { |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2641 Py_INCREF(lz->fillvalue); |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2642 item = lz->fillvalue; |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2643 } else { |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2644 assert(PyIter_Check(it)); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
2645 item = (*Py_TYPE(it)->tp_iternext)(it); |
|
40878
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2646 if (item == NULL) { |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2647 lz->numactive -= 1; |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2648 if (lz->numactive == 0) { |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2649 Py_DECREF(result); |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2650 return NULL; |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2651 } else { |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2652 Py_INCREF(lz->fillvalue); |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2653 item = lz->fillvalue; |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2654 PyTuple_SET_ITEM(lz->ittuple, i, NULL); |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2655 Py_DECREF(it); |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2656 } |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2657 } |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2658 } |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2659 olditem = PyTuple_GET_ITEM(result, i); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2660 PyTuple_SET_ITEM(result, i, item); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2661 Py_DECREF(olditem); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2662 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2663 } else { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2664 result = PyTuple_New(tuplesize); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2665 if (result == NULL) |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2666 return NULL; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2667 for (i=0 ; i < tuplesize ; i++) { |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2668 it = PyTuple_GET_ITEM(lz->ittuple, i); |
|
40878
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2669 if (it == NULL) { |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2670 Py_INCREF(lz->fillvalue); |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2671 item = lz->fillvalue; |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2672 } else { |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2673 assert(PyIter_Check(it)); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
2674 item = (*Py_TYPE(it)->tp_iternext)(it); |
|
40878
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2675 if (item == NULL) { |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2676 lz->numactive -= 1; |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2677 if (lz->numactive == 0) { |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2678 Py_DECREF(result); |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2679 return NULL; |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2680 } else { |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2681 Py_INCREF(lz->fillvalue); |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2682 item = lz->fillvalue; |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2683 PyTuple_SET_ITEM(lz->ittuple, i, NULL); |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2684 Py_DECREF(it); |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2685 } |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2686 } |
|
8128619e984d
Remove filler struct item and fix leak.
Raymond Hettinger <python@rcn.com>
parents:
40877
diff
changeset
|
2687 } |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2688 PyTuple_SET_ITEM(result, i, item); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2689 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2690 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2691 return result; |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2692 } |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2693 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2694 PyDoc_STRVAR(izip_longest_doc, |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2695 "izip_longest(iter1 [,iter2 [...]], [fillvalue=None]) --> izip_longest object\n\ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2696 \n\ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2697 Return an izip_longest object whose .next() method returns a tuple where\n\ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2698 the i-th element comes from the i-th iterable argument. The .next()\n\ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2699 method continues until the longest iterable in the argument sequence\n\ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2700 is exhausted and then it raises StopIteration. When the shorter iterables\n\ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2701 are exhausted, the fillvalue is substituted in their place. The fillvalue\n\ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2702 defaults to None or can be specified by a keyword argument.\n\ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2703 "); |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2704 |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2705 static PyTypeObject iziplongest_type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41590
diff
changeset
|
2706 PyVarObject_HEAD_INIT(NULL, 0) |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2707 "itertools.izip_longest", /* tp_name */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2708 sizeof(iziplongestobject), /* tp_basicsize */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2709 0, /* tp_itemsize */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2710 /* methods */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2711 (destructor)izip_longest_dealloc, /* tp_dealloc */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2712 0, /* tp_print */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2713 0, /* tp_getattr */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2714 0, /* tp_setattr */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2715 0, /* tp_compare */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2716 0, /* tp_repr */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2717 0, /* tp_as_number */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2718 0, /* tp_as_sequence */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2719 0, /* tp_as_mapping */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2720 0, /* tp_hash */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2721 0, /* tp_call */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2722 0, /* tp_str */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2723 PyObject_GenericGetAttr, /* tp_getattro */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2724 0, /* tp_setattro */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2725 0, /* tp_as_buffer */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2726 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2727 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2728 izip_longest_doc, /* tp_doc */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2729 (traverseproc)izip_longest_traverse, /* tp_traverse */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2730 0, /* tp_clear */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2731 0, /* tp_richcompare */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2732 0, /* tp_weaklistoffset */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2733 PyObject_SelfIter, /* tp_iter */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2734 (iternextfunc)izip_longest_next, /* tp_iternext */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2735 0, /* tp_methods */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2736 0, /* tp_members */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2737 0, /* tp_getset */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2738 0, /* tp_base */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2739 0, /* tp_dict */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2740 0, /* tp_descr_get */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2741 0, /* tp_descr_set */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2742 0, /* tp_dictoffset */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2743 0, /* tp_init */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2744 0, /* tp_alloc */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2745 izip_longest_new, /* tp_new */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2746 PyObject_GC_Del, /* tp_free */ |
|
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2747 }; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2748 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2749 /* module level code ********************************************************/ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2750 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2751 PyDoc_STRVAR(module_doc, |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2752 "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
|
2753 \n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2754 Infinite iterators:\n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2755 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
|
2756 cycle(p) --> p0, p1, ... plast, p0, p1, ...\n\ |
| 28601 | 2757 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
|
2758 \n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2759 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
|
2760 izip(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\ |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2761 izip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\ |
| 27769 | 2762 ifilter(pred, seq) --> elements of seq where pred(elem) is True\n\ |
| 2763 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
|
2764 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
|
2765 seq[start:stop:step]\n\ |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2766 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
|
2767 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
|
2768 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
|
2769 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
|
2770 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
|
2771 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
|
2772 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
|
2773 "); |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2774 |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2775 |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
2776 static PyMethodDef module_methods[] = { |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
2777 {"tee", (PyCFunction)tee, METH_VARARGS, tee_doc}, |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
2778 {NULL, NULL} /* sentinel */ |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
2779 }; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
2780 |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2781 PyMODINIT_FUNC |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2782 inititertools(void) |
|
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2783 { |
| 27769 | 2784 int i; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2785 PyObject *m; |
| 27769 | 2786 char *name; |
| 2787 PyTypeObject *typelist[] = { | |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
2788 &cycle_type, |
| 27769 | 2789 &dropwhile_type, |
| 2790 &takewhile_type, | |
| 2791 &islice_type, | |
| 2792 &starmap_type, | |
| 2793 &imap_type, | |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
2794 &chain_type, |
| 27769 | 2795 &ifilter_type, |
| 2796 &ifilterfalse_type, | |
| 2797 &count_type, | |
| 2798 &izip_type, | |
|
40877
6fa0d682e1e5
Add itertools.izip_longest().
Raymond Hettinger <python@rcn.com>
parents:
40766
diff
changeset
|
2799 &iziplongest_type, |
| 27769 | 2800 &repeat_type, |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
2801 &groupby_type, |
| 27769 | 2802 NULL |
| 2803 }; | |
| 2804 | |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43458
diff
changeset
|
2805 Py_TYPE(&teedataobject_type) = &PyType_Type; |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
2806 m = Py_InitModule3("itertools", module_methods, module_doc); |
|
36324
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
2807 if (m == NULL) |
|
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
2808 return; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2809 |
| 27769 | 2810 for (i=0 ; typelist[i] != NULL ; i++) { |
| 2811 if (PyType_Ready(typelist[i]) < 0) | |
| 2812 return; | |
|
29225
f3f306457b43
Fixed dotted name assertion.
Raymond Hettinger <python@rcn.com>
parents:
28945
diff
changeset
|
2813 name = strchr(typelist[i]->tp_name, '.'); |
|
28024
61fa6b55c1de
User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents:
27770
diff
changeset
|
2814 assert (name != NULL); |
| 27769 | 2815 Py_INCREF(typelist[i]); |
|
29225
f3f306457b43
Fixed dotted name assertion.
Raymond Hettinger <python@rcn.com>
parents:
28945
diff
changeset
|
2816 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]); |
| 27769 | 2817 } |
|
30900
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
2818 |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
2819 if (PyType_Ready(&teedataobject_type) < 0) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
2820 return; |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
2821 if (PyType_Ready(&tee_type) < 0) |
|
3096fab2a19c
Improve the implementation of itertools.tee().
Raymond Hettinger <python@rcn.com>
parents:
30800
diff
changeset
|
2822 return; |
|
31136
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
2823 if (PyType_Ready(&_grouper_type) < 0) |
|
fde74fadf780
Implement itertools.groupby()
Raymond Hettinger <python@rcn.com>
parents:
30900
diff
changeset
|
2824 return; |
|
27570
ba57c3daf6e9
Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2825 } |
