1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygobject - Python bindings for GObject
3 * Copyright (C) 2008 Johan Dahlin
5 * ginputstream.override: module overrides for GInputStream
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
27 override g_input_stream_read kwargs
29 _wrap_g_input_stream_read(PyGObject *self, PyObject *args, PyObject *kwargs)
31 static char *kwlist[] = { "count", "cancellable", NULL };
32 PyGObject *pycancellable = NULL;
34 GCancellable *cancellable;
37 size_t bytesread, buffersize, chunksize;
39 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
40 "|lO:InputStream.read",
45 buffersize = (count < 0 ? BUFSIZE : count);
47 if (!pygio_check_cancellable(pycancellable, &cancellable))
50 v = PyString_FromStringAndSize((char *)NULL, buffersize);
57 pyg_begin_allow_threads;
59 chunksize = g_input_stream_read(G_INPUT_STREAM(self->obj),
60 PyString_AS_STRING((PyStringObject *)v) + bytesread,
61 buffersize - bytesread, cancellable,
63 pyg_end_allow_threads;
65 if (pyg_error_check(&error)) {
74 bytesread += chunksize;
75 if (bytesread < buffersize) {
76 /* g_input_stream_read() decided to not read full buffer. We
77 * then return early too, even if 'count' is less than 0.
83 buffersize += BUFSIZE;
84 if (_PyString_Resize(&v, buffersize) < 0)
88 /* Got what was requested. */
93 if (bytesread != buffersize)
94 _PyString_Resize(&v, bytesread);
99 override g_input_stream_read_all kwargs
101 _wrap_g_input_stream_read_all(PyGObject *self, PyObject *args, PyObject *kwargs)
103 static char *kwlist[] = { "count", "cancellable", NULL };
104 PyGObject *pycancellable = NULL;
106 GCancellable *cancellable;
108 GError *error = NULL;
109 size_t bytesread, buffersize, chunksize;
111 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
112 "|lO:InputStream.read",
117 buffersize = (count < 0 ? BUFSIZE : count);
119 if (!pygio_check_cancellable(pycancellable, &cancellable))
122 v = PyString_FromStringAndSize((char *)NULL, buffersize);
129 pyg_begin_allow_threads;
131 g_input_stream_read_all(G_INPUT_STREAM(self->obj),
132 PyString_AS_STRING((PyStringObject *)v) + bytesread,
133 buffersize - bytesread,
135 cancellable, &error);
136 pyg_end_allow_threads;
138 if (pyg_error_check(&error)) {
143 bytesread += chunksize;
144 if (bytesread < buffersize || chunksize == 0) {
150 buffersize += BUFSIZE;
151 if (_PyString_Resize(&v, buffersize) < 0)
155 /* Got what was requested. */
160 if (bytesread != buffersize)
161 _PyString_Resize(&v, bytesread);
166 override g_input_stream_read_async kwargs
168 _wrap_g_input_stream_read_async(PyGObject *self,
172 static char *kwlist[] = { "count", "callback", "io_priority",
173 "cancellable", "user_data", NULL };
175 int io_priority = G_PRIORITY_DEFAULT;
176 PyGObject *pycancellable = NULL;
177 GCancellable *cancellable;
180 notify = pygio_notify_new();
182 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
183 "lO|iOO:InputStream.read_async",
192 if (!pygio_notify_callback_is_valid(notify))
195 if (!pygio_check_cancellable(pycancellable, &cancellable))
198 if (!pygio_notify_allocate_buffer(notify, count))
201 pygio_notify_reference_callback(notify);
202 pygio_notify_attach_to_result(notify);
204 g_input_stream_read_async(G_INPUT_STREAM(self->obj),
209 (GAsyncReadyCallback) async_result_callback_marshal,
216 pygio_notify_free(notify);
220 override g_input_stream_read_finish kwargs
222 _wrap_g_input_stream_read_finish(PyGObject *self,
226 static char *kwlist[] = { "result", NULL };
228 GError *error = NULL;
229 Py_ssize_t bytesread;
232 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
233 "O!:gio.InputStream.read_finish",
234 kwlist, &PyGAsyncResult_Type, &result))
237 bytesread = g_input_stream_read_finish(G_INPUT_STREAM(self->obj),
238 G_ASYNC_RESULT(result->obj), &error);
240 if (pyg_error_check(&error))
244 return PyString_FromString("");
246 notify = pygio_notify_get_attached(result);
247 return PyString_FromStringAndSize(notify->buffer, bytesread);
250 override g_input_stream_close_async kwargs
252 _wrap_g_input_stream_close_async(PyGObject *self,
256 static char *kwlist[] = { "callback", "io_priority", "cancellable",
258 int io_priority = G_PRIORITY_DEFAULT;
259 PyGObject *pycancellable = NULL;
260 GCancellable *cancellable;
263 notify = pygio_notify_new();
265 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
266 "O|iOO:InputStream.close_async",
274 if (!pygio_notify_callback_is_valid(notify))
277 if (!pygio_check_cancellable(pycancellable, &cancellable))
280 pygio_notify_reference_callback(notify);
282 g_input_stream_close_async(G_INPUT_STREAM(self->obj),
285 (GAsyncReadyCallback)async_result_callback_marshal,
292 pygio_notify_free(notify);
296 override g_input_stream_skip_async kwargs
298 _wrap_g_input_stream_skip_async(PyGObject *self,
302 static char *kwlist[] = { "count", "callback", "io_priority",
303 "cancellable", "user_data", NULL };
305 int io_priority = G_PRIORITY_DEFAULT;
306 PyGObject *pycancellable = NULL;
307 GCancellable *cancellable;
310 notify = pygio_notify_new();
312 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
313 "lO|iOO:InputStream.skip_async",
322 if (!pygio_notify_callback_is_valid(notify))
325 if (!pygio_check_cancellable(pycancellable, &cancellable))
328 pygio_notify_reference_callback(notify);
331 g_input_stream_skip_async(G_INPUT_STREAM(self->obj),
335 (GAsyncReadyCallback) async_result_callback_marshal,
342 pygio_notify_free(notify);