[Python-checkins] bpo-34974: Do not replace unexpected errors in bytes() and bytearray(). (GH-9852)
Miss Islington (bot)
webhook-mailer at python.org
Sun Oct 14 17:32:06 EDT 2018
https://github.com/python/cpython/commit/08ba7eb89d5353af31ef9e66a5337abea1b676ef
commit: 08ba7eb89d5353af31ef9e66a5337abea1b676ef
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-10-14T14:32:03-07:00
summary:
bpo-34974: Do not replace unexpected errors in bytes() and bytearray(). (GH-9852)
bytes and bytearray constructors converted unexpected exceptions
(e.g. MemoryError and KeyboardInterrupt) to TypeError.
(cherry picked from commit e890421e334ccf0c000c6b29c4a521d86cd12f47)
Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2018-10-13-22-24-19.bpo-34974.7LgTc2.rst
M Lib/test/test_bytes.py
M Objects/bytearrayobject.c
M Objects/bytesobject.c
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 8c9d17fd1614..8b1973241e45 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -126,8 +126,8 @@ def test_from_buffer(self):
a = self.type2test(b"\x01\x02\x03")
self.assertEqual(a, b"\x01\x02\x03")
- # http://bugs.python.org/issue29159
- # Fallback when __index__ raises exception other than OverflowError
+ # Issues #29159 and #34974.
+ # Fallback when __index__ raises a TypeError
class B(bytes):
def __index__(self):
raise TypeError
@@ -184,6 +184,20 @@ def test_constructor_overflow(self):
except (OverflowError, MemoryError):
pass
+ def test_constructor_exceptions(self):
+ # Issue #34974: bytes and bytearray constructors replace unexpected
+ # exceptions.
+ class BadInt:
+ def __index__(self):
+ 1/0
+ self.assertRaises(ZeroDivisionError, self.type2test, BadInt())
+ self.assertRaises(ZeroDivisionError, self.type2test, [BadInt()])
+
+ class BadIterable:
+ def __iter__(self):
+ 1/0
+ self.assertRaises(ZeroDivisionError, self.type2test, BadIterable())
+
def test_compare(self):
b1 = self.type2test([1, 2, 3])
b2 = self.type2test([1, 2, 3])
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-10-13-22-24-19.bpo-34974.7LgTc2.rst b/Misc/NEWS.d/next/Core and Builtins/2018-10-13-22-24-19.bpo-34974.7LgTc2.rst
new file mode 100644
index 000000000000..2a7e773648ec
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-10-13-22-24-19.bpo-34974.7LgTc2.rst
@@ -0,0 +1,3 @@
+:class:`bytes` and :class:`bytearray` constructors no longer convert
+unexpected exceptions (e.g. :exc:`MemoryError` and :exc:`KeyboardInterrupt`)
+to :exc:`TypeError`.
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 15524572929c..673dd0c65133 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -39,7 +39,6 @@ _getbytevalue(PyObject* arg, int *value)
} else {
PyObject *index = PyNumber_Index(arg);
if (index == NULL) {
- PyErr_Format(PyExc_TypeError, "an integer is required");
*value = -1;
return 0;
}
@@ -819,7 +818,7 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
if (PyIndex_Check(arg)) {
count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
if (count == -1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
+ if (!PyErr_ExceptionMatches(PyExc_TypeError))
return -1;
PyErr_Clear(); /* fall through */
}
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 04ebe0b701e6..cd39ad632bbc 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2606,7 +2606,7 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (PyIndex_Check(x)) {
size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
if (size == -1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
+ if (!PyErr_ExceptionMatches(PyExc_TypeError))
return NULL;
PyErr_Clear(); /* fall through */
}
@@ -2788,6 +2788,9 @@ PyBytes_FromObject(PyObject *x)
Py_DECREF(it);
return result;
}
+ if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
+ return NULL;
+ }
}
PyErr_Format(PyExc_TypeError,
More information about the Python-checkins
mailing list