Close contexts created during shell completion #2644. (#2800)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andreas Backx · committed by GitHub · parent d8763b9 · Browse files · 4 files·+65 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: akhil-vempali <avempali@atlassian.com>
Diff stats
4 files+65 added−32 removed
- tests:+26 added−0 removed
- other:+39 added−32 removed
Provenance
- Commit
0ef55fd- Tree
5ae2e6d- Author
Andreas Backx <AndreasBackx@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
d8763b9
4 files changed+65 added−32 removed
modified:src/click/core.py+2 added-2 removed
702 unmodified lines703 def exit(self, code: int = 0) -> t.NoReturn:704 """Exits the application with a given exit code.705 706 .. versionchanged:: 8.2707- Force closing of callbacks registered with708- :meth:`call_on_close` before exiting the CLI.707+ Callbacks and context managers registered with :meth:`call_on_close`708+ and :meth:`with_resource` are closed before exiting.709 """710 self.close()711 raise Exit(code)712 2326 unmodified linesmodified:src/click/shell_completion.py+34 added-30 removed
542 unmodified lines543 :param prog_name: Name of the executable in the shell.544 :param args: List of complete args before the incomplete value.545 """546 ctx_args["resilient_parsing"] = True547- ctx = cli.make_context(prog_name, args.copy(), **ctx_args)548- args = ctx._protected_args + ctx.args547+ with cli.make_context(prog_name, args.copy(), **ctx_args) as ctx:548+ args = ctx._protected_args + ctx.args549 550- while args:551- command = ctx.command550+ while args:551+ command = ctx.command552 553- if isinstance(command, Group):554- if not command.chain:555- name, cmd, args = command.resolve_command(ctx, args)556-557- if cmd is None:558- return ctx559-560- ctx = cmd.make_context(name, args, parent=ctx, resilient_parsing=True)561- args = ctx._protected_args + ctx.args562- else:563- sub_ctx = ctx564-565- while args:553+ if isinstance(command, Group):554+ if not command.chain:555 name, cmd, args = command.resolve_command(ctx, args)556 557 if cmd is None:558 return ctx559 571- sub_ctx = cmd.make_context(572- name,573- args,574- parent=ctx,575- allow_extra_args=True,576- allow_interspersed_args=False,577- resilient_parsing=True,578- )579- args = sub_ctx.args560+ with cmd.make_context(561+ name, args, parent=ctx, resilient_parsing=True562+ ) as sub_ctx:563+ args = ctx._protected_args + ctx.args564+ ctx = sub_ctx565+ else:566+ sub_ctx = ctx567 581- ctx = sub_ctx582- args = [*sub_ctx._protected_args, *sub_ctx.args]583- else:584- break568+ while args:569+ name, cmd, args = command.resolve_command(ctx, args)570 571+ if cmd is None:572+ return ctx573+574+ with cmd.make_context(575+ name,576+ args,577+ parent=ctx,578+ allow_extra_args=True,579+ allow_interspersed_args=False,580+ resilient_parsing=True,581+ ) as sub_sub_ctx:582+ args = sub_ctx.args583+ sub_ctx = sub_sub_ctx584+585+ ctx = sub_ctx586+ args = [*sub_ctx._protected_args, *sub_ctx.args]587+ else:588+ break589+590 return ctx591 592 593 def _resolve_incomplete(44 unmodified linesmodified:tests/test_shell_completion.py+26 added
1+import warnings2+3 import pytest4 5 import click.shell_completion6 from click.core import Argument408 unmodified lines415 416 # Using `add_completion_class` as a decorator adds the new shell immediately417 assert "mysh" in click.shell_completion._available_shells418 assert click.shell_completion._available_shells["mysh"] is MyshComplete419+420+421+# Don't make the ResourceWarning give an error422+@pytest.mark.filterwarnings("default")423+def test_files_closed(runner) -> None:424+ with runner.isolated_filesystem():425+ config_file = "foo.txt"426+ with open(config_file, "w") as f:427+ f.write("bar")428+429+ @click.group()430+ @click.option(431+ "--config-file",432+ default=config_file,433+ type=click.File(mode="r"),434+ )435+ @click.pass_context436+ def cli(ctx, config_file):437+ pass438+439+ with warnings.catch_warnings(record=True) as current_warnings:440+ assert not current_warnings, "There should be no warnings to start"441+ _get_completions(cli, args=[], incomplete="")442+ assert not current_warnings, "There should be no warnings after either"modified:CHANGES.rst+3 added
57 unmodified lines58 invalid choice message. :issue:`2621` :pr:`2622`59 - If help is shown because ``no_args_is_help`` is enabled (defaults to ``True``60 for groups, ``False`` for commands), the exit code is 2 instead of 0.61 :issue:`1489` :pr:`1489`62+- Contexts created during shell completion are closed properly, fixing63+ ``ResourceWarning``s when using ``click.File``. :issue:`2644` :pr:`2800`64+ :pr:`2767`65 66 67 Version 8.1.868 -------------1116 unmodified lines