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 const 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 const 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 const 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
)
201 PyObject
*l
= 0, *i
= 0, *k
, *rc
= 0;
204 if ((l
= PyList_New(0)) == 0 ||
205 (i
= PyObject_GetIter(me
)) == 0)
207 while ((k
= PyIter_Next(i
)) != 0)
208 { err
= PyList_Append(l
, k
); Py_DECREF(k
); if (err
) goto done
; }
209 if (PyErr_Occurred()) goto done
;
212 Py_XDECREF(l
); Py_XDECREF(i
);
216 PyObject
*gmapmeth_values(PyObject
*me
)
218 PyObject
*l
= 0, *i
= 0, *k
, *v
, *rc
= 0;
221 if ((l
= PyList_New(0)) == 0 ||
222 (i
= PyObject_GetIter(me
)) == 0)
224 while ((k
= PyIter_Next(i
)) != 0) {
225 if ((v
= PyObject_GetItem(me
, k
)) == 0 ||
228 Py_DECREF(k
); Py_XDECREF(v
);
231 if (PyErr_Occurred()) goto done
;
234 Py_XDECREF(l
); Py_XDECREF(i
);
238 PyObject
*gmapmeth_items(PyObject
*me
)
240 PyObject
*l
= 0, *i
= 0, *k
, *v
, *z
, *rc
= 0;
243 if ((l
= PyList_New(0)) == 0 ||
244 (i
= PyObject_GetIter(me
)) == 0)
246 while ((k
= PyIter_Next(i
)) != 0) {
248 if ((v
= PyObject_GetItem(me
, k
)) == 0 ||
249 (z
= Py_BuildValue("(OO)", k
, v
)) == 0 ||
252 Py_DECREF(k
); Py_XDECREF(v
); Py_XDECREF(z
);
255 if (PyErr_Occurred()) goto done
;
258 Py_XDECREF(l
); Py_XDECREF(i
);
262 PyObject
*gmapmeth_iterkeys(PyObject
*me
)
263 { return (PyObject_GetIter(me
)); }
265 PyObject
*gmapmeth_itervalues(PyObject
*me
)
270 if ((i
= PyObject_GetIter(me
)) == 0)
272 ii
= PyObject_NEW(iter_pyobj
, valiter_pytype
);
273 ii
->map
= me
; Py_INCREF(me
);
275 return ((PyObject
*)ii
);
278 PyObject
*gmapmeth_iteritems(PyObject
*me
)
283 if ((i
= PyObject_GetIter(me
)) == 0)
285 ii
= PyObject_NEW(iter_pyobj
, itemiter_pytype
);
286 ii
->map
= me
; Py_INCREF(me
);
288 return ((PyObject
*)ii
);
291 PyObject
*gmapmeth_clear(PyObject
*me
)
293 PyObject
*i
= 0, *k
= 0, *rc
= 0;
295 if ((i
= PyObject_GetIter(me
)) == 0)
297 while ((k
= PyIter_Next(i
)) != 0) {
298 PyObject_DelItem(me
, k
);
301 if (PyErr_Occurred()) goto end
;
302 rc
= me
; Py_INCREF(me
);
308 static const char *const def_kwlist
[] = { "key", "default", 0 };
309 #define DEF_KWLIST ((/*unconst*/ char **)def_kwlist)
311 PyObject
*gmapmeth_get(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
313 PyObject
*k
, *def
= Py_None
, *v
;
315 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|O:get", DEF_KWLIST
, &k
, &def
))
317 if ((v
= PyObject_GetItem(me
, k
)) != 0) return (v
);
322 PyObject
*gmapmeth_setdefault(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
324 PyObject
*k
, *def
= Py_None
, *v
;
326 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|O:setdefault", DEF_KWLIST
,
329 if ((v
= PyObject_GetItem(me
, k
)) != 0) return (v
);
331 if (PyObject_SetItem(me
, k
, def
)) return (0);
335 PyObject
*gmapmeth_pop(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
337 PyObject
*k
, *def
= 0, *v
;
339 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|O:pop", DEF_KWLIST
, &k
, &def
))
341 if ((v
= PyObject_GetItem(me
, k
)) != 0) {
342 PyObject_DelItem(me
, k
);
351 PyObject
*gmapmeth_update(PyObject
*me
, PyObject
*arg
)
353 PyObject
*map
, *i
= 0, *k
, *v
, *rc
= 0;
356 if (!PyArg_ParseTuple(arg
, "O:update", &map
) ||
357 (i
= PyObject_GetIter(map
)) == 0)
359 while ((k
= PyIter_Next(i
)) != 0) {
360 if ((v
= PyObject_GetItem(map
, k
)) == 0 ||
361 PyObject_SetItem(me
, k
, v
))
363 Py_DECREF(k
); Py_XDECREF(v
);
366 if (PyErr_Occurred()) goto end
;
367 rc
= me
; Py_INCREF(me
);
373 PyObject
*gmapmeth_popitem(PyObject
*me
)
375 PyObject
*i
= 0, *k
= 0, *v
= 0, *rc
= 0;
377 if ((i
= PyObject_GetIter(me
)) == 0)
379 if ((k
= PyIter_Next(i
)) == 0) {
380 if (!PyErr_Occurred()) VALERR("popitem(): mapping is empty");
383 if ((v
= PyObject_GetItem(me
, k
)) == 0 ||
384 PyObject_DelItem(me
, k
))
386 rc
= Py_BuildValue("(OO)", k
, v
);
388 Py_XDECREF(i
); Py_XDECREF(k
); Py_XDECREF(v
);
392 const PyMethodDef gmap_pymethods
[] = {
397 /*----- Submodule initialization ------------------------------------------*/
399 void pyke_gmap_pyinit(void)
401 INITTYPE(itemiter
, root
);
402 INITTYPE(valiter
, root
);
405 void pyke_gmap_pyinsert(PyObject
*mod
)
407 INSERT("ItemIter", itemiter_pytype
);
408 INSERT("ValueIter", valiter_pytype
);
411 /*----- That's all, folks -------------------------------------------------*/