Incorrect GIR annotation for Gtk.TextBufferSerializeFunc return value
Submitted by Jaap Karssenberg
Link to original bug (#791443)
Description
The issue is the return value of the Gtk.TextBufferSerializeFunc callback. The return value documented in the C documentation is "a newly-allocated array of guint8 which contains the serialized data, or None if an error occurred".
However I found that when using the python bindings, when I return a string I get an error that indicates a single character is expected. (And when I return a single character, a segfault happens.)
Digging into the "Gtk.gir" XML annotation I find this definition for the return type:
<return-value transfer-ownership="none" nullable="1">
[...]</doc>
</return-value>
For comparison I look at the return type of Gtk.Clipboard.wait_for_rich_text() which is also given as "guint8 *" in the documentation and - more importantly - should return the exact same data as the buffer serialization function if when were to do copy paste from a text buffer.
<return-value transfer-ownership="full" nullable="1">
[...]</doc>
</array>
</return-value>
So my hypothesis is that in the annotation something went wrong and the return value of the TextBufferSerializeFunc incorrectly omits the "<array>
" part. My naive guess is that changing it to look more like the second blob should work.
The following script shows the issue:
##############################
import gi gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
mimetype = 'x-test/x-serialize'
def serialize_func(rbuffer, cbuffer, start, end, user_data): return b'Test 123'
textbuffer = Gtk.TextBuffer() atom = textbuffer.register_serialize_format( mimetype, serialize_func )
textbuffer.set_text('abc') data = "" textbuffer, atom, *textbuffer.get_bounds() )
print('DATA: %r' % data) ##############################
what I expect:
$ python3 test_serialization.py DATA: b'Test 123' what I get:
$ python3 test_serialization.py TypeError: Must be a single character DATA: b''
For reference: Mailing list discussion: https://mail.gnome.org/archives/python-hackers-list/2017-December/msg00000.html
Version: 3.22.x