bpo-34756: Silence only ImportError and AttributeError in sys.breakpointhook(). (GH-9457)

(cherry picked from commit 6fe9c446f8302553952f63fc6d96be4dfa48ceba)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index efe5b29..75e4f4b 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -171,6 +171,12 @@
     return retval;
 
   error:
+    if (!PyErr_ExceptionMatches(PyExc_ImportError)
+        && !PyErr_ExceptionMatches(PyExc_AttributeError))
+    {
+        PyMem_RawFree(envar);
+        return NULL;
+    }
     /* If any of the imports went wrong, then warn and ignore. */
     PyErr_Clear();
     int status = PyErr_WarnFormat(