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