From 0bc4a3fa66423268ce17b57af8a5b1775dbb8a9a Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 16 May 2015 17:22:22 +0200 Subject: update bus.write MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/bus.py | 12 ++++++++++-- src/native_bus.pyx | 10 +++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/bus.py b/src/bus.py index 4b3f9fd..7146e83 100644 --- a/src/bus.py +++ b/src/bus.py @@ -55,6 +55,12 @@ class Bus: Fail if interrupted ''' + NOWAIT = 1 + ''' + Function shall fail with errno set to `EAGAIN` + if the it would block and this flag is used + ''' + def __init__(self, pathname : str = None): ''' @@ -131,14 +137,16 @@ class Bus: self.bus = None - def write(self, message : str): + def write(self, message : str, flags : int = 0): ''' Broadcast a message a bus @param message:str The message to write, may not be longer than 2047 bytes after UTF-8 encoding + @param flags:int `Bus.NOWAIT` if the function shall fail if there is another process attempting + to broadcast on the bus ''' from native_bus import bus_write_wrapped - if bus_write_wrapped(self.bus, message) == -1: + if bus_write_wrapped(self.bus, message, flags) == -1: raise self.__oserror() diff --git a/src/native_bus.pyx b/src/native_bus.pyx index de33e91..fdcf341 100644 --- a/src/native_bus.pyx +++ b/src/native_bus.pyx @@ -68,13 +68,15 @@ Close a bus @return 0 on success, -1 on error ''' -cdef extern int bus_write(long, const char *) +cdef extern int bus_write(long, const char *, int) ''' Broadcast a message a bus @param bus Bus information @param message The message to write, may not be longer than `BUS_MEMORY_SIZE` including the NUL-termination +@param flags `BUS_NOWAIT` fail if other process is attempting + to write @return 0 on success, -1 on error ''' @@ -184,20 +186,22 @@ def bus_close_wrapped(bus : int) -> int: return bus_close(bus) -def bus_write_wrapped(bus : int, message : str) -> int: +def bus_write_wrapped(bus : int, message : str, flags : int) -> int: ''' Broadcast a message a bus @param bus Bus information @param message The message to write, may not be longer than `BUS_MEMORY_SIZE` including the NUL-termination + @param flags `BUS_NOWAIT` fail if other process is attempting + to write @return 0 on success, -1 on error ''' cdef const char* cmessage cdef bytes bs bs = message.encode('utf-8') + bytes([0]) cmessage = bs - return bus_write(bus, cmessage) + return bus_write(bus, cmessage, flags) cdef int bus_callback_wrapper(const char *message, user_data): -- cgit v1.2.3-70-g09d2