Sanitize python object -> tag number exception handling
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 22 Sep 2009 16:19:02 +0000 (19:19 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Tue, 22 Sep 2009 16:19:02 +0000 (19:19 +0300)
- raise exception in tagNumFromPyObject(), not in 12 different callers
- check against RPMTAG_NOT_FOUND consistently instead of -1 and whatnot
- unknown tags are value, not key or type errors

python/header-py.c
python/rpmds-py.c
python/rpmfi-py.c
python/rpmmi-py.c
python/rpmte-py.c
python/rpmts-py.c

index ebae98d..e8f4bf7 100644 (file)
@@ -333,19 +333,21 @@ static void hdr_dealloc(hdrObject * s)
     PyObject_Del(s);
 }
 
-/** \ingroup py_c
- */
 rpmTag tagNumFromPyObject (PyObject *item)
 {
-    char * str;
+    rpmTag tag = RPMTAG_NOT_FOUND;
 
     if (PyInt_Check(item)) {
-       return PyInt_AsLong(item);
+       /* XXX we should probably validate tag numbers too */
+       tag = PyInt_AsLong(item);
     } else if (PyString_Check(item)) {
-       str = PyString_AsString(item);
-       return rpmTagGetValue(str);
+       tag = rpmTagGetValue(PyString_AsString(item));
+    }
+    if (tag == RPMTAG_NOT_FOUND) {
+       PyErr_SetString(PyExc_ValueError, "unknown header tag");
     }
-    return RPMTAG_NOT_FOUND;
+       
+    return tag;
 }
 
 /** \ingroup py_c
@@ -362,10 +364,7 @@ static PyObject * hdr_subscript(hdrObject * s, PyObject * item)
     struct rpmtd_s td;
 
     tag = tagNumFromPyObject (item);
-    if (tag == RPMTAG_NOT_FOUND) {
-       PyErr_SetString(PyExc_KeyError, "unknown header tag");
-       return NULL;
-    }
+    if (tag == RPMTAG_NOT_FOUND) return NULL;
 
     tagtype = rpmTagGetType(tag); 
     forceArray = (tagtype & RPM_MASK_RETURN_TYPE) == RPM_ARRAY_RETURN_TYPE;
index 223c27d..125b3d2 100644 (file)
@@ -507,10 +507,7 @@ static PyObject * rpmds_new(PyTypeObject * subtype, PyObject *args, PyObject *kw
 
     if (to != NULL) {
        tagN = tagNumFromPyObject(to);
-       if (tagN == -1) {
-           PyErr_SetString(PyExc_KeyError, "unknown header tag");
-           return NULL;
-       }
+       if (tagN == RPMTAG_NOT_FOUND) return NULL;
     }
     s->ds = rpmdsNew(hdrGetHeader(ho), tagN, 0);
 
@@ -603,10 +600,7 @@ rpmds_Single(PyObject * s, PyObject * args, PyObject * kwds)
 
     if (to != NULL) {
        tagN = tagNumFromPyObject(to);
-       if (tagN == -1) {
-           PyErr_SetString(PyExc_KeyError, "unknown header tag");
-           return NULL;
-       }
+       if (tagN == RPMTAG_NOT_FOUND) return NULL;
     }
     if (N != NULL) N = xstrdup(N);
     if (EVR != NULL) EVR = xstrdup(EVR);
@@ -628,10 +622,7 @@ hdr_dsFromHeader(PyObject * s, PyObject * args, PyObject * kwds)
 
     if (to != NULL) {
        tagN = tagNumFromPyObject(to);
-       if (tagN == -1) {
-           PyErr_SetString(PyExc_KeyError, "unknown header tag");
-           return NULL;
-       }
+       if (tagN == RPMTAG_NOT_FOUND) return NULL;
     }
     return rpmds_Wrap( rpmdsNew(hdrGetHeader(ho), tagN, 0) );
 }
index 70bc6db..45e2d29 100644 (file)
@@ -400,10 +400,7 @@ static PyObject * rpmfi_new(PyTypeObject * subtype, PyObject *args, PyObject *kw
 
     if (to != NULL) {
        tagN = tagNumFromPyObject(to);
-       if (tagN == -1) {
-           PyErr_SetString(PyExc_KeyError, "unknown header tag");
-           return NULL;
-       }
+       if (tagN == RPMTAG_NOT_FOUND) return NULL;
     }
     s->fi = rpmfiNew(NULL, hdrGetHeader(ho), tagN, flags);
 
@@ -495,10 +492,7 @@ hdr_fiFromHeader(PyObject * s, PyObject * args, PyObject * kwds)
 
     if (to != NULL) {
        tagN = tagNumFromPyObject(to);
-       if (tagN == -1) {
-           PyErr_SetString(PyExc_KeyError, "unknown header tag");
-           return NULL;
-       }
+       if (tagN == RPMTAG_NOT_FOUND) return NULL;
     }
     return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, flags) );
 }
index 95060ab..08b02e8 100644 (file)
@@ -152,10 +152,8 @@ rpmmi_Pattern(rpmmiObject * s, PyObject * args, PyObject * kwds)
            &TagN, &type, &pattern))
        return NULL;
 
-    if ((tag = tagNumFromPyObject (TagN)) == -1) {
-       PyErr_SetString(PyExc_TypeError, "unknown tag type");
-       return NULL;
-    }
+    tag = tagNumFromPyObject (TagN);
+    if (tag == RPMTAG_NOT_FOUND) return NULL;
 
     rpmdbSetIteratorRE(s->mi, tag, type, pattern);
 
index bfafada..0a761cc 100644 (file)
@@ -193,10 +193,7 @@ rpmte_DS(rpmteObject * s, PyObject * args, PyObject * kwds)
        return NULL;
 
     tag = tagNumFromPyObject(TagN);
-    if (tag == -1) {
-       PyErr_SetString(PyExc_TypeError, "unknown tag type");
-       return NULL;
-    }
+    if (tag == RPMTAG_NOT_FOUND) return NULL;
 
     ds = rpmteDS(s->te, tag);
     if (ds == NULL) {
index 355e2f9..7fd3172 100644 (file)
@@ -1079,8 +1079,7 @@ fprintf(stderr, "*** rpmts_Match(%p) ts %p\n", s, s->ts);
            &TagN, &Key))
        return NULL;
 
-    if (TagN && (tag = tagNumFromPyObject (TagN)) == -1) {
-       PyErr_SetString(PyExc_TypeError, "unknown tag type");
+    if (TagN && (tag = tagNumFromPyObject (TagN)) == RPMTAG_NOT_FOUND) {
        return NULL;
     }