]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
2005-11-04 Robert McQueen <robot101@debian.org>
authorRobert McQueen <robot101@debian.org>
Fri, 4 Nov 2005 12:18:00 +0000 (12:18 +0000)
committerRobert McQueen <robot101@debian.org>
Fri, 4 Nov 2005 12:18:00 +0000 (12:18 +0000)
        * python/dbus_bindings.pyx, test/python/test-client.py: Fix
        marshalling of boolean values. Add some booleans to the values in
        the test client.

        * python/decorators.py, python/service.py: Add an 'async_callbacks'
        argument to the dbus.service.method decorator, which allows you to
        name arguments to take two callback functions for replying with
        return values or an exception.

        * test/python/test-client.py, test/python/test-service.py: Add test
        case using asynchronous method reply functions, both return values and
        errors, and from within both the function itself and from a mainloop
        callback.

        * python/decorators.py, python/service.py: Perform checking that the
        number of method/signal arguments matches the number of types in the
        signature at class loading time, not when you first introspect the
        class.

        * python/service.py: Remove debug print left by the last commit.

ChangeLog
python/dbus_bindings.pyx
python/decorators.py
python/service.py
test/python/test-client.py
test/python/test-service.py

index e854263105607329c984386264487ac5f84c57c1..b90e2e0e766e77e714a0e9285ae1639a7a2fe133 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,26 @@
+2005-11-04  Robert McQueen  <robot101@debian.org>
+
+       * python/dbus_bindings.pyx, test/python/test-client.py: Fix
+       marshalling of boolean values. Add some booleans to the values in
+       the test client.
+
+       * python/decorators.py, python/service.py: Add an 'async_callbacks'
+       argument to the dbus.service.method decorator, which allows you to
+       name arguments to take two callback functions for replying with
+       return values or an exception.
+
+       * test/python/test-client.py, test/python/test-service.py: Add test
+       case using asynchronous method reply functions, both return values and
+       errors, and from within both the function itself and from a mainloop
+       callback.
+
+       * python/decorators.py, python/service.py: Perform checking that the
+       number of method/signal arguments matches the number of types in the
+       signature at class loading time, not when you first introspect the
+       class.
+
+       * python/service.py: Remove debug print left by the last commit.
+
 2005-11-03  Robert McQueen  <robot101@debian.org>
 
        * python/service.py: Heavy refactoring of method invocation, with
index c693a3d06d3a799d8661417bd1ef96220283011a..a96a5adceaac491d6805e9016fb8efcedc8dc741 100644 (file)
@@ -893,7 +893,7 @@ cdef class MessageIter:
         ptype = type(value)
         ret = ""
         if ptype == bool:
-            ret = TYPE_BOOL
+            ret = TYPE_BOOLEAN
             ret = str(chr(ret))
         elif ptype == int:
             ret = TYPE_INT32
@@ -937,7 +937,7 @@ cdef class MessageIter:
             ret = TYPE_BYTE
             ret = str(chr(ret))
         elif isinstance(value, Boolean) or value == Boolean:
-            ret = TYPE_BOOL
+            ret = TYPE_BOOLEAN
             ret = str(chr(ret))
         elif isinstance(value, Int16) or value == Int16:
             ret = TYPE_INT16
index 8b553736d5ee277830e017dcbfd38f08cf1914e3..3973f3e4e5cb8a56d9b1f7b19281e82ce929953f 100644 (file)
@@ -2,16 +2,35 @@ import _util
 import inspect
 import dbus_bindings
 
-def method(dbus_interface, in_signature=None, out_signature=None):
+def method(dbus_interface, in_signature=None, out_signature=None, async_callbacks=None):
     _util._validate_interface_or_name(dbus_interface)
 
     def decorator(func):
+        args = inspect.getargspec(func)[0]
+        args.pop(0)
+
+        if async_callbacks:
+            if type(async_callbacks) != tuple:
+                raise TypeError('async_callbacks must be a tuple of (keyword for return callback, keyword for error callback)')
+            if len(async_callbacks) != 2:
+                raise ValueError('async_callbacks must be a tuple of (keyword for return callback, keyword for error callback)')
+            args.remove(async_callbacks[0])
+            args.remove(async_callbacks[1])
+
+        if in_signature:
+            in_sig = tuple(dbus_bindings.Signature(in_signature))
+
+            if len(in_sig) > len(args):
+                raise ValueError, 'input signature is longer than the number of arguments taken'
+            elif len(in_sig) < len(args):
+                raise ValueError, 'input signature is shorter than the number of arguments taken'
+
         func._dbus_is_method = True
+        func._dbus_async_callbacks = async_callbacks
         func._dbus_interface = dbus_interface
         func._dbus_in_signature = in_signature
         func._dbus_out_signature = out_signature
-        func._dbus_args = inspect.getargspec(func)[0]
-        func._dbus_args.pop(0)
+        func._dbus_args = args
         return func
 
     return decorator
@@ -29,13 +48,23 @@ def signal(dbus_interface, signature=None):
       
             self._connection.send(message)
 
+        args = inspect.getargspec(func)[0]
+        args.pop(0)
+
+        if signature:
+            sig = tuple(dbus_bindings.Signature(func._dbus_signature))
+
+            if len(sig) > len(args):
+                raise ValueError, 'signal signature is longer than the number of arguments provided'
+            elif len(sig) < len(args):
+                raise ValueError, 'signal signature is shorter than the number of arguments provided'
+
         emit_signal.__name__ = func.__name__
         emit_signal.__doc__ = func.__doc__
         emit_signal._dbus_is_signal = True
         emit_signal._dbus_interface = dbus_interface
         emit_signal._dbus_signature = signature
-        emit_signal._dbus_args = inspect.getargspec(func)[0]
-        emit_signal._dbus_args.pop(0)
+        emit_signal._dbus_args = args
         return emit_signal
 
     return decorator
index 34b0384d763cdde7266ae61b13b8b2b08e148bf3..14a2d6d3a3990636a84bad916f643eeed75c8e72 100644 (file)
@@ -169,13 +169,9 @@ class InterfaceType(type):
 
         if func._dbus_in_signature:
             # convert signature into a tuple so length refers to number of
-            # types, not number of characters
+            # types, not number of characters. the length is checked by
+            # the decorator to make sure it matches the length of args.
             in_sig = tuple(dbus_bindings.Signature(func._dbus_in_signature))
-
-            if len(in_sig) > len(args):
-                raise ValueError, 'input signature is longer than the number of arguments taken'
-            elif len(in_sig) < len(args):
-                raise ValueError, 'input signature is shorter than the number of arguments taken'
         else:
             # magic iterator which returns as many v's as we need
             in_sig = dbus_bindings.VariantSignature()
@@ -204,11 +200,6 @@ class InterfaceType(type):
             # convert signature into a tuple so length refers to number of
             # types, not number of characters
             sig = tuple(dbus_bindings.Signature(func._dbus_signature))
-
-            if len(sig) > len(args):
-                raise ValueError, 'signal signature is longer than the number of arguments provided'
-            elif len(sig) < len(args):
-                raise ValueError, 'signal signature is shorter than the number of arguments provided'
         else:
             # magic iterator which returns as many v's as we need
             sig = dbus_bindings.VariantSignature()
@@ -248,16 +239,33 @@ class Object(Interface):
             interface_name = message.get_interface()
             (candidate_method, parent_method) = _method_lookup(self, method_name, interface_name)
 
-            # call method
+            # set up method call parameters
             args = message.get_args_list()
-            retval = candidate_method(self, *args)
+            keywords = {}
 
-            # send return reply if it's not an asynchronous function
-            # if we have a signature, use it to turn the return value into a tuple as appropriate
+            # iterate signature into list of complete types
             if parent_method._dbus_out_signature:
-                # iterate signature into list of complete types
                 signature = tuple(dbus_bindings.Signature(parent_method._dbus_out_signature))
+            else:
+                signature = None
 
+            # set up async callback functions
+            if parent_method._dbus_async_callbacks:
+                (return_callback, error_callback) = parent_method._dbus_async_callbacks
+                keywords[return_callback] = lambda *retval: _method_reply_return(connection, message, method_name, signature, *retval)
+                keywords[error_callback] = lambda exception: _method_reply_error(connection, message, exception)
+
+            # call method
+            retval = candidate_method(self, *args, **keywords)
+
+            # we're done - the method has got callback functions to reply with
+            if parent_method._dbus_async_callbacks:
+                return
+
+            # otherwise we send the return values in a reply. if we have a
+            # signature, use it to turn the return value into a tuple as
+            # appropriate
+            if parent_method._dbus_out_signature:
                 # if we have zero or one return values we want make a tuple
                 # for the _method_reply_return function, otherwise we need
                 # to check we're passing it a sequence
@@ -285,7 +293,6 @@ class Object(Interface):
                 else:
                     retval = (retval,)
 
-            print retval, signature
             _method_reply_return(connection, message, method_name, signature, *retval)
         except Exception, exception:
             # send error reply
index 433caac951e13475c3e550b5fe0eeae3836e26d8..8c09b67e1b2d86bb40e0f1adae4cee9225530732 100755 (executable)
@@ -29,6 +29,7 @@ test_types_vals = [1, 12323231, 3.14159265, 99999999.99,
                  (1,2,3), (1,), (1,"2",3), ("2", "what"), ("you", 1.2),
                  {1:"a", 2:"b"}, {"a":1, "b":2}, #{"a":(1,"B")},
                  {1:1.1, 2:2.2}, [[1,2,3],[2,3,4]], [["a","b"],["c","d"]],
+                 True, False,
                  #([1,2,3],"c", 1.2, ["a","b","c"], {"a": (1,"v"), "b": (2,"d")})
                  ]
 
@@ -153,6 +154,21 @@ class TestDBusBindings(unittest.TestCase):
         print "CheckInheritance returned %s" % ret
         self.assert_(ret, "overriding CheckInheritance from TestInterface failed")
 
+    def testAsyncMethods(self):
+        print "\n********* Testing asynchronous method implementation *******"
+        for (async, fail) in ((False, False), (False, True), (True, False), (True, True)):
+            try:
+                val = ('a', 1, False, [1,2], {1:2})
+                print "calling AsynchronousMethod with %s %s %s" % (async, fail, val)
+                ret = self.iface.AsynchronousMethod(async, fail, val)
+            except Exception, e:
+                print "%s:\n%s" % (e.__class__, e)
+                self.assert_(fail)
+            else:
+                self.assert_(not fail)
+                print val, ret
+                self.assert_(val == ret)
+
 class TestDBusPythonToGLibBindings(unittest.TestCase):
     def setUp(self):
         self.bus = dbus.SessionBus()
index ecb9fd6013e9aa04de0213a8d30878a16a93ff58..820514ae7e5b160750c3da18ddf763e990bb5bae 100755 (executable)
@@ -80,6 +80,22 @@ class TestObject(dbus.service.Object, TestInterface):
     def CheckInheritance(self):
         return True
 
+    @dbus.service.method('org.freedesktop.DBus.TestSuiteInterface', in_signature='bbv', out_signature='v', async_callbacks=('return_cb', 'error_cb'))
+    def AsynchronousMethod(self, async, fail, variant, return_cb, error_cb):
+        try:
+            if async:
+                gobject.timeout_add(500, self.AsynchronousMethod, False, fail, variant, return_cb, error_cb)
+                return
+            else:
+                if fail:
+                    raise RuntimeError
+                else:
+                    return_cb(variant)
+
+                return False # do not run again
+        except Exception, e:
+            error_cb(e)
+
 session_bus = dbus.SessionBus()
 name = dbus.service.BusName("org.freedesktop.DBus.TestSuitePythonService", bus=session_bus)
 object = TestObject(name)