From 69b62ab9e9fbb57f47f9f0bb3cbaab1b6d0f3087 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 23 Apr 2015 05:06:08 +0200 Subject: suppress error on exit and fix callback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/bus.py | 6 +++++- src/native_bus.pyx | 9 ++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/bus.py b/src/bus.py index 5502500..91ea5da 100644 --- a/src/bus.py +++ b/src/bus.py @@ -120,7 +120,10 @@ class Bus: ''' Close the bus ''' - from native_bus import bus_close_wrapped, bus_deallocate + try: + from native_bus import bus_close_wrapped, bus_deallocate + except: + return if self.bus is not None: if bus_close_wrapped(self.bus) == -1: raise self.__oserror() @@ -153,6 +156,7 @@ class Bus: 0: stop listening 1: continue listening -1: an error has occurred + NB! The received message will not be decoded from UTF-8 @param user_data See description of `callback` ''' from native_bus import bus_read_wrapped diff --git a/src/native_bus.pyx b/src/native_bus.pyx index 016fa3c..b6f3127 100644 --- a/src/native_bus.pyx +++ b/src/native_bus.pyx @@ -200,6 +200,12 @@ def bus_write_wrapped(bus : int, message : str) -> int: return bus_write(bus, cmessage) +cdef int bus_callback_wrapper(const char *message, user_data): + cdef bytes bs = message + callback, user_data = tuple(user_data) + return callback(bs, user_data) + + def bus_read_wrapped(bus : int, callback : callable, user_data) -> int: ''' Listen (in a loop, forever) for new message on a bus @@ -217,5 +223,6 @@ def bus_read_wrapped(bus : int, callback : callable, user_data) -> int: -1: an error has occurred @return 0 on success, -1 on error ''' - return bus_read(bus, callback, user_data) + user = (callback, user_data) + return bus_read(bus, &bus_callback_wrapper, user) -- cgit v1.2.3-70-g09d2