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