Skip to content

Commit 981c4e9

Browse files
committed
Document mmap object's __iter__ behavior that differs from that of bytearray.
Propose a workaround with a memoryview wrapper.
1 parent 1bfe86c commit 981c4e9

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Doc/library/mmap.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ byte by doing ``obj[index] = 97``, or change a subsequence by assigning to a
1616
slice: ``obj[i1:i2] = b'...'``. You can also read and write data starting at
1717
the current file position, and :meth:`seek` through the file to different positions.
1818

19+
.. note::
20+
Iterating over a mmap object yields :class:`bytes` objects of length 1; this
21+
differs from :class:`bytearray` which yields :class:`int` values in the range
22+
[0, 255]. This behavior has been retained for backwards-compatibility. To get
23+
an iterator that iterates over bytes you can create a :class:`memoryview` object
24+
referencing the mmap object.
25+
1926
A memory-mapped file is created by the :class:`~mmap.mmap` constructor, which is
2027
different on Unix and on Windows. In either case you must provide a file
2128
descriptor for a file opened for update. If you wish to map an existing Python

0 commit comments

Comments
 (0)