Issue #29159: Fix regression in bytes(x) when x.__index__() raises Exception.
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index c6d0707..a8d6980 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -798,18 +798,22 @@
     if (PyIndex_Check(arg)) {
         count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
         if (count == -1 && PyErr_Occurred()) {
-            return -1;
-        }
-        if (count < 0) {
-            PyErr_SetString(PyExc_ValueError, "negative count");
-            return -1;
-        }
-        if (count > 0) {
-            if (PyByteArray_Resize((PyObject *)self, count))
+            if (PyErr_ExceptionMatches(PyExc_OverflowError))
                 return -1;
-            memset(PyByteArray_AS_STRING(self), 0, count);
+            PyErr_Clear();  /* fall through */
         }
-        return 0;
+        else {
+            if (count < 0) {
+                PyErr_SetString(PyExc_ValueError, "negative count");
+                return -1;
+            }
+            if (count > 0) {
+                if (PyByteArray_Resize((PyObject *)self, count))
+                    return -1;
+                memset(PyByteArray_AS_STRING(self), 0, count);
+            }
+            return 0;
+        }
     }
 
     /* Use the buffer API */