annotate Objects/iterobject.c @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 7ca46e08200d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Iterator objects */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 #include "Python.h"
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 typedef struct {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 PyObject_HEAD
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 long it_index;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 PyObject *it_seq;
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
9 } seqiterobject;
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 PyObject *
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
12 PySeqIter_New(PyObject *seq)
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 {
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
14 seqiterobject *it;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
15 it = PyObject_NEW(seqiterobject, &PySeqIter_Type);
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 if (it == NULL)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 return NULL;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 it->it_index = 0;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 Py_INCREF(seq);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 it->it_seq = seq;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 return (PyObject *)it;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 static void
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
24 iter_dealloc(seqiterobject *it)
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 Py_DECREF(it->it_seq);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 PyObject_DEL(it);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
30 static int
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
31 iter_traverse(seqiterobject *it, visitproc visit, void *arg)
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
32 {
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
33 return visit(it->it_seq, arg);
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
34 }
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
35
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
37 iter_next(seqiterobject *it)
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 PyObject *seq = it->it_seq;
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
40 PyObject *result = PySequence_GetItem(seq, it->it_index++);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
41
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
42 if (result == NULL && PyErr_ExceptionMatches(PyExc_IndexError))
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
43 PyErr_SetObject(PyExc_StopIteration, Py_None);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
44 return result;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
45 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
46
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
47 static PyObject *
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
48 iter_getiter(PyObject *it)
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
49 {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
50 Py_INCREF(it);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
51 return it;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
52 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
53
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
54 static PyObject *
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
55 iter_iternext(PyObject *iterator)
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
56 {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
57 seqiterobject *it;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
58 PyObject *seq;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
59
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
60 assert(PySeqIter_Check(iterator));
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
61 it = (seqiterobject *)iterator;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
62 seq = it->it_seq;
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 if (PyList_Check(seq)) {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 PyObject *item;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 if (it->it_index >= PyList_GET_SIZE(seq)) {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 return NULL;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 item = PyList_GET_ITEM(seq, it->it_index);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 it->it_index++;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 Py_INCREF(item);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 return item;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 else {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 PyObject *result = PySequence_GetItem(seq, it->it_index++);
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
76 if (result != NULL) {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
77 return result;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
78 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
79 if (PyErr_ExceptionMatches(PyExc_IndexError) ||
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
80 PyErr_ExceptionMatches(PyExc_StopIteration)) {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
81 PyErr_Clear();
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
82 return NULL;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
83 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
84 else {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
85 return NULL;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
86 }
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 static PyMethodDef iter_methods[] = {
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
91 {"next", (PyCFunction)iter_next, METH_NOARGS,
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 "it.next() -- get the next value, or raise StopIteration"},
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 {NULL, NULL} /* sentinel */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 };
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
96 PyTypeObject PySeqIter_Type = {
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 PyObject_HEAD_INIT(&PyType_Type)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 0, /* ob_size */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 "iterator", /* tp_name */
24567
7ca46e08200d Remove calls to 2.1 GC API (they are noops).
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
100 sizeof(seqiterobject), /* tp_basicsize */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 0, /* tp_itemsize */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 /* methods */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 (destructor)iter_dealloc, /* tp_dealloc */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
105 0, /* tp_getattr */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 0, /* tp_setattr */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 0, /* tp_compare */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 0, /* tp_repr */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 0, /* tp_as_number */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 0, /* tp_as_sequence */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 0, /* tp_as_mapping */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 0, /* tp_hash */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 0, /* tp_call */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
115 PyObject_GenericGetAttr, /* tp_getattro */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 0, /* tp_setattro */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 0, /* tp_as_buffer */
24567
7ca46e08200d Remove calls to 2.1 GC API (they are noops).
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
118 Py_TPFLAGS_DEFAULT, /* tp_flags */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 0, /* tp_doc */
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
120 (traverseproc)iter_traverse, /* tp_traverse */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 0, /* tp_clear */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 0, /* tp_richcompare */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 0, /* tp_weaklistoffset */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 (getiterfunc)iter_getiter, /* tp_iter */
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
125 (iternextfunc)iter_iternext, /* tp_iternext */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
126 iter_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
127 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
128 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
129 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
130 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
131 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
132 0, /* tp_descr_set */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 };
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 /* -------------------------------------- */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 typedef struct {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 PyObject_HEAD
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 PyObject *it_callable;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 PyObject *it_sentinel;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 } calliterobject;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 PyObject *
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 PyCallIter_New(PyObject *callable, PyObject *sentinel)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 calliterobject *it;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 it = PyObject_NEW(calliterobject, &PyCallIter_Type);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 if (it == NULL)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 return NULL;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 Py_INCREF(callable);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 it->it_callable = callable;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 Py_INCREF(sentinel);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 it->it_sentinel = sentinel;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 return (PyObject *)it;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 static void
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 calliter_dealloc(calliterobject *it)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 Py_DECREF(it->it_callable);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 Py_DECREF(it->it_sentinel);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 PyObject_DEL(it);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 }
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
163
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
164 static int
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
165 calliter_traverse(calliterobject *it, visitproc visit, void *arg)
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
166 {
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
167 int err;
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
168 if ((err = visit(it->it_callable, arg)))
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
169 return err;
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
170 if ((err = visit(it->it_sentinel, arg)))
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
171 return err;
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
172 return 0;
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
173 }
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
174
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 static PyObject *
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 calliter_next(calliterobject *it, PyObject *args)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 PyObject *result = PyObject_CallObject(it->it_callable, NULL);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 if (result != NULL) {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 if (PyObject_RichCompareBool(result, it->it_sentinel, Py_EQ)) {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 PyErr_SetObject(PyExc_StopIteration, Py_None);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 Py_DECREF(result);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 result = NULL;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 return result;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
188
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 static PyMethodDef calliter_methods[] = {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 {"next", (PyCFunction)calliter_next, METH_VARARGS,
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 "it.next() -- get the next value, or raise StopIteration"},
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 {NULL, NULL} /* sentinel */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 };
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 static PyObject *
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
196 calliter_iternext(calliterobject *it)
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
197 {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
198 PyObject *result = PyObject_CallObject(it->it_callable, NULL);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
199 if (result != NULL) {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
200 if (PyObject_RichCompareBool(result, it->it_sentinel, Py_EQ)) {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
201 Py_DECREF(result);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
202 result = NULL;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
203 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
204 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
205 else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
206 PyErr_Clear();
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
207 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
208 return result;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
209 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
210
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 PyTypeObject PyCallIter_Type = {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 PyObject_HEAD_INIT(&PyType_Type)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 0, /* ob_size */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 "callable-iterator", /* tp_name */
24567
7ca46e08200d Remove calls to 2.1 GC API (they are noops).
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
215 sizeof(calliterobject), /* tp_basicsize */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 0, /* tp_itemsize */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 /* methods */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 (destructor)calliter_dealloc, /* tp_dealloc */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
220 0, /* tp_getattr */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 0, /* tp_setattr */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 0, /* tp_compare */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 0, /* tp_repr */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 0, /* tp_as_number */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 0, /* tp_as_sequence */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 0, /* tp_as_mapping */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 0, /* tp_hash */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 0, /* tp_call */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
230 PyObject_GenericGetAttr, /* tp_getattro */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 0, /* tp_setattro */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 0, /* tp_as_buffer */
24567
7ca46e08200d Remove calls to 2.1 GC API (they are noops).
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
233 Py_TPFLAGS_DEFAULT, /* tp_flags */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 0, /* tp_doc */
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
235 (traverseproc)calliter_traverse, /* tp_traverse */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 0, /* tp_clear */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 0, /* tp_richcompare */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 0, /* tp_weaklistoffset */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 (getiterfunc)iter_getiter, /* tp_iter */
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
240 (iternextfunc)calliter_iternext, /* tp_iternext */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
241 calliter_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
242 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
243 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
244 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
245 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
246 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
247 0, /* tp_descr_set */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 };