Skip to content

Commit 37adf0f

Browse files
committed
Truncate external tracer error output
1 parent 344e1e6 commit 37adf0f

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

Lib/test/test_dtrace.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ def get_probe_binary():
5050
return binary
5151

5252

53+
def truncate_output(output, *, max_lines=3, max_chars=500):
54+
lines = [line.strip() for line in output.splitlines() if line.strip()]
55+
omitted = len(lines) - max_lines
56+
output = "; ".join(lines[:max_lines])
57+
if omitted > 0:
58+
output += f"; ... ({omitted} lines omitted)"
59+
if len(output) > max_chars:
60+
output = output[:max_chars - 3] + "..."
61+
return output
62+
63+
5364
def normalize_trace_output(output):
5465
"""Normalize DTrace output for comparison.
5566
@@ -120,7 +131,8 @@ def run_readelf(cmd):
120131
raise AssertionError(
121132
f"Command {' '.join(cmd)!r} failed "
122133
f"with exit code {proc.returncode}: "
123-
f"stdout={stdout!r} stderr={stderr!r}"
134+
f"stdout={truncate_output(stdout)!r} "
135+
f"stderr={truncate_output(stderr)!r}"
124136
)
125137

126138
return stdout
@@ -166,7 +178,8 @@ def trace(self, script_file, subcommand=None, *, timeout=None,
166178
if check_returncode and proc.returncode:
167179
raise AssertionError(
168180
f"Command {' '.join(command)!r} failed "
169-
f"with exit code {proc.returncode}: output={stdout!r}"
181+
f"with exit code {proc.returncode}: "
182+
f"output={truncate_output(stdout)!r}"
170183
)
171184
return stdout
172185

@@ -191,7 +204,9 @@ def assert_usable(self):
191204
output = str(fnfe)
192205
if output != "probe: success":
193206
raise unittest.SkipTest(
194-
"{}(1) failed: {}".format(self.COMMAND[0], output)
207+
"{}(1) failed: {}".format(
208+
self.COMMAND[0], truncate_output(output)
209+
)
195210
)
196211

197212

@@ -353,7 +368,8 @@ def run_case(self, name, optimize_python=None):
353368

354369
if proc.returncode != 0:
355370
raise AssertionError(
356-
f"bpftrace failed with code {proc.returncode}:\n{stderr}"
371+
f"bpftrace failed with code {proc.returncode}: "
372+
f"{truncate_output(stderr)}"
357373
)
358374

359375
stdout = self._filter_probe_rows(stdout)
@@ -392,12 +408,15 @@ def assert_usable(self):
392408
# Check for permission errors (bpftrace usually requires root)
393409
if proc.returncode != 0:
394410
raise unittest.SkipTest(
395-
f"bpftrace(1) failed with code {proc.returncode}: {stderr}"
411+
f"bpftrace(1) failed with code {proc.returncode}: "
412+
f"{truncate_output(stderr)}"
396413
)
397414

398415
if "probe: success" not in stdout:
399416
raise unittest.SkipTest(
400-
f"bpftrace(1) failed: stdout={stdout!r} stderr={stderr!r}"
417+
f"bpftrace(1) failed: "
418+
f"stdout={truncate_output(stdout)!r} "
419+
f"stderr={truncate_output(stderr)!r}"
401420
)
402421

403422

0 commit comments

Comments
 (0)