1
1
import functools
2
2
import inspect
3
+ import itertools
3
4
import sys
4
5
import warnings
5
6
6
7
import decorator
7
8
import greenlet
8
9
import pytest
9
10
10
- from twisted .internet import defer
11
- # from twisted.internet import error
11
+ from twisted .internet import defer , error
12
12
from twisted .internet .threads import blockingCallFromThread
13
13
from twisted .python import failure
14
14
@@ -120,6 +120,56 @@ def decorator_apply(dec, func):
120
120
dict (decfunc = dec (func )), __wrapped__ = func )
121
121
122
122
123
+ class DecoratorArgumentsError (Exception ):
124
+ pass
125
+
126
+
127
+ def repr_args_kwargs (* args , ** kwargs ):
128
+ arguments = ', ' .join (itertools .chain (
129
+ (repr (x ) for x in args ),
130
+ ('{}={}' .format (k , repr (v )) for k , v in kwargs .items ())
131
+ ))
132
+
133
+ return '({})' .format (arguments )
134
+
135
+
136
+ def positional_not_allowed_exception (* args , ** kwargs ):
137
+ arguments = repr_args_kwargs (* args , ** kwargs )
138
+
139
+ return DecoratorArgumentsError (
140
+ 'Positional decorator arguments not allowed: {}' .format (arguments ),
141
+ )
142
+
143
+
144
+ def _optional_arguments ():
145
+ def decorator_decorator (d ):
146
+ # TODO: this should get the signature of d minus the f or something
147
+ def decorator_wrapper (* args , ** decorator_arguments ):
148
+ """this is decorator_wrapper"""
149
+ if len (args ) > 1 :
150
+ raise positional_not_allowed_exception ()
151
+
152
+ if len (args ) == 1 :
153
+ maybe_f = args [0 ]
154
+
155
+ if len (decorator_arguments ) > 0 or not callable (maybe_f ):
156
+ raise positional_not_allowed_exception ()
157
+
158
+ f = maybe_f
159
+ return d (f )
160
+
161
+ # TODO: this should get the signature of d minus the kwargs
162
+ def decorator_closure_on_arguments (f ):
163
+ return d (f , ** decorator_arguments )
164
+
165
+ return decorator_closure_on_arguments
166
+
167
+ return decorator_wrapper
168
+
169
+ return decorator_decorator
170
+
171
+
172
+ @_optional_arguments ()
123
173
def inlineCallbacks (f ):
124
174
"""
125
175
Mark as inline callbacks test for pytest-twisted processing and apply
@@ -136,6 +186,7 @@ def inlineCallbacks(f):
136
186
return decorated
137
187
138
188
189
+ @_optional_arguments ()
139
190
def ensureDeferred (f ):
140
191
"""
141
192
Mark as async test for pytest-twisted processing.
@@ -178,7 +229,8 @@ def _set_mark(o, mark):
178
229
179
230
def _marked_async_fixture (mark ):
180
231
@functools .wraps (pytest .fixture )
181
- def fixture (* args , ** kwargs ):
232
+ @_optional_arguments ()
233
+ def fixture (f , * args , ** kwargs ):
182
234
try :
183
235
scope = args [0 ]
184
236
except IndexError :
@@ -198,13 +250,10 @@ def fixture(*args, **kwargs):
198
250
# https://github.com/pytest-dev/pytest-twisted/issues/56
199
251
raise AsyncFixtureUnsupportedScopeError .from_scope (scope = scope )
200
252
201
- def decorator (f ):
202
- _set_mark (f , mark )
203
- result = pytest .fixture (* args , ** kwargs )(f )
204
-
205
- return result
253
+ _set_mark (f , mark )
254
+ result = pytest .fixture (* args , ** kwargs )(f )
206
255
207
- return decorator
256
+ return result
208
257
209
258
return fixture
210
259
@@ -274,12 +323,6 @@ def tear_it_down(deferred):
274
323
yield deferred
275
324
except StopAsyncIteration :
276
325
return
277
- except Exception : # as e:
278
- pass
279
- # e = e
280
- else :
281
- pass
282
- # e = None
283
326
284
327
# TODO: six.raise_from()
285
328
raise AsyncGeneratorFixtureDidNotStopError .from_generator (
@@ -401,14 +444,9 @@ def init_asyncio_reactor():
401
444
402
445
def _install_reactor (reactor_installer , reactor_type ):
403
446
"""Install the specified reactor and create the greenlet."""
404
- # TODO: maybe fix this in qt5reactor? btw, it avoids creating a second
405
- # qt5reactor.core.QtEventReactor and this somehow fixes the hang
406
- # that showed up in 5.15.0.
407
- # try:
408
- if 'twisted.internet.reactor' not in sys .modules :
447
+ try :
409
448
reactor_installer ()
410
- # except error.ReactorAlreadyInstalledError:
411
- else :
449
+ except error .ReactorAlreadyInstalledError :
412
450
import twisted .internet .reactor
413
451
414
452
if not isinstance (twisted .internet .reactor , reactor_type ):
0 commit comments