Blob Blame History Raw
From 4b3294d5075489fc9ebf8046c3604e88a3679275 Mon Sep 17 00:00:00 2001
From: Julien Enselme <jenselme@localhost.localdomain>
Date: Mon, 10 Oct 2016 15:11:56 +0200
Subject: [PATCH] Revert "Event loop policy now set by a fixture setup hook
 wrapper."

This reverts commit 8dfe3313d7c21be70006c80fcae15ed2b0dd94e8.
---
 pytest_asyncio/plugin.py         | 41 +++++++++++++++-------------------------
 setup.py                         |  2 +-
 tests/test_dependent_fixtures.py | 19 -------------------
 3 files changed, 16 insertions(+), 46 deletions(-)

diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py
index 84a2547..92c8c2f 100644
--- a/pytest_asyncio/plugin.py
+++ b/pytest_asyncio/plugin.py
@@ -49,29 +49,6 @@ def pytest_pycollect_makeitem(collector, name, obj):
             return list(collector._genfunctions(name, obj))
 
 
-@pytest.hookimpl(hookwrapper=True)
-def pytest_fixture_setup(fixturedef, request):
-    """Adjust the event loop policy when an event loop is produced."""
-    outcome = yield
-
-    if fixturedef.argname == "event_loop" and 'asyncio' in request.keywords:
-        loop = outcome.get_result()
-        for kw in _markers_2_fixtures.keys():
-            if kw not in request.keywords:
-                continue
-            forbid_global_loop = (request.keywords[kw].kwargs
-                                  .get('forbid_global_loop', False))
-
-            policy = asyncio.get_event_loop_policy()
-            if forbid_global_loop:
-                asyncio.set_event_loop_policy(ForbiddenEventLoopPolicy())
-                fixturedef.addfinalizer(lambda: asyncio.set_event_loop_policy(policy))
-            else:
-                policy.set_event_loop(loop)
-
-    return outcome
-
-
 @pytest.mark.tryfirst
 def pytest_pyfunc_call(pyfuncitem):
     """
@@ -82,12 +59,24 @@ def pytest_pyfunc_call(pyfuncitem):
         if marker_name in pyfuncitem.keywords:
             event_loop = pyfuncitem.funcargs[fixture_name]
 
+            forbid_global_loop = pyfuncitem.keywords[marker_name].kwargs.get('forbid_global_loop')
+
+            policy = asyncio.get_event_loop_policy()
+            if forbid_global_loop:
+                asyncio.set_event_loop_policy(ForbiddenEventLoopPolicy())
+            else:
+                policy.set_event_loop(event_loop)
+
             funcargs = pyfuncitem.funcargs
             testargs = {arg: funcargs[arg]
                         for arg in pyfuncitem._fixtureinfo.argnames}
-            event_loop.run_until_complete(
-                asyncio.async(pyfuncitem.obj(**testargs), loop=event_loop))
-            return True
+            try:
+                event_loop.run_until_complete(
+                    asyncio.async(pyfuncitem.obj(**testargs), loop=event_loop))
+                return True
+            finally:
+                if forbid_global_loop:
+                    asyncio.set_event_loop_policy(policy)
 
 
 def pytest_runtest_setup(item):
diff --git a/setup.py b/setup.py
index 8dbcb17..b089eb8 100644
--- a/setup.py
+++ b/setup.py
@@ -42,7 +42,7 @@ setup(
         "Framework :: Pytest",
     ],
     install_requires=[
-        'pytest >= 3.0.2',
+        'pytest',
     ],
     extras_require={
         ':python_version == "3.3"': ['asyncio']
diff --git a/tests/test_dependent_fixtures.py b/tests/test_dependent_fixtures.py
index 018c482..b12ef3b 100644
--- a/tests/test_dependent_fixtures.py
+++ b/tests/test_dependent_fixtures.py
@@ -7,22 +7,3 @@ import pytest
 def test_dependent_fixture(dependent_fixture):
     """Test a dependent fixture."""
     yield from asyncio.sleep(0.1)
-
-
-@pytest.fixture
-def assert_no_global_loop(event_loop):
-    """A dummy fixture, to assert the event loop policy has been set."""
-    assert event_loop
-
-    with pytest.raises(NotImplementedError):
-        asyncio.get_event_loop()
-    yield
-    with pytest.raises(NotImplementedError):
-        asyncio.get_event_loop()
-
-
-@pytest.mark.asyncio(forbid_global_loop=True)
-@asyncio.coroutine
-def test_fixture_no_global_loop(assert_no_global_loop):
-    """The fixture will assert the correct policy has been set."""
-    pass
\ No newline at end of file
-- 
2.9.3