这个问题与我之前提出的问题有关。如果有人感兴趣,那就是这个。基本上,我想做的是使用Py_buffer
包装在memoryview
-object 中的 C 数组向 Python 公开。我已经使用它来工作PyBuffer_FillInfo
(工作=我可以在 Python 中操作数据并将其写入 C 中的标准输出),但是如果我尝试滚动我自己的缓冲区,我会在 C 函数返回后得到一个段错误。
我需要创建自己的缓冲区,因为 PyBuffer_FillInfo 假定格式为 char,使 itemsize 字段为 1。我需要能够提供大小为 1、2、4 和 8 的项目。
一些代码,这是一个工作示例:
Py_buffer *buf = (Py_buffer *) malloc(sizeof(*buf));
int r = PyBuffer_FillInfo(buf, NULL, malloc(sizeof(char) * 4), 4, 0, PyBUF_CONTIG);
PyObject *mv = PyMemoryView_FromBuffer(buf);
//Pack the memoryview object into an argument list and call the Python function
for (blah)
printf("%c\n", *buf->buf++); //this prints the values i set in the Python function
查看PyBuffer_FillInfo
非常简单的实现,我推出了自己的函数以提供自定义项目大小:
//buffer creation function
Py_buffer *getReadWriteBuffer(int nitems, int itemsize, char *fmt) {
Py_buffer *buf = (Py_buffer *) malloc(sizeof(*buf));
buf->obj = NULL
buf->buf = malloc(nitems * itemsize);
buf->len = nitems * itemsize;
buf->readonly = 0;
buf->itemsize = itemsize;
buf->format = fmt;
buf->ndim = 1;
buf->shape = NULL;
buf->strides = NULL;
buf->suboffsets = NULL;
buf->internal = NULL;
return buf;
}
我如何使用它:
Py_buffer *buf = getReadWriteBuffer(32, 2, "h");
PyObject *mv = PyMemoryView_FromBuffer(buf);
// pack the memoryview into an argument list and call the Python function as before
for (blah)
printf("%d\n", *buf->buf); //this prints all zeroes even though i modify the array in Python
return 0;
//the segfault happens somewhere after here
使用我自己的缓冲区对象的结果是C函数返回后的段错误。我真的不明白为什么会发生这种情况。非常感激任何的帮助。
编辑
根据这个我之前没找到的问题,itemsize > 1 可能根本不被支持。这使得这个问题更加有趣。也许我可以使用PyBuffer_FillInfo
足够大的内存块来保存我想要的东西(例如 32 个 C 浮点数)。在这种情况下,问题更多是关于如何将 Python 浮点数分配给memoryview
Python 函数中的对象。提问提问。