sgallagh / rpms / rpm

Forked from rpms/rpm 4 years ago
Clone
7d0e69f
commit 40f788a7bf3741f9c613ff302d4e1b0ceec2658c
7d0e69f
Author: Panu Matilainen <pmatilai@redhat.com>
7d0e69f
Date:   Wed Mar 24 09:53:25 2010 +0200
7d0e69f
7d0e69f
    Add __bool__() / __nonzero__() method to python rpmmi objects (ticket #153)
7d0e69f
    - Objects supporting __len__() use (len > 0) for boolean representation,
7d0e69f
      which normally makes sense but as the match iterator count is often
7d0e69f
      zero despite the iterator actually existing and returning something,
7d0e69f
      and breaks existing code (rpmlint at least)
7d0e69f
    - Adding a __bool__() (known as __nonzero__() in Python < 3) method
7d0e69f
      returning true for non-NULL iterator fixes this and gives more
7d0e69f
      meaningful answers than pre 4.8.0 which simply always returned True
7d0e69f
7d0e69f
diff --git a/python/rpmmi-py.c b/python/rpmmi-py.c
7d0e69f
index f6dd802..b7bfb1b 100644
7d0e69f
--- a/python/rpmmi-py.c
7d0e69f
+++ b/python/rpmmi-py.c
7d0e69f
@@ -137,11 +137,30 @@ static Py_ssize_t rpmmi_length(rpmmiObject * s)
7d0e69f
     return s->mi ? rpmdbGetIteratorCount(s->mi) : 0;
7d0e69f
 }
7d0e69f
 
7d0e69f
+static int rpmmi_bool(rpmmiObject *s)
7d0e69f
+{
7d0e69f
+    return (s->mi != NULL);
7d0e69f
+}
7d0e69f
+
7d0e69f
 PyMappingMethods rpmmi_as_mapping = {
7d0e69f
     (lenfunc) rpmmi_length,		/* mp_length */
7d0e69f
     0,
7d0e69f
 };
7d0e69f
 
7d0e69f
+static PyNumberMethods rpmmi_as_number = {
7d0e69f
+	0, /* nb_add */
7d0e69f
+	0, /* nb_subtract */
7d0e69f
+	0, /* nb_multiply */
7d0e69f
+	0, /* nb_divide */
7d0e69f
+	0, /* nb_remainder */
7d0e69f
+	0, /* nb_divmod */
7d0e69f
+	0, /* nb_power */
7d0e69f
+	0, /* nb_negative */
7d0e69f
+	0, /* nb_positive */
7d0e69f
+	0, /* nb_absolute */
7d0e69f
+	(inquiry)rpmmi_bool, /* nb_bool/nonzero */
7d0e69f
+};
7d0e69f
+
7d0e69f
 static char rpmmi_doc[] =
7d0e69f
 "";
7d0e69f
 
7d0e69f
@@ -156,7 +175,7 @@ PyTypeObject rpmmi_Type = {
7d0e69f
 	0,				/* tp_setattr */
7d0e69f
 	0,				/* tp_compare */
7d0e69f
 	0,				/* tp_repr */
7d0e69f
-	0,				/* tp_as_number */
7d0e69f
+	&rpmmi_as_number,		/* tp_as_number */
7d0e69f
 	0,				/* tp_as_sequence */
7d0e69f
 	&rpmmi_as_mapping,		/* tp_as_mapping */
7d0e69f
 	0,				/* tp_hash */