annotate Modules/itertoolsmodule.c @ 30241:3fb3747ce304 2.3

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