try..catch macro wrapper equivalent

30 views
Skip to first unread message

Justin Israel

unread,
Apr 28, 2013, 7:06:31 AM4/28/13
to cython...@googlegroups.com
Hey all,

I'm wrapping a large number of C++ functions that can raise an exception if the underlying socket connection is lost. While I have figured out how to wrap my "get connection" function to re-establish the connection and/or try other available servers in a list, I cannot figure out a solution to create a try..except wrapper to provide to the 80+ C++ functions.

i.e.

#-------------
*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.pyx*

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 of these C++ functions in something like a try_call() ?
If this were pure python, I could simply do something like this:

def try_call(fn, *args, **kwargs):
    # try fn(*args, **kwargs) and handle

But obviously I cannot pass these as python objects.

-- justin

Justin Israel

unread,
May 17, 2013, 9:57:17 PM5/17/13
to cython...@googlegroups.com
I ended up just making a python decorator to apply to my functions and method, but it required that I ditch the cpdef / inline in favor of def. Anyone have any suggestions?
Reply all
Reply to author
Forward
0 new messages