Skip to content
Myr

NamedTextIOWrapper: stop closing buffer

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

The NamedTextIOWrapper is closing the buffers that are owned by the StreamMixer, so implement a close() method that prevents it from doing that. Refs #824 Refs #2993

authored by Peter A. Jonsson · committed by Kevin Deldycke · parent b6afbf0 · Browse files · 3 files+36 added−12 removed
This commit

State

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

Diff stats

3 files+36 added−12 removed

  • tests:+25 added−0 removed
  • other:+11 added−12 removed

3 files+36 added−12 removed

modified:src/click/testing.py+9 added-12 removed
96 unmodified lines
97 self.output: io.BytesIO = io.BytesIO()98 self.stdout: io.BytesIO = BytesIOCopy(copy_to=self.output)99 self.stderr: io.BytesIO = BytesIOCopy(copy_to=self.output)100101 def __del__(self) -> None:102 """103 Guarantee that embedded file-like objects are closed in a104 predictable order, protecting against races between105 self.output being closed and other streams being flushed on close101107 .. versionadded:: 8.2.2108 """109 self.stderr.close()110 self.stdout.close()111 self.output.close()112113102class _NamedTextIOWrapper(io.TextIOWrapper):103 def __init__(104 self, buffer: t.BinaryIO, name: str, mode: str, **kwargs: t.Any105 ) -> None:106 super().__init__(buffer, **kwargs)107 self._name = name108 self._mode = mode109110 def close(self) -> None:111 """112 The buffer this object contains belongs to some other object, so113 prevent the default __del__ implementation from closing that buffer.114115 .. versionadded:: 8.3.2116 """117 ...118119 @property120 def name(self) -> str:121 return self._name122
452 unmodified lines
added:tests/test_testing_logging.py+25 added
1import logging23import click4from click.testing import CliRunner567# Minimized version of:8# https://github.com/pallets/click/issues/824#issuecomment-30272632629#10# Test needs to be run as "pytest --log-cli-level 30 tests/test_testing_logging.py"11# to test the intended functionality.12def test_runner_logger():13 logger = logging.getLogger(__name__)1415 @click.command()16 @click.option("--name", prompt="Your name", help="The person to greet.")17 def hello(name):18 logger.warning("Greeting user now...")19 click.echo(f"Hello, {name}!")2021 runner = CliRunner()22 result = runner.invoke(hello, input="Peter")23 assert result.exit_code == 024 # FIXME: second half of the output is missing with --log-cli-level 30.25 assert result.output in ("Your name: Peter\n", "Your name: Peter\nHello, Peter!\n")
modified:CHANGES.rst+2 added
4 unmodified lines
5- Fix handling of ``flag_value`` when ``is_flag=False`` to allow such options to be6 used without an explicit value. :issue:`3084`7- Hide ``Sentinel.UNSET`` values as ``None`` when using ``lookup_default()``.8 :issue:`3136` :pr:`3199` :pr:`3202` :pr:`3209` :pr:`3212` :pr:`3224`9- Prevent ``_NamedTextIOWrapper`` from closing streams owned by ``StreamMixer``.10 :issue:`824` :issue:`2993` :pr:`3139`1112Version 8.3.113--------------14
1305 unmodified lines