Skip to content

Commit fe886f7

Browse files
authored
chore(perf): don't request inspect context when inspecting stack (#2835)
1 parent 7f5db36 commit fe886f7

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

playwright/_impl/_connection.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,7 @@ def _send_message_to_server(
362362
"params": self._replace_channels_with_guids(params),
363363
"metadata": metadata,
364364
}
365-
if (
366-
self._tracing_count > 0
367-
and frames
368-
and frames
369-
and object._guid != "localUtils"
370-
):
365+
if self._tracing_count > 0 and frames and object._guid != "localUtils":
371366
self.local_utils.add_stack_to_tracing_no_reply(id, frames)
372367

373368
self._transport.send(message)
@@ -519,7 +514,10 @@ async def wrap_api_call(
519514
if self._api_zone.get():
520515
return await cb()
521516
task = asyncio.current_task(self._loop)
522-
st: List[inspect.FrameInfo] = getattr(task, "__pw_stack__", inspect.stack())
517+
st: List[inspect.FrameInfo] = getattr(
518+
task, "__pw_stack__", None
519+
) or inspect.stack(0)
520+
523521
parsed_st = _extract_stack_trace_information_from_stack(st, is_internal)
524522
self._api_zone.set(parsed_st)
525523
try:
@@ -535,7 +533,9 @@ def wrap_api_call_sync(
535533
if self._api_zone.get():
536534
return cb()
537535
task = asyncio.current_task(self._loop)
538-
st: List[inspect.FrameInfo] = getattr(task, "__pw_stack__", inspect.stack())
536+
st: List[inspect.FrameInfo] = getattr(
537+
task, "__pw_stack__", None
538+
) or inspect.stack(0)
539539
parsed_st = _extract_stack_trace_information_from_stack(st, is_internal)
540540
self._api_zone.set(parsed_st)
541541
try:

tests/async/test_asyncio.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,15 @@ async def raise_exception() -> None:
8787
assert "Something went wrong" in str(exc_info.value.exceptions[0])
8888
assert isinstance(exc_info.value.exceptions[0], ValueError)
8989
assert await page.evaluate("() => 11 * 11") == 121
90+
91+
92+
async def test_should_return_proper_api_name_on_error(page: Page) -> None:
93+
try:
94+
await page.evaluate("does_not_exist")
95+
96+
assert (
97+
False
98+
), "Accessing undefined JavaScript variable should have thrown exception"
99+
except Exception as error:
100+
# 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
101+
assert str(error).startswith("Page.evaluate:")

tests/sync/test_sync.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,15 @@ def test_call_sync_method_after_playwright_close_with_own_loop(
346346
p.start()
347347
p.join()
348348
assert p.exitcode == 0
349+
350+
351+
def test_should_return_proper_api_name_on_error(page: Page) -> None:
352+
try:
353+
page.evaluate("does_not_exist")
354+
355+
assert (
356+
False
357+
), "Accessing undefined JavaScript variable should have thrown exception"
358+
except Exception as error:
359+
# 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
360+
assert str(error).startswith("Page.evaluate:")

0 commit comments

Comments
 (0)