Search code examples
pythonpython-3.xdbus

DBus Error: Unable to append with type error saying list indices must be integers or slices, not dict


I write a dbus program with Python using package dbus.

@dbus.service.method("com.fsevenm.castboard.KaroWidget", in_signature='', out_signature="a{si}")
def get_screens(self):
    return [
        {
            "id": 1
        }
    ]

I am sure the type and the signature are correct but each time I call the method it gives this error in the console:

ERROR:dbus.service:Unable to append ([{'id': 1}],) to message with signature a{si}: <class 'TypeError'>: list indices must be integers or slices, not dict

The response of dbus:

('g-io-error-quark: GDBus.Error:org.freedesktop.DBus.Python.TypeError: '
 'Traceback (most recent call last):\n'
 '  File "/usr/lib/python3/dist-packages/dbus/service.py", line 751, in '
 '_message_cb\n'
 '    _method_reply_return(connection, message, method_name, signature, '
 '*retval)\n'
 '  File "/usr/lib/python3/dist-packages/dbus/service.py", line 254, in '
 '_method_reply_return\n'
 '    reply.append(signature=signature, *retval)\n'
 'TypeError: list indices must be integers or slices, not list\n'
 ' (36)')('g-io-error-quark: GDBus.Error:org.freedesktop.DBus.Python.TypeError: '
 'Traceback (most recent call last):\n'
 '  File "/usr/lib/python3/dist-packages/dbus/service.py", line 751, in '
 '_message_cb\n'
 '    _method_reply_return(connection, message, method_name, signature, '
 '*retval)\n'
 '  File "/usr/lib/python3/dist-packages/dbus/service.py", line 254, in '
 '_method_reply_return\n'
 '    reply.append(signature=signature, *retval)\n'
 'TypeError: list indices must be integers or slices, not dict\n'
 ' (36)')

How can I fix this?


Solution

  • I extended the dbus-python example service to have a list of dictionaries like you were trying to do.

    I think you were missing an a for the array and then a second a for the dictionary.

    The following worked for me:

    @dbus.service.method("com.example.SampleInterface",
                         in_signature='', out_signature='aa{si}')
    def GetArrayDict(self):
        return [
                {"first": 1},
                {"second": 2}
                ]