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; | |
930d78e3 | 36 | PyObject *home_module = 0; |
dc075750 | 37 | |
11cb3d97 MW |
38 | /*----- Conversions -------------------------------------------------------*/ |
39 | ||
fbca05a1 MW |
40 | PyObject *getulong(unsigned long w) |
41 | { | |
2308db6f | 42 | if (w <= LONG_MAX) |
fbca05a1 MW |
43 | return (PyInt_FromLong(w)); |
44 | else | |
45 | return (PyLong_FromUnsignedLong(w)); | |
46 | } | |
11cb3d97 | 47 | |
d12daa59 | 48 | #ifndef HAVE_LONG_LONG |
9f9ea9ea MW |
49 | static PyObject *i32 = 0; |
50 | static int init_i32(void) | |
51 | { if (!i32 && (i32 = PyInt_FromLong(32)) == 0) return (-1); return (0); } | |
d12daa59 | 52 | #endif |
9f9ea9ea MW |
53 | |
54 | PyObject *getk64(kludge64 u) | |
55 | { | |
d12daa59 MW |
56 | #ifdef HAVE_LONG_LONG |
57 | return (PyLong_FromUnsignedLongLong(GET64(unsigned PY_LONG_LONG, u))); | |
58 | #else | |
9f9ea9ea MW |
59 | PyObject *i = 0, *j = 0, *t; |
60 | PyObject *rc = 0; | |
61 | ||
62 | if (init_i32()) goto end; | |
63 | if ((i = PyLong_FromUnsignedLong(HI64(u))) == 0) goto end; | |
64 | if ((t = PyNumber_InPlaceLshift(i, i32)) == 0) goto end; | |
65 | Py_DECREF(i); i = t; | |
66 | if ((j = PyLong_FromUnsignedLong(LO64(u))) == 0) goto end; | |
67 | if ((t = PyNumber_InPlaceOr(i, j)) == 0) goto end; | |
68 | Py_DECREF(i); i = t; | |
69 | if ((rc = PyNumber_Int(i)) == 0) goto end; | |
70 | end: | |
71 | if (i) Py_DECREF(i); | |
72 | if (j) Py_DECREF(j); | |
73 | return (rc); | |
d12daa59 | 74 | #endif |
9f9ea9ea MW |
75 | } |
76 | ||
11cb3d97 MW |
77 | PyObject *getbool(int b) |
78 | { | |
79 | if (b) RETURN_TRUE; | |
80 | else RETURN_FALSE; | |
81 | } | |
82 | ||
83 | PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
84 | { | |
85 | PyErr_SetString(PyExc_TypeError, "can't instantiate this class"); | |
86 | return (0); | |
87 | } | |
88 | ||
89 | int convulong(PyObject *o, void *pp) | |
90 | { | |
91 | long i; | |
92 | unsigned long *p = pp; | |
93 | PyObject *t; | |
94 | ||
8c45178c | 95 | if (!o) VALERR("can't delete"); |
11cb3d97 MW |
96 | if (PyInt_Check(o)) { |
97 | i = PyInt_AS_LONG(o); | |
79144037 | 98 | if (i < 0) VALERR("must be nonnegative"); |
11cb3d97 MW |
99 | *p = i; |
100 | } else { | |
101 | if ((t = PyNumber_Long(o)) == 0) goto end; | |
102 | *p = PyLong_AsUnsignedLong(t); | |
103 | Py_DECREF(t); | |
104 | if (PyErr_Occurred()) goto end; | |
105 | } | |
106 | return (1); | |
107 | end: | |
108 | return (0); | |
109 | } | |
110 | ||
4eb1c80e MW |
111 | #ifdef HAVE_UINT64 |
112 | # define CONVu64(n) do { \ | |
113 | kludge64 k; \ | |
114 | uint64 t; \ | |
115 | if (!convk64(o, &k)) goto end; \ | |
116 | t = GET64(uint64, k); \ | |
117 | if (t > MASK##n) VALERR("out of range"); \ | |
118 | *p = t; \ | |
119 | } while (0) | |
120 | #else | |
121 | # define CONVu64(n) assert(!"shouldn't be possible") | |
122 | #endif | |
123 | ||
11cb3d97 MW |
124 | #define CONVU_(n) \ |
125 | int convu##n(PyObject *o, void *pp) \ | |
126 | { \ | |
127 | unsigned long u; \ | |
128 | uint##n *p = pp; \ | |
129 | \ | |
4eb1c80e MW |
130 | if (MASK##n > ULONG_MAX) \ |
131 | CONVu64(n); \ | |
132 | else { \ | |
133 | if (!convulong(o, &u)) goto end; \ | |
134 | if (u > MASK##n) VALERR("out of range"); \ | |
135 | *p = u; \ | |
136 | } \ | |
11cb3d97 MW |
137 | return (1); \ |
138 | end: \ | |
139 | return (0); \ | |
140 | } | |
141 | DOUINTSZ(CONVU_) | |
142 | ||
143 | int convuint(PyObject *o, void *pp) | |
144 | { | |
145 | unsigned long u; | |
146 | unsigned *p = pp; | |
147 | ||
148 | if (!convulong(o, &u)) goto end; | |
79144037 | 149 | if (u > UINT_MAX) VALERR("out of range"); |
11cb3d97 MW |
150 | *p = u; |
151 | return (1); | |
152 | end: | |
153 | return (0); | |
154 | } | |
155 | ||
9f9ea9ea MW |
156 | int convk64(PyObject *o, void *pp) |
157 | { | |
d12daa59 | 158 | PyObject *i = 0; |
9f9ea9ea | 159 | int rc = 0; |
d12daa59 MW |
160 | #if HAVE_LONG_LONG |
161 | unsigned PY_LONG_LONG t; | |
162 | #else | |
163 | PyObject *t; | |
9f9ea9ea | 164 | uint32 lo, hi; |
d12daa59 | 165 | #endif |
9f9ea9ea | 166 | |
8f46aec4 | 167 | if (!o) VALERR("can't delete"); |
d12daa59 MW |
168 | #if HAVE_LONG_LONG |
169 | if ((i = PyNumber_Long(o)) == 0) goto end; | |
170 | t = PyLong_AsUnsignedLongLong(i); | |
171 | if (t == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) goto end; | |
172 | ASSIGN64(*(kludge64 *)pp, t); | |
173 | #else | |
9f9ea9ea MW |
174 | if (init_i32()) goto end; |
175 | if ((i = PyNumber_Int(o)) == 0) goto end; | |
176 | lo = PyInt_AsUnsignedLongMask(i); | |
177 | if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end; | |
178 | Py_DECREF(i); i = t; | |
179 | hi = PyInt_AsUnsignedLongMask(i); | |
180 | if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end; | |
181 | Py_DECREF(i); i = t; | |
182 | if (PyObject_IsTrue(i)) VALERR("out of range"); | |
183 | SET64(*(kludge64 *)pp, hi, lo); | |
d12daa59 | 184 | #endif |
9f9ea9ea | 185 | rc = 1; |
d12daa59 | 186 | |
9f9ea9ea MW |
187 | end: |
188 | if (i) Py_DECREF(i); | |
189 | return (rc); | |
190 | } | |
191 | ||
11cb3d97 MW |
192 | int convmpw(PyObject *o, void *pp) |
193 | { | |
194 | unsigned long u; | |
195 | unsigned *p = pp; | |
196 | ||
197 | if (!convulong(o, &u)) goto end; | |
79144037 | 198 | if (u > MPW_MAX) VALERR("out of range"); |
11cb3d97 MW |
199 | *p = u; |
200 | return (1); | |
201 | end: | |
202 | return (0); | |
203 | } | |
204 | ||
205 | int convszt(PyObject *o, void *pp) | |
206 | { | |
207 | unsigned long u; | |
208 | size_t *p = pp; | |
209 | ||
210 | if (!convulong(o, &u)) goto end; | |
79144037 | 211 | if (u > ~(size_t)0) VALERR("out of range"); |
11cb3d97 MW |
212 | *p = u; |
213 | return (1); | |
214 | end: | |
215 | return (0); | |
216 | } | |
217 | ||
218 | int convbool(PyObject *o, void *pp) | |
219 | { | |
8c45178c | 220 | if (!o) VALERR("can't delete"); |
11cb3d97 MW |
221 | *(int *)pp = PyObject_IsTrue(o); |
222 | return (1); | |
8c45178c MW |
223 | end: |
224 | return (0); | |
11cb3d97 MW |
225 | } |
226 | ||
227 | /*----- Type messing ------------------------------------------------------*/ | |
228 | ||
229 | static const PyTypeObject emptytype = { 0 }; | |
230 | ||
231 | void *newtype(PyTypeObject *metaty, | |
232 | const PyTypeObject *skel, | |
233 | const char *name) | |
234 | { | |
235 | PyHeapTypeObject *ty = | |
236 | (PyHeapTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0)); | |
237 | if (!skel) skel = &emptytype; | |
238 | memcpy(ty, skel, sizeof(*skel)); | |
24b3d57b | 239 | if (ty->ht_type.tp_base) Py_INCREF(ty->ht_type.tp_base); |
11cb3d97 | 240 | #define COPY(blah) do { \ |
24b3d57b | 241 | if (ty->ht_type.tp_as_##blah) { \ |
11cb3d97 | 242 | memcpy(&ty->as_##blah, \ |
24b3d57b | 243 | ty->ht_type.tp_as_##blah, \ |
11cb3d97 | 244 | sizeof(ty->as_##blah)); \ |
24b3d57b | 245 | ty->ht_type.tp_as_##blah = &ty->as_##blah; \ |
11cb3d97 MW |
246 | } \ |
247 | } while (0) | |
248 | COPY(number); | |
249 | COPY(sequence); | |
250 | COPY(mapping); | |
251 | COPY(buffer); | |
252 | #undef COPY | |
253 | if (name) | |
24b3d57b MW |
254 | ty->ht_name = PyString_FromString(name); |
255 | else if (ty->ht_type.tp_name) | |
256 | ty->ht_name = PyString_FromString(ty->ht_type.tp_name); | |
257 | if (ty->ht_name) | |
258 | ty->ht_type.tp_name = PyString_AS_STRING(ty->ht_name); | |
98963817 | 259 | DISCARD(PyObject_INIT(&ty->ht_type, metaty)); |
11cb3d97 MW |
260 | Py_INCREF(metaty); |
261 | return (ty); | |
262 | } | |
263 | ||
dc075750 MW |
264 | void typeready(PyTypeObject *ty) |
265 | { | |
266 | PyType_Ready(ty); | |
267 | PyDict_SetItemString(ty->tp_dict, "__module__", modname); | |
268 | } | |
269 | ||
76ca8edd | 270 | PyTypeObject *inittype(PyTypeObject *tyskel, PyTypeObject *meta) |
11cb3d97 | 271 | { |
76ca8edd | 272 | PyTypeObject *ty = newtype(meta, tyskel, 0); |
11cb3d97 | 273 | ty->tp_flags |= Py_TPFLAGS_HEAPTYPE; |
dc075750 | 274 | typeready(ty); |
11cb3d97 MW |
275 | return (ty); |
276 | } | |
277 | ||
278 | /*----- Constants ---------------------------------------------------------*/ | |
279 | ||
280 | void setconstants(PyObject *mod, const struct nameval *c) | |
281 | { | |
282 | PyObject *x; | |
9044246b | 283 | unsigned long u; |
11cb3d97 MW |
284 | |
285 | while (c->name) { | |
9044246b MW |
286 | u = c->value; |
287 | if (u <= LONG_MAX) x = PyInt_FromLong(u); | |
288 | else if (c->f&CF_SIGNED) x = PyInt_FromLong(-1 - (long)(ULONG_MAX - u)); | |
289 | else x = PyLong_FromUnsignedLong(u); | |
290 | PyModule_AddObject(mod, (/*unconst*/ char *)c->name, x); c++; | |
11cb3d97 MW |
291 | } |
292 | } | |
293 | ||
294 | /*----- Building method tables --------------------------------------------*/ | |
295 | ||
296 | DA_DECL(method_v, PyMethodDef); | |
297 | static method_v global_pymethods = DA_INIT; | |
298 | void addmethods(const PyMethodDef *m) | |
299 | { | |
300 | size_t n; | |
301 | ||
302 | for (n = 0; m[n].ml_name; n++); | |
303 | DA_ENSURE(&global_pymethods, n); | |
304 | memcpy(DA(&global_pymethods) + DA_LEN(&global_pymethods), | |
305 | m, n * sizeof(*m)); | |
306 | DA_EXTEND(&global_pymethods, n); | |
307 | } | |
308 | ||
309 | PyMethodDef *donemethods(void) | |
310 | { | |
311 | static const PyMethodDef mzero = { 0 }; | |
312 | DA_PUSH(&global_pymethods, mzero); | |
313 | return (DA(&global_pymethods)); | |
314 | } | |
315 | ||
316 | /*----- Exceptions --------------------------------------------------------*/ | |
317 | ||
634f3db3 MW |
318 | PyObject *mkexc(PyObject *mod, PyObject *base, |
319 | const char *name, PyMethodDef *mm) | |
11cb3d97 MW |
320 | { |
321 | PyObject *nameobj = 0; | |
322 | PyObject *dict = 0; | |
323 | PyObject *exc = 0; | |
324 | PyObject *func = 0; | |
325 | PyObject *meth = 0; | |
326 | ||
289517ed | 327 | if ((dict = PyDict_New()) == 0) goto fail; |
11cb3d97 MW |
328 | |
329 | if (mm) { | |
330 | while (mm->ml_name) { | |
331 | if ((func = PyCFunction_NewEx(mm, 0, mod)) == 0 || | |
332 | (meth = PyMethod_New(func, 0, exc)) == 0 || | |
333 | PyDict_SetItemString(dict, mm->ml_name, meth)) | |
334 | goto fail; | |
335 | Py_DECREF(func); func = 0; | |
336 | Py_DECREF(meth); meth = 0; | |
337 | mm++; | |
338 | } | |
339 | } | |
340 | ||
289517ed MW |
341 | if ((nameobj = PyString_FromFormat("%s.%s", |
342 | PyModule_GetName(mod), | |
343 | name)) == 0 || | |
344 | (exc = PyErr_NewException(PyString_AS_STRING(nameobj), | |
345 | base, dict)) == 0) | |
346 | goto fail; | |
347 | ||
11cb3d97 MW |
348 | done: |
349 | Py_XDECREF(nameobj); | |
350 | Py_XDECREF(dict); | |
351 | return (exc); | |
352 | ||
353 | fail: | |
354 | Py_XDECREF(exc); | |
355 | Py_XDECREF(func); | |
356 | Py_XDECREF(meth); | |
357 | exc = 0; | |
358 | goto done; | |
359 | } | |
360 | ||
930d78e3 MW |
361 | void report_lost_exception_v(struct excinfo *exc, |
362 | const char *why, va_list ap) | |
363 | { | |
364 | PyObject *hookfn = 0; | |
365 | PyObject *whyobj = 0; | |
366 | PyObject *obj = 0; | |
367 | ||
368 | /* Make sure we start out without a pending exception, or this will get | |
369 | * really confusing. | |
370 | */ | |
371 | assert(!PyErr_Occurred()); | |
372 | ||
373 | /* Format the explanation. */ | |
374 | if (why) whyobj = PyString_FromFormatV(why, ap); | |
375 | else { whyobj = Py_None; Py_INCREF(whyobj); } | |
376 | ||
377 | /* Find our home module's `lostexchook' function. This won't work if | |
378 | * there's no module, or the function isn't defined, or it's `None'. | |
379 | */ | |
380 | if (!home_module) goto sys; | |
381 | hookfn = PyObject_GetAttrString(home_module, "lostexchook"); | |
382 | if (hookfn == Py_None) goto sys; | |
383 | else if (hookfn) ; | |
384 | else if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto ouch; | |
385 | else { PyErr_Clear(); goto sys; } | |
386 | ||
387 | /* Call the hook function. */ | |
388 | obj = PyObject_CallFunction(hookfn, "(OOOO)", | |
389 | whyobj, exc->ty, exc->val, exc->tb); | |
390 | if (!obj) goto ouch; | |
391 | goto end; | |
392 | ||
393 | /* Something went wrong reporting the problem. */ | |
394 | ouch: | |
395 | PySys_WriteStderr("\n!!! FAILURE REPORTING LOST EXCEPTION\n"); | |
396 | PyErr_Print(); | |
397 | /* drop through... */ | |
398 | ||
399 | /* There was no hook, so try to do something sensible using | |
400 | * `sys.excepthook'. | |
401 | */ | |
402 | sys: | |
403 | PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n", | |
404 | PyString_AS_STRING(whyobj)); | |
405 | RESTORE_EXCINFO(exc); | |
406 | PyErr_Print(); | |
407 | /* drop through... */ | |
408 | ||
409 | /* Clean up afterwards. */ | |
410 | end: | |
411 | Py_XDECREF(hookfn); | |
412 | Py_XDECREF(whyobj); | |
413 | Py_XDECREF(obj); | |
414 | } | |
415 | ||
416 | void report_lost_exception(struct excinfo *exc, const char *why, ...) | |
417 | { | |
418 | va_list ap; | |
419 | ||
420 | va_start(ap, why); | |
421 | report_lost_exception_v(exc, why, ap); | |
422 | va_end(ap); | |
423 | } | |
424 | ||
425 | void stash_exception(struct excinfo *exc, const char *why, ...) | |
426 | { | |
427 | va_list ap; | |
428 | struct excinfo stash; | |
429 | ||
430 | if (!exc->ty) | |
431 | STASH_EXCINFO(exc); | |
432 | else { | |
433 | va_start(ap, why); | |
434 | STASH_EXCINFO(&stash); | |
435 | report_lost_exception_v(&stash, why, ap); | |
436 | va_end(ap); | |
437 | } | |
438 | } | |
439 | ||
440 | void restore_exception(struct excinfo *exc, const char *why, ...) | |
441 | { | |
442 | va_list ap; | |
443 | struct excinfo stash; | |
444 | ||
445 | if (!PyErr_Occurred()) | |
446 | RESTORE_EXCINFO(exc); | |
447 | else { | |
448 | va_start(ap, why); | |
449 | STASH_EXCINFO(&stash); | |
450 | report_lost_exception_v(exc, why, ap); | |
451 | RESTORE_EXCINFO(&stash); | |
452 | va_end(ap); | |
453 | } | |
454 | } | |
455 | ||
11cb3d97 MW |
456 | /*----- Generic dictionary methods ----------------------------------------*/ |
457 | ||
458 | static PyTypeObject *itemiter_pytype, *valiter_pytype; | |
459 | ||
460 | typedef struct iter_pyobj { | |
461 | PyObject_HEAD | |
462 | PyObject *map; | |
463 | PyObject *i; | |
464 | } iter_pyobj; | |
465 | #define ITER_MAP(o) (((iter_pyobj *)(o))->map) | |
466 | #define ITER_I(o) (((iter_pyobj *)(o))->i) | |
467 | ||
468 | static void iter_pydealloc(PyObject *me) | |
469 | { Py_DECREF(ITER_MAP(me)); Py_DECREF(ITER_I(me)); FREEOBJ(me); } | |
470 | ||
471 | static PyObject *itemiter_pynext(PyObject *me) | |
472 | { | |
473 | PyObject *k = 0, *v = 0, *rc = 0; | |
b2687a0a | 474 | |
11cb3d97 MW |
475 | if ((k = PyIter_Next(ITER_I(me))) != 0 && |
476 | (v = PyObject_GetItem(ITER_MAP(me), k)) != 0) | |
477 | rc = Py_BuildValue("(OO)", k, v); | |
478 | Py_XDECREF(k); Py_XDECREF(v); | |
479 | return (rc); | |
480 | } | |
481 | ||
482 | static PyTypeObject itemiter_pytype_skel = { | |
483 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
484 | "ItemIter", /* @tp_name@ */ | |
485 | sizeof(iter_pyobj), /* @tp_basicsize@ */ | |
486 | 0, /* @tp_itemsize@ */ | |
487 | ||
488 | iter_pydealloc, /* @tp_dealloc@ */ | |
489 | 0, /* @tp_print@ */ | |
490 | 0, /* @tp_getattr@ */ | |
491 | 0, /* @tp_setattr@ */ | |
492 | 0, /* @tp_compare@ */ | |
493 | 0, /* @tp_repr@ */ | |
494 | 0, /* @tp_as_number@ */ | |
495 | 0, /* @tp_as_sequence@ */ | |
496 | 0, /* @tp_as_mapping@ */ | |
497 | 0, /* @tp_hash@ */ | |
498 | 0, /* @tp_call@ */ | |
499 | 0, /* @tp_str@ */ | |
500 | 0, /* @tp_getattro@ */ | |
501 | 0, /* @tp_setattro@ */ | |
502 | 0, /* @tp_as_buffer@ */ | |
503 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
504 | Py_TPFLAGS_BASETYPE, | |
505 | ||
506 | /* @tp_doc@ */ | |
c34453a5 | 507 | "Iterates over the keys of a mapping.", |
11cb3d97 MW |
508 | |
509 | 0, /* @tp_traverse@ */ | |
510 | 0, /* @tp_clear@ */ | |
511 | 0, /* @tp_richcompare@ */ | |
512 | 0, /* @tp_weaklistoffset@ */ | |
513 | PyObject_SelfIter, /* @tp_iter@ */ | |
514 | itemiter_pynext, /* @tp_iternext@ */ | |
515 | 0, /* @tp_methods@ */ | |
516 | 0, /* @tp_members@ */ | |
517 | 0, /* @tp_getset@ */ | |
518 | 0, /* @tp_base@ */ | |
519 | 0, /* @tp_dict@ */ | |
520 | 0, /* @tp_descr_get@ */ | |
521 | 0, /* @tp_descr_set@ */ | |
522 | 0, /* @tp_dictoffset@ */ | |
523 | 0, /* @tp_init@ */ | |
524 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
525 | abstract_pynew, /* @tp_new@ */ | |
526 | 0, /* @tp_free@ */ | |
527 | 0 /* @tp_is_gc@ */ | |
528 | }; | |
529 | ||
530 | static PyObject *valiter_pynext(PyObject *me) | |
531 | { | |
532 | PyObject *k = 0, *rc = 0; | |
b2687a0a | 533 | |
11cb3d97 MW |
534 | if ((k = PyIter_Next(ITER_I(me))) != 0) |
535 | rc = PyObject_GetItem(ITER_MAP(me), k); | |
536 | Py_XDECREF(k); | |
537 | return (rc); | |
538 | } | |
539 | ||
540 | static PyTypeObject valiter_pytype_skel = { | |
541 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
542 | "ValueIter", /* @tp_name@ */ | |
543 | sizeof(iter_pyobj), /* @tp_basicsize@ */ | |
544 | 0, /* @tp_itemsize@ */ | |
545 | ||
546 | iter_pydealloc, /* @tp_dealloc@ */ | |
547 | 0, /* @tp_print@ */ | |
548 | 0, /* @tp_getattr@ */ | |
549 | 0, /* @tp_setattr@ */ | |
550 | 0, /* @tp_compare@ */ | |
551 | 0, /* @tp_repr@ */ | |
552 | 0, /* @tp_as_number@ */ | |
553 | 0, /* @tp_as_sequence@ */ | |
554 | 0, /* @tp_as_mapping@ */ | |
555 | 0, /* @tp_hash@ */ | |
556 | 0, /* @tp_call@ */ | |
557 | 0, /* @tp_str@ */ | |
558 | 0, /* @tp_getattro@ */ | |
559 | 0, /* @tp_setattro@ */ | |
560 | 0, /* @tp_as_buffer@ */ | |
561 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
562 | Py_TPFLAGS_BASETYPE, | |
563 | ||
564 | /* @tp_doc@ */ | |
c34453a5 | 565 | "Iterates over the values of a mapping.", |
11cb3d97 MW |
566 | |
567 | 0, /* @tp_traverse@ */ | |
568 | 0, /* @tp_clear@ */ | |
569 | 0, /* @tp_richcompare@ */ | |
570 | 0, /* @tp_weaklistoffset@ */ | |
571 | PyObject_SelfIter, /* @tp_iter@ */ | |
572 | valiter_pynext, /* @tp_iternext@ */ | |
573 | 0, /* @tp_methods@ */ | |
574 | 0, /* @tp_members@ */ | |
575 | 0, /* @tp_getset@ */ | |
576 | 0, /* @tp_base@ */ | |
577 | 0, /* @tp_dict@ */ | |
578 | 0, /* @tp_descr_get@ */ | |
579 | 0, /* @tp_descr_set@ */ | |
580 | 0, /* @tp_dictoffset@ */ | |
581 | 0, /* @tp_init@ */ | |
582 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
583 | abstract_pynew, /* @tp_new@ */ | |
584 | 0, /* @tp_free@ */ | |
585 | 0 /* @tp_is_gc@ */ | |
586 | }; | |
587 | ||
588 | PySequenceMethods gmap_pysequence = { | |
589 | 0, /* @sq_length@ */ | |
590 | 0, /* @sq_concat@ */ | |
591 | 0, /* @sq_repeat@ */ | |
592 | 0, /* @sq_item@ */ | |
593 | 0, /* @sq_slice@ */ | |
594 | 0, /* @sq_ass_item@ */ | |
595 | 0, /* @sq_ass_slice@ */ | |
596 | PyMapping_HasKey, /* @sq_contains@ */ | |
597 | 0, /* @sq_inplace_concat@ */ | |
598 | 0 /* @sq_inplace_repeat@ */ | |
b2687a0a | 599 | }; |
11cb3d97 | 600 | |
3d8f5f7c | 601 | Py_ssize_t gmap_pysize(PyObject *me) |
11cb3d97 MW |
602 | { |
603 | PyObject *i = 0, *x = 0; | |
77a0d53f | 604 | Py_ssize_t rc = -1, n = 0; |
11cb3d97 MW |
605 | |
606 | if ((i = PyObject_GetIter(me)) == 0) goto done; | |
607 | while ((x = PyIter_Next(i)) != 0) { n++; Py_DECREF(x); x = 0; } | |
608 | if (PyErr_Occurred()) goto done; | |
609 | rc = n; | |
610 | done: | |
611 | Py_XDECREF(i); Py_XDECREF(x); | |
612 | return (rc); | |
613 | } | |
614 | ||
615 | PyObject *gmapmeth_has_key(PyObject *me, PyObject *arg) | |
616 | { | |
617 | PyObject *k; | |
618 | if (!PyArg_ParseTuple(arg, "O:has_key", &k)) return (0); | |
619 | return (getbool(PyMapping_HasKey(me, k))); | |
620 | } | |
621 | ||
622 | PyObject *gmapmeth_keys(PyObject *me, PyObject *arg) | |
623 | { | |
624 | PyObject *l = 0, *i = 0, *k, *rc = 0; | |
625 | int err; | |
626 | ||
627 | if (!PyArg_ParseTuple(arg, ":keys") || | |
628 | (l = PyList_New(0)) == 0 || | |
629 | (i = PyObject_GetIter(me)) == 0) | |
630 | goto done; | |
631 | while ((k = PyIter_Next(i)) != 0) | |
632 | { err = PyList_Append(l, k); Py_DECREF(k); if (err) goto done; } | |
633 | if (PyErr_Occurred()) goto done; | |
634 | rc = l; l = 0; | |
635 | done: | |
636 | Py_XDECREF(l); Py_XDECREF(i); | |
637 | return (rc); | |
638 | } | |
639 | ||
640 | PyObject *gmapmeth_values(PyObject *me, PyObject *arg) | |
641 | { | |
642 | PyObject *l = 0, *i = 0, *k, *v, *rc = 0; | |
643 | int err = 0; | |
644 | ||
645 | if (!PyArg_ParseTuple(arg, ":values") || | |
646 | (l = PyList_New(0)) == 0 || | |
647 | (i = PyObject_GetIter(me)) == 0) | |
648 | goto done; | |
b2687a0a | 649 | while ((k = PyIter_Next(i)) != 0) { |
11cb3d97 MW |
650 | if ((v = PyObject_GetItem(me, k)) == 0 || |
651 | PyList_Append(l, v)) | |
652 | err = -1; | |
653 | Py_DECREF(k); Py_XDECREF(v); | |
654 | if (err) goto done; | |
655 | } | |
656 | if (PyErr_Occurred()) goto done; | |
657 | rc = l; l = 0; | |
658 | done: | |
659 | Py_XDECREF(l); Py_XDECREF(i); | |
660 | return (rc); | |
661 | } | |
662 | ||
663 | PyObject *gmapmeth_items(PyObject *me, PyObject *arg) | |
664 | { | |
665 | PyObject *l = 0, *i = 0, *k, *v, *z, *rc = 0; | |
666 | int err = 0; | |
667 | ||
668 | if (!PyArg_ParseTuple(arg, ":items") || | |
669 | (l = PyList_New(0)) == 0 || | |
670 | (i = PyObject_GetIter(me)) == 0) | |
671 | goto done; | |
672 | while ((k = PyIter_Next(i)) != 0) { | |
673 | z = 0; | |
674 | if ((v = PyObject_GetItem(me, k)) == 0 || | |
675 | (z = Py_BuildValue("(OO)", k, v)) == 0 || | |
676 | PyList_Append(l, z)) | |
677 | err = -1; | |
678 | Py_DECREF(k); Py_XDECREF(v); Py_XDECREF(z); | |
679 | if (err) goto done; | |
680 | } | |
681 | if (PyErr_Occurred()) goto done; | |
682 | rc = l; l = 0; | |
683 | done: | |
684 | Py_XDECREF(l); Py_XDECREF(i); | |
685 | return (rc); | |
686 | } | |
687 | ||
688 | PyObject *gmapmeth_iterkeys(PyObject *me, PyObject *arg) | |
689 | { | |
690 | if (!PyArg_ParseTuple(arg, ":iterkeys")) return (0); | |
691 | return (PyObject_GetIter(me)); | |
692 | } | |
693 | ||
694 | PyObject *gmapmeth_itervalues(PyObject *me, PyObject *arg) | |
695 | { | |
696 | PyObject *i; | |
697 | iter_pyobj *ii; | |
698 | ||
699 | if (!PyArg_ParseTuple(arg, ":itervalues") || | |
700 | (i = PyObject_GetIter(me)) == 0) | |
701 | return (0); | |
702 | ii = PyObject_NEW(iter_pyobj, valiter_pytype); | |
703 | ii->map = me; Py_INCREF(me); | |
704 | ii->i = i; | |
705 | return ((PyObject *)ii); | |
706 | } | |
707 | ||
708 | PyObject *gmapmeth_iteritems(PyObject *me, PyObject *arg) | |
709 | { | |
710 | PyObject *i; | |
711 | iter_pyobj *ii; | |
712 | ||
713 | if (!PyArg_ParseTuple(arg, ":iteritems") || | |
714 | (i = PyObject_GetIter(me)) == 0) | |
715 | return (0); | |
716 | ii = PyObject_NEW(iter_pyobj, itemiter_pytype); | |
717 | ii->map = me; Py_INCREF(me); | |
718 | ii->i = i; | |
719 | return ((PyObject *)ii); | |
720 | } | |
721 | ||
722 | PyObject *gmapmeth_clear(PyObject *me, PyObject *arg) | |
723 | { | |
724 | PyObject *i = 0, *k = 0, *rc = 0; | |
725 | ||
726 | if (!PyArg_ParseTuple(arg, ":clear") || | |
727 | (i = PyObject_GetIter(me)) == 0) | |
728 | goto end; | |
729 | while ((k = PyIter_Next(i)) != 0) { | |
730 | PyObject_DelItem(me, k); | |
731 | Py_DECREF(k); | |
732 | } | |
733 | if (PyErr_Occurred()) goto end; | |
734 | rc = me; Py_INCREF(me); | |
735 | end: | |
736 | Py_XDECREF(i); | |
737 | return (rc); | |
738 | } | |
739 | ||
827f89d7 | 740 | static const char *const def_kwlist[] = { "key", "default", 0 }; |
11cb3d97 MW |
741 | |
742 | PyObject *gmapmeth_get(PyObject *me, PyObject *arg, PyObject *kw) | |
743 | { | |
744 | PyObject *k, *def = Py_None, *v; | |
745 | ||
7f38dc76 | 746 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:get", |
827f89d7 MW |
747 | (/*unconst*/ char **)def_kwlist, |
748 | &k, &def)) | |
11cb3d97 MW |
749 | return (0); |
750 | if ((v = PyObject_GetItem(me, k)) != 0) return (v); | |
751 | PyErr_Clear(); | |
752 | RETURN_OBJ(def); | |
753 | } | |
754 | ||
755 | PyObject *gmapmeth_setdefault(PyObject *me, PyObject *arg, PyObject *kw) | |
756 | { | |
757 | PyObject *k, *def = Py_None, *v; | |
758 | ||
8afbf782 | 759 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:setdefault", |
827f89d7 MW |
760 | (/*unconst*/ char **)def_kwlist, |
761 | &k, &def)) | |
11cb3d97 MW |
762 | return (0); |
763 | if ((v = PyObject_GetItem(me, k)) != 0) return (v); | |
764 | PyErr_Clear(); | |
765 | if (PyObject_SetItem(me, k, def)) return (0); | |
766 | RETURN_OBJ(def); | |
767 | } | |
768 | ||
769 | PyObject *gmapmeth_pop(PyObject *me, PyObject *arg, PyObject *kw) | |
770 | { | |
771 | PyObject *k, *def = 0, *v; | |
772 | ||
7f38dc76 | 773 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:pop", |
827f89d7 MW |
774 | (/*unconst*/ char **)def_kwlist, |
775 | &k, &def)) | |
11cb3d97 MW |
776 | return (0); |
777 | if ((v = PyObject_GetItem(me, k)) != 0) { | |
778 | PyObject_DelItem(me, k); | |
779 | return (v); | |
8afbf782 MW |
780 | } else if (def) { |
781 | PyErr_Clear(); | |
782 | RETURN_OBJ(def); | |
783 | } else | |
784 | return (0); | |
11cb3d97 MW |
785 | } |
786 | ||
787 | PyObject *gmapmeth_update(PyObject *me, PyObject *arg) | |
788 | { | |
789 | PyObject *map, *i = 0, *k, *v, *rc = 0; | |
790 | int err = 0; | |
791 | ||
792 | if (!PyArg_ParseTuple(arg, "O:update", &map) || | |
793 | (i = PyObject_GetIter(map)) == 0) | |
794 | goto end; | |
795 | while ((k = PyIter_Next(i)) != 0) { | |
796 | if ((v = PyObject_GetItem(map, k)) == 0 || | |
797 | PyObject_SetItem(me, k, v)) | |
798 | err = -1; | |
799 | Py_DECREF(k); Py_XDECREF(v); | |
800 | if (err) goto end; | |
801 | } | |
802 | if (PyErr_Occurred()) goto end; | |
803 | rc = me; Py_INCREF(me); | |
804 | end: | |
805 | Py_XDECREF(i); | |
806 | return (rc); | |
807 | } | |
808 | ||
809 | PyObject *gmapmeth_popitem(PyObject *me, PyObject *arg) | |
810 | { | |
811 | PyObject *i = 0, *k = 0, *v = 0, *rc = 0; | |
812 | ||
813 | if (!PyArg_ParseTuple(arg, ":popitem") || | |
fcccd4ae | 814 | (i = PyObject_GetIter(me)) == 0) |
11cb3d97 MW |
815 | goto end; |
816 | if ((k = PyIter_Next(i)) == 0) { | |
817 | if (!PyErr_Occurred()) VALERR("popitem(): mapping is empty"); | |
818 | goto end; | |
819 | } | |
820 | if ((v = PyObject_GetItem(me, k)) == 0 || | |
821 | PyObject_DelItem(me, k)) | |
822 | goto end; | |
823 | rc = Py_BuildValue("(OO)", k, v); | |
824 | end: | |
825 | Py_XDECREF(i); Py_XDECREF(k); Py_XDECREF(v); | |
826 | return (rc); | |
827 | } | |
828 | ||
829 | PyMethodDef gmap_pymethods[] = { | |
830 | GMAP_METHODS | |
831 | { 0 } | |
832 | }; | |
833 | ||
834 | /*----- Initialization ----------------------------------------------------*/ | |
835 | ||
930d78e3 MW |
836 | static PyObject *meth__set_home_module(PyObject *me, PyObject *arg) |
837 | { | |
838 | PyObject *mod; | |
839 | ||
840 | if (!PyArg_ParseTuple(arg, "O!:_set_home_module", &PyModule_Type, &mod)) | |
841 | return (0); | |
842 | Py_XDECREF(home_module); home_module = mod; Py_INCREF(home_module); | |
843 | RETURN_NONE; | |
844 | } | |
845 | ||
846 | static const PyMethodDef methods[] = { | |
847 | #define METHNAME(func) meth_##func | |
848 | METH (_set_home_module, "_set_home_module(MOD)") | |
849 | #undef METHNAME | |
850 | { 0 } | |
851 | }; | |
852 | ||
e56eb7d6 | 853 | void util_pyinit(void) |
11cb3d97 | 854 | { |
dc075750 | 855 | modname = PyString_FromString("catacomb"); |
11cb3d97 MW |
856 | INITTYPE(itemiter, root); |
857 | INITTYPE(valiter, root); | |
930d78e3 | 858 | addmethods(methods); |
11cb3d97 MW |
859 | } |
860 | ||
e56eb7d6 | 861 | void util_pyinsert(PyObject *mod) |
11cb3d97 MW |
862 | { |
863 | INSERT("ItemIter", itemiter_pytype); | |
864 | INSERT("ValueIter", valiter_pytype); | |
865 | } | |
866 | ||
867 | /*----- That's all, folks -------------------------------------------------*/ |