Skip to content

Commit 55b2800

Browse files
authored
chore: more stack-related performance improvements (#2844)
1 parent 871149c commit 55b2800

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

playwright/_impl/_connection.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def _send_message_to_server(
333333
task = asyncio.current_task(self._loop)
334334
callback.stack_trace = cast(
335335
traceback.StackSummary,
336-
getattr(task, "__pw_stack_trace__", traceback.extract_stack()),
336+
getattr(task, "__pw_stack_trace__", traceback.extract_stack(limit=10)),
337337
)
338338
callback.no_reply = no_reply
339339
self._callbacks[id] = callback
@@ -387,9 +387,7 @@ def dispatch(self, msg: ParsedMessagePayload) -> None:
387387
parsed_error = parse_error(
388388
error["error"], format_call_log(msg.get("log")) # type: ignore
389389
)
390-
parsed_error._stack = "".join(
391-
traceback.format_list(callback.stack_trace)[-10:]
392-
)
390+
parsed_error._stack = "".join(callback.stack_trace.format())
393391
callback.future.set_exception(parsed_error)
394392
else:
395393
result = self._replace_guids_with_channels(msg.get("result"))

playwright/_impl/_network.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ async def _race_with_page_close(self, future: Coroutine) -> None:
530530
setattr(
531531
fut,
532532
"__pw_stack__",
533-
getattr(asyncio.current_task(self._loop), "__pw_stack__", inspect.stack()),
533+
getattr(asyncio.current_task(self._loop), "__pw_stack__", inspect.stack(0)),
534534
)
535535
target_closed_future = self.request._target_closed_future()
536536
await asyncio.wait(

playwright/_impl/_path_utils.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
import inspect
1616
from pathlib import Path
17+
from types import FrameType
18+
from typing import cast
1719

1820

1921
def get_file_dirname() -> Path:
2022
"""Returns the callee (`__file__`) directory name"""
21-
frame = inspect.stack()[1]
22-
module = inspect.getmodule(frame[0])
23+
frame = cast(FrameType, inspect.currentframe()).f_back
24+
module = inspect.getmodule(frame)
2325
assert module
2426
assert module.__file__
2527
return Path(module.__file__).parent.absolute()

playwright/_impl/_sync_base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def _sync(
105105

106106
g_self = greenlet.getcurrent()
107107
task: asyncio.tasks.Task[Any] = self._loop.create_task(coro)
108-
setattr(task, "__pw_stack__", inspect.stack())
109-
setattr(task, "__pw_stack_trace__", traceback.extract_stack())
108+
setattr(task, "__pw_stack__", inspect.stack(0))
109+
setattr(task, "__pw_stack_trace__", traceback.extract_stack(limit=10))
110110

111111
task.add_done_callback(lambda _: g_self.switch())
112112
while not task.done():

0 commit comments

Comments
 (0)