Bug description:
On main, bytes.translate() returns its input untranslated when the input is shorter than 256 bytes, its first bytes (up to eight) are NUL, and the remaining bytes map to themselves:
t = bytes.maketrans(b'\x00', b'Z')
print(b'\x00'.translate(t))
print(b'\x00\x00'.translate(t))
print((b'\x00' * 300).translate(t)[:2])
Output on main:
b'\x00'
b'\x00\x00'
b'ZZ'
Python 3.14 prints b'Z', b'ZZ' and b'ZZ'. This started with GH-157346 (gh-155742), which moved the PyBytesWriter_Finish() call in bytes_translate_impl() before the memcmp() that decides whether to return the original object.
Expected: b'Z' and b'ZZ'.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
Bug description:
On main,
bytes.translate()returns its input untranslated when the input is shorter than 256 bytes, its first bytes (up to eight) are NUL, and the remaining bytes map to themselves:Output on main:
Python 3.14 prints
b'Z',b'ZZ'andb'ZZ'. This started with GH-157346 (gh-155742), which moved thePyBytesWriter_Finish()call inbytes_translate_impl()before thememcmp()that decides whether to return the original object.Expected:
b'Z'andb'ZZ'.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs