Open the pager temp file in text mode
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kevin Deldycke · committed by Kevin Deldycke · parent 9c4dfda · Browse files · 3 files·+77 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: H-Sorkatti <H.Sorkatti.B@Gmail.com>
Diff stats
3 files+77 added−1 removed
- tests:+71 added−0 removed
- other:+6 added−1 removed
Provenance
- Commit
a2ac583- Tree
a52cd09- Author
Kevin Deldycke <kevin@deldycke.com>- Committer
Kevin Deldycke <kevin@deldycke.com>- Parent
9c4dfda
3 files changed+77 added−1 removed
modified:src/click/_termui_impl.py+1 added-1 removed
621 unmodified lines622 color = False623 # On Windows, NamedTemporaryFile cannot be opened by another process624 # while Python still has it open, so we use delete=False and clean up manually625 # rather than using a contextmanager here.626- f = tempfile.NamedTemporaryFile(mode="wb", delete=False)626+ f = tempfile.NamedTemporaryFile(mode="w", delete=False)627 try:628 yield t.cast(t.BinaryIO, f), encoding, color629 f.flush()630 f.close()315 unmodified linesmodified:tests/test_termui.py+71 added
934 unmodified lines935 936 assert stream.flush_calls == 1937 938 939+def _force_tempfile_pager(monkeypatch, pager_cmd="cat"):940+ """Route ``get_pager_file`` through the ``_tempfilepager`` backend.941+942+ That backend is only reachable on Windows, so the platform flag and the tty943+ probes are faked to exercise it from any runner.944+ """945+ cmd_path = shutil.which(pager_cmd)946+ assert cmd_path is not None, f"{pager_cmd} not available"947+ monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)948+ monkeypatch.setattr(click._termui_impl, "WIN", True)949+ monkeypatch.setitem(click._termui_impl.os.environ, "PAGER", cmd_path)950+951+952+def _page_with_get_pager_file():953+ with click.get_pager_file() as pager:954+ pager.write("hello\n")955+956+957+def _page_with_echo_via_pager():958+ click.echo_via_pager("hello")959+960+961+@pytest.mark.skipif(shutil.which("cat") is None, reason="cat not available")962+@pytest.mark.parametrize(963+ "page",964+ [965+ pytest.param(_page_with_get_pager_file, id="get_pager_file"),966+ pytest.param(_page_with_echo_via_pager, id="echo_via_pager"),967+ ],968+)969+def test_tempfile_pager_accepts_text(monkeypatch, capfd, page):970+ """The temp file backend must yield a text stream (issues #3731, #3740).971+972+ ``_tempfilepager`` opens its temp file in binary mode and yields the973+ ``_TemporaryFileWrapper`` straight to ``get_pager_file``. That wrapper974+ exposes no ``.buffer``, so the ``_has_binary_buffer`` probe is False and no975+ ``MaybeStripAnsi`` wrapper is installed. The caller then writes ``str`` to a976+ binary handle and gets ``TypeError: a bytes-like object is required``.977+ """978+ _force_tempfile_pager(monkeypatch)979+980+ page()981+982+ out, err = capfd.readouterr()983+ assert err == ""984+ assert out.replace("\r\n", "\n") == "hello\n"985+986+987+@pytest.mark.skipif(shutil.which("cat") is None, reason="cat not available")988+@pytest.mark.parametrize(989+ ("color", "expected"),990+ [991+ pytest.param(False, "hello\n", id="strip ansi"),992+ pytest.param(True, click.style("hello", fg="red") + "\n", id="preserve ansi"),993+ ],994+)995+def test_tempfile_pager_handles_ansi(monkeypatch, capfd, color, expected):996+ """The temp file backend honors ``color`` like the pipe backend does.997+998+ ANSI stripping lives in ``MaybeStripAnsi``, which the binary temp file999+ never gets wrapped in, so ``color`` is silently ignored on this path.1000+ """1001+ _force_tempfile_pager(monkeypatch)1002+1003+ click.echo_via_pager(click.style("hello", fg="red"), color=color)1004+1005+ out, err = capfd.readouterr()1006+ assert err == ""1007+ assert out.replace("\r\n", "\n") == expected1008+1009+1010 def test_editor_unclosed_quote():1011 """An unclosed quote in the editor command raises ValueError."""1012 with pytest.raises(ValueError, match="No closing quotation"):1013 Editor(editor='"unclosed').edit_files(["f.txt"])670 unmodified linesmodified:CHANGES.md+5 added
63 unmodified lines64 input value it accepts (`ParamType[int, str]` for a type converting65 strings to integers), defaulting to `Any`. {pr}`3407`66 - {meth}`Command.get_help_option_names` returns the help option names in the67 order they were declared. {pr}`3728`68+- {func}`get_pager_file` yields a text stream on Windows again. The temporary69+ file backend opened its file in binary mode, so writing a `str` to the pager70+ raised `TypeError: a bytes-like object is required, not 'str'`, and the71+ `color` argument was ignored on that path. Regression introduced in `8.4.0`72+ by {pr}`1572`. {issue}`3731` {issue}`3732` {issue}`3740` {pr}`3739`73 74 ## Version 8.4.275 76 Released 2026-06-241561 unmodified lines