5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- Main code ---------------------------------------------------------*/
33 PyObject
*mexp_common(PyObject
*me
, PyObject
*arg
,
35 PyObject
*(*id
)(PyObject
*),
36 int (*fill
)(void *, PyObject
*,
37 PyObject
*, PyObject
*),
38 PyObject
*(*exp
)(PyObject
*, void *, int),
41 int i
= 0, j
, n
, flat
;
42 PyObject
*qq
, *x
, *y
, *z
= 0;
45 if (PyTuple_GET_SIZE(arg
) == 1)
46 arg
= PyTuple_GET_ITEM(arg
, 0);
47 if (!PySequence_Check(arg
)) TYERR("not a sequence");
48 n
= PySequence_Size(arg
); if (n
< 0) goto end
;
49 if (!n
) { z
= id(me
); goto end
; }
50 x
= PySequence_GetItem(arg
, 0);
51 if (PySequence_Check(x
))
54 if (n
% 2) VALERR("must have even number of arguments");
60 v
= xmalloc(n
* efsz
);
61 for (i
= j
= 0, vv
= v
; i
< n
; i
++, vv
+= efsz
) {
63 x
= PySequence_GetItem(arg
, j
++);
64 y
= PySequence_GetItem(arg
, j
++);
66 qq
= PySequence_GetItem(arg
, j
++);
68 if (!PySequence_Check(qq
) || PySequence_Size(qq
) != 2) {
70 TYERR("want a sequence of pairs");
72 x
= PySequence_GetItem(qq
, 0);
73 y
= PySequence_GetItem(qq
, 1);
76 if (!x
|| !y
) goto end
;
77 if (fill(vv
, me
, x
, y
)) {
80 if (!PyErr_Occurred())
81 PyErr_SetString(PyExc_TypeError
, "type mismatch");
91 for (j
= 0, vv
= v
; j
< i
; j
++, vv
+= efsz
)
98 int convmpw(PyObject
*o
, void *pp
)
103 if (!convulong(o
, &u
)) goto end
;
104 if (u
> MPW_MAX
) VALERR("out of range");
111 static PyTypeObject
*thingtab_pytype
;
113 typedef struct thingentry
{
117 #define THING_VAL(x) (((thingentry *)(x))->val)
119 typedef struct thingtab_pyobj
{
123 #define THINGTAB_T(x) (&((thingtab_pyobj *)(x))->t)
125 static void *thingtab_gmlookup(PyObject
*me
, PyObject
*key
, unsigned *f
)
129 p
= TEXT_STR(key
); if (!p
) return (0);
130 return (sym_find(THINGTAB_T(me
), p
, -1, 0, f
));
133 static void thingtab_gmiterinit(PyObject
*me
, void *i
)
134 { sym_mkiter(i
, THINGTAB_T(me
)); }
136 static void *thingtab_gmiternext(PyObject
*me
, void *i
)
137 { sym_iter
*it
= i
; void *e
; SYM_NEXT(it
, e
); return (e
); }
139 static PyObject
*thingtab_gmentrykey(PyObject
*me
, void *e
)
140 { return (TEXT_FROMSTR(SYM_NAME(e
))); }
142 static PyObject
*thingtab_gmentryvalue(PyObject
*me
, void *e
)
143 { PyObject
*rc
= THING_VAL(e
); RETURN_OBJ(rc
); }
145 static const gmap_ops thingtab_gmops
= {
151 thingtab_gmentryvalue
154 static Py_ssize_t
thing_pysize(PyObject
*me
)
155 { return gmap_pysize_from_sym(THINGTAB_T(me
)); }
157 static const PyMappingMethods thingtab_pymapping
= {
163 static thingtab_pyobj
*make_thingtab(void)
165 thingtab_pyobj
*map
= PyObject_NEW(thingtab_pyobj
, thingtab_pytype
);
167 map
->gmops
= &thingtab_gmops
;
172 PyObject
*make_algtab(const void *tab
, size_t esz
,
173 const char *(*namefn
)(const void *),
174 PyObject
*(*valfn
)(const void *))
176 thingtab_pyobj
*map
= make_thingtab();
183 name
= namefn(p
); if (!name
) break;
184 e
= sym_find(&map
->t
, name
, -1, sizeof(*e
), &f
); assert(!f
);
188 return ((PyObject
*)map
);
191 PyObject
*make_grouptab(const void *tab
, size_t esz
,
192 const char *(*namefn
)(const void *),
193 int (*ixfn
)(const void *), PyObject
*(*valfn
)(int))
195 thingtab_pyobj
*map
= make_thingtab();
196 struct { const char *name
; int ix
; } *ixtab
= 0;
197 PyObject
**valtab
, **vv
;
205 name
= namefn(p
); if (!name
) break;
209 ixtab
= xrealloc(ixtab
, n
*sizeof(*ixtab
), i
*sizeof(*ixtab
));
211 ixtab
[i
].name
= name
; ixtab
[i
].ix
= ixfn(p
); assert(ixtab
[i
].ix
>= 0);
216 valtab
= xmalloc(n
*sizeof(*valtab
));
217 for (i
= 0; i
< n
; i
++) valtab
[i
] = 0;
219 for (i
= 0; i
< n
; i
++) {
220 e
= sym_find(&map
->t
, ixtab
[i
].name
, -1, sizeof(*e
), &f
); assert(!f
);
221 vv
= &valtab
[ixtab
[i
].ix
];
222 if (*vv
) Py_INCREF(*vv
);
223 else *vv
= valfn(ixtab
[i
].ix
);
227 xfree(ixtab
); xfree(valtab
);
228 return ((PyObject
*)map
);
231 static const PyTypeObject thingtab_pytype_skel
= {
232 PyVarObject_HEAD_INIT(0, 0) /* Header */
233 "_MiscTable", /* @tp_name@ */
234 sizeof(thingtab_pyobj
), /* @tp_basicsize@ */
235 0, /* @tp_itemsize@ */
237 0, /* @tp_dealloc@ */
239 0, /* @tp_getattr@ */
240 0, /* @tp_setattr@ */
241 0, /* @tp_compare@ */
243 0, /* @tp_as_number@ */
244 PYSEQUENCE(gmap
), /* @tp_as_sequence@ */
245 PYMAPPING(thingtab
), /* @tp_as_mapping@ */
249 0, /* @tp_getattro@ */
250 0, /* @tp_setattro@ */
251 0, /* @tp_as_buffer@ */
252 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
256 "Class for tables of algorithms and abstract-group data.\n"
257 " Not instantiable by users.",
259 0, /* @tp_traverse@ */
261 0, /* @tp_richcompare@ */
262 0, /* @tp_weaklistoffset@ */
263 gmap_pyiter
, /* @tp_iter@ */
264 0, /* @tp_iternext@ */
265 PYMETHODS(gmapro
), /* @tp_methods@ */
266 0, /* @tp_members@ */
270 0, /* @tp_descr_get@ */
271 0, /* @tp_descr_set@ */
272 0, /* @tp_dictoffset@ */
274 PyType_GenericAlloc
, /* @tp_alloc@ */
275 abstract_pynew
, /* @tp_new@ */
280 static PyObject
*smallprimes(void)
282 PyObject
*v
= PyList_New(NPRIME
);
285 for (i
= 0; i
< NPRIME
; i
++)
286 PyList_SET_ITEM(v
, i
, PyInt_FromLong(primetab
[i
]));
290 static PyObject
*meth__ego(PyObject
*me
, PyObject
*arg
)
293 if (!PyArg_ParseTuple(arg
, "s:_ego", &argv0
))
295 if (STRCMP(QUIS
, ==, "<UNNAMED>"))
300 static const PyMethodDef methods
[] = {
301 #define METHNAME(func) meth_##func
302 METH (_ego
, "_ego(ARGV0)")
307 static void init_random(void)
309 #if PY_VERSION_HEX >= 0x02060000
313 if (!Py_HashRandomizationFlag
) return;
314 seed
= getenv("PYTHONHASHSEED");
315 if (!seed
|| STRCMP(seed
, ==, "random")) r
= GR_WORD(&rand_global
);
316 else r
= strtoul(seed
, 0, 0);
317 if (!r
) r
= 0xe011f220; /* zero doesn't work well */
318 unihash_setkey(&unihash_global
, r
);
323 static PyModuleDef moddef
= {
324 PyModuleDef_HEAD_INIT
,
325 "catacomb._base", /* @m_name@ */
326 "Low-level module for Catacomb bindings. Use `catacomb' instead.",
331 0, /* @m_traverse@ */
337 EXPORT PyMODINIT_FUNC
PY23(init_base
, PyInit__base
)(void)
341 modname
= TEXT_FROMSTR("catacomb");
344 INITTYPE(thingtab
, root
);
347 moddef
.m_methods
= donemethods();
348 mod
= PyModule_Create(&moddef
);
350 mod
= Py_InitModule("catacomb._base", donemethods());
353 INSERT("_MiscTable", thingtab_pytype
);
354 INSERT("smallprimes", smallprimes());
360 /*----- That's all, folks -------------------------------------------------*/