Skip to content

Commit 0d38538

Browse files
committed
fix: don't set attr on bound method (#539)
1 parent a4ec658 commit 0d38538

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

playwright/_impl/_impl_to_api_mapping.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
# limitations under the License.
1414

1515
import inspect
16-
from typing import Any, Callable, Dict, List, Optional
16+
from types import MethodType
17+
from typing import Any, Callable, Dict, List, Optional, cast
1718

1819
from playwright._impl._api_types import Error
1920

20-
INSTANCE_ATTR = "_pw_api_instance"
21+
API_ATTR = "_pw_api_instance_"
22+
IMPL_ATTR = "_pw_impl_instance_"
2123

2224

2325
class ImplWrapper:
@@ -41,10 +43,10 @@ def from_maybe_impl(self, obj: Any) -> Any:
4143
return [self.from_maybe_impl(item) for item in obj]
4244
api_class = self._mapping.get(type(obj))
4345
if api_class:
44-
api_instance = getattr(obj, INSTANCE_ATTR, None)
46+
api_instance = getattr(obj, API_ATTR, None)
4547
if not api_instance:
4648
api_instance = api_class(obj)
47-
setattr(obj, INSTANCE_ATTR, api_instance)
49+
setattr(obj, API_ATTR, api_instance)
4850
return api_instance
4951
else:
5052
return obj
@@ -85,8 +87,21 @@ def wrapper_func(*args: Any) -> Any:
8587
*list(map(lambda a: self.from_maybe_impl(a), args))[:arg_count]
8688
)
8789

88-
wrapper = getattr(handler, INSTANCE_ATTR, None)
90+
if inspect.ismethod(handler):
91+
wrapper = getattr(
92+
cast(MethodType, handler).__self__, IMPL_ATTR + handler.__name__, None
93+
)
94+
if not wrapper:
95+
wrapper = wrapper_func
96+
setattr(
97+
cast(MethodType, handler).__self__,
98+
IMPL_ATTR + handler.__name__,
99+
wrapper,
100+
)
101+
return wrapper
102+
103+
wrapper = getattr(handler, IMPL_ATTR, None)
89104
if not wrapper:
90105
wrapper = wrapper_func
91-
setattr(handler, INSTANCE_ATTR, wrapper)
106+
setattr(handler, IMPL_ATTR, wrapper)
92107
return wrapper

tests/async/test_network.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ async def test_page_events_request_should_fire_for_navigation_requests(
7373
assert len(requests) == 1
7474

7575

76+
async def test_page_events_request_should_accept_method(page: Page, server):
77+
class Log:
78+
def __init__(self):
79+
self.requests = []
80+
81+
def handle(self, request):
82+
self.requests.append(request)
83+
84+
log = Log()
85+
page.on("request", log.handle)
86+
await page.goto(server.EMPTY_PAGE)
87+
assert len(log.requests) == 1
88+
89+
7690
async def test_page_events_request_should_fire_for_iframes(page, server, utils):
7791
requests = []
7892
page.on("request", lambda r: requests.append(r))

0 commit comments

Comments
 (0)