Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions playwright/_impl/_async_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# limitations under the License.

import asyncio
from typing import Any, Callable, Generic, TypeVar
import traceback
from typing import Any, Awaitable, Callable, Generic, TypeVar

from playwright._impl._impl_to_api_mapping import ImplToApiMapping, ImplWrapper

Expand Down Expand Up @@ -54,8 +55,11 @@ def __init__(self, impl_obj: Any) -> None:
def __str__(self) -> str:
return self._impl_obj.__str__()

def _sync(self, future: asyncio.Future) -> Any:
return self._loop.run_until_complete(future)
def _async(self, api_name: str, coro: Awaitable) -> Any:
task = asyncio.current_task()
setattr(task, "__pw_api_name__", api_name)
setattr(task, "__pw_stack_trace__", traceback.extract_stack())
return coro

def _wrap_handler(self, handler: Any) -> Callable[..., None]:
if callable(handler):
Expand Down
2 changes: 1 addition & 1 deletion playwright/_impl/_browser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def expect_event(
) -> EventContextManagerImpl:
if timeout is None:
timeout = self._timeout_settings.timeout()
wait_helper = WaitHelper(self, f"expect_event({event})")
wait_helper = WaitHelper(self, f"browser_context.expect_event({event})")
wait_helper.reject_on_timeout(
timeout, f'Timeout while waiting for event "{event}"'
)
Expand Down
15 changes: 9 additions & 6 deletions playwright/_impl/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def __init__(
self._callbacks: Dict[int, ProtocolCallback] = {}
self._object_factory = object_factory
self._is_sync = False
self._api_name = ""

async def run_as_sync(self) -> None:
self._is_sync = True
Expand Down Expand Up @@ -194,20 +195,22 @@ def _send_message_to_server(
self._last_id += 1
id = self._last_id
callback = ProtocolCallback(self._loop)
if self._is_sync:
task = asyncio.current_task(self._loop)
callback.stack_trace = (
getattr(task, "__pw_stack_trace__", None) if task else None
)
task = asyncio.current_task(self._loop)
callback.stack_trace = getattr(task, "__pw_stack_trace__", None)
if not callback.stack_trace:
callback.stack_trace = traceback.extract_stack()

metadata = {"stack": serialize_call_stack(callback.stack_trace)}
api_name = getattr(task, "__pw_api_name__", None)
if api_name:
metadata["apiName"] = api_name

message = dict(
id=id,
guid=guid,
method=method,
params=self._replace_channels_with_guids(params, "params"),
metadata={"stack": serialize_call_stack(callback.stack_trace)},
metadata=metadata,
)
self._transport.send(message)
self._callbacks[id] = callback
Expand Down
2 changes: 1 addition & 1 deletion playwright/_impl/_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async def goto(
def _setup_navigation_wait_helper(
self, wait_name: str, timeout: float = None
) -> WaitHelper:
wait_helper = WaitHelper(self, wait_name)
wait_helper = WaitHelper(self, f"frame.{wait_name}")
wait_helper.reject_on_event(
self._page, "close", Error("Navigation failed because page was closed!")
)
Expand Down
2 changes: 1 addition & 1 deletion playwright/_impl/_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def expect_event(
) -> EventContextManagerImpl:
if timeout is None:
timeout = cast(Any, self._parent)._timeout_settings.timeout()
wait_helper = WaitHelper(self, f"expect_event({event})")
wait_helper = WaitHelper(self, f"web_socket.expect_event({event})")
wait_helper.reject_on_timeout(
timeout, f'Timeout while waiting for event "{event}"'
)
Expand Down
2 changes: 1 addition & 1 deletion playwright/_impl/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ def expect_event(
) -> EventContextManagerImpl:
if timeout is None:
timeout = self._timeout_settings.timeout()
wait_helper = WaitHelper(self, f"expect_event({event})")
wait_helper = WaitHelper(self, f"page.expect_event({event})")
wait_helper.reject_on_timeout(
timeout, f'Timeout while waiting for event "{event}"'
)
Expand Down
6 changes: 3 additions & 3 deletions playwright/_impl/_sync_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ def __init__(self, impl_obj: Any) -> None:
def __str__(self) -> str:
return self._impl_obj.__str__()

def _sync(self, coro: Awaitable) -> Any:
stack_trace = traceback.extract_stack()
def _sync(self, api_name: str, coro: Awaitable) -> Any:
g_self = greenlet.getcurrent()
task = self._loop.create_task(coro)
setattr(task, "__pw_stack_trace__", stack_trace)
setattr(task, "__pw_api_name__", api_name)
setattr(task, "__pw_stack_trace__", traceback.extract_stack())

def callback(result: Any) -> None:
g_self.switch()
Expand Down
Loading