iterobject.c 5.19 KB
Newer Older
1 2 3 4 5 6 7
/* Iterator objects */

#include "Python.h"

typedef struct {
	PyObject_HEAD
	long      it_index;
8
	PyObject *it_seq; /* Set to NULL when iterator is exhausted */
9
} seqiterobject;
10 11

PyObject *
12
PySeqIter_New(PyObject *seq)
13
{
14
	seqiterobject *it;
15 16 17 18 19

	if (!PySequence_Check(seq)) {
		PyErr_BadInternalCall();
		return NULL;
	}	
20
	it = PyObject_GC_New(seqiterobject, &PySeqIter_Type);
21 22 23 24 25
	if (it == NULL)
		return NULL;
	it->it_index = 0;
	Py_INCREF(seq);
	it->it_seq = seq;
26
	_PyObject_GC_TRACK(it);
27 28
	return (PyObject *)it;
}
29

30
static void
31
iter_dealloc(seqiterobject *it)
32
{
33
	_PyObject_GC_UNTRACK(it);
34
	Py_XDECREF(it->it_seq);
35
	PyObject_GC_Del(it);
36 37
}

38 39 40
static int
iter_traverse(seqiterobject *it, visitproc visit, void *arg)
{
41 42
	Py_VISIT(it->it_seq);
	return 0;
43 44
}

45 46 47 48 49
static PyObject *
iter_iternext(PyObject *iterator)
{
	seqiterobject *it;
	PyObject *seq;
50
	PyObject *result;
51 52 53 54

	assert(PySeqIter_Check(iterator));
	it = (seqiterobject *)iterator;
	seq = it->it_seq;
55 56
	if (seq == NULL)
		return NULL;
57

58 59 60 61 62 63 64 65 66
	result = PySequence_GetItem(seq, it->it_index);
	if (result != NULL) {
		it->it_index++;
		return result;
	}
	if (PyErr_ExceptionMatches(PyExc_IndexError) ||
	    PyErr_ExceptionMatches(PyExc_StopIteration))
	{
		PyErr_Clear();
67 68
		Py_DECREF(seq);
		it->it_seq = NULL;
69
	}
70
	return NULL;
71 72
}

73
static PyObject *
74 75
iter_len(seqiterobject *it)
{
Martin v. Löwis's avatar
Martin v. Löwis committed
76
	Py_ssize_t seqsize, len;
77 78 79 80

	if (it->it_seq) {
		seqsize = PySequence_Size(it->it_seq);
		if (seqsize == -1)
81
			return NULL;
82 83
		len = seqsize - it->it_index;
		if (len >= 0)
84
			return PyInt_FromSsize_t(len);
85
	}
86
	return PyInt_FromLong(0);
87 88
}

89
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
90 91

static PyMethodDef seqiter_methods[] = {
92
	{"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc},
93
 	{NULL,		NULL}		/* sentinel */
94 95
};

96
PyTypeObject PySeqIter_Type = {
97 98 99
	PyObject_HEAD_INIT(&PyType_Type)
	0,					/* ob_size */
	"iterator",				/* tp_name */
100
	sizeof(seqiterobject),			/* tp_basicsize */
101 102 103 104
	0,					/* tp_itemsize */
	/* methods */
	(destructor)iter_dealloc, 		/* tp_dealloc */
	0,					/* tp_print */
105
	0,					/* tp_getattr */
106 107 108 109
	0,					/* tp_setattr */
	0,					/* tp_compare */
	0,					/* tp_repr */
	0,					/* tp_as_number */
110
	0,					/* tp_as_sequence */
111 112 113 114
	0,					/* tp_as_mapping */
	0,					/* tp_hash */
	0,					/* tp_call */
	0,					/* tp_str */
115
	PyObject_GenericGetAttr,		/* tp_getattro */
116 117
	0,					/* tp_setattro */
	0,					/* tp_as_buffer */
118
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
119
 	0,					/* tp_doc */
120
 	(traverseproc)iter_traverse,		/* tp_traverse */
121 122 123
 	0,					/* tp_clear */
	0,					/* tp_richcompare */
	0,					/* tp_weaklistoffset */
124
	PyObject_SelfIter,			/* tp_iter */
125
	iter_iternext,				/* tp_iternext */
126
	seqiter_methods,			/* tp_methods */
127
	0,					/* tp_members */
128 129 130 131 132 133
};

/* -------------------------------------- */

typedef struct {
	PyObject_HEAD
134 135
	PyObject *it_callable; /* Set to NULL when iterator is exhausted */
	PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */
136 137 138 139 140 141
} calliterobject;

PyObject *
PyCallIter_New(PyObject *callable, PyObject *sentinel)
{
	calliterobject *it;
142
	it = PyObject_GC_New(calliterobject, &PyCallIter_Type);
143 144 145 146 147 148
	if (it == NULL)
		return NULL;
	Py_INCREF(callable);
	it->it_callable = callable;
	Py_INCREF(sentinel);
	it->it_sentinel = sentinel;
149
	_PyObject_GC_TRACK(it);
150 151 152 153 154
	return (PyObject *)it;
}
static void
calliter_dealloc(calliterobject *it)
{
155
	_PyObject_GC_UNTRACK(it);
156 157
	Py_XDECREF(it->it_callable);
	Py_XDECREF(it->it_sentinel);
158
	PyObject_GC_Del(it);
159
}
160

161 162 163
static int
calliter_traverse(calliterobject *it, visitproc visit, void *arg)
{
164 165
	Py_VISIT(it->it_callable);
	Py_VISIT(it->it_sentinel);
166 167 168
	return 0;
}

169 170 171
static PyObject *
calliter_iternext(calliterobject *it)
{
172
	if (it->it_callable != NULL) {
173 174 175 176 177 178
		PyObject *args = PyTuple_New(0);
		PyObject *result;
		if (args == NULL)
			return NULL;
		result = PyObject_Call(it->it_callable, args, NULL);
		Py_DECREF(args);
179 180 181 182 183 184 185
		if (result != NULL) {
			int ok;
			ok = PyObject_RichCompareBool(result,
						      it->it_sentinel,
						      Py_EQ);
			if (ok == 0)
				return result; /* Common case, fast path */
186
			Py_DECREF(result);
187
			if (ok > 0) {
188 189
				Py_CLEAR(it->it_callable);
				Py_CLEAR(it->it_sentinel);
190 191 192 193
			}
		}
		else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
			PyErr_Clear();
194 195
			Py_CLEAR(it->it_callable);
			Py_CLEAR(it->it_sentinel);
196 197
		}
	}
198
	return NULL;
199 200
}

201 202 203 204
PyTypeObject PyCallIter_Type = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,					/* ob_size */
	"callable-iterator",			/* tp_name */
205
	sizeof(calliterobject),			/* tp_basicsize */
206 207 208 209
	0,					/* tp_itemsize */
	/* methods */
	(destructor)calliter_dealloc, 		/* tp_dealloc */
	0,					/* tp_print */
210
	0,					/* tp_getattr */
211 212 213 214 215 216 217 218 219
	0,					/* tp_setattr */
	0,					/* tp_compare */
	0,					/* tp_repr */
	0,					/* tp_as_number */
	0,					/* tp_as_sequence */
	0,					/* tp_as_mapping */
	0,					/* tp_hash */
	0,					/* tp_call */
	0,					/* tp_str */
220
	PyObject_GenericGetAttr,		/* tp_getattro */
221 222
	0,					/* tp_setattro */
	0,					/* tp_as_buffer */
223
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
224 225 226
	0,					/* tp_doc */
	(traverseproc)calliter_traverse,	/* tp_traverse */
	0,					/* tp_clear */
227 228
	0,					/* tp_richcompare */
	0,					/* tp_weaklistoffset */
229
	PyObject_SelfIter,			/* tp_iter */
230
	(iternextfunc)calliter_iternext,	/* tp_iternext */
231
	0,					/* tp_methods */
232
};