dbus/test/python test-client.py,1.7,1.8 test-service.py,1.3,1.4

Robert McQueen robot101 at freedesktop.org
Sat Oct 29 12:13:19 PDT 2005


Update of /cvs/dbus/dbus/test/python
In directory gabe:/tmp/cvs-serv30548/test/python

Modified Files:
	test-client.py test-service.py 
Log Message:
2005-10-29  Robert McQueen  <robot101 at debian.org>

        * python/decorators.py: Add optional arguments to the method and
        signal decorators to allow you to specify the signature of arguments
        and return values. Preserve the doc strings of signal functions in the
        decorated version, for pydoc and friends.

        * python/dbus_bindings.pyx, python/proxies.py: Replace the
        parse_signature_block function with an iterable dbus.Signature()
        type. Fix a bug in MessageIter.append_strict where you could append
        anything by claiming it was a string.

        * python/service.py: Use the out_signature decoration on methods to
        marshal return values, meaning you no longer require dbus.Array()
        or dbus.Dictionary() to indicate the type when returning empty
        arrays or dictionaries. Fix a bug where exceptions which are defined
        in __main__ are not turned into error replies.

        * test/python/test-client.py, test/python/test-service.py: Add test
        for correct marshalling of return values according to out_signature.
        Fix a bug in the async call test where the error_handler is missing a
        self argument.


Index: test-client.py
===================================================================
RCS file: /cvs/dbus/dbus/test/python/test-client.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- test-client.py	24 Oct 2005 18:29:50 -0000	1.7
+++ test-client.py	29 Oct 2005 19:13:17 -0000	1.8
@@ -88,7 +88,7 @@
 
                 self.test_controler.assertEquals(val, self.expected_result)
                 
-            def error_handler(error):
+            def error_handler(self, error):
                 print error
                 if self.do_exit:
                     main_loop.quit()
@@ -105,6 +105,45 @@
             
         main_loop.run()
 
+    def testReturnMarshalling(self):
+        print "\n********* Testing return marshalling ***********"
+
+        # these values are the same as in the server, and the
+        # methods should only succeed when they are called with
+        # the right value number, because they have out_signature
+        # decorations, and return an unmatching type when called
+        # with a different number
+        values = ["", ("",""), ("","",""), [], {}, ["",""], ["","",""]]
+        methods = [
+                    (self.iface.ReturnOneString, set([0]), set([0])),
+                    (self.iface.ReturnTwoStrings, set([1, 5]), set([5])),
+                    (self.iface.ReturnStruct, set([1, 5]), set([1])),
+                    # all of our test values are sequences so will marshall correctly into an array :P
+                    (self.iface.ReturnArray, set(range(len(values))), set([3, 5, 6])),
+                    (self.iface.ReturnDict, set([0, 3, 4]), set([4]))
+                ]
+
+        for (method, success_values, return_values) in methods:
+            print "\nTrying correct behaviour of", method._method_name
+            for value in range(len(values)):
+                try:
+                    ret = method(value)
+                except Exception, e:
+                    print "%s(%s) raised %s" % (method._method_name, repr(values[value]), e.__class__)
+
+                    # should fail if it tried to marshal the wrong type
+                    self.assert_(value not in success_values, "%s should succeed when we ask it to return %s\n%s" % (method._method_name, repr(values[value]), e))
+                else:
+                    print "%s(%s) returned %s" % (method._method_name, repr(values[value]), repr(ret))
+
+                    # should only succeed if it's the right return type
+                    self.assert_(value in success_values, "%s should fail when we ask it to return %s" % (method._method_name, repr(values[value])))
+
+                    # check the value is right too :D
+                    returns = map(lambda n: values[n], return_values)
+                    self.assert_(ret in returns, "%s should return one of %s" % (method._method_name, repr(returns)))
+        print
+
 class TestDBusPythonToGLibBindings(unittest.TestCase):
     def setUp(self):
         self.bus = dbus.SessionBus()

Index: test-service.py
===================================================================
RCS file: /cvs/dbus/dbus/test/python/test-service.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- test-service.py	14 Oct 2005 21:44:00 -0000	1.3
+++ test-service.py	29 Oct 2005 19:13:17 -0000	1.4
@@ -36,6 +36,42 @@
 
         return dbus.Array(ret, signature="(uus)")
 
+    def returnValue(self, test):
+        if test == 0:
+            return ""
+        elif test == 1:
+            return "",""
+        elif test == 2:
+            return "","",""
+        elif test == 3:
+            return []
+        elif test == 4:
+            return {}
+        elif test == 5:
+            return ["",""]
+        elif test == 6:
+            return ["","",""]
+
+    @dbus.service.method("org.freedesktop.DBus.TestSuiteInterface", in_signature='u', out_signature='s')
+    def ReturnOneString(self, test):
+        return self.returnValue(test)
+
+    @dbus.service.method("org.freedesktop.DBus.TestSuiteInterface", in_signature='u', out_signature='ss')
+    def ReturnTwoStrings(self, test):
+        return self.returnValue(test)
+
+    @dbus.service.method("org.freedesktop.DBus.TestSuiteInterface", in_signature='u', out_signature='(ss)')
+    def ReturnStruct(self, test):
+        return self.returnValue(test)
+
+    @dbus.service.method("org.freedesktop.DBus.TestSuiteInterface", in_signature='u', out_signature='as')
+    def ReturnArray(self, test):
+        return self.returnValue(test)
+
+    @dbus.service.method("org.freedesktop.DBus.TestSuiteInterface", in_signature='u', out_signature='a{ss}')
+    def ReturnDict(self, test):
+        return self.returnValue(test)
+
 session_bus = dbus.SessionBus()
 name = dbus.service.BusName("org.freedesktop.DBus.TestSuitePythonService", bus=session_bus)
 object = TestObject(name)



More information about the dbus-commit mailing list