[Python-Dev] new buffer in python2.7

"Martin v. Löwis" martin at v.loewis.de
Fri Oct 29 12:15:04 CEST 2010

> Actually I would like code like
>   s = socket()
>   ...
>   header = struct.unpack("i", s)
> In other words, struct should interact with files/streams directly, instead of 
> requiring me to first read a chunk who's size I manually have to determine 
> etc.

That is easy to achieve using the existing API:

def read_and_unpack(stream, format):
    data = stream.read(struct.calcsize(format))
    return struct.unpack(format, data)

> Otherwise, I'm +1 on your suggestion, avoiding copying is a good thing.

I believe my function also doesn't involve any unnecessary copies.


More information about the Python-Dev mailing list