Skip to content

Commit e890421

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.
1 parent de2aea0 commit e890421

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
@@ -41,7 +41,6 @@ _getbytevalue(PyObject* arg, int *value)
4141
} else {
4242
PyObject *index = PyNumber_Index(arg);
4343
if (index == NULL) {
44-
PyErr_Format(PyExc_TypeError, "an integer is required");
4544
*value = -1;
4645
return 0;
4746
}
@@ -821,7 +820,7 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
821820
if (PyIndex_Check(arg)) {
822821
count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
823822
if (count == -1 && PyErr_Occurred()) {
824-
if (PyErr_ExceptionMatches(PyExc_OverflowError))
823+
if (!PyErr_ExceptionMatches(PyExc_TypeError))
825824
return -1;
826825
PyErr_Clear(); /* fall through */
827826
}

Objects/bytesobject.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2596,7 +2596,7 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
25962596
if (PyIndex_Check(x)) {
25972597
size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
25982598
if (size == -1 && PyErr_Occurred()) {
2599-
if (PyErr_ExceptionMatches(PyExc_OverflowError))
2599+
if (!PyErr_ExceptionMatches(PyExc_TypeError))
26002600
return NULL;
26012601
PyErr_Clear(); /* fall through */
26022602
}
@@ -2778,6 +2778,9 @@ PyBytes_FromObject(PyObject *x)
27782778
Py_DECREF(it);
27792779
return result;
27802780
}
2781+
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2782+
return NULL;
2783+
}
27812784
}
27822785

27832786
PyErr_Format(PyExc_TypeError,

0 commit comments

Comments
 (0)