Skip to content

Commit 9d87605

Browse files
committed
Truncate external tracer error output
1 parent 90f8699 commit 9d87605

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
@@ -51,6 +51,17 @@ def get_probe_binary():
5151
return binary
5252

5353

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

127139
return stdout
@@ -167,7 +179,8 @@ def trace(self, script_file, subcommand=None, *, timeout=None,
167179
if check_returncode and proc.returncode:
168180
raise AssertionError(
169181
f"Command {shlex.join(command)!r} failed "
170-
f"with exit code {proc.returncode}: output={stdout!r}"
182+
f"with exit code {proc.returncode}: "
183+
f"output={truncate_output(stdout)!r}"
171184
)
172185
return stdout
173186

@@ -192,7 +205,9 @@ def assert_usable(self):
192205
output = str(fnfe)
193206
if output != "probe: success":
194207
raise unittest.SkipTest(
195-
"{}(1) failed: {}".format(self.COMMAND[0], output)
208+
"{}(1) failed: {}".format(
209+
self.COMMAND[0], truncate_output(output)
210+
)
196211
)
197212

198213

@@ -354,7 +369,8 @@ def run_case(self, name, optimize_python=None):
354369

355370
if proc.returncode != 0:
356371
raise AssertionError(
357-
f"bpftrace failed with code {proc.returncode}:\n{stderr}"
372+
f"bpftrace failed with code {proc.returncode}: "
373+
f"{truncate_output(stderr)}"
358374
)
359375

360376
stdout = self._filter_probe_rows(stdout)
@@ -394,12 +410,15 @@ def assert_usable(self):
394410
# Check for permission errors (bpftrace usually requires root)
395411
if proc.returncode != 0:
396412
raise unittest.SkipTest(
397-
f"bpftrace(1) failed with code {proc.returncode}: {stderr}"
413+
f"bpftrace(1) failed with code {proc.returncode}: "
414+
f"{truncate_output(stderr)}"
398415
)
399416

400417
if "probe: success" not in stdout:
401418
raise unittest.SkipTest(
402-
f"bpftrace(1) failed: stdout={stdout!r} stderr={stderr!r}"
419+
f"bpftrace(1) failed: "
420+
f"stdout={truncate_output(stdout)!r} "
421+
f"stderr={truncate_output(stderr)!r}"
403422
)
404423

405424

0 commit comments

Comments
 (0)