mmap.find() and mmap.rfind() return the mapping length when searching for an empty subsequence with start > len(mapping).
import mmap
with mmap.mmap(-1, 3) as m:
m[:] = b"abc"
print(m.find(b"", 4))
print(m.rfind(b"", 4))
Actual output:
Expected output:
bytes, bytearray, and str all return -1 for the equivalent operation.
The mmap documentation states that start and end are interpreted as in slice notation, and the existing mmap tests compare search behavior against bytes.find().
The cause appears to be that mmap_gfind_lock_held() clamps a positive start greater than the mapping size to the mapping size. This converts an out-of-range search into a successful empty-subsequence match.
Confirmed on CPython main (884ac3e3ec0), Python 3.12.8, and Python 3.10.0 on Windows.
Linked PRs
mmap.find()andmmap.rfind()return the mapping length when searching for an empty subsequence withstart > len(mapping).Actual output:
Expected output:
bytes,bytearray, andstrall return-1for the equivalent operation.The mmap documentation states that
startandendare interpreted as in slice notation, and the existing mmap tests compare search behavior againstbytes.find().The cause appears to be that
mmap_gfind_lock_held()clamps a positivestartgreater than the mapping size to the mapping size. This converts an out-of-range search into a successful empty-subsequence match.Confirmed on CPython main (
884ac3e3ec0), Python 3.12.8, and Python 3.10.0 on Windows.Linked PRs