chiark / gitweb /
key.c: Reformat the rest of the `KeyError' constructor.
[catacomb-python] / key.c
diff --git a/key.c b/key.c
index 551169952835761e373b4e2ce347a332939704a2..99c25aba5d1f688365c12c2065001be0372abf9a 100644 (file)
--- a/key.c
+++ b/key.c
@@ -36,29 +36,31 @@ static PyObject *keyfilebrokenexc;
 
 static PyObject *kxmeth___init__(PyObject *me, PyObject *arg)
 {
-  int err;
+  long err;
   PyObject *x = 0;
+  Py_ssize_t n;
 
-  if (!PyArg_ParseTuple(arg, "Oi:__init__", &me, &err) ||
-      (x = PyInt_FromLong(err)) == 0 ||
-      PyObject_SetAttrString(me, "err", x))
-    goto fail;
-  Py_DECREF(x); x = 0;
-  if ((x = PyString_FromString(key_strerror(err))) == 0 ||
-      PyObject_SetAttrString(me, "errstring", x))
-    goto fail;
+  n = PyTuple_GET_SIZE(arg);
+  if (n < 2) TYERR("__init__() takes at least two arguments");
+  me = PyTuple_GET_ITEM(arg, 0);
+  err = PyInt_AsLong(PyTuple_GET_ITEM(arg, 1));
+  if (err == -1 && PyErr_Occurred()) goto end;
+  if (INT_MIN > err || err > INT_MAX) OVFERR("error code out of range");
+
+  x = PyInt_FromLong(err); if (!x) goto end;
+  if (PyObject_SetAttrString(me, "err", x)) goto end;
   Py_DECREF(x); x = 0;
-  if ((x = PyString_FromString(key_strerror(err))) == 0 ||
-      PyObject_SetAttrString(me, "errstring", x))
-    goto fail;
+
+  x = PyString_FromString(key_strerror(err)); if (!x) goto end;
+  if (PyObject_SetAttrString(me, "errstring", x)) goto end;
   Py_DECREF(x); x = 0;
-  if ((x = PySequence_GetSlice(arg, 1, PySequence_Size(arg))) == 0 ||
-      PyObject_SetAttrString(me, "args", x))
-    goto fail;
+
+  x = PyTuple_GetSlice(arg, 1, n); if (!x) goto end;
+  if (PyObject_SetAttrString(me, "args", x)) goto end;
   Py_DECREF(x); x = 0;
   RETURN_NONE;
 
-fail:
+end:
   Py_XDECREF(x);
   return (0);
 }
@@ -104,7 +106,7 @@ static PyMethodDef keyexc_pymethods[] = {
 
 static void keyexc_raise(int err)
 {
-  PyObject *arg = Py_BuildValue("(is)", err, key_strerror(err));
+  PyObject *arg = Py_BuildValue("(i)", err);
   if (arg) PyErr_SetObject(keyexc, arg);
   Py_XDECREF(arg);
 }
@@ -229,8 +231,8 @@ static int convfilter(PyObject *x, void *p)
       goto end;
     else if (n != 2)
       goto tyerr;
-    else if ((a = PySequence_GetItem(x, 0)) == 0 || convuint(a, &f->f) ||
-            (b = PySequence_GetItem(x, 1)) == 0 || convuint(b, &f->m))
+    else if ((a = PySequence_GetItem(x, 0)) == 0 || !convuint(a, &f->f) ||
+            (b = PySequence_GetItem(x, 1)) == 0 || !convuint(b, &f->m))
       goto end;
   }
   rc = 1;