Closed issue #8931: Make alternate formatting for 'c' raise an exception. Patch by Torsten Landschoff.

This commit is contained in:
Eric V. Smith 2014-04-15 03:05:02 -04:00
parent 9417764e01
commit a0d107324d
3 changed files with 13 additions and 0 deletions

View File

@ -343,6 +343,8 @@ def test(i, format_spec, result):
self.assertRaises(ValueError, 3 .__format__, ",n") self.assertRaises(ValueError, 3 .__format__, ",n")
# can't have ',' with 'c' # can't have ',' with 'c'
self.assertRaises(ValueError, 3 .__format__, ",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 # ensure that only int and float type specifiers work
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +

View File

@ -34,6 +34,10 @@ Core and Builtins
replacement fields. It now matches the behavior of str.format() in replacement fields. It now matches the behavior of str.format() in
this regard. Patches by Phil Elson and Ramchandra Apte. 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 Library
------- -------

View File

@ -846,6 +846,13 @@ format_long_internal(PyObject *value, const InternalFormatSpec *format,
" format specifier 'c'"); " format specifier 'c'");
goto done; 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() */ /* taken from unicodeobject.c formatchar() */
/* Integer input truncated to a character */ /* Integer input truncated to a character */