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 keys 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 values 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;
507 Py_ssize_t rc
= -1, n
= 0;
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
;
514 Py_XDECREF(i
); Py_XDECREF(x
);
518 PyObject
*gmapmeth_has_key(PyObject
*me
, PyObject
*arg
)
521 if (!PyArg_ParseTuple(arg
, "O:has_key", &k
)) return (0);
522 return (getbool(PyMapping_HasKey(me
, k
)));
525 PyObject
*gmapmeth_keys(PyObject
*me
, PyObject
*arg
)
527 PyObject
*l
= 0, *i
= 0, *k
, *rc
= 0;
530 if (!PyArg_ParseTuple(arg
, ":keys") ||
531 (l
= PyList_New(0)) == 0 ||
532 (i
= PyObject_GetIter(me
)) == 0)
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
;
539 Py_XDECREF(l
); Py_XDECREF(i
);
543 PyObject
*gmapmeth_values(PyObject
*me
, PyObject
*arg
)
545 PyObject
*l
= 0, *i
= 0, *k
, *v
, *rc
= 0;
548 if (!PyArg_ParseTuple(arg
, ":values") ||
549 (l
= PyList_New(0)) == 0 ||
550 (i
= PyObject_GetIter(me
)) == 0)
552 while ((k
= PyIter_Next(i
)) != 0) {
553 if ((v
= PyObject_GetItem(me
, k
)) == 0 ||
556 Py_DECREF(k
); Py_XDECREF(v
);
559 if (PyErr_Occurred()) goto done
;
562 Py_XDECREF(l
); Py_XDECREF(i
);
566 PyObject
*gmapmeth_items(PyObject
*me
, PyObject
*arg
)
568 PyObject
*l
= 0, *i
= 0, *k
, *v
, *z
, *rc
= 0;
571 if (!PyArg_ParseTuple(arg
, ":items") ||
572 (l
= PyList_New(0)) == 0 ||
573 (i
= PyObject_GetIter(me
)) == 0)
575 while ((k
= PyIter_Next(i
)) != 0) {
577 if ((v
= PyObject_GetItem(me
, k
)) == 0 ||
578 (z
= Py_BuildValue("(OO)", k
, v
)) == 0 ||
581 Py_DECREF(k
); Py_XDECREF(v
); Py_XDECREF(z
);
584 if (PyErr_Occurred()) goto done
;
587 Py_XDECREF(l
); Py_XDECREF(i
);
591 PyObject
*gmapmeth_iterkeys(PyObject
*me
, PyObject
*arg
)
593 if (!PyArg_ParseTuple(arg
, ":iterkeys")) return (0);
594 return (PyObject_GetIter(me
));
597 PyObject
*gmapmeth_itervalues(PyObject
*me
, PyObject
*arg
)
602 if (!PyArg_ParseTuple(arg
, ":itervalues") ||
603 (i
= PyObject_GetIter(me
)) == 0)
605 ii
= PyObject_NEW(iter_pyobj
, valiter_pytype
);
606 ii
->map
= me
; Py_INCREF(me
);
608 return ((PyObject
*)ii
);
611 PyObject
*gmapmeth_iteritems(PyObject
*me
, PyObject
*arg
)
616 if (!PyArg_ParseTuple(arg
, ":iteritems") ||
617 (i
= PyObject_GetIter(me
)) == 0)
619 ii
= PyObject_NEW(iter_pyobj
, itemiter_pytype
);
620 ii
->map
= me
; Py_INCREF(me
);
622 return ((PyObject
*)ii
);
625 PyObject
*gmapmeth_clear(PyObject
*me
, PyObject
*arg
)
627 PyObject
*i
= 0, *k
= 0, *rc
= 0;
629 if (!PyArg_ParseTuple(arg
, ":clear") ||
630 (i
= PyObject_GetIter(me
)) == 0)
632 while ((k
= PyIter_Next(i
)) != 0) {
633 PyObject_DelItem(me
, k
);
636 if (PyErr_Occurred()) goto end
;
637 rc
= me
; Py_INCREF(me
);
643 static char *def_kwlist
[] = { "key", "default", 0 };
645 PyObject
*gmapmeth_get(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
647 PyObject
*k
, *def
= Py_None
, *v
;
649 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO:get", def_kwlist
, &k
, &def
))
651 if ((v
= PyObject_GetItem(me
, k
)) != 0) return (v
);
656 PyObject
*gmapmeth_setdefault(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
658 PyObject
*k
, *def
= Py_None
, *v
;
660 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO:setdefault",
661 def_kwlist
, &k
, &def
))
663 if ((v
= PyObject_GetItem(me
, k
)) != 0) return (v
);
665 if (PyObject_SetItem(me
, k
, def
)) return (0);
669 PyObject
*gmapmeth_pop(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
671 PyObject
*k
, *def
= 0, *v
;
673 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO:pop", def_kwlist
, &k
, &def
))
675 if ((v
= PyObject_GetItem(me
, k
)) != 0) {
676 PyObject_DelItem(me
, k
);
683 PyObject
*gmapmeth_update(PyObject
*me
, PyObject
*arg
)
685 PyObject
*map
, *i
= 0, *k
, *v
, *rc
= 0;
688 if (!PyArg_ParseTuple(arg
, "O:update", &map
) ||
689 (i
= PyObject_GetIter(map
)) == 0)
691 while ((k
= PyIter_Next(i
)) != 0) {
692 if ((v
= PyObject_GetItem(map
, k
)) == 0 ||
693 PyObject_SetItem(me
, k
, v
))
695 Py_DECREF(k
); Py_XDECREF(v
);
698 if (PyErr_Occurred()) goto end
;
699 rc
= me
; Py_INCREF(me
);
705 PyObject
*gmapmeth_popitem(PyObject
*me
, PyObject
*arg
)
707 PyObject
*i
= 0, *k
= 0, *v
= 0, *rc
= 0;
709 if (!PyArg_ParseTuple(arg
, ":popitem") ||
710 (i
= PyObject_GetIter(me
)))
712 if ((k
= PyIter_Next(i
)) == 0) {
713 if (!PyErr_Occurred()) VALERR("popitem(): mapping is empty");
716 if ((v
= PyObject_GetItem(me
, k
)) == 0 ||
717 PyObject_DelItem(me
, k
))
719 rc
= Py_BuildValue("(OO)", k
, v
);
721 Py_XDECREF(i
); Py_XDECREF(k
); Py_XDECREF(v
);
725 PyMethodDef gmap_pymethods
[] = {
730 /*----- Initialization ----------------------------------------------------*/
732 void util_pyinit(void)
734 modname
= PyString_FromString("catacomb");
735 INITTYPE(itemiter
, root
);
736 INITTYPE(valiter
, root
);
739 void util_pyinsert(PyObject
*mod
)
741 INSERT("ItemIter", itemiter_pytype
);
742 INSERT("ValueIter", valiter_pytype
);
745 /*----- That's all, folks -------------------------------------------------*/