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@gmail.com>
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 32ff5af..5b62842 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2597,7 +2597,7 @@
     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 */
         }
@@ -2779,6 +2779,9 @@
             Py_DECREF(it);
             return result;
         }
+        if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
+            return NULL;
+        }
     }
 
     PyErr_Format(PyExc_TypeError,