File size: 6,067 Bytes
065fee7 |
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
#ifndef _MULTIDICT_ITER_H
#define _MULTIDICT_ITER_H
#ifdef __cplusplus
extern "C" {
#endif
static PyTypeObject multidict_items_iter_type;
static PyTypeObject multidict_values_iter_type;
static PyTypeObject multidict_keys_iter_type;
typedef struct multidict_iter {
PyObject_HEAD
MultiDictObject *md; // MultiDict or CIMultiDict
Py_ssize_t current;
uint64_t version;
} MultidictIter;
static inline void
_init_iter(MultidictIter *it, MultiDictObject *md)
{
Py_INCREF(md);
it->md = md;
it->current = 0;
it->version = pair_list_version(&md->pairs);
}
static inline PyObject *
multidict_items_iter_new(MultiDictObject *md)
{
MultidictIter *it = PyObject_GC_New(
MultidictIter, &multidict_items_iter_type);
if (it == NULL) {
return NULL;
}
_init_iter(it, md);
PyObject_GC_Track(it);
return (PyObject *)it;
}
static inline PyObject *
multidict_keys_iter_new(MultiDictObject *md)
{
MultidictIter *it = PyObject_GC_New(
MultidictIter, &multidict_keys_iter_type);
if (it == NULL) {
return NULL;
}
_init_iter(it, md);
PyObject_GC_Track(it);
return (PyObject *)it;
}
static inline PyObject *
multidict_values_iter_new(MultiDictObject *md)
{
MultidictIter *it = PyObject_GC_New(
MultidictIter, &multidict_values_iter_type);
if (it == NULL) {
return NULL;
}
_init_iter(it, md);
PyObject_GC_Track(it);
return (PyObject *)it;
}
static inline PyObject *
multidict_items_iter_iternext(MultidictIter *self)
{
PyObject *key = NULL;
PyObject *value = NULL;
PyObject *ret = NULL;
if (self->version != pair_list_version(&self->md->pairs)) {
PyErr_SetString(PyExc_RuntimeError, "Dictionary changed during iteration");
return NULL;
}
if (!_pair_list_next(&self->md->pairs, &self->current, NULL, &key, &value, NULL)) {
PyErr_SetNone(PyExc_StopIteration);
return NULL;
}
ret = PyTuple_Pack(2, key, value);
if (ret == NULL) {
return NULL;
}
return ret;
}
static inline PyObject *
multidict_values_iter_iternext(MultidictIter *self)
{
PyObject *value = NULL;
if (self->version != pair_list_version(&self->md->pairs)) {
PyErr_SetString(PyExc_RuntimeError, "Dictionary changed during iteration");
return NULL;
}
if (!pair_list_next(&self->md->pairs, &self->current, NULL, NULL, &value)) {
PyErr_SetNone(PyExc_StopIteration);
return NULL;
}
Py_INCREF(value);
return value;
}
static inline PyObject *
multidict_keys_iter_iternext(MultidictIter *self)
{
PyObject *key = NULL;
if (self->version != pair_list_version(&self->md->pairs)) {
PyErr_SetString(PyExc_RuntimeError, "Dictionary changed during iteration");
return NULL;
}
if (!pair_list_next(&self->md->pairs, &self->current, NULL, &key, NULL)) {
PyErr_SetNone(PyExc_StopIteration);
return NULL;
}
Py_INCREF(key);
return key;
}
static inline void
multidict_iter_dealloc(MultidictIter *self)
{
PyObject_GC_UnTrack(self);
Py_XDECREF(self->md);
PyObject_GC_Del(self);
}
static inline int
multidict_iter_traverse(MultidictIter *self, visitproc visit, void *arg)
{
Py_VISIT(self->md);
return 0;
}
static inline int
multidict_iter_clear(MultidictIter *self)
{
Py_CLEAR(self->md);
return 0;
}
static inline PyObject *
multidict_iter_len(MultidictIter *self)
{
return PyLong_FromLong(pair_list_len(&self->md->pairs));
}
PyDoc_STRVAR(length_hint_doc,
"Private method returning an estimate of len(list(it)).");
static PyMethodDef multidict_iter_methods[] = {
{
"__length_hint__",
(PyCFunction)(void(*)(void))multidict_iter_len,
METH_NOARGS,
length_hint_doc
},
{
NULL,
NULL
} /* sentinel */
};
/***********************************************************************/
static PyTypeObject multidict_items_iter_type = {
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
"multidict._multidict._itemsiter", /* tp_name */
sizeof(MultidictIter), /* tp_basicsize */
.tp_dealloc = (destructor)multidict_iter_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_traverse = (traverseproc)multidict_iter_traverse,
.tp_clear = (inquiry)multidict_iter_clear,
.tp_iter = PyObject_SelfIter,
.tp_iternext = (iternextfunc)multidict_items_iter_iternext,
.tp_methods = multidict_iter_methods,
};
static PyTypeObject multidict_values_iter_type = {
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
"multidict._multidict._valuesiter", /* tp_name */
sizeof(MultidictIter), /* tp_basicsize */
.tp_dealloc = (destructor)multidict_iter_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_traverse = (traverseproc)multidict_iter_traverse,
.tp_clear = (inquiry)multidict_iter_clear,
.tp_iter = PyObject_SelfIter,
.tp_iternext = (iternextfunc)multidict_values_iter_iternext,
.tp_methods = multidict_iter_methods,
};
static PyTypeObject multidict_keys_iter_type = {
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
"multidict._multidict._keysiter", /* tp_name */
sizeof(MultidictIter), /* tp_basicsize */
.tp_dealloc = (destructor)multidict_iter_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_traverse = (traverseproc)multidict_iter_traverse,
.tp_clear = (inquiry)multidict_iter_clear,
.tp_iter = PyObject_SelfIter,
.tp_iternext = (iternextfunc)multidict_keys_iter_iternext,
.tp_methods = multidict_iter_methods,
};
static inline int
multidict_iter_init(void)
{
if (PyType_Ready(&multidict_items_iter_type) < 0 ||
PyType_Ready(&multidict_values_iter_type) < 0 ||
PyType_Ready(&multidict_keys_iter_type) < 0) {
return -1;
}
return 0;
}
#ifdef __cplusplus
}
#endif
#endif
|