Kaydet (Commit) a634e232 authored tarafından INADA Naoki's avatar INADA Naoki

Issue #29159: Fix regression in bytes(x) when x.__index__() raises Exception.

üst a251fb02
...@@ -4,6 +4,7 @@ XXX This is a mess. Common tests should be unified with string_tests.py (and ...@@ -4,6 +4,7 @@ XXX This is a mess. Common tests should be unified with string_tests.py (and
the latter should be modernized). the latter should be modernized).
""" """
import array
import os import os
import re import re
import sys import sys
...@@ -81,6 +82,18 @@ class BaseBytesTest: ...@@ -81,6 +82,18 @@ class BaseBytesTest:
self.assertRaises(ValueError, self.type2test, [Indexable(-1)]) self.assertRaises(ValueError, self.type2test, [Indexable(-1)])
self.assertRaises(ValueError, self.type2test, [Indexable(256)]) 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): def test_from_ssize(self):
self.assertEqual(self.type2test(0), b'') self.assertEqual(self.type2test(0), b'')
self.assertEqual(self.type2test(1), b'\x00') self.assertEqual(self.type2test(1), b'\x00')
......
...@@ -10,6 +10,8 @@ What's New in Python 3.6.1 release candidate 1? ...@@ -10,6 +10,8 @@ What's New in Python 3.6.1 release candidate 1?
Core and Builtins 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 #28932: Do not include <sys/random.h> if it does not exist.
- Issue #25677: Correct the positioning of the syntax error caret for - Issue #25677: Correct the positioning of the syntax error caret for
......
...@@ -798,8 +798,11 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) ...@@ -798,8 +798,11 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
if (PyIndex_Check(arg)) { if (PyIndex_Check(arg)) {
count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
if (count == -1 && PyErr_Occurred()) { if (count == -1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
return -1; return -1;
PyErr_Clear(); /* fall through */
} }
else {
if (count < 0) { if (count < 0) {
PyErr_SetString(PyExc_ValueError, "negative count"); PyErr_SetString(PyExc_ValueError, "negative count");
return -1; return -1;
...@@ -811,6 +814,7 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) ...@@ -811,6 +814,7 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
} }
return 0; return 0;
} }
}
/* Use the buffer API */ /* Use the buffer API */
if (PyObject_CheckBuffer(arg)) { if (PyObject_CheckBuffer(arg)) {
......
...@@ -2593,8 +2593,11 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -2593,8 +2593,11 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (PyIndex_Check(x)) { if (PyIndex_Check(x)) {
size = PyNumber_AsSsize_t(x, PyExc_OverflowError); size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
if (size == -1 && PyErr_Occurred()) { if (size == -1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
return NULL; return NULL;
PyErr_Clear(); /* fall through */
} }
else {
if (size < 0) { if (size < 0) {
PyErr_SetString(PyExc_ValueError, "negative count"); PyErr_SetString(PyExc_ValueError, "negative count");
return NULL; return NULL;
...@@ -2604,6 +2607,7 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -2604,6 +2607,7 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL; return NULL;
return new; return new;
} }
}
return PyBytes_FromObject(x); return PyBytes_FromObject(x);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment