Skip to content

bytes.translate() returns short inputs starting with NUL untranslated #158334

Description

@fedonman

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions