@@ -50,11 +50,6 @@ class _instances:
5050 reactor = None
5151
5252
53- class _tracking :
54- async_yield_fixture_cache = {}
55- to_be_torn_down = []
56-
57-
5853def _deprecate (deprecated , recommended ):
5954 def decorator (f ):
6055 @functools .wraps (f )
@@ -282,6 +277,13 @@ def pytest_fixture_setup(fixturedef, request):
282277 return not None
283278
284279
280+ def async_yield_fixture_finalizer (coroutine ):
281+ _run_inline_callbacks (
282+ tear_it_down ,
283+ defer .ensureDeferred (coroutine .__anext__ ()),
284+ )
285+
286+
285287@defer .inlineCallbacks
286288def _async_pytest_fixture_setup (fixturedef , request , mark ):
287289 """Setup an async or async yield fixture."""
@@ -299,15 +301,14 @@ def _async_pytest_fixture_setup(fixturedef, request, mark):
299301 elif mark == 'async_yield_fixture' :
300302 coroutine = fixture_function (** kwargs )
301303
302- finalizer = functools .partial (
303- _tracking .to_be_torn_down .append ,
304- coroutine ,
304+ request .addfinalizer (
305+ functools .partial (
306+ async_yield_fixture_finalizer ,
307+ coroutine = coroutine ,
308+ ),
305309 )
306- request .addfinalizer (finalizer )
307310
308- arg_value = yield defer .ensureDeferred (
309- coroutine .__anext__ (),
310- )
311+ arg_value = yield defer .ensureDeferred (coroutine .__anext__ ())
311312 else :
312313 raise UnrecognizedCoroutineMarkError .from_mark (mark = mark )
313314
@@ -348,22 +349,6 @@ def in_reactor(d, f, *args):
348349 blockingCallFromThread (_instances .reactor , f , * args )
349350
350351
351- @pytest .hookimpl (hookwrapper = True )
352- def pytest_runtest_teardown (item ):
353- """Tear down collected async yield fixtures."""
354- yield
355-
356- deferreds = []
357- while len (_tracking .to_be_torn_down ) > 0 :
358- coroutine = _tracking .to_be_torn_down .pop (0 )
359- deferred = defer .ensureDeferred (coroutine .__anext__ ())
360-
361- deferreds .append (deferred )
362-
363- for deferred in deferreds :
364- _run_inline_callbacks (tear_it_down , deferred )
365-
366-
367352def pytest_pyfunc_call (pyfuncitem ):
368353 """Interface to async test call handler."""
369354 # TODO: only handle 'our' tests? what is the point of handling others?
0 commit comments