Author: georg.brandl Date: Fri Aug 31 12:37:15 2007 New Revision: 57835 Modified: python/branches/py3k/Doc/library/stdtypes.rst python/branches/py3k/Doc/library/string.rst python/branches/py3k/Lib/distutils/command/install.py python/branches/py3k/Lib/distutils/fancy_getopt.py python/branches/py3k/Lib/string.py python/branches/py3k/Lib/test/test_bigmem.py python/branches/py3k/Lib/test/test_string.py python/branches/py3k/Lib/textwrap.py python/branches/py3k/Lib/urllib.py Log: string.maketrans() now produces translation tables for bytes.translate() -- wrong module? Fix all remaining instances that did bad things with the new str.translate().
Modified: python/branches/py3k/Doc/library/stdtypes.rst ============================================================================== --- python/branches/py3k/Doc/library/stdtypes.rst (original) +++ python/branches/py3k/Doc/library/stdtypes.rst Fri Aug 31 12:37:15 2007 @@ -1549,10 +1549,10 @@ b'example' -.. method:: bytes.translate(table[, deletechars]) +.. method:: bytes.translate(table[, delete]) Return a copy of the bytes object where all bytes occurring in the optional - argument *deletechars* are removed, and the remaining bytes have been mapped + argument *delete* are removed, and the remaining bytes have been mapped through the given translation table, which must be a bytes object of length 256. @@ -1560,8 +1560,7 @@ create a translation table. .. XXX a None table doesn't seem to be supported - For string objects, set the *table* argument to - ``None`` for translations that only delete characters:: + Set the *table* argument to ``None`` for translations that only delete characters:: >>> 'read this short text'.translate(None, 'aeiou') 'rd ths shrt txt' Modified: python/branches/py3k/Doc/library/string.rst ============================================================================== --- python/branches/py3k/Doc/library/string.rst (original) +++ python/branches/py3k/Doc/library/string.rst Fri Aug 31 12:37:15 2007 @@ -128,10 +128,11 @@ .. method:: get_field(field_name, args, kwargs, used_args) Given *field_name* as returned by :meth:`parse` (see above), convert it to - an object to be formatted. The default version takes strings of the form - defined in :pep:`3101`, such as "0[name]" or "label.title". It records - which args have been used in *used_args*. *args* and *kwargs* are as - passed in to :meth:`vformat`. + an object to be formatted. Returns a tuple (obj, used_key). The default + version takes strings of the form defined in :pep:`3101`, such as + "0[name]" or "label.title". *args* and *kwargs* are as passed in to + :meth:`vformat`. The return value *used_key* has the same meaning as the + *key* parameter to :meth:`get_value`. .. method:: get_value(key, args, kwargs) @@ -554,15 +555,8 @@ leading and trailing whitespace. -.. XXX is obsolete with unicode.translate -.. function:: maketrans(from, to) +.. function:: maketrans(frm, to) - Return a translation table suitable for passing to :func:`translate`, that will - map each character in *from* into the character at the same position in *to*; - *from* and *to* must have the same length. - - .. warning:: - - Don't use strings derived from :const:`lowercase` and :const:`uppercase` as - arguments; in some locales, these don't have the same length. For case - conversions, always use :func:`lower` and :func:`upper`. + Return a translation table suitable for passing to :meth:`bytes.translate`, + that will map each character in *from* into the character at the same + position in *to*; *from* and *to* must have the same length. Modified: python/branches/py3k/Lib/distutils/command/install.py ============================================================================== --- python/branches/py3k/Lib/distutils/command/install.py (original) +++ python/branches/py3k/Lib/distutils/command/install.py Fri Aug 31 12:37:15 2007 @@ -348,11 +348,10 @@ if opt_name[-1] == "=": opt_name = opt_name[0:-1] if self.negative_opt.has_key(opt_name): - opt_name = self.negative_opt[opt_name].translate( - longopt_xlate) + opt_name = longopt_xlate(self.negative_opt[opt_name]) val = not getattr(self, opt_name) else: - opt_name = opt_name.translate(longopt_xlate) + opt_name = longopt_xlate(opt_name) val = getattr(self, opt_name) print(" %s: %s" % (opt_name, val)) Modified: python/branches/py3k/Lib/distutils/fancy_getopt.py ============================================================================== --- python/branches/py3k/Lib/distutils/fancy_getopt.py (original) +++ python/branches/py3k/Lib/distutils/fancy_getopt.py Fri Aug 31 12:37:15 2007 @@ -26,7 +26,7 @@ # This is used to translate long options to legitimate Python identifiers # (for use as attributes of some object). -longopt_xlate = string.maketrans('-', '_') +longopt_xlate = lambda s: s.replace('-', '_') class FancyGetopt: """Wrapper around the standard 'getopt()' module that provides some @@ -107,7 +107,7 @@ """Translate long option name 'long_option' to the form it has as an attribute of some object: ie., translate hyphens to underscores.""" - return long_option.translate(longopt_xlate) + return longopt_xlate(long_option) def _check_alias_dict(self, aliases, what): assert isinstance(aliases, dict) @@ -372,7 +372,7 @@ return parser.getopt(args, object) -WS_TRANS = string.maketrans(string.whitespace, ' ' * len(string.whitespace)) +WS_TRANS = {ord(_wschar) : ' ' for _wschar in string.whitespace} def wrap_text(text, width): """wrap_text(text : string, width : int) -> [string] @@ -432,7 +432,7 @@ """Convert a long option name to a valid Python identifier by changing "-" to "_". """ - return opt.translate(longopt_xlate) + return longopt_xlate(opt) class OptionDummy: Modified: python/branches/py3k/Lib/string.py ============================================================================== --- python/branches/py3k/Lib/string.py (original) +++ python/branches/py3k/Lib/string.py Fri Aug 31 12:37:15 2007 @@ -25,10 +25,6 @@ punctuation = """!"#$%&'()*+,-./:;<=>[EMAIL PROTECTED]|}~""" 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 @@ 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 #################################################################### Modified: python/branches/py3k/Lib/test/test_bigmem.py ============================================================================== --- python/branches/py3k/Lib/test/test_bigmem.py (original) +++ python/branches/py3k/Lib/test/test_bigmem.py Fri Aug 31 12:37:15 2007 @@ -3,7 +3,6 @@ import unittest import operator -import string import sys # Bigmem testing houserules: @@ -376,7 +375,7 @@ @bigmemtest(minsize=_2G, memuse=2) def test_translate(self, size): - trans = string.maketrans('.aZ', '-!$') + trans = {ord('.'):'-', ord('a'):'!', ord('Z'):'$'} SUBSTR = 'aZz.z.Aaz.' sublen = len(SUBSTR) repeats = size // sublen + 2 Modified: python/branches/py3k/Lib/test/test_string.py ============================================================================== --- python/branches/py3k/Lib/test/test_string.py (original) +++ python/branches/py3k/Lib/test/test_string.py Fri Aug 31 12:37:15 2007 @@ -103,10 +103,11 @@ def test_maketrans(self): - transtable = '\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>[EMAIL PROTECTED]|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377' + transtable = b'\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>[EMAIL PROTECTED]|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377' - self.assertEqual(string.maketrans('abc', 'xyz'), transtable) - self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzq') + self.assertEqual(string.maketrans(b'abc', b'xyz'), transtable) + self.assertRaises(ValueError, string.maketrans, b'abc', b'xyzq') + self.assertRaises(TypeError, string.maketrans, 'abc', 'def') def test_main(): test_support.run_unittest(ModuleTest) Modified: python/branches/py3k/Lib/textwrap.py ============================================================================== --- python/branches/py3k/Lib/textwrap.py (original) +++ python/branches/py3k/Lib/textwrap.py Fri Aug 31 12:37:15 2007 @@ -59,8 +59,6 @@ Drop leading and trailing whitespace from lines. """ - whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace)) - unicode_whitespace_trans = {} uspace = ord(' ') for x in _whitespace: @@ -116,10 +114,7 @@ if self.expand_tabs: text = text.expandtabs() if self.replace_whitespace: - if isinstance(text, str8): - text = text.translate(self.whitespace_trans) - elif isinstance(text, str): - text = text.translate(self.unicode_whitespace_trans) + text = text.translate(self.unicode_whitespace_trans) return text Modified: python/branches/py3k/Lib/urllib.py ============================================================================== --- python/branches/py3k/Lib/urllib.py (original) +++ python/branches/py3k/Lib/urllib.py Fri Aug 31 12:37:15 2007 @@ -1420,7 +1420,6 @@ # Test program def test(args=[]): - import string if not args: args = [ '/etc/passwd', @@ -1443,9 +1442,7 @@ fp = open(fn, 'rb') data = fp.read() del fp - if '\r' in data: - table = string.maketrans("", "") - data = data.translate(table, "\r") + data = data.replace("\r", "") print(data) fn, h = None, None print('-'*40) _______________________________________________ Python-3000-checkins mailing list Python-3000-checkins@python.org http://mail.python.org/mailman/listinfo/python-3000-checkins