【问题标题】:Segmentation Fault when Using PyArray_SimpleNewFromData使用 PyArray_SimpleNewFromData 时出现分段错误
【发布时间】:2018-06-18 01:48:07
【问题描述】:

其他用户提出了类似的问题,但没有一个答案对我有用。这是我的代码,灵感来自https://ubuntuforums.org/showthread.php?t=1266059

#include <stdio.h>
#include <Python.h>
#include "numpy/arrayobject.h"

int main() {
    printf("Embedding Python\n");
    PyObject *pName, *pModule, *pFunc;
    PyObject *pArgs, *pValue, *vec;

    const char* script = "TestScript";
    const char* function = "Test";

    Py_Initialize();

    pName = PyUnicode_FromString(script);
    pModule = PyImport_Import(pName);

    if (pModule != NULL) {
        printf("Step-1\n");

        pFunc = PyObject_GetAttrString(pModule, function);

        fflush( stdout );
        printf("Step-2\n");

        if (pFunc && PyCallable_Check(pFunc)) {
            // define a vector
            double* v = new double[3];
            v[0] = 1.0;
            v[1] = 2.0;
            v[2] = 3.0;             

            fflush( stdout );
            printf("Step-3\n");

            npy_intp vdim[] = { 3 };

            fflush( stdout );
            printf("Step-4\n");

            vec = PyArray_SimpleNewFromData(1, vdim, PyArray_DOUBLE, (void*)v);

            fflush( stdout );
            printf("Step-5");

            pArgs = PyTuple_New(2);
            PyTuple_SetItem(pArgs, 0, vec);
            PyTuple_SetItem(pArgs, 1, vec);

            pValue = PyObject_CallObject(pFunc, pArgs);
            Py_DECREF(pArgs);
            Py_DECREF(pName);

            if (pValue != NULL) {
                printf("Result of call: %f\n", PyFloat_AsDouble(pValue));
                Py_DECREF(pValue);
            }

            delete [] v;
        } else {
            if (PyErr_Occurred()) {
                PyErr_Print();
                fprintf(stderr, "Cannot find function \"%s\"\n", function);
            }
        }
    } else {
        PyErr_Print();
        //fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
        return 1;
    }

    Py_Finalize();
    return 0;
}

我在 Ubuntu 16.04 上使用 Python3.5 编译此代码。这是编译代码的命令

g++ main.cc -I/usr/include/python3.5m -I/usr/include/python3.5m -Wno-unused-result -Wsign-compare -g -fstack-protector-strong -Wformat -Werror=格式安全 -DNDEBUG -g -fwrapv -O3 -Wall -L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -L/usr/lib -lpython3.5m -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions -Wunused-function

In file included from /usr/include/python3.5m/numpy/ndarraytypes.h:1777:0,
                 from /usr/include/python3.5m/numpy/ndarrayobject.h:18,
                 from /usr/include/python3.5m/numpy/arrayobject.h:4,
                 from main.cc:3: /usr/include/python3.5m/numpy/npy_1_7_deprecated_api.h:15:2: warning:
#warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]  #warning "Using deprecated NumPy API, disable it by " \   ^ In file included from /usr/include/python3.5m/numpy/ndarrayobject.h:27:0,
                 from /usr/include/python3.5m/numpy/arrayobject.h:4,
                 from main.cc:3: /usr/include/python3.5m/numpy/__multiarray_api.h:1448:1: warning: ‘int
_import_array()’ defined but not used [-Wunused-function]  _import_array(void)

在执行时我得到以下输出

Embedding Python 
Step-1 
Step-2 
Step-3 
Step-4 
Segmentation fault(core dumped)

【问题讨论】:

    标签: c++ numpy segmentation-fault python-3.5


    【解决方案1】:

    你需要调用import_array()函数才能使用numpy的C-API,否则你会看到这种错误。

    https://docs.scipy.org/doc/numpy/reference/c-api.array.html?highlight=import_array#importing-the-api

    【讨论】:

    • 谢谢。该程序现在正在运行。但是,在 Python 端打印数组时,我得到 [1. 2. 3.] 而不是 [1, 2, 3,];有一个句号而不是逗号。这是为什么呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 2017-08-26
    • 2019-05-10
    • 2018-02-15
    • 2016-01-15
    • 2011-01-14
    • 2013-01-03
    相关资源
    最近更新 更多