Try..catch shell macro equivalent in cython

I wrap a large number of C ++ functions that can throw an exception if the underlying socket connection is lost. Although I figured out how to wrap the "get the connection" function to restore the connection and / or try other available servers in the list, I cannot find a solution to create a try..except shell to provide 80 + C ++.

#-- client.pxd --- cdef extern from "rpc/RpcService.h": cdef cppclass RpcServiceClient: void getProject(ProjectT&, Guid& id) nogil except + cdef extern from "client.h": cdef cppclass Client: RpcServiceClient proxy() nogil cdef Client* getClient() nogil except + #-- module.pxd --- cdef inline Client* conn() except *: # wrap getClient() here with try..except if the # connection was never established cpdef inline get_project(Guid& guid): cdef: ProjectT projT # cpp object Project project # cdef python class # this would catch fine in my conn() wrapper # if the connection had never been established # the first time. But if the existing connection # suddenly drops, it will be getProject() that # raises the exception conn().proxy().getProject(projT, guid) project = initProject(projT) return project 

Any tips on how I can wrap all these C ++ functions in something like try_call() ? If it were pure python, I could just do something like this:

 def try_call(fn, *args, **kwargs): # try fn(*args, **kwargs) and handle try_call(conn().proxy().getProject, projT, guid) 

But obviously, I cannot pass these cython functions as python objects (or maybe I can?).

Or something like this in C ++:

 TRY_CALL_OR_RECONNECT conn().proxy().getProject(projT, guid) END_TRY_CALL_OR_RECONNECT 
+7
source share
1 answer

you can check decorators

 def try_wrapper(x): try: x() except: doSomethingElse() @try_wrapper def defYouWantToWrap(): doSomething() 

it may not be the best tutorial, but hopefully it can point you in the right direction

0
source

All Articles