[Python-checkins] cpython (merge 3.6 -> default): Merge 3.6

inada.naoki python-checkins at python.org
Fri Jan 6 03:45:02 EST 2017


https://hg.python.org/cpython/rev/da14ffe01f08
changeset:   106008:da14ffe01f08
parent:      106006:c9b846f48752
parent:      106007:505cc50ddc82
user:        INADA Naoki <songofacandy at gmail.com>
date:        Fri Jan 06 17:44:43 2017 +0900
summary:
  Merge 3.6

files:
  Lib/test/test_bytes.py    |  13 +++++++++++++
  Misc/NEWS                 |   2 ++
  Objects/bytearrayobject.c |  24 ++++++++++++++----------
  Objects/bytesobject.c     |  20 ++++++++++++--------
  4 files changed, 41 insertions(+), 18 deletions(-)


diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -4,6 +4,7 @@
 the latter should be modernized).
 """
 
+import array
 import os
 import re
 import sys
@@ -81,6 +82,18 @@
         self.assertRaises(ValueError, self.type2test, [Indexable(-1)])
         self.assertRaises(ValueError, self.type2test, [Indexable(256)])
 
+    def test_from_buffer(self):
+        a = self.type2test(array.array('B', [1, 2, 3]))
+        self.assertEqual(a, b"\x01\x02\x03")
+
+        # http://bugs.python.org/issue29159
+        # Fallback when __index__ raises exception other than OverflowError
+        class B(bytes):
+            def __index__(self):
+                raise TypeError
+
+        self.assertEqual(self.type2test(B(b"foobar")), b"foobar")
+
     def test_from_ssize(self):
         self.assertEqual(self.type2test(0), b'')
         self.assertEqual(self.type2test(1), b'\x00')
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@
 Core and Builtins
 -----------------
 
+- Issue #29159: Fix regression in bytes(x) when x.__index__() raises Exception.
+
 - Issue #29049: Call _PyObject_GC_TRACK() lazily when calling Python function.
   Calling function is up to 5% faster.
 
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -798,18 +798,22 @@
     if (PyIndex_Check(arg)) {
         count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
         if (count == -1 && PyErr_Occurred()) {
-            return -1;
+            if (PyErr_ExceptionMatches(PyExc_OverflowError))
+                return -1;
+            PyErr_Clear();  /* fall through */
         }
-        if (count < 0) {
-            PyErr_SetString(PyExc_ValueError, "negative count");
-            return -1;
+        else {
+            if (count < 0) {
+                PyErr_SetString(PyExc_ValueError, "negative count");
+                return -1;
+            }
+            if (count > 0) {
+                if (PyByteArray_Resize((PyObject *)self, count))
+                    return -1;
+                memset(PyByteArray_AS_STRING(self), 0, count);
+            }
+            return 0;
         }
-        if (count > 0) {
-            if (PyByteArray_Resize((PyObject *)self, count))
-                return -1;
-            memset(PyByteArray_AS_STRING(self), 0, count);
-        }
-        return 0;
     }
 
     /* Use the buffer API */
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2593,16 +2593,20 @@
     if (PyIndex_Check(x)) {
         size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
         if (size == -1 && PyErr_Occurred()) {
-            return NULL;
+            if (PyErr_ExceptionMatches(PyExc_OverflowError))
+                return NULL;
+            PyErr_Clear();  /* fall through */
         }
-        if (size < 0) {
-            PyErr_SetString(PyExc_ValueError, "negative count");
-            return NULL;
+        else {
+            if (size < 0) {
+                PyErr_SetString(PyExc_ValueError, "negative count");
+                return NULL;
+            }
+            new = _PyBytes_FromSize(size, 1);
+            if (new == NULL)
+                return NULL;
+            return new;
         }
-        new = _PyBytes_FromSize(size, 1);
-        if (new == NULL)
-            return NULL;
-        return new;
     }
 
     return PyBytes_FromObject(x);

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


More information about the Python-checkins mailing list