]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#13579: teach string.Formatter about 'a'.
authorR David Murray <rdmurray@bitdance.com>
Sun, 19 Aug 2012 21:26:34 +0000 (17:26 -0400)
committerR David Murray <rdmurray@bitdance.com>
Sun, 19 Aug 2012 21:26:34 +0000 (17:26 -0400)
Patch by Francisco Martín Brugué.

Doc/library/string.rst
Lib/string.py
Lib/test/test_string.py
Misc/NEWS

index aed191b748e1b036307fc07dafc16b2760e2da46..79d4e3f47aa1290bde0145ff32c1a3a8a6f1fb52 100644 (file)
@@ -91,8 +91,8 @@ implementation as the built-in :meth:`format` method.
 
    .. 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 @@ implementation as the built-in :meth:`format` method.
       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 @@ implementation as the built-in :meth:`format` method.
 
       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:
index ef0334c472fcb70fd2fb571020e95a527fb517f6..0f4ede23e14d3e833c7fc757cf2bef98e374eb9f 100644 (file)
@@ -236,12 +236,14 @@ class Formatter:
 
     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))
 
 
index a352ee356054b5d68853024e24055928f29f4b11..34d1dcf3fe6490e6fa3e6d7e0b5bbbde7b677342 100644 (file)
@@ -26,6 +26,18 @@ class ModuleTest(unittest.TestCase):
         self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def')
         self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t')
 
+    def test_conversion_specifiers(self):
+        fmt = string.Formatter()
+        self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-")
+        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_formatter(self):
         fmt = string.Formatter()
         self.assertEqual(fmt.format("foo"), "foo")
index 52e297c7805b45f9e426f5b26ec0f3ca15015a5d..857a161536c8118bbdb80c5910f6f12efd110110 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -104,6 +104,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #13579: string.Formatter now understands the 'a' conversion specifier.
+
 - Issue #15595: Fix subprocess.Popen(universal_newlines=True)
   for certain locales (utf-16 and utf-32 family). Patch by Chris Jerdonek.