Skip to content

Commit 2a62bcd

Browse files
committed
Switch context collections to sets due to duplicate events
1 parent aac337b commit 2a62bcd

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

playwright/_impl/_browser.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@
1414

1515
from pathlib import Path
1616
from types import SimpleNamespace
17-
from typing import TYPE_CHECKING, Dict, List, Optional, Pattern, Sequence, Union, cast
17+
from typing import (
18+
TYPE_CHECKING,
19+
Dict,
20+
List,
21+
Optional,
22+
Pattern,
23+
Sequence,
24+
Set,
25+
Union,
26+
cast,
27+
)
1828

1929
from playwright._impl._api_structures import (
2030
ClientCertificate,
@@ -60,7 +70,7 @@ def __init__(
6070
self._should_close_connection_on_close = False
6171
self._cr_tracing_path: Optional[str] = None
6272

63-
self._contexts: List[BrowserContext] = []
73+
self._contexts: Set[BrowserContext] = set()
6474
self._traces_dir: Optional[str] = None
6575
self._channel.on(
6676
"context",
@@ -88,7 +98,7 @@ def _connect_to_browser_type(
8898

8999
def _did_create_context(self, context: BrowserContext) -> None:
90100
context._browser = self
91-
self._contexts.append(context)
101+
self._contexts.add(context)
92102
# Note: when connecting to a browser, initial contexts arrive before `_browserType` is set,
93103
# and will be configured later in `ConnectToBrowserType`.
94104
if self._browser_type:
@@ -97,15 +107,15 @@ def _did_create_context(self, context: BrowserContext) -> None:
97107
def _setup_browser_context(self, context: BrowserContext) -> None:
98108
context._tracing._traces_dir = self._traces_dir
99109
print("Appending context to selectors")
100-
self._browser_type._playwright.selectors._contextsForSelectors.append(context)
110+
self._browser_type._playwright.selectors._contextsForSelectors.add(context)
101111

102112
def _on_close(self) -> None:
103113
self._is_connected = False
104114
self.emit(Browser.Events.Disconnected, self)
105115

106116
@property
107117
def contexts(self) -> List[BrowserContext]:
108-
return self._contexts.copy()
118+
return list(self._contexts)
109119

110120
@property
111121
def browser_type(self) -> "BrowserType":

playwright/_impl/_browser_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ def _on_close(self) -> None:
574574
if self._browser:
575575
try:
576576
self._browser._contexts.remove(self)
577-
except ValueError:
577+
except KeyError:
578578
print("Context already removed from browser contexts")
579579
pass
580580
try:
@@ -586,7 +586,7 @@ def _on_close(self) -> None:
586586
"Successfully removed context from browser selectors",
587587
self._browser._browser_type._playwright.selectors._contextsForSelectors,
588588
)
589-
except ValueError:
589+
except KeyError:
590590
print("Context already removed from browser selectors")
591591
pass
592592

playwright/_impl/_selectors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import asyncio
1616
from pathlib import Path
17-
from typing import Any, Dict, List, Optional, Union
17+
from typing import Any, Dict, List, Optional, Set, Union
1818

1919
from playwright._impl._browser_context import BrowserContext
2020
from playwright._impl._errors import Error
@@ -25,7 +25,7 @@
2525
class Selectors:
2626
def __init__(self, loop: asyncio.AbstractEventLoop, dispatcher_fiber: Any) -> None:
2727
self._loop = loop
28-
self._contextsForSelectors: List[BrowserContext] = []
28+
self._contextsForSelectors: Set[BrowserContext] = set()
2929
self._selectorEngines: List[Dict] = []
3030
self._dispatcher_fiber = dispatcher_fiber
3131
self._testIdAttributeName: Optional[str] = None

0 commit comments

Comments
 (0)