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
Provenance
- Commit
f6f8976- Tree
5868e36- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
0d01686
2 files changed+33 added−29 removed
modified:src/click/testing.py+27 added-20 removed
14 unmodified lines15 class EchoingStdin:16 def __init__(self, input, output):17 self._input = input18 self._output = output19- self.paused = False19+ self._paused = False20 21 def __getattr__(self, x):22 return getattr(self._input, x)23 24 def _echo(self, rv):25- if not self.paused:25+ if not self._paused:26 self._output.write(rv)27+28 return rv29 30 def read(self, n=-1):31 return self._echo(self._input.read(n))13 unmodified lines45 def __repr__(self):46 return repr(self._input)47 48 49+@contextlib.contextmanager50+def _pause_echo(stream):51+ if stream is None:52+ yield53+ else:54+ stream._paused = True55+ yield56+ stream._paused = False57+58+59 class _NamedTextIOWrapper(io.TextIOWrapper):60 def __init__(self, buffer, name=None, mode=None, **kwargs):61 super().__init__(buffer, **kwargs)62 self._name = name143 unmodified lines206 .. versionchanged:: 4.0207 Added the ``color`` parameter.208 """209 input = make_input_stream(input, self.charset)210+ echo_input = None211 212 old_stdin = sys.stdin213 old_stdout = sys.stdout214 old_stderr = sys.stderr4 unmodified lines219 220 bytes_output = io.BytesIO()221 222 if self.echo_stdin:211- input = EchoingStdin(input, bytes_output)212- echo_input = input223+ input = echo_input = EchoingStdin(input, bytes_output)224 225 sys.stdin = input = _NamedTextIOWrapper(226 input, encoding=self.charset, name="<stdin>", mode="r"227 )228+229 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 = 1233 234 sys.stdout = _NamedTextIOWrapper(235 bytes_output, encoding=self.charset, name="<stdout>", mode="w"11 unmodified lines247 mode="w",248 errors="backslashreplace",249 )250 251+ @_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 val258 259+ @_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 val264 265+ @_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:268+269+ if echo:270 sys.stdout.write(char)263- sys.stdout.flush()264- elif not echo and self.echo_stdin:265- echo_input.paused = False271+272+ sys.stdout.flush()273 return char274 275 default_color = color276 170 unmodified linesmodified:tests/test_testing.py+6 added-9 removed
50 unmodified lines51 foo = input("Foo: ")52 click.echo(f"foo={foo}")53 54 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"58 59 @click.command()60 @click.option("--foo", prompt=True)61 def test_prompt(foo):62 click.echo(f"foo={foo}")63 64- 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"67 68 @click.command()69 @click.option("--foo", prompt=True, hide_input=True)70 def test_hidden_prompt(foo):71 click.echo(f"foo={foo}")72 74- 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"76 77 @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}")82 85- 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