diff --git a/playwright/_impl/_browser.py b/playwright/_impl/_browser.py index 62c9ba4ec..dc04e8cf7 100644 --- a/playwright/_impl/_browser.py +++ b/playwright/_impl/_browser.py @@ -125,7 +125,6 @@ async def new_context( self._contexts.append(context) context._browser = self context._options = params - context._tracing._local_utils = self._local_utils return context async def new_page( diff --git a/playwright/_impl/_browser_type.py b/playwright/_impl/_browser_type.py index f38aad50d..500e8b138 100644 --- a/playwright/_impl/_browser_type.py +++ b/playwright/_impl/_browser_type.py @@ -90,7 +90,6 @@ async def launch( browser = cast( Browser, from_channel(await self._channel.send("launch", params)) ) - browser._local_utils = self._playwright._utils return browser async def launch_persistent_context( @@ -150,7 +149,6 @@ async def launch_persistent_context( from_channel(await self._channel.send("launchPersistentContext", params)), ) context._options = params - context.tracing._local_utils = self._playwright._utils return context async def connect_over_cdp( @@ -163,7 +161,6 @@ async def connect_over_cdp( params = locals_to_params(locals()) response = await self._channel.send_return_as_dict("connectOverCDP", params) browser = cast(Browser, from_channel(response["browser"])) - browser._local_utils = self._playwright._utils default_context = cast( Optional[BrowserContext], @@ -194,6 +191,7 @@ async def connect( self._connection._object_factory, transport, self._connection._loop, + local_utils=self._connection.local_utils, ) connection.mark_as_remote() connection._is_sync = self._connection._is_sync @@ -216,7 +214,6 @@ async def connect( assert pre_launched_browser browser = cast(Browser, from_channel(pre_launched_browser)) browser._should_close_connection_on_close = True - browser._local_utils = self._playwright._utils def handle_transport_close() -> None: for context in browser.contexts: diff --git a/playwright/_impl/_connection.py b/playwright/_impl/_connection.py index 36b529212..f19d02f91 100644 --- a/playwright/_impl/_connection.py +++ b/playwright/_impl/_connection.py @@ -28,6 +28,7 @@ from playwright._impl._transport import Transport if TYPE_CHECKING: + from playwright._impl._local_utils import LocalUtils from playwright._impl._playwright import Playwright @@ -109,7 +110,7 @@ def __init__( parent if isinstance(parent, ChannelOwner) else None ) self._objects: Dict[str, "ChannelOwner"] = {} - self._channel = Channel(self._connection, guid) + self._channel: Channel = Channel(self._connection, guid) self._channel._object = self self._initializer = initializer @@ -173,6 +174,7 @@ def __init__( object_factory: Callable[[ChannelOwner, str, str, Dict], ChannelOwner], transport: Transport, loop: asyncio.AbstractEventLoop, + local_utils: Optional["LocalUtils"] = None, ) -> None: super().__init__() self._dispatcher_fiber = dispatcher_fiber @@ -193,6 +195,12 @@ def __init__( self._api_zone: contextvars.ContextVar[Optional[Dict]] = contextvars.ContextVar( "ApiZone", default=None ) + self._local_utils: Optional["LocalUtils"] = local_utils + + @property + def local_utils(self) -> "LocalUtils": + assert self._local_utils + return self._local_utils def mark_as_remote(self) -> None: self.is_remote = True diff --git a/playwright/_impl/_fetch.py b/playwright/_impl/_fetch.py index 0e528ec34..cc0fc54d0 100644 --- a/playwright/_impl/_fetch.py +++ b/playwright/_impl/_fetch.py @@ -78,7 +78,6 @@ async def new_context( APIRequestContext, from_channel(await self.playwright._channel.send("newRequest", params)), ) - context._tracing._local_utils = self.playwright._utils return context diff --git a/playwright/_impl/_object_factory.py b/playwright/_impl/_object_factory.py index e04d1adec..f6dc4a260 100644 --- a/playwright/_impl/_object_factory.py +++ b/playwright/_impl/_object_factory.py @@ -71,7 +71,10 @@ def create_remote_object( if type == "JSHandle": return JSHandle(parent, type, guid, initializer) if type == "LocalUtils": - return LocalUtils(parent, type, guid, initializer) + local_utils = LocalUtils(parent, type, guid, initializer) + if not local_utils._connection._local_utils: + local_utils._connection._local_utils = local_utils + return local_utils if type == "Page": return Page(parent, type, guid, initializer) if type == "Playwright": diff --git a/playwright/_impl/_tracing.py b/playwright/_impl/_tracing.py index fe0fa1189..b6b0025ed 100644 --- a/playwright/_impl/_tracing.py +++ b/playwright/_impl/_tracing.py @@ -18,7 +18,6 @@ from playwright._impl._artifact import Artifact from playwright._impl._connection import ChannelOwner, from_nullable_channel from playwright._impl._helper import locals_to_params -from playwright._impl._local_utils import LocalUtils class Tracing(ChannelOwner): @@ -26,7 +25,6 @@ def __init__( self, parent: ChannelOwner, type: str, guid: str, initializer: Dict ) -> None: super().__init__(parent, type, guid, initializer) - _local_utils: LocalUtils async def start( self, @@ -86,4 +84,6 @@ async def _do_stop_chunk(self, file_path: Union[pathlib.Path, str] = None) -> No # Add local sources to the remote trace if necessary. if result.get("sourceEntries", []): - await self._local_utils.zip(file_path, result["sourceEntries"]) + await self._connection.local_utils.zip( + str(file_path), result["sourceEntries"] + )