From 53e9cde2ca64de906967546750e5c6dd6aa58da6 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 23 Nov 2010 19:17:19 +0000 Subject: fd.o #21831: deserialize empty ByteArray objects correctly For some reason libdbus gives us NULL instead of a pointer to a zero-length object (i.e. any random place in the message would do), which Py_BuildValue doesn't interpret the way we'd want it to. --- _dbus_bindings/message-get-args.c | 6 ++++++ test/test-standalone.py | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/_dbus_bindings/message-get-args.c b/_dbus_bindings/message-get-args.c index 7d55ffd..d6b7aba 100644 --- a/_dbus_bindings/message-get-args.c +++ b/_dbus_bindings/message-get-args.c @@ -387,6 +387,12 @@ _message_iter_get_pyobject(DBusMessageIter *iter, dbus_message_iter_get_fixed_array(&sub, (const unsigned char **)&u.s, &n); + if (n == 0 && u.s == NULL) { + /* fd.o #21831: s# turns (NULL, 0) into None, but + * dbus_message_iter_get_fixed_array produces (NULL, 0) + * for an empty byte-blob... */ + u.s = ""; + } args = Py_BuildValue("(s#)", u.s, (Py_ssize_t)n); if (!args) break; ret = PyObject_Call((PyObject *)&DBusPyByteArray_Type, diff --git a/test/test-standalone.py b/test/test-standalone.py index 929fc37..04d1e6d 100755 --- a/test/test-standalone.py +++ b/test/test-standalone.py @@ -82,6 +82,9 @@ class TestTypes(unittest.TestCase): def test_Byte(self): self.assertEquals(types.Byte('x', variant_level=2), types.Byte(ord('x'))) + def test_ByteArray(self): + self.assertEquals(types.ByteArray(''), '') + def test_object_path_attr(self): class MyObject(object): __dbus_object_path__ = '/foo' @@ -175,6 +178,10 @@ class TestMessageMarshalling(unittest.TestCase): s = SignalMessage('/', 'foo.bar', 'baz') s.append(types.ByteArray('ab'), signature='av') aeq(s.get_args_list(), [[types.Byte('a'), types.Byte('b')]]) + s = SignalMessage('/', 'foo.bar', 'baz') + s.append(types.ByteArray(''), signature='ay') + aeq(s.get_args_list(), [[]]) + aeq(s.get_args_list(byte_arrays=True), [types.ByteArray('')]) def test_append_Variant(self): a = self.assert_ -- cgit v1.2.1