Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 749bd42

Browse files
committed
Merge #13579: teach string.Formatter about 'a'.
Patch by Francisco Martín Brugué.
2 parents 8528c31 + e56bf97 commit 749bd42

4 files changed

Lines changed: 20 additions & 9 deletions

File tree

Doc/library/string.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ implementation as the built-in :meth:`format` method.
9191

9292
.. method:: format(format_string, *args, **kwargs)
9393

94-
:meth:`format` is the primary API method. It takes a format template
95-
string, and an arbitrary set of positional and keyword argument.
94+
:meth:`format` is the primary API method. It takes a format string and
95+
an arbitrary set of positional and keyword arguments.
9696
:meth:`format` is just a wrapper that calls :meth:`vformat`.
9797

9898
.. method:: vformat(format_string, args, kwargs)
@@ -101,8 +101,8 @@ implementation as the built-in :meth:`format` method.
101101
separate function for cases where you want to pass in a predefined
102102
dictionary of arguments, rather than unpacking and repacking the
103103
dictionary as individual arguments using the ``*args`` and ``**kwds``
104-
syntax. :meth:`vformat` does the work of breaking up the format template
105-
string into character data and replacement fields. It calls the various
104+
syntax. :meth:`vformat` does the work of breaking up the format string
105+
into character data and replacement fields. It calls the various
106106
methods described below.
107107

108108
In addition, the :class:`Formatter` defines a number of methods that are
@@ -173,7 +173,8 @@ implementation as the built-in :meth:`format` method.
173173

174174
Converts the value (returned by :meth:`get_field`) given a conversion type
175175
(as in the tuple returned by the :meth:`parse` method). The default
176-
version understands 'r' (repr) and 's' (str) conversion types.
176+
version understands 's' (str), 'r' (repr) and 'a' (ascii) conversion
177+
types.
177178

178179

179180
.. _formatstrings:

Lib/string.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,14 @@ def format_field(self, value, format_spec):
220220

221221
def convert_field(self, value, conversion):
222222
# do any conversion on the resulting object
223-
if conversion == 'r':
224-
return repr(value)
223+
if conversion is None:
224+
return value
225225
elif conversion == 's':
226226
return str(value)
227-
elif conversion is None:
228-
return value
227+
elif conversion == 'r':
228+
return repr(value)
229+
elif conversion == 'a':
230+
return ascii(value)
229231
raise ValueError("Unknown conversion specifier {0!s}".format(conversion))
230232

231233

Lib/test/test_string.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def test_conversion_specifiers(self):
3737
self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-")
3838
self.assertEqual(fmt.format("{0!s}", 'test'), 'test')
3939
self.assertRaises(ValueError, fmt.format, "{0!h}", 'test')
40+
# issue13579
41+
self.assertEqual(fmt.format("{0!a}", 42), '42')
42+
self.assertEqual(fmt.format("{0!a}", string.ascii_letters),
43+
"'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'")
44+
self.assertEqual(fmt.format("{0!a}", chr(255)), "'\\xff'")
45+
self.assertEqual(fmt.format("{0!a}", chr(256)), "'\\u0100'")
4046

4147
def test_name_lookup(self):
4248
fmt = string.Formatter()

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Core and Builtins
1616
Library
1717
-------
1818

19+
- Issue #13579: string.Formatter now understands the 'a' conversion specifier.
20+
1921
- Issue #15595: Fix subprocess.Popen(universal_newlines=True)
2022
for certain locales (utf-16 and utf-32 family). Patch by Chris Jerdonek.
2123

0 commit comments

Comments
 (0)