[pygobject] Add GIBaseInfo.equal method



commit c86b2fe8d01070f06c45fffd910d890afba1313a
Author: Simon Feltman <sfeltman src gnome org>
Date:   Fri Oct 4 13:41:08 2013 -0700

    Add GIBaseInfo.equal method
    
    Break PyGIBaseInfo rich compare into two methods: equal and richcompare.
    Equal is a direct exposure of the GI method and richcompare makes use of
    this with additional support for Pyton "==" and "!=" operators.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=709008

 gi/pygi-info.c           |   32 ++++++++++++++++++++++++--------
 tests/test_repository.py |    1 +
 2 files changed, 25 insertions(+), 8 deletions(-)
---
diff --git a/gi/pygi-info.c b/gi/pygi-info.c
index db963a0..30c6a7a 100644
--- a/gi/pygi-info.c
+++ b/gi/pygi-info.c
@@ -152,25 +152,40 @@ _base_info_repr (PyGIBaseInfo *self)
 }
 
 static PyObject *
-_base_info_richcompare (PyGIBaseInfo *self, PyObject *other, int op)
+_wrap_g_base_info_equal (PyGIBaseInfo *self, PyObject *other)
 {
-    PyObject *res;
     GIBaseInfo *other_info;
 
-    if (!PyObject_TypeCheck(other, &PyGIBaseInfo_Type)) {
-        Py_INCREF(Py_NotImplemented);
+    if (!PyObject_TypeCheck (other, &PyGIBaseInfo_Type)) {
+        Py_INCREF (Py_NotImplemented);
         return Py_NotImplemented;
     }
 
     other_info = ((PyGIBaseInfo *)other)->info;
+    if (g_base_info_equal (self->info, other_info)) {
+        Py_RETURN_TRUE;
+    } else {
+        Py_RETURN_FALSE;
+    }
+}
+
+static PyObject *
+_base_info_richcompare (PyGIBaseInfo *self, PyObject *other, int op)
+{
+    PyObject *res;
 
     switch (op) {
         case Py_EQ:
-            res = g_base_info_equal (self->info, other_info) ? Py_True : Py_False;
-            break;
+            return _wrap_g_base_info_equal (self, other);
         case Py_NE:
-            res = g_base_info_equal (self->info, other_info) ? Py_False : Py_True;
-            break;
+            res = _wrap_g_base_info_equal (self, other);
+            if (res == Py_True) {
+                Py_DECREF (res);
+                Py_RETURN_FALSE;
+            } else {
+                Py_DECREF (res);
+                Py_RETURN_TRUE;
+            }
         default:
             res = Py_NotImplemented;
             break;
@@ -270,6 +285,7 @@ static PyMethodDef _PyGIBaseInfo_methods[] = {
     { "get_name_unescaped", (PyCFunction) _wrap_g_base_info_get_name_unescaped, METH_NOARGS },
     { "get_namespace", (PyCFunction) _wrap_g_base_info_get_namespace, METH_NOARGS },
     { "get_container", (PyCFunction) _wrap_g_base_info_get_container, METH_NOARGS },
+    { "equal", (PyCFunction) _wrap_g_base_info_equal, METH_O },
     { NULL, NULL, 0 }
 };
 
diff --git a/tests/test_repository.py b/tests/test_repository.py
index 76d4417..d890c7f 100644
--- a/tests/test_repository.py
+++ b/tests/test_repository.py
@@ -78,6 +78,7 @@ class Test(unittest.TestCase):
         info2 = repo.find_by_name('GIMarshallingTests', 'Object')
         self.assertFalse(info is info2)
         self.assertEqual(info, info2)
+        self.assertTrue(info.equal(info2))
 
     def test_object_info(self):
         info = repo.find_by_name('GIMarshallingTests', 'Object')


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]