LEW21 / pydbus

Pythonic DBus library
GNU Lesser General Public License v2.1
326 stars 76 forks source link

Support overloaded methods #82

Open acetylen opened 5 years ago

acetylen commented 5 years ago

I have a service written in C++, which has the following interface on D-Bus:

 <interface name="com.example.myservice1">
...
  <method name="publish">
    <arg name="data" type="(say)" direction="in"/>
  </method>  
  <method name="publish">
    <arg name="data" type="(say)" direction="in"/>
    <arg name="flag" type="(i)" direction="in"/>
  </method>
...
 </interface>

When interacting with the service, only the second method appears in the introspection data. Attempting to use the first method results in

>>> import pydbus
>>> a = pydbus.SessionBus()
>>> r = a.get('com.example.myservice', '/publisher1')
>>> r.publish((1, 'asdf', [1,2,3,4,5]))
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    r.publish((1, 'asdf', [1,2,3,4,5]))
  File ".../pydbus/proxy_method.py", line 62, in __call__
    raise TypeError(self.__qualname__ + " missing {} required positional argument(s)".format(-argdiff))
TypeError: com.example.myservice1.publish missing 1 required positional argument(s)

While attempting to use the second method results in a fairly useless

>>> r.publish((1, 'asdf', [1,2,3,4,5]), (0,))
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    r.publish((1, 'asdf', [1,2,3,4,5]), (0,))
  File ".../pydbus/proxy_method.py", line 74, in __ca
ll__
    self._iface_name, self.__name__, GLib.Variant(self._sinargs, args), GLib.VariantType.new(se
lf._soutargs),
  File "...gi/overrides/GLib.py", line 242, in __new__
    (v, rest_format, _) = creator._create(format_string, [value])
  File ".../gi/overrides/GLib.py", line 131, in _create
    return self._create_tuple(format, args)
  File ".../gi/overrides/GLib.py", line 166, in _create_tuple
    (v, format, _) = self._create(format, args[0][i:])
  File ".../gi/overrides/GLib.py", line 131, in _create
    return self._create_tuple(format, args)
  File ".../gi/overrides/GLib.py", line 170, in _create_tuple
    raise TypeError('tuple type string not closed with )')
TypeError: tuple type string not closed with )

I know this is due to Python's duck-typing approach, but I think creating multimethods when encountering overloaded functions may solve it elegantly.