Skip to content

Commit 08ba7eb

Browse files
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 e890421) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 7c1c42b commit 08ba7eb

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

Lib/test/test_bytes.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ def test_from_buffer(self):
126126
a = self.type2test(b"\x01\x02\x03")
127127
self.assertEqual(a, b"\x01\x02\x03")
128128

129-
# http://bugs.python.org/issue29159
130-
# Fallback when __index__ raises exception other than OverflowError
129+
# Issues #29159 and #34974.
130+
# Fallback when __index__ raises a TypeError
131131
class B(bytes):
132132
def __index__(self):
133133
raise TypeError
@@ -184,6 +184,20 @@ def test_constructor_overflow(self):
184184
except (OverflowError, MemoryError):
185185
pass
186186

187+
def test_constructor_exceptions(self):
188+
# Issue #34974: bytes and bytearray constructors replace unexpected
189+
# exceptions.
190+
class BadInt:
191+
def __index__(self):
192+
1/0
193+
self.assertRaises(ZeroDivisionError, self.type2test, BadInt())
194+
self.assertRaises(ZeroDivisionError, self.type2test, [BadInt()])
195+
196+
class BadIterable:
197+
def __iter__(self):
198+
1/0
199+
self.assertRaises(ZeroDivisionError, self.type2test, BadIterable())
200+
187201
def test_compare(self):
188202
b1 = self.type2test([1, 2, 3])
189203
b2 = self.type2test([1, 2, 3])
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`bytes` and :class:`bytearray` constructors no longer convert
2+
unexpected exceptions (e.g. :exc:`MemoryError` and :exc:`KeyboardInterrupt`)
3+
to :exc:`TypeError`.

Objects/bytearrayobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ _getbytevalue(PyObject* arg, int *value)
3939
} else {
4040
PyObject *index = PyNumber_Index(arg);
4141
if (index == NULL) {
42-
PyErr_Format(PyExc_TypeError, "an integer is required");
4342
*value = -1;
4443
return 0;
4544
}
@@ -819,7 +818,7 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
819818
if (PyIndex_Check(arg)) {
820819
count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
821820
if (count == -1 && PyErr_Occurred()) {
822-
if (PyErr_ExceptionMatches(PyExc_OverflowError))
821+
if (!PyErr_ExceptionMatches(PyExc_TypeError))
823822
return -1;
824823
PyErr_Clear(); /* fall through */
825824
}

Objects/bytesobject.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2606,7 +2606,7 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26062606
if (PyIndex_Check(x)) {
26072607
size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
26082608
if (size == -1 && PyErr_Occurred()) {
2609-
if (PyErr_ExceptionMatches(PyExc_OverflowError))
2609+
if (!PyErr_ExceptionMatches(PyExc_TypeError))
26102610
return NULL;
26112611
PyErr_Clear(); /* fall through */
26122612
}
@@ -2788,6 +2788,9 @@ PyBytes_FromObject(PyObject *x)
27882788
Py_DECREF(it);
27892789
return result;
27902790
}
2791+
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2792+
return NULL;
2793+
}
27912794
}
27922795

27932796
PyErr_Format(PyExc_TypeError,

0 commit comments

Comments
 (0)