我在这里有一个最简单的测试用例:
%module test
%{
static char* MyExceptionName = "_test.MyException";
static PyObject* MyException = NULL;
%}
%inline %{
static PyObject* Foo()
{
PyErr_SetNone(MyException);
return NULL;
}
%}
%init
{
MyException = PyErr_NewException(MyExceptionName, NULL, NULL);
}
这是 setup.py 脚本:
from distutils.core import setup, Extension
setup(name="test", version="1.0",
ext_modules = [Extension("_test", ["test_wrap.c"])])
当我按如下方式构建和测试它时,我得到:
swig -python -threads test.i
python_d -c "import test; test.Foo()"
Fatal Python error: PyThreadState_Get: no current thread
我得到的回溯是
python27_d.dll!Py_FatalError(const char * msg=0x000000001e355a00) Line 1677 C
python27_d.dll!PyThreadState_Get() Line 330 C
python27_d.dll!PyErr_Restore(_object * type=0x00000000020d50b8, _object * value=0x0000000000000000, _object * traceback=0x0000000000000000) Line 27 + 0x5 bytes C
python27_d.dll!PyErr_SetObject(_object * exception=0x00000000020d50b8, _object * value=0x0000000000000000) Line 58 C
python27_d.dll!PyErr_SetNone(_object * exception=0x00000000020d50b8) Line 64 C
_test_d.pyd!Foo() Line 2976 C
环境:
- 赢7 64位,
- Python 2.7.3(默认,2012 年 8 月 15 日,18:18:52)[MSC v.1500 64 位 (AMD64)] 在 win32 上
- 痛饮 2.0.7