This helper method can be used with the context object to promote it to the current thread local (see :func:`get_current_context`). The default behavior of this is to invoke the cleanup functions which can be disabled by setting `cleanup` to `False`. The cleanup func
(self, cleanup: bool = True)
| 563 | |
| 564 | @contextmanager |
| 565 | def scope(self, cleanup: bool = True) -> cabc.Generator[Context]: |
| 566 | """This helper method can be used with the context object to promote |
| 567 | it to the current thread local (see :func:`get_current_context`). |
| 568 | The default behavior of this is to invoke the cleanup functions which |
| 569 | can be disabled by setting `cleanup` to `False`. The cleanup |
| 570 | functions are typically used for things such as closing file handles. |
| 571 | |
| 572 | If the cleanup is intended the context object can also be directly |
| 573 | used as a context manager. |
| 574 | |
| 575 | Example usage:: |
| 576 | |
| 577 | with ctx.scope(): |
| 578 | assert get_current_context() is ctx |
| 579 | |
| 580 | This is equivalent:: |
| 581 | |
| 582 | with ctx: |
| 583 | assert get_current_context() is ctx |
| 584 | |
| 585 | .. versionadded:: 5.0 |
| 586 | |
| 587 | :param cleanup: controls if the cleanup functions should be run or |
| 588 | not. The default is to run these functions. In |
| 589 | some situations the context only wants to be |
| 590 | temporarily pushed in which case this can be disabled. |
| 591 | Nested pushes automatically defer the cleanup. |
| 592 | """ |
| 593 | if not cleanup: |
| 594 | self._depth += 1 |
| 595 | try: |
| 596 | with self as rv: |
| 597 | yield rv |
| 598 | finally: |
| 599 | if not cleanup: |
| 600 | self._depth -= 1 |
| 601 | |
| 602 | @property |
| 603 | def meta(self) -> dict[str, t.Any]: |
no outgoing calls