我正在尝试从:SWIG 2.0.11和Python 2.7.12升级到SWIG 3.0.12和Python 3.6,但是当我在任何迭代器上运行测试(使用%template自动生成)时,我得到以下异常:
SystemError: <built-in function xxx_iterator> returned a result with an error set
Run Code Online (Sandbox Code Playgroud)
例如,即使最简单的迭代也会失败:
Traceback (most recent call last):
File "testRender.py", line 459, in testRender
for v in vertices:
File "ncore.py", line 90833, in __iter__
return self.iterator()
File "ncore.py", line 90830, in iterator
return _ncore.Vertices_iterator(self)
SystemError: <built-in function Vertices_iterator> returned a result with an error set
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
再次,这一切都适用于SWIG 2.0.11和Python 2.7.12 ....
编辑:添加更简单的示例:
它可能是任何模板生成的迭代器,因此,例如,在.i文件中定义的此模板:
%template(Ints) std::list<int>;
Run Code Online (Sandbox Code Playgroud)
使用这个简单的代码时会失败:
intsList = ncore.Ints()
intsList.append(1)
intsList.append(2)
for i in intsList:
print(i)
Run Code Online (Sandbox Code Playgroud)
使用类似于此的消息:
Traceback (most recent call last):
File "testRender.py", line 459, in testRender
for i in intsList:
File "ncore.py", line 90833, in __iter__
return self.iterator()
File "ncore.py", line 90830, in iterator
return _ncore.Ints_iterator(self)
SystemError: <built-in function Ints_iterator> returned a result with an error set
Run Code Online (Sandbox Code Playgroud)
pe3*_*e3k -1
这很奇怪,只是从头开始重新编译所有内容。然后我测试了你的简化示例(如果理解正确):
我的测试.i:
%module mytest
%{
#include <list>
using namespace std;
%}
%include "std_list.i"
namespace std {
%template(Ints) list<int>;
}
Run Code Online (Sandbox Code Playgroud)
编译步骤:
swig -Wall -c++ -python -py3 -o mytest_wrap.cpp mytest.i
g++ -c -g -ansi mytest_wrap.cpp -I/usr/local/include/python3.6m/ -fPIC -o mytest_wrap.o
g++ -g -ansi -o _mytest.so mytest_wrap.o -shared
Run Code Online (Sandbox Code Playgroud)
然后,将 mytest 模块导入到 python 中后,一切都变得非常顺利。
测试配置: