[Python-checkins] cpython (2.7): Issue #7267: format(int, 'c') now raises OverflowError when the argument is not

victor.stinner python-checkins at python.org
Mon Nov 9 06:22:19 EST 2015


https://hg.python.org/cpython/rev/2f2c52c9ff38
changeset: 99013:2f2c52c9ff38
branch: 2.7
parent: 99005:77184a429dae
user: Victor Stinner <victor.stinner at gmail.com>
date: Mon Nov 09 12:21:09 2015 +0100
summary:
 Issue #7267: format(int, 'c') now raises OverflowError when the argument is not
in range(0, 256).
files:
 Lib/test/test_str.py | 5 +++++
 Misc/NEWS | 3 +++
 Objects/stringlib/formatter.h | 8 ++++++++
 3 files changed, 16 insertions(+), 0 deletions(-)
diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py
--- a/Lib/test/test_str.py
+++ b/Lib/test/test_str.py
@@ -428,6 +428,11 @@
 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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 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
--- a/Objects/stringlib/formatter.h
+++ b/Objects/stringlib/formatter.h
@@ -789,6 +789,7 @@
 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 @@
 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;
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /