将Python嵌入C++接收错误分段故障(核心倾倒)

发布于 2025-01-27 16:13:31 字数 1284 浏览 4 评论 0原文

这是我第一次嵌入C ++中的Python。

我只是想创建一个简单的程序,因此我了解它的工作原理。

以下是我的代码。

main.cpp

#define PY_SSIZE_T_CLEAN
#include </usr/include/python3.8/Python.h>
#include <iostream>


int main(int argc, char *argv[]){
    
    PyObject *pName, *pModule, *pFunc, *pArgs, *pValue;

    Py_Initialize();


    pName = PyUnicode_FromString((char*)"script");
    pModule = PyImport_Import(pName);
    pFunc = PyObject_GetAttrString(pModule, (char*)"test");
    pArgs = PyTuple_Pack(1, PyUnicode_FromString((char*)"Greg"));
    pValue = PyObject_CallObject(pFunc, pArgs);
    
    auto result = _PyUnicode_AsString(pValue);
    std::cout << result << std::endl;

    Py_Finalize();

      

}

script.py

def test(person):
    return "What's up " + person;

这是我在Linux上编译的方式

g++ -I/usr/include/python3.8/ main.cpp -L/usr/lib/python3.8/config-3.8-x86_64-linux-gnu -lpython3.8 -o output

,因为(#include&lt; python.h&gt;是的,是的,是的,是的,我尝试了sudo apt apt-get install python3.8-dev )

文件成功编译,但是当我尝试运行./Output时,我会收到以下错误。

Segmentation fault (core dumped)

我搜索了此错误的含义,并且说分割故障是访问“不属于您”的内存引起的一种特定类型的错误。

但是哪个记忆不属于我?是Python文件吗?

任何指导都将不胜感激。

This is my first go at Embedding Python in C++.

I am just trying to create a simple program so I understand how it works.

The following is my code.

main.cpp

#define PY_SSIZE_T_CLEAN
#include </usr/include/python3.8/Python.h>
#include <iostream>


int main(int argc, char *argv[]){
    
    PyObject *pName, *pModule, *pFunc, *pArgs, *pValue;

    Py_Initialize();


    pName = PyUnicode_FromString((char*)"script");
    pModule = PyImport_Import(pName);
    pFunc = PyObject_GetAttrString(pModule, (char*)"test");
    pArgs = PyTuple_Pack(1, PyUnicode_FromString((char*)"Greg"));
    pValue = PyObject_CallObject(pFunc, pArgs);
    
    auto result = _PyUnicode_AsString(pValue);
    std::cout << result << std::endl;

    Py_Finalize();

      

}

script.py

def test(person):
    return "What's up " + person;

This is how I have been compiling on Linux

g++ -I/usr/include/python3.8/ main.cpp -L/usr/lib/python3.8/config-3.8-x86_64-linux-gnu -lpython3.8 -o output

I am compiling like this because (#include <Python.h> has been giving me troubles, Yes I have tried sudo apt-get install python3.8-dev)

The file compiles successfully but when I try to run ./output I receive the following error.

Segmentation fault (core dumped)

I searched up what this error means and it is saying that Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.”

But which memory does not belong to me? Is it the python file?

Any Guidance would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是我的挚爱i 2025-02-03 16:13:31

在这些语句中的每一个之后,您都需要使用某些形式检查错误:

if (varname == NULL) {
    cout << “An error occured” << endl;
    PyErr_Print();
    return EXIT_FAILURE;
 }

这将检查是否通过错误来检查Python层;如果是这样,将要求它将Python Trackback打印到屏幕上,然后退出。您可以使用此追溯来弄清楚您的错误是什么。

这些功能中的任何一个都可能失败,并且您需要在继续之前检查故障。因此,使用Python c API非常谨慎。大多数C API函数返回指针返回错误时返回null,并将NULL传递到任何函数而无需首先检查它的情况下会导致崩溃。

您可以从访问空指针中获得细分故障,因为几乎所有现代系统都会将NULL的访问访问到分段故障或某种崩溃以捕获编程错误。

After each and every one of those statements, you will need to check for errors, using something of the form:

if (varname == NULL) {
    cout << “An error occured” << endl;
    PyErr_Print();
    return EXIT_FAILURE;
 }

This will check if the python layer through an error; and if so will ask it to print the Python traceback to the screen, and exit. You can use this traceback to figure out what your error is.

Any of those functions can fail, and you need to check for failure before you continue. Using the Python C API is extremely fiddly because of this. Most C API functions that return a pointer return NULL on error, and passing NULL into any function without checking it first is bound to result in a crash.

You get a segmentation fault from accessing a NULL pointer, as nearly all modern systems map access of NULL to a segmentation fault or crash of some sort to catch programming errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文