首页 > 解决方案 > Python C API:尝试使用 MSVC 的示例模块时访问冲突

问题描述

我正在用 C++ 制作一个 Python 模块。到目前为止,我使用 MingW 编译模块,效果很好。但我想切换到 MSVC,因为我使用的另一个库更容易与 MSVC 一起使用。

但是,我无法让它工作。编译和链接工作,但是当我尝试使用 Python 新创建的模块时会出现问题。SystemError: Type does not define the tp_name field.我打电话时收到错误PyType_Ready。在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;
}

CMakeLists.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:\x\pythontest>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:\x\pythontest\Debug\custom.pyd
  Building Custom Rule C:/x/pythontest/CMakeLists.txt

----- Running -----
Error code: -1073741819

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

标签: pythonc++cpython-c-api

解决方案


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


推荐阅读