Skip to content

Commit 9b82e23

Browse files
committed
refactor: DisposableStub takes parent ChannelOwner, align _sync with SyncBase
1 parent f29091a commit 9b82e23

5 files changed

Lines changed: 11 additions & 20 deletions

File tree

playwright/_impl/_browser_context.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,7 @@ async def route(
438438
),
439439
)
440440
await self._update_interception_patterns()
441-
return DisposableStub(
442-
lambda: self.unroute(url, handler), self._loop, self._dispatcher_fiber
443-
)
441+
return DisposableStub(lambda: self.unroute(url, handler), self)
444442

445443
async def unroute(
446444
self, url: URLMatch, handler: Optional[RouteHandlerCallback] = None

playwright/_impl/_disposable.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import asyncio
1616
import inspect
1717
import traceback
18-
from asyncio import AbstractEventLoop
1918
from typing import Awaitable, Callable, Dict
2019

2120
import greenlet
@@ -51,12 +50,11 @@ class DisposableStub:
5150
def __init__(
5251
self,
5352
dispose_fn: Callable[[], Awaitable[None]],
54-
loop: AbstractEventLoop,
55-
dispatcher_fiber: object,
53+
parent: ChannelOwner,
5654
) -> None:
5755
self._dispose_fn = dispose_fn
58-
self._loop = loop
59-
self._dispatcher_fiber = dispatcher_fiber
56+
self._loop = parent._loop
57+
self._dispatcher_fiber = parent._dispatcher_fiber
6058

6159
async def dispose(self) -> None:
6260
await self._dispose_fn()
@@ -74,7 +72,9 @@ def __exit__(self, *args: object) -> None:
7472
self._sync(self.dispose())
7573

7674
def _sync(self, coro: object) -> object:
75+
__tracebackhide__ = True
7776
if self._loop.is_closed():
77+
coro.close() # type: ignore
7878
raise Error("Event loop is closed! Is Playwright already stopped?")
7979
g_self = greenlet.getcurrent()
8080
task = self._loop.create_task(coro) # type: ignore

playwright/_impl/_page.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,7 @@ async def route(
690690
),
691691
)
692692
await self._update_interception_patterns()
693-
return DisposableStub(
694-
lambda: self.unroute(url, handler), self._loop, self._dispatcher_fiber
695-
)
693+
return DisposableStub(lambda: self.unroute(url, handler), self)
696694

697695
async def unroute(
698696
self, url: URLMatch, handler: Optional[RouteHandlerCallback] = None

playwright/_impl/_screencast.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async def start(
7171
self._artifact = from_channel(result)
7272
self._save_path = path
7373

74-
return DisposableStub(lambda: self.stop(), self._loop, self._dispatcher_fiber)
74+
return DisposableStub(lambda: self.stop(), self._page)
7575

7676
async def stop(self) -> None:
7777
self._started = False
@@ -95,9 +95,7 @@ async def show_actions(
9595
None,
9696
{"duration": duration, "position": position, "fontSize": fontSize},
9797
)
98-
return DisposableStub(
99-
lambda: self.hide_actions(), self._loop, self._dispatcher_fiber
100-
)
98+
return DisposableStub(lambda: self.hide_actions(), self._page)
10199

102100
async def hide_actions(self) -> None:
103101
await self._page._channel.send("screencastHideActions", None)
@@ -115,8 +113,7 @@ async def show_overlay(self, html: str, duration: float = None) -> DisposableStu
115113
None,
116114
{"id": overlay_id},
117115
),
118-
self._loop,
119-
self._dispatcher_fiber,
116+
self._page,
120117
)
121118

122119
async def show_chapter(

playwright/_impl/_tracing.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,7 @@ async def group(
144144
self, name: str, location: TracingGroupLocation = None
145145
) -> DisposableStub:
146146
await self._channel.send("tracingGroup", None, locals_to_params(locals()))
147-
return DisposableStub(
148-
lambda: self.group_end(), self._loop, self._dispatcher_fiber
149-
)
147+
return DisposableStub(lambda: self.group_end(), self)
150148

151149
async def group_end(self) -> None:
152150
await self._channel.send(

0 commit comments

Comments
 (0)