Re: how to get bytes from bytearray without copying

2014-03-04 Thread Juraj Ivančić
On 3.3.2014. 2:27, Ian Kelly wrote: Python 3.3 has a C API function to create a memoryview for a char*, that can be made read-only. http://docs.python.org/3/c-api/memoryview.html#PyMemoryView_FromMemory I don't see a way to do what you want in pure Python, apart from perhaps writing an el

Re: how to get bytes from bytearray without copying

2014-03-03 Thread Juraj Ivančić
On 3.3.2014. 2:27, Ian Kelly wrote: Python 3.3 has a C API function to create a memoryview for a char*, that can be made read-only. http://docs.python.org/3/c-api/memoryview.html#PyMemoryView_FromMemory Yes, this is probably what I'll do in absence of pure Python solution. Thanks for th

Re: how to get bytes from bytearray without copying

2014-03-03 Thread Juraj Ivančić
On 3.3.2014. 1:49, Mark Lawrence wrote: 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 by

Re: how to get bytes from bytearray without copying

2014-03-03 Thread Juraj Ivančić
On 3.3.2014. 1:44, Cameron Simpson wrote: ValueError: cannot hash writable memoryview object Have you considered subclassing memoryview and giving the subclass a __hash__ method? I have, and then, when I failed to subclass it, I considered doing aggregation, and make it behave byte-like. Bu

how to get bytes from bytearray without copying

2014-03-02 Thread Juraj Ivančić
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 byt