diff options
author | Guido van Rossum <guido@python.org> | 2000-12-19 02:39:08 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2000-12-19 02:39:08 (GMT) |
commit | 5aff7752eb28c6ddaa68738ee77e1947b72e1a58 (patch) | |
tree | 1be49fbf54cca165facab28d2e08856926e0bf50 /Lib | |
parent | cda4f9a8dca961f9047eb6c5cbe647cec507e1a1 (diff) | |
download | cpython-5aff7752eb28c6ddaa68738ee77e1947b72e1a58.zip cpython-5aff7752eb28c6ddaa68738ee77e1947b72e1a58.tar.gz cpython-5aff7752eb28c6ddaa68738ee77e1947b72e1a58.tar.bz2 |
Make string.translate(s, table) work for Unicode s. Two things are
required to work around restrictions on the arguments of
u.translate():
1) don't pass the deletions argument if it's empty;
2) convert table to Unicode if s is Unicode.
This fixes SF bug #124060.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/string.py | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/Lib/string.py b/Lib/string.py index bf2811c..0dffb2a 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -297,15 +297,22 @@ def expandtabs(s, tabsize=8): # Character translation through look-up table. def translate(s, table, deletions=""): - """translate(s,table [,deletechars]) -> string + """translate(s,table [,deletions]) -> string Return a copy of the string s, where all characters occurring - in the optional argument deletechars are removed, and the + in the optional argument deletions are removed, and the remaining characters have been mapped through the given - translation table, which must be a string of length 256. + translation table, which must be a string of length 256. The + deletions argument is not allowed for Unicode strings. """ - return s.translate(table, deletions) + if deletions: + return s.translate(table, deletions) + else: + # Add s[:0] so that if s is Unicode and table is an 8-bit string, + # table is converted to Unicode. This means that table *cannot* + # be a dictionary -- for that feature, use u.translate() directly. + return s.translate(table + s[:0]) # Capitalize a string, e.g. "aBc dEf" -> "Abc def". def capitalize(s): |