From f426da73852d1b04393d6e46d8b9560418c40266 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Fri, 1 Jun 2018 09:17:54 +0300 Subject: [PATCH] Uvloop 0.10 workaround (#3046) * Dont install child watcher explicitly * Setup a policy before creating a loop --- aiohttp/pytest_plugin.py | 10 ++++++---- aiohttp/test_utils.py | 8 +++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/aiohttp/pytest_plugin.py b/aiohttp/pytest_plugin.py index 05984f525b8..064a39f449e 100644 --- a/aiohttp/pytest_plugin.py +++ b/aiohttp/pytest_plugin.py @@ -169,13 +169,13 @@ def pytest_generate_tests(metafunc): return loops = metafunc.config.option.aiohttp_loop - avail_factories = {'pyloop': asyncio.new_event_loop} + avail_factories = {'pyloop': asyncio.DefaultEventLoopPolicy} if uvloop is not None: # pragma: no cover - avail_factories['uvloop'] = uvloop.new_event_loop + avail_factories['uvloop'] = uvloop.EventLoopPolicy if tokio is not None: # pragma: no cover - avail_factories['tokio'] = tokio.new_event_loop + avail_factories['tokio'] = tokio.EventLoopPolicy if loops == 'all': loops = 'pyloop,uvloop?,tokio?' @@ -200,7 +200,9 @@ def pytest_generate_tests(metafunc): @pytest.fixture def loop(loop_factory, fast, loop_debug): """Return an instance of the event loop.""" - with loop_context(loop_factory, fast=fast) as _loop: + policy = loop_factory() + asyncio.set_event_loop_policy(policy) + with loop_context(fast=fast) as _loop: if loop_debug: _loop.set_debug(True) # pragma: no cover asyncio.set_event_loop(_loop) diff --git a/aiohttp/test_utils.py b/aiohttp/test_utils.py index 510dcb2c428..e225c2a26dd 100644 --- a/aiohttp/test_utils.py +++ b/aiohttp/test_utils.py @@ -414,8 +414,14 @@ def setup_test_loop(loop_factory=asyncio.new_event_loop): once they are done with the loop. """ loop = loop_factory() + try: + module = loop.__class__.__module__ + skip_watcher = 'uvloop' in module + except AttributeError: # pragma: no cover + # Just in case + skip_watcher = True asyncio.set_event_loop(loop) - if sys.platform != "win32": + if sys.platform != "win32" and not skip_watcher: policy = asyncio.get_event_loop_policy() watcher = asyncio.SafeChildWatcher() watcher.attach_loop(loop)