Defer subcommand context creation until later.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This fixes a regression that was introduced with the 3.0 release. This broke well established patters and was never intended to break. This fixes #200
authored by Armin Ronacher · committed by Armin Ronacher · parent 85dec99 · Browse files · 3 files·+40 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
3 files+40 added−16 removed
- tests:+20 added−0 removed
- other:+20 added−16 removed
Provenance
- Commit
d88d0a7- Tree
1eb05f1- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
85dec99
3 files changed+40 added−16 removed
modified:click/core.py+16 added-16 removed
881 unmodified lines882 # 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]888-886 # Make sure the context is entered so we do not clean up887 # resources until the result processor has worked.888 with ctx:889 Command.invoke(self, ctx)890+ sub_ctx = self.handle_subcommand(ctx, args)891+ ctx.invoked_subcommands = [sub_ctx.info_name]892 with sub_ctx:893 return _process_result(sub_ctx.command.invoke(sub_ctx))894 896- # Otherwise we make every single context and invoke them in a897- # 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)903- contexts.append(sub_ctx)904- 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]908-895 # Make sure the context is entered so we do not clean up896 # resources until the result processor has worked.897 with ctx:898 Command.invoke(self, ctx)899+900+ # Otherwise we make every single context and invoke them in a901+ # chain. In that case the return value to the result processor902+ # is the list of all invoked subcommand's results.903+ contexts = []904+ while args:905+ sub_ctx = self.handle_subcommand(ctx, args, allow_extra_args=True,906+ allow_interspersed_args=False)907+ contexts.append(sub_ctx)908+ args = sub_ctx.args909+910+ # Now that we have all contexts, we can invoke them.911+ ctx.invoked_subcommands = [x.info_name for x in contexts]912+913 rv = []914 for sub_ctx in contexts:915 with sub_ctx:916 rv.append(sub_ctx.command.invoke(sub_ctx))613 unmodified linesmodified:tests/test_commands.py+20 added
172 unmodified lines173 ' -h, --help show this help message and exit',174 ' -f FILE, --file=FILE write report to FILE',175 ' -q, --quiet don\'t print status messages to stdout',176 ]177+178+179+def test_object_propagation(runner):180+ for chain in False, True:181+ @click.group(chain=chain)182+ @click.option('--debug/--no-debug', default=False)183+ @click.pass_context184+ def cli(ctx, debug):185+ if ctx.obj is None:186+ ctx.obj = {}187+ ctx.obj['DEBUG'] = debug188+189+ @cli.command()190+ @click.pass_context191+ def sync(ctx):192+ click.echo('Debug is %s' % (ctx.obj['DEBUG'] and 'on' or 'off'))193+194+ result = runner.invoke(cli, ['sync'])195+ assert result.exception is None196+ assert result.output == 'Debug is off\n'modified:CHANGES+4 added
6 unmodified lines7 -----------8 9 (bugfix release, release date to be decided)10 11+- Fixed a regression that caused contexts of subcommands to be12+ created before the parent command was invoked which was a13+ regression from earlier Click versions.14+15 Version 3.016 -----------17 18 (codename "clonk clonk", released on August 12th 2014)143 unmodified lines