diff -r a29eaffa7d72 Lib/test/test_str.py --- a/Lib/test/test_str.py Tue Jun 25 00:48:02 2013 +0200 +++ b/Lib/test/test_str.py Tue Jul 02 02:32:30 2013 +0200 @@ -428,6 +428,11 @@ class StrTest( self.assertEqual('{:{f}}{g}{}'.format(1, 3, g='g', f=2), ' 1g3') self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g') + def test_format_c_overflow(self): + # issue #7267 + self.assertRaises(OverflowError, '{0:c}'.format, -1) + self.assertRaises(OverflowError, '{0:c}'.format, 256) + def test_buffer_is_readonly(self): self.assertRaises(TypeError, sys.stdin.readinto, b"") diff -r a29eaffa7d72 Misc/NEWS --- a/Misc/NEWS Tue Jun 25 00:48:02 2013 +0200 +++ b/Misc/NEWS Tue Jul 02 02:32:30 2013 +0200 @@ -9,6 +9,9 @@ What's New in Python 2.7.6? Core and Builtins ----------------- +- Issue #7267: format(int, 'c') now raises OverflowError when the argument is + not in range(0, 256). + - Issue #18184: PyUnicode_FromFormat() and PyUnicode_FromFormatV() now raise OverflowError when an argument of %c format is out of range. diff -r a29eaffa7d72 Objects/stringlib/formatter.h --- a/Objects/stringlib/formatter.h Tue Jun 25 00:48:02 2013 +0200 +++ b/Objects/stringlib/formatter.h Tue Jul 02 02:32:30 2013 +0200 @@ -788,6 +788,7 @@ format_int_or_long_internal(PyObject *va x = PyLong_AsLong(value); if (x == -1 && PyErr_Occurred()) goto done; +#if STRINGLIB_IS_UNICODE #ifdef Py_UNICODE_WIDE if (x < 0 || x > 0x10ffff) { PyErr_SetString(PyExc_OverflowError, @@ -803,6 +804,13 @@ format_int_or_long_internal(PyObject *va goto done; } #endif +#else + if (x < 0 || x > 0xff) { + PyErr_SetString(PyExc_OverflowError, + "%c arg not in range(0x100)"); + goto done; + } +#endif numeric_char = (STRINGLIB_CHAR)x; pnumeric_chars = &numeric_char; n_digits = 1;