使用 SWIG 圍繞 C++ 類生成包裝器會(huì)在運(yùn)行時(shí)導(dǎo)致奇怪的斷言:Assertion failed!Program: C:\Python37\python37_d.dllFile: c:\_work\4\s\objects\typeobject.cLine: 3634Expression: PyTuple_Check(args)For information on how your program can cause an assertionfailure, see the Visual C++ documentation on asserts(Press Retry to debug the application - JIT must be enabled)如果在彈出的對話框上點(diǎn)擊忽略,一切似乎都正常。我創(chuàng)建了一個(gè)示例程序來嘗試復(fù)制問題并遇到了同樣的問題:主程序#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 onceclass 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, Extensionsetup(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"], )])
添加回答
舉報(bào)
0/150
提交
取消