python / python/cpython

`str.translate` caching values causes incorrect result

Open
#144,463 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs pending
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

It seems that str.translate is caching values internally and this is causing unintended results.

When the translation table returns an ordinal or a string ordinal string, it caches the values returned by __getitem__ and will not call the function again. However if you are returning a 2-length string this doesn't occur (and in fact calls __getitem__ an additional time for some reason?).

I expect that the __getitem__ be executed for every character since the function documentation does not mention this caching value and more-so the function behaves inconsistently.

This is likely caused by #65317.

class counting_translate_ord(dict[int, int]):
    def __init__(self) -> None:
        super().__init__()
        self.missing_calls = 0

    def __getitem__(self, key: int) -> int:
        self.missing_calls += 1
        return ord(str(self.missing_calls % 10))


table = counting_translate_ord()

print(("----").translate(table))  # 1111
print(table.missing_calls) # 1
print(("!@#$").translate(table))  # 2345
print(table.missing_calls) # 5


class counting_translate_str(dict[int, str]):
    def __init__(self) -> None:
        super().__init__()
        self.missing_calls = 0

    def __getitem__(self, key: int) -> str:
        self.missing_calls += 1
        return str(self.missing_calls % 10)


table = counting_translate_str()

print(("----").translate(table))  # 1111
print(table.missing_calls) # 1
print(("!@#$").translate(table))  # 2345
print(table.missing_calls) # 5


class counting_translate_2str(dict[int, str]):
    def __init__(self) -> None:
        super().__init__()
        self.missing_calls = 0

    def __getitem__(self, key: int) -> str:
        self.missing_calls += 1
        return format(self.missing_calls, "02d")[:2]


table = counting_translate_2str()

print(("----").translate(table))  # 02030405
print(table.missing_calls) # 5
print(("!@#$").translate(table))  # 07080910
print(table.missing_calls) # 10
CPython versions tested on:

3.12

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 provided Python reproducer against the three counting translation-table cases and compare their getitem call counts and results. Then locate the CPython entry point and tests for str.translate; done means the behavior is consistent with the documented contract and covered by regression tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.