3 * Generic mapping support
5 * (c) 2019 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Pyke: the Python Kit for Extensions.
12 * Pyke is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
17 * Pyke is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with Pyke. If not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Iteration ---------------------------------------------------------*/
33 static PyTypeObject
*itemiter_pytype
, *valiter_pytype
;
35 typedef struct iter_pyobj
{
40 #define ITER_MAP(o) (((iter_pyobj *)(o))->map)
41 #define ITER_I(o) (((iter_pyobj *)(o))->i)
43 static void iter_pydealloc(PyObject
*me
)
44 { Py_DECREF(ITER_MAP(me
)); Py_DECREF(ITER_I(me
)); FREEOBJ(me
); }
46 static PyObject
*itemiter_pynext(PyObject
*me
)
48 PyObject
*k
= 0, *v
= 0, *rc
= 0;
50 if ((k
= PyIter_Next(ITER_I(me
))) != 0 &&
51 (v
= PyObject_GetItem(ITER_MAP(me
), k
)) != 0)
52 rc
= Py_BuildValue("(OO)", k
, v
);
53 Py_XDECREF(k
); Py_XDECREF(v
);
57 static PyTypeObject itemiter_pytype_skel
= {
58 PyObject_HEAD_INIT(0) 0, /* Header */
59 "ItemIter", /* @tp_name@ */
60 sizeof(iter_pyobj
), /* @tp_basicsize@ */
61 0, /* @tp_itemsize@ */
63 iter_pydealloc
, /* @tp_dealloc@ */
69 0, /* @tp_as_number@ */
70 0, /* @tp_as_sequence@ */
71 0, /* @tp_as_mapping@ */
75 0, /* @tp_getattro@ */
76 0, /* @tp_setattro@ */
77 0, /* @tp_as_buffer@ */
78 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
82 "Iterates over the items of a mapping.",
84 0, /* @tp_traverse@ */
86 0, /* @tp_richcompare@ */
87 0, /* @tp_weaklistoffset@ */
88 PyObject_SelfIter
, /* @tp_iter@ */
89 itemiter_pynext
, /* @tp_iternext@ */
95 0, /* @tp_descr_get@ */
96 0, /* @tp_descr_set@ */
97 0, /* @tp_dictoffset@ */
99 PyType_GenericAlloc
, /* @tp_alloc@ */
100 abstract_pynew
, /* @tp_new@ */
105 static PyObject
*valiter_pynext(PyObject
*me
)
107 PyObject
*k
= 0, *rc
= 0;
109 if ((k
= PyIter_Next(ITER_I(me
))) != 0)
110 rc
= PyObject_GetItem(ITER_MAP(me
), k
);
115 static PyTypeObject valiter_pytype_skel
= {
116 PyObject_HEAD_INIT(0) 0, /* Header */
117 "ValueIter", /* @tp_name@ */
118 sizeof(iter_pyobj
), /* @tp_basicsize@ */
119 0, /* @tp_itemsize@ */
121 iter_pydealloc
, /* @tp_dealloc@ */
123 0, /* @tp_getattr@ */
124 0, /* @tp_setattr@ */
125 0, /* @tp_compare@ */
127 0, /* @tp_as_number@ */
128 0, /* @tp_as_sequence@ */
129 0, /* @tp_as_mapping@ */
133 0, /* @tp_getattro@ */
134 0, /* @tp_setattro@ */
135 0, /* @tp_as_buffer@ */
136 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
140 "Iterates over the values of a mapping.",
142 0, /* @tp_traverse@ */
144 0, /* @tp_richcompare@ */
145 0, /* @tp_weaklistoffset@ */
146 PyObject_SelfIter
, /* @tp_iter@ */
147 valiter_pynext
, /* @tp_iternext@ */
148 0, /* @tp_methods@ */
149 0, /* @tp_members@ */
153 0, /* @tp_descr_get@ */
154 0, /* @tp_descr_set@ */
155 0, /* @tp_dictoffset@ */
157 PyType_GenericAlloc
, /* @tp_alloc@ */
158 abstract_pynew
, /* @tp_new@ */
163 PySequenceMethods gmap_pysequence
= {
169 0, /* @sq_ass_item@ */
170 0, /* @sq_ass_slice@ */
171 PyMapping_HasKey
, /* @sq_contains@ */
172 0, /* @sq_inplace_concat@ */
173 0 /* @sq_inplace_repeat@ */
176 /*----- Other mapping protocol support ------------------------------------*/
178 Py_ssize_t
gmap_pysize(PyObject
*me
)
180 PyObject
*i
= 0, *x
= 0;
181 Py_ssize_t rc
= -1, n
= 0;
183 if ((i
= PyObject_GetIter(me
)) == 0) goto done
;
184 while ((x
= PyIter_Next(i
)) != 0) { n
++; Py_DECREF(x
); x
= 0; }
185 if (PyErr_Occurred()) goto done
;
188 Py_XDECREF(i
); Py_XDECREF(x
);
192 PyObject
*gmapmeth_has_key(PyObject
*me
, PyObject
*arg
)
195 if (!PyArg_ParseTuple(arg
, "O:has_key", &k
)) return (0);
196 return (getbool(PyMapping_HasKey(me
, k
)));
199 PyObject
*gmapmeth_keys(PyObject
*me
, PyObject
*arg
)
201 PyObject
*l
= 0, *i
= 0, *k
, *rc
= 0;
204 if (!PyArg_ParseTuple(arg
, ":keys") ||
205 (l
= PyList_New(0)) == 0 ||
206 (i
= PyObject_GetIter(me
)) == 0)
208 while ((k
= PyIter_Next(i
)) != 0)
209 { err
= PyList_Append(l
, k
); Py_DECREF(k
); if (err
) goto done
; }
210 if (PyErr_Occurred()) goto done
;
213 Py_XDECREF(l
); Py_XDECREF(i
);
217 PyObject
*gmapmeth_values(PyObject
*me
, PyObject
*arg
)
219 PyObject
*l
= 0, *i
= 0, *k
, *v
, *rc
= 0;
222 if (!PyArg_ParseTuple(arg
, ":values") ||
223 (l
= PyList_New(0)) == 0 ||
224 (i
= PyObject_GetIter(me
)) == 0)
226 while ((k
= PyIter_Next(i
)) != 0) {
227 if ((v
= PyObject_GetItem(me
, k
)) == 0 ||
230 Py_DECREF(k
); Py_XDECREF(v
);
233 if (PyErr_Occurred()) goto done
;
236 Py_XDECREF(l
); Py_XDECREF(i
);
240 PyObject
*gmapmeth_items(PyObject
*me
, PyObject
*arg
)
242 PyObject
*l
= 0, *i
= 0, *k
, *v
, *z
, *rc
= 0;
245 if (!PyArg_ParseTuple(arg
, ":items") ||
246 (l
= PyList_New(0)) == 0 ||
247 (i
= PyObject_GetIter(me
)) == 0)
249 while ((k
= PyIter_Next(i
)) != 0) {
251 if ((v
= PyObject_GetItem(me
, k
)) == 0 ||
252 (z
= Py_BuildValue("(OO)", k
, v
)) == 0 ||
255 Py_DECREF(k
); Py_XDECREF(v
); Py_XDECREF(z
);
258 if (PyErr_Occurred()) goto done
;
261 Py_XDECREF(l
); Py_XDECREF(i
);
265 PyObject
*gmapmeth_iterkeys(PyObject
*me
, PyObject
*arg
)
267 if (!PyArg_ParseTuple(arg
, ":iterkeys")) return (0);
268 return (PyObject_GetIter(me
));
271 PyObject
*gmapmeth_itervalues(PyObject
*me
, PyObject
*arg
)
276 if (!PyArg_ParseTuple(arg
, ":itervalues") ||
277 (i
= PyObject_GetIter(me
)) == 0)
279 ii
= PyObject_NEW(iter_pyobj
, valiter_pytype
);
280 ii
->map
= me
; Py_INCREF(me
);
282 return ((PyObject
*)ii
);
285 PyObject
*gmapmeth_iteritems(PyObject
*me
, PyObject
*arg
)
290 if (!PyArg_ParseTuple(arg
, ":iteritems") ||
291 (i
= PyObject_GetIter(me
)) == 0)
293 ii
= PyObject_NEW(iter_pyobj
, itemiter_pytype
);
294 ii
->map
= me
; Py_INCREF(me
);
296 return ((PyObject
*)ii
);
299 PyObject
*gmapmeth_clear(PyObject
*me
, PyObject
*arg
)
301 PyObject
*i
= 0, *k
= 0, *rc
= 0;
303 if (!PyArg_ParseTuple(arg
, ":clear") ||
304 (i
= PyObject_GetIter(me
)) == 0)
306 while ((k
= PyIter_Next(i
)) != 0) {
307 PyObject_DelItem(me
, k
);
310 if (PyErr_Occurred()) goto end
;
311 rc
= me
; Py_INCREF(me
);
317 static const char *const def_kwlist
[] = { "key", "default", 0 };
319 PyObject
*gmapmeth_get(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
321 PyObject
*k
, *def
= Py_None
, *v
;
323 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|O:get",
324 (/*unconst*/ char **)def_kwlist
,
327 if ((v
= PyObject_GetItem(me
, k
)) != 0) return (v
);
332 PyObject
*gmapmeth_setdefault(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
334 PyObject
*k
, *def
= Py_None
, *v
;
336 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|O:setdefault",
337 (/*unconst*/ char **)def_kwlist
,
340 if ((v
= PyObject_GetItem(me
, k
)) != 0) return (v
);
342 if (PyObject_SetItem(me
, k
, def
)) return (0);
346 PyObject
*gmapmeth_pop(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
348 PyObject
*k
, *def
= 0, *v
;
350 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|O:pop",
351 (/*unconst*/ char **)def_kwlist
,
354 if ((v
= PyObject_GetItem(me
, k
)) != 0) {
355 PyObject_DelItem(me
, k
);
364 PyObject
*gmapmeth_update(PyObject
*me
, PyObject
*arg
)
366 PyObject
*map
, *i
= 0, *k
, *v
, *rc
= 0;
369 if (!PyArg_ParseTuple(arg
, "O:update", &map
) ||
370 (i
= PyObject_GetIter(map
)) == 0)
372 while ((k
= PyIter_Next(i
)) != 0) {
373 if ((v
= PyObject_GetItem(map
, k
)) == 0 ||
374 PyObject_SetItem(me
, k
, v
))
376 Py_DECREF(k
); Py_XDECREF(v
);
379 if (PyErr_Occurred()) goto end
;
380 rc
= me
; Py_INCREF(me
);
386 PyObject
*gmapmeth_popitem(PyObject
*me
, PyObject
*arg
)
388 PyObject
*i
= 0, *k
= 0, *v
= 0, *rc
= 0;
390 if (!PyArg_ParseTuple(arg
, ":popitem") ||
391 (i
= PyObject_GetIter(me
)) == 0)
393 if ((k
= PyIter_Next(i
)) == 0) {
394 if (!PyErr_Occurred()) VALERR("popitem(): mapping is empty");
397 if ((v
= PyObject_GetItem(me
, k
)) == 0 ||
398 PyObject_DelItem(me
, k
))
400 rc
= Py_BuildValue("(OO)", k
, v
);
402 Py_XDECREF(i
); Py_XDECREF(k
); Py_XDECREF(v
);
406 PyMethodDef gmap_pymethods
[] = {
411 /*----- Submodule initialization ------------------------------------------*/
413 void pyke_gmap_pyinit(void)
415 INITTYPE(itemiter
, root
);
416 INITTYPE(valiter
, root
);
419 void pyke_gmap_pyinsert(PyObject
*mod
)
421 INSERT("ItemIter", itemiter_pytype
);
422 INSERT("ValueIter", valiter_pytype
);
425 /*----- That's all, folks -------------------------------------------------*/