Skip to content

More stack-related performance improvements #2844

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 3 commits into from
May 12, 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
6 changes: 2 additions & 4 deletions playwright/_impl/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def _send_message_to_server(
task = asyncio.current_task(self._loop)
callback.stack_trace = cast(
traceback.StackSummary,
getattr(task, "__pw_stack_trace__", traceback.extract_stack()),
getattr(task, "__pw_stack_trace__", traceback.extract_stack(limit=10)),
)
callback.no_reply = no_reply
self._callbacks[id] = callback
Expand Down Expand Up @@ -387,9 +387,7 @@ def dispatch(self, msg: ParsedMessagePayload) -> None:
parsed_error = parse_error(
error["error"], format_call_log(msg.get("log")) # type: ignore
)
parsed_error._stack = "".join(
traceback.format_list(callback.stack_trace)[-10:]
)
parsed_error._stack = "".join(callback.stack_trace.format())
callback.future.set_exception(parsed_error)
else:
result = self._replace_guids_with_channels(msg.get("result"))
Expand Down
2 changes: 1 addition & 1 deletion playwright/_impl/_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ async def _race_with_page_close(self, future: Coroutine) -> None:
setattr(
fut,
"__pw_stack__",
getattr(asyncio.current_task(self._loop), "__pw_stack__", inspect.stack()),
getattr(asyncio.current_task(self._loop), "__pw_stack__", inspect.stack(0)),
)
target_closed_future = self.request._target_closed_future()
await asyncio.wait(
Expand Down
6 changes: 4 additions & 2 deletions playwright/_impl/_path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

import inspect
from pathlib import Path
from types import FrameType
from typing import cast


def get_file_dirname() -> Path:
"""Returns the callee (`__file__`) directory name"""
frame = inspect.stack()[1]
module = inspect.getmodule(frame[0])
frame = cast(FrameType, inspect.currentframe()).f_back
module = inspect.getmodule(frame)
assert module
assert module.__file__
return Path(module.__file__).parent.absolute()
4 changes: 2 additions & 2 deletions playwright/_impl/_sync_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def _sync(

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

task.add_done_callback(lambda _: g_self.switch())
while not task.done():
Expand Down
Loading