Push hdrCheck() error code handling over to python side
authorPanu Matilainen <pmatilai@redhat.com>
Mon, 5 Oct 2009 11:55:37 +0000 (14:55 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Mon, 5 Oct 2009 11:55:37 +0000 (14:55 +0300)
- return (rpmrc, message) tuple from C to let python do whatever it
  wishes with the information
- let python side worry about generating backwards compatible returns

python/rpm/transaction.py
python/rpmts-py.c

index c852b86..07a8db0 100644 (file)
@@ -138,3 +138,13 @@ class TransactionSet(_rpm.ts):
             res.append(((n, v, r),(needname,needver),needflags,sense,p.key))
 
         return res
+
+    def hdrCheck(self, blob):
+        res, msg = _rpm.ts.hdrCheck(self, blob)
+        # generate backwards compatibly broken exceptions
+        if res == _rpm.RPMRC_NOKEY:
+            raise _rpm.error, "public key not availaiable"
+        elif res == _rpm.RPMRC_NOTTRUSTED:
+            raise _rpm.error, "public key not trusted"
+        elif res != _rpm.RPMRC_OK:
+            raise _rpm.error, msg
index 5d6bf8f..208de9a 100644 (file)
@@ -374,46 +374,25 @@ rpmts_HdrFromFdno(rpmtsObject * s, PyObject * args, PyObject * kwds)
 }
 
 static PyObject *
-rpmts_HdrCheck(rpmtsObject * s, PyObject * args, PyObject * kwds)
+rpmts_HdrCheck(rpmtsObject * s, PyObject *obj)
 {
     PyObject * blob;
-    PyObject * result = NULL;
     char * msg = NULL;
     const void * uh;
     int uc;
     rpmRC rpmrc;
-    char * kwlist[] = {"headers", NULL};
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:HdrCheck", kwlist, &blob))
+    if (!PyArg_Parse(obj, "S:HdrCheck", &blob))
        return NULL;
 
     uh = PyString_AsString(blob);
     uc = PyString_Size(blob);
 
+    Py_BEGIN_ALLOW_THREADS;
     rpmrc = headerCheck(s->ts, uh, uc, &msg);
+    Py_END_ALLOW_THREADS;
 
-    switch (rpmrc) {
-    case RPMRC_OK:
-       Py_INCREF(Py_None);
-       result = Py_None;
-       break;
-
-    case RPMRC_NOKEY:
-       PyErr_SetString(pyrpmError, "public key not availaiable");
-       break;
-
-    case RPMRC_NOTTRUSTED:
-       PyErr_SetString(pyrpmError, "public key not trusted");
-       break;
-
-    case RPMRC_FAIL:
-    default:
-       PyErr_SetString(pyrpmError, msg);
-       break;
-    }
-    msg = _free(msg);
-
-    return result;
+    return Py_BuildValue("(is)", rpmrc, msg);
 }
 
 static PyObject *
@@ -663,7 +642,7 @@ static struct PyMethodDef rpmts_methods[] = {
  {"hdrFromFdno",(PyCFunction) rpmts_HdrFromFdno,METH_VARARGS|METH_KEYWORDS,
 "ts.hdrFromFdno(fdno) -> hdr\n\
 - Read a package header from a file descriptor.\n" },
- {"hdrCheck",  (PyCFunction) rpmts_HdrCheck,   METH_VARARGS|METH_KEYWORDS,
+ {"hdrCheck",  (PyCFunction) rpmts_HdrCheck,   METH_O,
        NULL },
  {"pgpPrtPkts",        (PyCFunction) rpmts_PgpPrtPkts, METH_VARARGS|METH_KEYWORDS,
        NULL },