Consistently make keyword-lists be static and read-only.
[pyke] / util.c
diff --git a/util.c b/util.c
index 7201401..2e6ee25 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,13 +1,11 @@
 /* -*-c-*-
  *
- * $Id$
- *
  * Miscellaneous utilities (not Catacomb-specific)
  *
  * (c) 2005 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of the Python interface to Catacomb.
  *
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * Catacomb/Python is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with Catacomb/Python; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 #include "catacomb-python.h"
 
+/*----- External values ---------------------------------------------------*/
+
+static PyObject *modname = 0;
+
 /*----- Conversions -------------------------------------------------------*/
 
 PyObject *getulong(unsigned long w)
@@ -40,6 +42,29 @@ PyObject *getulong(unsigned long w)
     return (PyLong_FromUnsignedLong(w));
 }
 
+static PyObject *i32 = 0;
+static int init_i32(void)
+  { if (!i32 && (i32 = PyInt_FromLong(32)) == 0) return (-1); return (0); }
+
+PyObject *getk64(kludge64 u)
+{
+  PyObject *i = 0, *j = 0, *t;
+  PyObject *rc = 0;
+
+  if (init_i32()) goto end;
+  if ((i = PyLong_FromUnsignedLong(HI64(u))) == 0) goto end;
+  if ((t = PyNumber_InPlaceLshift(i, i32)) == 0) goto end;
+  Py_DECREF(i); i = t;
+  if ((j = PyLong_FromUnsignedLong(LO64(u))) == 0) goto end;
+  if ((t = PyNumber_InPlaceOr(i, j)) == 0) goto end;
+  Py_DECREF(i); i = t;
+  if ((rc = PyNumber_Int(i)) == 0) goto end;
+end:
+  if (i) Py_DECREF(i);
+  if (j) Py_DECREF(j);
+  return (rc);
+}
+
 PyObject *getbool(int b)
 {
   if (b) RETURN_TRUE;
@@ -58,6 +83,7 @@ int convulong(PyObject *o, void *pp)
   unsigned long *p = pp;
   PyObject *t;
 
+  if (!o) VALERR("can't delete");
   if (PyInt_Check(o)) {
     i = PyInt_AS_LONG(o);
     if (i < 0) VALERR("must be nonnegative");
@@ -101,6 +127,29 @@ end:
   return (0);
 }
 
+int convk64(PyObject *o, void *pp)
+{
+  PyObject *i = 0, *t;
+  int rc = 0;
+  uint32 lo, hi;
+
+  if (!o) VALERR("can't delete");
+  if (init_i32()) goto end;
+  if ((i = PyNumber_Int(o)) == 0) goto end;
+  lo = PyInt_AsUnsignedLongMask(i);
+  if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end;
+  Py_DECREF(i); i = t;
+  hi = PyInt_AsUnsignedLongMask(i);
+  if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end;
+  Py_DECREF(i); i = t;
+  if (PyObject_IsTrue(i)) VALERR("out of range");
+  SET64(*(kludge64 *)pp, hi, lo);
+  rc = 1;
+end:
+  if (i) Py_DECREF(i);
+  return (rc);
+}
+
 int convmpw(PyObject *o, void *pp)
 {
   unsigned long u;
@@ -129,8 +178,11 @@ end:
 
 int convbool(PyObject *o, void *pp)
 {
+  if (!o) VALERR("can't delete");
   *(int *)pp = PyObject_IsTrue(o);
   return (1);
+end:
+  return (0);
 }
 
 /*----- Type messing ------------------------------------------------------*/
@@ -165,16 +217,22 @@ void *newtype(PyTypeObject *metaty,
     ty->ht_name = PyString_FromString(ty->ht_type.tp_name);
   if (ty->ht_name)
     ty->ht_type.tp_name = PyString_AS_STRING(ty->ht_name);
-  PyObject_INIT(&ty->ht_type, metaty);
+  DISCARD(PyObject_INIT(&ty->ht_type, metaty));
   Py_INCREF(metaty);
   return (ty);
 }
 
-PyTypeObject *inittype(PyTypeObject *tyskel)
+void typeready(PyTypeObject *ty)
 {
-  PyTypeObject *ty = newtype(&PyType_Type, tyskel, 0);
-  ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
   PyType_Ready(ty);
+  PyDict_SetItemString(ty->tp_dict, "__module__", modname);
+}
+
+PyTypeObject *inittype(PyTypeObject *tyskel, PyTypeObject *meta)
+{
+  PyTypeObject *ty = newtype(meta, tyskel, 0);
+  ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
+  typeready(ty);
   return (ty);
 }
 
@@ -218,8 +276,8 @@ PyMethodDef *donemethods(void)
 
 /*----- Exceptions --------------------------------------------------------*/
 
-PyObject * mkexc(PyObject *mod, PyObject *base,
-                const char *name, PyMethodDef *mm)
+PyObject *mkexc(PyObject *mod, PyObject *base,
+               const char *name, PyMethodDef *mm)
 {
   PyObject *nameobj = 0;
   PyObject *dict = 0;
@@ -278,7 +336,7 @@ static void iter_pydealloc(PyObject *me)
 static PyObject *itemiter_pynext(PyObject *me)
 {
   PyObject *k = 0, *v = 0, *rc = 0;
-  
+
   if ((k = PyIter_Next(ITER_I(me))) != 0 &&
       (v = PyObject_GetItem(ITER_MAP(me), k)) != 0)
     rc = Py_BuildValue("(OO)", k, v);
@@ -337,7 +395,7 @@ static PyTypeObject itemiter_pytype_skel = {
 static PyObject *valiter_pynext(PyObject *me)
 {
   PyObject *k = 0, *rc = 0;
-  
+
   if ((k = PyIter_Next(ITER_I(me))) != 0)
     rc = PyObject_GetItem(ITER_MAP(me), k);
   Py_XDECREF(k);
@@ -403,9 +461,9 @@ PySequenceMethods gmap_pysequence = {
   PyMapping_HasKey,                    /* @sq_contains@ */
   0,                                   /* @sq_inplace_concat@ */
   0                                    /* @sq_inplace_repeat@ */
-};  
+};
 
-int gmap_pysize(PyObject *me)
+Py_ssize_t gmap_pysize(PyObject *me)
 {
   PyObject *i = 0, *x = 0;
   int rc = -1;
@@ -454,7 +512,7 @@ PyObject *gmapmeth_values(PyObject *me, PyObject *arg)
       (l = PyList_New(0)) == 0 ||
       (i = PyObject_GetIter(me)) == 0)
     goto done;
-  while ((k = PyIter_Next(i)) != 0) {  
+  while ((k = PyIter_Next(i)) != 0) {
     if ((v = PyObject_GetItem(me, k)) == 0 ||
        PyList_Append(l, v))
       err = -1;
@@ -545,13 +603,15 @@ end:
   return (rc);
 }
 
-static char *def_kwlist[] = { "key", "default", 0 };
+static const char *const def_kwlist[] = { "key", "default", 0 };
 
 PyObject *gmapmeth_get(PyObject *me, PyObject *arg, PyObject *kw)
 {
   PyObject *k, *def = Py_None, *v;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:get", def_kwlist, &k, &def))
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:get",
+                                  (/*unconst*/ char **)def_kwlist,
+                                  &k, &def))
     return (0);
   if ((v = PyObject_GetItem(me, k)) != 0) return (v);
   PyErr_Clear();
@@ -563,7 +623,8 @@ PyObject *gmapmeth_setdefault(PyObject *me, PyObject *arg, PyObject *kw)
   PyObject *k, *def = Py_None, *v;
 
   if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:setdefault",
-                                  def_kwlist, &k, &def))
+                                  (/*unconst*/ char **)def_kwlist,
+                                  &k, &def))
     return (0);
   if ((v = PyObject_GetItem(me, k)) != 0) return (v);
   PyErr_Clear();
@@ -575,7 +636,9 @@ PyObject *gmapmeth_pop(PyObject *me, PyObject *arg, PyObject *kw)
 {
   PyObject *k, *def = 0, *v;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:pop", def_kwlist, &k, &def))
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:pop",
+                                  (/*unconst*/ char **)def_kwlist,
+                                  &k, &def))
     return (0);
   if ((v = PyObject_GetItem(me, k)) != 0) {
     PyObject_DelItem(me, k);
@@ -634,13 +697,14 @@ PyMethodDef gmap_pymethods[] = {
 
 /*----- Initialization ----------------------------------------------------*/
 
-void util_init(void)
+void util_pyinit(void)
 {
+  modname = PyString_FromString("catacomb");
   INITTYPE(itemiter, root);
   INITTYPE(valiter, root);
 }
 
-void util_insert(PyObject *mod)
+void util_pyinsert(PyObject *mod)
 {
   INSERT("ItemIter", itemiter_pytype);
   INSERT("ValueIter", valiter_pytype);