pygobject r753 - in trunk: . gio tests



Author: johan
Date: Sat Mar 22 18:14:01 2008
New Revision: 753
URL: http://svn.gnome.org/viewvc/pygobject?rev=753&view=rev

Log:
2008-03-22  Johan Dahlin  <johan gnome org>

        * gio/ginputstream.override:
        * gio/gio.override:
        * tests/test_gio.py:

        Make read_finish() return the string, remove the get_buffer method.
        This is more pythonic, as it mimics the normal read() behavior of 
        python.
        Update the tests and use a separate marshaller for read.



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

Modified: trunk/gio/ginputstream.override
==============================================================================
--- trunk/gio/ginputstream.override	(original)
+++ trunk/gio/ginputstream.override	Sat Mar 22 18:14:01 2008
@@ -19,6 +19,64 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  * USA
  */
+#define BUFSIZE 8192
+
+typedef struct {
+    PyObject *callback;
+    PyObject *data;
+    PyObject *buffer;
+} PyGAsyncRequestNotifyRead;
+
+static void
+py_decref_callback (gpointer data)
+{
+    Py_DECREF((PyObject*)data);
+}
+
+static void
+async_result_callback_marshal_read(GObject *source_object, 
+				   GAsyncResult *result, 
+				   PyGAsyncRequestNotifyRead *notify)
+{
+    PyObject *ret;
+    PyGILState_STATE state;
+    static GQuark quark = 0;
+    
+    state = pyg_gil_state_ensure();
+
+    /* buffer is only used by read_async */
+    if (notify->buffer) {
+	if (!quark)
+	    quark = g_quark_from_string("pygio::buffer");
+	Py_XINCREF(notify->buffer);
+	g_object_set_qdata_full(G_OBJECT(result), quark,
+				notify->buffer, py_decref_callback);
+    }
+    
+    if (notify->data)
+	ret = PyEval_CallFunction(notify->callback, "(OOO)", 
+				  pygobject_new(source_object), 
+				  pygobject_new((GObject *)result),
+				  notify->data);
+    else
+	ret = PyObject_CallFunction(notify->callback, "(OO)", 
+				    pygobject_new(source_object), 
+				    pygobject_new((GObject *)result));
+
+    if (ret == NULL)
+      {
+	PyErr_Print();
+	PyErr_Clear();
+      }
+
+    Py_XDECREF(ret);
+
+    Py_DECREF(notify->callback);
+    Py_XDECREF(notify->data);
+    g_slice_free(PyGAsyncRequestNotifyRead, notify);
+
+    pyg_gil_state_release(state);
+}
 %%
 override g_input_stream_read kwargs
 static PyObject *
@@ -103,9 +161,9 @@
   GCancellable *cancellable;
 
 
-  PyGAsyncRequestNotify *notify;
+  PyGAsyncRequestNotifyRead *notify;
   
-  notify = g_slice_new0(PyGAsyncRequestNotify);
+  notify = g_slice_new0(PyGAsyncRequestNotifyRead);
 
   if (!PyArg_ParseTupleAndKeywords(args, kwargs,
 				   "liOO|O:InputStream.read_async",
@@ -115,7 +173,7 @@
 				   &notify->callback,
 				   &notify->data))
     {
-      g_slice_free(PyGAsyncRequestNotify, notify);
+      g_slice_free(PyGAsyncRequestNotifyRead, notify);
       return NULL;
     }
 
@@ -132,7 +190,7 @@
   if (!PyCallable_Check(notify->callback))
     {
       PyErr_SetString(PyExc_TypeError, "callback argument not callable");
-      g_slice_free(PyGAsyncRequestNotify, notify);
+      g_slice_free(PyGAsyncRequestNotifyRead, notify);
       return NULL;
     }
   Py_INCREF(notify->callback);
@@ -147,9 +205,38 @@
 			    count,
 			    io_priority,
 			    cancellable,
-			    (GAsyncReadyCallback)async_result_callback_marshal,
+			    (GAsyncReadyCallback)async_result_callback_marshal_read,
 			    notify);
   
   Py_INCREF(Py_None);
   return Py_None;
 }
+%%
+override g_input_stream_read_finish kwargs
+static PyObject *
+_wrap_g_input_stream_read_finish(PyGObject *self, PyObject *args, PyObject *kwargs)
+{
+    static char *kwlist[] = { "result", NULL };
+    PyGObject *result;
+    GError *error = NULL;
+    static GQuark quark = 0;
+    PyObject *buffer;
+    
+    if (!quark)
+	quark = g_quark_from_string("pygio::buffer");
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+				     "O!:GInputStream.read_finish",
+				     kwlist, &PyGAsyncResult_Type, &result))
+        return NULL;
+    
+
+    g_input_stream_read_finish(G_INPUT_STREAM(self->obj), G_ASYNC_RESULT(result->obj), &error);
+    
+    if (pyg_error_check(&error))
+        return NULL;
+
+    buffer = g_object_get_qdata(G_OBJECT(result->obj), quark);
+    /* FIXME: Should we refcount the buffer here? */
+    return buffer;
+}

Modified: trunk/gio/gio.override
==============================================================================
--- trunk/gio/gio.override	(original)
+++ trunk/gio/gio.override	Sat Mar 22 18:14:01 2008
@@ -53,13 +53,16 @@
     static GQuark quark = 0;
     
     state = pyg_gil_state_ensure();
-    
-    if (!quark)
-	quark = g_quark_from_string("pygio::buffer");
-    Py_XINCREF(notify->buffer);
-    g_object_set_qdata_full(G_OBJECT(result), quark,
-			    notify->buffer, py_decref_callback);
 
+    /* buffer is only used by read_async */
+    if (notify->buffer) {
+	if (!quark)
+	    quark = g_quark_from_string("pygio::buffer");
+	Py_XINCREF(notify->buffer);
+	g_object_set_qdata_full(G_OBJECT(result), quark,
+				notify->buffer, py_decref_callback);
+    }
+    
     if (notify->data)
 	ret = PyEval_CallFunction(notify->callback, "(OOO)", 
 				  pygobject_new(source_object), 
@@ -161,19 +164,3 @@
   
   return ret;
 }
-%%
-define GSimpleAsyncResult.get_buffer noargs 
-static PyObject *
-_wrap_g_simple_async_result_get_buffer(PyGObject *self)
-{
-    static GQuark quark = 0;
-    PyObject *buffer;
-
-    if (!quark)
-	quark = g_quark_from_string("pygio::buffer");
-
-    buffer = g_object_get_qdata(G_OBJECT(self->obj), quark);
-
-    Py_INCREF(buffer);
-    return buffer;
-}

Modified: trunk/tests/test_gio.py
==============================================================================
--- trunk/tests/test_gio.py	(original)
+++ trunk/tests/test_gio.py	Sat Mar 22 18:14:01 2008
@@ -66,9 +66,8 @@
     def testReadAsync(self):
         def callback(stream, result):
             try:
-                read = stream.read_finish(result)
-                self.assertEquals(read, len("testing"))
-                self.assertEquals(result.get_buffer(), "testing")
+                data = stream.read_finish(result)
+                self.assertEquals(data, "testing")
                 stream.close()
             finally:
                 loop.quit()
@@ -82,7 +81,6 @@
         self.count = 0
         def callback(stream, result):
             try:
-                #self.assertEquals(result.get_buffer(), None)
                 self.count += 1
                 if self.count == 1:
                     return



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