catacomb/__init__.py (KeySZRange.pad): Return correct value.
[catacomb-python] / key.c
diff --git a/key.c b/key.c
index 57d01f9..87462ab 100644 (file)
--- a/key.c
+++ b/key.c
@@ -1,13 +1,11 @@
 /* -*-c-*-
  *
- * $Id$
- *
  * Key files and data
  *
  * (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.
@@ -38,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);
 }
@@ -231,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;
@@ -270,7 +270,7 @@ static int convflags(PyObject *x, void *p)
   rc = 1;
   goto end;
 tyerr:
-  TYERR("expected flag string or flag/mask pair");
+  TYERR("expected flag string or integer bitfield");
 end:
   return (rc);
 }
@@ -336,7 +336,7 @@ static void keydata_pydealloc(PyObject *me)
 static PyObject *kdmeth_matchp(PyObject *me, PyObject *arg)
 {
   key_filter f;
-  
+
   if (!PyArg_ParseTuple(arg, "O&:matchp", convfilter, &f))
     return (0);
   return (getbool(KEY_MATCH(KEYDATA_KD(me), &f)));
@@ -350,14 +350,29 @@ static PyObject *kdmeth_split(PyObject *me, PyObject *arg)
   RETURN_ME;
 }
 
+static PyObject *kdmeth_copy(PyObject *me, PyObject *arg, PyObject *kw)
+{
+  key_filter f = { 0, 0 };
+  static const char *const kwlist[] = { "filter", 0 };
+  key_data *kd;
+
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:copy", KWLIST,
+                                  convfilter, &f))
+    return (0);
+  if ((kd = key_copydata(KEYDATA_KD(me), &f)) == 0)
+    RETURN_NONE;
+  else
+    return (keydata_pywrap(kd));
+}
+
 static PyObject *kdmeth_write(PyObject *me, PyObject *arg, PyObject *kw)
 {
   key_filter f = { 0, 0 };
   dstr d = DSTR_INIT;
   PyObject *rc = 0;
-  static char *kwlist[] = { "filter", 0 };
+  static const char *const kwlist[] = { "filter", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:write", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:write", KWLIST,
                                   convfilter, &f))
     return (0);
   key_write(KEYDATA_KD(me), &d, &f);
@@ -371,9 +386,9 @@ static PyObject *kdmeth_encode(PyObject *me, PyObject *arg, PyObject *kw)
   key_filter f = { 0, 0 };
   dstr d = DSTR_INIT;
   PyObject *rc = 0;
-  static char *kwlist[] = { "filter", 0 };
+  static const char *const kwlist[] = { "filter", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:encode", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:encode", KWLIST,
                                   convfilter, &f))
     return (0);
   key_encode(KEYDATA_KD(me), &d, &f);
@@ -388,7 +403,7 @@ static PyObject *kdmeth_plock(PyObject *me, PyObject *arg)
   int err;
   PyObject *rc = 0;
   key_data *kd;
-  
+
   if (!PyArg_ParseTuple(arg, "s:plock", &tag))
     goto end;
   if ((err = key_plock(&kd, KEYDATA_KD(me), tag)) != 0)
@@ -401,10 +416,10 @@ end:
 static PyObject *kdmeth_lock(PyObject *me, PyObject *arg)
 {
   char *p;
-  int n;
+  Py_ssize_t n;
   PyObject *rc = 0;
   key_data *kd;
-  
+
   if (!PyArg_ParseTuple(arg, "s#:lock", &p, &n))
     goto end;
   key_lock(&kd, KEYDATA_KD(me), p, n);
@@ -432,7 +447,7 @@ end:
 static PyObject *meth__KeyData_decode(PyObject *me, PyObject *arg)
 {
   const char *p;
-  int n;
+  Py_ssize_t n;
   key_data *kd;
   PyObject *rc = 0;
 
@@ -440,7 +455,7 @@ static PyObject *meth__KeyData_decode(PyObject *me, PyObject *arg)
     goto end;
   if ((kd = key_decode(p, n)) == 0)
     KEYERR(KERR_MALFORMED);
-  rc =  keydata_pywrap(kd);
+  rc = keydata_pywrap(kd);
 end:
   return (rc);
 }
@@ -452,8 +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(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
@@ -469,7 +485,7 @@ static PyGetSetDef keydata_pygetset[] = {
 
 static PyTypeObject keydata_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeyData",                  /* @tp_name@ */
+  "KeyData",                           /* @tp_name@ */
   sizeof(keydata_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -519,12 +535,12 @@ static PyObject *keydatabin_pynew(PyTypeObject *ty,
                                  PyObject *arg, PyObject *kw)
 {
   char *p;
-  int n;
+  Py_ssize_t n;
   unsigned f = 0;
   keydata_pyobj *me = 0;
-  static char *kwlist[] = { "key", "flags", 0 };
+  static const char *const kwlist[] = { "key", "flags", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:new", KWLIST,
                                   &p, &n, convflags, &f))
     goto end;
   me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
@@ -546,7 +562,7 @@ static PyGetSetDef keydatabin_pygetset[] = {
 
 static PyTypeObject keydatabin_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeyDataBinary",            /* @tp_name@ */
+  "KeyDataBinary",                     /* @tp_name@ */
   sizeof(keydata_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -569,7 +585,7 @@ static PyTypeObject keydatabin_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Key data for binary keys.",
+"KeyDataBinary(KEY, [flags = 0]): key data for binary keys.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -596,12 +612,12 @@ static PyObject *keydataenc_pynew(PyTypeObject *ty,
                                  PyObject *arg, PyObject *kw)
 {
   char *p;
-  int n;
+  Py_ssize_t n;
   unsigned f = 0;
   keydata_pyobj *me = 0;
-  static char *kwlist[] = { "key", "flags", 0 };
+  static const char *const kwlist[] = { "key", "flags", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:new", KWLIST,
                                   &p, &n, convflags, &f))
     goto end;
   me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
@@ -622,7 +638,7 @@ end:
 static PyObject *kdemeth_lock(PyObject *me, PyObject *arg)
 {
   char *hunoz;
-  int hukairz;
+  Py_ssize_t hukairz;
   if (!PyArg_ParseTuple(arg, "s#:lock", &hunoz, &hukairz)) goto end;
   KEYERR(KERR_WRONGTYPE);
 end:
@@ -635,7 +651,7 @@ static PyObject *kdemeth_punlock(PyObject *me, PyObject *arg)
   int err;
   PyObject *rc = 0;
   key_data *kd;
-  
+
   if (!PyArg_ParseTuple(arg, "s:punlock", &tag))
     goto end;
   if ((err = key_punlock(&kd, KEYDATA_KD(me), tag)) != 0)
@@ -648,11 +664,11 @@ end:
 static PyObject *kdemeth_unlock(PyObject *me, PyObject *arg)
 {
   char *p;
-  int n;
+  Py_ssize_t n;
   int err;
   PyObject *rc = 0;
   key_data *kd;
-  
+
   if (!PyArg_ParseTuple(arg, "s#:unlock", &p, &n))
     goto end;
   if ((err = key_unlock(&kd, KEYDATA_KD(me), p, n)) != 0)
@@ -683,7 +699,7 @@ static PyGetSetDef keydataenc_pygetset[] = {
 
 static PyTypeObject keydataenc_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeyDataEncrypted",         /* @tp_name@ */
+  "KeyDataEncrypted",                  /* @tp_name@ */
   sizeof(keydata_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -706,7 +722,7 @@ static PyTypeObject keydataenc_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Key data for encrypted keys.",
+"KeyDataEncrypted(KEY, [flags = 0]): key data for encrypted keys.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -735,9 +751,9 @@ static PyObject *keydatamp_pynew(PyTypeObject *ty,
   mp *x = 0;
   unsigned f = 0;
   keydata_pyobj *me = 0;
-  static char *kwlist[] = { "key", "flags", 0 };
+  static const char *const kwlist[] = { "key", "flags", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST,
                                   convmp, &x, convflags, &f))
     goto end;
   me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
@@ -759,7 +775,7 @@ static PyGetSetDef keydatamp_pygetset[] = {
 
 static PyTypeObject keydatamp_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeyDataMP",                        /* @tp_name@ */
+  "KeyDataMP",                         /* @tp_name@ */
   sizeof(keydata_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -782,7 +798,7 @@ static PyTypeObject keydatamp_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Key data for large-integer keys.",
+"KeyDataMP(KEY, [flags = 0]): key data for large-integer keys.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -811,9 +827,9 @@ static PyObject *keydatastr_pynew(PyTypeObject *ty,
   char *p;
   unsigned f = 0;
   keydata_pyobj *me = 0;
-  static char *kwlist[] = { "key", "flags", 0 };
+  static const char *const kwlist[] = { "key", "flags", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:new", KWLIST,
                                   &p, convflags, &f))
     goto end;
   me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
@@ -834,7 +850,7 @@ static PyGetSetDef keydatastr_pygetset[] = {
 
 static PyTypeObject keydatastr_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeyDataString",            /* @tp_name@ */
+  "KeyDataString",                     /* @tp_name@ */
   sizeof(keydata_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -857,7 +873,7 @@ static PyTypeObject keydatastr_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Key data for string keys.",
+"KeyDataString(KEY, [flags = 0]): key data for string keys.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -886,9 +902,9 @@ static PyObject *keydataec_pynew(PyTypeObject *ty,
   ec x = EC_INIT;
   unsigned f = 0;
   keydata_pyobj *me = 0;
-  static char *kwlist[] = { "key", "flags", 0 };
+  static const char *const kwlist[] = { "key", "flags", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST,
                                   convecpt, &x, convflags, &f))
     goto end;
   me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
@@ -914,7 +930,7 @@ static PyGetSetDef keydataec_pygetset[] = {
 
 static PyTypeObject keydataec_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeyDataECPt",              /* @tp_name@ */
+  "KeyDataECPt",                       /* @tp_name@ */
   sizeof(keydata_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -937,7 +953,7 @@ static PyTypeObject keydataec_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Key data for elliptic-curve keys.",
+"KeyDataECPt(KEY, [flags = 0]): key data for elliptic-curve keys.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -985,7 +1001,7 @@ static void subkeyiter_pydealloc(PyObject *me)
 
 static PyTypeObject subkeyiter_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.SubKeyIter",               /* @tp_name@ */
+  "SubKeyIter",                                /* @tp_name@ */
   sizeof(subkeyiter_pyobj),            /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1039,10 +1055,10 @@ static PyObject *keydatastruct_pynew(PyTypeObject *ty,
   char *p;
   keydata_pyobj *me = 0;
   key_data *kd = 0;
-  static char *kwlist[] = { "subkeys", 0 };
+  static const char *const kwlist[] = { "subkeys", 0 };
 
   Py_XINCREF(arg); Py_XINCREF(kw);
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:new", kwlist, &sub))
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:new", KWLIST, &sub))
     goto end;
   kd = key_newstruct();
   if (sub) {
@@ -1097,6 +1113,7 @@ static int keydatastruct_pystore(PyObject *me,
 
   if ((tag = PyString_AsString(key)) == 0)
     goto end;
+  key_split(&KEYDATA_KD(me));
   if (value) {
     if (!KEYDATA_PYCHECK(value))
       TYERR("expected KeyData value");
@@ -1119,7 +1136,7 @@ static PyMappingMethods keydatastruct_pymapping = {
 
 static PyTypeObject keydatastruct_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeyDataStructured",                /* @tp_name@ */
+  "KeyDataStructured",                 /* @tp_name@ */
   sizeof(keydata_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1142,7 +1159,7 @@ static PyTypeObject keydatastruct_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Key data for structured keys.",
+"KeyDataStructured([subkeys = []]): key data for structured keys.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -1195,7 +1212,7 @@ static void keyattriter_pydealloc(PyObject *me)
 
 static PyTypeObject keyattriter_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeyAttributeIter",         /* @tp_name@ */
+  "KeyAttributeIter",                  /* @tp_name@ */
   sizeof(keyattriter_pyobj),           /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1305,7 +1322,7 @@ static PyMappingMethods keyattrs_pymapping = {
 
 static PyTypeObject keyattrs_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeyAttributes",            /* @tp_name@ */
+  "KeyAttributes",                     /* @tp_name@ */
   sizeof(keyattrs_pyobj),              /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1370,16 +1387,17 @@ static PyObject *key_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
   PyObject *kfobj;
   uint32 id;
   char *type;
-  long exptime = KEXP_FOREVER;
-  static char *kwlist[] = { "keyfile", "id", "type", "exptime", 0 };
+  unsigned long exptime = KEXP_FOREVER;
+  static const char *const kwlist[] =
+    { "keyfile", "id", "type", "exptime", 0 };
   key *k;
   int err;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&sl:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&s|O&:new", KWLIST,
                                   keyfile_pytype, &kfobj, convu32, &id,
-                                  &type, &exptime))
+                                  &type, convulong, &exptime))
     goto end;
-  if ((err = key_new(KEYFILE_KF(kfobj), id, type, exptime, &k)) == 0)
+  if ((err = key_new(KEYFILE_KF(kfobj), id, type, exptime, &k)) != 0)
     KEYERR(err);
   return (key_dowrap(ty, kfobj, k));
 end:
@@ -1395,7 +1413,7 @@ static void key_pydealloc(PyObject *me)
 static PyObject *kmeth_delete(PyObject *me, PyObject *arg)
 {
   int err;
-  
+
   if (!PyArg_ParseTuple(arg, ":delete")) goto end;
   if ((err = key_delete(KEY_KF(me), KEY_K(me))) != 0) KEYERR(err);
   RETURN_ME;
@@ -1406,7 +1424,7 @@ end:
 static PyObject *kmeth_expire(PyObject *me, PyObject *arg)
 {
   int err;
-  
+
   if (!PyArg_ParseTuple(arg, ":expire")) goto end;
   if ((err = key_expire(KEY_KF(me), KEY_K(me))) != 0) KEYERR(err);
   RETURN_ME;
@@ -1418,7 +1436,7 @@ static PyObject *kmeth_used(PyObject *me, PyObject *arg)
 {
   long t;
   int err;
-  
+
   if (!PyArg_ParseTuple(arg, "l:used", &t)) goto end;
   if ((err = key_used(KEY_KF(me), KEY_K(me), t)) != 0) KEYERR(err);
   RETURN_ME;
@@ -1433,9 +1451,9 @@ static PyObject *kmeth_extract(PyObject *me, PyObject *arg, PyObject *kw)
   PyObject *nameobj;
   char *name;
   FILE *fp;
-  static char *kwlist[] = { "file", "filter", 0 };
+  static const char *const kwlist[] = { "file", "filter", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O&:extract", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O&:extract", KWLIST,
                                   &PyFile_Type, &file,
                                   convfilter, &f) ||
       (fp = PyFile_AsFile(file)) == 0 ||
@@ -1454,9 +1472,9 @@ static PyObject *kmeth_fingerprint(PyObject *me,
 {
   ghash *h;
   key_filter f = { KF_NONSECRET, KF_NONSECRET };
-  static char *kwlist[] = { "hash", "filter", 0 };
+  static const char *const kwlist[] = { "hash", "filter", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:fingerprint", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:fingerprint", KWLIST,
                                   convghash, &h, convfilter, &f))
     return (0);
   return (getbool(key_fingerprint(KEY_K(me), h, &f)));
@@ -1482,7 +1500,8 @@ static int kset_exptime(PyObject *me, PyObject *x, void *hunoz)
   key *k = KEY_K(me);
   unsigned long et;
 
-  if ((et = PyLong_AsUnsignedLong(x)) == (unsigned long)-1 && PyErr_Occurred())
+  if (!x) NIERR("__del__");
+  if (!convulong(x, &et))
     goto end;
   if (!(KEY_KF(me)->f & KF_WRITE))
     KEYERR(KERR_READONLY);
@@ -1498,7 +1517,8 @@ static int kset_deltime(PyObject *me, PyObject *x, void *hunoz)
   key *k = KEY_K(me);
   unsigned long dt;
 
-  if ((dt = PyLong_AsUnsignedLong(x)) == (unsigned long)-1 && PyErr_Occurred())
+  if (!x) NIERR("__del__");
+  if (!convulong(x, &dt))
     goto end;
   if (dt == KEXP_FOREVER && k->exp != KEXP_FOREVER)
     VALERR("key will eventually expire");
@@ -1582,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 }
 };
@@ -1607,7 +1627,7 @@ static PyGetSetDef key_pygetset[] = {
 
 static PyTypeObject key_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.Key",                      /* @tp_name@ */
+  "Key",                               /* @tp_name@ */
   sizeof(key_pyobj),                   /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1630,7 +1650,7 @@ static PyTypeObject key_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Key object.",
+"Key(KF, ID, TYPE, [exptime = KEXP_FOREVER]): key object.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -1670,7 +1690,7 @@ static PyObject *keyiter_pynext(PyObject *me)
 
   if ((k = key_next(KEYITER_I(me))) == 0)
     return (0);
-  return (key_pywrap(KEYITER_KFOBJ(me), k));
+  return (getulong(k->id));
 }
 
 static void keyiter_pydealloc(PyObject *me)
@@ -1681,7 +1701,7 @@ static void keyiter_pydealloc(PyObject *me)
 
 static PyTypeObject keyiter_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeyFileIter",              /* @tp_name@ */
+  "KeyFileIter",                       /* @tp_name@ */
   sizeof(keyiter_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1758,10 +1778,10 @@ static PyObject *keyfile_pynew(PyTypeObject *ty,
   char *file = 0;
   unsigned how = KOPEN_READ;
   keyfile_pyobj *rc = 0;
-  static char *kwlist[] = { "file", "how", "report", 0 };
+  static const char *const kwlist[] = { "file", "how", "report", 0 };
 
   Py_XINCREF(arg); Py_XINCREF(kw);
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|iO:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|iO:new", KWLIST,
                                   &file, &how, &ri.func))
     goto end;
   if (ri.func && !PyCallable_Check(ri.func))
@@ -1817,10 +1837,10 @@ static PyObject *kfmeth_merge(PyObject *me, PyObject *arg, PyObject *kw)
   PyObject *x = 0;
   FILE *fp = 0;
   int rc;
-  static char *kwlist[] = { "file", "report", 0 };
+  static const char *const kwlist[] = { "file", "report", 0 };
 
   Py_XINCREF(arg); Py_XINCREF(kw);
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O:merge", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O:merge", KWLIST,
                                   &PyFile_Type, &x, &ri.func))
     goto end;
   if (ri.func && !PyCallable_Check(ri.func))
@@ -1909,21 +1929,21 @@ static PyObject *keyfile_pylookup(PyObject *me, PyObject *key)
   }
 end:
   return (rc);
-}  
+}
 
 static PyObject *kfmeth_newkey(PyObject *me, PyObject *arg, PyObject *kw)
 {
   uint32 id;
   char *type;
   long exptime = KEXP_FOREVER;
-  static char *kwlist[] = { "id", "type", "exptime", 0 };
+  static const char *const kwlist[] = { "id", "type", "exptime", 0 };
   key *k;
   int err;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&sl:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&s|l:newkey", KWLIST,
                                   convu32, &id, &type, &exptime))
     goto end;
-  if ((err = key_new(KEYFILE_KF(me), id, type, exptime, &k)) == 0)
+  if ((err = key_new(KEYFILE_KF(me), id, type, exptime, &k)) != 0)
     KEYERR(err);
   return (key_pywrap(me, k));
 end:
@@ -1938,9 +1958,9 @@ static PyObject *kfmeth_qtag(PyObject *me, PyObject *arg, PyObject *kw)
   char *tag;
   dstr d = DSTR_INIT;
   PyObject *rc = 0;
-  static char *kwlist[] = { "tag", "new", 0 };
+  static const char *const kwlist[] = { "tag", "new", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O!:qtag", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O!:qtag", KWLIST,
                                   &tag, keydata_pytype, &newkdobj))
     goto end;
   if (key_qtag(KEYFILE_KF(me), tag, &d, &k, &kd))
@@ -1954,7 +1974,7 @@ static PyObject *kfmeth_qtag(PyObject *me, PyObject *arg, PyObject *kw)
   }
   key_incref(*kd);
   rc = Py_BuildValue("(s#NN)",
-                    d.buf, d.len,
+                    d.buf, (Py_ssize_t)d.len,
                     key_pywrap(me, k),
                     keydata_pywrap(okd));
 end:
@@ -1973,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 }
@@ -2002,7 +2023,7 @@ static PyMappingMethods keyfile_pymapping = {
 
 static PyTypeObject keyfile_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeyFile",                  /* @tp_name@ */
+  "KeyFile",                           /* @tp_name@ */
   sizeof(keyfile_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -2025,7 +2046,8 @@ static PyTypeObject keyfile_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Keyring file.",
+"KeyFile(FILE, [how = KOPEN_READ], [report = ?]): Keyring file.\n\
+   calls REPORT(FILE, LINE, MSG) on problems",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -2066,7 +2088,7 @@ static PyMethodDef methods[] = {
           "KeyData.readflags(STRING) -> (FLAGS, MASK, REST)")
   METH (_KeyData_writeflags,   "KeyData.writeflags(FLAGS) -> STRING")
   METH (_KeyData_read,         "KeyData.read(STRING) -> (KD, REST)")
-  METH (_KeyData_decode,       "KeyData.read(BYTES) -> KD")
+  METH (_KeyData_decode,       "KeyData.decode(BYTES) -> KD")
   METH (barf,                  "barf(ERR)")
 #undef METHNAME
   { 0 }