gpt4 book ai didi

python - SWIG 在 Windows 中生成 C++ Python3 包装器导致断言 MSVC 2017

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:39:56 25 4
gpt4 key购买 nike

使用 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)

如果在弹出的对话框中单击忽略,一切似乎都正常。

我创建了一个示例程序来尝试重现该问题并遇到了同样的问题:

主要.cpp

#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接口(interface)文件)

%module test_module

%{
#include "../swiginterface.h"
%}

%include "../SwigInterface.h"

%inline %{
SwigInterface * test;
%}

设置函数( 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"],
)])

swig_runtime.h 中出现问题的函数:

SWIGRUNTIME PyObject* 
SWIG_Python_NewShadowInstance(SwigPyClientData *data, PyObject *swig_this)
{
#if (PY_VERSION_HEX >= 0x02020000)
PyObject *inst = 0;
PyObject *newraw = data->newraw;
if (newraw) {
inst = PyObject_Call(newraw, data->newargs, NULL);
if (inst) {
#if !defined(SWIG_PYTHON_SLOW_GETSET_THIS)
PyObject **dictptr = _PyObject_GetDictPtr(inst);
if (dictptr != NULL) {
PyObject *dict = *dictptr;
if (dict == NULL) {
dict = PyDict_New();
*dictptr = dict;
PyDict_SetItem(dict, SWIG_This(), swig_this);
}
}
#else
PyObject *key = SWIG_This();
PyObject_SetAttr(inst, key, swig_this);
#endif
}
} else {
#if PY_VERSION_HEX >= 0x03000000
inst = ((PyTypeObject*) data->newargs)->tp_new((PyTypeObject*) data->newargs, Py_None, Py_None); //<----- HERE
if (inst) {
PyObject_SetAttr(inst, SWIG_This(), swig_this);
Py_TYPE(inst)->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
}
#else
PyObject *dict = PyDict_New();
if (dict) {
PyDict_SetItem(dict, SWIG_This(), swig_this);
inst = PyInstance_NewRaw(data->newargs, dict);
Py_DECREF(dict);
}
#endif
}
return inst;

我已经用多个版本的 SWIG 试过了,结果都一样。它可以毫无问题地将模块加载到 C++ 代码和外部 python 解释器中。我该如何调试/解决这个问题?

最佳答案

为了解决这个问题,我需要从Python3.7降级到Python3.6

关于python - SWIG 在 Windows 中生成 C++ Python3 包装器导致断言 MSVC 2017,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53301146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com