annotate Objects/iterobject.c @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents 255d1d3e66a3
children 9c3572a5575f
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;
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
8 PyObject *it_seq; /* Set to NULL when iterator is exhausted */
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;
23397
0d8740094bec Patch #552433: Special-case tuples. Avoid sub-type checking for lists.
Martin v. Löwis <martin@v.loewis.de>
parents: 22396
diff changeset
15
0d8740094bec Patch #552433: Special-case tuples. Avoid sub-type checking for lists.
Martin v. Löwis <martin@v.loewis.de>
parents: 22396
diff changeset
16 if (!PySequence_Check(seq)) {
0d8740094bec Patch #552433: Special-case tuples. Avoid sub-type checking for lists.
Martin v. Löwis <martin@v.loewis.de>
parents: 22396
diff changeset
17 PyErr_BadInternalCall();
0d8740094bec Patch #552433: Special-case tuples. Avoid sub-type checking for lists.
Martin v. Löwis <martin@v.loewis.de>
parents: 22396
diff changeset
18 return NULL;
0d8740094bec Patch #552433: Special-case tuples. Avoid sub-type checking for lists.
Martin v. Löwis <martin@v.loewis.de>
parents: 22396
diff changeset
19 }
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
20 it = PyObject_GC_New(seqiterobject, &PySeqIter_Type);
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 if (it == NULL)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 return NULL;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 it->it_index = 0;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 Py_INCREF(seq);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 it->it_seq = seq;
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
26 _PyObject_GC_TRACK(it);
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 return (PyObject *)it;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 }
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
29
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 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
31 iter_dealloc(seqiterobject *it)
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 {
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
33 _PyObject_GC_UNTRACK(it);
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
34 Py_XDECREF(it->it_seq);
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
35 PyObject_GC_Del(it);
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
38 static int
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
39 iter_traverse(seqiterobject *it, visitproc visit, void *arg)
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
40 {
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
41 if (it->it_seq == NULL)
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
42 return 0;
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
43 return visit(it->it_seq, arg);
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
44 }
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
45
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 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
47 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
48 {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
49 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
50 return it;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
51 }
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 static PyObject *
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
54 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
55 {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
56 seqiterobject *it;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
57 PyObject *seq;
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
58 PyObject *result;
17617
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;
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
63 if (seq == NULL)
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
64 return NULL;
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
66 result = PySequence_GetItem(seq, it->it_index);
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
67 if (result != NULL) {
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
68 it->it_index++;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
69 return result;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
70 }
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
71 if (PyErr_ExceptionMatches(PyExc_IndexError) ||
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
72 PyErr_ExceptionMatches(PyExc_StopIteration))
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
73 {
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
74 PyErr_Clear();
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
75 Py_DECREF(seq);
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
76 it->it_seq = NULL;
23397
0d8740094bec Patch #552433: Special-case tuples. Avoid sub-type checking for lists.
Martin v. Löwis <martin@v.loewis.de>
parents: 22396
diff changeset
77 }
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
78 return NULL;
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
81 PyTypeObject PySeqIter_Type = {
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 PyObject_HEAD_INIT(&PyType_Type)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 0, /* ob_size */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 "iterator", /* tp_name */
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
85 sizeof(seqiterobject), /* tp_basicsize */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 0, /* tp_itemsize */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 /* methods */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 (destructor)iter_dealloc, /* tp_dealloc */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
90 0, /* tp_getattr */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 0, /* tp_setattr */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 0, /* tp_compare */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 0, /* tp_repr */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 0, /* tp_as_number */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 0, /* tp_as_sequence */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 0, /* tp_as_mapping */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 0, /* tp_hash */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 0, /* tp_call */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
100 PyObject_GenericGetAttr, /* tp_getattro */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 0, /* tp_setattro */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 0, /* tp_as_buffer */
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
103 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 0, /* tp_doc */
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
105 (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
106 0, /* tp_clear */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 0, /* tp_richcompare */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 0, /* tp_weaklistoffset */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 (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
110 (iternextfunc)iter_iternext, /* tp_iternext */
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
111 0, /* tp_methods */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
112 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
113 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
114 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
115 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
116 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
117 0, /* tp_descr_set */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 };
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 /* -------------------------------------- */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 typedef struct {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 PyObject_HEAD
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
124 PyObject *it_callable; /* Set to NULL when iterator is exhausted */
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
125 PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 } calliterobject;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 PyObject *
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 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
130 {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 calliterobject *it;
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
132 it = PyObject_GC_New(calliterobject, &PyCallIter_Type);
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 if (it == NULL)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 return NULL;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 Py_INCREF(callable);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 it->it_callable = callable;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 Py_INCREF(sentinel);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 it->it_sentinel = sentinel;
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
139 _PyObject_GC_TRACK(it);
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 return (PyObject *)it;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 static void
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 calliter_dealloc(calliterobject *it)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 {
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
145 _PyObject_GC_UNTRACK(it);
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
146 Py_XDECREF(it->it_callable);
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
147 Py_XDECREF(it->it_sentinel);
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
148 PyObject_GC_Del(it);
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 }
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
150
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
151 static int
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
152 calliter_traverse(calliterobject *it, visitproc visit, void *arg)
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
153 {
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
154 int err;
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
155 if (it->it_callable != NULL && (err = visit(it->it_callable, arg)))
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
156 return err;
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
157 if (it->it_sentinel != NULL && (err = visit(it->it_sentinel, arg)))
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
158 return err;
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
159 return 0;
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
160 }
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
161
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 static PyObject *
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
163 calliter_iternext(calliterobject *it)
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 {
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
165 if (it->it_callable != NULL) {
24867
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24683
diff changeset
166 PyObject *args = PyTuple_New(0);
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24683
diff changeset
167 PyObject *result;
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24683
diff changeset
168 if (args == NULL)
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24683
diff changeset
169 return NULL;
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24683
diff changeset
170 result = PyObject_Call(it->it_callable, args, NULL);
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24683
diff changeset
171 Py_DECREF(args);
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
172 if (result != NULL) {
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
173 int ok;
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
174 ok = PyObject_RichCompareBool(result,
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
175 it->it_sentinel,
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
176 Py_EQ);
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
177 if (ok == 0)
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
178 return result; /* Common case, fast path */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 Py_DECREF(result);
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
180 if (ok > 0) {
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
181 Py_DECREF(it->it_callable);
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
182 it->it_callable = NULL;
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
183 Py_DECREF(it->it_sentinel);
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
184 it->it_sentinel = NULL;
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
185 }
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
186 }
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
187 else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
188 PyErr_Clear();
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
189 Py_DECREF(it->it_callable);
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
190 it->it_callable = NULL;
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
191 Py_DECREF(it->it_sentinel);
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
192 it->it_sentinel = NULL;
17598
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 }
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
195 return NULL;
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
196 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
197
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 PyTypeObject PyCallIter_Type = {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 PyObject_HEAD_INIT(&PyType_Type)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 0, /* ob_size */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 "callable-iterator", /* tp_name */
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
202 sizeof(calliterobject), /* tp_basicsize */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 0, /* tp_itemsize */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 /* methods */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 (destructor)calliter_dealloc, /* tp_dealloc */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
207 0, /* tp_getattr */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 0, /* tp_setattr */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 0, /* tp_compare */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 0, /* tp_repr */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 0, /* tp_as_number */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 0, /* tp_as_sequence */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 0, /* tp_as_mapping */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 0, /* tp_hash */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 0, /* tp_call */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
217 PyObject_GenericGetAttr, /* tp_getattro */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 0, /* tp_setattro */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 0, /* tp_as_buffer */
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
220 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 0, /* tp_doc */
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
222 (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
223 0, /* tp_clear */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 0, /* tp_richcompare */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 0, /* tp_weaklistoffset */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 (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
227 (iternextfunc)calliter_iternext, /* tp_iternext */
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
228 0, /* tp_methods */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
229 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
230 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
231 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
232 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
233 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
234 0, /* tp_descr_set */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 };