[Python-ideas] struct.unpack should support open files

Steven D'Aprano steve at pearwood.info
Tue Dec 25 20:16:32 EST 2018


On Tue, Dec 25, 2018 at 04:51:18PM -0600, eryk sun wrote:
> On 12/24/18, Drew Warwick <dwarwick96 at gmail.com> wrote:
> > The struct unpack API is inconvenient to use with files. I must do:
> >
> > struct.unpack(fmt, file.read(struct.calcsize(fmt))
> 
> Alternatively, we can memory-map the file via mmap. An important
> difference is that the mmap buffer interface is low-level (e.g. no
> file pointer and the offset has to be page aligned), so we have to
> slice out bytes for the given offset and size. We can avoid copying
> via memoryview slices. 

Seems awfully complicated. How do we do all these things, and what 
advantage does it give?


> We can also use ctypes instead of
> memoryview/struct.

Only if you want non-portable code.

What advantage over struct is ctypes?



-- 
Steve


More information about the Python-ideas mailing list