[Python-checkins] cpython: Issue #23466: Raised OverflowError if %c argument is out of range.

serhiy.storchaka python-checkins at python.org
Fri Apr 3 19:54:08 CEST 2015


https://hg.python.org/cpython/rev/313fd1c819c5
changeset:   95420:313fd1c819c5
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Fri Apr 03 20:53:46 2015 +0300
summary:
  Issue #23466: Raised OverflowError if %c argument is out of range.

files:
  Lib/test/test_format.py |  12 ++++++------
  Objects/bytesobject.c   |  11 ++++++++---
  2 files changed, 14 insertions(+), 9 deletions(-)


diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -358,12 +358,12 @@
                  "not all arguments converted during bytes formatting")
         test_exc(b'no format', bytearray(b'1'), TypeError,
                  "not all arguments converted during bytes formatting")
-        test_exc(b"%c", -1, TypeError,
-                "%c requires an integer in range(256) or a single byte")
-        test_exc(b"%c", 256, TypeError,
-                "%c requires an integer in range(256) or a single byte")
-        test_exc(b"%c", 2**128, TypeError,
-                "%c requires an integer in range(256) or a single byte")
+        test_exc(b"%c", -1, OverflowError,
+                "%c arg not in range(256)")
+        test_exc(b"%c", 256, OverflowError,
+                "%c arg not in range(256)")
+        test_exc(b"%c", 2**128, OverflowError,
+                "%c arg not in range(256)")
         test_exc(b"%c", b"Za", TypeError,
                 "%c requires an integer in range(256) or a single byte")
         test_exc(b"%c", "Y", TypeError,
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -496,10 +496,15 @@
             ival = PyLong_AsLongAndOverflow(iobj, &overflow);
             Py_DECREF(iobj);
         }
-        if (!overflow && 0 <= ival && ival <= 255) {
-            *p = (char)ival;
-            return 1;
+        if (!overflow && ival == -1 && PyErr_Occurred())
+            goto onError;
+        if (overflow || !(0 <= ival && ival <= 255)) {
+            PyErr_SetString(PyExc_OverflowError,
+                            "%c arg not in range(256)");
+            return 0;
         }
+        *p = (char)ival;
+        return 1;
     }
   onError:
     PyErr_SetString(PyExc_TypeError,

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list