Python C API:使用 MSVC 尝试示例模块时的访问冲突



我正在用C++制作一个Python模块。到目前为止,我使用MingW编译模块,工作正常。但是我想切换到 MSVC,因为我使用的其他库更容易与 MSVC 一起使用。

但是,我无法让它工作。编译和链接有效,但是当我尝试使用来自 Python 的新创建的模块时会出现问题。当我打电话给PyType_Ready时,我收到错误SystemError: Type does not define the tp_name field.。在 Python 的源代码中,我看到当tp_name为 null 时报告此错误,但我检查它不是 null。

我的模块太大而无法调试,所以我尝试了 Python 文档的示例模块,这给出了一个不同的问题。这是我的文件和run.bat的输出.

自定义.c

#define PY_SSIZE_T_CLEAN
#include <Python.h>
typedef struct {
PyObject_HEAD
/* Type-specific fields go here. */
} CustomObject;
static PyTypeObject CustomType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "custom.Custom",
.tp_doc = "Custom objects",
.tp_basicsize = sizeof(CustomObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
};
static PyModuleDef custommodule = {
PyModuleDef_HEAD_INIT,
.m_name = "custom",
.m_doc = "Example module that creates an extension type.",
.m_size = -1,
};
PyMODINIT_FUNC
PyInit_custom(void)
{
PyObject *m;
if (PyType_Ready(&CustomType) < 0)
return NULL;
m = PyModule_Create(&custommodule);
if (m == NULL)
return NULL;
Py_INCREF(&CustomType);
if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) {
Py_DECREF(&CustomType);
Py_DECREF(m);
return NULL;
}
return m;
}

CMakeList.txt

cmake_minimum_required(VERSION 3.15)
project(module)
add_library(module SHARED custom.c)
target_include_directories(module PRIVATE "C:/Program Files/Python37/include")
target_link_libraries(module "C:/Program Files/Python37/libs/python37_d.lib")
# Output name
set_target_properties(module PROPERTIES PREFIX "")
set_target_properties(module PROPERTIES OUTPUT_NAME "custom")
set_target_properties(module PROPERTIES SUFFIX ".pyd")

跑.bat

@echo off
echo ----- Compiling -----
cmake .
cmake --build .
echo.
echo ----- Running -----
python -c "import sys; sys.path.append('Debug'); import custom; print('ok')"
echo Error code: %errorlevel%

输出

Microsoft Windows [Version 10.0.17763.914]
(c) 2018 Microsoft Corporation. All rights reserved.
C:xpythontest>run
----- Compiling -----
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.17763.
-- The C compiler identification is MSVC 19.24.28314.0
-- The CXX compiler identification is MSVC 19.24.28314.0
-- Check for working C compiler: H:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: H:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: H:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: H:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/x/pythontest
Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Checking Build System
Building Custom Rule C:/x/pythontest/CMakeLists.txt
custom.c
Creating library C:/x/pythontest/Debug/custom.lib and object C:/x/pythontest/Debug/custom.exp
module.vcxproj -> C:xpythontestDebugcustom.pyd
Building Custom Rule C:/x/pythontest/CMakeLists.txt
----- Running -----
Error code: -1073741819

错误代码 -1073741819 0xC0000005,即访问冲突。因此,示例代码崩溃。这是一个与我自己的模块不同的错误,但它可能具有相同的原因。我做错了什么?

我找到了解决方案:我需要在发布模式而不是调试模式下构建并链接到 python37.lib 而不是 python37_d.lib。

最新更新