Skip to content

_connect.py: Don't request context when inspecting stack #2835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 30, 2025
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
16 changes: 8 additions & 8 deletions playwright/_impl/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,7 @@ def _send_message_to_server(
"params": self._replace_channels_with_guids(params),
"metadata": metadata,
}
if (
self._tracing_count > 0
and frames
and frames
and object._guid != "localUtils"
):
if self._tracing_count > 0 and frames and object._guid != "localUtils":
self.local_utils.add_stack_to_tracing_no_reply(id, frames)

self._transport.send(message)
Expand Down Expand Up @@ -519,7 +514,10 @@ async def wrap_api_call(
if self._api_zone.get():
return await cb()
task = asyncio.current_task(self._loop)
st: List[inspect.FrameInfo] = getattr(task, "__pw_stack__", inspect.stack())
st: List[inspect.FrameInfo] = getattr(
task, "__pw_stack__", None
) or inspect.stack(0)

parsed_st = _extract_stack_trace_information_from_stack(st, is_internal)
self._api_zone.set(parsed_st)
try:
Expand All @@ -535,7 +533,9 @@ def wrap_api_call_sync(
if self._api_zone.get():
return cb()
task = asyncio.current_task(self._loop)
st: List[inspect.FrameInfo] = getattr(task, "__pw_stack__", inspect.stack())
st: List[inspect.FrameInfo] = getattr(
task, "__pw_stack__", None
) or inspect.stack(0)
parsed_st = _extract_stack_trace_information_from_stack(st, is_internal)
self._api_zone.set(parsed_st)
try:
Expand Down
12 changes: 12 additions & 0 deletions tests/async/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,15 @@ async def raise_exception() -> None:
assert "Something went wrong" in str(exc_info.value.exceptions[0])
assert isinstance(exc_info.value.exceptions[0], ValueError)
assert await page.evaluate("() => 11 * 11") == 121


async def test_should_return_proper_api_name_on_error(page: Page) -> None:
try:
await page.evaluate("does_not_exist")

assert (
False
), "Accessing undefined JavaScript variable should have thrown exception"
except Exception as error:
# Each browser returns slightly different error messages, but they should all start with "Page.evaluate:", because that was the Playwright method where the error originated
assert str(error).startswith("Page.evaluate:")
12 changes: 12 additions & 0 deletions tests/sync/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,15 @@ def test_call_sync_method_after_playwright_close_with_own_loop(
p.start()
p.join()
assert p.exitcode == 0


def test_should_return_proper_api_name_on_error(page: Page) -> None:
try:
page.evaluate("does_not_exist")

assert (
False
), "Accessing undefined JavaScript variable should have thrown exception"
except Exception as error:
# Each browser returns slightly different error messages, but they should all start with "Page.evaluate:", because that was the Playwright method where the error originated
assert str(error).startswith("Page.evaluate:")
Loading