*.c: Split the constant definitions into the various submodules.
[catacomb-python] / catacomb.c
1 /* -*-c-*-
2 *
3 * Where the fun begins
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16 *
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.
21 *
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
33 PyObject *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
45 if (PyTuple_GET_SIZE(arg) == 1)
46 arg = PyTuple_GET_ITEM(arg, 0);
47 Py_INCREF(arg);
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))
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
90 end:
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
100 int 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);
109 end:
110 return (0);
111 }
112
113 static PyObject *smallprimes(void)
114 {
115 PyObject *v = PyList_New(NPRIME);
116 int i;
117
118 for (i = 0; i < NPRIME; i++)
119 PyList_SET_ITEM(v, i, PyInt_FromLong(primetab[i]));
120 return (v);
121 }
122
123 static PyObject *meth__ego(PyObject *me, PyObject *arg)
124 {
125 char *argv0;
126 if (!PyArg_ParseTuple(arg, "s:_ego", &argv0))
127 return (0);
128 if (STRCMP(QUIS, ==, "<UNNAMED>"))
129 ego(argv0);
130 RETURN_NONE;
131 }
132
133 static const PyMethodDef methods[] = {
134 #define METHNAME(func) meth_##func
135 METH (_ego, "_ego(ARGV0)")
136 #undef METHNAME
137 { 0 }
138 };
139
140 static void init_random(void)
141 {
142 #if PY_VERSION_HEX >= 0x02060000
143 char *seed;
144 uint32 r;
145
146 if (!Py_HashRandomizationFlag) return;
147 seed = getenv("PYTHONHASHSEED");
148 if (!seed || STRCMP(seed, ==, "random")) r = GR_WORD(&rand_global);
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
155 EXPORT void init_base(void)
156 {
157 PyObject *mod;
158
159 modname = PyString_FromString("catacomb");
160 addmethods(methods);
161 INIT_MODULES;
162 init_random();
163 mod = Py_InitModule("catacomb._base", donemethods());
164 INSERT_MODULES;
165 INSERT("smallprimes", smallprimes());
166 }
167
168 /*----- That's all, folks -------------------------------------------------*/