Skip to content

gh-70546: Document mmap object's __iter__ behavior that differs from that of bytearray.#140284

Open
ztane wants to merge 1 commit intopython:mainfrom
ztane:mmap-iterator-behavior-documented
Open

gh-70546: Document mmap object's __iter__ behavior that differs from that of bytearray.#140284
ztane wants to merge 1 commit intopython:mainfrom
ztane:mmap-iterator-behavior-documented

Conversation

@ztane
Copy link
Copy Markdown
Contributor

@ztane ztane commented Oct 18, 2025

#70546 reports that mmap object __iter__ behavior dffers from that of bytearray. This pr adds a notice that states that mmap objects have __iter__ that differs from that of bytearrays and that you can wrap the object inside a memoryview to work around the difference.

…tearray.

Propose a workaround with a memoryview wrapper.
@ztane ztane force-pushed the mmap-iterator-behavior-documented branch from 395b9e0 to 981c4e9 Compare October 18, 2025 09:11
@ztane
Copy link
Copy Markdown
Contributor Author

ztane commented Oct 18, 2025

If this PR is merged, I think the referenced issue could be also closed (I cannot do it because it still belongs to a mannequin ;)

@sergey-miryanov
Copy link
Copy Markdown
Contributor

I believe this shouldn't skip news, because it is a user-facing update.
Also, maybe it is worth to add an example of usage memoryview to the note?

@sergey-miryanov
Copy link
Copy Markdown
Contributor

@serhiy-storchaka Could you please take a look?

Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not only iterating (mmap does not even implement explicit __iter__), but subscribing by an index which returns a bytes object of length 1. And assignment requires a bytes object of length 1, not integer.

To add insult to injury, the code is self-contradictory -- so PyObject_GetItem() returns an integer, but PySequence_GetItem() returns a bytes object of length 1. This is perhaps not visible from Python side.

The documentation should mention all four operations which are different between mmap and other bytes-like objects (bytes, bytearray, array.array, memview): subscribing by an index, assigning by an index, iteration and the containment check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

4 participants