summaryrefslogtreecommitdiff
path: root/src/iter.c
blob: 5368f247b40016d1b87c07f404c233f3b291c6d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "module.h"
#include "except.h"
#include "iter.h"

/*
 * Helpers
 */

static void
xpybIter_err(xpybIter *self)
{
    if (self->is_list)
	PyErr_Format(xpybExcept_base,
		     "Extra items in '%s' list (expect multiple of %d).",
		     PyString_AS_STRING(self->name), self->groupsize);
    else
	PyErr_Format(xpybExcept_base,
		     "Too few items in '%s' list (expect %d).",
		     PyString_AS_STRING(self->name), self->groupsize);
}

static PyObject *
xpybIter_pop(xpybIter *self)
{
    PyObject *cur, *next, *item;

    cur = PyList_GET_ITEM(self->stack, self->top);
    item = PyIter_Next(cur);

    if (item == NULL) {
	if (PyErr_Occurred() || self->top < 1)
	    return NULL;
	if (PyList_SetSlice(self->stack, self->top, self->top + 1, NULL) < 0)
	    return NULL;
	self->top--;
	return xpybIter_pop(self);
    }

    if (PySequence_Check(item)) {
	next = PyObject_GetIter(item);
	if (next == NULL)
	    goto err1;
	if (PyList_Append(self->stack, next) < 0)
	    goto err2;

	self->top++;
	Py_DECREF(next);
	Py_DECREF(item);
	return xpybIter_pop(self);
    }

    return item;
err2:
    Py_DECREF(next);
err1:
    Py_DECREF(item);
    return NULL;
}


/*
 * Infrastructure
 */

static PyObject *
xpybIter_new(PyTypeObject *self, PyObject *args, PyObject *kw)
{
    return PyType_GenericNew(self, args, kw);
}

static int
xpybIter_init(xpybIter *self, PyObject *args, PyObject *kw)
{
    PyObject *name, *list, *bool;
    Py_ssize_t groupsize;

    if (!PyArg_ParseTuple(args, "OnSO", &list, &groupsize, &name, &bool))
	return -1;
    
    Py_INCREF(self->name = name);
    Py_INCREF(self->list = list);
    self->groupsize = groupsize;
    self->is_list = PyObject_IsTrue(bool);
    return 0;
}

static PyObject *
xpybIter_get(xpybIter *self)
{
    PyObject *iterator;

    Py_CLEAR(self->stack);

    self->stack = PyList_New(1);
    if (self->stack == NULL)
	return NULL;

    iterator = PyObject_GetIter(self->list);
    if (iterator == NULL)
	return NULL;

    PyList_SET_ITEM(self->stack, 0, iterator);
    self->top = 0;

    Py_INCREF(self);
    return (PyObject *)self;
}

static PyObject *
xpybIter_next(xpybIter *self)
{
    PyObject *tuple, *tmp;
    Py_ssize_t i;

    tuple = PyTuple_New(self->groupsize);
    if (tuple == NULL)
	return NULL;

    for (i = 0; i < self->groupsize; i++) {
	tmp = xpybIter_pop(self);
	if (tmp == NULL) {
	    if (i > 0 && !PyErr_Occurred())
		xpybIter_err(self);
	    goto end;
	}
	PyTuple_SET_ITEM(tuple, i, tmp);
    }

    return tuple;
end:
    Py_DECREF(tuple);
    return NULL;
}

static void
xpybIter_dealloc(xpybIter *self)
{
    Py_CLEAR(self->stack);
    Py_CLEAR(self->list);
    Py_CLEAR(self->name);

    xpybIter_type.tp_base->tp_dealloc((PyObject *)self);
}


/*
 * Members
 */


/*
 * Definition
 */

PyTypeObject xpybIter_type = {
    PyObject_HEAD_INIT(NULL)
    .tp_name = "xcb.Iterator",
    .tp_basicsize = sizeof(xpybIter),
    .tp_init = (initproc)xpybIter_init,
    .tp_new = xpybIter_new,
    .tp_dealloc = (destructor)xpybIter_dealloc,
    .tp_flags = Py_TPFLAGS_DEFAULT,
    .tp_doc = "XCB flattening-iterator object",
    .tp_iter = (getiterfunc)xpybIter_get,
    .tp_iternext = (iternextfunc)xpybIter_next
};


/*
 * Module init
 */
int xpybIter_modinit(PyObject *m)
{
    if (PyType_Ready(&xpybIter_type) < 0)
        return -1;
    Py_INCREF(&xpybIter_type);
    if (PyModule_AddObject(m, "Iterator", (PyObject *)&xpybIter_type) < 0)
	return -1;

    return 0;
}