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)
| 513 | return exit # Allow use as a decorator. |
| 514 | |
| 515 | def enter_context(self, cm): |
| 516 | """Enters the supplied context manager. |
| 517 | |
| 518 | If successful, also pushes its __exit__ method as a callback and |
| 519 | returns the result of the __enter__ method. |
| 520 | """ |
| 521 | # We look up the special methods on the type to match the with |
| 522 | # statement. |
| 523 | cls = type(cm) |
| 524 | try: |
| 525 | _enter = cls.__enter__ |
| 526 | _exit = cls.__exit__ |
| 527 | except AttributeError: |
| 528 | raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does " |
| 529 | f"not support the context manager protocol") from None |
| 530 | result = _enter(cm) |
| 531 | self._push_cm_exit(cm, _exit) |
| 532 | return result |
| 533 | |
| 534 | def callback(self, callback, /, *args, **kwds): |
| 535 | """Registers an arbitrary callback and arguments. |