From 236c7b738488b5be33d5ab669479bb22a5f50ec3 Mon Sep 17 00:00:00 2001 From: "John (J5) Palmieri" Date: Thu, 1 Sep 2005 01:22:06 +0000 Subject: * 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 --- python/Makefile.am | 4 ++-- python/dbus_bindings.pyx | 40 ++++++++++++++++++---------------------- python/dbus_glib_bindings.pyx | 2 +- python/glib.py | 6 ++++-- 4 files changed, 25 insertions(+), 27 deletions(-) (limited to 'python') diff --git a/python/Makefile.am b/python/Makefile.am index 52ba8e5b..78bc134b 100644 --- a/python/Makefile.am +++ b/python/Makefile.am @@ -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 diff --git a/python/dbus_bindings.pyx b/python/dbus_bindings.pyx index 2871ae8c..3108a3f5 100644 --- a/python/dbus_bindings.pyx +++ b/python/dbus_bindings.pyx @@ -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 = user_data - assert (type(tup) == list) + tup = 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 = 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, 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, 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, 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(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, 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) diff --git a/python/dbus_glib_bindings.pyx b/python/dbus_glib_bindings.pyx index e432352a..3e2e33bd 100644 --- a/python/dbus_glib_bindings.pyx +++ b/python/dbus_glib_bindings.pyx @@ -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 () diff --git a/python/glib.py b/python/glib.py index 8e781422..e1c3d9ca 100644 --- a/python/glib.py +++ b/python/glib.py @@ -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) -- cgit