Numpy C-Api 示例给出了 SegFault
我试图了解 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的模块的 init 函数需要
之后
在教程顶部附近提到这一点 调用,但很容易错过。如果没有这个,它就会在
PyArray_ContigouslyFromObject
上出现段错误。Your init function for the module needs to call
after
It mentions this in the tutorial near the top, but it is easy to miss. Without this, it segfaults on
PyArray_ContiguousFromObject
.