Skip to content
Myr

fix a race in `click.testing.StreamMixer`'s finalization, seen in a multi-threaded context (#2991)

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

authored by Rowlando13 · committed by GitHub · parents 7b72bfb, 4679b1c · Browse files · 2 files+14 added−4 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+14 added−4 removed

  • tests:+2 added−4 removed
  • other:+12 added−0 removed

2 files+14 added−4 removed

modified:src/click/testing.py+12 added
97 unmodified lines
98 self.output: io.BytesIO = io.BytesIO()99 self.stdout: io.BytesIO = BytesIOCopy(copy_to=self.output)100 self.stderr: io.BytesIO = BytesIOCopy(copy_to=self.output)101102 def __del__(self) -> None:103 """104 Guarantee that embedded file-like objects are closed in a105 predictable order, protecting against races between106 self.output being closed and other streams being flushed on close107108 .. versionadded:: 8.2.2109 """110 self.stderr.close()111 self.stdout.close()112 self.output.close()113114115class _NamedTextIOWrapper(io.TextIOWrapper):116 def __init__(117 self, buffer: t.BinaryIO, name: str, mode: str, **kwargs: t.Any118 ) -> None:
459 unmodified lines
modified:tests/test_testing.py+2 added-4 removed
446 unmodified lines
447 runner = CliRunner()448449 with runner.isolation() as (_, err, _):450 click.echo("\udce2", err=True, nl=False)451 assert err.getvalue() == b"\\udce2"452452 assert err.getvalue() == b"\\udce2"453454454def test_isolation_flushes_unflushed_stderr():455 """An un-flushed write to stderr, as with `print(..., file=sys.stderr)`, will end up456 flushed by the runner at end of invocation.457 """458 runner = CliRunner()459460 with runner.isolation() as (_, err, _):461 click.echo("\udce2", err=True, nl=False)462 assert err.getvalue() == b"\\udce2"463464 assert err.getvalue() == b"\\udce2"465464 @click.command()465 def cli():466 # set end="", flush=False so that it's totally clear that we won't get any467 # auto-flush behaviors
4 unmodified lines