[Python-Dev] Why does _pyio.*.readinto have to work with 'b' arrays? (original) (raw)
Nick Coghlan ncoghlan at gmail.com
Sun Jun 15 08:37:48 CEST 2014
- Previous message: [Python-Dev] Why does _pyio.*.readinto have to work with 'b' arrays?
- Next message: [Python-Dev] Why does _pyio.*.readinto have to work with 'b' arrays?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 15 June 2014 14:57, Nikolaus Rath <Nikolaus at rath.org> wrote:
On 06/14/2014 09:31 PM, Nick Coghlan wrote:
On 15 June 2014 10:41, Benjamin Peterson <benjamin at python.org> wrote:
On Sat, Jun 14, 2014, at 15:39, Nikolaus Rath wrote:
It seems to me that a much cleaner solution would be to simply declare pyio's readinto to only work with bytearrays, and to explicitly raise a (more helpful) TypeError if anything else is passed in.
That seems reasonable. I don't think pyio's behavior is terribly important compared to the C io module. pyio was written before the various memoryview fixes that were implemented in Python 3.3 - it seems to me it would make more sense to use memoryview to correctly handle arbitrary buffer exporters (we implemented similar fixes for the base64 module in 3.4). Definitely. But is there a way to do that without writing C code?
Yes, Python level reshaping and typecasting of memory views is one of the key enhancements Stefan implemented for 3.3.
from array import array a = array('b', b'x'*10) am = memoryview(a) a array('b', [120, 120, 120, 120, 120, 120, 120, 120, 120, 120]) am[:3] = memoryview(b'foo').cast('b') a array('b', [102, 111, 111, 120, 120, 120, 120, 120, 120, 120])
Cheers, Nick.
-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
- Previous message: [Python-Dev] Why does _pyio.*.readinto have to work with 'b' arrays?
- Next message: [Python-Dev] Why does _pyio.*.readinto have to work with 'b' arrays?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]