Python C API : Access Violation when trying example module with MSVC

标签 python c++ c python-c-api

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

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

我的模块太大而无法调试,所以我尝试了example module相反,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,这是访问冲突。所以示例代码崩溃了。这是与我自己的模块不同的错误,但它可能具有相同的原因。我做错了什么?

最佳答案

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

关于Python C API : Access Violation when trying example module with MSVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59395037/

相关文章:

c++ - boost_1_60_0 .zip 在 Windows 中的安装

c++ - 编译 OSL(开放着色语言)时出错,缺少 OPENIMAGEIO_LIBRARY_DIRS

c++ - 如何在 CentOS 7 上通过 clang 构建 libcxx 和 libcxxabi

python - 如何选择两列来绘制数据框?

python - 如何从使用构建器实例化的类继承?

python - 无法在 Mac 中从 Python 卸载 matplotlib、numpy 和 scipy。无法从 virtualenv 导入 matplotlib

c - 输出中 C 结构组件中的前导 0xff

python - 有没有办法记录重试次数

c - C 中括号内的 double

c - 使用命令行参数中的 getchar 和 putchar 来编码程序以发送到解码