Skip to content

Commit bcf7114

Browse files
committed
move globals into class for consistency
At the moment I do not recall why, but for consistency...
1 parent b10cfd3 commit bcf7114

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

pytest_twisted.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class _instances:
4949
reactor = None
5050

5151

52+
class _tracking:
53+
async_yield_fixture_cache = {}
54+
to_be_torn_down = []
55+
56+
5257
def _deprecate(deprecated, recommended):
5358
def decorator(f):
5459
@functools.wraps(f)
@@ -169,9 +174,6 @@ def pytest_fixture_setup(fixturedef, request):
169174
return True
170175

171176

172-
async_yield_fixture_cache = {}
173-
174-
175177
@defer.inlineCallbacks
176178
def _pytest_fixture_setup(fixturedef, request, mark):
177179
fixture_function = fixturedef.func
@@ -188,7 +190,7 @@ def _pytest_fixture_setup(fixturedef, request, mark):
188190
elif mark == 'async_yield_fixture':
189191
coroutine = fixture_function(**kwargs)
190192
# TODO: use request.addfinalizer() instead?
191-
async_yield_fixture_cache[request.param_index] = coroutine
193+
_tracking.async_yield_fixture_cache[request.param_index] = coroutine
192194
arg_value = yield defer.ensureDeferred(
193195
coroutine.__anext__(),
194196
)
@@ -202,14 +204,18 @@ def _pytest_fixture_setup(fixturedef, request, mark):
202204

203205
# TODO: but don't we want to do the finalizer? not wait until post it?
204206
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+
)
206211

207212
if maybe_coroutine is None:
208213
return None
209214

210215
coroutine = maybe_coroutine
211216

212-
to_be_torn_down.append(defer.ensureDeferred(coroutine.__anext__()))
217+
deferred = defer.ensureDeferred(coroutine.__anext__())
218+
_tracking.to_be_torn_down.append(deferred)
213219
return None
214220

215221

@@ -229,8 +235,6 @@ def tear_it_down(deferred):
229235
)
230236

231237

232-
to_be_torn_down = []
233-
234238
# TODO: https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_runtest_protocol
235239
# claims it should also take a nextItem but that triggers a direct error
236240

@@ -256,14 +260,18 @@ def in_reactor(d, f, *args):
256260
def pytest_runtest_teardown(item):
257261
yield
258262

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)
261265
run_inline_callbacks(tear_it_down, deferred)
262266

263267

264268
@defer.inlineCallbacks
265269
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+
}
267275
result = yield pyfuncitem.obj(**kwargs)
268276
defer.returnValue(result)
269277

0 commit comments

Comments
 (0)