| 47 | |
| 48 | |
| 49 | class DisposableStub: |
| 50 | def __init__( |
| 51 | self, |
| 52 | dispose_fn: Callable[[], Awaitable[None]], |
| 53 | parent: ChannelOwner, |
| 54 | ) -> None: |
| 55 | self._dispose_fn = dispose_fn |
| 56 | self._loop = parent._loop |
| 57 | self._dispatcher_fiber = parent._dispatcher_fiber |
| 58 | |
| 59 | async def dispose(self) -> None: |
| 60 | await self._dispose_fn() |
| 61 | |
| 62 | async def __aenter__(self) -> "DisposableStub": |
| 63 | return self |
| 64 | |
| 65 | async def __aexit__(self, *args: object) -> None: |
| 66 | await self.dispose() |
| 67 | |
| 68 | def __enter__(self) -> "DisposableStub": |
| 69 | return self |
| 70 | |
| 71 | def __exit__(self, *args: object) -> None: |
| 72 | self._sync(self.dispose()) |
| 73 | |
| 74 | def _sync(self, coro: object) -> object: |
| 75 | __tracebackhide__ = True |
| 76 | if self._loop.is_closed(): |
| 77 | coro.close() # type: ignore |
| 78 | raise Error("Event loop is closed! Is Playwright already stopped?") |
| 79 | g_self = greenlet.getcurrent() |
| 80 | task = self._loop.create_task(coro) # type: ignore |
| 81 | setattr(task, "__pw_stack__", inspect.stack(0)) |
| 82 | setattr(task, "__pw_stack_trace__", traceback.extract_stack(limit=10)) |
| 83 | task.add_done_callback(lambda _: g_self.switch()) |
| 84 | while not task.done(): |
| 85 | self._dispatcher_fiber.switch() # type: ignore |
| 86 | asyncio._set_running_loop(self._loop) |
| 87 | return task.result() |
| 88 | |
| 89 | async def close(self) -> None: |
| 90 | await self.dispose() |
| 91 | |
| 92 | def __repr__(self) -> str: |
| 93 | return "<Disposable>" |
no outgoing calls
no test coverage detected