echo doesn't fail with no streams
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 9a536ee · 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
Provenance
- Commit
ecb9983- Tree
dc64d1c- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
9a536ee
5 files changed+38 added−3 removed
modified:src/click/_compat.py+8 added-3 removed
574 unmodified lines575 return False576 577 578 def _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()583 583- def func() -> t.TextIO:584+ def func() -> t.Optional[t.TextIO]:585 stream = src_func()586+587+ if stream is None:588+ return None589+590 try:591 rv = cache.get(stream)592 except Exception:593 rv = None30 unmodified linesmodified:src/click/_termui_impl.py+14 added
8 unmodified lines9 import sys10 import time11 import typing as t12 from gettext import gettext as _13+from io import StringIO14 from types import TracebackType15 16 from ._compat import _default_text_stdout17 from ._compat import CYGWIN43 unmodified lines61 self.show_percent = show_percent62 self.show_pos = show_pos63 self.item_show_func = item_show_func64 self.label: str = label or ""65+66 if file is None:67 file = _default_text_stdout()68+69+ # There are no standard streams attached to write to. For example,70+ # pythonw on Windows.71+ if file is None:72+ file = StringIO()73+74 self.file = file75 self.color = color76 self.update_min_steps = update_min_steps77 self._completed_intervals = 0281 unmodified lines359 360 def 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()363+364+ # There are no standard streams attached to write to. For example,365+ # pythonw on Windows.366+ if stdout is None:367+ stdout = StringIO()368+369 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 linesmodified:src/click/utils.py+5 added
265 unmodified lines266 file = _default_text_stderr()267 else:268 file = _default_text_stdout()269 270+ # There are no standard streams attached to write to. For example,271+ # pythonw on Windows.272+ if file is None:273+ return274+275 # 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 linesmodified:tests/test_utils.py+9 added
41 unmodified lines42 click.echo("hello", file=f)43 assert f.getvalue() == "hello\n"44 45 46+def 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)53+54+55 @pytest.mark.parametrize(56 ("styles", "ref"),57 [58 ({"fg": "black"}, "\x1b[30mx y\x1b[0m"),449 unmodified linesmodified:CHANGES.rst+2 added
28 unmodified lines29 - 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`35 36 37 Version 8.1.338 -------------1040 unmodified lines