Skip to content

Commit 024ef39

Browse files
committed
fix
1 parent 80f8bf9 commit 024ef39

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

playwright/_impl/_browser_context.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import asyncio
16-
import contextlib
1716
import json
1817
import sys
1918
from pathlib import Path
@@ -121,10 +120,12 @@ def __init__(
121120
)
122121
self._channel.on(
123122
"route",
124-
lambda params: self._on_route(
125-
from_channel(params.get("route")),
123+
lambda params: self._emit_sync(
124+
self._on_route(
125+
from_channel(params.get("route")),
126+
)
126127
),
127-
),
128+
)
128129

129130
self._channel.on(
130131
"backgroundPage",
@@ -201,19 +202,20 @@ async def _on_route(self, route: Route) -> None:
201202
handled = await route_handler.handle(route)
202203
finally:
203204
if len(self._routes) == 0:
204-
with contextlib.suppress(Exception):
205-
await self._connection.wrap_api_call(
205+
self._emit_sync(
206+
self._connection.wrap_api_call(
206207
lambda: self._update_interception_patterns(), True
207208
)
209+
)
208210
if handled:
209211
return
210212
await route._internal_continue(is_internal=True)
211213

212-
async def _on_binding(self, binding_call: BindingCall) -> None:
214+
def _on_binding(self, binding_call: BindingCall) -> None:
213215
func = self._bindings.get(binding_call._initializer["name"])
214216
if func is None:
215217
return
216-
await binding_call.call(func)
218+
self._emit_sync(binding_call.call(func))
217219

218220
def set_default_navigation_timeout(self, timeout: float) -> None:
219221
self._timeout_settings.set_navigation_timeout(timeout)

playwright/_impl/_connection.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
TYPE_CHECKING,
2424
Any,
2525
Callable,
26+
Coroutine,
2627
Dict,
2728
List,
2829
Mapping,
@@ -169,6 +170,19 @@ def _add_event_handler(self, event: str, k: Any, v: Any) -> None:
169170
self._update_subscription(event, True)
170171
super()._add_event_handler(event, k, v)
171172

173+
def _emit_sync(self, coro: Coroutine, ignore_errors: bool = True) -> None:
174+
fut = asyncio.ensure_future(coro, loop=self._loop)
175+
176+
def cb(f: asyncio.Task) -> None:
177+
if f.cancelled():
178+
return
179+
180+
exc: Optional[BaseException] = f.exception()
181+
if exc and not ignore_errors:
182+
self.emit("error", exc)
183+
184+
fut.add_done_callback(cb)
185+
172186
def remove_listener(self, event: str, f: Any) -> None:
173187
super().remove_listener(event, f)
174188
if not self.listeners(event):

playwright/_impl/_page.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import asyncio
1616
import base64
17-
import contextlib
1817
import inspect
1918
import re
2019
import sys
@@ -194,7 +193,9 @@ def __init__(
194193
)
195194
self._channel.on(
196195
"route",
197-
lambda params: self._on_route(from_channel(params["route"])),
196+
lambda params: self._emit_sync(
197+
self._on_route(from_channel(params["route"]))
198+
),
198199
)
199200
self._channel.on("video", lambda params: self._on_video(params))
200201
self._channel.on(
@@ -254,19 +255,20 @@ async def _on_route(self, route: Route) -> None:
254255
handled = await route_handler.handle(route)
255256
finally:
256257
if len(self._routes) == 0:
257-
with contextlib.suppress(Exception):
258-
await self._connection.wrap_api_call(
258+
self._emit_sync(
259+
self._connection.wrap_api_call(
259260
lambda: self._update_interception_patterns(), True
260261
)
262+
)
261263
if handled:
262264
return
263265
await self._browser_context._on_route(route)
264266

265-
async def _on_binding(self, binding_call: "BindingCall") -> None:
267+
def _on_binding(self, binding_call: "BindingCall") -> None:
266268
func = self._bindings.get(binding_call._initializer["name"])
267269
if func:
268-
await binding_call.call(func)
269-
await self._browser_context._on_binding(binding_call)
270+
self._emit_sync(binding_call.call(func))
271+
self._browser_context._on_binding(binding_call)
270272

271273
def _on_worker(self, worker: "Worker") -> None:
272274
self._workers.append(worker)
@@ -284,16 +286,15 @@ def _on_close(self) -> None:
284286
def _on_crash(self) -> None:
285287
self.emit(Page.Events.Crash, self)
286288

287-
async def _on_dialog(self, params: Any) -> None:
289+
def _on_dialog(self, params: Any) -> None:
288290
dialog = cast(Dialog, from_channel(params["dialog"]))
289291
if self.listeners(Page.Events.Dialog):
290292
self.emit(Page.Events.Dialog, dialog)
291293
else:
292-
with contextlib.suppress(Exception):
293-
if dialog.type == "beforeunload":
294-
await dialog.accept()
295-
else:
296-
await dialog.dismiss()
294+
if dialog.type == "beforeunload":
295+
self._emit_sync(dialog.accept())
296+
else:
297+
self._emit_sync(dialog.dismiss())
297298

298299
def _on_download(self, params: Any) -> None:
299300
url = params["url"]
@@ -1268,7 +1269,7 @@ async def call(self, func: Callable) -> None:
12681269
await self._channel.send("resolve", dict(result=serialize_argument(result)))
12691270
except Exception as e:
12701271
tb = sys.exc_info()[2]
1271-
asyncio.create_task(
1272+
self._emit_sync(
12721273
self._channel.send(
12731274
"reject", dict(error=dict(error=serialize_error(e, tb)))
12741275
)

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