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
= 0, *x
= 0, *y
= 0, *z
= 0, *it
= 0;
46 if (PyTuple_Size(arg
) == 1) arg
= PyTuple_GET_ITEM(arg
, 0);
47 it
= PyObject_GetIter(arg
); if (!it
) goto end
;
50 if (!PyErr_Occurred()) z
= id(me
);
53 flat
= !PySequence_Check(qq
);
54 if (!PySequence_Check(arg
))
57 n
= PySequence_Size(arg
);
58 if (n
== (size_t)-1 && PyErr_Occurred()) goto end
;
67 if (!PySequence_Check(qq
) || PySequence_Size(qq
) != 2)
68 TYERR("want a sequence of pairs");
69 x
= PySequence_GetItem(qq
, 0);
70 y
= PySequence_GetItem(qq
, 1);
75 if (PyErr_Occurred()) goto end
;
76 VALERR("must have even number of operands");
79 if (!x
|| !y
) goto end
;
81 if (i
>= n
) { n
*= 2; v
= xrealloc(v
, n
*efsz
, i
*efsz
); }
82 if (fill(v
+ o
, me
, x
, y
)) {
83 if (PyErr_Occurred()) goto end
;
84 TYERR("type mismatch");
93 if (PyErr_Occurred()) goto end
;
101 while (i
--) { o
-= efsz
; drop(v
+ o
); }
103 Py_XDECREF(it
); Py_XDECREF(qq
); Py_XDECREF(x
); Py_XDECREF(y
);
107 int convmpw(PyObject
*o
, void *pp
)
112 if (!convulong(o
, &u
)) goto end
;
113 if (u
> MPW_MAX
) VALERR("out of range");
120 static PyTypeObject
*thingtab_pytype
;
122 typedef struct thingentry
{
126 #define THING_VAL(x) (((thingentry *)(x))->val)
128 typedef struct thingtab_pyobj
{
132 #define THINGTAB_T(x) (&((thingtab_pyobj *)(x))->t)
134 static void *thingtab_gmlookup(PyObject
*me
, PyObject
*key
, unsigned *f
)
138 p
= TEXT_STR(key
); if (!p
) return (0);
139 return (sym_find(THINGTAB_T(me
), p
, -1, 0, f
));
142 static void thingtab_gmiterinit(PyObject
*me
, void *i
)
143 { sym_mkiter(i
, THINGTAB_T(me
)); }
145 static void *thingtab_gmiternext(PyObject
*me
, void *i
)
146 { sym_iter
*it
= i
; void *e
; SYM_NEXT(it
, e
); return (e
); }
148 static PyObject
*thingtab_gmentrykey(PyObject
*me
, void *e
)
149 { return (TEXT_FROMSTR(SYM_NAME(e
))); }
151 static PyObject
*thingtab_gmentryvalue(PyObject
*me
, void *e
)
152 { PyObject
*rc
= THING_VAL(e
); RETURN_OBJ(rc
); }
154 static const gmap_ops thingtab_gmops
= {
160 thingtab_gmentryvalue
163 static Py_ssize_t
thing_pysize(PyObject
*me
)
164 { return gmap_pysize_from_sym(THINGTAB_T(me
)); }
166 static const PyMappingMethods thingtab_pymapping
= {
172 static thingtab_pyobj
*make_thingtab(void)
174 thingtab_pyobj
*map
= PyObject_NEW(thingtab_pyobj
, thingtab_pytype
);
176 map
->gmops
= &thingtab_gmops
;
181 PyObject
*make_algtab(const void *tab
, size_t esz
,
182 const char *(*namefn
)(const void *),
183 PyObject
*(*valfn
)(const void *))
185 thingtab_pyobj
*map
= make_thingtab();
192 name
= namefn(p
); if (!name
) break;
193 e
= sym_find(&map
->t
, name
, -1, sizeof(*e
), &f
); assert(!f
);
197 return ((PyObject
*)map
);
200 PyObject
*make_grouptab(const void *tab
, size_t esz
,
201 const char *(*namefn
)(const void *),
202 int (*ixfn
)(const void *), PyObject
*(*valfn
)(int))
204 thingtab_pyobj
*map
= make_thingtab();
205 struct { const char *name
; int ix
; } *ixtab
= 0;
206 PyObject
**valtab
, **vv
;
214 name
= namefn(p
); if (!name
) break;
218 ixtab
= xrealloc(ixtab
, n
*sizeof(*ixtab
), i
*sizeof(*ixtab
));
220 ixtab
[i
].name
= name
; ixtab
[i
].ix
= ixfn(p
); assert(ixtab
[i
].ix
>= 0);
225 valtab
= xmalloc(n
*sizeof(*valtab
));
226 for (i
= 0; i
< n
; i
++) valtab
[i
] = 0;
228 for (i
= 0; i
< n
; i
++) {
229 e
= sym_find(&map
->t
, ixtab
[i
].name
, -1, sizeof(*e
), &f
); assert(!f
);
230 vv
= &valtab
[ixtab
[i
].ix
];
231 if (*vv
) Py_INCREF(*vv
);
232 else *vv
= valfn(ixtab
[i
].ix
);
236 xfree(ixtab
); xfree(valtab
);
237 return ((PyObject
*)map
);
240 static const PyTypeObject thingtab_pytype_skel
= {
241 PyVarObject_HEAD_INIT(0, 0) /* Header */
242 "_MiscTable", /* @tp_name@ */
243 sizeof(thingtab_pyobj
), /* @tp_basicsize@ */
244 0, /* @tp_itemsize@ */
246 0, /* @tp_dealloc@ */
248 0, /* @tp_getattr@ */
249 0, /* @tp_setattr@ */
250 0, /* @tp_compare@ */
252 0, /* @tp_as_number@ */
253 PYSEQUENCE(gmap
), /* @tp_as_sequence@ */
254 PYMAPPING(thingtab
), /* @tp_as_mapping@ */
258 0, /* @tp_getattro@ */
259 0, /* @tp_setattro@ */
260 0, /* @tp_as_buffer@ */
261 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
265 "Class for tables of algorithms and abstract-group data.\n"
266 " Not instantiable by users.",
268 0, /* @tp_traverse@ */
270 0, /* @tp_richcompare@ */
271 0, /* @tp_weaklistoffset@ */
272 gmap_pyiter
, /* @tp_iter@ */
273 0, /* @tp_iternext@ */
274 PYMETHODS(gmapro
), /* @tp_methods@ */
275 0, /* @tp_members@ */
279 0, /* @tp_descr_get@ */
280 0, /* @tp_descr_set@ */
281 0, /* @tp_dictoffset@ */
283 PyType_GenericAlloc
, /* @tp_alloc@ */
284 abstract_pynew
, /* @tp_new@ */
289 static PyObject
*smallprimes(void)
291 PyObject
*v
= PyList_New(NPRIME
);
294 for (i
= 0; i
< NPRIME
; i
++)
295 PyList_SET_ITEM(v
, i
, PyInt_FromLong(primetab
[i
]));
299 static PyObject
*meth__ego(PyObject
*me
, PyObject
*arg
)
302 if (!PyArg_ParseTuple(arg
, "s:_ego", &argv0
))
304 if (STRCMP(QUIS
, ==, "<UNNAMED>"))
309 static const PyMethodDef methods
[] = {
310 #define METHNAME(func) meth_##func
311 METH (_ego
, "_ego(ARGV0)")
316 static void init_random(void)
318 #if PY_VERSION_HEX >= 0x02060000
322 if (!Py_HashRandomizationFlag
) return;
323 seed
= getenv("PYTHONHASHSEED");
324 if (!seed
|| STRCMP(seed
, ==, "random")) r
= GR_WORD(&rand_global
);
325 else r
= strtoul(seed
, 0, 0);
326 if (!r
) r
= 0xe011f220; /* zero doesn't work well */
327 unihash_setkey(&unihash_global
, r
);
332 static PyModuleDef moddef
= {
333 PyModuleDef_HEAD_INIT
,
334 "catacomb._base", /* @m_name@ */
335 "Low-level module for Catacomb bindings. Use `catacomb' instead.",
340 0, /* @m_traverse@ */
346 EXPORT PyMODINIT_FUNC
PY23(init_base
, PyInit__base
)(void)
350 modname
= TEXT_FROMSTR("catacomb");
353 INITTYPE(thingtab
, root
);
356 moddef
.m_methods
= donemethods();
357 mod
= PyModule_Create(&moddef
);
359 mod
= Py_InitModule("catacomb._base", donemethods());
362 INSERT("_MiscTable", thingtab_pytype
);
363 INSERT("smallprimes", smallprimes());
369 /*----- That's all, folks -------------------------------------------------*/