From 3555323c72bb9275fb64f312f707cdc3102cd877 Mon Sep 17 00:00:00 2001 From: Charalampos Stratakis Date: Sat, 27 Jun 2026 00:51:39 +0200 Subject: [PATCH 1/2] gh-98894: Check readelf failures in test_dtrace Report readelf command failures directly instead of later failing with missing-probe assertions. --- Lib/test/test_dtrace.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_dtrace.py b/Lib/test/test_dtrace.py index 2cfd94239aee24e..50f8bf77d4001fd 100644 --- a/Lib/test/test_dtrace.py +++ b/Lib/test/test_dtrace.py @@ -423,7 +423,7 @@ def get_readelf_version(): version, stderr = proc.communicate() if proc.returncode: - raise Exception( + raise AssertionError( f"Command {' '.join(cmd)!r} failed " f"with exit code {proc.returncode}: " f"stdout={version!r} stderr={stderr!r}" @@ -464,13 +464,21 @@ def get_readelf_output(self): command = ["readelf", "-n", binary] # Force the C locale to disable localization. env = dict(os.environ, LC_ALL="C") - stdout, _ = subprocess.Popen( + proc = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, env=env, - ).communicate() + ) + with proc: + stdout, _ = proc.communicate() + + if proc.returncode: + raise AssertionError( + f"Command {' '.join(command)!r} failed " + f"with exit code {proc.returncode}: stdout={stdout!r}" + ) return stdout def test_check_probes(self): From 3372f7cdebf8961d79cfd9433438b334220978a4 Mon Sep 17 00:00:00 2001 From: Charalampos Stratakis Date: Thu, 2 Jul 2026 15:03:05 +0200 Subject: [PATCH 2/2] Create a helper for readelf checking shared code --- Lib/test/test_dtrace.py | 70 +++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/Lib/test/test_dtrace.py b/Lib/test/test_dtrace.py index 50f8bf77d4001fd..788d31c60f4d978 100644 --- a/Lib/test/test_dtrace.py +++ b/Lib/test/test_dtrace.py @@ -73,6 +73,33 @@ def kill_process_group(proc): proc.communicate() # Clean up +def run_readelf(cmd): + # Force the C locale to disable localization. + env = dict(os.environ, LC_ALL="C") + try: + proc = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + env=env, + ) + except OSError: + raise unittest.SkipTest("Couldn't find readelf on the path") + + with proc: + stdout, stderr = proc.communicate() + + if proc.returncode: + raise AssertionError( + f"Command {' '.join(cmd)!r} failed " + f"with exit code {proc.returncode}: " + f"stdout={stdout!r} stderr={stderr!r}" + ) + + return stdout + + class TraceBackend: EXTENSION = None COMMAND = None @@ -408,28 +435,7 @@ def setUpClass(cls): @staticmethod def get_readelf_version(): - try: - cmd = ["readelf", "--version"] - # Force the C locale to disable localization. - env = dict(os.environ, LC_ALL="C") - proc = subprocess.Popen( - cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True, - env=env, - ) - with proc: - version, stderr = proc.communicate() - - if proc.returncode: - raise AssertionError( - f"Command {' '.join(cmd)!r} failed " - f"with exit code {proc.returncode}: " - f"stdout={version!r} stderr={stderr!r}" - ) - except OSError: - raise unittest.SkipTest("Couldn't find readelf on the path") + version = run_readelf(["readelf", "--version"]) # Regex to parse: # 'GNU readelf (GNU Binutils) 2.40.0\n' -> 2.40 @@ -461,25 +467,7 @@ def get_readelf_output(self): binary = libpython_path break - command = ["readelf", "-n", binary] - # Force the C locale to disable localization. - env = dict(os.environ, LC_ALL="C") - proc = subprocess.Popen( - command, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - universal_newlines=True, - env=env, - ) - with proc: - stdout, _ = proc.communicate() - - if proc.returncode: - raise AssertionError( - f"Command {' '.join(command)!r} failed " - f"with exit code {proc.returncode}: stdout={stdout!r}" - ) - return stdout + return run_readelf(["readelf", "-n", binary]) def test_check_probes(self): readelf_output = self.get_readelf_output()