summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-11-09 11:21:09 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-11-09 11:21:09 (GMT)
commite192d0bbb969bcefe4658daddd9e495153fadad5 (patch)
tree830b284ff305b9ffd984c5c682dca8c7b7860fde
parent8d0a94df33a356a67d72c85ebf1aebb641e94e45 (diff)
downloadcpython-e192d0bbb969bcefe4658daddd9e495153fadad5.zip
cpython-e192d0bbb969bcefe4658daddd9e495153fadad5.tar.gz
cpython-e192d0bbb969bcefe4658daddd9e495153fadad5.tar.bz2
Issue #7267: format(int, 'c') now raises OverflowError when the argument is not
in range(0, 256).
-rw-r--r--Lib/test/test_str.py5
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/stringlib/formatter.h8
3 files changed, 16 insertions, 0 deletions
diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py
index 2cd7966..774c634 100644
--- a/Lib/test/test_str.py
+++ b/Lib/test/test_str.py
@@ -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 --git a/Misc/NEWS b/Misc/NEWS
index f9163d6..3ea5c6b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 2.7.11?
Core and Builtins
-----------------
+- Issue #7267: format(int, 'c') now raises OverflowError when the argument is
+ not in range(0, 256).
+
- Issue #24806: Prevent builtin types that are not allowed to be subclassed from
being subclassed through multiple inheritance.
diff --git a/Objects/stringlib/formatter.h b/Objects/stringlib/formatter.h
index 122abe6..b75755e 100644
--- a/Objects/stringlib/formatter.h
+++ b/Objects/stringlib/formatter.h
@@ -789,6 +789,7 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
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,
@@ -804,6 +805,13 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
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;