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 static PyObject *modname = 0;
37 /*----- Conversions -------------------------------------------------------*/
39 PyObject *getulong(unsigned long w)
42 return (PyInt_FromLong(w));
44 return (PyLong_FromUnsignedLong(w));
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); }
53 PyObject *getk64(kludge64 u)
56 return (PyLong_FromUnsignedLongLong(GET64(unsigned PY_LONG_LONG, u)));
58 PyObject *i = 0, *j = 0, *t;
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;
65 if ((j = PyLong_FromUnsignedLong(LO64(u))) == 0) goto end;
66 if ((t = PyNumber_InPlaceOr(i, j)) == 0) goto end;
68 if ((rc = PyNumber_Int(i)) == 0) goto end;
76 PyObject *getbool(int b)
82 PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
84 PyErr_SetString(PyExc_TypeError, "can't instantiate this class");
88 int convulong(PyObject *o, void *pp)
91 unsigned long *p = pp;
94 if (!o) VALERR("can't delete");
97 if (i < 0) VALERR("must be nonnegative");
100 if ((t = PyNumber_Long(o)) == 0) goto end;
101 *p = PyLong_AsUnsignedLong(t);
103 if (PyErr_Occurred()) goto end;
111 # define CONVu64(n) do { \
114 if (!convk64(o, &k)) goto end; \
115 t = GET64(uint64, k); \
116 if (t > MASK##n) VALERR("out of range"); \
120 # define CONVu64(n) assert(!"shouldn't be possible")
124 int convu##n(PyObject *o, void *pp) \
129 if (MASK##n > ULONG_MAX) \
132 if (!convulong(o, &u)) goto end; \
133 if (u > MASK##n) VALERR("out of range"); \
142 int convuint(PyObject *o, void *pp)
147 if (!convulong(o, &u)) goto end;
148 if (u > UINT_MAX) VALERR("out of range");
155 int convk64(PyObject *o, void *pp)
160 unsigned PY_LONG_LONG t;
166 if (!o) VALERR("can't delete");
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);
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;
178 hi = PyInt_AsUnsignedLongMask(i);
179 if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end;
181 if (PyObject_IsTrue(i)) VALERR("out of range");
182 SET64(*(kludge64 *)pp, hi, lo);
191 int convmpw(PyObject *o, void *pp)
196 if (!convulong(o, &u)) goto end;
197 if (u > MPW_MAX) VALERR("out of range");
204 int convszt(PyObject *o, void *pp)
209 if (!convulong(o, &u)) goto end;
210 if (u > ~(size_t)0) VALERR("out of range");
217 int convbool(PyObject *o, void *pp)
219 if (!o) VALERR("can't delete");
220 *(int *)pp = PyObject_IsTrue(o);
226 /*----- Type messing ------------------------------------------------------*/
228 static const PyTypeObject emptytype = { 0 };
230 void *newtype(PyTypeObject *metaty,
231 const PyTypeObject *skel,
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; \
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);
257 ty->ht_type.tp_name = PyString_AS_STRING(ty->ht_name);
258 DISCARD(PyObject_INIT(&ty->ht_type, metaty));
263 void typeready(PyTypeObject *ty)
266 PyDict_SetItemString(ty->tp_dict, "__module__", modname);
269 PyTypeObject *inittype(PyTypeObject *tyskel, PyTypeObject *meta)
271 PyTypeObject *ty = newtype(meta, tyskel, 0);
272 ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
277 /*----- Constants ---------------------------------------------------------*/
279 void setconstants(PyObject *mod, const struct nameval *c)
284 if (c->value > LONG_MAX)
285 x = PyLong_FromUnsignedLong(c->value);
287 x = PyInt_FromLong(c->value);
288 PyModule_AddObject(mod, (/*unconst*/ char *)c->name, x);
293 /*----- Building method tables --------------------------------------------*/
295 DA_DECL(method_v, PyMethodDef);
296 static method_v global_pymethods = DA_INIT;
297 void addmethods(const PyMethodDef *m)
301 for (n = 0; m[n].ml_name; n++);
302 DA_ENSURE(&global_pymethods, n);
303 memcpy(DA(&global_pymethods) + DA_LEN(&global_pymethods),
305 DA_EXTEND(&global_pymethods, n);
308 PyMethodDef *donemethods(void)
310 static const PyMethodDef mzero = { 0 };
311 DA_PUSH(&global_pymethods, mzero);
312 return (DA(&global_pymethods));
315 /*----- Exceptions --------------------------------------------------------*/
317 PyObject *mkexc(PyObject *mod, PyObject *base,
318 const char *name, PyMethodDef *mm)
320 PyObject *nameobj = 0;
326 if ((nameobj = PyString_FromFormat("%s.%s",
327 PyModule_GetName(mod),
329 (dict = PyDict_New()) == 0 ||
330 (exc = PyErr_NewException(PyString_AS_STRING(nameobj),
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))
340 Py_DECREF(func); func = 0;
341 Py_DECREF(meth); meth = 0;
359 /*----- Generic dictionary methods ----------------------------------------*/
361 static PyTypeObject *itemiter_pytype, *valiter_pytype;
363 typedef struct iter_pyobj {
368 #define ITER_MAP(o) (((iter_pyobj *)(o))->map)
369 #define ITER_I(o) (((iter_pyobj *)(o))->i)
371 static void iter_pydealloc(PyObject *me)
372 { Py_DECREF(ITER_MAP(me)); Py_DECREF(ITER_I(me)); FREEOBJ(me); }
374 static PyObject *itemiter_pynext(PyObject *me)
376 PyObject *k = 0, *v = 0, *rc = 0;
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);
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@ */
391 iter_pydealloc, /* @tp_dealloc@ */
393 0, /* @tp_getattr@ */
394 0, /* @tp_setattr@ */
395 0, /* @tp_compare@ */
397 0, /* @tp_as_number@ */
398 0, /* @tp_as_sequence@ */
399 0, /* @tp_as_mapping@ */
403 0, /* @tp_getattro@ */
404 0, /* @tp_setattro@ */
405 0, /* @tp_as_buffer@ */
406 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
410 "Iterates over the items of a mapping.",
412 0, /* @tp_traverse@ */
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@ */
423 0, /* @tp_descr_get@ */
424 0, /* @tp_descr_set@ */
425 0, /* @tp_dictoffset@ */
427 PyType_GenericAlloc, /* @tp_alloc@ */
428 abstract_pynew, /* @tp_new@ */
433 static PyObject *valiter_pynext(PyObject *me)
435 PyObject *k = 0, *rc = 0;
437 if ((k = PyIter_Next(ITER_I(me))) != 0)
438 rc = PyObject_GetItem(ITER_MAP(me), k);
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@ */
449 iter_pydealloc, /* @tp_dealloc@ */
451 0, /* @tp_getattr@ */
452 0, /* @tp_setattr@ */
453 0, /* @tp_compare@ */
455 0, /* @tp_as_number@ */
456 0, /* @tp_as_sequence@ */
457 0, /* @tp_as_mapping@ */
461 0, /* @tp_getattro@ */
462 0, /* @tp_setattro@ */
463 0, /* @tp_as_buffer@ */
464 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
468 "Iterates over the items of a mapping.",
470 0, /* @tp_traverse@ */
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@ */
481 0, /* @tp_descr_get@ */
482 0, /* @tp_descr_set@ */
483 0, /* @tp_dictoffset@ */
485 PyType_GenericAlloc, /* @tp_alloc@ */
486 abstract_pynew, /* @tp_new@ */
491 PySequenceMethods gmap_pysequence = {
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@ */
504 Py_ssize_t gmap_pysize(PyObject *me)
506 PyObject *i = 0, *x = 0;
510 if ((i = PyObject_GetIter(me)) == 0) goto done;
511 while ((x = PyIter_Next(i)) != 0) { n++; Py_DECREF(x); x = 0; }
512 if (PyErr_Occurred()) goto done;
515 Py_XDECREF(i); Py_XDECREF(x);
519 PyObject *gmapmeth_has_key(PyObject *me, PyObject *arg)
522 if (!PyArg_ParseTuple(arg, "O:has_key", &k)) return (0);
523 return (getbool(PyMapping_HasKey(me, k)));
526 PyObject *gmapmeth_keys(PyObject *me, PyObject *arg)
528 PyObject *l = 0, *i = 0, *k, *rc = 0;
531 if (!PyArg_ParseTuple(arg, ":keys") ||
532 (l = PyList_New(0)) == 0 ||
533 (i = PyObject_GetIter(me)) == 0)
535 while ((k = PyIter_Next(i)) != 0)
536 { err = PyList_Append(l, k); Py_DECREF(k); if (err) goto done; }
537 if (PyErr_Occurred()) goto done;
540 Py_XDECREF(l); Py_XDECREF(i);
544 PyObject *gmapmeth_values(PyObject *me, PyObject *arg)
546 PyObject *l = 0, *i = 0, *k, *v, *rc = 0;
549 if (!PyArg_ParseTuple(arg, ":values") ||
550 (l = PyList_New(0)) == 0 ||
551 (i = PyObject_GetIter(me)) == 0)
553 while ((k = PyIter_Next(i)) != 0) {
554 if ((v = PyObject_GetItem(me, k)) == 0 ||
557 Py_DECREF(k); Py_XDECREF(v);
560 if (PyErr_Occurred()) goto done;
563 Py_XDECREF(l); Py_XDECREF(i);
567 PyObject *gmapmeth_items(PyObject *me, PyObject *arg)
569 PyObject *l = 0, *i = 0, *k, *v, *z, *rc = 0;
572 if (!PyArg_ParseTuple(arg, ":items") ||
573 (l = PyList_New(0)) == 0 ||
574 (i = PyObject_GetIter(me)) == 0)
576 while ((k = PyIter_Next(i)) != 0) {
578 if ((v = PyObject_GetItem(me, k)) == 0 ||
579 (z = Py_BuildValue("(OO)", k, v)) == 0 ||
582 Py_DECREF(k); Py_XDECREF(v); Py_XDECREF(z);
585 if (PyErr_Occurred()) goto done;
588 Py_XDECREF(l); Py_XDECREF(i);
592 PyObject *gmapmeth_iterkeys(PyObject *me, PyObject *arg)
594 if (!PyArg_ParseTuple(arg, ":iterkeys")) return (0);
595 return (PyObject_GetIter(me));
598 PyObject *gmapmeth_itervalues(PyObject *me, PyObject *arg)
603 if (!PyArg_ParseTuple(arg, ":itervalues") ||
604 (i = PyObject_GetIter(me)) == 0)
606 ii = PyObject_NEW(iter_pyobj, valiter_pytype);
607 ii->map = me; Py_INCREF(me);
609 return ((PyObject *)ii);
612 PyObject *gmapmeth_iteritems(PyObject *me, PyObject *arg)
617 if (!PyArg_ParseTuple(arg, ":iteritems") ||
618 (i = PyObject_GetIter(me)) == 0)
620 ii = PyObject_NEW(iter_pyobj, itemiter_pytype);
621 ii->map = me; Py_INCREF(me);
623 return ((PyObject *)ii);
626 PyObject *gmapmeth_clear(PyObject *me, PyObject *arg)
628 PyObject *i = 0, *k = 0, *rc = 0;
630 if (!PyArg_ParseTuple(arg, ":clear") ||
631 (i = PyObject_GetIter(me)) == 0)
633 while ((k = PyIter_Next(i)) != 0) {
634 PyObject_DelItem(me, k);
637 if (PyErr_Occurred()) goto end;
638 rc = me; Py_INCREF(me);
644 static char *def_kwlist[] = { "key", "default", 0 };
646 PyObject *gmapmeth_get(PyObject *me, PyObject *arg, PyObject *kw)
648 PyObject *k, *def = Py_None, *v;
650 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:get", def_kwlist, &k, &def))
652 if ((v = PyObject_GetItem(me, k)) != 0) return (v);
657 PyObject *gmapmeth_setdefault(PyObject *me, PyObject *arg, PyObject *kw)
659 PyObject *k, *def = Py_None, *v;
661 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:setdefault",
662 def_kwlist, &k, &def))
664 if ((v = PyObject_GetItem(me, k)) != 0) return (v);
666 if (PyObject_SetItem(me, k, def)) return (0);
670 PyObject *gmapmeth_pop(PyObject *me, PyObject *arg, PyObject *kw)
672 PyObject *k, *def = 0, *v;
674 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:pop", def_kwlist, &k, &def))
676 if ((v = PyObject_GetItem(me, k)) != 0) {
677 PyObject_DelItem(me, k);
684 PyObject *gmapmeth_update(PyObject *me, PyObject *arg)
686 PyObject *map, *i = 0, *k, *v, *rc = 0;
689 if (!PyArg_ParseTuple(arg, "O:update", &map) ||
690 (i = PyObject_GetIter(map)) == 0)
692 while ((k = PyIter_Next(i)) != 0) {
693 if ((v = PyObject_GetItem(map, k)) == 0 ||
694 PyObject_SetItem(me, k, v))
696 Py_DECREF(k); Py_XDECREF(v);
699 if (PyErr_Occurred()) goto end;
700 rc = me; Py_INCREF(me);
706 PyObject *gmapmeth_popitem(PyObject *me, PyObject *arg)
708 PyObject *i = 0, *k = 0, *v = 0, *rc = 0;
710 if (!PyArg_ParseTuple(arg, ":popitem") ||
711 (i = PyObject_GetIter(me)))
713 if ((k = PyIter_Next(i)) == 0) {
714 if (!PyErr_Occurred()) VALERR("popitem(): mapping is empty");
717 if ((v = PyObject_GetItem(me, k)) == 0 ||
718 PyObject_DelItem(me, k))
720 rc = Py_BuildValue("(OO)", k, v);
722 Py_XDECREF(i); Py_XDECREF(k); Py_XDECREF(v);
726 PyMethodDef gmap_pymethods[] = {
731 /*----- Initialization ----------------------------------------------------*/
733 void util_pyinit(void)
735 modname = PyString_FromString("catacomb");
736 INITTYPE(itemiter, root);
737 INITTYPE(valiter, root);
740 void util_pyinsert(PyObject *mod)
742 INSERT("ItemIter", itemiter_pytype);
743 INSERT("ValueIter", valiter_pytype);
746 /*----- That's all, folks -------------------------------------------------*/