我正在 Cython 中编写一个 Python 2.7 扩展模块。 如何创建一个 Python 对象来实现新样式的缓冲区接口,该接口包装了 C 库给我的一大块内存? 内存块只是一串字节,而不是结构或多维数组。我得到了一个const void *
指针和一个长度,以及一些关于指针保持有效时间的详细信息。
我无法复制内存——这会降低我的应用程序的性能。
使用旧式缓冲区对象,我可以简单地使用PyBuffer_FromMemory()
,但我似乎找不到类似的简单方法来生成新式缓冲区对象。
我是否必须创建自己的实现缓冲区接口的类?或者 Cython 是否提供了一种简单的方法来做到这一点?
我已经阅读了Cython 文档中的Unicode 和 Passing Strings和Typed Memoryviews页面,但是该文档不精确且不是很完整,并且没有与我想要做的类似的示例。
这是我尝试过的(test.pyx
):
from libc.stdlib cimport malloc
from libc.string cimport memcpy
## pretend that this function is in some C library and that it does
## something interesting. (this function is unrelated to the problem
## I'm experiencing -- this is just an example function that returns a
## chunk of memory that I want to wrap in an object that follows the
## new buffer protocol.)
cdef void dummy_function(const void **p, size_t *l):
cdef void *tmp = malloc(17)
memcpy(tmp, "some test\0 bytes", 17)
p[0] = tmp
l[0] = 17
cpdef getbuf():
cdef const void *cstr
cdef size_t l
dummy_function(&cstr, &l)
## error: test.pyx:21:20: Invalid base type for memoryview slice: void
#cdef const void[:] ret = cstr[:l]
## error: test.pyx:24:9: Assignment to const 'ret'
#cdef const char[:] ret = cstr[:l]
## error: test.pyx:27:27: Cannot convert 'void const *' to memoryviewslice
#cdef char[:] ret = cstr[:l]
## this next attempt cythonizes, but raises an exception:
## $ python -c 'import test; test.getbuf()'
## Traceback (most recent call last):
## File "<string>", line 1, in <module>
## File "test.pyx", line 15, in test.getbuf (test.c:1411)
## File "test.pyx", line 38, in test.getbuf (test.c:1350)
## File "stringsource", line 614, in View.MemoryView.memoryview_cwrapper (test.c:6763)
## File "stringsource", line 321, in View.MemoryView.memoryview.__cinit__ (test.c:3309)
## BufferError: Object is not writable.
cdef char[:] ret = (<const char *>cstr)[:l]
## this raises the same exception as above
#cdef char[:] ret = (<char *>cstr)[:l]
return ret