From d8484207d721f5883ff5d060769dc0a453fdc410 Mon Sep 17 00:00:00 2001 From: miyakogi Date: Fri, 10 May 2019 08:07:46 +0900 Subject: [PATCH] Do not call async events implicitly --- pyppeteer/browser.py | 18 ++++++++++++++---- pyppeteer/network_manager.py | 7 ++++++- tests/test_page.py | 10 +++++++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/pyppeteer/browser.py b/pyppeteer/browser.py index 94647609..2fc297fd 100644 --- a/pyppeteer/browser.py +++ b/pyppeteer/browser.py @@ -44,9 +44,10 @@ def __init__(self, connection: Connection, contextIds: List[str], self._process = process self._screenshotTaskQueue: List = [] self._connection = connection + loop = self._connection._loop def _dummy_callback() -> Awaitable[None]: - fut = self._connection._loop.create_future() + fut = loop.create_future() fut.set_result(None) return fut @@ -64,9 +65,18 @@ def _dummy_callback() -> Awaitable[None]: self._connection.setClosedCallback( lambda: self.emit(Browser.Events.Disconnected) ) - self._connection.on('Target.targetCreated', self._targetCreated) - self._connection.on('Target.targetDestroyed', self._targetDestroyed) - self._connection.on('Target.targetInfoChanged', self._targetInfoChanged) # noqa: E501 + self._connection.on( + 'Target.targetCreated', + lambda event: loop.create_task(self._targetCreated(event)), + ) + self._connection.on( + 'Target.targetDestroyed', + lambda event: loop.create_task(self._targetDestroyed(event)), + ) + self._connection.on( + 'Target.targetInfoChanged', + lambda event: loop.create_task(self._targetInfoChanged(event)), + ) @property def process(self) -> Optional[Popen]: diff --git a/pyppeteer/network_manager.py b/pyppeteer/network_manager.py index 8b15efc7..372b5462 100644 --- a/pyppeteer/network_manager.py +++ b/pyppeteer/network_manager.py @@ -53,7 +53,12 @@ def __init__(self, client: CDPSession, frameManager: FrameManager) -> None: self._requestHashToRequestIds = Multimap() self._requestHashToInterceptionIds = Multimap() - self._client.on('Network.requestWillBeSent', self._onRequestWillBeSent) + self._client.on( + 'Network.requestWillBeSent', + lambda event: self._client._loop.create_task( + self._onRequestWillBeSent(event) + ), + ) self._client.on('Network.requestIntercepted', self._onRequestIntercepted) # noqa: E501 self._client.on('Network.requestServedFromCache', self._onRequestServedFromCache) # noqa: #501 self._client.on('Network.responseReceived', self._onResponseReceived) diff --git a/tests/test_page.py b/tests/test_page.py index 8f501913..c98ab9d7 100644 --- a/tests/test_page.py +++ b/tests/test_page.py @@ -1874,18 +1874,22 @@ async def test_frame(self): class TestEvents(BaseTestCase): @sync async def test_close_window_close(self): - newPagePromise = asyncio.get_event_loop().create_future() + loop = asyncio.get_event_loop() + newPagePromise = loop.create_future() async def page_created(target): page = await target.page() newPagePromise.set_result(page) - self.context.once('targetcreated', page_created) + self.context.once( + 'targetcreated', + lambda target: loop.create_task(page_created(target)), + ) await self.page.evaluate( 'window["newPage"] = window.open("about:blank")') newPage = await newPagePromise - closedPromise = asyncio.get_event_loop().create_future() + closedPromise = loop.create_future() newPage.on('close', lambda: closedPromise.set_result(True)) await self.page.evaluate('window["newPage"].close()') await closedPromise