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