使用 SWIG 围绕 C++ 类生成包装器会在运行时导致奇怪的断言:
Assertion failed!
Program: C:\Python37\python37_d.dll
File: c:\_work\4\s\objects\typeobject.c
Line: 3634
Expression: PyTuple_Check(args)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts
(Press Retry to debug the application - JIT must be enabled)
如果在弹出的对话框上点击忽略,一切似乎都正常。
我创建了一个示例程序来尝试复制问题并遇到了同样的问题:
主程序
#include "testwrapper.h"
#pragma push_macro("slots")
#undef slots
#include "Python.h"
#pragma pop_macro("slots")
#include "SwigModules/generated/swig_runtime.h"
PyObject * ConvertToWrapper(SwigInterface * instance)
{
swig_type_info * pTypeInfo = SWIG_TypeQuery("SwigInterface *");
PyObject* obj = SWIG_NewPointerObj(instance, pTypeInfo, 0); <- issue occurs here
return obj;
}
TestWrapper * wrapper = new TestWrapper();
void TestSwig()
{
Py_Initialize();
PyRun_SimpleString("import test_module");
ConvertToWrapper(wrapper);
Py_Finalize();
}
int main(int argc, char *argv[])
{
TestSwig();
return 0;
}
测试包装器.h
#pragma once
#include "swiginterface.h"
class TestWrapper : public SwigInterface
{
public:
TestWrapper(){}
virtual ~TestWrapper(){}
virtual void Test();
};
swiginterface.h
#pragma once
class SwigInterface
{
public:
virtual ~SwigInterface(){}
virtual void Test() = 0;
};
test_module.i(swig 接口文件)
%module test_module
%{
#include "../swiginterface.h"
%}
%include "../SwigInterface.h"
%inline %{
SwigInterface * test;
%}
setup_function (python)
from distutils.core import setup, Extension
setup(name="test_module",
py_modules=['test_module'],
ext_modules=[Extension("_test_module",
["test_module.i"],
extra_compile_args=["-DSWIG_TYPE_TABLE=test_module"],
swig_opts=["-c++", "-py3"],
)])
ABOUTYOU
海绵宝宝撒
相关分类