[New-bugs-announce] [issue12817] test_multiprocessing: io.BytesIO() requires bytearray buffers

Stefan Krah report at bugs.python.org
Mon Aug 22 13:13:31 CEST 2011


New submission from Stefan Krah <stefan-usenet at bytereef.org>:

Hello,

in my private repo I've changed memoryview's getbufferproc to be PEP-3118
compliant. test_multiprocessing does the equivalent of the following sequence,
which is not allowed by PEP-3118:


>>> import array, io
>>> a = array.array('i', [1,2,3,4,5])
>>> m = memoryview(a)
>>> m.format
'i'
>>> buf = io.BytesIO(bytearray(5))
>>> buf.readinto(m)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected an object with a writable buffer interface
>>>


The error occurs in Objects/abstract.c:315:

   ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0))


Here, PyObject_AsWriteBuffer() requests a simple writable buffer of unsigned
bytes *without format information* from the memoryview. The memoryview's
getbufferproc is required to return an error:

"If format is not explicitly requested then the format must be returned
 as NULL (which means "B", or unsigned bytes)."

But the underlying buffer has format 'i' and not 'B', hence the error.


Antoine, is it correct that io.BytesIO should only be used with bytearray
buffers?

If so, this is a bug in the tests (patch attached).

----------
components: Tests
files: test_multiprocessing_use_bytearray.diff
keywords: needs review, patch
messages: 142718
nosy: ncoghlan, pitrou, skrah
priority: normal
severity: normal
stage: patch review
status: open
title: test_multiprocessing: io.BytesIO() requires bytearray buffers
type: behavior
versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3
Added file: http://bugs.python.org/file22997/test_multiprocessing_use_bytearray.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12817>
_______________________________________


More information about the New-bugs-announce mailing list