]> granicus.if.org Git - python/commitdiff
Issue #29159: Fix regression in bytes(x) when x.__index__() raises Exception.
authorINADA Naoki <songofacandy@gmail.com>
Fri, 6 Jan 2017 08:32:01 +0000 (17:32 +0900)
committerINADA Naoki <songofacandy@gmail.com>
Fri, 6 Jan 2017 08:32:01 +0000 (17:32 +0900)
Lib/test/test_bytes.py
Misc/NEWS
Objects/bytearrayobject.c
Objects/bytesobject.c

index b396a76a40d837b16520bb4335eaedf9ed2241b8..a103a7d39cae2416109f8d6b039ebded7bdc1c27 100644 (file)
@@ -4,6 +4,7 @@ XXX This is a mess.  Common tests should be unified with string_tests.py (and
 the latter should be modernized).
 """
 
+import array
 import os
 import re
 import sys
@@ -81,6 +82,18 @@ class BaseBytesTest:
         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')
index 0a32e264b6caaf18cc623d8d40d0e775ae8c4236..113ca8abbe8467114f81d754e8d7daffb047a0fb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ What's New in Python 3.6.1 release candidate 1?
 Core and Builtins
 -----------------
 
+- Issue #29159: Fix regression in bytes(x) when x.__index__() raises Exception.
+
 - Issue #28932: Do not include <sys/random.h> if it does not exist.
 
 - Issue #25677: Correct the positioning of the syntax error caret for
index c6d0707167c99aa3ba568dd013e0395a7d679e97..a8d698025087361f68d0b76b4820c0f279814cf8 100644 (file)
@@ -798,18 +798,22 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
     if (PyIndex_Check(arg)) {
         count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
         if (count == -1 && PyErr_Occurred()) {
-            return -1;
-        }
-        if (count < 0) {
-            PyErr_SetString(PyExc_ValueError, "negative count");
-            return -1;
+            if (PyErr_ExceptionMatches(PyExc_OverflowError))
+                return -1;
+            PyErr_Clear();  /* fall through */
         }
-        if (count > 0) {
-            if (PyByteArray_Resize((PyObject *)self, count))
+        else {
+            if (count < 0) {
+                PyErr_SetString(PyExc_ValueError, "negative count");
                 return -1;
-            memset(PyByteArray_AS_STRING(self), 0, count);
+            }
+            if (count > 0) {
+                if (PyByteArray_Resize((PyObject *)self, count))
+                    return -1;
+                memset(PyByteArray_AS_STRING(self), 0, count);
+            }
+            return 0;
         }
-        return 0;
     }
 
     /* Use the buffer API */
index b22e57effe6a1819682b3c1b2fd9f6e0e6603601..5d4844096008528074b17aa06507ecb2e1734387 100644 (file)
@@ -2593,16 +2593,20 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     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);