annotate Objects/iterobject.c @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents fd25030223b0
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
6 PyObject_HEAD
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
7 long it_index;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
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
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
16 if (!PySequence_Check(seq)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
17 PyErr_BadInternalCall();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
18 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
19 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
20 it = PyObject_GC_New(seqiterobject, &PySeqIter_Type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
21 if (it == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
22 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
23 it->it_index = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
24 Py_INCREF(seq);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
25 it->it_seq = seq;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
26 _PyObject_GC_TRACK(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
27 return (PyObject *)it;
17598
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
33 _PyObject_GC_UNTRACK(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
34 Py_XDECREF(it->it_seq);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
41 Py_VISIT(it->it_seq);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
42 return 0;
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
43 }
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
44
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 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
46 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
47 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
48 seqiterobject *it;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
49 PyObject *seq;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
50 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
51
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
52 assert(PySeqIter_Check(iterator));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
53 it = (seqiterobject *)iterator;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
54 seq = it->it_seq;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
55 if (seq == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
56 return NULL;
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
58 result = PySequence_GetItem(seq, it->it_index);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
59 if (result != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
60 it->it_index++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
61 return result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
62 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
63 if (PyErr_ExceptionMatches(PyExc_IndexError) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
64 PyErr_ExceptionMatches(PyExc_StopIteration))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
65 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
66 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
67 Py_DECREF(seq);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
68 it->it_seq = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
69 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
70 return NULL;
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 33386
diff changeset
73 static PyObject *
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 29587
diff changeset
74 iter_len(seqiterobject *it)
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 29587
diff changeset
75 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
76 Py_ssize_t seqsize, len;
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31686
diff changeset
77
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
78 if (it->it_seq) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
79 seqsize = PySequence_Size(it->it_seq);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
80 if (seqsize == -1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
81 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
82 len = seqsize - it->it_index;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
83 if (len >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
84 return PyInt_FromSsize_t(len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
85 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
86 return PyInt_FromLong(0);
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 29587
diff changeset
87 }
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 29587
diff changeset
88
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
89 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 33386
diff changeset
90
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 33386
diff changeset
91 static PyMethodDef seqiter_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
92 {"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
93 {NULL, NULL} /* sentinel */
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 29587
diff changeset
94 };
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 29587
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 = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
97 PyVarObject_HEAD_INIT(&PyType_Type, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
98 "iterator", /* tp_name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
99 sizeof(seqiterobject), /* tp_basicsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
100 0, /* tp_itemsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
101 /* methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
102 (destructor)iter_dealloc, /* tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
103 0, /* tp_print */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
104 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
105 0, /* tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
106 0, /* tp_compare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
107 0, /* tp_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
108 0, /* tp_as_number */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
109 0, /* tp_as_sequence */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
110 0, /* tp_as_mapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
111 0, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
112 0, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
113 0, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
114 PyObject_GenericGetAttr, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
115 0, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
116 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
117 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
118 0, /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
119 (traverseproc)iter_traverse, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
120 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
121 0, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
122 0, /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
123 PyObject_SelfIter, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
124 iter_iternext, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
125 seqiter_methods, /* tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
126 0, /* tp_members */
17598
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
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 /* -------------------------------------- */
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 typedef struct {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
132 PyObject_HEAD
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
133 PyObject *it_callable; /* Set to NULL when iterator is exhausted */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
134 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
135 } calliterobject;
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 PyObject *
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 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
139 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
140 calliterobject *it;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
141 it = PyObject_GC_New(calliterobject, &PyCallIter_Type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
142 if (it == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
143 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
144 Py_INCREF(callable);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
145 it->it_callable = callable;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
146 Py_INCREF(sentinel);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
147 it->it_sentinel = sentinel;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
148 _PyObject_GC_TRACK(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
149 return (PyObject *)it;
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 static void
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 calliter_dealloc(calliterobject *it)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
154 _PyObject_GC_UNTRACK(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
155 Py_XDECREF(it->it_callable);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
156 Py_XDECREF(it->it_sentinel);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
157 PyObject_GC_Del(it);
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 }
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
159
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
160 static int
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
161 calliter_traverse(calliterobject *it, visitproc visit, void *arg)
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
162 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
163 Py_VISIT(it->it_callable);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
164 Py_VISIT(it->it_sentinel);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
165 return 0;
18511
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
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 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
169 calliter_iternext(calliterobject *it)
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
171 if (it->it_callable != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
172 PyObject *args = PyTuple_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
173 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
174 if (args == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
175 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
176 result = PyObject_Call(it->it_callable, args, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
177 Py_DECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
178 if (result != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
179 int ok;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
180 ok = PyObject_RichCompareBool(result,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
181 it->it_sentinel,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
182 Py_EQ);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
183 if (ok == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
184 return result; /* Common case, fast path */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
185 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
186 if (ok > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
187 Py_CLEAR(it->it_callable);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
188 Py_CLEAR(it->it_sentinel);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
189 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
190 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
191 else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
192 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
193 Py_CLEAR(it->it_callable);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
194 Py_CLEAR(it->it_sentinel);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
195 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
196 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
197 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
198 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
199
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 PyTypeObject PyCallIter_Type = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
201 PyVarObject_HEAD_INIT(&PyType_Type, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
202 "callable-iterator", /* tp_name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
203 sizeof(calliterobject), /* tp_basicsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
204 0, /* tp_itemsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
205 /* methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
206 (destructor)calliter_dealloc, /* tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
207 0, /* tp_print */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
208 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
209 0, /* tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
210 0, /* tp_compare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
211 0, /* tp_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
212 0, /* tp_as_number */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
213 0, /* tp_as_sequence */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
214 0, /* tp_as_mapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
215 0, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
216 0, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
217 0, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
218 PyObject_GenericGetAttr, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
219 0, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
220 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
221 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
222 0, /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
223 (traverseproc)calliter_traverse, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
224 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
225 0, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
226 0, /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
227 PyObject_SelfIter, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
228 (iternextfunc)calliter_iternext, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 42333
diff changeset
229 0, /* tp_methods */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 };