Skip to content

Commit

Permalink
echo doesn't fail with no streams (#2555)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Jul 6, 2023
2 parents 9a536ee + ecb9983 commit 5499471
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -30,6 +30,8 @@ Unreleased
bytes with the replacement character (````). :issue:`2395`
- Remove unnecessary attempt to detect MSYS2 environment. :issue:`2355`
- Remove outdated and unnecessary detection of App Engine environment. :pr:`2554`
- ``echo()`` does not fail when no streams are attached, such as with ``pythonw`` on
Windows. :issue:`2415`


Version 8.1.3
Expand Down
11 changes: 8 additions & 3 deletions src/click/_compat.py
Expand Up @@ -576,12 +576,17 @@ def isatty(stream: t.IO[t.Any]) -> bool:


def _make_cached_stream_func(
src_func: t.Callable[[], t.TextIO], wrapper_func: t.Callable[[], t.TextIO]
) -> t.Callable[[], t.TextIO]:
src_func: t.Callable[[], t.Optional[t.TextIO]],
wrapper_func: t.Callable[[], t.TextIO],
) -> t.Callable[[], t.Optional[t.TextIO]]:
cache: t.MutableMapping[t.TextIO, t.TextIO] = WeakKeyDictionary()

def func() -> t.TextIO:
def func() -> t.Optional[t.TextIO]:
stream = src_func()

if stream is None:
return None

try:
rv = cache.get(stream)
except Exception:
Expand Down
14 changes: 14 additions & 0 deletions src/click/_termui_impl.py
Expand Up @@ -10,6 +10,7 @@
import time
import typing as t
from gettext import gettext as _
from io import StringIO
from types import TracebackType

from ._compat import _default_text_stdout
Expand Down Expand Up @@ -61,8 +62,15 @@ def __init__(
self.show_pos = show_pos
self.item_show_func = item_show_func
self.label: str = label or ""

if file is None:
file = _default_text_stdout()

# There are no standard streams attached to write to. For example,
# pythonw on Windows.
if file is None:
file = StringIO()

self.file = file
self.color = color
self.update_min_steps = update_min_steps
Expand Down Expand Up @@ -352,6 +360,12 @@ def generator(self) -> t.Iterator[V]:
def pager(generator: t.Iterable[str], color: t.Optional[bool] = None) -> None:
"""Decide what method to use for paging through text."""
stdout = _default_text_stdout()

# There are no standard streams attached to write to. For example,
# pythonw on Windows.
if stdout is None:
stdout = StringIO()

if not isatty(sys.stdin) or not isatty(stdout):
return _nullpager(stdout, generator, color)
pager_cmd = (os.environ.get("PAGER", None) or "").strip()
Expand Down
5 changes: 5 additions & 0 deletions src/click/utils.py
Expand Up @@ -267,6 +267,11 @@ def echo(
else:
file = _default_text_stdout()

# There are no standard streams attached to write to. For example,
# pythonw on Windows.
if file is None:
return

# Convert non bytes/text into the native string type.
if message is not None and not isinstance(message, (str, bytes, bytearray)):
out: t.Optional[t.Union[str, bytes]] = str(message)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_utils.py
Expand Up @@ -43,6 +43,15 @@ def test_echo_custom_file():
assert f.getvalue() == "hello\n"


def test_echo_no_streams(monkeypatch, runner):
"""echo should not fail when stdout and stderr are None with pythonw on Windows."""
with runner.isolation():
sys.stdout = None
sys.stderr = None
click.echo("test")
click.echo("test", err=True)


@pytest.mark.parametrize(
("styles", "ref"),
[
Expand Down

0 comments on commit 5499471

Please sign in to comment.