mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
string.maketrans() which actually works on bytes. (Also closes #5675.)
This commit is contained in:
parent
78532baeab
commit
abc387747d
11 changed files with 134 additions and 32 deletions
|
|
@ -418,18 +418,15 @@ class BaseStrTest:
|
|||
@bigmemtest(minsize=_2G, memuse=2)
|
||||
def test_translate(self, size):
|
||||
_ = self.from_latin1
|
||||
trans = {
|
||||
ord(_('.')): _('-'),
|
||||
ord(_('a')): _('!'),
|
||||
ord(_('Z')): _('$'),
|
||||
}
|
||||
SUBSTR = _('aZz.z.Aaz.')
|
||||
if not isinstance(SUBSTR, str):
|
||||
# Workaround the inexistence of bytes.maketrans()
|
||||
chars = bytearray(range(256))
|
||||
for k, v in trans.items():
|
||||
chars[k] = ord(v)
|
||||
trans = chars
|
||||
if isinstance(SUBSTR, str):
|
||||
trans = {
|
||||
ord(_('.')): _('-'),
|
||||
ord(_('a')): _('!'),
|
||||
ord(_('Z')): _('$'),
|
||||
}
|
||||
else:
|
||||
trans = bytes.maketrans(b'.aZ', b'-!$')
|
||||
sublen = len(SUBSTR)
|
||||
repeats = size // sublen + 2
|
||||
s = SUBSTR * repeats
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue