* python/Makefile.am: Break on pyrexc errors instead of ignoring them
authorJohn (J5) Palmieri <johnp@redhat.com>
Thu, 1 Sep 2005 01:22:06 +0000 (01:22 +0000)
committerJohn (J5) Palmieri <johnp@redhat.com>
Thu, 1 Sep 2005 01:22:06 +0000 (01:22 +0000)
* python/dbus_bindings.pyx: Memory management foo
(global): remove hacky _user_data_references global list
(GIL_safe_cunregister_function_handler): userdata now stuffed into
tuples. Unref user_data
(GIL_safe_cmessage_function_handler): userdata now stuffed into tuples
(Connection::__del__): Remove and replace with __dealloc__ method
(Connection::add_filter): Stuff user_data into a tuple.  Use Py_INCREF
to keep tuple from being deallocated instead of the global var hack
(Connection::register_object_path): Stuff user_data into a tuple.
Use Py_INCREF to keep tuple from being deallocated instead of the
global var hack
(Connection::register_fallback): Stuff user_data into a tuple.
Use Py_INCREF to keep tuple from being deallocated instead of the
global var hack
(GIL_safe_pending_call_notification): Don't unref the message
because it gets unreffed when going out of scope.  Py_XDECREF
the user_data
(PendingCall::__del__): Remove and replace with __dealloc__ method
(PendingCall::set_notify): ref the pending call because we will
need it to stick around for when the notify callback gets called
(Message::__del__): Remove and replace with __dealloc__ method

* python/dbus_glib_bindings.pyx (init_gthreads): Changed to
gthreads_init to match up with the dbus call

* python/glib.py (init_threads): Changed to threads_init to match
up with gobject.threads_init().  init_threads is kept for backwards
compat but will most likely be deprecated in the future

* test/python/test-client.py:
- revamp to use Python's unittest functionality
- add async call tests
- setup threads in glib and dbus so we make sure locks are working

ChangeLog
python/Makefile.am
python/dbus_bindings.pyx
python/dbus_glib_bindings.pyx
python/glib.py
test/python/test-client.py

index de2b50b4a99a55d9f9035560a8c79f717513f385..750a1ec297728f46ec03fd61ed26cd99bb0a7069 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,41 @@
+2005-08-31  John (J5) Palmieri  <johnp@redhat.com>
+
+       * python/Makefile.am: Break on pyrexc errors instead of ignoring them
+
+       * python/dbus_bindings.pyx: Memory management foo
+       (global): remove hacky _user_data_references global list
+       (GIL_safe_cunregister_function_handler): userdata now stuffed into
+       tuples. Unref user_data
+       (GIL_safe_cmessage_function_handler): userdata now stuffed into tuples
+       (Connection::__del__): Remove and replace with __dealloc__ method
+       (Connection::add_filter): Stuff user_data into a tuple.  Use Py_INCREF
+       to keep tuple from being deallocated instead of the global var hack
+       (Connection::register_object_path): Stuff user_data into a tuple.
+       Use Py_INCREF to keep tuple from being deallocated instead of the 
+       global var hack
+       (Connection::register_fallback): Stuff user_data into a tuple.
+       Use Py_INCREF to keep tuple from being deallocated instead of the 
+       global var hack
+       (GIL_safe_pending_call_notification): Don't unref the message
+       because it gets unreffed when going out of scope.  Py_XDECREF
+       the user_data
+       (PendingCall::__del__): Remove and replace with __dealloc__ method
+       (PendingCall::set_notify): ref the pending call because we will
+       need it to stick around for when the notify callback gets called
+       (Message::__del__): Remove and replace with __dealloc__ method
+
+       * python/dbus_glib_bindings.pyx (init_gthreads): Changed to 
+       gthreads_init to match up with the dbus call
+
+       * python/glib.py (init_threads): Changed to threads_init to match
+       up with gobject.threads_init().  init_threads is kept for backwards
+       compat but will most likely be deprecated in the future
+
+       * test/python/test-client.py: 
+       - revamp to use Python's unittest functionality
+       - add async call tests
+       - setup threads in glib and dbus so we make sure locks are working
+       
 2005-08-30  John (J5) Palmieri  <johnp@redhat.com>
 
        * python/dbus_bindings.pyx 
index 52ba8e5b3d4e3e8418c809531beff18d7cc9ad1b..78bc134b387b916707071d30379e5ec50e61bf31 100644 (file)
@@ -38,8 +38,8 @@ dbus_bindings.pxd: $(srcdir)/dbus_bindings.pxd.in $(srcdir)/extract.py
        -$(PYTHON) $(srcdir)/extract.py $(srcdir)/dbus_bindings.pxd.in -I$(srcdir)/$(top_builddir)  -I$(srcdir) > $@.tmp && mv $@.tmp $@
 
 dbus_bindings.c: $(srcdir)/dbus_bindings.pyx dbus_bindings.pxd 
-       -pyrexc $(srcdir)/dbus_bindings.pyx -I. -o ./dbus_bindings.c
+       pyrexc $(srcdir)/dbus_bindings.pyx -I. -o ./dbus_bindings.c
 
 dbus_glib_bindings.c: $(srcdir)/dbus_glib_bindings.pyx dbus_bindings.pxd 
-       -pyrexc $(srcdir)/dbus_glib_bindings.pyx  -I. -o ./dbus_glib_bindings.c
+       pyrexc $(srcdir)/dbus_glib_bindings.pyx  -I. -o ./dbus_glib_bindings.c
 
index 2871ae8cb6686b2f02cdb237788b806a821d6be4..3108a3f5f2254706f6d3cce793eec1ab21e63e84 100644 (file)
@@ -63,9 +63,6 @@ ctypedef struct DBusObjectPathVTable:
   void (* dbus_internal_pad3) (void *)
   void (* dbus_internal_pad4) (void *)
 
-
-_user_data_references = [ ]
-
 class DBusException(Exception):
     pass
 
@@ -182,14 +179,15 @@ cdef void _GIL_safe_cunregister_function_handler (DBusConnection *connection,
                                                   void *user_data):
     cdef Connection conn
 
-    itup = <object>user_data
-    assert (type(tup) == list)    
+    tup = <object>user_data
+    assert (type(tup) == tuple)    
     function = tup[1]
     conn = Connection()
     conn.__cinit__(None, connection)
 
     args = (conn)
     function(*args)
+    Py_XDECREF(tup) 
 
 cdef void cunregister_function_handler (DBusConnection *connection,
                                         void *user_data):
@@ -210,7 +208,7 @@ cdef DBusHandlerResult _GIL_safe_cmessage_function_handler (
     cdef Message message
 
     tup = <object>user_data
-    assert (type(tup) == list)
+    assert (type(tup) == tuple)
     function = tup[0]
     message = EmptyMessage()
 
@@ -221,7 +219,7 @@ cdef DBusHandlerResult _GIL_safe_cmessage_function_handler (
     conn.__cinit__(None, connection)
     args = (conn,
             message)
-
     retval = function(*args)
 
     if (retval == None):
@@ -265,7 +263,7 @@ cdef class Connection:
             if dbus_error_is_set(&error):
                 raise DBusException, error.message
 
-    def __del__(self):
+    def __dealloc__(self):
         if self.conn != NULL:
             dbus_connection_unref(self.conn)
 
@@ -416,10 +414,9 @@ cdef class Connection:
     # FIXME: set_dispatch_status_function, get_unix_user, set_unix_user_function
 
     def add_filter(self, filter_function):
-        user_data = [ filter_function ]
-        global _user_data_references
-        _user_data_references.append(user_data)
-        
+        user_data = (filter_function,)
+        Py_XINCREF(user_data)
+       
         return dbus_connection_add_filter(self.conn,
                                           cmessage_function_handler,
                                           <void*>user_data,
@@ -459,9 +456,8 @@ cdef class Connection:
         cvtable.unregister_function = cunregister_function_handler 
         cvtable.message_function    = cmessage_function_handler
 
-        user_data = [message_cb, unregister_cb]
-        global _user_data_references
-        _user_data_references.append(user_data)
+        user_data = (message_cb, unregister_cb)
+        Py_XINCREF(user_data)
         
         return dbus_connection_register_object_path(self.conn, path, &cvtable,
                                                     <void*>user_data) 
@@ -472,10 +468,9 @@ cdef class Connection:
         cvtable.unregister_function = cunregister_function_handler 
         cvtable.message_function    = cmessage_function_handler
 
-        user_data = [message_cb, unregister_cb]
-        global _user_data_references
-        _user_data_references.append(user_data)        
-        
+        user_data = (message_cb, unregister_cb)
+        Py_XINCREF(user_data)
+       
         return dbus_connection_register_fallback(self.conn, path, &cvtable,
                                                  <void*>user_data) 
 
@@ -527,8 +522,8 @@ cdef void _GIL_safe_pending_call_notification (DBusPendingCall *pending_call,
     else:
         error_handler(DBusException('Unexpected Message Type: ' + message.type_to_name(type)))
 
-    dbus_message_unref(dbus_message)
     dbus_pending_call_unref(pending_call)
+    Py_XDECREF(<object>user_data)
 
 cdef void _pending_call_notification(DBusPendingCall *pending_call, 
                                      void *user_data):
@@ -555,7 +550,7 @@ cdef class PendingCall:
         self.pending_call = _pending_call
         dbus_pending_call_ref(self.pending_call)
 
-    def __del__(self):
+    def __dealloc__(self):
         if self.pending_call != NULL:
             dbus_pending_call_unref(self.pending_call)
 
@@ -580,6 +575,7 @@ cdef class PendingCall:
     def set_notify(self, reply_handler, error_handler):
         user_data = (reply_handler, error_handler)
         Py_XINCREF(user_data)
+        dbus_pending_call_ref(self.pending_call)
         dbus_pending_call_set_notify(self.pending_call, _pending_call_notification, 
                                      <void *>user_data, _pending_call_free_user_data)
         
@@ -1297,7 +1293,7 @@ cdef class Message:
                 self.msg = dbus_message_new_error(cmsg, error_name, error_message)
  
 
-    def __del__(self):
+    def __dealloc__(self):
         if self.msg != NULL:
             dbus_message_unref(self.msg)
             
index e432352a231d9c30e7f2e056f69496ee1938d347..3e2e33bdf1f62d2140905d26b5750b1b89fbc194 100644 (file)
@@ -13,5 +13,5 @@ def setup_with_g_main(conn):
    connection = conn
    dbus_connection_setup_with_g_main(connection._get_conn(), NULL)
 
-def init_gthreads ():
+def gthreads_init ():
     dbus_g_thread_init ()
index 8e7814225e810ae9fa61801134118a853a00dc0f..e1c3d9ca4c38d6dba99fae89828708220ad6647b 100644 (file)
@@ -5,11 +5,13 @@ def _setup_with_g_main(conn):
     dbus_glib_bindings.setup_with_g_main(conn._connection)
 
 _dbus_gthreads_initialized = False
-def init_threads():
+def threads_init():
     global _dbus_gthreads_initialized
     if not _dbus_gthreads_initialized:
-        dbus_glib_bindings.init_gthreads ()
+        dbus_glib_bindings.gthreads_init()
         _dbus_gthreads_initialized = True
 
+def init_threads():
+    threads_init()
 
 setattr(dbus, "_dbus_mainloop_setup_function", _setup_with_g_main)
index 51d655db60778b2116c79a2135512082a977762e..13cee958cdb70d3ac88389e26489f8aa03441cc1 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 import sys
 import os
+import unittest
 
 builddir = os.environ["DBUS_TOP_BUILDDIR"]
 pydir = builddir + "/python"
@@ -10,45 +11,90 @@ sys.path.insert(0, pydir + "/.libs")
 
 import dbus
 import dbus_bindings
+import gobject
+import dbus.glib
 
 if not dbus.__file__.startswith(pydir):
     raise Exception("DBus modules are not being picked up from the package")
 
-bus = dbus.SessionBus()
-remote_object = bus.get_object("org.freedesktop.DBus.TestSuitePythonService", "/org/freedesktop/DBus/TestSuitePythonObject")
-iface = dbus.Interface(remote_object, "org.freedesktop.DBus.TestSuiteInterface")
-
-try:
-    #test dbus_interface parameter 
-    print remote_object.Echo("dbus_interface test Passed", dbus_interface = "org.freedesktop.DBus.TestSuiteInterface")
+if not dbus_bindings.__file__.startswith(pydir):
+    raise Exception("DBus modules are not being picked up from the package")
 
-    #test introspection
-    print "\n********* Introspection Test ************"
-    print remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable")
-    print "Introspection test passed"
+class TestDBusBindings(unittest.TestCase):
+    def setUp(self):
+        self.bus = dbus.SessionBus()
+        self.remote_object = self.bus.get_object("org.freedesktop.DBus.TestSuitePythonService", "/org/freedesktop/DBus/TestSuitePythonObject")
+        self.iface = dbus.Interface(self.remote_object, "org.freedesktop.DBus.TestSuiteInterface")
 
-    #test sending python types and getting them back
-    print "\n********* Testing Python Types ***********"
-    test_vals = [1, 12323231, 3.14159265, 99999999.99,
+        self.test_types_vals = [1, 12323231, 3.14159265, 99999999.99,
                  "dude", "123", "What is all the fuss about?", "gob@gob.com",
                  [1,2,3], ["how", "are", "you"], [1.23,2.3], [1], ["Hello"],
                  (1,2,3), (1,), (1,"2",3), ("2", "what"), ("you", 1.2),
                  {1:"a", 2:"b"}, {"a":1, "b":2}, {1:1.1, 2:2.2}, {1.1:"a", 1.2:"b"}, 
                  [[1,2,3],[2,3,4]], [["a","b"],["c","d"]],
-                 ([1,2,3],"c", 1.2, ["a","b","c"], {"a": (1,"v"), "b": (2,"d")})]
+                 ([1,2,3],"c", 1.2, ["a","b","c"], {"a": (1,"v"), "b": (2,"d")})
+                 ]
+
+    def testInterfaceKeyword(self):
+        #test dbus_interface parameter
+        print self.remote_object.Echo("dbus_interface on Proxy test Passed", dbus_interface = "org.freedesktop.DBus.TestSuiteInterface")
+        print self.iface.Echo("dbus_interface on Interface test Passed", dbus_interface = "org.freedesktop.DBus.TestSuiteInterface")
+        self.assert_(True)
+        
+    def testIntrospection(self):
+        #test introspection
+        print "\n********* Introspection Test ************"
+        print self.remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable")
+        print "Introspection test passed"
+        self.assert_(True)
+
+    def testPythonTypes(self):
+        #test sending python types and getting them back
+        print "\n********* Testing Python Types ***********"
                  
-    for send_val in test_vals:
-        print "Testing %s"% str(send_val)
-        recv_val = iface.Echo(send_val)
-        #TODO: is this right in python - construct a better comparison
-        #      method
-        if send_val != recv_val:
-            raise Exception("Python type tests: %s does not equal %s"%(str(send_val), str(recv_val)))
-    
-    
-    
-except Exception, e:
-    print e
-    sys.exit(1)
-
-sys.exit(0)
+        for send_val in self.test_types_vals:
+            print "Testing %s"% str(send_val)
+            recv_val = self.iface.Echo(send_val)
+            self.assertEquals(send_val, recv_val)
+   
+    def testAsyncCalls(self):
+        #test sending python types and getting them back async
+        print "\n********* Testing Async Calls ***********"
+
+        
+        main_loop = gobject.MainLoop()
+        class async_check:
+            def __init__(self, test_controler, expected_result, do_exit):
+                self.expected_result = expected_result
+                self.do_exit = do_exit
+                self.test_controler = test_controler
+
+            def callback(self, val):
+                if self.do_exit:
+                    main_loop.quit()
+
+                self.test_controler.assertEquals(val, self.expected_result)
+                
+            def error_handler(error):
+                print error
+                if self.do_exit:
+                    main_loop.quit()
+
+                self.test_controler.assert_(val, False)
+        
+        last_type = self.test_types_vals[-1]
+        for send_val in self.test_types_vals:
+            print "Testing %s"% str(send_val)
+            check = async_check(self, send_val, last_type == send_val) 
+            recv_val = self.iface.Echo(send_val, 
+                                       reply_handler = check.callback,
+                                       error_handler = check.error_handler)
+            
+        main_loop.run()
+   
+if __name__ == '__main__':
+    gobject.threads_init()
+    dbus.glib.init_threads()
+
+    unittest.main()
+