From: Eric V. Smith Date: Tue, 15 Apr 2014 07:05:02 +0000 (-0400) Subject: Closed issue #8931: Make alternate formatting for 'c' raise an exception. Patch by... X-Git-Tag: v3.4.1rc1~76 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a0d107324d38ab6fc5af4c6fee272e1097f98f49;p=thirdparty%2FPython%2Fcpython.git Closed issue #8931: Make alternate formatting for 'c' raise an exception. Patch by Torsten Landschoff. --- diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index ec10752e6a2a..11d95465a813 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -343,6 +343,8 @@ class TypesTests(unittest.TestCase): self.assertRaises(ValueError, 3 .__format__, ",n") # can't have ',' with 'c' self.assertRaises(ValueError, 3 .__format__, ",c") + # can't have '#' with 'c' + self.assertRaises(ValueError, 3 .__format__, "#c") # ensure that only int and float type specifiers work for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + diff --git a/Misc/NEWS b/Misc/NEWS index 45401d6cd8a5..a158999f9829 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -34,6 +34,10 @@ Core and Builtins replacement fields. It now matches the behavior of str.format() in this regard. Patches by Phil Elson and Ramchandra Apte. +- Issue #8931: Make alternate formatting ('#') for type 'c' raise an + exception. It had no effect, now trying to specify it is an error. + Patch by Torsten Landschoff. + Library ------- diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c index e3a814984151..056bb7690259 100644 --- a/Python/formatter_unicode.c +++ b/Python/formatter_unicode.c @@ -846,6 +846,13 @@ format_long_internal(PyObject *value, const InternalFormatSpec *format, " format specifier 'c'"); goto done; } + /* error to request alternate format */ + if (format->alternate) { + PyErr_SetString(PyExc_ValueError, + "Alternate form (#) not allowed with integer" + " format specifier 'c'"); + goto done; + } /* taken from unicodeobject.c formatchar() */ /* Integer input truncated to a character */