Enters the supplied context manager. If successful, also pushes its __exit__ method as a callback and returns the result of the __enter__ method.
(self, cm)
| 514 | return exit # Allow use as a decorator. |
| 515 | |
| 516 | def enter_context(self, cm): |
| 517 | """Enters the supplied context manager. |
| 518 | |
| 519 | If successful, also pushes its __exit__ method as a callback and |
| 520 | returns the result of the __enter__ method. |
| 521 | """ |
| 522 | _enter = _lookup_special(cm, '__enter__', _sentinel) |
| 523 | if _enter is _sentinel: |
| 524 | cls = type(cm) |
| 525 | raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does " |
| 526 | f"not support the context manager protocol") |
| 527 | _exit = _lookup_special(cm, '__exit__', _sentinel) |
| 528 | if _exit is _sentinel: |
| 529 | cls = type(cm) |
| 530 | raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does " |
| 531 | f"not support the context manager protocol") |
| 532 | result = _enter() |
| 533 | self._push_exit_callback(_exit) |
| 534 | return result |
| 535 | |
| 536 | def callback(self, callback, /, *args, **kwds): |
| 537 | """Registers an arbitrary callback and arguments. |