pygobject r744 - in trunk: . gio tests



Author: johan
Date: Mon Jan 21 20:10:30 2008
New Revision: 744
URL: http://svn.gnome.org/viewvc/pygobject?rev=744&view=rev

Log:
2008-01-21  Johan Dahlin  <johan gnome org>

	* gio/gio.override (async_result_callback_marshal): 
	Don't assume the buffer is set, use Py_XINCREF instead of Py_INCREF.

	* gio/Makefile.am:
	* gio/ginputstream.override:
	* gio/goutputstream.override:
	* gio/gvolumemonitor.override:
	* gio/gfile.override:
	* tests/test_gio.py:
	Implement and test GFile.read_async. Use try/finally to always quit
	the mainloop, even if the test fail.
	Update source comment headers.



Added:
   trunk/gio/gfile.override
Modified:
   trunk/ChangeLog
   trunk/gio/Makefile.am
   trunk/gio/ginputstream.override
   trunk/gio/gio.override
   trunk/gio/goutputstream.override
   trunk/gio/gvolumemonitor.override
   trunk/tests/test_gio.py

Modified: trunk/gio/Makefile.am
==============================================================================
--- trunk/gio/Makefile.am	(original)
+++ trunk/gio/Makefile.am	Mon Jan 21 20:10:30 2008
@@ -35,6 +35,7 @@
 # gio module
 GIO_OVERRIDES = 			\
 	gio.override			\
+	gfile.override			\
 	ginputstream.override 		\
 	goutputstream.override 		\
 	gvolumemonitor.override

Added: trunk/gio/gfile.override
==============================================================================
--- (empty file)
+++ trunk/gio/gfile.override	Mon Jan 21 20:10:30 2008
@@ -0,0 +1,75 @@
+/* -*- Mode: C; c-basic-offset: 4 -*-
+ * pygobject - Python bindings for GObject
+ * Copyright (C) 2008  Johan Dahlin
+ *
+ *   gfile.override: module overrides for GInputStream
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+%%
+override g_file_read_async kwargs
+static PyObject *
+_wrap_g_file_read_async(PyGObject *self, PyObject *args, PyObject *kwargs)
+{
+  static char *kwlist[] = { "io_priority", "cancellable", "callback",
+                            "user_data", NULL };
+  int io_priority = G_PRIORITY_DEFAULT;
+  PyGObject *pycancellable;
+  GCancellable *cancellable;
+  PyGAsyncRequestNotify *notify;
+  
+  notify = g_slice_new0(PyGAsyncRequestNotify);
+
+  if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+                                   "iOO|O:InputStream.read_async",
+                                   kwlist,
+                                   &io_priority,
+                                   &pycancellable,
+                                   &notify->callback,
+                                   &notify->data))
+    {
+      g_slice_free(PyGAsyncRequestNotify, notify);
+      return NULL;
+    }
+
+  if ((PyObject*)pycancellable == Py_None)
+    cancellable = NULL;
+  else if (pygobject_check(pycancellable, &PyGCancellable_Type))
+      cancellable = G_CANCELLABLE(pycancellable->obj);
+  else
+    {
+      PyErr_SetString(PyExc_TypeError, "cancellable should be a gio.Cancellable");
+      return NULL;
+    }
+
+  if (!PyCallable_Check(notify->callback))
+    {
+      PyErr_SetString(PyExc_TypeError, "callback argument not callable");
+      g_slice_free(PyGAsyncRequestNotify, notify);
+      return NULL;
+    }
+  Py_INCREF(notify->callback);
+  Py_XINCREF(notify->data);
+  
+  g_file_read_async(G_FILE(self->obj),
+                    io_priority,
+                    cancellable,
+                    (GAsyncReadyCallback)async_result_callback_marshal,
+                    notify);
+  
+  Py_INCREF(Py_None);
+  return Py_None;
+}

Modified: trunk/gio/ginputstream.override
==============================================================================
--- trunk/gio/ginputstream.override	(original)
+++ trunk/gio/ginputstream.override	Mon Jan 21 20:10:30 2008
@@ -1,5 +1,5 @@
 /* -*- Mode: C; c-basic-offset: 4 -*-
- * pygtk- Python bindings for the GTK toolkit.
+ * pygobject - Python bindings for GObject
  * Copyright (C) 2008  Johan Dahlin
  *
  *   ginputstream.override: module overrides for GInputStream

Modified: trunk/gio/gio.override
==============================================================================
--- trunk/gio/gio.override	(original)
+++ trunk/gio/gio.override	Mon Jan 21 20:10:30 2008
@@ -53,10 +53,10 @@
     static GQuark quark = 0;
     
     state = pyg_gil_state_ensure();
-
+    
     if (!quark)
 	quark = g_quark_from_string("pygio::buffer");
-    Py_INCREF(notify->buffer);
+    Py_XINCREF(notify->buffer);
     g_object_set_qdata_full(G_OBJECT(result), quark,
 			    notify->buffer, py_decref_callback);
 
@@ -86,6 +86,7 @@
 }
 %%
 include
+  gfile.override
   ginputstream.override
   goutputstream.override
   gvolumemonitor.override

Modified: trunk/gio/goutputstream.override
==============================================================================
--- trunk/gio/goutputstream.override	(original)
+++ trunk/gio/goutputstream.override	Mon Jan 21 20:10:30 2008
@@ -1,5 +1,5 @@
 /* -*- Mode: C; c-basic-offset: 4 -*-
- * pygtk- Python bindings for the GTK toolkit.
+ * pygobject - Python bindings for GObject
  * Copyright (C) 2008  Johan Dahlin
  *
  *   goutputstream.override: module overrides for GOutputStream

Modified: trunk/gio/gvolumemonitor.override
==============================================================================
--- trunk/gio/gvolumemonitor.override	(original)
+++ trunk/gio/gvolumemonitor.override	Mon Jan 21 20:10:30 2008
@@ -1,5 +1,5 @@
 /* -*- Mode: C; c-basic-offset: 4 -*-
- * pygtk- Python bindings for the GTK toolkit.
+ * pygobject - Python bindings for GObject
  * Copyright (C) 2008  Johan Dahlin
  *
  *   gvolumemonitor.override: module overrides for GVolumeMonitor

Modified: trunk/tests/test_gio.py
==============================================================================
--- trunk/tests/test_gio.py	(original)
+++ trunk/tests/test_gio.py	Mon Jan 21 20:10:30 2008
@@ -6,6 +6,33 @@
 from common import gio, gobject
 
 
+class TestFile(unittest.TestCase):
+    def setUp(self):
+        self._f = open("file.txt", "w+")
+        self.file = gio.file_new_for_path("file.txt")
+
+    def tearDown(self):
+        self._f.close()
+        os.unlink("file.txt")
+
+    def testReadAsync(self):
+        self._f.write("testing")
+        self._f.seek(0)
+
+        def callback(file, result):
+            try:
+                stream = file.read_finish(result)
+                self.failUnless(isinstance(stream, gio.InputStream))
+                self.assertEquals(stream.read(), "testing")
+            finally:
+                loop.quit()
+
+        self.file.read_async(0, None, callback)
+
+        loop = gobject.MainLoop()
+        loop.run()
+
+
 class TestInputStream(unittest.TestCase):
     def setUp(self):
         self._f = open("inputstream.txt", "w+")
@@ -22,11 +49,13 @@
 
     def testReadAsync(self):
         def callback(stream, result):
-            read = stream.read_finish(result)
-            self.assertEquals(read, len("testing"))
-            self.assertEquals(result.get_buffer(), "testing")
-            stream.close()
-            loop.quit()
+            try:
+                read = stream.read_finish(result)
+                self.assertEquals(read, len("testing"))
+                self.assertEquals(result.get_buffer(), "testing")
+                stream.close()
+            finally:
+                loop.quit()
 
         self.stream.read_async(7, 0, None, callback)
 
@@ -36,12 +65,14 @@
     def testReadAsyncError(self):
         self.count = 0
         def callback(stream, result):
-            #self.assertEquals(result.get_buffer(), None)
-            self.count += 1
-            if self.count == 1:
-                return
-            self.assertRaises(gobject.GError, stream.read_finish, result)
-            loop.quit()
+            try:
+                #self.assertEquals(result.get_buffer(), None)
+                self.count += 1
+                if self.count == 1:
+                    return
+                self.assertRaises(gobject.GError, stream.read_finish, result)
+            finally:
+                loop.quit()
 
         self.stream.read_async(10240, 0, None, callback)
         self.stream.read_async(10240, 0, None, callback)



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