Skip to content

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

@ztane

Description

@ztane

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions