From 897c90621a7cb85545864df9fd56c607dcef5585 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 17 Aug 2016 09:05:14 +0200 Subject: fixs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libcoopgamma.py | 11 ++++++----- src/libcoopgamma_native.pyx.gpp | 37 +++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/libcoopgamma.py b/src/libcoopgamma.py index c75ed04..ee0ec63 100644 --- a/src/libcoopgamma.py +++ b/src/libcoopgamma.py @@ -720,7 +720,7 @@ class Context: params = (self.fd, data) return 'libcoopgamma.Context(%s)' % ', '.join(repr(p) for p in params) - def connect(self, method, site): + def connect(self, method = None, site = None): ''' Connect to a coopgamma server, and start it if necessary @@ -736,12 +736,13 @@ class Context: ''' if method is not None and isinstance(method, int): method = str(method) - error = llibcoopgamma_native.ibcoopgamma_native_connect(method, site, self.address) - if error is not None: - if error == 0: + (successful, value) = libcoopgamma_native.libcoopgamma_native_connect(method, site, self.address) + if not successful: + if value == 0: raise ServerInitialisationError() else: - raise ErrorReport.create_error(error) + raise ErrorReport.create_error(value) + self.fd = value def detach(self): ''' diff --git a/src/libcoopgamma_native.pyx.gpp b/src/libcoopgamma_native.pyx.gpp index 1947981..b673340 100644 --- a/src/libcoopgamma_native.pyx.gpp +++ b/src/libcoopgamma_native.pyx.gpp @@ -989,7 +989,7 @@ def libcoopgamma_native_context_marshal(address : int): return int(errno) try: libcoopgamma_context_marshal(this, buf) - return bytes((buf[i]) for i in range(int(n))) + return bytes(int((buf[i])) for i in range(int(n))) finally: free(buf) @@ -1085,7 +1085,7 @@ def libcoopgamma_native_async_context_marshal(address : int): if buf is NULL: return int(errno) libcoopgamma_async_context_marshal(this, buf) - ret = bytes((buf[i]) for i in range(int(n))) + ret = bytes(int((buf[i])) for i in range(int(n))) finally: free(buf) return ret @@ -1133,16 +1133,33 @@ def libcoopgamma_native_connect(method : str, site : str, address : int): SIGCHLD must not be ignored or blocked - @param method:str? The adjustment method, `NULL` for automatic - @param site:str? The site, `NULL` for automatic - @param address:int The address of the state of the library, must be initialised - @return :int? `None`, 0 if the server on could not be initialised, - the value `errno` on other errors + @param method:str? The adjustment method, `NULL` for automatic + @param site:str? The site, `NULL` for automatic + @param address:int The address of the state of the library, must be initialised + @return :(:bool, :int) If [0] = `True`: [1] is the socket's file descriptor. + If [0] = `False`: [1] is 0 if the server on could not be + initialised, or the value `errno` on other errors ''' + cdef char* cmethod = NULL + cdef char* csite = NULL cdef libcoopgamma_context_t* ctx = address - if libcoopgamma_connect(method, site, ctx) < 0: - return int(errno) - return None + try: + if method is not None: + buf = method.encode('utf-8') + bytes([0]) + cmethod = malloc(len(buf) * sizeof(char)) + for i in range(len(buf)): + cmethod[i] = (buf[i]) + if site is not None: + buf = site.encode('utf-8') + bytes([0]) + csite = malloc(len(buf) * sizeof(char)) + for i in range(len(buf)): + csite[i] = (buf[i]) + if libcoopgamma_connect(cmethod, csite, ctx) < 0: + return (False, int(errno)) + return (True, int(ctx.fd)) + finally: + free(cmethod) + free(csite) def libcoopgamma_native_set_nonblocking(address : int, nonblocking : bool): -- cgit v1.2.3-70-g09d2