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);
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
)
100 int convmpw(PyObject
*o
, void *pp
)
105 if (!convulong(o
, &u
)) goto end
;
106 if (u
> MPW_MAX
) VALERR("out of range");
113 static PyObject
*smallprimes(void)
115 PyObject
*v
= PyList_New(NPRIME
);
118 for (i
= 0; i
< NPRIME
; i
++)
119 PyList_SET_ITEM(v
, i
, PyInt_FromLong(primetab
[i
]));
123 static PyObject
*meth__ego(PyObject
*me
, PyObject
*arg
)
126 if (!PyArg_ParseTuple(arg
, "s:_ego", &argv0
))
128 if (STRCMP(QUIS
, ==, "<UNNAMED>"))
133 static const PyMethodDef methods
[] = {
134 #define METHNAME(func) meth_##func
135 METH (_ego
, "_ego(ARGV0)")
140 static void init_random(void)
142 #if PY_VERSION_HEX >= 0x02060000
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
);
155 EXPORT
void init_base(void)
159 modname
= PyString_FromString("catacomb");
163 mod
= Py_InitModule("catacomb._base", donemethods());
165 INSERT("smallprimes", smallprimes());
168 /*----- That's all, folks -------------------------------------------------*/