summaryrefslogtreecommitdiff
path: root/Lib/string.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2007-08-31 10:37:15 +0000
committerGeorg Brandl <georg@python.org>2007-08-31 10:37:15 +0000
commitefdb28e0d6c5406e92e02808fcc2ad939cf92801 (patch)
tree1252b0f873ece2e0552c60332211765a35f601e7 /Lib/string.py
parent57352756c4d913c117323dbd7b8f9ece595a7c1e (diff)
downloadcpython-efdb28e0d6c5406e92e02808fcc2ad939cf92801.tar.gz
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.py35
1 files changed, 14 insertions, 21 deletions
diff --git a/Lib/string.py b/Lib/string.py
index f60753fde2..ff21eb1944 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
####################################################################