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 /*----- External values ---------------------------------------------------*/
33 static PyObject *modname = 0;
35 /*----- Conversions -------------------------------------------------------*/
37 PyObject *getulong(unsigned long w)
40 return (PyInt_FromLong(w));
42 return (PyLong_FromUnsignedLong(w));
45 PyObject *getbool(int b)
51 PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
53 PyErr_SetString(PyExc_TypeError, "can't instantiate this class");
57 int convulong(PyObject *o, void *pp)
60 unsigned long *p = pp;
65 if (i < 0) VALERR("must be nonnegative");
68 if ((t = PyNumber_Long(o)) == 0) goto end;
69 *p = PyLong_AsUnsignedLong(t);
71 if (PyErr_Occurred()) goto end;
79 int convu##n(PyObject *o, void *pp) \
84 if (!convulong(o, &u)) goto end; \
85 if (u > MASK##n) VALERR("out of range"); \
93 int convuint(PyObject *o, void *pp)
98 if (!convulong(o, &u)) goto end;
99 if (u > UINT_MAX) VALERR("out of range");
106 int convmpw(PyObject *o, void *pp)
111 if (!convulong(o, &u)) goto end;
112 if (u > MPW_MAX) VALERR("out of range");
119 int convszt(PyObject *o, void *pp)
124 if (!convulong(o, &u)) goto end;
125 if (u > ~(size_t)0) VALERR("out of range");
132 int convbool(PyObject *o, void *pp)
134 *(int *)pp = PyObject_IsTrue(o);
138 /*----- Type messing ------------------------------------------------------*/
140 static const PyTypeObject emptytype = { 0 };
142 void *newtype(PyTypeObject *metaty,
143 const PyTypeObject *skel,
146 PyHeapTypeObject *ty =
147 (PyHeapTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0));
148 if (!skel) skel = &emptytype;
149 memcpy(ty, skel, sizeof(*skel));
150 if (ty->ht_type.tp_base) Py_INCREF(ty->ht_type.tp_base);
151 #define COPY(blah) do { \
152 if (ty->ht_type.tp_as_##blah) { \
153 memcpy(&ty->as_##blah, \
154 ty->ht_type.tp_as_##blah, \
155 sizeof(ty->as_##blah)); \
156 ty->ht_type.tp_as_##blah = &ty->as_##blah; \
165 ty->ht_name = PyString_FromString(name);
166 else if (ty->ht_type.tp_name)
167 ty->ht_name = PyString_FromString(ty->ht_type.tp_name);
169 ty->ht_type.tp_name = PyString_AS_STRING(ty->ht_name);
170 DISCARD(PyObject_INIT(&ty->ht_type, metaty));
175 void typeready(PyTypeObject *ty)
178 PyDict_SetItemString(ty->tp_dict, "__module__", modname);
181 PyTypeObject *inittype(PyTypeObject *tyskel)
183 PyTypeObject *ty = newtype(&PyType_Type, tyskel, 0);
184 ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
189 /*----- Constants ---------------------------------------------------------*/
191 void setconstants(PyObject *mod, const struct nameval *c)
196 if (c->value > LONG_MAX)
197 x = PyLong_FromUnsignedLong(c->value);
199 x = PyInt_FromLong(c->value);
200 PyModule_AddObject(mod, (/*unconst*/ char *)c->name, x);
205 /*----- Building method tables --------------------------------------------*/
207 DA_DECL(method_v, PyMethodDef);
208 static method_v global_pymethods = DA_INIT;
209 void addmethods(const PyMethodDef *m)
213 for (n = 0; m[n].ml_name; n++);
214 DA_ENSURE(&global_pymethods, n);
215 memcpy(DA(&global_pymethods) + DA_LEN(&global_pymethods),
217 DA_EXTEND(&global_pymethods, n);
220 PyMethodDef *donemethods(void)
222 static const PyMethodDef mzero = { 0 };
223 DA_PUSH(&global_pymethods, mzero);
224 return (DA(&global_pymethods));
227 /*----- Exceptions --------------------------------------------------------*/
229 PyObject * mkexc(PyObject *mod, PyObject *base,
230 const char *name, PyMethodDef *mm)
232 PyObject *nameobj = 0;
238 if ((nameobj = PyString_FromFormat("%s.%s",
239 PyModule_GetName(mod),
241 (dict = PyDict_New()) == 0 ||
242 (exc = PyErr_NewException(PyString_AS_STRING(nameobj),
247 while (mm->ml_name) {
248 if ((func = PyCFunction_NewEx(mm, 0, mod)) == 0 ||
249 (meth = PyMethod_New(func, 0, exc)) == 0 ||
250 PyDict_SetItemString(dict, mm->ml_name, meth))
252 Py_DECREF(func); func = 0;
253 Py_DECREF(meth); meth = 0;
271 /*----- Generic dictionary methods ----------------------------------------*/
273 static PyTypeObject *itemiter_pytype, *valiter_pytype;
275 typedef struct iter_pyobj {
280 #define ITER_MAP(o) (((iter_pyobj *)(o))->map)
281 #define ITER_I(o) (((iter_pyobj *)(o))->i)
283 static void iter_pydealloc(PyObject *me)
284 { Py_DECREF(ITER_MAP(me)); Py_DECREF(ITER_I(me)); FREEOBJ(me); }
286 static PyObject *itemiter_pynext(PyObject *me)
288 PyObject *k = 0, *v = 0, *rc = 0;
290 if ((k = PyIter_Next(ITER_I(me))) != 0 &&
291 (v = PyObject_GetItem(ITER_MAP(me), k)) != 0)
292 rc = Py_BuildValue("(OO)", k, v);
293 Py_XDECREF(k); Py_XDECREF(v);
297 static PyTypeObject itemiter_pytype_skel = {
298 PyObject_HEAD_INIT(0) 0, /* Header */
299 "ItemIter", /* @tp_name@ */
300 sizeof(iter_pyobj), /* @tp_basicsize@ */
301 0, /* @tp_itemsize@ */
303 iter_pydealloc, /* @tp_dealloc@ */
305 0, /* @tp_getattr@ */
306 0, /* @tp_setattr@ */
307 0, /* @tp_compare@ */
309 0, /* @tp_as_number@ */
310 0, /* @tp_as_sequence@ */
311 0, /* @tp_as_mapping@ */
315 0, /* @tp_getattro@ */
316 0, /* @tp_setattro@ */
317 0, /* @tp_as_buffer@ */
318 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
322 "Iterates over the items of a mapping.",
324 0, /* @tp_traverse@ */
326 0, /* @tp_richcompare@ */
327 0, /* @tp_weaklistoffset@ */
328 PyObject_SelfIter, /* @tp_iter@ */
329 itemiter_pynext, /* @tp_iternext@ */
330 0, /* @tp_methods@ */
331 0, /* @tp_members@ */
335 0, /* @tp_descr_get@ */
336 0, /* @tp_descr_set@ */
337 0, /* @tp_dictoffset@ */
339 PyType_GenericAlloc, /* @tp_alloc@ */
340 abstract_pynew, /* @tp_new@ */
345 static PyObject *valiter_pynext(PyObject *me)
347 PyObject *k = 0, *rc = 0;
349 if ((k = PyIter_Next(ITER_I(me))) != 0)
350 rc = PyObject_GetItem(ITER_MAP(me), k);
355 static PyTypeObject valiter_pytype_skel = {
356 PyObject_HEAD_INIT(0) 0, /* Header */
357 "ValueIter", /* @tp_name@ */
358 sizeof(iter_pyobj), /* @tp_basicsize@ */
359 0, /* @tp_itemsize@ */
361 iter_pydealloc, /* @tp_dealloc@ */
363 0, /* @tp_getattr@ */
364 0, /* @tp_setattr@ */
365 0, /* @tp_compare@ */
367 0, /* @tp_as_number@ */
368 0, /* @tp_as_sequence@ */
369 0, /* @tp_as_mapping@ */
373 0, /* @tp_getattro@ */
374 0, /* @tp_setattro@ */
375 0, /* @tp_as_buffer@ */
376 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
380 "Iterates over the items of a mapping.",
382 0, /* @tp_traverse@ */
384 0, /* @tp_richcompare@ */
385 0, /* @tp_weaklistoffset@ */
386 PyObject_SelfIter, /* @tp_iter@ */
387 valiter_pynext, /* @tp_iternext@ */
388 0, /* @tp_methods@ */
389 0, /* @tp_members@ */
393 0, /* @tp_descr_get@ */
394 0, /* @tp_descr_set@ */
395 0, /* @tp_dictoffset@ */
397 PyType_GenericAlloc, /* @tp_alloc@ */
398 abstract_pynew, /* @tp_new@ */
403 PySequenceMethods gmap_pysequence = {
409 0, /* @sq_ass_item@ */
410 0, /* @sq_ass_slice@ */
411 PyMapping_HasKey, /* @sq_contains@ */
412 0, /* @sq_inplace_concat@ */
413 0 /* @sq_inplace_repeat@ */
416 Py_ssize_t gmap_pysize(PyObject *me)
418 PyObject *i = 0, *x = 0;
422 if ((i = PyObject_GetIter(me)) == 0) goto done;
423 while ((x = PyIter_Next(i)) != 0) { n++; Py_DECREF(x); x = 0; }
424 if (PyErr_Occurred()) goto done;
427 Py_XDECREF(i); Py_XDECREF(x);
431 PyObject *gmapmeth_has_key(PyObject *me, PyObject *arg)
434 if (!PyArg_ParseTuple(arg, "O:has_key", &k)) return (0);
435 return (getbool(PyMapping_HasKey(me, k)));
438 PyObject *gmapmeth_keys(PyObject *me, PyObject *arg)
440 PyObject *l = 0, *i = 0, *k, *rc = 0;
443 if (!PyArg_ParseTuple(arg, ":keys") ||
444 (l = PyList_New(0)) == 0 ||
445 (i = PyObject_GetIter(me)) == 0)
447 while ((k = PyIter_Next(i)) != 0)
448 { err = PyList_Append(l, k); Py_DECREF(k); if (err) goto done; }
449 if (PyErr_Occurred()) goto done;
452 Py_XDECREF(l); Py_XDECREF(i);
456 PyObject *gmapmeth_values(PyObject *me, PyObject *arg)
458 PyObject *l = 0, *i = 0, *k, *v, *rc = 0;
461 if (!PyArg_ParseTuple(arg, ":values") ||
462 (l = PyList_New(0)) == 0 ||
463 (i = PyObject_GetIter(me)) == 0)
465 while ((k = PyIter_Next(i)) != 0) {
466 if ((v = PyObject_GetItem(me, k)) == 0 ||
469 Py_DECREF(k); Py_XDECREF(v);
472 if (PyErr_Occurred()) goto done;
475 Py_XDECREF(l); Py_XDECREF(i);
479 PyObject *gmapmeth_items(PyObject *me, PyObject *arg)
481 PyObject *l = 0, *i = 0, *k, *v, *z, *rc = 0;
484 if (!PyArg_ParseTuple(arg, ":items") ||
485 (l = PyList_New(0)) == 0 ||
486 (i = PyObject_GetIter(me)) == 0)
488 while ((k = PyIter_Next(i)) != 0) {
490 if ((v = PyObject_GetItem(me, k)) == 0 ||
491 (z = Py_BuildValue("(OO)", k, v)) == 0 ||
494 Py_DECREF(k); Py_XDECREF(v); Py_XDECREF(z);
497 if (PyErr_Occurred()) goto done;
500 Py_XDECREF(l); Py_XDECREF(i);
504 PyObject *gmapmeth_iterkeys(PyObject *me, PyObject *arg)
506 if (!PyArg_ParseTuple(arg, ":iterkeys")) return (0);
507 return (PyObject_GetIter(me));
510 PyObject *gmapmeth_itervalues(PyObject *me, PyObject *arg)
515 if (!PyArg_ParseTuple(arg, ":itervalues") ||
516 (i = PyObject_GetIter(me)) == 0)
518 ii = PyObject_NEW(iter_pyobj, valiter_pytype);
519 ii->map = me; Py_INCREF(me);
521 return ((PyObject *)ii);
524 PyObject *gmapmeth_iteritems(PyObject *me, PyObject *arg)
529 if (!PyArg_ParseTuple(arg, ":iteritems") ||
530 (i = PyObject_GetIter(me)) == 0)
532 ii = PyObject_NEW(iter_pyobj, itemiter_pytype);
533 ii->map = me; Py_INCREF(me);
535 return ((PyObject *)ii);
538 PyObject *gmapmeth_clear(PyObject *me, PyObject *arg)
540 PyObject *i = 0, *k = 0, *rc = 0;
542 if (!PyArg_ParseTuple(arg, ":clear") ||
543 (i = PyObject_GetIter(me)) == 0)
545 while ((k = PyIter_Next(i)) != 0) {
546 PyObject_DelItem(me, k);
549 if (PyErr_Occurred()) goto end;
550 rc = me; Py_INCREF(me);
556 static char *def_kwlist[] = { "key", "default", 0 };
558 PyObject *gmapmeth_get(PyObject *me, PyObject *arg, PyObject *kw)
560 PyObject *k, *def = Py_None, *v;
562 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:get", def_kwlist, &k, &def))
564 if ((v = PyObject_GetItem(me, k)) != 0) return (v);
569 PyObject *gmapmeth_setdefault(PyObject *me, PyObject *arg, PyObject *kw)
571 PyObject *k, *def = Py_None, *v;
573 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:setdefault",
574 def_kwlist, &k, &def))
576 if ((v = PyObject_GetItem(me, k)) != 0) return (v);
578 if (PyObject_SetItem(me, k, def)) return (0);
582 PyObject *gmapmeth_pop(PyObject *me, PyObject *arg, PyObject *kw)
584 PyObject *k, *def = 0, *v;
586 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:pop", def_kwlist, &k, &def))
588 if ((v = PyObject_GetItem(me, k)) != 0) {
589 PyObject_DelItem(me, k);
596 PyObject *gmapmeth_update(PyObject *me, PyObject *arg)
598 PyObject *map, *i = 0, *k, *v, *rc = 0;
601 if (!PyArg_ParseTuple(arg, "O:update", &map) ||
602 (i = PyObject_GetIter(map)) == 0)
604 while ((k = PyIter_Next(i)) != 0) {
605 if ((v = PyObject_GetItem(map, k)) == 0 ||
606 PyObject_SetItem(me, k, v))
608 Py_DECREF(k); Py_XDECREF(v);
611 if (PyErr_Occurred()) goto end;
612 rc = me; Py_INCREF(me);
618 PyObject *gmapmeth_popitem(PyObject *me, PyObject *arg)
620 PyObject *i = 0, *k = 0, *v = 0, *rc = 0;
622 if (!PyArg_ParseTuple(arg, ":popitem") ||
623 (i = PyObject_GetIter(me)))
625 if ((k = PyIter_Next(i)) == 0) {
626 if (!PyErr_Occurred()) VALERR("popitem(): mapping is empty");
629 if ((v = PyObject_GetItem(me, k)) == 0 ||
630 PyObject_DelItem(me, k))
632 rc = Py_BuildValue("(OO)", k, v);
634 Py_XDECREF(i); Py_XDECREF(k); Py_XDECREF(v);
638 PyMethodDef gmap_pymethods[] = {
643 /*----- Initialization ----------------------------------------------------*/
645 void util_pyinit(void)
647 modname = PyString_FromString("catacomb");
648 INITTYPE(itemiter, root);
649 INITTYPE(valiter, root);
652 void util_pyinsert(PyObject *mod)
654 INSERT("ItemIter", itemiter_pytype);
655 INSERT("ValueIter", valiter_pytype);
658 /*----- That's all, folks -------------------------------------------------*/