[patch] python get_dict()

David Zeuthen david@fubar.dk
Tue, 21 Oct 2003 03:00:15 +0200


--=-2MP7c6r+T89Wl8ohApdc
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi,

attached is a patch against HEAD for the python bindings such that the
dict objects returned from method invocations are converted to a python
Dictionary object.

OK to commit?

Thanks,
David

-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-                              web:             http://fubar.dk/~david
-    David Zeuthen             pgp key:     http://fubar.dk/pgpkey.asc
-  david(at)fubar.dk           pgp key id:                    b89bab82

    A billion here, a couple of billion there - first thing you
    know it adds up to be real money. - E.M. Dirksen

--=-2MP7c6r+T89Wl8ohApdc
Content-Disposition: attachment; filename=dbus-python-get_dict.patch
Content-Type: text/x-patch; name=dbus-python-get_dict.patch; charset=iso-8859-1
Content-Transfer-Encoding: 7bit

Index: dbus_bindings.pyx.in
===================================================================
RCS file: /cvs/dbus/dbus/python/dbus_bindings.pyx.in,v
retrieving revision 1.3
diff -u -r1.3 dbus_bindings.pyx.in
--- dbus_bindings.pyx.in	15 Oct 2003 21:25:13 -0000	1.3
+++ dbus_bindings.pyx.in	21 Oct 2003 00:52:27 -0000
@@ -428,10 +428,33 @@
                 retval = self.get_boolean_array()
             else:
                 raise TypeError, "Unknown array type %d in MessageIter" % (array_type)
+        elif arg_type == TYPE_DICT:
+            retval = self.get_dict()
         else:
             raise TypeError, 'Unknown arg type %d in MessageIter' % (arg_type)
 
         return retval
+
+    def get_dict(self):
+        cdef DBusMessageIter dict_iter
+        cdef DBusMessageIter* old_iter
+
+        dict = {}
+        dbus_message_iter_init_dict_iterator(self.iter, &dict_iter)
+        # FIXME: nasty hack so we can use existing self.get() method
+        old_iter = self.iter
+        self.iter = &dict_iter
+
+        while True:
+            key = self.get_dict_key()
+            value = self.get()
+            dict[key] = value
+            if not self.has_next():
+                break
+            self.next()
+
+        self.iter = old_iter
+        return dict
     
     def get_arg_type(self):
         return dbus_message_iter_get_arg_type(self.iter)

--=-2MP7c6r+T89Wl8ohApdc--