Enters the supplied asynchronous context manager. If successful, also adds its __aexit__ method as a cleanup function and returns the result of the __aenter__ method.
(self, cm)
| 63 | self.addCleanup(*(func, *args), **kwargs) |
| 64 | |
| 65 | async def enterAsyncContext(self, cm): |
| 66 | """Enters the supplied asynchronous context manager. |
| 67 | |
| 68 | If successful, also adds its __aexit__ method as a cleanup |
| 69 | function and returns the result of the __aenter__ method. |
| 70 | """ |
| 71 | # We look up the special methods on the type to match the with |
| 72 | # statement. |
| 73 | cls = type(cm) |
| 74 | try: |
| 75 | enter = cls.__aenter__ |
| 76 | exit = cls.__aexit__ |
| 77 | except AttributeError: |
| 78 | msg = (f"'{cls.__module__}.{cls.__qualname__}' object does " |
| 79 | "not support the asynchronous context manager protocol") |
| 80 | try: |
| 81 | cls.__enter__ |
| 82 | cls.__exit__ |
| 83 | except AttributeError: |
| 84 | pass |
| 85 | else: |
| 86 | msg += (" but it supports the context manager protocol. " |
| 87 | "Did you mean to use enterContext()?") |
| 88 | raise TypeError(msg) from None |
| 89 | result = await enter(cm) |
| 90 | self.addAsyncCleanup(exit, cm, None, None, None) |
| 91 | return result |
| 92 | |
| 93 | def _callSetUp(self): |
| 94 | # Force loop to be initialized and set as the current loop |