why doesn't an mmap.mmap object have some kind of memoryview method?

MRAB python at mrabarnett.plus.com
Sun Aug 5 20:15:33 EDT 2018


On 2018-08-06 00:28, Cameron Simpson wrote:
> I'm tinkering with a module which scans video files. The MP4 parser has a
> `discard_data` parameter which tells certain parts of the parser to discard
> some of the scanned data, particularly the MDAT box parse because I don't
> normally want to blow the machine's RAM on a huge video stream - I'm normally
> parsing out the semantic info such as metadata.
> 
> However, I would _like_ to keep it available in a frugal fashion if I can, and
> it seems to me that the mmap module is ideal: map the file into memory, use the
> file itself as the backing store, and return views of the mmap.
> 
> But slicing a mmap object returns a bytes, which I _imagine_ to be a copy of
> the file data instead of a view into the mapping (because bytes are supposed to
> be immutable, and in theory some other actor might change the file even though
> I myself have it mapped read only).
> 
> It seems obvious to me that a method returning a memoryview of the mapped file
> would be very handy here: no data copies at all, and not even any I/O unless
> the data are accessed. But I see no such method in the documentation.
> 
> Does anyone have any insight here?
> 
The docs show that although slicing a mmap returns bytes, you can assign 
to a slice of a mmap itself.

You can also give an offset and length into the file when you create the 
mmap.



More information about the Python-list mailing list