Skip to content

Commit d60a677

Browse files
authored
gh-98894: Check readelf failures in test_dtrace (#152345)
Report readelf command failures directly instead of later failing with missing-probe assertions.
1 parent 4e4869b commit d60a677

1 file changed

Lines changed: 29 additions & 33 deletions

File tree

Lib/test/test_dtrace.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,33 @@ def kill_process_group(proc):
7373
proc.communicate() # Clean up
7474

7575

76+
def run_readelf(cmd):
77+
# Force the C locale to disable localization.
78+
env = dict(os.environ, LC_ALL="C")
79+
try:
80+
proc = subprocess.Popen(
81+
cmd,
82+
stdout=subprocess.PIPE,
83+
stderr=subprocess.PIPE,
84+
text=True,
85+
env=env,
86+
)
87+
except OSError:
88+
raise unittest.SkipTest("Couldn't find readelf on the path")
89+
90+
with proc:
91+
stdout, stderr = proc.communicate()
92+
93+
if proc.returncode:
94+
raise AssertionError(
95+
f"Command {' '.join(cmd)!r} failed "
96+
f"with exit code {proc.returncode}: "
97+
f"stdout={stdout!r} stderr={stderr!r}"
98+
)
99+
100+
return stdout
101+
102+
76103
class TraceBackend:
77104
EXTENSION = None
78105
COMMAND = None
@@ -408,28 +435,7 @@ def setUpClass(cls):
408435

409436
@staticmethod
410437
def get_readelf_version():
411-
try:
412-
cmd = ["readelf", "--version"]
413-
# Force the C locale to disable localization.
414-
env = dict(os.environ, LC_ALL="C")
415-
proc = subprocess.Popen(
416-
cmd,
417-
stdout=subprocess.PIPE,
418-
stderr=subprocess.PIPE,
419-
universal_newlines=True,
420-
env=env,
421-
)
422-
with proc:
423-
version, stderr = proc.communicate()
424-
425-
if proc.returncode:
426-
raise Exception(
427-
f"Command {' '.join(cmd)!r} failed "
428-
f"with exit code {proc.returncode}: "
429-
f"stdout={version!r} stderr={stderr!r}"
430-
)
431-
except OSError:
432-
raise unittest.SkipTest("Couldn't find readelf on the path")
438+
version = run_readelf(["readelf", "--version"])
433439

434440
# Regex to parse:
435441
# 'GNU readelf (GNU Binutils) 2.40.0\n' -> 2.40
@@ -461,17 +467,7 @@ def get_readelf_output(self):
461467
binary = libpython_path
462468
break
463469

464-
command = ["readelf", "-n", binary]
465-
# Force the C locale to disable localization.
466-
env = dict(os.environ, LC_ALL="C")
467-
stdout, _ = subprocess.Popen(
468-
command,
469-
stdout=subprocess.PIPE,
470-
stderr=subprocess.STDOUT,
471-
universal_newlines=True,
472-
env=env,
473-
).communicate()
474-
return stdout
470+
return run_readelf(["readelf", "-n", binary])
475471

476472
def test_check_probes(self):
477473
readelf_output = self.get_readelf_output()

0 commit comments

Comments
 (0)