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
# 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

# setup.py
from Cython.Distutils import build_ext
from distutils.core import setup
from distutils.extension import Extension

setup_info = dict(
    name="buffertest",
    ext_modules=[
        Extension("buffertest", ["buffertest.pyx"]),
        ],
    cmdclass={"build_ext": build_ext},
)

setup(**setup_info)

import buffertest
buf = buffertest.Buffertest()
m = memoryview(buf)

for name in ('format', 'itemsize', 'ndim', 'readonly', 'shape', 'strides', 'suboffsets'):
    print name, getattr(m, name)
print "len", len(m)

_______________________________________________
cython-devel mailing list
cython-devel@python.org
http://mail.python.org/mailman/listinfo/cython-devel

Reply via email to