Preserve contexts around result callbacks and ensure that they are invoked without subcommands.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 5250692 · Browse files · 1 file·+36 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+36 added−16 removed
- other:+36 added−16 removed
Provenance
- Commit
bd61da2- Tree
f31c731- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5250692
1 file changed+36 added−16 removed
modified:click/core.py+36 added-16 removed
854 unmodified lines855 ctx.exit()856 return Command.parse_args(self, ctx, args)857 858 def invoke(self, ctx):859+ def _process_result(value):860+ if self.result_callback is not None:861+ value = ctx.invoke(self.result_callback, value,862+ **ctx.params)863+ return value864+865 if not ctx.args:866+ # If we are invoked without command the chain flag controls867+ # how this happens. If we are not in chain mode, the return868+ # value here is the return value of the command.869+ # If however we are in chain mode, the return value is the870+ # return value of the result processor invoked with an empty871+ # list (which means that no subcommand actually was executed).872 if self.invoke_without_command:873+ if not self.chain:861- return Command.invoke(self, ctx)874+ return Command.invoke(self, ctx)875+ with ctx:876+ Command.invoke(self, ctx)877+ return _process_result([])878 ctx.fail('Missing command.')879 880 args = ctx.args881 866- def _process_result(value):867- if self.result_callback is not None:868- value = ctx.invoke(self.result_callback, value)869- return value870-882 # If we're not in chain mode, we only allow the invocation of a883 # single command but we also inform the current context about the884 # name of the command to invoke.885 if not self.chain:886 sub_ctx = self.handle_subcommand(ctx, args)887 ctx.invoked_subcommands = [sub_ctx.info_name]877- Command.invoke(self, ctx)878- with sub_ctx:879- return _process_result(sub_ctx.command.invoke(sub_ctx))888 889+ # Make sure the context is entered so we do not clean up890+ # resources until the result processor has worked.891+ with ctx:892+ Command.invoke(self, ctx)893+ with sub_ctx:894+ return _process_result(sub_ctx.command.invoke(sub_ctx))895+896 # Otherwise we make every single context and invoke them in a882- # chain.897+ # chain. In that case the return value to the result processor898+ # is the list of all invoked subcommand's results.899 contexts = []900 while args:901 sub_ctx = self.handle_subcommand(ctx, args, allow_extra_args=True,902 allow_interspersed_args=False)1 unmodified line904 args = sub_ctx.args905 906 # Now that we have all contexts, we can invoke them.907 ctx.invoked_subcommands = [x.info_name for x in contexts]892- Command.invoke(self, ctx)893- rv = []894- for sub_ctx in contexts:895- with sub_ctx:896- rv.append(sub_ctx.command.invoke(sub_ctx))897- return _process_result(rv)908 909+ # Make sure the context is entered so we do not clean up910+ # resources until the result processor has worked.911+ with ctx:912+ Command.invoke(self, ctx)913+ rv = []914+ for sub_ctx in contexts:915+ with sub_ctx:916+ rv.append(sub_ctx.command.invoke(sub_ctx))917+ return _process_result(rv)918+919 def handle_subcommand(self, ctx, args, **extra):920 cmd_name = make_str(args[0])921 original_cmd_name = cmd_name922 607 unmodified lines