Numpy C-Api 示例给出了 SegFault

发布于 2024-12-09 01:57:04 字数 1761 浏览 0 评论 0原文

我试图了解 Python C-Api 的工作原理,并且我想在 Python 和 C 扩展之间交换 numpy 数组。

因此,我开始了本教程: http://dsnra.jpl.nasa.gov/software /Python/numpydoc/numpy-13.html

尝试在那里做第一个例子,一个计算 2d numpy 数组轨迹的 C 模块,对我来说非常简洁,因为我想做基本操作也在二维数组中。

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

int main(){
Py_Initialize();
import_array();
}

static char doc[] =
"This is the C extension for xor_masking routine";

    static PyObject *
    trace(PyObject *self, PyObject *args)
    {
    PyObject *input;
    PyArrayObject *array;
    double sum;
    int i, n;

    if (!PyArg_ParseTuple(args, "O", &input))
    return NULL;
    array = (PyArrayObject *)
    PyArray_ContiguousFromObject(input, PyArray_DOUBLE, 2, 2);
    if (array == NULL)
    return NULL;

    n = array->dimensions[0];
    if (n > array->dimensions[1])
    n = array->dimensions[1];
    sum = 0.;
    for (i = 0; i < n; i++)
    sum += *(double *)(array->data + i*array->strides[0] + i*array->strides[1]);
    Py_DECREF(array);
    return PyFloat_FromDouble(sum);
    }

static PyMethodDef TraceMethods[] = {
    {"trace", trace, METH_VARARGS, doc},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
inittrace(void)
{
    (void) Py_InitModule("trace", TraceMethods);
}


}

该模块的名称是trace,并且使用setup.py文件进行编译:

from distutils.core import setup, Extension

module = Extension('trace', sources = ['xor_masking.cpp'])
setup(name = 'Trace Test', version = '1.0', ext_modules = [module])

该文件已编译,trace.so已在IPython中导入,但是当我尝试使用trace()方法时,出现了分段错误,我不知道不知道为什么。

我使用 Fedora 15、Python 2.7.1、gcc 4.3.0、Numpy 1.5.1 运行此程序

I'm trying to understand how the Python C- Api works, and I want to exchange numpy arrays between Python and a C Extension.

So, I started this tutorial: http://dsnra.jpl.nasa.gov/software/Python/numpydoc/numpy-13.html

Tried to do the first example there, a C module that calculates the trace of a 2d numpy array, was very neat for me, since I want to do elementary operations in 2d arrays also.

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

int main(){
Py_Initialize();
import_array();
}

static char doc[] =
"This is the C extension for xor_masking routine";

    static PyObject *
    trace(PyObject *self, PyObject *args)
    {
    PyObject *input;
    PyArrayObject *array;
    double sum;
    int i, n;

    if (!PyArg_ParseTuple(args, "O", &input))
    return NULL;
    array = (PyArrayObject *)
    PyArray_ContiguousFromObject(input, PyArray_DOUBLE, 2, 2);
    if (array == NULL)
    return NULL;

    n = array->dimensions[0];
    if (n > array->dimensions[1])
    n = array->dimensions[1];
    sum = 0.;
    for (i = 0; i < n; i++)
    sum += *(double *)(array->data + i*array->strides[0] + i*array->strides[1]);
    Py_DECREF(array);
    return PyFloat_FromDouble(sum);
    }

static PyMethodDef TraceMethods[] = {
    {"trace", trace, METH_VARARGS, doc},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
inittrace(void)
{
    (void) Py_InitModule("trace", TraceMethods);
}


}

The module's name is trace, and it is compiled with the setup.py file:

from distutils.core import setup, Extension

module = Extension('trace', sources = ['xor_masking.cpp'])
setup(name = 'Trace Test', version = '1.0', ext_modules = [module])

The file is compiled, trace.so is imported in IPython, but when I try to use the method trace(), I get a Segmentation Fault, I don't know why.

I run this with Fedora 15, Python 2.7.1, gcc 4.3.0, Numpy 1.5.1

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

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

发布评论

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

评论(1

温柔女人霸气范 2024-12-16 01:57:04

您的模块的 init 函数需要

import_array();

之后

(void) Py_InitModule("trace", TraceMethods);

在教程顶部附近提到这一点 调用,但很容易错过。如果没有这个,它就会在 PyArray_ContigouslyFromObject 上出现段错误。

Your init function for the module needs to call

import_array();

after

(void) Py_InitModule("trace", TraceMethods);

It mentions this in the tutorial near the top, but it is easy to miss. Without this, it segfaults on PyArray_ContiguousFromObject.

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