Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b6b4a8

Browse files
committed
test_gdb: use subprocess.Popen context manager to fix ResourceWarning warnings
when the test is interrupted (or fail).
1 parent 2f3ac1e commit 5b6b4a8

1 file changed

Lines changed: 29 additions & 20 deletions

File tree

Lib/test/test_gdb.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,32 @@
2121
from test import support
2222
from test.support import run_unittest, findfile, python_is_optimized
2323

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-
# Regex to parse:
32-
# 'GNU gdb (GDB; SUSE Linux Enterprise 12) 7.7\n' -> 7.7
33-
# 'GNU gdb (GDB) Fedora 7.9.1-17.fc22\n' -> 7.9
34-
gdb_version_number = re.search(b"^GNU gdb .*? (\d+)\.(\d)", gdb_version)
35-
if not gdb_version_number:
36-
raise Exception("unable to parse GDB version: %a" % gdb_version)
37-
gdb_major_version = int(gdb_version_number.group(1))
38-
gdb_minor_version = int(gdb_version_number.group(2))
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()
3945
if gdb_major_version < 7:
4046
raise unittest.SkipTest("gdb versions before 7.0 didn't support python "
4147
"embedding. Saw %s.%s:\n%s"
4248
% (gdb_major_version, gdb_minor_version,
43-
gdb_version.decode('ascii', 'replace')))
49+
gdb_version))
4450

4551
if not sysconfig.is_python_build():
4652
raise unittest.SkipTest("test_gdb only works on source builds at the moment.")
@@ -66,9 +72,12 @@ def run_gdb(*args, **env_vars):
6672
base_cmd = ('gdb', '--batch', '-nx')
6773
if (gdb_major_version, gdb_minor_version) >= (7, 4):
6874
base_cmd += ('-iex', 'add-auto-load-safe-path ' + checkout_hook_path)
69-
out, err = subprocess.Popen(base_cmd + args,
70-
stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env,
71-
).communicate()
75+
proc = subprocess.Popen(base_cmd + args,
76+
stdout=subprocess.PIPE,
77+
stderr=subprocess.PIPE,
78+
env=env)
79+
with proc:
80+
out, err = proc.communicate()
7281
return out.decode('utf-8', 'replace'), err.decode('utf-8', 'replace')
7382

7483
# Verify that "gdb" was built with the embedded python support enabled:
@@ -886,7 +895,7 @@ def test_locals_after_up(self):
886895
def test_main():
887896
if support.verbose:
888897
print("GDB version %s.%s:" % (gdb_major_version, gdb_minor_version))
889-
for line in os.fsdecode(gdb_version).splitlines():
898+
for line in gdb_version.splitlines():
890899
print(" " * 4 + line)
891900
run_unittest(PrettyPrintTests,
892901
PyListTests,

0 commit comments

Comments
 (0)