pyke/mapping.c, key.c: Make the mapping code more intrusive and complete.
[catacomb-python] / catacomb.c
CommitLineData
d7ab1bab 1/* -*-c-*-
2 *
d7ab1bab 3 * Where the fun begins
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
b2687a0a 8/*----- Licensing notice --------------------------------------------------*
d7ab1bab 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 *
d7ab1bab 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 *
d7ab1bab 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
31/*----- Main code ---------------------------------------------------------*/
32
d7ab1bab 33PyObject *mexp_common(PyObject *me, PyObject *arg,
34 size_t efsz,
35 PyObject *(*id)(PyObject *),
36 int (*fill)(void *, PyObject *,
37 PyObject *, PyObject *),
38 PyObject *(*exp)(PyObject *, void *, int),
39 void (*drop)(void *))
40{
41 int i = 0, j, n, flat;
42 PyObject *qq, *x, *y, *z = 0;
43 char *v = 0, *vv;
44
8dd6ebb3
MW
45 if (PyTuple_GET_SIZE(arg) == 1)
46 arg = PyTuple_GET_ITEM(arg, 0);
d7ab1bab 47 Py_INCREF(arg);
48 if (!PySequence_Check(arg)) TYERR("not a sequence");
b17bbe3b
MW
49 n = PySequence_Size(arg); if (n < 0) goto end;
50 if (!n) { z = id(me); goto end; }
d7ab1bab 51 x = PySequence_GetItem(arg, 0);
52 if (PySequence_Check(x))
53 flat = 0;
54 else {
55 if (n % 2) VALERR("must have even number of arguments");
56 n /= 2;
57 flat = 1;
58 }
59 Py_DECREF(x);
60
61 v = xmalloc(n * efsz);
62 for (i = j = 0, vv = v; i < n; i++, vv += efsz) {
63 if (flat) {
64 x = PySequence_GetItem(arg, j++);
65 y = PySequence_GetItem(arg, j++);
66 } else {
67 qq = PySequence_GetItem(arg, j++);
68 if (!qq) goto end;
69 if (!PySequence_Check(qq) || PySequence_Size(qq) != 2) {
70 Py_DECREF(qq);
71 TYERR("want a sequence of pairs");
72 }
73 x = PySequence_GetItem(qq, 0);
74 y = PySequence_GetItem(qq, 1);
75 Py_DECREF(qq);
76 }
77 if (!x || !y) goto end;
78 if (fill(vv, me, x, y)) {
79 Py_DECREF(x);
80 Py_DECREF(y);
81 if (!PyErr_Occurred())
82 PyErr_SetString(PyExc_TypeError, "type mismatch");
83 goto end;
84 }
85 Py_DECREF(x);
86 Py_DECREF(y);
87 }
88 z = exp(me, v, n);
89
90end:
91 if (v) {
92 for (j = 0, vv = v; j < i; j++, vv += efsz)
93 drop(vv);
94 xfree(v);
95 }
96 Py_DECREF(arg);
97 return (z);
98}
99
361fd0e6
MW
100int convmpw(PyObject *o, void *pp)
101{
102 unsigned long u;
103 unsigned *p = pp;
104
105 if (!convulong(o, &u)) goto end;
106 if (u > MPW_MAX) VALERR("out of range");
107 *p = u;
108 return (1);
109end:
110 return (0);
111}
112
d7ab1bab 113static PyObject *smallprimes(void)
114{
115 PyObject *v = PyList_New(NPRIME);
116 int i;
117
118 for (i = 0; i < NPRIME; i++)
8dd6ebb3 119 PyList_SET_ITEM(v, i, PyInt_FromLong(primetab[i]));
d7ab1bab 120 return (v);
121}
122
46e6ad89 123static PyObject *meth__ego(PyObject *me, PyObject *arg)
124{
125 char *argv0;
126 if (!PyArg_ParseTuple(arg, "s:_ego", &argv0))
127 return (0);
20441612 128 if (STRCMP(QUIS, ==, "<UNNAMED>"))
46e6ad89 129 ego(argv0);
130 RETURN_NONE;
131}
132
ddd4720b 133static const PyMethodDef methods[] = {
46e6ad89 134#define METHNAME(func) meth_##func
ef783f91 135 METH (_ego, "_ego(ARGV0)")
46e6ad89 136#undef METHNAME
137 { 0 }
138};
139
bb87b97a
MW
140static void init_random(void)
141{
57aaeb41 142#if PY_VERSION_HEX >= 0x02060000
bb87b97a
MW
143 char *seed;
144 uint32 r;
145
146 if (!Py_HashRandomizationFlag) return;
147 seed = getenv("PYTHONHASHSEED");
20441612 148 if (!seed || STRCMP(seed, ==, "random")) r = GR_WORD(&rand_global);
bb87b97a
MW
149 else r = strtoul(seed, 0, 0);
150 if (!r) r = 0xe011f220; /* zero doesn't work well */
151 unihash_setkey(&unihash_global, r);
152#endif
153}
154
6640e060 155EXPORT void init_base(void)
11cb3d97 156{
d7ab1bab 157 PyObject *mod;
47061e38
MW
158
159 modname = PyString_FromString("catacomb");
46e6ad89 160 addmethods(methods);
d7ab1bab 161 INIT_MODULES;
bb87b97a 162 init_random();
11cb3d97 163 mod = Py_InitModule("catacomb._base", donemethods());
d7ab1bab 164 INSERT_MODULES;
165 INSERT("smallprimes", smallprimes());
d7ab1bab 166}
167
168/*----- That's all, folks -------------------------------------------------*/