Skip to content
Myr

echo doesn't fail with no streams (#2555)

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 9a536ee, ecb9983 · Browse files · 5 files+38 added−3 removed
This commit

State

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

Diff stats

5 files+38 added−3 removed

  • tests:+9 added−0 removed
  • other:+29 added−3 removed

5 files+38 added−3 removed

modified:src/click/_compat.py+8 added-3 removed
574 unmodified lines
575 return False576577578def _make_cached_stream_func(579 src_func: t.Callable[[], t.TextIO], wrapper_func: t.Callable[[], t.TextIO]580) -> t.Callable[[], t.TextIO]:579 src_func: t.Callable[[], t.Optional[t.TextIO]],580 wrapper_func: t.Callable[[], t.TextIO],581) -> t.Callable[[], t.Optional[t.TextIO]]:582 cache: t.MutableMapping[t.TextIO, t.TextIO] = WeakKeyDictionary()583583 def func() -> t.TextIO:584 def func() -> t.Optional[t.TextIO]:585 stream = src_func()586587 if stream is None:588 return None589590 try:591 rv = cache.get(stream)592 except Exception:593 rv = None
30 unmodified lines
modified:src/click/_termui_impl.py+14 added
8 unmodified lines
9import sys10import time11import typing as t12from gettext import gettext as _13from io import StringIO14from types import TracebackType1516from ._compat import _default_text_stdout17from ._compat import CYGWIN
43 unmodified lines
61 self.show_percent = show_percent62 self.show_pos = show_pos63 self.item_show_func = item_show_func64 self.label: str = label or ""6566 if file is None:67 file = _default_text_stdout()6869 # There are no standard streams attached to write to. For example,70 # pythonw on Windows.71 if file is None:72 file = StringIO()7374 self.file = file75 self.color = color76 self.update_min_steps = update_min_steps77 self._completed_intervals = 0
281 unmodified lines
359360def pager(generator: t.Iterable[str], color: t.Optional[bool] = None) -> None:361 """Decide what method to use for paging through text."""362 stdout = _default_text_stdout()363364 # There are no standard streams attached to write to. For example,365 # pythonw on Windows.366 if stdout is None:367 stdout = StringIO()368369 if not isatty(sys.stdin) or not isatty(stdout):370 return _nullpager(stdout, generator, color)371 pager_cmd = (os.environ.get("PAGER", None) or "").strip()372 if pager_cmd:
367 unmodified lines
modified:src/click/utils.py+5 added
265 unmodified lines
266 file = _default_text_stderr()267 else:268 file = _default_text_stdout()269270 # There are no standard streams attached to write to. For example,271 # pythonw on Windows.272 if file is None:273 return274275 # Convert non bytes/text into the native string type.276 if message is not None and not isinstance(message, (str, bytes, bytearray)):277 out: t.Optional[t.Union[str, bytes]] = str(message)278 else:
346 unmodified lines
modified:tests/test_utils.py+9 added
41 unmodified lines
42 click.echo("hello", file=f)43 assert f.getvalue() == "hello\n"444546def test_echo_no_streams(monkeypatch, runner):47 """echo should not fail when stdout and stderr are None with pythonw on Windows."""48 with runner.isolation():49 sys.stdout = None50 sys.stderr = None51 click.echo("test")52 click.echo("test", err=True)535455@pytest.mark.parametrize(56 ("styles", "ref"),57 [58 ({"fg": "black"}, "\x1b[30mx y\x1b[0m"),
449 unmodified lines
modified:CHANGES.rst+2 added
28 unmodified lines
29- Don't fail when writing filenames to streams with strict errors. Replace invalid30 bytes with the replacement character (``�``). :issue:`2395`31- Remove unnecessary attempt to detect MSYS2 environment. :issue:`2355`32- Remove outdated and unnecessary detection of App Engine environment. :pr:`2554`33- ``echo()`` does not fail when no streams are attached, such as with ``pythonw`` on34 Windows. :issue:`2415`353637Version 8.1.338-------------
1040 unmodified lines