annotate Objects/iterobject.c @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 6620a510e9a9
children fd25030223b0
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 {
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37304
diff changeset
41 Py_VISIT(it->it_seq);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37304
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 {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
48 seqiterobject *it;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
49 PyObject *seq;
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
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
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17598
diff changeset
52 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
53 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
54 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
55 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
56 return NULL;
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
58 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
59 if (result != NULL) {
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
60 it->it_index++;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
61 return result;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
62 }
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
63 if (PyErr_ExceptionMatches(PyExc_IndexError) ||
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
64 PyErr_ExceptionMatches(PyExc_StopIteration))
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
65 {
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
diff changeset
66 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
67 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
68 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
69 }
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24258
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 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
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
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31686
diff changeset
78 if (it->it_seq) {
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31686
diff changeset
79 seqsize = PySequence_Size(it->it_seq);
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31686
diff changeset
80 if (seqsize == -1)
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 33386
diff changeset
81 return NULL;
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31686
diff changeset
82 len = seqsize - it->it_index;
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31686
diff changeset
83 if (len >= 0)
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
84 return PyInt_FromSsize_t(len);
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31686
diff changeset
85 }
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 33386
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[] = {
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
92 {"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc},
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 33386
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 = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 37756
diff changeset
97 PyVarObject_HEAD_INIT(&PyType_Type, 0)
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 "iterator", /* tp_name */
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
99 sizeof(seqiterobject), /* tp_basicsize */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 0, /* tp_itemsize */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 /* methods */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 (destructor)iter_dealloc, /* tp_dealloc */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
104 0, /* tp_getattr */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 0, /* tp_setattr */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 0, /* tp_compare */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 0, /* tp_repr */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 33386
diff changeset
109 0, /* tp_as_sequence */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 0, /* tp_as_mapping */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 0, /* tp_hash */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 0, /* tp_call */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
114 PyObject_GenericGetAttr, /* tp_getattro */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 0, /* tp_setattro */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 0, /* tp_as_buffer */
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
117 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
118 0, /* tp_doc */
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
119 (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
120 0, /* tp_clear */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 0, /* tp_richcompare */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
123 PyObject_SelfIter, /* tp_iter */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36519
diff changeset
124 iter_iternext, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 33386
diff changeset
125 seqiter_methods, /* tp_methods */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
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 {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 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
133 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
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 {
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 calliterobject *it;
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
141 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
142 if (it == NULL)
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 return NULL;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 Py_INCREF(callable);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 it->it_callable = callable;
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 Py_INCREF(sentinel);
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 it->it_sentinel = sentinel;
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
148 _PyObject_GC_TRACK(it);
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 return (PyObject *)it;
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 {
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
154 _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
155 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
156 Py_XDECREF(it->it_sentinel);
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
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 {
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37304
diff changeset
163 Py_VISIT(it->it_callable);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37304
diff changeset
164 Py_VISIT(it->it_sentinel);
18511
d211709f25f1 GC for iterator objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 17647
diff changeset
165 return 0;
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 {
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
171 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
172 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
173 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
174 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
175 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
176 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
177 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
178 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
179 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
180 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
181 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
182 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
183 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
184 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
185 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
186 if (ok > 0) {
33386
c2c49ee727d0 SF patch #1020188: Use Py_CLEAR where necessary to avoid crashes
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
187 Py_CLEAR(it->it_callable);
c2c49ee727d0 SF patch #1020188: Use Py_CLEAR where necessary to avoid crashes
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
188 Py_CLEAR(it->it_sentinel);
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
189 }
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
190 }
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
diff changeset
191 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
192 PyErr_Clear();
33386
c2c49ee727d0 SF patch #1020188: Use Py_CLEAR where necessary to avoid crashes
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
193 Py_CLEAR(it->it_callable);
c2c49ee727d0 SF patch #1020188: Use Py_CLEAR where necessary to avoid crashes
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
194 Py_CLEAR(it->it_sentinel);
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 }
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 }
24258
eac54a83272f Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23606
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 = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 37756
diff changeset
201 PyVarObject_HEAD_INIT(&PyType_Type, 0)
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 "callable-iterator", /* tp_name */
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
203 sizeof(calliterobject), /* tp_basicsize */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 0, /* tp_itemsize */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 /* methods */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 (destructor)calliter_dealloc, /* tp_dealloc */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
208 0, /* tp_getattr */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 0, /* tp_setattr */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 0, /* tp_compare */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 0, /* tp_repr */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 0, /* tp_as_number */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 0, /* tp_as_sequence */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 0, /* tp_as_mapping */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 0, /* tp_hash */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 0, /* tp_call */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18511
diff changeset
218 PyObject_GenericGetAttr, /* tp_getattro */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 0, /* tp_setattro */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 0, /* tp_as_buffer */
22396
6706b83d1bd1 Re-enable GC of iter objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
221 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
29587
7f79ead712f5 Whitespace normalization.
Walter Dörwald <walter@livinglogic.de>
parents: 28320
diff changeset
222 0, /* tp_doc */
7f79ead712f5 Whitespace normalization.
Walter Dörwald <walter@livinglogic.de>
parents: 28320
diff changeset
223 (traverseproc)calliter_traverse, /* tp_traverse */
7f79ead712f5 Whitespace normalization.
Walter Dörwald <walter@livinglogic.de>
parents: 28320
diff changeset
224 0, /* tp_clear */
17598
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 0, /* tp_richcompare */
c3bb0ff396dc Adding iterobject.[ch], which were accidentally not added. Sorry\!
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
227 PyObject_SelfIter, /* 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
228 (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
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 };