Skip to content

Commit

Permalink
Prevent TextIOWrapper from buffering echo stream
Browse files Browse the repository at this point in the history
TextIOWrapper does buffered reads, so the first call to readline (or
read) will read a large chunk from the underlying stream. When that
stream is the EchoingStdin stream, it causes multiple lines of input
to be echoed prematurely.

The solution in this commit is not ideal because it relies on the
undocumented internal _CHUNK_SIZE variable. I couldn't find any other
way to disable read buffering without reimplementing TextIOWrapper.
  • Loading branch information
saebischer committed Mar 23, 2021
1 parent ad31c92 commit f50e8fc
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/click/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ def isolation(self, input=None, env=None, color=False):
sys.stdin = input = _NamedTextIOWrapper(
input, encoding=self.charset, name="<stdin>", mode="r"
)
if self.echo_stdin:
# Force unbuffered reads, otherwise the underlying EchoingStdin
# stream will echo a big chunk of input on the first read.
input._CHUNK_SIZE = 1

sys.stdout = _NamedTextIOWrapper(
bytes_output, encoding=self.charset, name="<stdout>", mode="w"
)
Expand Down

0 comments on commit f50e8fc

Please sign in to comment.