gpt4 book ai didi

c++ - 将 c++ 异常传播到 cython - python 异常

转载 作者:太空狗 更新时间:2023-10-29 20:28:37 26 4
gpt4 key购买 nike

我对 Cython 0.17.1 有疑问

如果文件不存在,我的函数将抛出 std::runtime_error,我想以某种方式将此异常传播到我的 Cython 代码。

void loadFile(const string &filename)
{
// some code, if filename doesn't exists
throw std::runtime_error( std::string("File doesn't exists" ) );
}

在正确包装函数后来自 Cython:

try:
loadFile(myfilename)
except RuntimeError:
print "Can't load file"

但是这个异常总是被忽略,我怎样才能从 Python 中捕获 c++ 异常?

最佳答案

你是用extern声明异常处理吗?您应该阅读 C++ 异常处理:http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#exceptions

基本上,您需要执行以下操作:

cdef extern from "some_file.h":
cdef int foo() except +

关于c++ - 将 c++ 异常传播到 cython - python 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13185213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com