aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2015-04-23 05:06:08 +0200
committerMattias Andrée <maandree@operamail.com>2015-04-23 05:06:08 +0200
commit69b62ab9e9fbb57f47f9f0bb3cbaab1b6d0f3087 (patch)
treeaec14cec2dcb02354878ab3809d279a140248b8b
parentfix some errors (diff)
downloadpython-bus-1.0.tar.gz
python-bus-1.0.tar.bz2
python-bus-1.0.tar.xz
suppress error on exit and fix callback1.0
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--src/bus.py6
-rw-r--r--src/native_bus.pyx9
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(<long>bus, cmessage)
+cdef int bus_callback_wrapper(const char *message, user_data):
+ cdef bytes bs = message
+ callback, user_data = tuple(<object>user_data)
+ return <int>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(<long>bus, <int (*)(const char *, void *)><void *>callback, <void *>user_data)
+ user = (callback, user_data)
+ return bus_read(<long>bus, <int (*)(const char *, void *)>&bus_callback_wrapper, <void *>user)