chiark / gitweb /
util.c: Augment `convu64' to collect its argument via `kludge64'.
[pyke] / util.c
1 /* -*-c-*-
2  *
3  * Miscellaneous utilities (not Catacomb-specific)
4  *
5  * (c) 2005 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the Python interface to Catacomb.
11  *
12  * Catacomb/Python is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * Catacomb/Python is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Catacomb/Python; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "catacomb-python.h"
30
31 /*----- External values ---------------------------------------------------*/
32
33 static PyObject *modname = 0;
34
35 /*----- Conversions -------------------------------------------------------*/
36
37 PyObject *getulong(unsigned long w)
38 {
39   if (w <= LONG_MAX)
40     return (PyInt_FromLong(w));
41   else
42     return (PyLong_FromUnsignedLong(w));
43 }
44
45 static PyObject *i32 = 0;
46 static int init_i32(void)
47   { if (!i32 && (i32 = PyInt_FromLong(32)) == 0) return (-1); return (0); }
48
49 PyObject *getk64(kludge64 u)
50 {
51   PyObject *i = 0, *j = 0, *t;
52   PyObject *rc = 0;
53
54   if (init_i32()) goto end;
55   if ((i = PyLong_FromUnsignedLong(HI64(u))) == 0) goto end;
56   if ((t = PyNumber_InPlaceLshift(i, i32)) == 0) goto end;
57   Py_DECREF(i); i = t;
58   if ((j = PyLong_FromUnsignedLong(LO64(u))) == 0) goto end;
59   if ((t = PyNumber_InPlaceOr(i, j)) == 0) goto end;
60   Py_DECREF(i); i = t;
61   if ((rc = PyNumber_Int(i)) == 0) goto end;
62 end:
63   if (i) Py_DECREF(i);
64   if (j) Py_DECREF(j);
65   return (rc);
66 }
67
68 PyObject *getbool(int b)
69 {
70   if (b) RETURN_TRUE;
71   else RETURN_FALSE;
72 }
73
74 PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
75 {
76   PyErr_SetString(PyExc_TypeError, "can't instantiate this class");
77   return (0);
78 }
79
80 int convulong(PyObject *o, void *pp)
81 {
82   long i;
83   unsigned long *p = pp;
84   PyObject *t;
85
86   if (!o) VALERR("can't delete");
87   if (PyInt_Check(o)) {
88     i = PyInt_AS_LONG(o);
89     if (i < 0) VALERR("must be nonnegative");
90     *p = i;
91   } else {
92     if ((t = PyNumber_Long(o)) == 0) goto end;
93     *p = PyLong_AsUnsignedLong(t);
94     Py_DECREF(t);
95     if (PyErr_Occurred()) goto end;
96   }
97   return (1);
98 end:
99   return (0);
100 }
101
102 #ifdef HAVE_UINT64
103 #  define CONVu64(n) do {                                               \
104      kludge64 k;                                                        \
105      uint64 t;                                                          \
106      if (!convk64(o, &k)) goto end;                                     \
107      t = GET64(uint64, k);                                              \
108      if (t > MASK##n) VALERR("out of range");                           \
109      *p = t;                                                            \
110    } while (0)
111 #else
112 #  define CONVu64(n) assert(!"shouldn't be possible")
113 #endif
114
115 #define CONVU_(n)                                                       \
116   int convu##n(PyObject *o, void *pp)                                   \
117   {                                                                     \
118     unsigned long u;                                                    \
119     uint##n *p = pp;                                                    \
120                                                                         \
121     if (MASK##n > ULONG_MAX)                                            \
122       CONVu64(n);                                                       \
123     else {                                                              \
124       if (!convulong(o, &u)) goto end;                                  \
125       if (u > MASK##n) VALERR("out of range");                          \
126       *p = u;                                                           \
127     }                                                                   \
128     return (1);                                                         \
129   end:                                                                  \
130     return (0);                                                         \
131   }
132 DOUINTSZ(CONVU_)
133
134 int convuint(PyObject *o, void *pp)
135 {
136   unsigned long u;
137   unsigned *p = pp;
138
139   if (!convulong(o, &u)) goto end;
140   if (u > UINT_MAX) VALERR("out of range");
141   *p = u;
142   return (1);
143 end:
144   return (0);
145 }
146
147 int convk64(PyObject *o, void *pp)
148 {
149   PyObject *i = 0, *t;
150   int rc = 0;
151   uint32 lo, hi;
152
153   if (!o) VALERR("can't delete");
154   if (init_i32()) goto end;
155   if ((i = PyNumber_Int(o)) == 0) goto end;
156   lo = PyInt_AsUnsignedLongMask(i);
157   if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end;
158   Py_DECREF(i); i = t;
159   hi = PyInt_AsUnsignedLongMask(i);
160   if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end;
161   Py_DECREF(i); i = t;
162   if (PyObject_IsTrue(i)) VALERR("out of range");
163   SET64(*(kludge64 *)pp, hi, lo);
164   rc = 1;
165 end:
166   if (i) Py_DECREF(i);
167   return (rc);
168 }
169
170 int convmpw(PyObject *o, void *pp)
171 {
172   unsigned long u;
173   unsigned *p = pp;
174
175   if (!convulong(o, &u)) goto end;
176   if (u > MPW_MAX) VALERR("out of range");
177   *p = u;
178   return (1);
179 end:
180   return (0);
181 }
182
183 int convszt(PyObject *o, void *pp)
184 {
185   unsigned long u;
186   size_t *p = pp;
187
188   if (!convulong(o, &u)) goto end;
189   if (u > ~(size_t)0) VALERR("out of range");
190   *p = u;
191   return (1);
192 end:
193   return (0);
194 }
195
196 int convbool(PyObject *o, void *pp)
197 {
198   if (!o) VALERR("can't delete");
199   *(int *)pp = PyObject_IsTrue(o);
200   return (1);
201 end:
202   return (0);
203 }
204
205 /*----- Type messing ------------------------------------------------------*/
206
207 static const PyTypeObject emptytype = { 0 };
208
209 void *newtype(PyTypeObject *metaty,
210               const PyTypeObject *skel,
211               const char *name)
212 {
213   PyHeapTypeObject *ty =
214     (PyHeapTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0));
215   if (!skel) skel = &emptytype;
216   memcpy(ty, skel, sizeof(*skel));
217   if (ty->ht_type.tp_base) Py_INCREF(ty->ht_type.tp_base);
218 #define COPY(blah) do {                                                 \
219     if (ty->ht_type.tp_as_##blah) {                                     \
220       memcpy(&ty->as_##blah,                                            \
221              ty->ht_type.tp_as_##blah,                                  \
222              sizeof(ty->as_##blah));                                    \
223       ty->ht_type.tp_as_##blah = &ty->as_##blah;                        \
224     }                                                                   \
225   } while (0)
226   COPY(number);
227   COPY(sequence);
228   COPY(mapping);
229   COPY(buffer);
230 #undef COPY
231   if (name)
232     ty->ht_name = PyString_FromString(name);
233   else if (ty->ht_type.tp_name)
234     ty->ht_name = PyString_FromString(ty->ht_type.tp_name);
235   if (ty->ht_name)
236     ty->ht_type.tp_name = PyString_AS_STRING(ty->ht_name);
237   DISCARD(PyObject_INIT(&ty->ht_type, metaty));
238   Py_INCREF(metaty);
239   return (ty);
240 }
241
242 void typeready(PyTypeObject *ty)
243 {
244   PyType_Ready(ty);
245   PyDict_SetItemString(ty->tp_dict, "__module__", modname);
246 }
247
248 PyTypeObject *inittype(PyTypeObject *tyskel, PyTypeObject *meta)
249 {
250   PyTypeObject *ty = newtype(meta, tyskel, 0);
251   ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
252   typeready(ty);
253   return (ty);
254 }
255
256 /*----- Constants ---------------------------------------------------------*/
257
258 void setconstants(PyObject *mod, const struct nameval *c)
259 {
260   PyObject *x;
261
262   while (c->name) {
263     if (c->value > LONG_MAX)
264       x = PyLong_FromUnsignedLong(c->value);
265     else
266       x = PyInt_FromLong(c->value);
267     PyModule_AddObject(mod, (/*unconst*/ char *)c->name, x);
268     c++;
269   }
270 }
271
272 /*----- Building method tables --------------------------------------------*/
273
274 DA_DECL(method_v, PyMethodDef);
275 static method_v global_pymethods = DA_INIT;
276 void addmethods(const PyMethodDef *m)
277 {
278   size_t n;
279
280   for (n = 0; m[n].ml_name; n++);
281   DA_ENSURE(&global_pymethods, n);
282   memcpy(DA(&global_pymethods) + DA_LEN(&global_pymethods),
283          m, n * sizeof(*m));
284   DA_EXTEND(&global_pymethods, n);
285 }
286
287 PyMethodDef *donemethods(void)
288 {
289   static const PyMethodDef mzero = { 0 };
290   DA_PUSH(&global_pymethods, mzero);
291   return (DA(&global_pymethods));
292 }
293
294 /*----- Exceptions --------------------------------------------------------*/
295
296 PyObject *mkexc(PyObject *mod, PyObject *base,
297                 const char *name, PyMethodDef *mm)
298 {
299   PyObject *nameobj = 0;
300   PyObject *dict = 0;
301   PyObject *exc = 0;
302   PyObject *func = 0;
303   PyObject *meth = 0;
304
305   if ((nameobj = PyString_FromFormat("%s.%s",
306                                      PyModule_GetName(mod),
307                                      name)) == 0 ||
308       (dict = PyDict_New()) == 0 ||
309       (exc = PyErr_NewException(PyString_AS_STRING(nameobj),
310                                 base, dict)) == 0)
311     goto fail;
312
313   if (mm) {
314     while (mm->ml_name) {
315       if ((func = PyCFunction_NewEx(mm, 0, mod)) == 0 ||
316           (meth = PyMethod_New(func, 0, exc)) == 0 ||
317           PyDict_SetItemString(dict, mm->ml_name, meth))
318         goto fail;
319       Py_DECREF(func); func = 0;
320       Py_DECREF(meth); meth = 0;
321       mm++;
322     }
323   }
324
325 done:
326   Py_XDECREF(nameobj);
327   Py_XDECREF(dict);
328   return (exc);
329
330 fail:
331   Py_XDECREF(exc);
332   Py_XDECREF(func);
333   Py_XDECREF(meth);
334   exc = 0;
335   goto done;
336 }
337
338 /*----- Generic dictionary methods ----------------------------------------*/
339
340 static PyTypeObject *itemiter_pytype, *valiter_pytype;
341
342 typedef struct iter_pyobj {
343   PyObject_HEAD
344   PyObject *map;
345   PyObject *i;
346 } iter_pyobj;
347 #define ITER_MAP(o) (((iter_pyobj *)(o))->map)
348 #define ITER_I(o) (((iter_pyobj *)(o))->i)
349
350 static void iter_pydealloc(PyObject *me)
351   { Py_DECREF(ITER_MAP(me)); Py_DECREF(ITER_I(me)); FREEOBJ(me); }
352
353 static PyObject *itemiter_pynext(PyObject *me)
354 {
355   PyObject *k = 0, *v = 0, *rc = 0;
356
357   if ((k = PyIter_Next(ITER_I(me))) != 0 &&
358       (v = PyObject_GetItem(ITER_MAP(me), k)) != 0)
359     rc = Py_BuildValue("(OO)", k, v);
360   Py_XDECREF(k); Py_XDECREF(v);
361   return (rc);
362 }
363
364 static PyTypeObject itemiter_pytype_skel = {
365   PyObject_HEAD_INIT(0) 0,              /* Header */
366   "ItemIter",                           /* @tp_name@ */
367   sizeof(iter_pyobj),                   /* @tp_basicsize@ */
368   0,                                    /* @tp_itemsize@ */
369
370   iter_pydealloc,                       /* @tp_dealloc@ */
371   0,                                    /* @tp_print@ */
372   0,                                    /* @tp_getattr@ */
373   0,                                    /* @tp_setattr@ */
374   0,                                    /* @tp_compare@ */
375   0,                                    /* @tp_repr@ */
376   0,                                    /* @tp_as_number@ */
377   0,                                    /* @tp_as_sequence@ */
378   0,                                    /* @tp_as_mapping@ */
379   0,                                    /* @tp_hash@ */
380   0,                                    /* @tp_call@ */
381   0,                                    /* @tp_str@ */
382   0,                                    /* @tp_getattro@ */
383   0,                                    /* @tp_setattro@ */
384   0,                                    /* @tp_as_buffer@ */
385   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
386     Py_TPFLAGS_BASETYPE,
387
388   /* @tp_doc@ */
389 "Iterates over the items of a mapping.",
390
391   0,                                    /* @tp_traverse@ */
392   0,                                    /* @tp_clear@ */
393   0,                                    /* @tp_richcompare@ */
394   0,                                    /* @tp_weaklistoffset@ */
395   PyObject_SelfIter,                    /* @tp_iter@ */
396   itemiter_pynext,                      /* @tp_iternext@ */
397   0,                                    /* @tp_methods@ */
398   0,                                    /* @tp_members@ */
399   0,                                    /* @tp_getset@ */
400   0,                                    /* @tp_base@ */
401   0,                                    /* @tp_dict@ */
402   0,                                    /* @tp_descr_get@ */
403   0,                                    /* @tp_descr_set@ */
404   0,                                    /* @tp_dictoffset@ */
405   0,                                    /* @tp_init@ */
406   PyType_GenericAlloc,                  /* @tp_alloc@ */
407   abstract_pynew,                       /* @tp_new@ */
408   0,                                    /* @tp_free@ */
409   0                                     /* @tp_is_gc@ */
410 };
411
412 static PyObject *valiter_pynext(PyObject *me)
413 {
414   PyObject *k = 0, *rc = 0;
415
416   if ((k = PyIter_Next(ITER_I(me))) != 0)
417     rc = PyObject_GetItem(ITER_MAP(me), k);
418   Py_XDECREF(k);
419   return (rc);
420 }
421
422 static PyTypeObject valiter_pytype_skel = {
423   PyObject_HEAD_INIT(0) 0,              /* Header */
424   "ValueIter",                          /* @tp_name@ */
425   sizeof(iter_pyobj),                   /* @tp_basicsize@ */
426   0,                                    /* @tp_itemsize@ */
427
428   iter_pydealloc,                       /* @tp_dealloc@ */
429   0,                                    /* @tp_print@ */
430   0,                                    /* @tp_getattr@ */
431   0,                                    /* @tp_setattr@ */
432   0,                                    /* @tp_compare@ */
433   0,                                    /* @tp_repr@ */
434   0,                                    /* @tp_as_number@ */
435   0,                                    /* @tp_as_sequence@ */
436   0,                                    /* @tp_as_mapping@ */
437   0,                                    /* @tp_hash@ */
438   0,                                    /* @tp_call@ */
439   0,                                    /* @tp_str@ */
440   0,                                    /* @tp_getattro@ */
441   0,                                    /* @tp_setattro@ */
442   0,                                    /* @tp_as_buffer@ */
443   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
444     Py_TPFLAGS_BASETYPE,
445
446   /* @tp_doc@ */
447 "Iterates over the items of a mapping.",
448
449   0,                                    /* @tp_traverse@ */
450   0,                                    /* @tp_clear@ */
451   0,                                    /* @tp_richcompare@ */
452   0,                                    /* @tp_weaklistoffset@ */
453   PyObject_SelfIter,                    /* @tp_iter@ */
454   valiter_pynext,                       /* @tp_iternext@ */
455   0,                                    /* @tp_methods@ */
456   0,                                    /* @tp_members@ */
457   0,                                    /* @tp_getset@ */
458   0,                                    /* @tp_base@ */
459   0,                                    /* @tp_dict@ */
460   0,                                    /* @tp_descr_get@ */
461   0,                                    /* @tp_descr_set@ */
462   0,                                    /* @tp_dictoffset@ */
463   0,                                    /* @tp_init@ */
464   PyType_GenericAlloc,                  /* @tp_alloc@ */
465   abstract_pynew,                       /* @tp_new@ */
466   0,                                    /* @tp_free@ */
467   0                                     /* @tp_is_gc@ */
468 };
469
470 PySequenceMethods gmap_pysequence = {
471   0,                                    /* @sq_length@ */
472   0,                                    /* @sq_concat@ */
473   0,                                    /* @sq_repeat@ */
474   0,                                    /* @sq_item@ */
475   0,                                    /* @sq_slice@ */
476   0,                                    /* @sq_ass_item@ */
477   0,                                    /* @sq_ass_slice@ */
478   PyMapping_HasKey,                     /* @sq_contains@ */
479   0,                                    /* @sq_inplace_concat@ */
480   0                                     /* @sq_inplace_repeat@ */
481 };
482
483 Py_ssize_t gmap_pysize(PyObject *me)
484 {
485   PyObject *i = 0, *x = 0;
486   int rc = -1;
487   int n = 0;
488
489   if ((i = PyObject_GetIter(me)) == 0) goto done;
490   while ((x = PyIter_Next(i)) != 0) { n++; Py_DECREF(x); x = 0; }
491   if (PyErr_Occurred()) goto done;
492   rc = n;
493 done:
494   Py_XDECREF(i); Py_XDECREF(x);
495   return (rc);
496 }
497
498 PyObject *gmapmeth_has_key(PyObject *me, PyObject *arg)
499 {
500   PyObject *k;
501   if (!PyArg_ParseTuple(arg, "O:has_key", &k)) return (0);
502   return (getbool(PyMapping_HasKey(me, k)));
503 }
504
505 PyObject *gmapmeth_keys(PyObject *me, PyObject *arg)
506 {
507   PyObject *l = 0, *i = 0, *k, *rc = 0;
508   int err;
509
510   if (!PyArg_ParseTuple(arg, ":keys") ||
511       (l = PyList_New(0)) == 0 ||
512       (i = PyObject_GetIter(me)) == 0)
513     goto done;
514   while ((k = PyIter_Next(i)) != 0)
515     { err = PyList_Append(l, k); Py_DECREF(k); if (err) goto done; }
516   if (PyErr_Occurred()) goto done;
517   rc = l; l = 0;
518 done:
519   Py_XDECREF(l); Py_XDECREF(i);
520   return (rc);
521 }
522
523 PyObject *gmapmeth_values(PyObject *me, PyObject *arg)
524 {
525   PyObject *l = 0, *i = 0, *k, *v, *rc = 0;
526   int err = 0;
527
528   if (!PyArg_ParseTuple(arg, ":values") ||
529       (l = PyList_New(0)) == 0 ||
530       (i = PyObject_GetIter(me)) == 0)
531     goto done;
532   while ((k = PyIter_Next(i)) != 0) {
533     if ((v = PyObject_GetItem(me, k)) == 0 ||
534         PyList_Append(l, v))
535       err = -1;
536     Py_DECREF(k); Py_XDECREF(v);
537     if (err) goto done;
538   }
539   if (PyErr_Occurred()) goto done;
540   rc = l; l = 0;
541 done:
542   Py_XDECREF(l); Py_XDECREF(i);
543   return (rc);
544 }
545
546 PyObject *gmapmeth_items(PyObject *me, PyObject *arg)
547 {
548   PyObject *l = 0, *i = 0, *k, *v, *z, *rc = 0;
549   int err = 0;
550
551   if (!PyArg_ParseTuple(arg, ":items") ||
552       (l = PyList_New(0)) == 0 ||
553       (i = PyObject_GetIter(me)) == 0)
554     goto done;
555   while ((k = PyIter_Next(i)) != 0) {
556     z = 0;
557     if ((v = PyObject_GetItem(me, k)) == 0 ||
558         (z = Py_BuildValue("(OO)", k, v)) == 0 ||
559         PyList_Append(l, z))
560       err = -1;
561     Py_DECREF(k); Py_XDECREF(v); Py_XDECREF(z);
562     if (err) goto done;
563   }
564   if (PyErr_Occurred()) goto done;
565   rc = l; l = 0;
566 done:
567   Py_XDECREF(l); Py_XDECREF(i);
568   return (rc);
569 }
570
571 PyObject *gmapmeth_iterkeys(PyObject *me, PyObject *arg)
572 {
573   if (!PyArg_ParseTuple(arg, ":iterkeys")) return (0);
574   return (PyObject_GetIter(me));
575 }
576
577 PyObject *gmapmeth_itervalues(PyObject *me, PyObject *arg)
578 {
579   PyObject *i;
580   iter_pyobj *ii;
581
582   if (!PyArg_ParseTuple(arg, ":itervalues") ||
583       (i = PyObject_GetIter(me)) == 0)
584     return (0);
585   ii = PyObject_NEW(iter_pyobj, valiter_pytype);
586   ii->map = me; Py_INCREF(me);
587   ii->i = i;
588   return ((PyObject *)ii);
589 }
590
591 PyObject *gmapmeth_iteritems(PyObject *me, PyObject *arg)
592 {
593   PyObject *i;
594   iter_pyobj *ii;
595
596   if (!PyArg_ParseTuple(arg, ":iteritems") ||
597       (i = PyObject_GetIter(me)) == 0)
598     return (0);
599   ii = PyObject_NEW(iter_pyobj, itemiter_pytype);
600   ii->map = me; Py_INCREF(me);
601   ii->i = i;
602   return ((PyObject *)ii);
603 }
604
605 PyObject *gmapmeth_clear(PyObject *me, PyObject *arg)
606 {
607   PyObject *i = 0, *k = 0, *rc = 0;
608
609   if (!PyArg_ParseTuple(arg, ":clear") ||
610       (i = PyObject_GetIter(me)) == 0)
611     goto end;
612   while ((k = PyIter_Next(i)) != 0) {
613     PyObject_DelItem(me, k);
614     Py_DECREF(k);
615   }
616   if (PyErr_Occurred()) goto end;
617   rc = me; Py_INCREF(me);
618 end:
619   Py_XDECREF(i);
620   return (rc);
621 }
622
623 static char *def_kwlist[] = { "key", "default", 0 };
624
625 PyObject *gmapmeth_get(PyObject *me, PyObject *arg, PyObject *kw)
626 {
627   PyObject *k, *def = Py_None, *v;
628
629   if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:get", def_kwlist, &k, &def))
630     return (0);
631   if ((v = PyObject_GetItem(me, k)) != 0) return (v);
632   PyErr_Clear();
633   RETURN_OBJ(def);
634 }
635
636 PyObject *gmapmeth_setdefault(PyObject *me, PyObject *arg, PyObject *kw)
637 {
638   PyObject *k, *def = Py_None, *v;
639
640   if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:setdefault",
641                                    def_kwlist, &k, &def))
642     return (0);
643   if ((v = PyObject_GetItem(me, k)) != 0) return (v);
644   PyErr_Clear();
645   if (PyObject_SetItem(me, k, def)) return (0);
646   RETURN_OBJ(def);
647 }
648
649 PyObject *gmapmeth_pop(PyObject *me, PyObject *arg, PyObject *kw)
650 {
651   PyObject *k, *def = 0, *v;
652
653   if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:pop", def_kwlist, &k, &def))
654     return (0);
655   if ((v = PyObject_GetItem(me, k)) != 0) {
656     PyObject_DelItem(me, k);
657     return (v);
658   }
659   PyErr_Clear();
660   RETURN_OBJ(def);
661 }
662
663 PyObject *gmapmeth_update(PyObject *me, PyObject *arg)
664 {
665   PyObject *map, *i = 0, *k, *v, *rc = 0;
666   int err = 0;
667
668   if (!PyArg_ParseTuple(arg, "O:update", &map) ||
669       (i = PyObject_GetIter(map)) == 0)
670     goto end;
671   while ((k = PyIter_Next(i)) != 0) {
672     if ((v = PyObject_GetItem(map, k)) == 0 ||
673         PyObject_SetItem(me, k, v))
674       err = -1;
675     Py_DECREF(k); Py_XDECREF(v);
676     if (err) goto end;
677   }
678   if (PyErr_Occurred()) goto end;
679   rc = me; Py_INCREF(me);
680 end:
681   Py_XDECREF(i);
682   return (rc);
683 }
684
685 PyObject *gmapmeth_popitem(PyObject *me, PyObject *arg)
686 {
687   PyObject *i = 0, *k = 0, *v = 0, *rc = 0;
688
689   if (!PyArg_ParseTuple(arg, ":popitem") ||
690       (i = PyObject_GetIter(me)))
691     goto end;
692   if ((k = PyIter_Next(i)) == 0) {
693     if (!PyErr_Occurred()) VALERR("popitem(): mapping is empty");
694     goto end;
695   }
696   if ((v = PyObject_GetItem(me, k)) == 0 ||
697       PyObject_DelItem(me, k))
698     goto end;
699   rc = Py_BuildValue("(OO)", k, v);
700 end:
701   Py_XDECREF(i); Py_XDECREF(k); Py_XDECREF(v);
702   return (rc);
703 }
704
705 PyMethodDef gmap_pymethods[] = {
706   GMAP_METHODS
707   { 0 }
708 };
709
710 /*----- Initialization ----------------------------------------------------*/
711
712 void util_pyinit(void)
713 {
714   modname = PyString_FromString("catacomb");
715   INITTYPE(itemiter, root);
716   INITTYPE(valiter, root);
717 }
718
719 void util_pyinsert(PyObject *mod)
720 {
721   INSERT("ItemIter", itemiter_pytype);
722   INSERT("ValueIter", valiter_pytype);
723 }
724
725 /*----- That's all, folks -------------------------------------------------*/