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