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