diff options
author | R David Murray <rdmurray@bitdance.com> | 2012-08-19 21:45:40 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2012-08-19 21:45:40 (GMT) |
commit | 749bd42072c3ecf04be3e38ca479e48692bbfd7e (patch) | |
tree | 0471849a785ec2bc5df6f60497cebaf66d790051 /Lib | |
parent | 8528c3145e5856a88199f07e155b3c75710cc2a1 (diff) | |
parent | e56bf97ef4283a877c459390516bb7385e8e4ec4 (diff) | |
download | cpython-749bd42072c3ecf04be3e38ca479e48692bbfd7e.zip cpython-749bd42072c3ecf04be3e38ca479e48692bbfd7e.tar.gz cpython-749bd42072c3ecf04be3e38ca479e48692bbfd7e.tar.bz2 |
Merge #13579: teach string.Formatter about 'a'.
Patch by Francisco Martín Brugué.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/string.py | 10 | ||||
-rw-r--r-- | Lib/test/test_string.py | 6 |
2 files changed, 12 insertions, 4 deletions
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() |