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:
Georg Brandl 2009-04-12 15:51:51 +00:00
parent 78532baeab
commit abc387747d
11 changed files with 134 additions and 32 deletions

View file

@ -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