summaryrefslogtreecommitdiff
path: root/python/rpmmi-py.c
diff options
context:
space:
mode:
authorPanu Matilainen <pmatilai@redhat.com>2010-03-24 09:53:25 +0200
committerPanu Matilainen <pmatilai@redhat.com>2010-03-24 09:53:25 +0200
commit40f788a7bf3741f9c613ff302d4e1b0ceec2658c (patch)
treec65a9b13e7b019aad4417ca56c6567078cced81d /python/rpmmi-py.c
parent8c7e53ec80e84f48bfc67181f3d5dd81ecdb7523 (diff)
downloadlibrpm-tizen-40f788a7bf3741f9c613ff302d4e1b0ceec2658c.tar.gz
librpm-tizen-40f788a7bf3741f9c613ff302d4e1b0ceec2658c.tar.bz2
librpm-tizen-40f788a7bf3741f9c613ff302d4e1b0ceec2658c.zip
Add __bool__() / __nonzero__() method to python rpmmi objects (ticket #153)
- Objects supporting __len__() use (len > 0) for boolean representation, which normally makes sense but as the match iterator count is often zero despite the iterator actually existing and returning something, and breaks existing code (rpmlint at least) - Adding a __bool__() (known as __nonzero__() in Python < 3) method returning true for non-NULL iterator fixes this and gives more meaningful answers than pre 4.8.0 which simply always returned True
Diffstat (limited to 'python/rpmmi-py.c')
-rw-r--r--python/rpmmi-py.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/python/rpmmi-py.c b/python/rpmmi-py.c
index f6dd80271..b7bfb1b5d 100644
--- a/python/rpmmi-py.c
+++ b/python/rpmmi-py.c
@@ -137,11 +137,30 @@ static Py_ssize_t rpmmi_length(rpmmiObject * s)
return s->mi ? rpmdbGetIteratorCount(s->mi) : 0;
}
+static int rpmmi_bool(rpmmiObject *s)
+{
+ return (s->mi != NULL);
+}
+
PyMappingMethods rpmmi_as_mapping = {
(lenfunc) rpmmi_length, /* mp_length */
0,
};
+static PyNumberMethods rpmmi_as_number = {
+ 0, /* nb_add */
+ 0, /* nb_subtract */
+ 0, /* nb_multiply */
+ 0, /* nb_divide */
+ 0, /* nb_remainder */
+ 0, /* nb_divmod */
+ 0, /* nb_power */
+ 0, /* nb_negative */
+ 0, /* nb_positive */
+ 0, /* nb_absolute */
+ (inquiry)rpmmi_bool, /* nb_bool/nonzero */
+};
+
static char rpmmi_doc[] =
"";
@@ -156,7 +175,7 @@ PyTypeObject rpmmi_Type = {
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
- 0, /* tp_as_number */
+ &rpmmi_as_number, /* tp_as_number */
0, /* tp_as_sequence */
&rpmmi_as_mapping, /* tp_as_mapping */
0, /* tp_hash */