summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2012-08-19 21:45:40 (GMT)
committerR David Murray <rdmurray@bitdance.com>2012-08-19 21:45:40 (GMT)
commit749bd42072c3ecf04be3e38ca479e48692bbfd7e (patch)
tree0471849a785ec2bc5df6f60497cebaf66d790051
parent8528c3145e5856a88199f07e155b3c75710cc2a1 (diff)
parente56bf97ef4283a877c459390516bb7385e8e4ec4 (diff)
downloadcpython-749bd42072c3ecf04be3e38ca479e48692bbfd7e.zip
cpython-749bd42072c3ecf04be3e38ca479e48692bbfd7e.tar.gz
cpython-749bd42072c3ecf04be3e38ca479e48692bbfd7e.tar.bz2
Merge #13579: teach string.Formatter about 'a'.
Patch by Francisco Martín Brugué.
-rw-r--r--Doc/library/string.rst11
-rw-r--r--Lib/string.py10
-rw-r--r--Lib/test/test_string.py6
-rw-r--r--Misc/NEWS2
4 files changed, 20 insertions, 9 deletions
diff --git a/Doc/library/string.rst b/Doc/library/string.rst
index aed191b..79d4e3f 100644
--- a/Doc/library/string.rst
+++ b/Doc/library/string.rst
@@ -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:
diff --git a/Lib/string.py b/Lib/string.py
index 8bcd1dc..b57c79b 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -220,12 +220,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))
diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py
index 1615732..c2bdfdb 100644
--- a/Lib/test/test_string.py
+++ b/Lib/test/test_string.py
@@ -37,6 +37,12 @@ class ModuleTest(unittest.TestCase):
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_name_lookup(self):
fmt = string.Formatter()
diff --git a/Misc/NEWS b/Misc/NEWS
index 9f16caf..bb893cf 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -16,6 +16,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.