swig c to python:抛出一个中止的实例后终止
SWIG C++ to Python: terminate called after throwing an instance of ... Aborted
我正在尝试编写一个SWIG模块,我似乎无法弄清楚如何从C 中捕获异常并将它们传播到Python。这是我的代码的简化版本:
示例.cpp:
#include "example.h"
Looper::Looper() {
nframes = 0;
}
void Looper::set_nframes(int nf) {
if (nf < 0) {
throw LooperValueError();
}
nframes = nf;
}
int Looper::get_nframes(void) {
return nframes;
}
示例.h:
class LooperValueError {};
class Looper {
private:
int nframes;
public:
Looper();
void set_nframes(int);
int get_nframes(void);
};
示例.i:
%module example
%{
#include "example.h"
%}
%include "example.h"
%exception {
try {
$function
} catch (LooperValueError) {
PyErr_SetString(PyExc_ValueError,"Looper value out of range");
return NULL;
}
}
这可以很好地构建。但是在Python中,当我致电looper.set_nframes(-2)时,我没有像我期望的那样获得价值。相反,代码解释器崩溃了:
terminate called after throwing an instance of 'LooperValueError'
Aborted
似乎没有被包装纸抓住。我在做什么错?
%exception
的效果仅适用于随后的声明。您在%include
之后写了%exception
,因此实际上并没有应用于任何内容。(请查看生成的代码以验证这一点 - 您的try/catch块实际上还没有达到输出)。
,您的界面应该像这样:
%module example
%{
#include "example.h"
%}
%exception {
try {
$function
} catch (const LooperValueError&) {
PyErr_SetString(PyExc_ValueError,"Looper value out of range");
return NULL;
}
}
%include "example.h"
我进行了调整的另一个小点:通常,您应该宁愿通过const引用而不是通过值来捕获异常。
相关文章:
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- 声明一个变量,该变量在 c++ 或 c 中具有值,当程序终止时不会被销毁
- 如何提前终止在另一个文件中定义的函数?
- 程序在其中一个案例完成后终止,但对于其他情况则不终止
- 我正在尝试计算 char 数组中的内容,直到 null 终止,但每次编译时,我都会得到一个比我的数组大的数字
- swig c to python:抛出一个中止的实例后终止
- 在抛出 std::bad_alloc 实例后终止调用.使用两个类,一个带有指向另一个的指针
- 对我是否需要一个哨兵值来终止我的 do while 循环以允许多个条目感到困惑
- 为什么一个指向char的C++指针在缺少null终止符的情况下充当字符串
- Winsock2 tcp/ip-一些数据包被忽略,可能是由于前一个数据包的null终止符
- 在终止值C++之前查找序列的最后一个值
- 当我采用测试用例数 = 1 时,我的程序终止.其他明智的是,它给出了正确的答案,但跳过了最后一个测试用例
- 我怎么能知道一个进程是什么时候创建的,并阻止它在windows中执行或终止它
- 是否有任何 pthread 函数可以在最后一个线程终止时调用某些内容
- 可以终止线程从另一个进程终止线程
- 从另一个线程终止C++中的一个线程
- 将一个线程(卡住的)从另一个线程终止
- C++将一个未终止的char*连接到std::字符串
- 我不希望一个作为守护进程的服务器进程关闭——即使在收到终止信号时也是如此.有什么办法可以确保这一点吗
- 获取一个 std::thread 来分离并终止自身