【发布时间】:2013-12-20 09:27:39
【问题描述】:
我正在尝试使用 SWIG 将 C lib 包装到 python mod,但我无法让异常正常工作。这是代码的一个小例子,
except_test.i
%module except_test
%{
#include "except_test.h"
#include <stdio.h>
%}
%include "except_test.h"
%{
static int flagged_exception = 0;
void throw_except()
{
flagged_exception = 1;
}
%}
%exception {
$action
if (flagged_exception) {
PyErr_SetString(PyExc_RuntimeError, "test except");
flagged_exception = 0;
}
}
except_test.c:
int except_test(int a) {
if (a < 0) {
throw_except();
return 0;
} else{
return -1;
}
}
然后当我运行 except_test() 函数时,没有抛出异常
run_except.py
from except_test import *
b = except_test(-1)
print 'b=', b
运行:
$ python run_except.py
b= 0
$
这里有什么问题?
【问题讨论】:
-
throw语句在哪里?另请注意,C 没有异常。