Skip to content
Myr

fix I/O operation on closed file error with CliRunner and echo_via_pager (#3482)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by GitHub · parents a5f5aa6, becbde5 · Browse files · 4 files+38 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+38 added−12 removed

  • tests:+15 added−10 removed
  • other:+23 added−2 removed

4 files+38 added−12 removed

modified:src/click/_termui_impl.py+22 added-2 removed
604 unmodified lines
605 finally:606 os.unlink(f.name)607608609class _SkipClose:610 def __init__(self, stream: t.IO[t.Any]) -> None:611 self.stream = stream612613 def __getattr__(self, name: str) -> t.Any:614 return getattr(self.stream, name)615616 @property617 def buffer(self) -> t.BinaryIO:618 return _SkipClose(self.stream.buffer) # type: ignore[attr-defined, return-value]619620 def close(self) -> None:621 pass622623624@contextlib.contextmanager625def _nullpager(626 stream: t.TextIO, color: bool | None = None627) -> t.Iterator[tuple[t.TextIO, str, bool]]:613 """Simply print unformatted text. This is the ultimate fallback."""628 """Simply print unformatted text. This is the ultimate fallback. Don't close the629 output stream in this case, since it's coming from elsewhere rather than our630 internal helpers.631 """632 encoding = get_best_encoding(stream)633634 if color is None:635 color = False617 yield stream, encoding, color636637 yield _SkipClose(stream), encoding, color # type: ignore[misc]638639640class Editor:641 def __init__(642 self,643 editor: str | None = None,
286 unmodified lines
modified:tests/test_termui.py+4 added-10 removed
796 unmodified lines
797798def test_get_pager_file_nullpager_keeps_stringio_stream(monkeypatch):799 """The no-stdout fallback should keep a text-only stream and set ``.color``."""800801 created = []802803 def make_stringio():804 stream = io.StringIO()801 stream = io.StringIO()805 created.append(stream)806 return stream807802 monkeypatch.setattr(sys, "stdout", None)809 monkeypatch.setattr(click._termui_impl, "StringIO", make_stringio)803 monkeypatch.setattr(click._termui_impl, "StringIO", lambda: stream)804 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: False)805806 styled_text = click.style("hello", fg="red")807808 with click.get_pager_file(color=False) as pager:815 assert pager is created[0]809 pager.write(styled_text)810811 assert not stream.closed818 assert created[0].getvalue() == styled_text812 assert stream.getvalue() == styled_text813814815def test_get_pager_file_flushes_stream_on_exception(monkeypatch):816 """Exceptions should still flush the yielded stream in ``finally``."""
652 unmodified lines
modified:tests/test_testing.py+11 added
254 unmodified lines
255 assert not result.exception256 assert result.output == ""257258259def test_with_echo_via_pager():260 @click.command()261 def cli():262 click.echo_via_pager("Hello, Click!")263264 runner = CliRunner()265 result = runner.invoke(cli)266 assert not result.exception267 assert result.output == "Hello, Click!\n"268269270def test_exit_code_and_output_from_sys_exit():271 # See issue #362272 @click.command()273 def cli_string():
487 unmodified lines
modified:CHANGES.rst+1 added
9 unmodified lines
10- Zsh completion scripts parse correctly on Windows. :issue:`3277`11- Shell completion of `Choice` `Enum` values produces a valid completion12 result. :issue:`3015`13- Fix empty byte-string handling in echo. :issue:`3487`14- Fix closed file error with `echo_via_pager`. :issue:`3449`151617Version 8.4.018-------------
1463 unmodified lines