Re: how to get bytes from bytearray without copying

2014-03-04 Thread Stefan Behnel
Juraj Ivančić, 04.03.2014 16:23: > Just for reference, it is doable in pure Python, with ctypes help For some questionable meaning of "pure". Stefan -- https://mail.python.org/mailman/listinfo/python-list

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 Cameron Simpson
On 03Mar2014 09:15, Juraj Ivančić wrote: > 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 conside

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

Re: how to get bytes from bytearray without copying

2014-03-02 Thread Cameron Simpson
On 02Mar2014 17:55, Ian Kelly wrote: > On Sun, Mar 2, 2014 at 5:44 PM, Cameron Simpson wrote: > > Have you considered subclassing memoryview and giving the subclass > > a __hash__ method? > > >>> class MyMemoryView(memoryview): > ... def __hash__(self): return 42 > ... > Traceback (most rece

Re: how to get bytes from bytearray without copying

2014-03-02 Thread Ian Kelly
On Sun, Mar 2, 2014 at 5:07 PM, 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

Re: how to get bytes from bytearray without copying

2014-03-02 Thread Ian Kelly
On Sun, Mar 2, 2014 at 5:44 PM, Cameron Simpson wrote: > Have you considered subclassing memoryview and giving the subclass > a __hash__ method? >>> class MyMemoryView(memoryview): ... def __hash__(self): return 42 ... Traceback (most recent call last): File "", line 1, in TypeError: type

Re: how to get bytes from bytearray without copying

2014-03-02 Thread Mark Lawrence
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 fr

Re: how to get bytes from bytearray without copying

2014-03-02 Thread Cameron Simpson
On 03Mar2014 01: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

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