pygobject r939 - in trunk: . gio tests



Author: paulp
Date: Sun Aug 10 15:53:51 2008
New Revision: 939
URL: http://svn.gnome.org/viewvc/pygobject?rev=939&view=rev

Log:
2008-08-10  Paul Pogonyshev  <pogonyshev gmx net>

	Bug 546120 â make gio.File more Pythonic

	* gio/gfile.override (_wrap_g_file_tp_richcompare)
	(_wrap_g_file_tp_hash, _wrap_g_file_tp_repr): New functions.

	* tests/test_gio.py (TestFile.test_eq, TestFile.test_hash): New
	tests.


Modified:
   trunk/ChangeLog
   trunk/gio/gfile.override
   trunk/tests/test_gio.py

Modified: trunk/gio/gfile.override
==============================================================================
--- trunk/gio/gfile.override	(original)
+++ trunk/gio/gfile.override	Sun Aug 10 15:53:51 2008
@@ -1123,6 +1123,64 @@
     Py_INCREF(Py_None);
     return Py_None;
 }
+%%
+override-slot GFile.tp_richcompare
+static PyObject *
+_wrap_g_file_tp_richcompare(PyGObject *self, PyGObject *other, int op)
+{
+    PyObject *result;
+
+    if (PyObject_TypeCheck(self, &PyGFile_Type)
+        && PyObject_TypeCheck(other, &PyGFile_Type)) {
+        GFile *file1 = G_FILE(self->obj);
+        GFile *file2 = G_FILE(other->obj);
+
+        switch (op) {
+        case Py_EQ:
+            result = (g_file_equal(file1, file2)
+                      ? Py_True : Py_False);
+            break;
+        case Py_NE:
+            result = (!g_file_equal(file1, file2)
+                      ? Py_True : Py_False);
+            break;
+        default:
+            result = Py_NotImplemented;
+        }
+    }
+    else
+        result = Py_NotImplemented;
+
+    Py_INCREF(result);
+    return result;
+}
+%%
+override-slot GFile.tp_hash
+static long
+_wrap_g_file_tp_hash(PyGObject *self)
+{
+    return g_file_hash(G_FILE(self->obj));
+}
+%%
+override-slot GFile.tp_repr
+static PyObject *
+_wrap_g_file_tp_repr(PyGObject *self)
+{
+    char *uri = g_file_get_uri(G_FILE(self->obj));
+    gchar *representation;
+    PyObject *result;
+
+    if (uri) {
+	representation = g_strdup_printf("<%s at %p: %s>", self->ob_type->tp_name, self, uri);
+	g_free(uri);
+    }
+    else
+	representation = g_strdup_printf("<%s at %p: UNKNOWN URI>", self->ob_type->tp_name, self);
+
+    result = PyString_FromString(representation);
+    g_free(representation);
+    return result;
+}
 
 /* GFile.eject_mountable */
 /* GFile.find_enclosing_mount_async */

Modified: trunk/tests/test_gio.py
==============================================================================
--- trunk/tests/test_gio.py	(original)
+++ trunk/tests/test_gio.py	Sun Aug 10 15:53:51 2008
@@ -413,7 +413,16 @@
         loop = glib.MainLoop()
         loop.run()
 
-    
+    def test_eq(self):
+        self.assertEqual(gio.File('foo'),
+                         gio.File('foo'))
+        self.assertNotEqual(gio.File('foo'),
+                            gio.File('bar'))
+
+    def test_hash(self):
+        self.assertEquals(hash(gio.File('foo')),
+                          hash(gio.File('foo')))
+
 
 class TestGFileEnumerator(unittest.TestCase):
     def setUp(self):



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