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
Provenance
- Commit
5ee8e31- Tree
91b8d0a- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
a5f5aa6- Parent 2
becbde5
4 files changed+38 added−12 removed
modified:src/click/_termui_impl.py+22 added-2 removed
604 unmodified lines605 finally:606 os.unlink(f.name)607 608 609+class _SkipClose:610+ def __init__(self, stream: t.IO[t.Any]) -> None:611+ self.stream = stream612+613+ def __getattr__(self, name: str) -> t.Any:614+ return getattr(self.stream, name)615+616+ @property617+ def buffer(self) -> t.BinaryIO:618+ return _SkipClose(self.stream.buffer) # type: ignore[attr-defined, return-value]619+620+ def close(self) -> None:621+ pass622+623+624 @contextlib.contextmanager625 def _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)633+634 if color is None:635 color = False617- yield stream, encoding, color636 637+ yield _SkipClose(stream), encoding, color # type: ignore[misc]638 639+640 class Editor:641 def __init__(642 self,643 editor: str | None = None,286 unmodified linesmodified:tests/test_termui.py+4 added-10 removed
796 unmodified lines797 798 def test_get_pager_file_nullpager_keeps_stringio_stream(monkeypatch):799 """The no-stdout fallback should keep a text-only stream and set ``.color``."""800 801- created = []802-803- def make_stringio():804- stream = io.StringIO()801+ stream = io.StringIO()805- created.append(stream)806- return stream807-802 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)805 806 styled_text = click.style("hello", fg="red")807 808 with click.get_pager_file(color=False) as pager:815- assert pager is created[0]809 pager.write(styled_text)810 811+ assert not stream.closed818- assert created[0].getvalue() == styled_text812+ assert stream.getvalue() == styled_text813 814 815 def test_get_pager_file_flushes_stream_on_exception(monkeypatch):816 """Exceptions should still flush the yielded stream in ``finally``."""652 unmodified linesmodified:tests/test_testing.py+11 added
254 unmodified lines255 assert not result.exception256 assert result.output == ""257 258 259+def test_with_echo_via_pager():260+ @click.command()261+ def cli():262+ click.echo_via_pager("Hello, Click!")263+264+ runner = CliRunner()265+ result = runner.invoke(cli)266+ assert not result.exception267+ assert result.output == "Hello, Click!\n"268+269+270 def test_exit_code_and_output_from_sys_exit():271 # See issue #362272 @click.command()273 def cli_string():487 unmodified linesmodified:CHANGES.rst+1 added
9 unmodified lines10 - 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`15 16 17 Version 8.4.018 -------------1463 unmodified lines