c – try..catch宏包装器等效于cython

我正在包装大量的C函数,如果底层套接字连接丢失,可能引发异常.虽然我已经想出如何包装我的“获取连接”功能来重新建立连接和/或尝试列表中的其他可用服务器,但我无法找到一个解决方案来创建一个try..except包装器来提供给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

关于如何将所有这些C函数包装成try_call()之类的任何提示
如果这是纯python,我可以简单地做这样的事情:

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

try_call(conn().proxy().getProject,projT,guid)

但显然我不能将这些cython函数作为python对象传递(或者我可以吗?).

或者C中的这样的事情:

TRY_CALL_OR_RECONNECT
conn().proxy().getProject(projT,guid)
END_TRY_CALL_OR_RECONNECT

解决方法

你可能想看看装饰员
def try_wrapper(x):
    try:
        x()
    except:
        doSomethingElse()

@try_wrapper
def defYouWantToWrap():
    doSomething()

这可能不是最好的教程,但希望它可以指出你正确的方向

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...