|
21 | 21 | from test import support |
22 | 22 | from test.support import run_unittest, findfile, python_is_optimized |
23 | 23 |
|
24 | | -try: |
25 | | - gdb_version, _ = subprocess.Popen(["gdb", "-nx", "--version"], |
26 | | - stdout=subprocess.PIPE).communicate() |
27 | | -except OSError: |
28 | | - # This is what "no gdb" looks like. There may, however, be other |
29 | | - # errors that manifest this way too. |
30 | | - raise unittest.SkipTest("Couldn't find gdb on the path") |
31 | | -try: |
32 | | - gdb_version_number = re.search(b"^GNU gdb [^\d]*(\d+)\.(\d)", gdb_version) |
33 | | - gdb_major_version = int(gdb_version_number.group(1)) |
34 | | - gdb_minor_version = int(gdb_version_number.group(2)) |
35 | | -except Exception: |
36 | | - raise ValueError("unable to parse GDB version: %r" % gdb_version) |
37 | | - |
| 24 | +def get_gdb_version(): |
| 25 | + try: |
| 26 | + proc = subprocess.Popen(["gdb", "-nx", "--version"], |
| 27 | + stdout=subprocess.PIPE, |
| 28 | + universal_newlines=True) |
| 29 | + with proc: |
| 30 | + version = proc.communicate()[0] |
| 31 | + except OSError: |
| 32 | + # This is what "no gdb" looks like. There may, however, be other |
| 33 | + # errors that manifest this way too. |
| 34 | + raise unittest.SkipTest("Couldn't find gdb on the path") |
| 35 | + |
| 36 | + # Regex to parse: |
| 37 | + # 'GNU gdb (GDB; SUSE Linux Enterprise 12) 7.7\n' -> 7.7 |
| 38 | + # 'GNU gdb (GDB) Fedora 7.9.1-17.fc22\n' -> 7.9 |
| 39 | + match = re.search("^GNU gdb .*? (\d+)\.(\d)", version) |
| 40 | + if match is None: |
| 41 | + raise Exception("unable to parse GDB version: %r" % version) |
| 42 | + return (version, int(match.group(1)), int(match.group(2))) |
| 43 | + |
| 44 | +gdb_version, gdb_major_version, gdb_minor_version = get_gdb_version() |
38 | 45 | if gdb_major_version < 7: |
39 | | - raise unittest.SkipTest("gdb versions before 7.0 didn't support python embedding" |
40 | | - " Saw:\n" + gdb_version.decode('ascii', 'replace')) |
| 46 | + raise unittest.SkipTest("gdb versions before 7.0 didn't support python " |
| 47 | + "embedding. Saw %s.%s:\n%s" |
| 48 | + % (gdb_major_version, gdb_minor_version, |
| 49 | + gdb_version)) |
41 | 50 |
|
42 | 51 | if not sysconfig.is_python_build(): |
43 | 52 | raise unittest.SkipTest("test_gdb only works on source builds at the moment.") |
@@ -65,7 +74,8 @@ def run_gdb(*args, **env_vars): |
65 | 74 | base_cmd += ('-iex', 'add-auto-load-safe-path ' + checkout_hook_path) |
66 | 75 | proc = subprocess.Popen(base_cmd + args, |
67 | 76 | stdout=subprocess.PIPE, |
68 | | - stderr=subprocess.PIPE, env=env) |
| 77 | + stderr=subprocess.PIPE, |
| 78 | + env=env) |
69 | 79 | with proc: |
70 | 80 | out, err = proc.communicate() |
71 | 81 | return out.decode('utf-8', 'replace'), err.decode('utf-8', 'replace') |
@@ -886,8 +896,8 @@ def test_locals_after_up(self): |
886 | 896 |
|
887 | 897 | def test_main(): |
888 | 898 | if support.verbose: |
889 | | - print("GDB version:") |
890 | | - for line in os.fsdecode(gdb_version).splitlines(): |
| 899 | + print("GDB version %s.%s:" % (gdb_major_version, gdb_minor_version)) |
| 900 | + for line in gdb_version.splitlines(): |
891 | 901 | print(" " * 4 + line) |
892 | 902 | run_unittest(PrettyPrintTests, |
893 | 903 | PyListTests, |
|
0 commit comments