Skip to content

Commit 81c097c

Browse files
committed
fix sync variant
1 parent db8ed1a commit 81c097c

File tree

4 files changed

+36
-29
lines changed

4 files changed

+36
-29
lines changed

playwright/_impl/_browser_type.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ async def connect(
172172
slow_mo: float = None,
173173
headers: Dict[str, str] = None,
174174
) -> Browser:
175-
transport = WebSocketTransport(self._connection._loop, ws_endpoint, timeout, headers)
175+
transport = WebSocketTransport(
176+
self._connection._loop, ws_endpoint, timeout, headers
177+
)
176178

177179
connection = Connection(
178180
self._connection._dispatcher_fiber,
@@ -182,7 +184,7 @@ async def connect(
182184
connection._is_sync = self._connection._is_sync
183185
connection._loop = self._connection._loop
184186
connection._loop.create_task(connection.run())
185-
await connection.wait_until_started()
187+
await connection.initialize()
186188
playwright = await connection.wait_for_object_with_known_name("Playwright")
187189
self._connection._child_ws_connections.append(connection)
188190
pre_launched_browser = playwright._initializer.get("preLaunchedBrowser")

playwright/_impl/_connection.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,21 @@ def __init__(
165165
self._is_sync = False
166166
self._api_name = ""
167167
self._child_ws_connections: List["Connection"] = []
168+
self._initialized = False
168169

169170
async def run_as_sync(self) -> None:
170171
self._is_sync = True
171172
await self.run()
172-
await self.wait_until_started()
173+
await self.initialize()
173174

174175
async def run(self) -> None:
175176
self._loop = asyncio.get_running_loop()
176177
self._root_object = RootChannelOwner(self)
177178
await self._transport.run()
178179

179-
async def wait_until_started(self) -> None:
180-
await self._transport.wait_until_started
180+
async def initialize(self) -> None:
181+
await self._transport.wait_until_initialized
182+
self._initialized = True
181183

182184
def stop_sync(self) -> None:
183185
self._transport.request_stop()
@@ -194,6 +196,7 @@ def cleanup(self) -> None:
194196
ws_connection._transport.dispose()
195197

196198
async def wait_for_object_with_known_name(self, guid: str) -> Any:
199+
assert self._initialized
197200
if guid in self._objects:
198201
return self._objects[guid]
199202
callback = self._loop.create_future()

playwright/_impl/_transport.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ def __init__(self, loop: asyncio.AbstractEventLoop) -> None:
4646
self.on_error_future: asyncio.Future
4747
self.on_message = lambda _: None
4848

49-
self.wait_until_started: asyncio.Future = loop.create_future()
50-
self._wait_until_started_set_success = (
51-
lambda: self.wait_until_started.set_result(True)
49+
self.wait_until_initialized: asyncio.Future = loop.create_future()
50+
self._wait_until_initialized_set_success = (
51+
lambda: self.wait_until_initialized.set_result(True)
5252
)
53-
self._wait_until_started_set_exception = (
54-
lambda exc: self.wait_until_started.set_exception(exc)
53+
self._wait_until_initialized_set_exception = (
54+
lambda exc: self.wait_until_initialized.set_exception(exc)
5555
)
5656

5757
@abstractmethod
@@ -88,7 +88,9 @@ def deserialize_message(self, data: bytes) -> Any:
8888

8989

9090
class PipeTransport(Transport):
91-
def __init__(self, loop: asyncio.AbstractEventLoop, driver_executable: Path) -> None:
91+
def __init__(
92+
self, loop: asyncio.AbstractEventLoop, driver_executable: Path
93+
) -> None:
9294
super().__init__(loop)
9395
self._stopped = False
9496
self._driver_executable = driver_executable
@@ -115,17 +117,11 @@ async def run(self) -> None:
115117
stderr=_get_stderr_fileno(),
116118
limit=32768,
117119
)
118-
except FileNotFoundError:
119-
self._wait_until_started_set_exception(
120-
Error(
121-
"playwright's driver is not found, You can read the contributing guide "
122-
"for some guidance on how to get everything setup for working on the code "
123-
"https://github.com/microsoft/playwright-python/blob/master/CONTRIBUTING.md"
124-
)
125-
)
120+
except Exception as exc:
121+
self._wait_until_initialized_set_exception(exc)
126122
return
127123

128-
self._wait_until_started_set_success()
124+
self._wait_until_initialized_set_success()
129125

130126
assert proc.stdout
131127
assert proc.stdin
@@ -161,16 +157,19 @@ def send(self, message: Dict) -> None:
161157

162158
class WebSocketTransport(AsyncIOEventEmitter, Transport):
163159
def __init__(
164-
self, loop: asyncio.AbstractEventLoop, ws_endpoint: str, timeout: float = None, headers: Dict[str, str] = None
160+
self,
161+
loop: asyncio.AbstractEventLoop,
162+
ws_endpoint: str,
163+
timeout: float = None,
164+
headers: Dict[str, str] = None,
165165
) -> None:
166-
super().__init__()
166+
super().__init__(loop)
167167
Transport.__init__(self, loop)
168168

169169
self._stopped = False
170170
self.ws_endpoint = ws_endpoint
171-
self.timeout = timeout
171+
self.timeout = timeout or 30000
172172
self.headers = headers
173-
self._loop: asyncio.AbstractEventLoop
174173

175174
def request_stop(self) -> None:
176175
self._stopped = True
@@ -189,17 +188,18 @@ async def run(self) -> None:
189188
if self.timeout is not None:
190189
options["close_timeout"] = self.timeout / 1000
191190
options["ping_timeout"] = self.timeout / 1000
192-
if self.headers is not None:
191+
192+
if self.headers:
193193
options["extra_headers"] = self.headers
194194
try:
195195
self._connection = await websockets.connect(self.ws_endpoint, **options)
196196
except Exception as err:
197-
self._wait_until_started_set_exception(
197+
self._wait_until_initialized_set_exception(
198198
Error(f"playwright's websocket endpoint connection error: {err}")
199199
)
200200
return
201201

202-
self._wait_until_started_set_success()
202+
self._wait_until_initialized_set_success()
203203

204204
while not self._stopped:
205205
try:

playwright/async_api/_context_manager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ def __init__(self) -> None:
2828

2929
async def __aenter__(self) -> AsyncPlaywright:
3030
self._connection = Connection(
31-
None, create_remote_object, PipeTransport(asyncio.get_event_loop(), compute_driver_executable())
31+
None,
32+
create_remote_object,
33+
PipeTransport(asyncio.get_event_loop(), compute_driver_executable()),
3234
)
3335
loop = asyncio.get_running_loop()
3436
self._connection._loop = loop
3537
loop.create_task(self._connection.run())
36-
await self._connection.wait_until_started()
38+
await self._connection.initialize()
3739
playwright = AsyncPlaywright(
3840
await self._connection.wait_for_object_with_known_name("Playwright")
3941
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy