Skip to content
Myr

Added Context.scope

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 86facdb · Browse files · 4 files+65 added−1 removed
This commit

State

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

Diff stats

4 files+65 added−1 removed

  • tests:+24 added−0 removed
  • other:+41 added−1 removed

4 files+65 added−1 removed

modified:click/core.py+38 added-1 removed
302 unmodified lines
303 self._depth -= 1304 if self._depth == 0:305 self.close()306307 @contextmanager308 def scope(self, cleanup=True):309 """This helper method can be used with the context object to promote310 it to the current thread local (see :func:`get_current_context`).311 The default behavior of this is to invoke the cleanup functions which312 can be disabled by setting `cleanup` to `False`. The cleanup313 functions are typically used for things such as closing file handles.314315 If the cleanup is intended the context object can also be directly316 used as a context manager.317318 Example usage::319320 with ctx.scope():321 assert get_current_context() is ctx322323 This is equivalent::324325 with ctx:326 assert get_current_context() is ctx327328 .. versionadded:: 5.0329330 :param cleanup: controls if the cleanup functions should be run or331 not. The default is to run these functions. In332 some situations the context only wants to be333 temporarily pushed in which case this can be disabled.334 Nested pushes automatically defer the cleanup.335 """336 if not cleanup:337 self._depth += 1338 with self as rv:339 yield rv340 if not cleanup:341 self._depth -= 1342343 @property344 def meta(self):345 """This is a dictionary which is shared with all the contexts346 that are nested. It exists so that click utiltiies can store some
240 unmodified lines
587 for key, value in iteritems(self.context_settings):588 if key not in extra:589 extra[key] = value590 ctx = Context(self, info_name=info_name, parent=parent, **extra)591 with ctx.scope(cleanup=False):555 self.parse_args(ctx, args)592 self.parse_args(ctx, args)593 return ctx594595 def parse_args(self, ctx, args):596 """Given a context and a list of arguments this creates the parser
1096 unmodified lines
modified:click/globals.py+2 added
9 unmodified lines
10 alternative to the :func:`pass_context` decorator. This function is11 primarily useful for helpers such as :func:`echo` which might be12 interested in changing it's behavior based on the current context.1314 To push the current context, :meth:`Context.scope` can be used.1516 .. versionadded:: 5.01718 :param silent: is set to `True` the return value is `None` if no context19 is available. The default behavior is to raise a
29 unmodified lines
modified:tests/test_context.py+24 added
140 unmodified lines
141 set_language('de_DE')142 assert get_language() == 'de_DE'143144 runner.invoke(cli, [], catch_exceptions=False)145146147def test_context_pushing():148 rv = []149150 @click.command()151 def cli():152 pass153154 ctx = click.Context(cli)155156 @ctx.call_on_close157 def test_callback():158 rv.append(42)159160 with ctx.scope(cleanup=False):161 pass162163 assert rv == []164165 with ctx.scope():166 pass167168 assert rv == [42]
modified:CHANGES+1 added
15 unmodified lines
16 using ``CliRunner``.17- added `get_current_context`.18- added a `meta` dictionary to the context which is shared across the19 linked list of contexts to allow click utilities to place state there.20- introduced `Context.scope`.2122Version 4.123-----------24
221 unmodified lines