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

Skip to content

Commit e56bf97

Browse files
committed
#13579: teach string.Formatter about 'a'.
Patch by Francisco Martín Brugué.
1 parent 8286071 commit e56bf97

4 files changed

Lines changed: 26 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
@@ -236,12 +236,14 @@ def format_field(self, value, format_spec):
236236

237237
def convert_field(self, value, conversion):
238238
# do any conversion on the resulting object
239-
if conversion == 'r':
240-
return repr(value)
239+
if conversion is None:
240+
return value
241241
elif conversion == 's':
242242
return str(value)
243-
elif conversion is None:
244-
return value
243+
elif conversion == 'r':
244+
return repr(value)
245+
elif conversion == 'a':
246+
return ascii(value)
245247
raise ValueError("Unknown conversion specifier {0!s}".format(conversion))
246248

247249

Lib/test/test_string.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ def test_capwords(self):
2626
self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def')
2727
self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t')
2828

29+
def test_conversion_specifiers(self):
30+
fmt = string.Formatter()
31+
self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-")
32+
self.assertEqual(fmt.format("{0!s}", 'test'), 'test')
33+
self.assertRaises(ValueError, fmt.format, "{0!h}", 'test')
34+
# issue13579
35+
self.assertEqual(fmt.format("{0!a}", 42), '42')
36+
self.assertEqual(fmt.format("{0!a}", string.ascii_letters),
37+
"'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'")
38+
self.assertEqual(fmt.format("{0!a}", chr(255)), "'\\xff'")
39+
self.assertEqual(fmt.format("{0!a}", chr(256)), "'\\u0100'")
40+
2941
def test_formatter(self):
3042
fmt = string.Formatter()
3143
self.assertEqual(fmt.format("foo"), "foo")

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ Core and Builtins
104104
Library
105105
-------
106106

107+
- Issue #13579: string.Formatter now understands the 'a' conversion specifier.
108+
107109
- Issue #15595: Fix subprocess.Popen(universal_newlines=True)
108110
for certain locales (utf-16 and utf-32 family). Patch by Chris Jerdonek.
109111

0 commit comments

Comments
 (0)