[patch][python] Fix fd.o #10174: make it possible to return multiple values with no signature.

Simon McVittie simon.mcvittie at collabora.co.uk
Tue Apr 24 10:16:13 PDT 2007


>From ae8014c72a7d304f20d9422009f42bc48fa8f298 Mon Sep 17 00:00:00 2001
From: Simon McVittie <simon.mcvittie at collabora.co.uk>
Date: Tue, 24 Apr 2007 17:45:03 +0100
Subject: [PATCH] Fix fd.o #10174: make it possible to return multiple values with no signature.
More specifically: when a service method with no signature synchronously
returns a tuple that is not a Struct, interpret it as a multi-valued return,
rather than as a structure.

This is a common Python idiom, and returning a struct makes little sense
anyway when D-Bus lets you return multiple values.

Returned lists are still interpreted as arrays - returning an array is
entirely sensible, and indeed likely to be common.

Async service methods are unaffected (there is no ambiguity), and it's still
possible to return a structure by returning a dbus.Struct with appropriate
contents.

https://bugs.freedesktop.org/show_bug.cgi?id=10174
---
 dbus/service.py      |    8 +++++++-
 test/test-client.py  |    6 ++++++
 test/test-service.py |    5 +++++
 3 files changed, 18 insertions(+), 1 deletions(-)

diff --git a/dbus/service.py b/dbus/service.py
index 775133f..2557de0 100644
--- a/dbus/service.py
+++ b/dbus/service.py
@@ -486,8 +486,14 @@ class Object(Interface):
 
             # no signature, so just turn the return into a tuple and send it as normal
             else:
-                if retval == None:
+                if retval is None:
                     retval = ()
+                elif (isinstance(retval, tuple)
+                      and not isinstance(retval, _dbus_bindings.Struct)):
+                # If the return is a tuple that is not a Struct, we use it
+                # as-is on the assumption that there are multiple return
+                # values - this is the usual Python idiom. (fd.o #10174)
+                    pass
                 else:
                     retval = (retval,)
 
diff --git a/test/test-client.py b/test/test-client.py
index 49578f7..de180ef 100755
--- a/test/test-client.py
+++ b/test/test-client.py
@@ -336,6 +336,12 @@ class TestDBusBindings(unittest.TestCase):
         ret = bus.name_has_owner('org.freedesktop.DBus.Python.TestName')
         self.assert_(not ret, 'deleting reference failed to release BusName org.freedesktop.DBus.Python.TestName')
 
+    def testMultipleReturnWithoutSignature(self):
+        # https://bugs.freedesktop.org/show_bug.cgi?id=10174
+        ret = self.iface.MultipleReturnWithoutSignature()
+        self.assert_(not isinstance(ret, dbus.Struct), repr(ret))
+        self.assertEquals(ret, ('abc', 123))
+
 """ Remove this for now
 class TestDBusPythonToGLibBindings(unittest.TestCase):
     def setUp(self):
diff --git a/test/test-service.py b/test/test-service.py
index 601ee5e..5c6fcdb 100755
--- a/test/test-service.py
+++ b/test/test-service.py
@@ -183,6 +183,11 @@ class TestObject(dbus.service.Object, TestInterface):
     def WhoAmI(self, sender):
         return sender
 
+    @dbus.service.method(IFACE)
+    def MultipleReturnWithoutSignature(self):
+        # https://bugs.freedesktop.org/show_bug.cgi?id=10174
+        return dbus.String('abc'), dbus.Int32(123)
+
 session_bus = dbus.SessionBus()
 name = dbus.service.BusName("org.freedesktop.DBus.TestSuitePythonService", bus=session_bus)
 object = TestObject(name)
-- 
1.5.1.2



More information about the dbus mailing list