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
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])
} else {
PyObject *index = PyNumber_Index(arg);
if (index == NULL) {
- PyErr_Format(PyExc_TypeError, "an integer is required");
*value = -1;
return 0;
}
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 */
}
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 */
}
Py_DECREF(it);
return result;
}
+ if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
+ return NULL;
+ }
}
PyErr_Format(PyExc_TypeError,