From 83d3800d0075f013110b4bd5e89cfda667784516 Mon Sep 17 00:00:00 2001 From: GreedyC <62262441+GreedyC@users.noreply.github.com> Date: Thu, 22 Oct 2026 11:20:06 +0300 Subject: [PATCH 1/2] fix: handle broken pipes during the final stdout flush --- termgraph/termgraph.py | 10 +++++++++- tests/test_init.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/termgraph/termgraph.py b/termgraph/termgraph.py index c8898f5..192f418 100644 --- a/termgraph/termgraph.py +++ b/termgraph/termgraph.py @@ -112,8 +112,16 @@ def main(): calendar_heatmap(data_obj.data, data_obj.labels, args) else: chart(data_obj, args, colors) + # Buffered output can fail only at shutdown unless it is flushed here. + sys.stdout.flush() except BrokenPipeError: - pass + # Avoid another failed flush during interpreter shutdown. + devnull = os.open(os.devnull, os.O_WRONLY) + try: + os.dup2(devnull, sys.stdout.fileno()) + finally: + os.close(devnull) + raise SystemExit(1) from None def chart(data_obj: Data, args: dict, colors: list) -> None: diff --git a/tests/test_init.py b/tests/test_init.py index 9e5b125..c051e95 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -1,5 +1,34 @@ +import pytest + from termgraph import termgraph as tg def test_init_args(): - tg.init_args() \ No newline at end of file + tg.init_args() + +@pytest.mark.parametrize("rows,width", [(1, 10), (300, 1000)]) +def test_cli_closed_stdout_pipe(tmp_path, rows, width): + """Exercise interpreter shutdown too, not only errors raised inside draw.""" + import os + import subprocess + import sys + + # One small row fails at the final flush; many wide rows fail while drawing. + data_file = tmp_path / f"pipe-{rows}.dat" + data_file.write_text("item 1\n" * rows) + read_fd, write_fd = os.pipe() + os.close(read_fd) + try: + result = subprocess.run( + [sys.executable, "-m", "termgraph.termgraph", str(data_file), + "--width", str(width)], + check=False, + stdout=write_fd, + stderr=subprocess.PIPE, + text=True, + timeout=15, + ) + finally: + os.close(write_fd) + assert result.returncode == 1 + assert result.stderr == "" From 189430393a1ee664b2ebd05a184f1db08e17b8ee Mon Sep 17 00:00:00 2001 From: GreedyC <62262441+GreedyC@users.noreply.github.com> Date: Thu, 22 Oct 2026 11:35:09 +0300 Subject: [PATCH 2/2] fix: handle closed pipes during CLI early exits --- termgraph/termgraph.py | 38 ++++++++++++++++++++++---------------- tests/test_init.py | 16 ++++++++++++---- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/termgraph/termgraph.py b/termgraph/termgraph.py index 192f418..d7b0bae 100644 --- a/termgraph/termgraph.py +++ b/termgraph/termgraph.py @@ -97,23 +97,12 @@ def init_args() -> dict: def main(): """Main function.""" - args = init_args() - - if args["version"]: - print(f"termgraph v{__version__}") - sys.exit() - - data_obj = Data.from_file(args["filename"], args) - colors = _extract_colors(data_obj, args) - try: - if args["calendar"]: - # calendar_heatmap still uses old interface - calendar_heatmap(data_obj.data, data_obj.labels, args) - else: - chart(data_obj, args, colors) - # Buffered output can fail only at shutdown unless it is flushed here. - sys.stdout.flush() + try: + _run() + finally: + # Flush even when argument parsing exits for --help or --version. + sys.stdout.flush() except BrokenPipeError: # Avoid another failed flush during interpreter shutdown. devnull = os.open(os.devnull, os.O_WRONLY) @@ -124,6 +113,23 @@ def main(): raise SystemExit(1) from None +def _run(): + args = init_args() + + if args["version"]: + print(f"termgraph v{__version__}") + sys.exit() + + data_obj = Data.from_file(args["filename"], args) + colors = _extract_colors(data_obj, args) + + if args["calendar"]: + # calendar_heatmap still uses old interface + calendar_heatmap(data_obj.data, data_obj.labels, args) + else: + chart(data_obj, args, colors) + + def chart(data_obj: Data, args: dict, colors: list) -> None: """Handle the normalization of data and the printing of the graph.""" # Convert CLI args dict to chart Args class, mapping incompatible keys diff --git a/tests/test_init.py b/tests/test_init.py index c051e95..5edaa28 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -6,8 +6,17 @@ def test_init_args(): tg.init_args() -@pytest.mark.parametrize("rows,width", [(1, 10), (300, 1000)]) -def test_cli_closed_stdout_pipe(tmp_path, rows, width): + +@pytest.mark.parametrize( + "rows,options", + [ + (1, ["--width", "10"]), + (300, ["--width", "1000"]), + (1, ["--version"]), + (1, ["--help"]), + ], +) +def test_cli_closed_stdout_pipe(tmp_path, rows, options): """Exercise interpreter shutdown too, not only errors raised inside draw.""" import os import subprocess @@ -20,8 +29,7 @@ def test_cli_closed_stdout_pipe(tmp_path, rows, width): os.close(read_fd) try: result = subprocess.run( - [sys.executable, "-m", "termgraph.termgraph", str(data_file), - "--width", str(width)], + [sys.executable, "-m", "termgraph.termgraph", str(data_file), *options], check=False, stdout=write_fd, stderr=subprocess.PIPE,