General: Fix lots of whitespace issues.
[pyke] / util.c
1 /* -*-c-*-
2 *
3 * $Id$
4 *
5 * Miscellaneous utilities (not Catacomb-specific)
6 *
7 * (c) 2005 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the Python interface to Catacomb.
13 *
14 * Catacomb/Python is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * Catacomb/Python is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Catacomb/Python; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 /*----- Header files ------------------------------------------------------*/
30
31 #include "catacomb-python.h"
32
33 /*----- Conversions -------------------------------------------------------*/
34
35 PyObject *getulong(unsigned long w)
36 {
37 if (w <= LONG_MAX)
38 return (PyInt_FromLong(w));
39 else
40 return (PyLong_FromUnsignedLong(w));
41 }
42
43 PyObject *getbool(int b)
44 {
45 if (b) RETURN_TRUE;
46 else RETURN_FALSE;
47 }
48
49 PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
50 {
51 PyErr_SetString(PyExc_TypeError, "can't instantiate this class");
52 return (0);
53 }
54
55 int convulong(PyObject *o, void *pp)
56 {
57 long i;
58 unsigned long *p = pp;
59 PyObject *t;
60
61 if (PyInt_Check(o)) {
62 i = PyInt_AS_LONG(o);
63 if (i < 0) VALERR("must be nonnegative");
64 *p = i;
65 } else {
66 if ((t = PyNumber_Long(o)) == 0) goto end;
67 *p = PyLong_AsUnsignedLong(t);
68 Py_DECREF(t);
69 if (PyErr_Occurred()) goto end;
70 }
71 return (1);
72 end:
73 return (0);
74 }
75
76 #define CONVU_(n) \
77 int convu##n(PyObject *o, void *pp) \
78 { \
79 unsigned long u; \
80 uint##n *p = pp; \
81 \
82 if (!convulong(o, &u)) goto end; \
83 if (u > MASK##n) VALERR("out of range"); \
84 *p = u; \
85 return (1); \
86 end: \
87 return (0); \
88 }
89 DOUINTSZ(CONVU_)
90
91 int convuint(PyObject *o, void *pp)
92 {
93 unsigned long u;
94 unsigned *p = pp;
95
96 if (!convulong(o, &u)) goto end;
97 if (u > UINT_MAX) VALERR("out of range");
98 *p = u;
99 return (1);
100 end:
101 return (0);
102 }
103
104 int convmpw(PyObject *o, void *pp)
105 {
106 unsigned long u;
107 unsigned *p = pp;
108
109 if (!convulong(o, &u)) goto end;
110 if (u > MPW_MAX) VALERR("out of range");
111 *p = u;
112 return (1);
113 end:
114 return (0);
115 }
116
117 int convszt(PyObject *o, void *pp)
118 {
119 unsigned long u;
120 size_t *p = pp;
121
122 if (!convulong(o, &u)) goto end;
123 if (u > ~(size_t)0) VALERR("out of range");
124 *p = u;
125 return (1);
126 end:
127 return (0);
128 }
129
130 int convbool(PyObject *o, void *pp)
131 {
132 *(int *)pp = PyObject_IsTrue(o);
133 return (1);
134 }
135
136 /*----- Type messing ------------------------------------------------------*/
137
138 static const PyTypeObject emptytype = { 0 };
139
140 void *newtype(PyTypeObject *metaty,
141 const PyTypeObject *skel,
142 const char *name)
143 {
144 PyHeapTypeObject *ty =
145 (PyHeapTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0));
146 if (!skel) skel = &emptytype;
147 memcpy(ty, skel, sizeof(*skel));
148 if (ty->ht_type.tp_base) Py_INCREF(ty->ht_type.tp_base);
149 #define COPY(blah) do { \
150 if (ty->ht_type.tp_as_##blah) { \
151 memcpy(&ty->as_##blah, \
152 ty->ht_type.tp_as_##blah, \
153 sizeof(ty->as_##blah)); \
154 ty->ht_type.tp_as_##blah = &ty->as_##blah; \
155 } \
156 } while (0)
157 COPY(number);
158 COPY(sequence);
159 COPY(mapping);
160 COPY(buffer);
161 #undef COPY
162 if (name)
163 ty->ht_name = PyString_FromString(name);
164 else if (ty->ht_type.tp_name)
165 ty->ht_name = PyString_FromString(ty->ht_type.tp_name);
166 if (ty->ht_name)
167 ty->ht_type.tp_name = PyString_AS_STRING(ty->ht_name);
168 PyObject_INIT(&ty->ht_type, metaty);
169 Py_INCREF(metaty);
170 return (ty);
171 }
172
173 PyTypeObject *inittype(PyTypeObject *tyskel)
174 {
175 PyTypeObject *ty = newtype(&PyType_Type, tyskel, 0);
176 ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
177 PyType_Ready(ty);
178 return (ty);
179 }
180
181 /*----- Constants ---------------------------------------------------------*/
182
183 void setconstants(PyObject *mod, const struct nameval *c)
184 {
185 PyObject *x;
186
187 while (c->name) {
188 if (c->value > LONG_MAX)
189 x = PyLong_FromUnsignedLong(c->value);
190 else
191 x = PyInt_FromLong(c->value);
192 PyModule_AddObject(mod, (/*unconst*/ char *)c->name, x);
193 c++;
194 }
195 }
196
197 /*----- Building method tables --------------------------------------------*/
198
199 DA_DECL(method_v, PyMethodDef);
200 static method_v global_pymethods = DA_INIT;
201 void addmethods(const PyMethodDef *m)
202 {
203 size_t n;
204
205 for (n = 0; m[n].ml_name; n++);
206 DA_ENSURE(&global_pymethods, n);
207 memcpy(DA(&global_pymethods) + DA_LEN(&global_pymethods),
208 m, n * sizeof(*m));
209 DA_EXTEND(&global_pymethods, n);
210 }
211
212 PyMethodDef *donemethods(void)
213 {
214 static const PyMethodDef mzero = { 0 };
215 DA_PUSH(&global_pymethods, mzero);
216 return (DA(&global_pymethods));
217 }
218
219 /*----- Exceptions --------------------------------------------------------*/
220
221 PyObject * mkexc(PyObject *mod, PyObject *base,
222 const char *name, PyMethodDef *mm)
223 {
224 PyObject *nameobj = 0;
225 PyObject *dict = 0;
226 PyObject *exc = 0;
227 PyObject *func = 0;
228 PyObject *meth = 0;
229
230 if ((nameobj = PyString_FromFormat("%s.%s",
231 PyModule_GetName(mod),
232 name)) == 0 ||
233 (dict = PyDict_New()) == 0 ||
234 (exc = PyErr_NewException(PyString_AS_STRING(nameobj),
235 base, dict)) == 0)
236 goto fail;
237
238 if (mm) {
239 while (mm->ml_name) {
240 if ((func = PyCFunction_NewEx(mm, 0, mod)) == 0 ||
241 (meth = PyMethod_New(func, 0, exc)) == 0 ||
242 PyDict_SetItemString(dict, mm->ml_name, meth))
243 goto fail;
244 Py_DECREF(func); func = 0;
245 Py_DECREF(meth); meth = 0;
246 mm++;
247 }
248 }
249
250 done:
251 Py_XDECREF(nameobj);
252 Py_XDECREF(dict);
253 return (exc);
254
255 fail:
256 Py_XDECREF(exc);
257 Py_XDECREF(func);
258 Py_XDECREF(meth);
259 exc = 0;
260 goto done;
261 }
262
263 /*----- Generic dictionary methods ----------------------------------------*/
264
265 static PyTypeObject *itemiter_pytype, *valiter_pytype;
266
267 typedef struct iter_pyobj {
268 PyObject_HEAD
269 PyObject *map;
270 PyObject *i;
271 } iter_pyobj;
272 #define ITER_MAP(o) (((iter_pyobj *)(o))->map)
273 #define ITER_I(o) (((iter_pyobj *)(o))->i)
274
275 static void iter_pydealloc(PyObject *me)
276 { Py_DECREF(ITER_MAP(me)); Py_DECREF(ITER_I(me)); FREEOBJ(me); }
277
278 static PyObject *itemiter_pynext(PyObject *me)
279 {
280 PyObject *k = 0, *v = 0, *rc = 0;
281
282 if ((k = PyIter_Next(ITER_I(me))) != 0 &&
283 (v = PyObject_GetItem(ITER_MAP(me), k)) != 0)
284 rc = Py_BuildValue("(OO)", k, v);
285 Py_XDECREF(k); Py_XDECREF(v);
286 return (rc);
287 }
288
289 static PyTypeObject itemiter_pytype_skel = {
290 PyObject_HEAD_INIT(0) 0, /* Header */
291 "ItemIter", /* @tp_name@ */
292 sizeof(iter_pyobj), /* @tp_basicsize@ */
293 0, /* @tp_itemsize@ */
294
295 iter_pydealloc, /* @tp_dealloc@ */
296 0, /* @tp_print@ */
297 0, /* @tp_getattr@ */
298 0, /* @tp_setattr@ */
299 0, /* @tp_compare@ */
300 0, /* @tp_repr@ */
301 0, /* @tp_as_number@ */
302 0, /* @tp_as_sequence@ */
303 0, /* @tp_as_mapping@ */
304 0, /* @tp_hash@ */
305 0, /* @tp_call@ */
306 0, /* @tp_str@ */
307 0, /* @tp_getattro@ */
308 0, /* @tp_setattro@ */
309 0, /* @tp_as_buffer@ */
310 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
311 Py_TPFLAGS_BASETYPE,
312
313 /* @tp_doc@ */
314 "Iterates over the items of a mapping.",
315
316 0, /* @tp_traverse@ */
317 0, /* @tp_clear@ */
318 0, /* @tp_richcompare@ */
319 0, /* @tp_weaklistoffset@ */
320 PyObject_SelfIter, /* @tp_iter@ */
321 itemiter_pynext, /* @tp_iternext@ */
322 0, /* @tp_methods@ */
323 0, /* @tp_members@ */
324 0, /* @tp_getset@ */
325 0, /* @tp_base@ */
326 0, /* @tp_dict@ */
327 0, /* @tp_descr_get@ */
328 0, /* @tp_descr_set@ */
329 0, /* @tp_dictoffset@ */
330 0, /* @tp_init@ */
331 PyType_GenericAlloc, /* @tp_alloc@ */
332 abstract_pynew, /* @tp_new@ */
333 0, /* @tp_free@ */
334 0 /* @tp_is_gc@ */
335 };
336
337 static PyObject *valiter_pynext(PyObject *me)
338 {
339 PyObject *k = 0, *rc = 0;
340
341 if ((k = PyIter_Next(ITER_I(me))) != 0)
342 rc = PyObject_GetItem(ITER_MAP(me), k);
343 Py_XDECREF(k);
344 return (rc);
345 }
346
347 static PyTypeObject valiter_pytype_skel = {
348 PyObject_HEAD_INIT(0) 0, /* Header */
349 "ValueIter", /* @tp_name@ */
350 sizeof(iter_pyobj), /* @tp_basicsize@ */
351 0, /* @tp_itemsize@ */
352
353 iter_pydealloc, /* @tp_dealloc@ */
354 0, /* @tp_print@ */
355 0, /* @tp_getattr@ */
356 0, /* @tp_setattr@ */
357 0, /* @tp_compare@ */
358 0, /* @tp_repr@ */
359 0, /* @tp_as_number@ */
360 0, /* @tp_as_sequence@ */
361 0, /* @tp_as_mapping@ */
362 0, /* @tp_hash@ */
363 0, /* @tp_call@ */
364 0, /* @tp_str@ */
365 0, /* @tp_getattro@ */
366 0, /* @tp_setattro@ */
367 0, /* @tp_as_buffer@ */
368 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
369 Py_TPFLAGS_BASETYPE,
370
371 /* @tp_doc@ */
372 "Iterates over the items of a mapping.",
373
374 0, /* @tp_traverse@ */
375 0, /* @tp_clear@ */
376 0, /* @tp_richcompare@ */
377 0, /* @tp_weaklistoffset@ */
378 PyObject_SelfIter, /* @tp_iter@ */
379 valiter_pynext, /* @tp_iternext@ */
380 0, /* @tp_methods@ */
381 0, /* @tp_members@ */
382 0, /* @tp_getset@ */
383 0, /* @tp_base@ */
384 0, /* @tp_dict@ */
385 0, /* @tp_descr_get@ */
386 0, /* @tp_descr_set@ */
387 0, /* @tp_dictoffset@ */
388 0, /* @tp_init@ */
389 PyType_GenericAlloc, /* @tp_alloc@ */
390 abstract_pynew, /* @tp_new@ */
391 0, /* @tp_free@ */
392 0 /* @tp_is_gc@ */
393 };
394
395 PySequenceMethods gmap_pysequence = {
396 0, /* @sq_length@ */
397 0, /* @sq_concat@ */
398 0, /* @sq_repeat@ */
399 0, /* @sq_item@ */
400 0, /* @sq_slice@ */
401 0, /* @sq_ass_item@ */
402 0, /* @sq_ass_slice@ */
403 PyMapping_HasKey, /* @sq_contains@ */
404 0, /* @sq_inplace_concat@ */
405 0 /* @sq_inplace_repeat@ */
406 };
407
408 int gmap_pysize(PyObject *me)
409 {
410 PyObject *i = 0, *x = 0;
411 int rc = -1;
412 int n = 0;
413
414 if ((i = PyObject_GetIter(me)) == 0) goto done;
415 while ((x = PyIter_Next(i)) != 0) { n++; Py_DECREF(x); x = 0; }
416 if (PyErr_Occurred()) goto done;
417 rc = n;
418 done:
419 Py_XDECREF(i); Py_XDECREF(x);
420 return (rc);
421 }
422
423 PyObject *gmapmeth_has_key(PyObject *me, PyObject *arg)
424 {
425 PyObject *k;
426 if (!PyArg_ParseTuple(arg, "O:has_key", &k)) return (0);
427 return (getbool(PyMapping_HasKey(me, k)));
428 }
429
430 PyObject *gmapmeth_keys(PyObject *me, PyObject *arg)
431 {
432 PyObject *l = 0, *i = 0, *k, *rc = 0;
433 int err;
434
435 if (!PyArg_ParseTuple(arg, ":keys") ||
436 (l = PyList_New(0)) == 0 ||
437 (i = PyObject_GetIter(me)) == 0)
438 goto done;
439 while ((k = PyIter_Next(i)) != 0)
440 { err = PyList_Append(l, k); Py_DECREF(k); if (err) goto done; }
441 if (PyErr_Occurred()) goto done;
442 rc = l; l = 0;
443 done:
444 Py_XDECREF(l); Py_XDECREF(i);
445 return (rc);
446 }
447
448 PyObject *gmapmeth_values(PyObject *me, PyObject *arg)
449 {
450 PyObject *l = 0, *i = 0, *k, *v, *rc = 0;
451 int err = 0;
452
453 if (!PyArg_ParseTuple(arg, ":values") ||
454 (l = PyList_New(0)) == 0 ||
455 (i = PyObject_GetIter(me)) == 0)
456 goto done;
457 while ((k = PyIter_Next(i)) != 0) {
458 if ((v = PyObject_GetItem(me, k)) == 0 ||
459 PyList_Append(l, v))
460 err = -1;
461 Py_DECREF(k); Py_XDECREF(v);
462 if (err) goto done;
463 }
464 if (PyErr_Occurred()) goto done;
465 rc = l; l = 0;
466 done:
467 Py_XDECREF(l); Py_XDECREF(i);
468 return (rc);
469 }
470
471 PyObject *gmapmeth_items(PyObject *me, PyObject *arg)
472 {
473 PyObject *l = 0, *i = 0, *k, *v, *z, *rc = 0;
474 int err = 0;
475
476 if (!PyArg_ParseTuple(arg, ":items") ||
477 (l = PyList_New(0)) == 0 ||
478 (i = PyObject_GetIter(me)) == 0)
479 goto done;
480 while ((k = PyIter_Next(i)) != 0) {
481 z = 0;
482 if ((v = PyObject_GetItem(me, k)) == 0 ||
483 (z = Py_BuildValue("(OO)", k, v)) == 0 ||
484 PyList_Append(l, z))
485 err = -1;
486 Py_DECREF(k); Py_XDECREF(v); Py_XDECREF(z);
487 if (err) goto done;
488 }
489 if (PyErr_Occurred()) goto done;
490 rc = l; l = 0;
491 done:
492 Py_XDECREF(l); Py_XDECREF(i);
493 return (rc);
494 }
495
496 PyObject *gmapmeth_iterkeys(PyObject *me, PyObject *arg)
497 {
498 if (!PyArg_ParseTuple(arg, ":iterkeys")) return (0);
499 return (PyObject_GetIter(me));
500 }
501
502 PyObject *gmapmeth_itervalues(PyObject *me, PyObject *arg)
503 {
504 PyObject *i;
505 iter_pyobj *ii;
506
507 if (!PyArg_ParseTuple(arg, ":itervalues") ||
508 (i = PyObject_GetIter(me)) == 0)
509 return (0);
510 ii = PyObject_NEW(iter_pyobj, valiter_pytype);
511 ii->map = me; Py_INCREF(me);
512 ii->i = i;
513 return ((PyObject *)ii);
514 }
515
516 PyObject *gmapmeth_iteritems(PyObject *me, PyObject *arg)
517 {
518 PyObject *i;
519 iter_pyobj *ii;
520
521 if (!PyArg_ParseTuple(arg, ":iteritems") ||
522 (i = PyObject_GetIter(me)) == 0)
523 return (0);
524 ii = PyObject_NEW(iter_pyobj, itemiter_pytype);
525 ii->map = me; Py_INCREF(me);
526 ii->i = i;
527 return ((PyObject *)ii);
528 }
529
530 PyObject *gmapmeth_clear(PyObject *me, PyObject *arg)
531 {
532 PyObject *i = 0, *k = 0, *rc = 0;
533
534 if (!PyArg_ParseTuple(arg, ":clear") ||
535 (i = PyObject_GetIter(me)) == 0)
536 goto end;
537 while ((k = PyIter_Next(i)) != 0) {
538 PyObject_DelItem(me, k);
539 Py_DECREF(k);
540 }
541 if (PyErr_Occurred()) goto end;
542 rc = me; Py_INCREF(me);
543 end:
544 Py_XDECREF(i);
545 return (rc);
546 }
547
548 static char *def_kwlist[] = { "key", "default", 0 };
549
550 PyObject *gmapmeth_get(PyObject *me, PyObject *arg, PyObject *kw)
551 {
552 PyObject *k, *def = Py_None, *v;
553
554 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:get", def_kwlist, &k, &def))
555 return (0);
556 if ((v = PyObject_GetItem(me, k)) != 0) return (v);
557 PyErr_Clear();
558 RETURN_OBJ(def);
559 }
560
561 PyObject *gmapmeth_setdefault(PyObject *me, PyObject *arg, PyObject *kw)
562 {
563 PyObject *k, *def = Py_None, *v;
564
565 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:setdefault",
566 def_kwlist, &k, &def))
567 return (0);
568 if ((v = PyObject_GetItem(me, k)) != 0) return (v);
569 PyErr_Clear();
570 if (PyObject_SetItem(me, k, def)) return (0);
571 RETURN_OBJ(def);
572 }
573
574 PyObject *gmapmeth_pop(PyObject *me, PyObject *arg, PyObject *kw)
575 {
576 PyObject *k, *def = 0, *v;
577
578 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:pop", def_kwlist, &k, &def))
579 return (0);
580 if ((v = PyObject_GetItem(me, k)) != 0) {
581 PyObject_DelItem(me, k);
582 return (v);
583 }
584 PyErr_Clear();
585 RETURN_OBJ(def);
586 }
587
588 PyObject *gmapmeth_update(PyObject *me, PyObject *arg)
589 {
590 PyObject *map, *i = 0, *k, *v, *rc = 0;
591 int err = 0;
592
593 if (!PyArg_ParseTuple(arg, "O:update", &map) ||
594 (i = PyObject_GetIter(map)) == 0)
595 goto end;
596 while ((k = PyIter_Next(i)) != 0) {
597 if ((v = PyObject_GetItem(map, k)) == 0 ||
598 PyObject_SetItem(me, k, v))
599 err = -1;
600 Py_DECREF(k); Py_XDECREF(v);
601 if (err) goto end;
602 }
603 if (PyErr_Occurred()) goto end;
604 rc = me; Py_INCREF(me);
605 end:
606 Py_XDECREF(i);
607 return (rc);
608 }
609
610 PyObject *gmapmeth_popitem(PyObject *me, PyObject *arg)
611 {
612 PyObject *i = 0, *k = 0, *v = 0, *rc = 0;
613
614 if (!PyArg_ParseTuple(arg, ":popitem") ||
615 (i = PyObject_GetIter(me)))
616 goto end;
617 if ((k = PyIter_Next(i)) == 0) {
618 if (!PyErr_Occurred()) VALERR("popitem(): mapping is empty");
619 goto end;
620 }
621 if ((v = PyObject_GetItem(me, k)) == 0 ||
622 PyObject_DelItem(me, k))
623 goto end;
624 rc = Py_BuildValue("(OO)", k, v);
625 end:
626 Py_XDECREF(i); Py_XDECREF(k); Py_XDECREF(v);
627 return (rc);
628 }
629
630 PyMethodDef gmap_pymethods[] = {
631 GMAP_METHODS
632 { 0 }
633 };
634
635 /*----- Initialization ----------------------------------------------------*/
636
637 void util_init(void)
638 {
639 INITTYPE(itemiter, root);
640 INITTYPE(valiter, root);
641 }
642
643 void util_insert(PyObject *mod)
644 {
645 INSERT("ItemIter", itemiter_pytype);
646 INSERT("ValueIter", valiter_pytype);
647 }
648
649 /*----- That's all, folks -------------------------------------------------*/