GLib.Variant.get_data_as_bytes() returning None?
I might be doing something wrong here, but I can't see what.
Here is a very simple working example:
from gi.repository import GLib
TYPE_STRING = 'mv'
variant = GLib.Variant(TYPE_STRING, None)
assert variant.is_normal_form()
assert variant.get_data_as_bytes().get_data() == b''
# Serialize the variant, to send it over the wire for example
data = variant.get_data_as_bytes().get_data()
# Now parse the byte string to remake the variant
variant = GLib.Variant.new_from_bytes(GLib.VariantType(TYPE_STRING), GLib.Bytes.new(data), False)
assert variant.is_normal_form()
assert variant.get_data_as_bytes().get_data() == b''
As you can see it just serializes a null maybe-variant, then deserializes it again, and obtains the same byte string both times.
Now here's a slightly more complex example, where the null maybe-variant is inside an array:
from gi.repository import GLib
TYPE_STRING = 'amv'
variant = GLib.Variant(TYPE_STRING, [None])
assert variant.is_normal_form()
assert variant.get_child_value(0).get_data_as_bytes().get_data() == b''
# Serialize the variant, to send it over the wire for example
data = variant.get_data_as_bytes().get_data()
# Now parse the byte string to remake the variant
variant = GLib.Variant.new_from_bytes(GLib.VariantType(TYPE_STRING), GLib.Bytes.new(data), False)
assert variant.is_normal_form()
assert variant.get_child_value(0).get_data_as_bytes().get_data() == b''
When running that script I get:
example.py:17: Warning: g_bytes_new_from_bytes: assertion 'offset <= bytes->size' failed
assert variant.get_child_value(0).get_data_as_bytes().get_data() == b''
Traceback (most recent call last):
File "example.py", line 17, in <module>
assert variant.get_child_value(0).get_data_as_bytes().get_data() == b''
AttributeError: 'NoneType' object has no attribute 'get_data'
As you can see, get_data_as_bytes()
returned None
, with an assertion failing in GLib.
I'm not sure whether the problem is in my code, in the pygobject wrapper or in GLib, and would appreciate any help figuring this out.