Skip to content

Commit 25fa4aa

Browse files
committed
basic cleanup
1 parent cd07b11 commit 25fa4aa

File tree

1 file changed

+18
-175
lines changed

1 file changed

+18
-175
lines changed

pytest_twisted.py

Lines changed: 18 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,6 @@ def stop_twisted_greenlet():
130130
_instances.gr_twisted.switch()
131131

132132

133-
class _CoroutineWrapper:
134-
def __init__(self, coroutine, mark):
135-
# TODO: really an async def now, maybe, if that worked out
136-
self.coroutine = coroutine
137-
self.__name__ = self.coroutine.__name__ + 'ptcr'
138-
self.mark = mark
139-
140-
# def __call__(self):
141-
# print()
142-
143-
144133
def _marked_async_fixture(mark):
145134
@functools.wraps(pytest.fixture)
146135
def fixture(*args, **kwargs):
@@ -152,29 +141,13 @@ def fixture(*args, **kwargs):
152141
if scope != 'function':
153142
raise AsyncFixtureUnsupportedScopeError.from_scope(scope=scope)
154143

155-
# def marker(f):
156-
# @functools.wraps(f)
157-
# def w(*args, **kwargs):
158-
# return _CoroutineWrapper(
159-
# coroutine=f(*args, **kwargs),
160-
# mark=mark,
161-
# )
162-
#
163-
# return w
164-
165-
def marker(f):
166-
f._pytest_twisted_coroutine_wrapper = _CoroutineWrapper(
167-
coroutine=f,
168-
mark=mark,
169-
)
144+
def _mark(f):
145+
setattr(f, _mark_attribute_name, mark)
170146

171147
return f
172148

173149
def decorator(f):
174-
# result = pytest.fixture(*args, **kwargs)(
175-
# _CoroutineWrapper(coroutine=f, mark=mark),
176-
# )
177-
result = pytest.fixture(*args, **kwargs)(marker(f))
150+
result = pytest.fixture(*args, **kwargs)(_mark(f))
178151

179152
return result
180153

@@ -183,19 +156,18 @@ def decorator(f):
183156
return fixture
184157

185158

159+
_mark_attribute_name = '_pytest_twisted_coroutine_wrapper'
186160
async_fixture = _marked_async_fixture('async_fixture')
187161
async_yield_fixture = _marked_async_fixture('async_yield_fixture')
188162

189163

190164
def pytest_fixture_setup(fixturedef, request):
191-
maybe_wrapper = getattr(
192-
fixturedef.func,
193-
'_pytest_twisted_coroutine_wrapper',
194-
None,
195-
)
196-
if not isinstance(maybe_wrapper, _CoroutineWrapper):
165+
maybe_mark = getattr(fixturedef.func, _mark_attribute_name, None)
166+
if maybe_mark is None:
197167
return None
198168

169+
mark = maybe_mark
170+
199171
if _instances.gr_twisted is not None:
200172
if _instances.gr_twisted.dead:
201173
raise RuntimeError("twisted reactor has stopped")
@@ -205,89 +177,49 @@ def in_reactor(d, f, *args):
205177

206178
d = defer.Deferred()
207179
_instances.reactor.callLater(
208-
0.0, in_reactor, d, _pytest_fixture_setup, fixturedef, request, maybe_wrapper
180+
0.0, in_reactor, d, _pytest_fixture_setup, fixturedef, request, mark
209181
)
210-
result = blockon_default(d)
182+
blockon_default(d)
211183
else:
212184
if not _instances.reactor.running:
213185
raise RuntimeError("twisted reactor is not running")
214-
result = blockingCallFromThread(
215-
_instances.reactor, _pytest_fixture_setup, fixturedef, request, maybe_wrapper
186+
blockingCallFromThread(
187+
_instances.reactor, _pytest_fixture_setup, fixturedef, request, mark
216188
)
217-
# return None
218-
return result
189+
190+
return True
219191

220192

221193
async_yield_fixture_cache = {}
222194

223195

224196
@defer.inlineCallbacks
225-
def _pytest_fixture_setup(fixturedef, request, wrapper):
226-
# return None
227-
#
228-
# if not isinstance(fixturedef.func, _CoroutineWrapper):
229-
# return None
230-
197+
def _pytest_fixture_setup(fixturedef, request, mark):
231198
fixture_function = fixturedef.func
232199

233-
async_generators = []
234-
235200
kwargs = {
236201
name: request.getfixturevalue(name)
237202
for name in fixturedef.argnames
238203
}
239204

240-
if wrapper.mark == 'async_fixture':
205+
if mark == 'async_fixture':
241206
arg_value = yield defer.ensureDeferred(
242207
fixture_function(**kwargs)
243208
)
244-
elif wrapper.mark == 'async_yield_fixture':
245-
# async_generators.append((arg, wrapper))
209+
elif mark == 'async_yield_fixture':
246210
coroutine = fixture_function(**kwargs)
247211
# TODO: use request.addfinalizer() instead?
248212
async_yield_fixture_cache[request.param_index] = coroutine
249213
arg_value = yield defer.ensureDeferred(
250214
coroutine.__anext__(),
251215
)
252216
else:
253-
raise UnrecognizedCoroutineMarkError.from_mark(
254-
mark=wrapper.mark,
255-
)
217+
raise UnrecognizedCoroutineMarkError.from_mark(mark=mark)
256218

257219
fixturedef.cached_result = (arg_value, request.param_index, None)
258220

259221
defer.returnValue(arg_value)
260222

261-
# async_generator_deferreds = [
262-
# (arg, defer.ensureDeferred(g.coroutine.__anext__()))
263-
# for arg, g in reversed(async_generators)
264-
# ]
265-
#
266-
# for arg, d in async_generator_deferreds:
267-
# try:
268-
# yield d
269-
# except StopAsyncIteration:
270-
# continue
271-
# else:
272-
# raise AsyncGeneratorFixtureDidNotStopError.from_generator(
273-
# generator=arg,
274-
# )
275-
276-
277-
# @defer.inlineCallbacks
278-
# def _pytest_fixture_post_finalizer(fixturedef, request, coroutine):
279-
# try:
280-
# yield defer.ensureDeferred(
281-
# coroutine.__anext__(),
282-
# )
283-
# except StopAsyncIteration:
284-
# # TODO: i don't remember why this makes sense...
285-
# pass
286-
# else:
287-
# raise AsyncGeneratorFixtureDidNotStopError.from_generator(
288-
# generator=coroutine,
289-
# )
290-
291223

292224
# TODO: but don't we want to do the finalizer? not wait until post it?
293225
def pytest_fixture_post_finalizer(fixturedef, request):
@@ -301,33 +233,6 @@ def pytest_fixture_post_finalizer(fixturedef, request):
301233
to_be_torn_down.append(defer.ensureDeferred(coroutine.__anext__()))
302234
return None
303235

304-
# try:
305-
# if _instances.gr_twisted is not None:
306-
# if _instances.gr_twisted.dead:
307-
# raise RuntimeError("twisted reactor has stopped")
308-
#
309-
# def in_reactor(d, f, *args):
310-
# return defer.maybeDeferred(f, *args).chainDeferred(d)
311-
#
312-
# d = defer.Deferred()
313-
# _instances.reactor.callLater(
314-
# 0.0, in_reactor, d, _pytest_fixture_post_finalizer, fixturedef, request, coroutine
315-
# )
316-
# result = blockon_default(d)
317-
# else:
318-
# if not _instances.reactor.running:
319-
# raise RuntimeError("twisted reactor is not running")
320-
# result = blockingCallFromThread(
321-
# _instances.reactor, _pytest_fixture_post_finalizer, fixturedef, request, coroutine
322-
# )
323-
# except StopAsyncIteration as e:
324-
# print(e)
325-
#
326-
# # async_yield_fixture_cache.pop(request.param_index)
327-
#
328-
# # return None
329-
return result
330-
331236

332237
@defer.inlineCallbacks
333238
def tear_it_down(deferred):
@@ -355,13 +260,6 @@ def tear_it_down(deferred):
355260
def pytest_runtest_teardown(item):
356261
yield
357262

358-
# deferreds = []
359-
#
360-
# while len(to_be_torn_down) > 0:
361-
# coroutine = to_be_torn_down.pop(0)
362-
# deferreds.append(defer.ensureDeferred(coroutine.__anext__()))
363-
#
364-
# for deferred in deferreds:
365263
while len(to_be_torn_down) > 0:
366264
deferred = to_be_torn_down.pop(0)
367265
if _instances.gr_twisted is not None:
@@ -377,16 +275,12 @@ def in_reactor(d, f, *args):
377275
0.0, in_reactor, d, tear_it_down, deferred
378276
)
379277
blockon_default(d)
380-
# blockon_default(tear_it_down(deferred))
381278
else:
382279
if not _instances.reactor.running:
383280
raise RuntimeError("twisted reactor is not running")
384281
blockingCallFromThread(
385282
_instances.reactor, tear_it_down, deferred,
386283
)
387-
# blockingCallFromThread(
388-
# _instances.reactor, tear_it_down, deferred
389-
# )
390284

391285

392286
@defer.inlineCallbacks
@@ -395,56 +289,6 @@ def _pytest_pyfunc_call(pyfuncitem):
395289
result = yield pyfuncitem.obj(**kwargs)
396290
defer.returnValue(result)
397291

398-
return
399-
# print()
400-
#
401-
# testfunction = pyfuncitem.obj
402-
# async_generators = []
403-
# funcargs = pyfuncitem.funcargs
404-
# if hasattr(pyfuncitem, "_fixtureinfo"):
405-
# testargs = {}
406-
# for arg in pyfuncitem._fixtureinfo.argnames:
407-
# if isinstance(funcargs[arg], _CoroutineWrapper):
408-
# wrapper = funcargs[arg]
409-
#
410-
# if wrapper.mark == 'async_fixture':
411-
# arg_value = yield defer.ensureDeferred(
412-
# wrapper.coroutine
413-
# )
414-
# elif wrapper.mark == 'async_yield_fixture':
415-
# async_generators.append((arg, wrapper))
416-
# arg_value = yield defer.ensureDeferred(
417-
# wrapper.coroutine.__anext__(),
418-
# )
419-
# else:
420-
# raise UnrecognizedCoroutineMarkError.from_mark(
421-
# mark=wrapper.mark,
422-
# )
423-
# else:
424-
# arg_value = funcargs[arg]
425-
#
426-
# testargs[arg] = arg_value
427-
# else:
428-
# testargs = funcargs
429-
# result = yield testfunction(**testargs)
430-
#
431-
# async_generator_deferreds = [
432-
# (arg, defer.ensureDeferred(g.coroutine.__anext__()))
433-
# for arg, g in reversed(async_generators)
434-
# ]
435-
#
436-
# for arg, d in async_generator_deferreds:
437-
# try:
438-
# yield d
439-
# except StopAsyncIteration:
440-
# continue
441-
# else:
442-
# raise AsyncGeneratorFixtureDidNotStopError.from_generator(
443-
# generator=arg,
444-
# )
445-
#
446-
# defer.returnValue(result)
447-
448292

449293
def pytest_pyfunc_call(pyfuncitem):
450294
if _instances.gr_twisted is not None:
@@ -471,7 +315,6 @@ def in_reactor(d, f, *args):
471315
# TODO: switch to some plugin callback to guarantee order before other fixtures?
472316
@pytest.fixture(scope="session", autouse=True)
473317
def twisted_greenlet(request):
474-
# request.addfinalizer(stop_twisted_greenlet)
475318
return _instances.gr_twisted
476319

477320

0 commit comments

Comments
 (0)