diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 77dac2d28..585e009ae 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -2035,7 +2035,10 @@ def ppaged( """Print output using a pager. A pager is used when the terminal is interactive and may exit immediately if the output - fits on the screen. A pager is not used inside a script (Python or text) or when output is + fits on the screen. On Windows, text with non-ASCII characters is printed directly when + using ``more``, which can silently drop Unicode output. If the output stream rejects a + character, it is shown as a backslash escape. A custom pager can still be used. + A pager is not used inside a script (Python or text) or when output is redirected or piped, and in these cases, output is sent to `poutput`. If the pager program cannot be run, for example because it is not installed, the output @@ -2094,6 +2097,16 @@ def ppaged( if console.legacy_windows: # A legacy Windows console prints ANSI style sequences as text when an external pager writes them. output = su.strip_style(output) + if sys.platform.startswith("win") and pager.casefold() in {"more", "more.com"} and not output.isascii(): + # Windows more can silently drop Unicode text while returning exit code 0. + try: + self.stdout.write(output) + except UnicodeEncodeError: + encoding = getattr(self.stdout, "encoding", None) or "ascii" + self.stdout.write(output.encode(encoding, errors="backslashreplace").decode(encoding)) + self.stdout.flush() + return + # As for a pipe: the pager gets UTF-8, and on Windows the console is UTF-8 while it runs. output_bytes = output.encode("utf-8", "replace") diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index dac6d65eb..03cae2acf 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -4213,11 +4213,31 @@ def test_ppaged_with_pager(outsim_app, monkeypatch, chop) -> None: assert expected_cmd == popen_mock.call_args_list[0].args[0] +@pytest.mark.skipif(sys.platform != "win32", reason="Windows more.exe behavior") +@pytest.mark.parametrize("chop", [False, True]) +def test_ppaged_preserves_unicode_with_windows_more(outsim_app, monkeypatch, chop) -> None: + """Windows more can exit successfully without printing the Unicode text.""" + monkeypatch.setattr(outsim_app, "stdin", mock.MagicMock()) + stdout = mock.MagicMock() + monkeypatch.setattr(outsim_app, "stdout", stdout) + monkeypatch.setattr(outsim_app, "pager", "more") + monkeypatch.setattr(outsim_app, "pager_chop", "more") + popen = mock.MagicMock() + monkeypatch.setattr("subprocess.Popen", popen) + + text = "\u6f22" * 100 + " END" + outsim_app.ppaged(text, chop=chop) + + popen.assert_not_called() + assert text in "".join(call.args[0] for call in stdout.write.call_args_list) + + def test_ppaged_pages_utf8_in_a_utf8_console(outsim_app, monkeypatch) -> None: """As for a pipe: the pager gets UTF-8, and on Windows the console is UTF-8 until the pager has exited. - The pager there is more, which decodes with the console's code page. + A custom pager still receives UTF-8 on Windows. """ + monkeypatch.setattr(outsim_app, "pager", "custom-pager") stdin_mock = mock.MagicMock() stdin_mock.isatty.return_value = True monkeypatch.setattr(outsim_app, "stdin", stdin_mock) @@ -4316,6 +4336,17 @@ def shown() -> str: yield outsim_app, shown +def test_ppaged_windows_more_escapes_only_unencodable_characters(paging_app, monkeypatch) -> None: + app, shown = paging_app + app.stdout.reconfigure(encoding="cp1252", errors="strict") + monkeypatch.setattr(sys, "platform", "win32") + app.pager = "more.com" + + app.ppaged("café 漢🙂 end") + + assert shown() == "café \\u6f22\\U0001f642 end\n" + + @pytest.mark.parametrize("chop", [True, False]) def test_ppaged_shows_the_output_when_the_pager_cannot_be_run(paging_app, capfd, chop) -> None: """A pager which is not installed must not cost the user the output.