bpo-42923: _Py_DumpExtensionModules() ignores stdlib ext (GH-24254)

diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index 5e72ba9..67175cd 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -547,8 +547,7 @@ def test_pynumber_tobase(self):
         self.assertRaises(TypeError, pynumber_tobase, '123', 10)
         self.assertRaises(SystemError, pynumber_tobase, 123, 0)
 
-    def test_fatal_error(self):
-        code = 'import _testcapi; _testcapi.fatal_error(b"MESSAGE")'
+    def check_fatal_error(self, code, expected, not_expected=()):
         with support.SuppressCrashReport():
             rc, out, err = assert_python_failure('-sSI', '-c', code)
 
@@ -556,15 +555,25 @@ def test_fatal_error(self):
         self.assertIn('Fatal Python error: test_fatal_error: MESSAGE\n',
                       err)
 
-        match = re.search('^Extension modules:(.*)$', err, re.MULTILINE)
+        match = re.search(r'^Extension modules:(.*) \(total: ([0-9]+)\)$',
+                          err, re.MULTILINE)
         if not match:
             self.fail(f"Cannot find 'Extension modules:' in {err!r}")
         modules = set(match.group(1).strip().split(', '))
-        # Test _PyModule_IsExtension(): the list doesn't have to
-        # be exhaustive.
-        for name in ('sys', 'builtins', '_imp', '_thread', '_weakref',
-                     '_io', 'marshal', '_signal', '_abc', '_testcapi'):
+        total = int(match.group(2))
+
+        for name in expected:
             self.assertIn(name, modules)
+        for name in not_expected:
+            self.assertNotIn(name, modules)
+        self.assertEqual(len(modules), total)
+
+    def test_fatal_error(self):
+        expected = ('_testcapi',)
+        not_expected = ('sys', 'builtins', '_imp', '_thread', '_weakref',
+                        '_io', 'marshal', '_signal', '_abc')
+        code = 'import _testcapi; _testcapi.fatal_error(b"MESSAGE")'
+        self.check_fatal_error(code, expected, not_expected)
 
 
 class TestPendingCalls(unittest.TestCase):
diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py
index c6b763a..b4a654f 100644
--- a/Lib/test/test_faulthandler.py
+++ b/Lib/test/test_faulthandler.py
@@ -334,19 +334,19 @@ def test_disable(self):
     def test_dump_ext_modules(self):
         code = """
             import faulthandler
+            # _testcapi is a test module and not considered as a stdlib module
+            import _testcapi
             faulthandler.enable()
             faulthandler._sigsegv()
             """
         stderr, exitcode = self.get_output(code)
         stderr = '\n'.join(stderr)
-        match = re.search('^Extension modules:(.*)$', stderr, re.MULTILINE)
+        match = re.search(r'^Extension modules:(.*) \(total: [0-9]+\)$',
+                          stderr, re.MULTILINE)
         if not match:
             self.fail(f"Cannot find 'Extension modules:' in {stderr!r}")
         modules = set(match.group(1).strip().split(', '))
-        # Only check for a few extensions, the list doesn't have to be
-        # exhaustive.
-        for ext in ('sys', 'builtins', '_io', 'faulthandler'):
-            self.assertIn(ext, modules)
+        self.assertIn('_testcapi', modules)
 
     def test_is_enabled(self):
         orig_stderr = sys.stderr