bpo-40436: Fix code parsing gdb version (GH-19792)


test_gdb and test.pythoninfo now check gdb command exit code.
(cherry picked from commit ec9bea4a3766bd815148a27f61eb24e7dd459ac7)

Co-authored-by: Victor Stinner <vstinner@python.org>
diff --git a/Lib/test/test_gdb.py b/Lib/test/test_gdb.py
index bd9a5cb..f043c92 100644
--- a/Lib/test/test_gdb.py
+++ b/Lib/test/test_gdb.py
@@ -17,12 +17,18 @@
 
 def get_gdb_version():
     try:
-        proc = subprocess.Popen(["gdb", "-nx", "--version"],
+        cmd = ["gdb", "-nx", "--version"]
+        proc = subprocess.Popen(cmd,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 universal_newlines=True)
         with proc:
-            version = proc.communicate()[0]
+            version, stderr = proc.communicate()
+
+        if proc.returncode:
+            raise Exception(f"Command {' '.join(cmd)!r} failed "
+                            f"with exit code {proc.returncode}: "
+                            f"stdout={version!r} stderr={stderr!r}")
     except OSError:
         # This is what "no gdb" looks like.  There may, however, be other
         # errors that manifest this way too.