@@ -130,17 +130,6 @@ def stop_twisted_greenlet():
130
130
_instances .gr_twisted .switch ()
131
131
132
132
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
-
144
133
def _marked_async_fixture (mark ):
145
134
@functools .wraps (pytest .fixture )
146
135
def fixture (* args , ** kwargs ):
@@ -152,29 +141,13 @@ def fixture(*args, **kwargs):
152
141
if scope != 'function' :
153
142
raise AsyncFixtureUnsupportedScopeError .from_scope (scope = scope )
154
143
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 )
170
146
171
147
return f
172
148
173
149
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 ))
178
151
179
152
return result
180
153
@@ -183,19 +156,18 @@ def decorator(f):
183
156
return fixture
184
157
185
158
159
+ _mark_attribute_name = '_pytest_twisted_coroutine_wrapper'
186
160
async_fixture = _marked_async_fixture ('async_fixture' )
187
161
async_yield_fixture = _marked_async_fixture ('async_yield_fixture' )
188
162
189
163
190
164
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 :
197
167
return None
198
168
169
+ mark = maybe_mark
170
+
199
171
if _instances .gr_twisted is not None :
200
172
if _instances .gr_twisted .dead :
201
173
raise RuntimeError ("twisted reactor has stopped" )
@@ -205,89 +177,49 @@ def in_reactor(d, f, *args):
205
177
206
178
d = defer .Deferred ()
207
179
_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
209
181
)
210
- result = blockon_default (d )
182
+ blockon_default (d )
211
183
else :
212
184
if not _instances .reactor .running :
213
185
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
216
188
)
217
- # return None
218
- return result
189
+
190
+ return True
219
191
220
192
221
193
async_yield_fixture_cache = {}
222
194
223
195
224
196
@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 ):
231
198
fixture_function = fixturedef .func
232
199
233
- async_generators = []
234
-
235
200
kwargs = {
236
201
name : request .getfixturevalue (name )
237
202
for name in fixturedef .argnames
238
203
}
239
204
240
- if wrapper . mark == 'async_fixture' :
205
+ if mark == 'async_fixture' :
241
206
arg_value = yield defer .ensureDeferred (
242
207
fixture_function (** kwargs )
243
208
)
244
- elif wrapper .mark == 'async_yield_fixture' :
245
- # async_generators.append((arg, wrapper))
209
+ elif mark == 'async_yield_fixture' :
246
210
coroutine = fixture_function (** kwargs )
247
211
# TODO: use request.addfinalizer() instead?
248
212
async_yield_fixture_cache [request .param_index ] = coroutine
249
213
arg_value = yield defer .ensureDeferred (
250
214
coroutine .__anext__ (),
251
215
)
252
216
else :
253
- raise UnrecognizedCoroutineMarkError .from_mark (
254
- mark = wrapper .mark ,
255
- )
217
+ raise UnrecognizedCoroutineMarkError .from_mark (mark = mark )
256
218
257
219
fixturedef .cached_result = (arg_value , request .param_index , None )
258
220
259
221
defer .returnValue (arg_value )
260
222
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
-
291
223
292
224
# TODO: but don't we want to do the finalizer? not wait until post it?
293
225
def pytest_fixture_post_finalizer (fixturedef , request ):
@@ -301,33 +233,6 @@ def pytest_fixture_post_finalizer(fixturedef, request):
301
233
to_be_torn_down .append (defer .ensureDeferred (coroutine .__anext__ ()))
302
234
return None
303
235
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
-
331
236
332
237
@defer .inlineCallbacks
333
238
def tear_it_down (deferred ):
@@ -355,13 +260,6 @@ def tear_it_down(deferred):
355
260
def pytest_runtest_teardown (item ):
356
261
yield
357
262
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:
365
263
while len (to_be_torn_down ) > 0 :
366
264
deferred = to_be_torn_down .pop (0 )
367
265
if _instances .gr_twisted is not None :
@@ -377,16 +275,12 @@ def in_reactor(d, f, *args):
377
275
0.0 , in_reactor , d , tear_it_down , deferred
378
276
)
379
277
blockon_default (d )
380
- # blockon_default(tear_it_down(deferred))
381
278
else :
382
279
if not _instances .reactor .running :
383
280
raise RuntimeError ("twisted reactor is not running" )
384
281
blockingCallFromThread (
385
282
_instances .reactor , tear_it_down , deferred ,
386
283
)
387
- # blockingCallFromThread(
388
- # _instances.reactor, tear_it_down, deferred
389
- # )
390
284
391
285
392
286
@defer .inlineCallbacks
@@ -395,56 +289,6 @@ def _pytest_pyfunc_call(pyfuncitem):
395
289
result = yield pyfuncitem .obj (** kwargs )
396
290
defer .returnValue (result )
397
291
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
-
448
292
449
293
def pytest_pyfunc_call (pyfuncitem ):
450
294
if _instances .gr_twisted is not None :
@@ -471,7 +315,6 @@ def in_reactor(d, f, *args):
471
315
# TODO: switch to some plugin callback to guarantee order before other fixtures?
472
316
@pytest .fixture (scope = "session" , autouse = True )
473
317
def twisted_greenlet (request ):
474
- # request.addfinalizer(stop_twisted_greenlet)
475
318
return _instances .gr_twisted
476
319
477
320
0 commit comments