util.c: Replace mLib `DISCARD' with a plain `(void)' cast.
[pyke] / util.c
diff --git a/util.c b/util.c
index 29f7d12..03a39af 100644 (file)
--- a/util.c
+++ b/util.c
@@ -32,7 +32,7 @@
 
 /*----- External values ---------------------------------------------------*/
 
-static PyObject *modname = 0;
+PyObject *modname = 0;
 PyObject *home_module = 0;
 
 /*----- Conversions -------------------------------------------------------*/
@@ -68,8 +68,8 @@ PyObject *getk64(kludge64 u)
   Py_DECREF(i); i = t;
   if ((rc = PyNumber_Int(i)) == 0) goto end;
 end:
-  if (i) Py_DECREF(i);
-  if (j) Py_DECREF(j);
+  Py_XDECREF(i);
+  Py_XDECREF(j);
   return (rc);
 #endif
 }
@@ -185,7 +185,7 @@ int convk64(PyObject *o, void *pp)
   rc = 1;
 
 end:
-  if (i) Py_DECREF(i);
+  Py_XDECREF(i);
   return (rc);
 }
 
@@ -256,7 +256,7 @@ 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);
-  DISCARD(PyObject_INIT(&ty->ht_type, metaty));
+  (void)PyObject_INIT(&ty->ht_type, metaty);
   Py_INCREF(metaty);
   return (ty);
 }
@@ -293,25 +293,32 @@ void setconstants(PyObject *mod, const struct nameval *c)
 
 /*----- Building method tables --------------------------------------------*/
 
-DA_DECL(method_v, PyMethodDef);
-static method_v global_pymethods = DA_INIT;
+static PyMethodDef *global_methods;
+static size_t nmethods, methodsz;
+
 void addmethods(const PyMethodDef *m)
 {
-  size_t n;
+  size_t n, want, newsz;
 
   for (n = 0; m[n].ml_name; n++);
-  DA_ENSURE(&global_pymethods, n);
-  memcpy(DA(&global_pymethods) + DA_LEN(&global_pymethods),
-        m, n * sizeof(*m));
-  DA_EXTEND(&global_pymethods, n);
+  want = nmethods + n + 1;
+  if (want > methodsz) {
+    newsz = methodsz ? 2*methodsz : 16;
+    while (want > newsz) newsz *= 2;
+    if (!global_methods)
+      global_methods = PyObject_Malloc(newsz*sizeof(PyMethodDef));
+    else
+      global_methods = PyObject_Realloc(global_methods,
+                                       newsz*sizeof(PyMethodDef));
+    assert(global_methods);
+    methodsz = newsz;
+  }
+  memcpy(global_methods + nmethods, m, n*sizeof(PyMethodDef));
+  nmethods += n;
+  global_methods[nmethods].ml_name = 0;
 }
 
-PyMethodDef *donemethods(void)
-{
-  static const PyMethodDef mzero = { 0 };
-  DA_PUSH(&global_pymethods, mzero);
-  return (DA(&global_pymethods));
-}
+PyMethodDef *donemethods(void) { return (global_methods); }
 
 /*----- Exceptions --------------------------------------------------------*/
 
@@ -504,7 +511,7 @@ static PyTypeObject itemiter_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Iterates over the keys of a mapping.",
+  "Iterates over the items of a mapping.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -562,7 +569,7 @@ static PyTypeObject valiter_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Iterates over the values of a mapping.",
+  "Iterates over the values of a mapping.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -737,13 +744,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, "O|O:get", def_kwlist, &k, &def))
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:get",
+                                  (/*unconst*/ char **)def_kwlist,
+                                  &k, &def))
     return (0);
   if ((v = PyObject_GetItem(me, k)) != 0) return (v);
   PyErr_Clear();
@@ -755,7 +764,8 @@ PyObject *gmapmeth_setdefault(PyObject *me, PyObject *arg, PyObject *kw)
   PyObject *k, *def = Py_None, *v;
 
   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:setdefault",
-                                  def_kwlist, &k, &def))
+                                  (/*unconst*/ char **)def_kwlist,
+                                  &k, &def))
     return (0);
   if ((v = PyObject_GetItem(me, k)) != 0) return (v);
   PyErr_Clear();
@@ -767,7 +777,9 @@ PyObject *gmapmeth_pop(PyObject *me, PyObject *arg, PyObject *kw)
 {
   PyObject *k, *def = 0, *v;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:pop", def_kwlist, &k, &def))
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:pop",
+                                  (/*unconst*/ char **)def_kwlist,
+                                  &k, &def))
     return (0);
   if ((v = PyObject_GetItem(me, k)) != 0) {
     PyObject_DelItem(me, k);
@@ -840,14 +852,13 @@ static PyObject *meth__set_home_module(PyObject *me, PyObject *arg)
 
 static const PyMethodDef methods[] = {
 #define METHNAME(func) meth_##func
-  METH (_set_home_module,      "_set_home_module(MOD)")
+  METH (_set_home_module, "_set_home_module(MOD)")
 #undef METHNAME
   { 0 }
 };
 
 void util_pyinit(void)
 {
-  modname = PyString_FromString("catacomb");
   INITTYPE(itemiter, root);
   INITTYPE(valiter, root);
   addmethods(methods);