Spaces:
Paused
Paused
File size: 1,049 Bytes
90694b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | """Utilities for consistently releasing Playwright resources."""
from __future__ import annotations
from collections.abc import Callable
from typing import Any
def _call_close(obj: Any, method_name: str) -> None:
method: Callable[[], Any] | None = getattr(obj, method_name, None)
if callable(method):
method()
def close_playwright_objects(
page: Any = None,
context: Any = None,
browser: Any = None,
playwright: Any = None,
*,
logger: Any = None,
label: str = "playwright",
) -> None:
"""Best-effort close for Playwright objects in dependency order."""
for name, obj, method_name in (
("page", page, "close"),
("context", context, "close"),
("browser", browser, "close"),
("playwright", playwright, "stop"),
):
if obj is None:
continue
try:
_call_close(obj, method_name)
except Exception as exc:
if logger is not None:
logger.debug("[%s] close %s failed: %s", label, name, exc)
|