Fixed underflow issue with truncating test/bench -C/--context

There was no check on context > stdout, so requesting more context than
was actually printed by the test could result in a negative value.
Python "helpfully" interpreted this as a negative index, resulting in
somewhat random context lengths.

This, combined with my tendency to just default to a large number like
--context=100, led to me thinking a test was printing much less than it
actually was...

Don't get me wrong, I love Python, and I think Python's negative indices
are a clever way to add flexibility to slice notation, but the
value-dependent semantics are a pretty unfortunate footgun...
This commit is contained in:
Christopher Haster
2024-04-03 19:59:45 -05:00
parent 1ce47bfc47
commit c3dc7cca10
2 changed files with 2 additions and 2 deletions

View File

@ -1306,7 +1306,7 @@ def run(runner, test_ids=[], **args):
stdout = failure.stdout
if failure.assert_ is not None:
stdout = stdout[:-1]
for line in stdout[len(stdout)-args.get('context', 5):]:
for line in stdout[max(len(stdout)-args.get('context', 5), 0):]:
sys.stdout.write(line)
if failure.assert_ is not None: