dbus/python dbus_bindings.pyx.in,1.9,1.10

Seth Nickell seth at pdx.freedesktop.org
Mon May 31 23:13:33 PDT 2004


Update of /cvs/dbus/dbus/python
In directory pdx:/tmp/cvs-serv16903/python

Modified Files:
	dbus_bindings.pyx.in 
Log Message:
2004-06-01  Seth Nickell  <seth at gnome.org>

	* python/dbus_bindings.pyx.in:
	* python/tests/test-client.py:

	Round off basic type support. Add dicts (yay!), and 
	remaining array types.

	Make MessageIter more general so it works for dicts too.

	Mark all loop variables as C integers.


Index: dbus_bindings.pyx.in
===================================================================
RCS file: /cvs/dbus/dbus/python/dbus_bindings.pyx.in,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- a/dbus_bindings.pyx.in	1 Jun 2004 01:20:59 -0000	1.9
+++ b/dbus_bindings.pyx.in	1 Jun 2004 06:13:31 -0000	1.10
@@ -72,6 +72,7 @@
 cdef class Message
 cdef class PendingCall
 cdef class Watch
+cdef class MessageIter
 
 cdef void cunregister_function_handler (DBusConnection *connection,
                                         void *user_data):
@@ -148,11 +149,13 @@
     
     #FIXME: this is totally busted, don't use a class shared member like parsed_path
     def _build_parsed_path(self, path_element_list):
-        cdef char **cpatharray        
+        cdef char **cpatharray
+        cdef int i
+        cdef int size
         size = len(path_element_list)
         cpatharray = <char **>malloc(sizeof(char*) * (size + 1))
 
-        for i in range(size):
+        for i from 0 <= i < size:
             path_element = path_element_list[i]
             cpatharray[i] = path_element
             
@@ -443,12 +446,11 @@
     cdef DBusMessageIter *iter
     cdef DBusMessageIter real_iter
 
-
-    def __init__(self, Message message):
-        cdef DBusMessage *msg
+    def __init__(self):
         self.iter = &self.real_iter
-        msg = message._get_msg()
-        dbus_message_iter_init(msg, self.iter)
+        
+    cdef __cinit__(self, DBusMessageIter *iter):
+        self.real_iter = iter[0]
     
     cdef DBusMessageIter *_get_iter(self):
         return self.iter
@@ -505,24 +507,26 @@
         return retval
 
     def get_dict(self):
-        cdef DBusMessageIter dict_iter
-        cdef DBusMessageIter* old_iter
+        cdef DBusMessageIter c_dict_iter
+        cdef MessageIter dict_iter
+        
+        dbus_message_iter_init_dict_iterator(self.iter, &c_dict_iter)
+
+        dict_iter = MessageIter()
+        dict_iter.__cinit__(&c_dict_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
+
+        end_of_dict = False
 
         while True:
-            key = self.get_dict_key()
-            value = self.get()
+            key = dict_iter.get_dict_key()
+            value = dict_iter.get()
             dict[key] = value
-            if not self.has_next():
+            if not dict_iter.has_next():
                 break
-            self.next()
+            dict_iter.next()
 
-        self.iter = old_iter
         return dict
 
     def get_arg_type(self):
@@ -562,6 +566,7 @@
     def get_byte_array(self):
         cdef int len
         cdef unsigned char *retval
+        cdef int i
         dbus_message_iter_get_byte_array(self.iter, &retval, <int*>&len)
         list = []
         for i from 0 <= i < len:
@@ -573,6 +578,7 @@
     def get_int32_array(self):
         cdef int len
         cdef dbus_int32_t *retval
+        cdef int i
         dbus_message_iter_get_int32_array(self.iter, &retval, <int*>&len)
         python_list = []
         for i from 0 <= i < len:
@@ -582,6 +588,7 @@
     def get_uint32_array(self):
         cdef int len
         cdef dbus_uint32_t *retval
+        cdef int i
         dbus_message_iter_get_uint32_array(self.iter, &retval, <int*>&len)
         python_list = []
         for i from 0 <= i < len:
@@ -591,6 +598,7 @@
     def get_double_array(self):
         cdef int len
         cdef double *retval
+        cdef int i
         dbus_message_iter_get_double_array(self.iter, &retval, <int*>&len)
         python_list = []
         for i from 0 <= i < len:
@@ -601,7 +609,7 @@
     def get_string_array(self):
         cdef int len
         cdef char **retval
-        
+        cdef int i
         dbus_message_iter_get_string_array(self.iter, &retval, <int*>&len)
         list = []
         for i from 0 <= i < len:
@@ -611,7 +619,7 @@
     def get_object_path_array(self):
         cdef int len
         cdef char **retval
-        
+        cdef int i
         dbus_message_iter_get_object_path_array(self.iter, &retval, <int*>&len)
         list = []
         for i from 0 <= i < len:
@@ -628,13 +636,15 @@
             retval = self.append_boolean(value)
         elif value_type == int:
             retval = self.append_int32(value)
-        elif value_type == float:
-            retval = self.append_double(value)
         elif value_type == str:
             retval = self.append_string(value)
+        elif value_type == float:
+            retval = self.append_double(value)
+        elif value_type == dict:
+            retval = self.append_dict(value)
         elif value_type == list:
             if (len(value) == 0):
-                raise TypeError, "Empty list"
+                raise TypeError, "Empty lists are currently not supported, return None instead"
             list_type = type(value[0])
             if list_type == str:
                 self.append_string_array(value)
@@ -682,11 +692,27 @@
     def append_object_path(self, value):
         return dbus_message_iter_append_object_path(self.iter, value)
 
-    # FIXME: append_array, append_dict_array, append_boolean_array, append_uint32_array
+    # FIXME: append_array, append_boolean_array, append_uint32_array
+
+    def append_dict(self, python_dict):
+        cdef DBusMessageIter c_dict_iter
+        cdef MessageIter dict_iter
+        
+        dbus_message_iter_append_dict(self.iter, &c_dict_iter)
+        
+        dict_iter = MessageIter()
+        dict_iter.__cinit__(&c_dict_iter)
+
+        for key, value in python_dict.iteritems():
+            if type(key) != str:
+                raise TypeError, "DBus dict keys must be strings"
+            dict_iter.append_dict_key(key)
+            dict_iter.append(value)
 
     def append_byte_array(self, python_list):
         cdef unsigned char * value
         cdef int length
+        cdef int i
         length = len(python_list)
         value = <unsigned char*>malloc(length * sizeof(unsigned char))
         for i from 0 <= i < length:
@@ -699,6 +725,7 @@
     def append_int32_array(self, python_list):
         cdef dbus_int32_t *value
         cdef int length
+        cdef int i
         length = len(python_list)
         value = <dbus_int32_t*>malloc(length * sizeof(dbus_int32_t))
         for i from 0 <= i < length:
@@ -711,6 +738,7 @@
     def append_double_array(self, python_list):
         cdef double *value
         cdef int length
+        cdef int i
         length = len(python_list)
         value = <double*>malloc(length * sizeof(double))
         for i from 0 <= i < length:
@@ -723,6 +751,7 @@
     def append_object_path_array(self, list):
         cdef char **value
         cdef int length
+        cdef int i
         length = len(list)
         value = <char**>malloc(length * sizeof(char *))
         for i from 0 <= i < length:
@@ -737,6 +766,7 @@
         cdef char **value
         cdef int length
         cdef dbus_bool_t return_code
+        cdef int i
         length = len(python_list)
         value = <char**>malloc(length * sizeof(char *))
         for i from 0 <= i < length:
@@ -868,7 +898,17 @@
         return self.msg
 
     def get_iter(self):
-        return MessageIter(self)
+        cdef DBusMessageIter iter
+        cdef MessageIter message_iter
+        cdef DBusMessage *msg
+
+        msg = self._get_msg()
+        dbus_message_iter_init(msg, &iter)
+
+        message_iter =  MessageIter()
+        message_iter.__cinit__(&iter)
+
+        return message_iter
 
     def get_args_list(self):
         retval = [ ]




More information about the dbus-commit mailing list