Skip to content

Commit a80f3ff

Browse files
authored
fix(listeners): fix removing listeners (microsoft#272)
1 parent a265102 commit a80f3ff

File tree

9 files changed

+62
-28
lines changed

9 files changed

+62
-28
lines changed

playwright/async_api.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3431,11 +3431,6 @@ def video(self) -> typing.Union["Video", NoneType]:
34313431
"""
34323432
return mapping.from_impl_nullable(self._impl_obj.video)
34333433

3434-
def remove_listener(self, event: str, f: typing.Any) -> NoneType:
3435-
return mapping.from_maybe_impl(
3436-
self._impl_obj.remove_listener(event=event, f=mapping.to_impl(f))
3437-
)
3438-
34393434
async def opener(self) -> typing.Union["Page", NoneType]:
34403435
"""Page.opener
34413436

playwright/async_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ def once(self, event_name: str, handler: Any) -> None:
7171
self._impl_obj.once(event_name, self._wrap_handler(handler))
7272

7373
def remove_listener(self, event_name: str, handler: Any) -> None:
74-
self._impl_obj.remove_listener(event_name, handler)
74+
self._impl_obj.remove_listener(event_name, self._wrap_handler(handler))

playwright/page.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -777,22 +777,6 @@ async def waitForFunction(
777777
def workers(self) -> List["Worker"]:
778778
return self._workers.copy()
779779

780-
# on(event: str | symbol, listener: Listener): self {
781-
# if (event === Page.Events.FileChooser) {
782-
# if (!self.listenerCount(event))
783-
# self._channel.setFileChooserInterceptedNoReply({ intercepted: True });
784-
# }
785-
# super.on(event, listener);
786-
# return self;
787-
# }
788-
789-
# removeListener(event: str | symbol, listener: Listener): self {
790-
# super.removeListener(event, listener);
791-
# if (event === Page.Events.FileChooser && !self.listenerCount(event))
792-
# self._channel.setFileChooserInterceptedNoReply({ intercepted: False });
793-
# return self;
794-
# }
795-
796780
async def pdf(
797781
self,
798782
scale: int = None,

playwright/sync_api.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3563,11 +3563,6 @@ def video(self) -> typing.Union["Video", NoneType]:
35633563
"""
35643564
return mapping.from_impl_nullable(self._impl_obj.video)
35653565

3566-
def remove_listener(self, event: str, f: typing.Any) -> NoneType:
3567-
return mapping.from_maybe_impl(
3568-
self._impl_obj.remove_listener(event=event, f=mapping.to_impl(f))
3569-
)
3570-
35713566
def opener(self) -> typing.Union["Page", NoneType]:
35723567
"""Page.opener
35733568

playwright/sync_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def once(self, event_name: str, handler: Any) -> None:
118118
self._impl_obj.once(event_name, self._wrap_handler(handler))
119119

120120
def remove_listener(self, event_name: str, handler: Any) -> None:
121-
self._impl_obj.remove_listener(event_name, handler)
121+
self._impl_obj.remove_listener(event_name, self._wrap_handler(handler))
122122

123123
def _gather(self, *actions: Callable) -> List[Any]:
124124
g_self = greenlet.getcurrent()

scripts/generate_async_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def generate(t: Any) -> None:
7878
not name.startswith("_")
7979
and isinstance(value, FunctionType)
8080
and "expect_" not in name
81+
and "remove_listener" != name
8182
):
8283
print("")
8384
if inspect.iscoroutinefunction(value):

scripts/generate_sync_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def generate(t: Any) -> None:
7878
not name.startswith("_")
7979
and isinstance(value, FunctionType)
8080
and "expect_" not in name
81+
and "remove_listener" != name
8182
):
8283
print("")
8384
print(

tests/async/test_listeners.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
async def test_listeners(page, server):
17+
log = []
18+
19+
def print_response(response):
20+
log.append(response)
21+
22+
page.on("response", print_response)
23+
await page.goto(f"{server.PREFIX}/input/textarea.html")
24+
assert len(log) > 0
25+
page.remove_listener("response", print_response)
26+
27+
log = []
28+
await page.goto(f"{server.PREFIX}/input/textarea.html")
29+
assert len(log) == 0

tests/sync/test_listeners.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def test_listeners(page, server):
17+
log = []
18+
19+
def print_response(response):
20+
log.append(response)
21+
22+
page.on("response", print_response)
23+
page.goto(f"{server.PREFIX}/input/textarea.html")
24+
assert len(log) > 0
25+
page.remove_listener("response", print_response)
26+
27+
log = []
28+
page.goto(f"{server.PREFIX}/input/textarea.html")
29+
assert len(log) == 0

0 commit comments

Comments
 (0)