Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure context __exit__ is called during shell completion #2767

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ def make_context(

ctx = self.context_class(self, info_name=info_name, parent=parent, **extra)

with ctx.scope(cleanup=False):
with ctx:
self.parse_args(ctx, args)
return ctx

Expand Down
63 changes: 32 additions & 31 deletions src/click/shell_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,44 +544,45 @@ def _resolve_context(
:param args: List of complete args before the incomplete value.
"""
ctx_args["resilient_parsing"] = True
ctx = cli.make_context(prog_name, args.copy(), **ctx_args)
args = ctx._protected_args + ctx.args
with cli.make_context(prog_name, args.copy(), **ctx_args) as ctx:
args = ctx._protected_args + ctx.args

while args:
command = ctx.command
while args:
command = ctx.command

if isinstance(command, Group):
if not command.chain:
name, cmd, args = command.resolve_command(ctx, args)

if cmd is None:
return ctx

ctx = cmd.make_context(name, args, parent=ctx, resilient_parsing=True)
args = ctx._protected_args + ctx.args
else:
sub_ctx = ctx

while args:
if isinstance(command, Group):
if not command.chain:
name, cmd, args = command.resolve_command(ctx, args)

if cmd is None:
return ctx

sub_ctx = cmd.make_context(
name,
args,
parent=ctx,
allow_extra_args=True,
allow_interspersed_args=False,
resilient_parsing=True,
)
args = sub_ctx.args

ctx = sub_ctx
args = [*sub_ctx._protected_args, *sub_ctx.args]
else:
break
with cmd.make_context(
name, args, parent=ctx, resilient_parsing=True
) as sub_ctx:
args = sub_ctx._protected_args + sub_ctx.args
ctx = sub_ctx
else:
sub_ctx = ctx

while args:
name, cmd, args = command.resolve_command(ctx, args)

if cmd is None:
return ctx

with cmd.make_context(
name,
args,
parent=ctx,
allow_extra_args=True,
allow_interspersed_args=False,
resilient_parsing=True,
) as sub_ctx:
args = sub_ctx.args
ctx = sub_ctx
else:
break

return ctx

Expand Down
Loading