source: trunk/essentials/dev-lang/python/Objects/iterobject.c@ 3314

Last change on this file since 3314 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 5.2 KB
Line 
1/* Iterator objects */
2
3#include "Python.h"
4
5typedef struct {
6 PyObject_HEAD
7 long it_index;
8 PyObject *it_seq; /* Set to NULL when iterator is exhausted */
9} seqiterobject;
10
11PyObject *
12PySeqIter_New(PyObject *seq)
13{
14 seqiterobject *it;
15
16 if (!PySequence_Check(seq)) {
17 PyErr_BadInternalCall();
18 return NULL;
19 }
20 it = PyObject_GC_New(seqiterobject, &PySeqIter_Type);
21 if (it == NULL)
22 return NULL;
23 it->it_index = 0;
24 Py_INCREF(seq);
25 it->it_seq = seq;
26 _PyObject_GC_TRACK(it);
27 return (PyObject *)it;
28}
29
30static void
31iter_dealloc(seqiterobject *it)
32{
33 _PyObject_GC_UNTRACK(it);
34 Py_XDECREF(it->it_seq);
35 PyObject_GC_Del(it);
36}
37
38static int
39iter_traverse(seqiterobject *it, visitproc visit, void *arg)
40{
41 Py_VISIT(it->it_seq);
42 return 0;
43}
44
45static PyObject *
46iter_iternext(PyObject *iterator)
47{
48 seqiterobject *it;
49 PyObject *seq;
50 PyObject *result;
51
52 assert(PySeqIter_Check(iterator));
53 it = (seqiterobject *)iterator;
54 seq = it->it_seq;
55 if (seq == NULL)
56 return NULL;
57
58 result = PySequence_GetItem(seq, it->it_index);
59 if (result != NULL) {
60 it->it_index++;
61 return result;
62 }
63 if (PyErr_ExceptionMatches(PyExc_IndexError) ||
64 PyErr_ExceptionMatches(PyExc_StopIteration))
65 {
66 PyErr_Clear();
67 Py_DECREF(seq);