diff options
author | Georg Brandl <georg@python.org> | 2007-08-31 10:37:15 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-08-31 10:37:15 (GMT) |
commit | 7f13e6b3e2546a7d6402f969113d928cc1d58dfc (patch) | |
tree | 1b6b41e5a53beb659964a72d8f064c9ecdb0d5ed /Lib/string.py | |
parent | 226878cba507cff4b6ce094063682d0b0b53cbb9 (diff) | |
download | cpython-7f13e6b3e2546a7d6402f969113d928cc1d58dfc.zip cpython-7f13e6b3e2546a7d6402f969113d928cc1d58dfc.tar.gz cpython-7f13e6b3e2546a7d6402f969113d928cc1d58dfc.tar.bz2 |
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Fix all remaining instances that did bad things with the new str.translate().
Diffstat (limited to 'Lib/string.py')
-rw-r--r-- | Lib/string.py | 35 |
1 files changed, 14 insertions, 21 deletions
diff --git a/Lib/string.py b/Lib/string.py index f60753f..ff21eb1 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -25,10 +25,6 @@ octdigits = '01234567' punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" printable = digits + ascii_letters + punctuation + whitespace -# Case conversion helpers -# Use str to convert Unicode literal in case of -U -_idmap = str('').join(chr(c) for c in range(256)) - # Functions which aren't available as string methods. # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". @@ -44,26 +40,23 @@ def capwords(s, sep=None): return (sep or ' ').join([x.capitalize() for x in s.split(sep)]) -# Construct a translation string -_idmapL = None -def maketrans(fromstr, tostr): - """maketrans(frm, to) -> string - - Return a translation table (a string of 256 bytes long) - suitable for use in string.translate. The strings frm and to - must be of the same length. +# Construct a translation map for bytes.translate +def maketrans(frm, to): + """maketrans(frm, to) -> bytes + Return a translation table (a bytes object of length 256) + suitable for use in bytes.translate where each byte in frm is + mapped to the byte at the same position in to. + The strings frm and to must be of the same length. """ - if len(fromstr) != len(tostr): + if len(frm) != len(to): raise ValueError("maketrans arguments must have same length") - global _idmapL - if not _idmapL: - _idmapL = list(_idmap) - L = _idmapL[:] - for i, c in enumerate(fromstr): - L[ord(c)] = tostr[i] - return ''.join(L) - + if not (isinstance(frm, bytes) and isinstance(to, bytes)): + raise TypeError("maketrans arguments must be bytes objects") + L = bytes(range(256)) + for i, c in enumerate(frm): + L[c] = to[i] + return L #################################################################### |