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.
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:21 | admin | set | github: 57026 |
| 2012年02月24日 11:12:55 | skrah | set | status: open -> closed resolution: not a bug messages: + msg154133 stage: patch review -> resolved |
| 2012年02月24日 10:54:36 | ezio.melotti | set | versions: - Python 3.1 |
| 2011年08月23日 12:47:08 | pitrou | set | messages: + msg142818 |
| 2011年08月23日 12:44:01 | skrah | set | messages: + msg142817 |
| 2011年08月22日 21:43:43 | pitrou | set | messages: + msg142755 |
| 2011年08月22日 14:27:25 | skrah | set | messages: + msg142727 |
| 2011年08月22日 11:13:31 | skrah | create | |