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 *, size_t),
43 PyObject
*qq
, *x
, *y
, *z
= 0;
46 if (PyTuple_GET_SIZE(arg
) == 1)
47 arg
= PyTuple_GET_ITEM(arg
, 0);
48 if (!PySequence_Check(arg
)) TYERR("not a sequence");
49 n
= PySequence_Size(arg
); if (n
< 0) goto end
;
50 if (!n
) { z
= id(me
); goto end
; }
51 x
= PySequence_GetItem(arg
, 0);
52 if (PySequence_Check(x
))
55 if (n
% 2) VALERR("must have even number of arguments");
61 v
= xmalloc(n
* efsz
);
62 for (i
= j
= 0, vv
= v
; i
< n
; i
++, vv
+= efsz
) {
64 x
= PySequence_GetItem(arg
, j
++);
65 y
= PySequence_GetItem(arg
, j
++);
67 qq
= PySequence_GetItem(arg
, j
++);
69 if (!PySequence_Check(qq
) || PySequence_Size(qq
) != 2) {
71 TYERR("want a sequence of pairs");
73 x
= PySequence_GetItem(qq
, 0);
74 y
= PySequence_GetItem(qq
, 1);
77 if (!x
|| !y
) goto end
;
78 if (fill(vv
, me
, x
, y
)) {
81 if (!PyErr_Occurred())
82 PyErr_SetString(PyExc_TypeError
, "type mismatch");
92 for (j
= 0, vv
= v
; j
< i
; j
++, vv
+= efsz
)
99 int convmpw(PyObject
*o
, void *pp
)
104 if (!convulong(o
, &u
)) goto end
;
105 if (u
> MPW_MAX
) VALERR("out of range");
112 static PyTypeObject
*thingtab_pytype
;
114 typedef struct thingentry
{
118 #define THING_VAL(x) (((thingentry *)(x))->val)
120 typedef struct thingtab_pyobj
{
124 #define THINGTAB_T(x) (&((thingtab_pyobj *)(x))->t)
126 static void *thingtab_gmlookup(PyObject
*me
, PyObject
*key
, unsigned *f
)
130 p
= TEXT_STR(key
); if (!p
) return (0);
131 return (sym_find(THINGTAB_T(me
), p
, -1, 0, f
));
134 static void thingtab_gmiterinit(PyObject
*me
, void *i
)
135 { sym_mkiter(i
, THINGTAB_T(me
)); }
137 static void *thingtab_gmiternext(PyObject
*me
, void *i
)
138 { sym_iter
*it
= i
; void *e
; SYM_NEXT(it
, e
); return (e
); }
140 static PyObject
*thingtab_gmentrykey(PyObject
*me
, void *e
)
141 { return (TEXT_FROMSTR(SYM_NAME(e
))); }
143 static PyObject
*thingtab_gmentryvalue(PyObject
*me
, void *e
)
144 { PyObject
*rc
= THING_VAL(e
); RETURN_OBJ(rc
); }
146 static const gmap_ops thingtab_gmops
= {
152 thingtab_gmentryvalue
155 static Py_ssize_t
thing_pysize(PyObject
*me
)
156 { return gmap_pysize_from_sym(THINGTAB_T(me
)); }
158 static const PyMappingMethods thingtab_pymapping
= {
164 static thingtab_pyobj
*make_thingtab(void)
166 thingtab_pyobj
*map
= PyObject_NEW(thingtab_pyobj
, thingtab_pytype
);
168 map
->gmops
= &thingtab_gmops
;
173 PyObject
*make_algtab(const void *tab
, size_t esz
,
174 const char *(*namefn
)(const void *),
175 PyObject
*(*valfn
)(const void *))
177 thingtab_pyobj
*map
= make_thingtab();
184 name
= namefn(p
); if (!name
) break;
185 e
= sym_find(&map
->t
, name
, -1, sizeof(*e
), &f
); assert(!f
);
189 return ((PyObject
*)map
);
192 PyObject
*make_grouptab(const void *tab
, size_t esz
,
193 const char *(*namefn
)(const void *),
194 int (*ixfn
)(const void *), PyObject
*(*valfn
)(int))
196 thingtab_pyobj
*map
= make_thingtab();
197 struct { const char *name
; int ix
; } *ixtab
= 0;
198 PyObject
**valtab
, **vv
;
206 name
= namefn(p
); if (!name
) break;
210 ixtab
= xrealloc(ixtab
, n
*sizeof(*ixtab
), i
*sizeof(*ixtab
));
212 ixtab
[i
].name
= name
; ixtab
[i
].ix
= ixfn(p
); assert(ixtab
[i
].ix
>= 0);
217 valtab
= xmalloc(n
*sizeof(*valtab
));
218 for (i
= 0; i
< n
; i
++) valtab
[i
] = 0;
220 for (i
= 0; i
< n
; i
++) {
221 e
= sym_find(&map
->t
, ixtab
[i
].name
, -1, sizeof(*e
), &f
); assert(!f
);
222 vv
= &valtab
[ixtab
[i
].ix
];
223 if (*vv
) Py_INCREF(*vv
);
224 else *vv
= valfn(ixtab
[i
].ix
);
228 xfree(ixtab
); xfree(valtab
);
229 return ((PyObject
*)map
);
232 static const PyTypeObject thingtab_pytype_skel
= {
233 PyVarObject_HEAD_INIT(0, 0) /* Header */
234 "_MiscTable", /* @tp_name@ */
235 sizeof(thingtab_pyobj
), /* @tp_basicsize@ */
236 0, /* @tp_itemsize@ */
238 0, /* @tp_dealloc@ */
240 0, /* @tp_getattr@ */
241 0, /* @tp_setattr@ */
242 0, /* @tp_compare@ */
244 0, /* @tp_as_number@ */
245 PYSEQUENCE(gmap
), /* @tp_as_sequence@ */
246 PYMAPPING(thingtab
), /* @tp_as_mapping@ */
250 0, /* @tp_getattro@ */
251 0, /* @tp_setattro@ */
252 0, /* @tp_as_buffer@ */
253 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
257 "Class for tables of algorithms and abstract-group data.\n"
258 " Not instantiable by users.",
260 0, /* @tp_traverse@ */
262 0, /* @tp_richcompare@ */
263 0, /* @tp_weaklistoffset@ */
264 gmap_pyiter
, /* @tp_iter@ */
265 0, /* @tp_iternext@ */
266 PYMETHODS(gmapro
), /* @tp_methods@ */
267 0, /* @tp_members@ */
271 0, /* @tp_descr_get@ */
272 0, /* @tp_descr_set@ */
273 0, /* @tp_dictoffset@ */
275 PyType_GenericAlloc
, /* @tp_alloc@ */
276 abstract_pynew
, /* @tp_new@ */
281 static PyObject
*smallprimes(void)
283 PyObject
*v
= PyList_New(NPRIME
);
286 for (i
= 0; i
< NPRIME
; i
++)
287 PyList_SET_ITEM(v
, i
, PyInt_FromLong(primetab
[i
]));
291 static PyObject
*meth__ego(PyObject
*me
, PyObject
*arg
)
294 if (!PyArg_ParseTuple(arg
, "s:_ego", &argv0
))
296 if (STRCMP(QUIS
, ==, "<UNNAMED>"))
301 static const PyMethodDef methods
[] = {
302 #define METHNAME(func) meth_##func
303 METH (_ego
, "_ego(ARGV0)")
308 static void init_random(void)
310 #if PY_VERSION_HEX >= 0x02060000
314 if (!Py_HashRandomizationFlag
) return;
315 seed
= getenv("PYTHONHASHSEED");
316 if (!seed
|| STRCMP(seed
, ==, "random")) r
= GR_WORD(&rand_global
);
317 else r
= strtoul(seed
, 0, 0);
318 if (!r
) r
= 0xe011f220; /* zero doesn't work well */
319 unihash_setkey(&unihash_global
, r
);
324 static PyModuleDef moddef
= {
325 PyModuleDef_HEAD_INIT
,
326 "catacomb._base", /* @m_name@ */
327 "Low-level module for Catacomb bindings. Use `catacomb' instead.",
332 0, /* @m_traverse@ */
338 EXPORT PyMODINIT_FUNC
PY23(init_base
, PyInit__base
)(void)
342 modname
= TEXT_FROMSTR("catacomb");
345 INITTYPE(thingtab
, root
);
348 moddef
.m_methods
= donemethods();
349 mod
= PyModule_Create(&moddef
);
351 mod
= Py_InitModule("catacomb._base", donemethods());
354 INSERT("_MiscTable", thingtab_pytype
);
355 INSERT("smallprimes", smallprimes());
361 /*----- That's all, folks -------------------------------------------------*/