@@ -49,6 +49,11 @@ class _instances:
49
49
reactor = None
50
50
51
51
52
+ class _tracking :
53
+ async_yield_fixture_cache = {}
54
+ to_be_torn_down = []
55
+
56
+
52
57
def _deprecate (deprecated , recommended ):
53
58
def decorator (f ):
54
59
@functools .wraps (f )
@@ -169,9 +174,6 @@ def pytest_fixture_setup(fixturedef, request):
169
174
return True
170
175
171
176
172
- async_yield_fixture_cache = {}
173
-
174
-
175
177
@defer .inlineCallbacks
176
178
def _pytest_fixture_setup (fixturedef , request , mark ):
177
179
fixture_function = fixturedef .func
@@ -188,7 +190,7 @@ def _pytest_fixture_setup(fixturedef, request, mark):
188
190
elif mark == 'async_yield_fixture' :
189
191
coroutine = fixture_function (** kwargs )
190
192
# TODO: use request.addfinalizer() instead?
191
- async_yield_fixture_cache [request .param_index ] = coroutine
193
+ _tracking . async_yield_fixture_cache [request .param_index ] = coroutine
192
194
arg_value = yield defer .ensureDeferred (
193
195
coroutine .__anext__ (),
194
196
)
@@ -202,14 +204,18 @@ def _pytest_fixture_setup(fixturedef, request, mark):
202
204
203
205
# TODO: but don't we want to do the finalizer? not wait until post it?
204
206
def pytest_fixture_post_finalizer (fixturedef , request ):
205
- maybe_coroutine = async_yield_fixture_cache .pop (request .param_index , None )
207
+ maybe_coroutine = _tracking .async_yield_fixture_cache .pop (
208
+ request .param_index ,
209
+ None ,
210
+ )
206
211
207
212
if maybe_coroutine is None :
208
213
return None
209
214
210
215
coroutine = maybe_coroutine
211
216
212
- to_be_torn_down .append (defer .ensureDeferred (coroutine .__anext__ ()))
217
+ deferred = defer .ensureDeferred (coroutine .__anext__ ())
218
+ _tracking .to_be_torn_down .append (deferred )
213
219
return None
214
220
215
221
@@ -229,8 +235,6 @@ def tear_it_down(deferred):
229
235
)
230
236
231
237
232
- to_be_torn_down = []
233
-
234
238
# TODO: https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_runtest_protocol
235
239
# claims it should also take a nextItem but that triggers a direct error
236
240
@@ -256,14 +260,18 @@ def in_reactor(d, f, *args):
256
260
def pytest_runtest_teardown (item ):
257
261
yield
258
262
259
- while len (to_be_torn_down ) > 0 :
260
- deferred = to_be_torn_down .pop (0 )
263
+ while len (_tracking . to_be_torn_down ) > 0 :
264
+ deferred = _tracking . to_be_torn_down .pop (0 )
261
265
run_inline_callbacks (tear_it_down , deferred )
262
266
263
267
264
268
@defer .inlineCallbacks
265
269
def _pytest_pyfunc_call (pyfuncitem ):
266
- kwargs = {name : value for name , value in pyfuncitem .funcargs .items () if name in pyfuncitem ._fixtureinfo .argnames }
270
+ kwargs = {
271
+ name : value
272
+ for name , value in pyfuncitem .funcargs .items ()
273
+ if name in pyfuncitem ._fixtureinfo .argnames
274
+ }
267
275
result = yield pyfuncitem .obj (** kwargs )
268
276
defer .returnValue (result )
269
277
0 commit comments