diff -r 910a4b12c796 Doc/library/string.rst --- a/Doc/library/string.rst Mon Apr 30 22:43:46 2012 +0800 +++ b/Doc/library/string.rst Mon Apr 30 18:16:32 2012 +0200 @@ -91,8 +91,8 @@ .. method:: format(format_string, *args, **kwargs) - :meth:`format` is the primary API method. It takes a format template - string, and an arbitrary set of positional and keyword argument. + :meth:`format` is the primary API method. It takes a format string and + an arbitrary set of positional and keyword arguments. :meth:`format` is just a wrapper that calls :meth:`vformat`. .. method:: vformat(format_string, args, kwargs) @@ -101,8 +101,8 @@ separate function for cases where you want to pass in a predefined dictionary of arguments, rather than unpacking and repacking the dictionary as individual arguments using the ``*args`` and ``**kwds`` - syntax. :meth:`vformat` does the work of breaking up the format template - string into character data and replacement fields. It calls the various + syntax. :meth:`vformat` does the work of breaking up the format string + into character data and replacement fields. It calls the various methods described below. In addition, the :class:`Formatter` defines a number of methods that are @@ -173,7 +173,8 @@ Converts the value (returned by :meth:`get_field`) given a conversion type (as in the tuple returned by the :meth:`parse` method). The default - version understands 'r' (repr) and 's' (str) conversion types. + version understands 's' (str), 'r' (repr) and 'a' (ascii) conversion + types. .. _formatstrings: diff -r 910a4b12c796 Lib/string.py --- a/Lib/string.py Mon Apr 30 22:43:46 2012 +0800 +++ b/Lib/string.py Mon Apr 30 18:16:32 2012 +0200 @@ -220,12 +220,14 @@ def convert_field(self, value, conversion): # do any conversion on the resulting object - if conversion == 'r': - return repr(value) + if conversion is None: + return value elif conversion == 's': return str(value) - elif conversion is None: - return value + elif conversion == 'r': + return repr(value) + elif conversion == 'a': + return ascii(value) raise ValueError("Unknown conversion specifier {0!s}".format(conversion)) diff -r 910a4b12c796 Lib/test/test_string.py --- a/Lib/test/test_string.py Mon Apr 30 22:43:46 2012 +0800 +++ b/Lib/test/test_string.py Mon Apr 30 18:16:32 2012 +0200 @@ -38,6 +38,13 @@ self.assertEqual(fmt.format("{0!s}", 'test'), 'test') self.assertRaises(ValueError, fmt.format, "{0!h}", 'test') + # issue13579 + self.assertEqual(fmt.format("{0!a}", 42), '42') + self.assertEqual(fmt.format("{0!a}", string.ascii_letters), + "'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'") + self.assertEqual(fmt.format("{0!a}", chr(255)), "'\\xff'") + self.assertEqual(fmt.format("{0!a}", chr(256)), "'\\u0100'") + def test_name_lookup(self): fmt = string.Formatter() class AnyAttr: