From ab1ac2f37f6ae5b195ebfb20b1895e841d59e2d7 Mon Sep 17 00:00:00 2001 From: Johan Dahlin Date: Sat, 19 Jan 2008 19:54:46 +0000 Subject: Impl. 2008-01-19 Johan Dahlin * gio/gio.override (_wrap_g_output_stream_write): Impl. * gio/gio.override: * gio/unix.defs: Add GUnixInputStream type and methods * tests/common.py: * tests/test_gio.py: Add GIO tests. svn path=/trunk/; revision=737 --- ChangeLog | 9 +++++++++ gio/gio.override | 41 ++++++++++++++++++++++++++++++++++++++++- gio/unix.defs | 18 ++++++++++++++++++ tests/common.py | 2 ++ tests/test_gio.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 tests/test_gio.py diff --git a/ChangeLog b/ChangeLog index 8710765..edc50ee 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2008-01-19 Johan Dahlin + * gio/gio.override (_wrap_g_output_stream_write): Impl. + * gio/gio.override: + * gio/unix.defs: + Add GUnixInputStream type and methods + + * tests/common.py: + * tests/test_gio.py: + Add GIO tests. + * gio/gio.override (_wrap_g_app_info_get_all_for_type), (_wrap_g_app_info_get_all), (_wrap_g_drive_get_volumes): Implement. diff --git a/gio/gio.override b/gio/gio.override index 78b83ed..f6df13a 100644 --- a/gio/gio.override +++ b/gio/gio.override @@ -265,7 +265,7 @@ _wrap_g_input_stream_read_async(PyGObject *self, PyObject *args, PyObject *kwarg "user_data", NULL }; long count = -1; int io_priority = G_PRIORITY_DEFAULT; - PyGObject *pycancellable = NULL; + PyGObject *pycancellable; PyObject *buffer; GCancellable *cancellable; PyGAsyncRequestNotify *notify; @@ -316,3 +316,42 @@ _wrap_g_input_stream_read_async(PyGObject *self, PyObject *args, PyObject *kwarg Py_INCREF(Py_None); return Py_None; } +%% +override g_output_stream_write kwargs +static PyObject * +_wrap_g_output_stream_write(PyGObject *self, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = { "buffer", "cancellable", NULL }; + PyGObject *pycancellable = NULL; + gchar *buffer; + long count = 0; + GCancellable *cancellable; + GError *error = NULL; + gssize written; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "s#|O!:OutputStream.write", + kwlist, &buffer, &count, + &PyGCancellable_Type, &pycancellable)) + return NULL; + + if (!pycancellable) + 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; + } + + pyg_begin_allow_threads; + written = g_output_stream_write(G_OUTPUT_STREAM(self->obj), + buffer, count, cancellable, &error); + pyg_end_allow_threads; + + if (pyg_error_check(&error)) + return NULL; + + return PyInt_FromLong(written); +} diff --git a/gio/unix.defs b/gio/unix.defs index 0aec7b4..2d28841 100644 --- a/gio/unix.defs +++ b/gio/unix.defs @@ -266,6 +266,24 @@ +;; From gunixinputstream.h + +(define-function unix_input_stream_get_type + (c-name "g_unix_input_stream_get_type") + (return-type "GType") +) + +(define-function unix_input_stream_new + (c-name "g_unix_input_stream_new") + (is-constructor-of "GUnixInputStream") + (return-type "GInputStream*") + (parameters + '("int" "fd") + '("gboolean" "close_fd_at_close") + ) +) + + ;; From gunixoutputstream.h (define-function unix_output_stream_get_type diff --git a/tests/common.py b/tests/common.py index d0ff439..8568635 100644 --- a/tests/common.py +++ b/tests/common.py @@ -9,6 +9,7 @@ def importModules(buildDir, srcDir): sys.path.insert(0, srcDir) sys.path.insert(0, buildDir) sys.path.insert(0, os.path.join(buildDir, 'gobject')) + sys.path.insert(0, os.path.join(buildDir, 'gio')) import ltihooks # testhelper @@ -17,6 +18,7 @@ def importModules(buildDir, srcDir): testhelper = importModule('testhelper', '.') gobject = importModule('gobject', buildDir, 'gobject') + gio = importModule('gio', buildDir, 'gio') ltihooks.uninstall() del ltihooks diff --git a/tests/test_gio.py b/tests/test_gio.py new file mode 100644 index 0000000..696a80a --- /dev/null +++ b/tests/test_gio.py @@ -0,0 +1,49 @@ +# -*- Mode: Python -*- + +import os +import unittest + +from common import gio, gobject + + +class TestInputStream(unittest.TestCase): + def setUp(self): + f = open("inputstream.txt", "w") + f.write("testing") + + self._f = open("inputstream.txt", "r") + self.stream = gio.unix.InputStream(self._f.fileno(), False) + + def tearDown(self): + self._f.close() + os.unlink("inputstream.txt") + + def testWrite(self): + self.assertEquals(self.stream.read(), "testing") + + +class TestOutputStream(unittest.TestCase): + def setUp(self): + self._f = open("outputstream.txt", "w") + self.stream = gio.unix.OutputStream(self._f.fileno(), False) + + def tearDown(self): + self._f.close() + os.unlink("outputstream.txt") + + def testWrite(self): + self.stream.write("testing") + self.stream.close() + self.failUnless(os.path.exists("outputstream.txt")) + self.assertEquals(open("outputstream.txt").read(), "testing") + + def testWriteAsync(self): + def callback(stream, result): + loop.quit() + + f = gio.file_new_for_path("outputstream.txt") + stream = f.read() + stream.read_async(10240, 0, None, callback) + + loop = gobject.MainLoop() + loop.run() -- cgit