3 * Miscellaneous utilities (not Catacomb-specific)
5 * (c) 2005 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /* #undef HAVE_LONG_LONG */
33 /*----- External values ---------------------------------------------------*/
35 PyObject *modname = 0;
36 PyObject *home_module = 0;
38 /*----- Conversions -------------------------------------------------------*/
40 PyObject *getulong(unsigned long w)
43 return (PyInt_FromLong(w));
45 return (PyLong_FromUnsignedLong(w));
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); }
54 PyObject *getk64(kludge64 u)
57 return (PyLong_FromUnsignedLongLong(GET64(unsigned PY_LONG_LONG, u)));
59 PyObject *i = 0, *j = 0, *t;
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;
66 if ((j = PyLong_FromUnsignedLong(LO64(u))) == 0) goto end;
67 if ((t = PyNumber_InPlaceOr(i, j)) == 0) goto end;
69 if ((rc = PyNumber_Int(i)) == 0) goto end;
77 PyObject *getbool(int b)
83 PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
85 PyErr_SetString(PyExc_TypeError, "can't instantiate this class");
89 int convulong(PyObject *o, void *pp)
92 unsigned long *p = pp;
95 if (!o) VALERR("can't delete");
98 if (i < 0) VALERR("must be nonnegative");
101 if ((t = PyNumber_Long(o)) == 0) goto end;
102 *p = PyLong_AsUnsignedLong(t);
104 if (PyErr_Occurred()) goto end;
112 # define CONVu64(n) do { \
115 if (!convk64(o, &k)) goto end; \
116 t = GET64(uint64, k); \
117 if (t > MASK##n) VALERR("out of range"); \
121 # define CONVu64(n) assert(!"shouldn't be possible")
125 int convu##n(PyObject *o, void *pp) \
130 if (MASK##n > ULONG_MAX) \
133 if (!convulong(o, &u)) goto end; \
134 if (u > MASK##n) VALERR("out of range"); \
143 int convuint(PyObject *o, void *pp)
148 if (!convulong(o, &u)) goto end;
149 if (u > UINT_MAX) VALERR("out of range");
156 int convk64(PyObject *o, void *pp)
161 unsigned PY_LONG_LONG t;
167 if (!o) VALERR("can't delete");
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);
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;
179 hi = PyInt_AsUnsignedLongMask(i);
180 if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end;
182 if (PyObject_IsTrue(i)) VALERR("out of range");
183 SET64(*(kludge64 *)pp, hi, lo);
192 int convmpw(PyObject *o, void *pp)
197 if (!convulong(o, &u)) goto end;
198 if (u > MPW_MAX) VALERR("out of range");
205 int convszt(PyObject *o, void *pp)
210 if (!convulong(o, &u)) goto end;
211 if (u > ~(size_t)0) VALERR("out of range");
218 int convbool(PyObject *o, void *pp)
220 if (!o) VALERR("can't delete");
221 *(int *)pp = PyObject_IsTrue(o);
227 /*----- Type messing ------------------------------------------------------*/
229 static const PyTypeObject emptytype = { 0 };
231 void *newtype(PyTypeObject *metaty,
232 const PyTypeObject *skel,
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; \
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);
258 ty->ht_type.tp_name = PyString_AS_STRING(ty->ht_name);
259 DISCARD(PyObject_INIT(&ty->ht_type, metaty));
264 void typeready(PyTypeObject *ty)
267 PyDict_SetItemString(ty->tp_dict, "__module__", modname);
270 PyTypeObject *inittype(PyTypeObject *tyskel, PyTypeObject *meta)
272 PyTypeObject *ty = newtype(meta, tyskel, 0);
273 ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
278 /*----- Constants ---------------------------------------------------------*/
280 void setconstants(PyObject *mod, const struct nameval *c)
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++;
294 /*----- Building method tables --------------------------------------------*/
296 static PyMethodDef *global_methods;
297 static size_t nmethods, methodsz;
299 void addmethods(const PyMethodDef *m)
301 size_t n, want, newsz;
303 for (n = 0; m[n].ml_name; n++);
304 want = nmethods + n + 1;
305 if (want > methodsz) {
306 newsz = methodsz ? 2*methodsz : 16;
307 while (want > newsz) newsz *= 2;
309 global_methods = PyObject_Malloc(newsz*sizeof(PyMethodDef));
311 global_methods = PyObject_Realloc(global_methods,
312 newsz*sizeof(PyMethodDef));
313 assert(global_methods);
316 memcpy(global_methods + nmethods, m, n*sizeof(PyMethodDef));
318 global_methods[nmethods].ml_name = 0;
321 PyMethodDef *donemethods(void) { return (global_methods); }
323 /*----- Exceptions --------------------------------------------------------*/
325 PyObject *mkexc(PyObject *mod, PyObject *base,
326 const char *name, PyMethodDef *mm)
328 PyObject *nameobj = 0;
334 if ((dict = PyDict_New()) == 0) goto fail;
337 while (mm->ml_name) {
338 if ((func = PyCFunction_NewEx(mm, 0, mod)) == 0 ||
339 (meth = PyMethod_New(func, 0, exc)) == 0 ||
340 PyDict_SetItemString(dict, mm->ml_name, meth))
342 Py_DECREF(func); func = 0;
343 Py_DECREF(meth); meth = 0;
348 if ((nameobj = PyString_FromFormat("%s.%s",
349 PyModule_GetName(mod),
351 (exc = PyErr_NewException(PyString_AS_STRING(nameobj),
368 void report_lost_exception_v(struct excinfo *exc,
369 const char *why, va_list ap)
371 PyObject *hookfn = 0;
372 PyObject *whyobj = 0;
375 /* Make sure we start out without a pending exception, or this will get
378 assert(!PyErr_Occurred());
380 /* Format the explanation. */
381 if (why) whyobj = PyString_FromFormatV(why, ap);
382 else { whyobj = Py_None; Py_INCREF(whyobj); }
384 /* Find our home module's `lostexchook' function. This won't work if
385 * there's no module, or the function isn't defined, or it's `None'.
387 if (!home_module) goto sys;
388 hookfn = PyObject_GetAttrString(home_module, "lostexchook");
389 if (hookfn == Py_None) goto sys;
391 else if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto ouch;
392 else { PyErr_Clear(); goto sys; }
394 /* Call the hook function. */
395 obj = PyObject_CallFunction(hookfn, "(OOOO)",
396 whyobj, exc->ty, exc->val, exc->tb);
400 /* Something went wrong reporting the problem. */
402 PySys_WriteStderr("\n!!! FAILURE REPORTING LOST EXCEPTION\n");
404 /* drop through... */
406 /* There was no hook, so try to do something sensible using
410 PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n",
411 PyString_AS_STRING(whyobj));
412 RESTORE_EXCINFO(exc);
414 /* drop through... */
416 /* Clean up afterwards. */
423 void report_lost_exception(struct excinfo *exc, const char *why, ...)
428 report_lost_exception_v(exc, why, ap);
432 void stash_exception(struct excinfo *exc, const char *why, ...)
435 struct excinfo stash;
441 STASH_EXCINFO(&stash);
442 report_lost_exception_v(&stash, why, ap);
447 void restore_exception(struct excinfo *exc, const char *why, ...)
450 struct excinfo stash;
452 if (!PyErr_Occurred())
453 RESTORE_EXCINFO(exc);
456 STASH_EXCINFO(&stash);
457 report_lost_exception_v(exc, why, ap);
458 RESTORE_EXCINFO(&stash);
463 /*----- Generic dictionary methods ----------------------------------------*/
465 static PyTypeObject *itemiter_pytype, *valiter_pytype;
467 typedef struct iter_pyobj {
472 #define ITER_MAP(o) (((iter_pyobj *)(o))->map)
473 #define ITER_I(o) (((iter_pyobj *)(o))->i)
475 static void iter_pydealloc(PyObject *me)
476 { Py_DECREF(ITER_MAP(me)); Py_DECREF(ITER_I(me)); FREEOBJ(me); }
478 static PyObject *itemiter_pynext(PyObject *me)
480 PyObject *k = 0, *v = 0, *rc = 0;
482 if ((k = PyIter_Next(ITER_I(me))) != 0 &&
483 (v = PyObject_GetItem(ITER_MAP(me), k)) != 0)
484 rc = Py_BuildValue("(OO)", k, v);
485 Py_XDECREF(k); Py_XDECREF(v);
489 static PyTypeObject itemiter_pytype_skel = {
490 PyObject_HEAD_INIT(0) 0, /* Header */
491 "ItemIter", /* @tp_name@ */
492 sizeof(iter_pyobj), /* @tp_basicsize@ */
493 0, /* @tp_itemsize@ */
495 iter_pydealloc, /* @tp_dealloc@ */
497 0, /* @tp_getattr@ */
498 0, /* @tp_setattr@ */
499 0, /* @tp_compare@ */
501 0, /* @tp_as_number@ */
502 0, /* @tp_as_sequence@ */
503 0, /* @tp_as_mapping@ */
507 0, /* @tp_getattro@ */
508 0, /* @tp_setattro@ */
509 0, /* @tp_as_buffer@ */
510 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
514 "Iterates over the items of a mapping.",
516 0, /* @tp_traverse@ */
518 0, /* @tp_richcompare@ */
519 0, /* @tp_weaklistoffset@ */
520 PyObject_SelfIter, /* @tp_iter@ */
521 itemiter_pynext, /* @tp_iternext@ */
522 0, /* @tp_methods@ */
523 0, /* @tp_members@ */
527 0, /* @tp_descr_get@ */
528 0, /* @tp_descr_set@ */
529 0, /* @tp_dictoffset@ */
531 PyType_GenericAlloc, /* @tp_alloc@ */
532 abstract_pynew, /* @tp_new@ */
537 static PyObject *valiter_pynext(PyObject *me)
539 PyObject *k = 0, *rc = 0;
541 if ((k = PyIter_Next(ITER_I(me))) != 0)
542 rc = PyObject_GetItem(ITER_MAP(me), k);
547 static PyTypeObject valiter_pytype_skel = {
548 PyObject_HEAD_INIT(0) 0, /* Header */
549 "ValueIter", /* @tp_name@ */
550 sizeof(iter_pyobj), /* @tp_basicsize@ */
551 0, /* @tp_itemsize@ */
553 iter_pydealloc, /* @tp_dealloc@ */
555 0, /* @tp_getattr@ */
556 0, /* @tp_setattr@ */
557 0, /* @tp_compare@ */
559 0, /* @tp_as_number@ */
560 0, /* @tp_as_sequence@ */
561 0, /* @tp_as_mapping@ */
565 0, /* @tp_getattro@ */
566 0, /* @tp_setattro@ */
567 0, /* @tp_as_buffer@ */
568 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
572 "Iterates over the values of a mapping.",
574 0, /* @tp_traverse@ */
576 0, /* @tp_richcompare@ */
577 0, /* @tp_weaklistoffset@ */
578 PyObject_SelfIter, /* @tp_iter@ */
579 valiter_pynext, /* @tp_iternext@ */
580 0, /* @tp_methods@ */
581 0, /* @tp_members@ */
585 0, /* @tp_descr_get@ */
586 0, /* @tp_descr_set@ */
587 0, /* @tp_dictoffset@ */
589 PyType_GenericAlloc, /* @tp_alloc@ */
590 abstract_pynew, /* @tp_new@ */
595 PySequenceMethods gmap_pysequence = {
601 0, /* @sq_ass_item@ */
602 0, /* @sq_ass_slice@ */
603 PyMapping_HasKey, /* @sq_contains@ */
604 0, /* @sq_inplace_concat@ */
605 0 /* @sq_inplace_repeat@ */
608 Py_ssize_t gmap_pysize(PyObject *me)
610 PyObject *i = 0, *x = 0;
611 Py_ssize_t rc = -1, n = 0;
613 if ((i = PyObject_GetIter(me)) == 0) goto done;
614 while ((x = PyIter_Next(i)) != 0) { n++; Py_DECREF(x); x = 0; }
615 if (PyErr_Occurred()) goto done;
618 Py_XDECREF(i); Py_XDECREF(x);
622 PyObject *gmapmeth_has_key(PyObject *me, PyObject *arg)
625 if (!PyArg_ParseTuple(arg, "O:has_key", &k)) return (0);
626 return (getbool(PyMapping_HasKey(me, k)));
629 PyObject *gmapmeth_keys(PyObject *me, PyObject *arg)
631 PyObject *l = 0, *i = 0, *k, *rc = 0;
634 if (!PyArg_ParseTuple(arg, ":keys") ||
635 (l = PyList_New(0)) == 0 ||
636 (i = PyObject_GetIter(me)) == 0)
638 while ((k = PyIter_Next(i)) != 0)
639 { err = PyList_Append(l, k); Py_DECREF(k); if (err) goto done; }
640 if (PyErr_Occurred()) goto done;
643 Py_XDECREF(l); Py_XDECREF(i);
647 PyObject *gmapmeth_values(PyObject *me, PyObject *arg)
649 PyObject *l = 0, *i = 0, *k, *v, *rc = 0;
652 if (!PyArg_ParseTuple(arg, ":values") ||
653 (l = PyList_New(0)) == 0 ||
654 (i = PyObject_GetIter(me)) == 0)
656 while ((k = PyIter_Next(i)) != 0) {
657 if ((v = PyObject_GetItem(me, k)) == 0 ||
660 Py_DECREF(k); Py_XDECREF(v);
663 if (PyErr_Occurred()) goto done;
666 Py_XDECREF(l); Py_XDECREF(i);
670 PyObject *gmapmeth_items(PyObject *me, PyObject *arg)
672 PyObject *l = 0, *i = 0, *k, *v, *z, *rc = 0;
675 if (!PyArg_ParseTuple(arg, ":items") ||
676 (l = PyList_New(0)) == 0 ||
677 (i = PyObject_GetIter(me)) == 0)
679 while ((k = PyIter_Next(i)) != 0) {
681 if ((v = PyObject_GetItem(me, k)) == 0 ||
682 (z = Py_BuildValue("(OO)", k, v)) == 0 ||
685 Py_DECREF(k); Py_XDECREF(v); Py_XDECREF(z);
688 if (PyErr_Occurred()) goto done;
691 Py_XDECREF(l); Py_XDECREF(i);
695 PyObject *gmapmeth_iterkeys(PyObject *me, PyObject *arg)
697 if (!PyArg_ParseTuple(arg, ":iterkeys")) return (0);
698 return (PyObject_GetIter(me));
701 PyObject *gmapmeth_itervalues(PyObject *me, PyObject *arg)
706 if (!PyArg_ParseTuple(arg, ":itervalues") ||
707 (i = PyObject_GetIter(me)) == 0)
709 ii = PyObject_NEW(iter_pyobj, valiter_pytype);
710 ii->map = me; Py_INCREF(me);
712 return ((PyObject *)ii);
715 PyObject *gmapmeth_iteritems(PyObject *me, PyObject *arg)
720 if (!PyArg_ParseTuple(arg, ":iteritems") ||
721 (i = PyObject_GetIter(me)) == 0)
723 ii = PyObject_NEW(iter_pyobj, itemiter_pytype);
724 ii->map = me; Py_INCREF(me);
726 return ((PyObject *)ii);
729 PyObject *gmapmeth_clear(PyObject *me, PyObject *arg)
731 PyObject *i = 0, *k = 0, *rc = 0;
733 if (!PyArg_ParseTuple(arg, ":clear") ||
734 (i = PyObject_GetIter(me)) == 0)
736 while ((k = PyIter_Next(i)) != 0) {
737 PyObject_DelItem(me, k);
740 if (PyErr_Occurred()) goto end;
741 rc = me; Py_INCREF(me);
747 static const char *const def_kwlist[] = { "key", "default", 0 };
749 PyObject *gmapmeth_get(PyObject *me, PyObject *arg, PyObject *kw)
751 PyObject *k, *def = Py_None, *v;
753 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:get",
754 (/*unconst*/ char **)def_kwlist,
757 if ((v = PyObject_GetItem(me, k)) != 0) return (v);
762 PyObject *gmapmeth_setdefault(PyObject *me, PyObject *arg, PyObject *kw)
764 PyObject *k, *def = Py_None, *v;
766 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:setdefault",
767 (/*unconst*/ char **)def_kwlist,
770 if ((v = PyObject_GetItem(me, k)) != 0) return (v);
772 if (PyObject_SetItem(me, k, def)) return (0);
776 PyObject *gmapmeth_pop(PyObject *me, PyObject *arg, PyObject *kw)
778 PyObject *k, *def = 0, *v;
780 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:pop",
781 (/*unconst*/ char **)def_kwlist,
784 if ((v = PyObject_GetItem(me, k)) != 0) {
785 PyObject_DelItem(me, k);
794 PyObject *gmapmeth_update(PyObject *me, PyObject *arg)
796 PyObject *map, *i = 0, *k, *v, *rc = 0;
799 if (!PyArg_ParseTuple(arg, "O:update", &map) ||
800 (i = PyObject_GetIter(map)) == 0)
802 while ((k = PyIter_Next(i)) != 0) {
803 if ((v = PyObject_GetItem(map, k)) == 0 ||
804 PyObject_SetItem(me, k, v))
806 Py_DECREF(k); Py_XDECREF(v);
809 if (PyErr_Occurred()) goto end;
810 rc = me; Py_INCREF(me);
816 PyObject *gmapmeth_popitem(PyObject *me, PyObject *arg)
818 PyObject *i = 0, *k = 0, *v = 0, *rc = 0;
820 if (!PyArg_ParseTuple(arg, ":popitem") ||
821 (i = PyObject_GetIter(me)) == 0)
823 if ((k = PyIter_Next(i)) == 0) {
824 if (!PyErr_Occurred()) VALERR("popitem(): mapping is empty");
827 if ((v = PyObject_GetItem(me, k)) == 0 ||
828 PyObject_DelItem(me, k))
830 rc = Py_BuildValue("(OO)", k, v);
832 Py_XDECREF(i); Py_XDECREF(k); Py_XDECREF(v);
836 PyMethodDef gmap_pymethods[] = {
841 /*----- Initialization ----------------------------------------------------*/
843 static PyObject *meth__set_home_module(PyObject *me, PyObject *arg)
847 if (!PyArg_ParseTuple(arg, "O!:_set_home_module", &PyModule_Type, &mod))
849 Py_XDECREF(home_module); home_module = mod; Py_INCREF(home_module);
853 static const PyMethodDef methods[] = {
854 #define METHNAME(func) meth_##func
855 METH (_set_home_module, "_set_home_module(MOD)")
860 void util_pyinit(void)
862 INITTYPE(itemiter, root);
863 INITTYPE(valiter, root);
867 void util_pyinsert(PyObject *mod)
869 INSERT("ItemIter", itemiter_pytype);
870 INSERT("ValueIter", valiter_pytype);
873 /*----- That's all, folks -------------------------------------------------*/