如何调试从 Visual Studio 2015 年的 C++ 项目调用的 Python 代码

How to debug Python code called from a C++ project in Visual Studio 2015

我有一个 visual studio 2015 c++ 项目调用了一个 python 模块。

Microsoft 网站上的以下教程提供了一种在从 visual studio python 项目调用时调试 C++ 代码的方法。

参考:https://docs.microsoft.com/en-us/visualstudio/python/debugging-mixed-mode

同样可以调试被 C++ 程序调用的 python 代码

比如我的C++代码如下

#include "stdafx.h"
#include <iostream>
#ifdef _DEBUG
#undef _DEBUG
#include <python.h>
#define _DEBUG
#else
#include <python.h>
#endif
int _tmain(int argc, _TCHAR* argv[])
{
    printf("Calling Python to find the sum of 2 and 2.\n");
    // Initialize the Python interpreter.
    Py_Initialize();

    // Create some Python objects that will later be assigned values.
    PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;

    // Convert the file name to a Python string.
    pName = PyUnicode_FromString("sample");

    // Import the file as a Python module.
    pModule = PyImport_Import(pName);

    // Create a dictionary for the contents of the module.
    pDict = PyModule_GetDict(pModule);

    // Get the add method from the dictionary.
    pFunc = PyDict_GetItemString(pDict, "add");

    // Create a Python tuple to hold the arguments to the method.
    pArgs = PyTuple_New(2);

    // Convert 2 to a Python integer.
    pValue = PyLong_FromLong(2);

    // Set the Python int as the first and second arguments to the method.
    PyTuple_SetItem(pArgs, 0, pValue);
    PyTuple_SetItem(pArgs, 1, pValue);

    // Call the function with the arguments.
    PyObject* pResult = PyObject_CallObject(pFunc, pArgs);

    // Print a message if calling the method failed.
    if (pResult == NULL)
        printf("Calling the add method failed.\n");

    // Convert the result to a long from a Python object.
    long result = PyLong_AsLong(pResult);

    // Destroy the Python interpreter.
    Py_Finalize();

    // Print the result.
    printf("The result is %d.\n", result); std::cin.ignore(); return 0;

}

调用 Python 3 代码如下

# Returns the sum of two numbers.
def add(a, b):
    c = a + b
    return c

调试时我想在 python 代码中的以下指令中放置一个断点

    c = a + b

所以在我按 F11 到达 C++ 代码中的以下指令后,visual studio 应该进入 python 代码

PyObject* pResult = PyObject_CallObject(pFunc, pArgs);

答案作为注释提供在 https://docs.microsoft.com/en-us/visualstudio/python/debugging-mixed-mode,其中指出

Mixed-mode debugging as described here is enabled only when you have a Python project loaded into Visual Studio. That project determines the Visual Studio's debugging mode, which is what makes the mixed-mode option available. If, however, you have a C++ project loaded (as you would when embedding Python in another application as described on python.org, then Visual Studio uses the native C++ debugger that doesn't support mixed-mode debugging.

In this case, start the C++ project without debugging (Debug > Start without debugging or Ctrl+F5), and then use Debug > Attach to Process.... In the dialog that appears, select the appropriate process, then use the Select... button to open the Select Code Type dialog in which you can select Python as shown below. Select OK to close that dialog, then Attach to start the debugger. Note that you may need to introduce a suitable pause or delay in the C++ app to ensure that it doesn't call the Python you want to debug before you can attach the debugger.