This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: test_multiprocessing: io.BytesIO() requires bytearray buffers
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ncoghlan, pitrou, skrah
Priority: normal Keywords: needs review, patch

Created on 2011-08-22 11:13 by skrah, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_multiprocessing_use_bytearray.diff skrah, 2011-08-22 11:13 review
Messages (6)
msg142718 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2011-08-22 11:13
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).
msg142727 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2011-08-22 14:27
Of course, there is another interpretation:

[PyBUF_FORMAT]

"The returned buffer must have true format information if this flag is provided. This would be used when the consumer is going to be checking for what 'kind' of data is actually stored. An exporter should always be able to provide this information if requested. If format is not explicitly requested then the format must be returned as NULL (which means "B", or unsigned bytes)"


So, the returned buffer may have false format information ('B' vs. 'i'
in this case) if this flag is not provided.


Do you agree with this? I'll then make it explicit in the documentation.
msg142755 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-08-22 21:43
> Antoine, is it correct that io.BytesIO should only be used with bytearray
> buffers?

BytesIO does a copy of the original object, it does not touch the
original buffer.
msg142817 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2011-08-23 12:44
I've come to think that PyBUF_SIMPLE requests were really intended
as a way of casting contiguous buffers with arbitrary formats to
one-dimensional buffers of unsigned bytes. At least that is what Numpy
does. 

Then test_multiprocessing would be correct. I've asked the general
question on the Numpy mailing list:

http://mail.scipy.org/pipermail/numpy-discussion/2011-August/058189.html
msg142818 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-08-23 12:47
Please also see issue5231.
msg154133 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-02-24 11:12
The current interpretation in the PEP-3118 repo is that a request
without PyBUF_FORMAT means "implicit cast to unsigned bytes".

This makes the behavior of PyObject_AsWriteBuffer() correct, so I'm
closing this.
History
Date User Action Args
2022-04-11 14:57:21adminsetgithub: 57026
2012-02-24 11:12:55skrahsetstatus: open -> closed
resolution: not a bug
messages: + msg154133

stage: patch review -> resolved
2012-02-24 10:54:36ezio.melottisetversions: - Python 3.1
2011-08-23 12:47:08pitrousetmessages: + msg142818
2011-08-23 12:44:01skrahsetmessages: + msg142817
2011-08-22 21:43:43pitrousetmessages: + msg142755
2011-08-22 14:27:25skrahsetmessages: + msg142727
2011-08-22 11:13:31skrahcreate