key.c: Parse `KeyError' constructor arguments by hand.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 24 Nov 2019 16:25:18 +0000 (16:25 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 27 Nov 2019 15:10:44 +0000 (15:10 +0000)
This allows additional arguments to be stored, as is conventional for
exception classes.

key.c

diff --git a/key.c b/key.c
index 4995035..d91651a 100644 (file)
--- a/key.c
+++ b/key.c
@@ -36,16 +36,21 @@ static PyObject *keyfilebrokenexc;
 
 static PyObject *kxmeth___init__(PyObject *me, PyObject *arg)
 {
-  int err;
+  long err;
   PyObject *x = 0;
   Py_ssize_t n;
 
   n = PyTuple_GET_SIZE(arg);
-  if (!PyArg_ParseTuple(arg, "Oi:__init__", &me, &err) ||
-      (x = PyInt_FromLong(err)) == 0 ||
-      PyObject_SetAttrString(me, "err", x))
-    goto end;
+  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 end;