[Cython] 2d buffer interface with aligned data

Christian Heimes lists at cheimes.de
Tue Jul 17 22:55:10 CEST 2012


Am 17.07.2012 18:55, schrieb Dag Sverre Seljebotn:
> Read PEP 3118. Then implement __getbuffer__ and __releasebuffer__ in
> your cdef class (don't know if it's documented but you can see example
> in tests/run/buffer.pyx).

The new buffer interface from PEP 3118 is only available for Python 2.6
and newer. I was hoping for some abstraction layer in Cython. Well, I
don't have to support Python 2.5 and older. Thanks for the hint!


> This is easily supported; above you would let
> 
> ndim = 2
> strides = [8, 1]
> shape = [2, 3]
> itemsize = 1
> 
> The alignment bytes are skipped simply because shape[0] * itemsize <
> strides[0].

Either I'm doing something wrong or I found a Cython bug. I've attached
two files. The output is unexpected and looks like something is
accessing uninitialized memory:

format BBB
itemsize 3
ndim 2
readonly True
shape (140704200676960L, 140704199917920L)
strides (2L, 140704196619665L)
suboffsets None
len 140704200677056


Cython: 0.16
Python: 2.7.3 on 64bit Linux

Christian
-------------- next part --------------
# buffertest.pyx
cimport cpython

cdef char *s = b"BGRBGRxxBGRBGRxxBGRBGRxx"

cdef class Buffertest:
    def __getbuffer__(self, cpython.Py_buffer* buffer, int flags):

        buffer.buf = <char*>s
        buffer.obj = self
        buffer.len = len(s)
        buffer.readonly = 1
        buffer.format = "BBB"
        buffer.ndim = 2
        buffer.shape = [3, 2]
        buffer.strides = [8, 1] 
        buffer.suboffsets = NULL
        buffer.itemsize = 3
        buffer.internal = NULL

-------------- next part --------------
A non-text attachment was scrubbed...
Name: setup.py
Type: text/x-python
Size: 526 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20120717/97bc214a/attachment.py>


More information about the cython-devel mailing list