diff options
| author | Paul Pogonyshev <pogonyshev@gmx.net> | 2008-08-27 21:22:08 +0000 |
|---|---|---|
| committer | Paul Pogonyshev <paulp@src.gnome.org> | 2008-08-27 21:22:08 +0000 |
| commit | 30eb71d505abcbaf42d098dac62d0c747338d910 (patch) | |
| tree | d0866a230255838c94dee43f1db5b3be199e3541 /gio/ginputstream.override | |
| parent | 70ede7ebbceb28e420c31d5ceb68e8abdd6216a5 (diff) | |
| download | pygobject-30eb71d505abcbaf42d098dac62d0c747338d910.tar.gz pygobject-30eb71d505abcbaf42d098dac62d0c747338d910.tar.xz pygobject-30eb71d505abcbaf42d098dac62d0c747338d910.zip | |
Rename from read(), document. (gio.InputStream.read): Rename from
2008-08-28 Paul Pogonyshev <pogonyshev@gmx.net>
* gio/gio.defs (gio.InputStream.read_part): Rename from read(),
document.
(gio.InputStream.read): Rename from read_all(), document.
(gio.OutputStream.write_part): Rename from write(), document.
(gio.OutputStream.write): Rename from write_all(), document.
* gio/ginputstream.override (_wrap_g_input_stream_read): Fix
several bugs.
(_wrap_g_input_stream_read_all): New function.
* gio/goutputstream.override (_wrap_g_output_stream_write_all):
New function.
* tests/test_gio.py (TestInputStream.testRead): Add more tests.
(TestInputStream.test_read_part): New test.
(TestInputStream._read_in_loop): New helper method.
(TestOutputStream.test_write_part): New test.
svn path=/trunk/; revision=950
Diffstat (limited to 'gio/ginputstream.override')
| -rw-r--r-- | gio/ginputstream.override | 109 |
1 files changed, 88 insertions, 21 deletions
diff --git a/gio/ginputstream.override b/gio/ginputstream.override index 44378cd..b320034 100644 --- a/gio/ginputstream.override +++ b/gio/ginputstream.override @@ -91,7 +91,7 @@ _wrap_g_input_stream_read(PyGObject *self, PyObject *args, PyObject *kwargs) &pycancellable)) return NULL; - buffersize = BUFSIZE; + buffersize = (count < 0 ? BUFSIZE : count); if (!pygio_check_cancellable(pycancellable, &cancellable)) return NULL; @@ -111,31 +111,99 @@ _wrap_g_input_stream_read(PyGObject *self, PyObject *args, PyObject *kwargs) &error); pyg_end_allow_threads; - if (pyg_error_check(&error)) - { - Py_DECREF(v); - return NULL; - } - else if (chunksize == 0) - { - PyErr_SetFromErrno(PyExc_IOError); - Py_DECREF(v); - return NULL; - } + if (pyg_error_check(&error)) { + Py_DECREF(v); + return NULL; + } + if (chunksize == 0) { + /* End of file. */ + break; + } bytesread += chunksize; - if (bytesread < buffersize) + if (bytesread < buffersize) { + /* g_input_stream_read() decided to not read full buffer. We + * then return early too, even if 'count' is less than 0. + */ + break; + } + + if (count < 0) { + buffersize += BUFSIZE; + if (_PyString_Resize(&v, buffersize) < 0) + return NULL; + } + else { + /* Got what was requested. */ break; + } + } + + if (bytesread != buffersize) + _PyString_Resize(&v, bytesread); + + return v; +} +%% +override g_input_stream_read_all kwargs +static PyObject * +_wrap_g_input_stream_read_all(PyGObject *self, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = { "count", "cancellable", NULL }; + PyGObject *pycancellable = NULL; + PyObject *v; + GCancellable *cancellable; + long count = -1; + GError *error = NULL; + size_t bytesread, buffersize, chunksize; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|lO:InputStream.read", + kwlist, &count, + &pycancellable)) + return NULL; + + buffersize = (count < 0 ? BUFSIZE : count); + + if (!pygio_check_cancellable(pycancellable, &cancellable)) + return NULL; + + v = PyString_FromStringAndSize((char *)NULL, buffersize); + if (v == NULL) + return NULL; - if (count < 0) - { - buffersize += BUFSIZE; - if (_PyString_Resize(&v, buffersize) < 0) - return NULL; - } - else + bytesread = 0; + for (;;) + { + pyg_begin_allow_threads; + errno = 0; + g_input_stream_read_all(G_INPUT_STREAM(self->obj), + PyString_AS_STRING((PyStringObject *)v) + bytesread, + buffersize - bytesread, + &chunksize, + cancellable, &error); + pyg_end_allow_threads; + + if (pyg_error_check(&error)) { + Py_DECREF(v); + return NULL; + } + + bytesread += chunksize; + if (bytesread < buffersize || chunksize == 0) { + /* End of file. */ + break; + } + + if (count < 0) { + buffersize += BUFSIZE; + if (_PyString_Resize(&v, buffersize) < 0) + return NULL; + } + else { /* Got what was requested. */ break; + } } if (bytesread != buffersize) @@ -384,5 +452,4 @@ _wrap_g_memory_input_stream_add_data(PyGObject *self, Py_INCREF(Py_None); return Py_None; } -/* GInputStream.read_all: No ArgType for void* */ /* GInputStream.skip_async: No ArgType for GAsyncReadyCallback */ |
