python / python/cpython

`mmap` objects does not support __contains__ operation with ints (unlike bytearray!)

Open
#140,300 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

The following code

import mmap

with open('test.dat', 'wb') as f:
    f.write(b'A')

with open('test.dat', 'rb') as f:
   mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
   print(65 in memoryview(mm))
   print(65 in bytearray(mm))
   print(65 in mm)
   mm.close()

prints

True
True
False

this even though mmap documentation claims that mmap works exactly like bytearray.

There is also discrepancy in the __iter__ implementation, but for that there is a workaround of using a memoryview object and that would be possibly backwards-incompatible change.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the issue's mmap, memoryview, and bytearray reproduction on Linux with CPython main. Trace the mmap object's containment behavior and add or update coverage for integer membership; done means mmap matches the documented bytearray behavior without regressing the noted iteration behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.