annotate Modules/itertoolsmodule.c @ 29298:b5478d1dd67e legacy-trunk

Test script changed.
author Greg Ward <gward@python.net>
date Thu, 29 May 2003 01:29:28 +0000
parents d72ad07c739e
children 70e649fa3fcd
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
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
19 PyTypeObject cycle_type;
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
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
114 PyTypeObject cycle_type = {
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
169 PyTypeObject dropwhile_type;
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
263 PyTypeObject dropwhile_type = {
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
318 PyTypeObject takewhile_type;
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
411 PyTypeObject takewhile_type = {
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
468 PyTypeObject islice_type;
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
601 PyTypeObject islice_type = {
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
655 PyTypeObject starmap_type;
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
740 PyTypeObject starmap_type = {
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
794 PyTypeObject imap_type;
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
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
885 writing a generator.
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
927 PyTypeObject imap_type = {
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
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
982 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);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1013 if (lz == NULL)
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1014 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1015
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1016 lz->ittuple = ittuple;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1017 lz->iternum = 0;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1018 lz->tuplesize = tuplesize;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1019
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1020 return (PyObject *)lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1021 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1022
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1023 static void
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1024 chain_dealloc(chainobject *lz)
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1025 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1026 PyObject_GC_UnTrack(lz);
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1027 Py_XDECREF(lz->ittuple);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1028 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1029 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1030
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1031 static int
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1032 chain_traverse(chainobject *lz, visitproc visit, void *arg)
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1033 {
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1034 if (lz->ittuple)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1035 return visit(lz->ittuple, arg);
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1036 return 0;
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1037 }
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1038
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1039 static PyObject *
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1040 chain_next(chainobject *lz)
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1041 {
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1042 PyObject *it;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1043 PyObject *item;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1044
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1045 while (lz->iternum < lz->tuplesize) {
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1046 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
1047 item = PyIter_Next(it);
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1048 if (item != NULL)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1049 return item;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1050 lz->iternum++;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1051 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1052 return NULL;
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
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1055 PyDoc_STRVAR(chain_doc,
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1056 "chain(*iterables) --> chain object\n\
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1057 \n\
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1058 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
1059 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
1060 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
1061
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1062 PyTypeObject chain_type = {
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1063 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1064 0, /* ob_size */
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1065 "itertools.chain", /* tp_name */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1066 sizeof(chainobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1067 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1068 /* methods */
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1069 (destructor)chain_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1070 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1071 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1072 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1073 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1074 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1075 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1076 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1077 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1078 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1079 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1080 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1081 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1082 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1083 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1084 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1085 Py_TPFLAGS_BASETYPE, /* tp_flags */
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1086 chain_doc, /* tp_doc */
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1087 (traverseproc)chain_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1088 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1089 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1090 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1091 PyObject_SelfIter, /* tp_iter */
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1092 (iternextfunc)chain_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1093 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1094 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1095 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1096 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1097 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1098 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1099 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1100 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1101 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1102 0, /* tp_alloc */
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1103 chain_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1104 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1105 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1106
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 /* ifilter object ************************************************************/
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 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1111 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1112 PyObject *func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1113 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1114 } ifilterobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1115
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1116 PyTypeObject ifilter_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1117
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1118 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1119 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
1120 {
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1121 PyObject *func, *seq;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1122 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1123 ifilterobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1124
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1125 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
1126 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1127
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1128 /* Get iterator. */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1129 it = PyObject_GetIter(seq);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1130 if (it == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1131 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1132
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1133 /* create ifilterobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1134 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
1135 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1136 Py_DECREF(it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1137 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1138 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1139 Py_INCREF(func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1140 lz->func = func;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1141 lz->it = it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1142
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1143 return (PyObject *)lz;
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1146 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1147 ifilter_dealloc(ifilterobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1148 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1149 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1150 Py_XDECREF(lz->func);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1151 Py_XDECREF(lz->it);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1152 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1153 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1154
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1155 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1156 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
1157 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1158 int err;
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 if (lz->it) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1161 err = visit(lz->it, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1162 if (err)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1163 return err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1164 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1165 if (lz->func) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1166 err = visit(lz->func, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1167 if (err)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1168 return err;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1169 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1170 return 0;
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1173 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1174 ifilter_next(ifilterobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1175 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1176 PyObject *item;
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
1177 PyObject *it = lz->it;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1178 long ok;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1179
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1180 for (;;) {
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
1181 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
1182 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
1183 if (item == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1184 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1185
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1186 if (lz->func == Py_None) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1187 ok = PyObject_IsTrue(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1188 } else {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1189 PyObject *good;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1190 good = PyObject_CallFunctionObjArgs(lz->func,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1191 item, NULL);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1192 if (good == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1193 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1194 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1195 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1196 ok = PyObject_IsTrue(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1197 Py_DECREF(good);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1198 }
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1199 if (ok)
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1200 return item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1201 Py_DECREF(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1202 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1203 }
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 PyDoc_STRVAR(ifilter_doc,
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1206 "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
1207 \n\
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1208 Return those items of sequence for which function(item) is true.\n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1209 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
1210
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1211 PyTypeObject ifilter_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1212 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1213 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1214 "itertools.ifilter", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1215 sizeof(ifilterobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1216 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1217 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1218 (destructor)ifilter_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1219 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1220 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1221 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1222 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1223 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1224 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1225 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1226 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1227 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1228 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1229 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1230 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1231 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1232 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1233 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
1234 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1235 ifilter_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1236 (traverseproc)ifilter_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1237 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1238 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1239 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1240 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1241 (iternextfunc)ifilter_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1242 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1243 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1244 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1245 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1246 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1247 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1248 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1249 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1250 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1251 0, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1252 ifilter_new, /* tp_new */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1253 PyObject_GC_Del, /* tp_free */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1254 };
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1255
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 /* ifilterfalse object ************************************************************/
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 typedef struct {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1260 PyObject_HEAD
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1261 PyObject *func;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1262 PyObject *it;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1263 } ifilterfalseobject;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1264
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1265 PyTypeObject ifilterfalse_type;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1266
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1267 static PyObject *
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1268 ifilterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1269 {
27770
2c389a0448a4 Remove unused variable.
Guido van Rossum <guido@python.org>
parents: 27769
diff changeset
1270 PyObject *func, *seq;
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1271 PyObject *it;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1272 ifilterfalseobject *lz;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1273
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1274 if (!PyArg_UnpackTuple(args, "ifilterfalse", 2, 2, &func, &seq))
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1275 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1276
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1277 /* Get iterator. */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1278 it = PyObject_GetIter(seq);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1279 if (it == NULL)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1280 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1281
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1282 /* create ifilterfalseobject structure */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1283 lz = (ifilterfalseobject *)type->tp_alloc(type, 0);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1284 if (lz == NULL) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1285 Py_DECREF(it);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1286 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1287 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1288 Py_INCREF(func);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1289 lz->func = func;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1290 lz->it = it;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1291
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1292 return (PyObject *)lz;
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
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1295 static void
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1296 ifilterfalse_dealloc(ifilterfalseobject *lz)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1297 {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1298 PyObject_GC_UnTrack(lz);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1299 Py_XDECREF(lz->func);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1300 Py_XDECREF(lz->it);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1301 lz->ob_type->tp_free(lz);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1302 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1303
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1304 static int
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1305 ifilterfalse_traverse(ifilterfalseobject *lz, visitproc visit, void *arg)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1306 {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1307 int err;
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 if (lz->it) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1310 err = visit(lz->it, arg);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1311 if (err)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1312 return err;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1313 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1314 if (lz->func) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1315 err = visit(lz->func, arg);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1316 if (err)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1317 return err;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1318 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1319 return 0;
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
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1322 static PyObject *
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1323 ifilterfalse_next(ifilterfalseobject *lz)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1324 {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1325 PyObject *item;
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
1326 PyObject *it = lz->it;
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1327 long ok;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1328
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1329 for (;;) {
28117
9b4eed4bb3e2 Several of the tools can make direct calls the inner iterators.
Raymond Hettinger <python@rcn.com>
parents: 28024
diff changeset
1330 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
1331 item = (*it->ob_type->tp_iternext)(it);
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1332 if (item == NULL)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1333 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1334
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1335 if (lz->func == Py_None) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1336 ok = PyObject_IsTrue(item);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1337 } else {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1338 PyObject *good;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1339 good = PyObject_CallFunctionObjArgs(lz->func,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1340 item, NULL);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1341 if (good == NULL) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1342 Py_DECREF(item);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1343 return NULL;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1344 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1345 ok = PyObject_IsTrue(good);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1346 Py_DECREF(good);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1347 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1348 if (!ok)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1349 return item;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1350 Py_DECREF(item);
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1351 }
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1352 }
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 PyDoc_STRVAR(ifilterfalse_doc,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1355 "ifilterfalse(function or None, sequence) --> ifilterfalse object\n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1356 \n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1357 Return those items of sequence for which function(item) is false.\n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1358 If function is None, return the items that are false.");
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1359
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1360 PyTypeObject ifilterfalse_type = {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1361 PyObject_HEAD_INIT(NULL)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1362 0, /* ob_size */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1363 "itertools.ifilterfalse", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1364 sizeof(ifilterfalseobject), /* tp_basicsize */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1365 0, /* tp_itemsize */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1366 /* methods */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1367 (destructor)ifilterfalse_dealloc, /* tp_dealloc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1368 0, /* tp_print */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1369 0, /* tp_getattr */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1370 0, /* tp_setattr */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1371 0, /* tp_compare */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1372 0, /* tp_repr */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1373 0, /* tp_as_number */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1374 0, /* tp_as_sequence */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1375 0, /* tp_as_mapping */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1376 0, /* tp_hash */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1377 0, /* tp_call */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1378 0, /* tp_str */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1379 PyObject_GenericGetAttr, /* tp_getattro */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1380 0, /* tp_setattro */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1381 0, /* tp_as_buffer */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1382 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1383 Py_TPFLAGS_BASETYPE, /* tp_flags */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1384 ifilterfalse_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1385 (traverseproc)ifilterfalse_traverse, /* tp_traverse */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1386 0, /* tp_clear */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1387 0, /* tp_richcompare */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1388 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1389 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1390 (iternextfunc)ifilterfalse_next, /* tp_iternext */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1391 0, /* tp_methods */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1392 0, /* tp_members */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1393 0, /* tp_getset */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1394 0, /* tp_base */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1395 0, /* tp_dict */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1396 0, /* tp_descr_get */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1397 0, /* tp_descr_set */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1398 0, /* tp_dictoffset */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1399 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1400 0, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1401 ifilterfalse_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1402 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1403 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1404
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1405
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1406 /* count object ************************************************************/
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 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1409 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1410 long cnt;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1411 } countobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1412
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1413 PyTypeObject count_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1414
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1415 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1416 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
1417 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1418 countobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1419 long cnt = 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1420
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1421 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
1422 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1423
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1424 /* create countobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1425 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
1426 if (lz == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1427 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1428 lz->cnt = cnt;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1429
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1430 return (PyObject *)lz;
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1433 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1434 count_next(countobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1435 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1436 return PyInt_FromLong(lz->cnt++);
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1439 PyDoc_STRVAR(count_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1440 "count([firstval]) --> count object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1441 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1442 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
1443 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
1444
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1445 PyTypeObject count_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1446 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1447 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1448 "itertools.count", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1449 sizeof(countobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1450 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1451 /* methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1452 (destructor)PyObject_Del, /* tp_dealloc */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1453 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1454 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1455 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1456 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1457 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1458 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1459 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1460 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1461 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1462 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1463 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1464 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1465 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1466 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1467 Py_TPFLAGS_DEFAULT, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1468 count_doc, /* tp_doc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1469 0, /* tp_traverse */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1470 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1471 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1472 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1473 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1474 (iternextfunc)count_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1475 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1476 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1477 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1478 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1479 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1480 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1481 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1482 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1483 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1484 0, /* tp_alloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1485 count_new, /* tp_new */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1486 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1487
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 /* izip object ************************************************************/
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 #include "Python.h"
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 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1494 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1495 long tuplesize;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1496 PyObject *ittuple; /* tuple of iterators */
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1497 PyObject *result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1498 } izipobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1499
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1500 PyTypeObject izip_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1501
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1502 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1503 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
1504 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1505 izipobject *lz;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1506 int i;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1507 PyObject *ittuple; /* tuple of iterators */
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1508 PyObject *result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1509 int tuplesize = PySequence_Length(args);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1510
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1511 if (tuplesize < 1) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1512 PyErr_SetString(PyExc_TypeError,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1513 "izip() requires at least one sequence");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1514 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1515 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1516
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1517 /* args must be a tuple */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1518 assert(PyTuple_Check(args));
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1519
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1520 /* obtain iterators */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1521 ittuple = PyTuple_New(tuplesize);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1522 if(ittuple == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1523 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1524 for (i=0; i < tuplesize; ++i) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1525 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
1526 PyObject *it = PyObject_GetIter(item);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1527 if (it == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1528 if (PyErr_ExceptionMatches(PyExc_TypeError))
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1529 PyErr_Format(PyExc_TypeError,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1530 "izip argument #%d must support iteration",
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1531 i+1);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1532 Py_DECREF(ittuple);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1533 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1534 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1535 PyTuple_SET_ITEM(ittuple, i, it);
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
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1538 /* create a result holder */
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1539 result = PyTuple_New(tuplesize);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1540 if (result == NULL) {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1541 Py_DECREF(ittuple);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1542 return NULL;
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1543 }
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1544 for (i=0 ; i < tuplesize ; i++) {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1545 Py_INCREF(Py_None);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1546 PyTuple_SET_ITEM(result, i, Py_None);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1547 }
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1548
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1549 /* create izipobject structure */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1550 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
1551 if (lz == NULL) {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1552 Py_DECREF(ittuple);
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1553 Py_DECREF(result);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1554 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1555 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1556 lz->ittuple = ittuple;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1557 lz->tuplesize = tuplesize;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1558 lz->result = result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1559
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1560 return (PyObject *)lz;
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
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1563 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1564 izip_dealloc(izipobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1565 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1566 PyObject_GC_UnTrack(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1567 Py_XDECREF(lz->ittuple);
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1568 Py_XDECREF(lz->result);
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1569 lz->ob_type->tp_free(lz);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1570 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1571
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1572 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1573 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
1574 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1575 if (lz->ittuple)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1576 return visit(lz->ittuple, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1577 return 0;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1578 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1579
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1580 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1581 izip_next(izipobject *lz)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1582 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1583 int i;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1584 long tuplesize = lz->tuplesize;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1585 PyObject *result = lz->result;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1586 PyObject *it;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1587 PyObject *item;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1588
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1589 if (result->ob_refcnt == 1) {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1590 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
1591 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
1592 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
1593 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
1594 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
1595 return NULL;
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1596 Py_DECREF(PyTuple_GET_ITEM(result, i));
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1597 PyTuple_SET_ITEM(result, i, item);
27747
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1598 }
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1599 Py_INCREF(result);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1600 } else {
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1601 result = PyTuple_New(tuplesize);
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1602 if (result == NULL)
941de791b17a SF bug #681003: itertools issues
Raymond Hettinger <python@rcn.com>
parents: 27570
diff changeset
1603 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
1604 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
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);
27748
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1608 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
1609 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
1610 return NULL;
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1611 }
59ae41e04ffb * Eliminated tuple re-use in imap(). Doing it correctly made the code
Raymond Hettinger <python@rcn.com>
parents: 27747
diff changeset
1612 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
1613 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1614 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1615 return result;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1616 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1617
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1618 PyDoc_STRVAR(izip_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1619 "izip(iter1 [,iter2 [...]]) --> izip object\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1620 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1621 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
1622 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
1623 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
1624 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
1625 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
1626 a list.");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1627
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1628 PyTypeObject izip_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1629 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1630 0, /* ob_size */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1631 "itertools.izip", /* tp_name */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1632 sizeof(izipobject), /* tp_basicsize */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1633 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1634 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1635 (destructor)izip_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1636 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1637 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1638 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1639 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1640 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1641 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1642 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1643 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1644 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1645 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1646 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1647 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1648 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1649 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1650 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
1651 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1652 izip_doc, /* tp_doc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1653 (traverseproc)izip_traverse, /* tp_traverse */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1654 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1655 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1656 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1657 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1658 (iternextfunc)izip_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1659 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1660 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1661 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1662 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1663 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1664 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1665 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1666 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1667 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1668 0, /* tp_alloc */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1669 izip_new, /* tp_new */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1670 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1671 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1672
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1673
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1674 /* repeat object ************************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1675
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1676 typedef struct {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1677 PyObject_HEAD
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1678 PyObject *element;
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1679 long cnt;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1680 } repeatobject;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1681
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1682 PyTypeObject repeat_type;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1683
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1684 static PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1685 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
1686 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1687 repeatobject *ro;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1688 PyObject *element;
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1689 long cnt = -1;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1690
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1691 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
1692 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1693
28945
001a90555780 * Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents: 28930
diff changeset
1694 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
1695 cnt = 0;
001a90555780 * Added a substantial number of edge case and argument tests for
Raymond Hettinger <python@rcn.com>
parents: 28930
diff changeset
1696
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1697 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
1698 if (ro == NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1699 return NULL;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1700 Py_INCREF(element);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1701 ro->element = element;
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1702 ro->cnt = cnt;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1703 return (PyObject *)ro;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1704 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1705
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1706 static void
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1707 repeat_dealloc(repeatobject *ro)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1708 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1709 PyObject_GC_UnTrack(ro);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1710 Py_XDECREF(ro->element);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1711 ro->ob_type->tp_free(ro);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1712 }
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1713
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1714 static int
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1715 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
1716 {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1717 if (ro->element)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1718 return visit(ro->element, arg);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1719 return 0;
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 PyObject *
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1723 repeat_next(repeatobject *ro)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1724 {
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1725 if (ro->cnt == 0)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1726 return NULL;
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1727 if (ro->cnt > 0)
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1728 ro->cnt--;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1729 Py_INCREF(ro->element);
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1730 return ro->element;
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1731 }
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 PyDoc_STRVAR(repeat_doc,
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1734 "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
1735 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
1736 endlessly.");
27570
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 PyTypeObject repeat_type = {
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1739 PyObject_HEAD_INIT(NULL)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1740 0, /* ob_size */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1741 "itertools.repeat", /* tp_name */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1742 sizeof(repeatobject), /* tp_basicsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1743 0, /* tp_itemsize */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1744 /* methods */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1745 (destructor)repeat_dealloc, /* tp_dealloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1746 0, /* tp_print */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1747 0, /* tp_getattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1748 0, /* tp_setattr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1749 0, /* tp_compare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1750 0, /* tp_repr */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1751 0, /* tp_as_number */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1752 0, /* tp_as_sequence */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1753 0, /* tp_as_mapping */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1754 0, /* tp_hash */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1755 0, /* tp_call */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1756 0, /* tp_str */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1757 PyObject_GenericGetAttr, /* tp_getattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1758 0, /* tp_setattro */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1759 0, /* tp_as_buffer */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1760 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
1761 Py_TPFLAGS_BASETYPE, /* tp_flags */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1762 repeat_doc, /* tp_doc */
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1763 (traverseproc)repeat_traverse, /* tp_traverse */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1764 0, /* tp_clear */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1765 0, /* tp_richcompare */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1766 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1767 PyObject_SelfIter, /* tp_iter */
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1768 (iternextfunc)repeat_next, /* tp_iternext */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1769 0, /* tp_methods */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1770 0, /* tp_members */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1771 0, /* tp_getset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1772 0, /* tp_base */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1773 0, /* tp_dict */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1774 0, /* tp_descr_get */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1775 0, /* tp_descr_set */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1776 0, /* tp_dictoffset */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1777 0, /* tp_init */
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 29225
diff changeset
1778 0, /* tp_alloc */
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1779 repeat_new, /* tp_new */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1780 PyObject_GC_Del, /* tp_free */
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1781 };
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1782
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1783
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1784 /* module level code ********************************************************/
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1785
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1786 PyDoc_STRVAR(module_doc,
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1787 "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
1788 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1789 Infinite iterators:\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1790 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
1791 cycle(p) --> p0, p1, ... plast, p0, p1, ...\n\
28601
58db5fbd9c28 Fix docstring typo
Andrew M. Kuchling <amk@amk.ca>
parents: 28320
diff changeset
1792 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
1793 \n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1794 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
1795 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
1796 ifilter(pred, seq) --> elements of seq where pred(elem) is True\n\
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1797 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
1798 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
1799 seq[start:stop:step]\n\
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1800 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
1801 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
1802 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
1803 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
1804 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
1805 ");
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1806
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1807
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1808 PyMODINIT_FUNC
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1809 inititertools(void)
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1810 {
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1811 int i;
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1812 PyObject *m;
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1813 char *name;
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1814 PyTypeObject *typelist[] = {
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1815 &cycle_type,
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1816 &dropwhile_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1817 &takewhile_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1818 &islice_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1819 &starmap_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1820 &imap_type,
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1821 &chain_type,
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1822 &ifilter_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1823 &ifilterfalse_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1824 &count_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1825 &izip_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1826 &repeat_type,
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1827 NULL
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1828 };
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1829
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1830 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
1831
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1832 for (i=0 ; typelist[i] != NULL ; i++) {
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1833 if (PyType_Ready(typelist[i]) < 0)
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1834 return;
29225
f3f306457b43 Fixed dotted name assertion.
Raymond Hettinger <python@rcn.com>
parents: 28945
diff changeset
1835 name = strchr(typelist[i]->tp_name, '.');
28024
61fa6b55c1de User requested changes to the itertools module.
Raymond Hettinger <python@rcn.com>
parents: 27770
diff changeset
1836 assert (name != NULL);
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1837 Py_INCREF(typelist[i]);
29225
f3f306457b43 Fixed dotted name assertion.
Raymond Hettinger <python@rcn.com>
parents: 28945
diff changeset
1838 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
27769
dc5e1d918055 C Code:
Raymond Hettinger <python@rcn.com>
parents: 27748
diff changeset
1839 }
27570
ba57c3daf6e9 Move itertools module from the sandbox and into production.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1840 }