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