Merge branch '1.3.x'
[catacomb-python] / key.c
diff --git a/key.c b/key.c
index 1700259..87462ab 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;
@@ -465,9 +467,9 @@ static PyMethodDef keydata_pymethods[] = {
 #define METHNAME(func) kdmeth_##func
   METH (matchp,                "KD.matchp(FILTER) -> BOOL")
   METH (split,                 "KD.split()")
-  KWMETH(write,                        "KD.write(filter = <any>) -> STRING")
-  KWMETH(encode,               "KD.encode(filter = <any>) -> BYTES")
-  KWMETH(copy,                 "KD.copy(filter = <any>) -> KD")
+  KWMETH(write,                        "KD.write([filter = <any>]) -> STRING")
+  KWMETH(encode,               "KD.encode([filter = <any>]) -> BYTES")
+  KWMETH(copy,                 "KD.copy([filter = <any>]) -> KD")
   METH (plock,                 "KD.plock(TAG) -> ENCRYPTED-KD")
   METH (lock,                  "KD.lock(KEY) -> ENCRYPTED-KD")
 #undef METHNAME
@@ -1498,6 +1500,7 @@ static int kset_exptime(PyObject *me, PyObject *x, void *hunoz)
   key *k = KEY_K(me);
   unsigned long et;
 
+  if (!x) NIERR("__del__");
   if (!convulong(x, &et))
     goto end;
   if (!(KEY_KF(me)->f & KF_WRITE))
@@ -1514,6 +1517,7 @@ static int kset_deltime(PyObject *me, PyObject *x, void *hunoz)
   key *k = KEY_K(me);
   unsigned long dt;
 
+  if (!x) NIERR("__del__");
   if (!convulong(x, &dt))
     goto end;
   if (dt == KEXP_FOREVER && k->exp != KEXP_FOREVER)
@@ -1598,8 +1602,8 @@ static PyMethodDef key_pymethods[] = {
   METH (delete,        "KEY.delete()")
   METH (expire,        "KEY.expire()")
   METH (used,          "KEY.used(TIME)")
-  KWMETH(extract,      "KEY.extract(FILE, filter = '')")
-  KWMETH(fingerprint,  "KEY.fingerprint(HASH, filtr = '-secret')")
+  KWMETH(extract,      "KEY.extract(FILE, [filter = <any>])")
+  KWMETH(fingerprint,  "KEY.fingerprint(HASH, [filter = '-secret'])")
 #undef METHNAME
   { 0 }
 };
@@ -1989,12 +1993,13 @@ static PyObject *kfget_filep(PyObject *me, void *hunoz)
 static PyMethodDef keyfile_pymethods[] = {
 #define METHNAME(func) kfmeth_##func
   METH (save,          "KF.save()")
-  KWMETH(merge,                "KF.merge(FILE, report = <built-in-reporter>)")
-  KWMETH(newkey,       "KF.newkey(ID, TYPE, exptime = KEXP_FOREVER) -> KEY")
+  KWMETH(merge,                "KF.merge(FILE, [report = <built-in-reporter>])")
+  KWMETH(newkey,       "KF.newkey(ID, TYPE, "
+                               "[exptime = KEXP_FOREVER]) -> KEY")
   METH (byid,          "KF.byid(KEYID) -> KEY|None")
   METH (bytype,        "KF.bytype(TYPE) -> KEY|None")
   METH (bytag,         "KF.bytag(TAG) -> KEY|None")
-  KWMETH(qtag,         "KF.qtag(TAG, new = KD) -> FULLTAG, KEY, OLDKD")
+  KWMETH(qtag,         "KF.qtag(TAG, [new = KD]) -> FULLTAG, KEY, OLDKD")
   GMAP_ROMETHODS
 #undef METHNAME
   { 0 }