[3.10] bpo-45083: Include the exception class qualname when formatting an exception (GH-28119) (GH-28134)

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
(cherry picked from commit b4b6342848ec0459182a992151099252434cc619)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>

* Use a private version of _PyType_GetQualName

Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 1fd5247..8717def 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -1069,6 +1069,20 @@ def __del__(self):
                     self.assertIn("del is broken", report)
                 self.assertTrue(report.endswith("\n"))
 
+    def test_original_unraisablehook_exception_qualname(self):
+        class A:
+            class B:
+                class X(Exception):
+                    pass
+
+        with test.support.captured_stderr() as stderr, \
+             test.support.swap_attr(sys, 'unraisablehook',
+                                    sys.__unraisablehook__):
+                 expected = self.write_unraisable_exc(
+                     A.B.X(), "msg", "obj");
+        report = stderr.getvalue()
+        testName = 'test_original_unraisablehook_exception_qualname'
+        self.assertIn(f"{testName}.<locals>.A.B.X", report)
 
     def test_original_unraisablehook_wrong_type(self):
         exc = ValueError(42)
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 61d86a1..ee18383 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -781,6 +781,19 @@ def test_syntax_error_various_offsets(self):
                 exp = "\n".join(expected)
                 self.assertEqual(exp, err)
 
+    def test_format_exception_only_qualname(self):
+        class A:
+            class B:
+                class X(Exception):
+                    def __str__(self):
+                        return "I am X"
+                    pass
+        err = self.get_report(A.B.X())
+        str_value = 'I am X'
+        str_name = '.'.join([A.B.X.__module__, A.B.X.__qualname__])
+        exp = "%s: %s\n" % (str_name, str_value)
+        self.assertEqual(exp, err)
+
 
 class PyExcReportingTests(BaseExceptionReportingTests, unittest.TestCase):
     #