how to get bytes from bytearray without copying

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Mar 2 19:49:28 EST 2014


On 03/03/2014 00:07, Juraj Ivančić wrote:
> Is it possible to somehow 'steal' bytearray's buffer and make it a
> read-only bytes? I failed to find a way to do this, and would like to
> make sure.
>
> My use case is, I would expect, fairly common. I read a certain
> (potentially very large) amount of data from the network into a
> pre-allocated bytearray. From that point on, this data is logically
> read-only. To prevent making redundant copies, I wrap it in a
> memoryview, and then slice and dice it. The problem with this memoryview
> is that it, and its slices, are considered writable, and thus cannot be
> hashed:
>
> ValueError: cannot hash writable memoryview object
>
> The only way (AFAICT) to make this work is to first create a bytes
> object from bytearray, but this copies the data. I don't need this copy,
> so I'd like to avoid it, because of both principle and performance reasons.
>
> Is there any reason why bytearray isn't able to release/convert its
> buffer to bytes? I see that it has a clear() method which... well...
> clears it. The former would be much more useful.
>
> I would also be content if there is some way of making memoryview
> artificially read-only to avoid the above error.
>
> Any help/thoughts/comments are highly appreciated.
>

If your data is readonly why can't you simply read it as bytes in the 
first place?  Failing that from 
http://docs.python.org/3/library/stdtypes.html#memoryview

tobytes() - Return the data in the buffer as a bytestring. This is 
equivalent to calling the bytes constructor on the memoryview.

 >>> m = memoryview(b"abc")
 >>> m.tobytes()
b'abc'
 >>> bytes(m)
b'abc'

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com





More information about the Python-list mailing list