iterobject.c 5.21 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
	if (it->it_seq == NULL)
		return 0;
43 44 45
	return visit(it->it_seq, arg);
}

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

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

59 60 61 62 63 64 65 66 67
	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();
68 69
		Py_DECREF(seq);
		it->it_seq = NULL;
70
	}
71
	return NULL;
72 73
}

74
PyTypeObject PySeqIter_Type = {
75 76 77
	PyObject_HEAD_INIT(&PyType_Type)
	0,					/* ob_size */
	"iterator",				/* tp_name */
78
	sizeof(seqiterobject),			/* tp_basicsize */
79 80 81 82
	0,					/* tp_itemsize */
	/* methods */
	(destructor)iter_dealloc, 		/* tp_dealloc */
	0,					/* tp_print */
83
	0,					/* tp_getattr */
84 85 86 87 88 89 90 91 92
	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 */
93
	PyObject_GenericGetAttr,		/* tp_getattro */
94 95
	0,					/* tp_setattro */
	0,					/* tp_as_buffer */
96
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
97
 	0,					/* tp_doc */
98
 	(traverseproc)iter_traverse,		/* tp_traverse */
99 100 101
 	0,					/* tp_clear */
	0,					/* tp_richcompare */
	0,					/* tp_weaklistoffset */
102
	PyObject_SelfIter,			/* tp_iter */
103
	(iternextfunc)iter_iternext,		/* tp_iternext */
104
	0,					/* tp_methods */
105 106 107 108 109 110
	0,					/* tp_members */
	0,					/* tp_getset */
	0,					/* tp_base */
	0,					/* tp_dict */
	0,					/* tp_descr_get */
	0,					/* tp_descr_set */
111 112 113 114 115 116
};

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

typedef struct {
	PyObject_HEAD
117 118
	PyObject *it_callable; /* Set to NULL when iterator is exhausted */
	PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */
119 120 121 122 123 124
} calliterobject;

PyObject *
PyCallIter_New(PyObject *callable, PyObject *sentinel)
{
	calliterobject *it;
125
	it = PyObject_GC_New(calliterobject, &PyCallIter_Type);
126 127 128 129 130 131
	if (it == NULL)
		return NULL;
	Py_INCREF(callable);
	it->it_callable = callable;
	Py_INCREF(sentinel);
	it->it_sentinel = sentinel;
132
	_PyObject_GC_TRACK(it);
133 134 135 136 137
	return (PyObject *)it;
}
static void
calliter_dealloc(calliterobject *it)
{
138
	_PyObject_GC_UNTRACK(it);
139 140
	Py_XDECREF(it->it_callable);
	Py_XDECREF(it->it_sentinel);
141
	PyObject_GC_Del(it);
142
}
143

144 145 146 147
static int
calliter_traverse(calliterobject *it, visitproc visit, void *arg)
{
	int err;
148
	if (it->it_callable != NULL && (err = visit(it->it_callable, arg)))
149
		return err;
150
	if (it->it_sentinel != NULL && (err = visit(it->it_sentinel, arg)))
151 152 153 154
		return err;
	return 0;
}

155 156 157
static PyObject *
calliter_iternext(calliterobject *it)
{
158
	if (it->it_callable != NULL) {
159 160 161 162 163 164
		PyObject *args = PyTuple_New(0);
		PyObject *result;
		if (args == NULL)
			return NULL;
		result = PyObject_Call(it->it_callable, args, NULL);
		Py_DECREF(args);
165 166 167 168 169 170 171
		if (result != NULL) {
			int ok;
			ok = PyObject_RichCompareBool(result,
						      it->it_sentinel,
						      Py_EQ);
			if (ok == 0)
				return result; /* Common case, fast path */
172
			Py_DECREF(result);
173 174 175 176 177 178 179 180 181 182 183 184 185
			if (ok > 0) {
				Py_DECREF(it->it_callable);
				it->it_callable = NULL;
				Py_DECREF(it->it_sentinel);
				it->it_sentinel = NULL;
			}
		}
		else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
			PyErr_Clear();
			Py_DECREF(it->it_callable);
			it->it_callable = NULL;
			Py_DECREF(it->it_sentinel);
			it->it_sentinel = NULL;
186 187
		}
	}
188
	return NULL;
189 190
}

191 192 193 194
PyTypeObject PyCallIter_Type = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,					/* ob_size */
	"callable-iterator",			/* tp_name */
195
	sizeof(calliterobject),			/* tp_basicsize */
196 197 198 199
	0,					/* tp_itemsize */
	/* methods */
	(destructor)calliter_dealloc, 		/* tp_dealloc */
	0,					/* tp_print */
200
	0,					/* tp_getattr */
201 202 203 204 205 206 207 208 209
	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 */
210
	PyObject_GenericGetAttr,		/* tp_getattro */
211 212
	0,					/* tp_setattro */
	0,					/* tp_as_buffer */
213
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
214
 	0,					/* tp_doc */
215
 	(traverseproc)calliter_traverse,	/* tp_traverse */
216 217 218
 	0,					/* tp_clear */
	0,					/* tp_richcompare */
	0,					/* tp_weaklistoffset */
219
	PyObject_SelfIter,			/* tp_iter */
220
	(iternextfunc)calliter_iternext,	/* tp_iternext */
221
	0,					/* tp_methods */
222 223 224 225 226 227
	0,					/* tp_members */
	0,					/* tp_getset */
	0,					/* tp_base */
	0,					/* tp_dict */
	0,					/* tp_descr_get */
	0,					/* tp_descr_set */
228
};