Skip to content
Myr

use decorator to pause echo

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

pause echo_stdin unconditionally, allowing functions to echo as normal this seems to work better with the readline "echo empty string" fix

authored by David Lord · committed by David Lord · parent 0d01686 · Browse files · 2 files+33 added−29 removed
This commit

State

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

Diff stats

2 files+33 added−29 removed

  • tests:+6 added−9 removed
  • other:+27 added−20 removed

2 files+33 added−29 removed

modified:src/click/testing.py+27 added-20 removed
14 unmodified lines
15class EchoingStdin:16 def __init__(self, input, output):17 self._input = input18 self._output = output19 self.paused = False19 self._paused = False2021 def __getattr__(self, x):22 return getattr(self._input, x)2324 def _echo(self, rv):25 if not self.paused:25 if not self._paused:26 self._output.write(rv)2728 return rv2930 def read(self, n=-1):31 return self._echo(self._input.read(n))
13 unmodified lines
45 def __repr__(self):46 return repr(self._input)474849@contextlib.contextmanager50def _pause_echo(stream):51 if stream is None:52 yield53 else:54 stream._paused = True55 yield56 stream._paused = False575859class _NamedTextIOWrapper(io.TextIOWrapper):60 def __init__(self, buffer, name=None, mode=None, **kwargs):61 super().__init__(buffer, **kwargs)62 self._name = name
143 unmodified lines
206 .. versionchanged:: 4.0207 Added the ``color`` parameter.208 """209 input = make_input_stream(input, self.charset)210 echo_input = None211212 old_stdin = sys.stdin213 old_stdout = sys.stdout214 old_stderr = sys.stderr
4 unmodified lines
219220 bytes_output = io.BytesIO()221222 if self.echo_stdin:211 input = EchoingStdin(input, bytes_output)212 echo_input = input223 input = echo_input = EchoingStdin(input, bytes_output)224225 sys.stdin = input = _NamedTextIOWrapper(226 input, encoding=self.charset, name="<stdin>", mode="r"227 )228229 if self.echo_stdin:218 # Force unbuffered reads, otherwise the underlying EchoingStdin219 # stream will echo a big chunk of input on the first read.230 # Force unbuffered reads, otherwise TextIOWrapper reads a231 # large chunk which is echoed early.232 input._CHUNK_SIZE = 1233234 sys.stdout = _NamedTextIOWrapper(235 bytes_output, encoding=self.charset, name="<stdout>", mode="w"
11 unmodified lines
247 mode="w",248 errors="backslashreplace",249 )250251 @_pause_echo(echo_input)252 def visible_input(prompt=None):253 sys.stdout.write(prompt or "")254 val = input.readline().rstrip("\r\n")242 if not self.echo_stdin:243 sys.stdout.write(f"{val}\n")255 sys.stdout.write(f"{val}\n")256 sys.stdout.flush()257 return val258259 @_pause_echo(echo_input)260 def hidden_input(prompt=None):261 sys.stdout.write(f"{prompt or ''}\n")262 sys.stdout.flush()250 if self.echo_stdin:251 echo_input.paused = True252 val = input.readline().rstrip("\r\n")263 return input.readline().rstrip("\r\n")253 if self.echo_stdin:254 echo_input.paused = False255 return val264265 @_pause_echo(echo_input)266 def _getchar(echo):258 if not echo and self.echo_stdin:259 echo_input.paused = True267 char = sys.stdin.read(1)261 if echo and not self.echo_stdin:268269 if echo:270 sys.stdout.write(char)263 sys.stdout.flush()264 elif not echo and self.echo_stdin:265 echo_input.paused = False271272 sys.stdout.flush()273 return char274275 default_color = color276
170 unmodified lines
modified:tests/test_testing.py+6 added-9 removed
50 unmodified lines
51 foo = input("Foo: ")52 click.echo(f"foo={foo}")5354 runner = CliRunner(echo_stdin=True)55 result = runner.invoke(test_python_input, input="wau wau\n")55 result = runner.invoke(test_python_input, input="bar bar\n")56 assert not result.exception57 assert result.output == "Foo: wau wau\nfoo=wau wau\n"57 assert result.output == "Foo: bar bar\nfoo=bar bar\n"5859 @click.command()60 @click.option("--foo", prompt=True)61 def test_prompt(foo):62 click.echo(f"foo={foo}")6364 runner = CliRunner(echo_stdin=True)65 result = runner.invoke(test_prompt, input="wau wau\n")64 result = runner.invoke(test_prompt, input="bar bar\n")65 assert not result.exception67 assert result.output == "Foo: wau wau\nfoo=wau wau\n"66 assert result.output == "Foo: bar bar\nfoo=bar bar\n"6768 @click.command()69 @click.option("--foo", prompt=True, hide_input=True)70 def test_hidden_prompt(foo):71 click.echo(f"foo={foo}")7274 runner = CliRunner(echo_stdin=True)75 result = runner.invoke(test_hidden_prompt, input="wau wau\n")73 result = runner.invoke(test_hidden_prompt, input="bar bar\n")74 assert not result.exception77 assert result.output == "Foo: \nfoo=wau wau\n"75 assert result.output == "Foo: \nfoo=bar bar\n"7677 @click.command()78 @click.option("--foo", prompt=True)79 @click.option("--bar", prompt=True)80 def test_multiple_prompts(foo, bar):81 click.echo(f"foo={foo}, bar={bar}")8285 runner = CliRunner(echo_stdin=True)83 result = runner.invoke(test_multiple_prompts, input="one\ntwo\n")84 assert not result.exception85 assert result.output == "Foo: one\nBar: two\nfoo=one, bar=two\n"86
334 unmodified lines