work around memoryview.tobytes() off-by-one bug with suboffsets
work around memoryview.tobytes() off-by-one bug with suboffsets

In CPython 2.7, the memoryview.tobytes() method (implemented in
function memoryview_tobytes() in cpython/Objects/memoryobject.c) calls
PyBuffer_ToContiguous() to copy the bytes in the buffer object to the
newly allocated string memory.  PyBuffer_ToContiguous() in turn calls
PyBuffer_IsContiguous(), which always returns false when the
suboffsets member of the Py_buffer struct is non-NULL (even if all
entries in that array are negative!).  When PyBuffer_IsContiguous()
returns false, PyBuffer_ToContiguous() runs an alternative memory copy
scheme which apparently has an off-by-one bug that the normal memory
copy scheme doesn't have.

This change sets the Py_buffer suboffsets member to NULL if all
entries are negative, avoiding the off-by-one bug.

To reproduce the bug:

    cpdef foo():
        cdef unsigned char[:] v = bytearray("testing")
        # the following prints 'estingt' without this workaround
        print repr(memoryview(v).tobytes())

Signed-off-by: Richard Hansen <rhansen@bbn.com>

--HG--
extra : transplant_source : h%F68%C5f%D2%A9%86%A7o%B4w%27%E5%EDt%3D%D5%3B%3C

hickford/cython,roxyboy/cython,acrispin/cython,mrGeen/cython,cython/cython,andreasvc/cython,achernet/cython,mcanthony/cython,hickford/cython,mrGeen/cython,da-woods/cython,madjar/cython,andreasvc/cython,encukou/cython,cython/cython,marscher/cython,fperez/cython,mcanthony/cython,hickford/cython,dahebolangkuan/cython,fperez/cython,slonik-az/cython,encukou/cython,acrispin/cython,fperez/cython,c-blake/cython,mcanthony/cython,marscher/cython,dahebolangkuan/cython,da-woods/cython,scoder/cython,cython/cython,hhsprings/cython,roxyboy/cython,mcanthony/cython,madjar/cython,JelleZijlstra/cython,encukou/cython,scoder/cython,achernet/cython,marscher/cython,c-blake/cython,da-woods/cython,hhsprings/cython,acrispin/cython,cython/cython,fperez/cython,c-blake/cython,encukou/cython,hickford/cython,hickford/cython,slonik-az/cython,hhsprings/cython,slonik-az/cython,madjar/cython,fabianrost84/cython,madjar/cython,slonik-az/cython,ChristopherHogan/cython,slonik-az/cython,scoder/cython,roxyboy/cython,ChristopherHogan/cython,hhsprings/cython,encukou/cython,mcanthony/cython,fabianrost84/cython,ChristopherHogan/cython,hhsprings/cython,fabianrost84/cython,madjar/cython,dahebolangkuan/cython,JelleZijlstra/cython,mrGeen/cython,c-blake/cython,da-woods/cython,fabianrost84/cython,achernet/cython,andreasvc/cython,mrGeen/cython,mrGeen/cython,roxyboy/cython,andreasvc/cython,acrispin/cython,achernet/cython,acrispin/cython,marscher/cython,JelleZijlstra/cython,fperez/cython,marscher/cython,fabianrost84/cython,roxyboy/cython,dahebolangkuan/cython,achernet/cython,JelleZijlstra/cython,andreasvc/cython,scoder/cython,dahebolangkuan/cython,c-blake/cython,JelleZijlstra/cython