Reading bytes from Gio.InputStream.read()/read_all()




Hi,

Has anyone managed to successfully read bytes from Gio.InputStream.read() and/or .read_all() using GLib/PyGObject 3.14 and Python 3? I can get the bytes from a call to read_bytes(), but do not want to because of the overhead of allocating a new buffer over and over when reading large amounts of data.

This is what happens trying to pass in either a bytes, bytearray, GLib.Bytes or GLib.ByteArray objects:

mjg montbard:~$ echo hello > test.txt
mjg montbard:~$ python3
Python 3.4.3 (default, Mar 8 2015, 10:25:06)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gi.repository import Gio
>>> file = Gio.File.new_for_path('test.txt')
>>>
>>> buf = bytes()
>>> input = file.read()
>>> input.read_all(buf)
(process:20360): GLib-GIO-CRITICAL **: g_input_stream_read_all: assertion 'buffer != NULL' failed
(False, 0)
>>>
>>> buf = bytearray(5)
>>> input = file.read()
>>> input.read_all(buf)
(True, 5)
>>> print(buf)
bytearray(b'\x00\x00\x00\x00\x00')
>>>
>> buf = GLib.Bytes.new()
>>> input = file.read()
>>> input.read_all(buf)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: Must be sequence, not Bytes
>>>
>>> buf = GLib.ByteArray()
>>> input = file.read()
>>> input.read_all(buf)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: Must be sequence, not ByteArray
>>>

Cheers,
//Mike

--
⊨ Michael Gratton, Percept Wrangler.
⚙ <http://mjog.vee.net/>




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