11import functools
22import inspect
3+ import itertools
34import sys
45import warnings
56
67import decorator
78import greenlet
89import pytest
910
10- from twisted .internet import defer
11- # from twisted.internet import error
11+ from twisted .internet import defer , error
1212from twisted .internet .threads import blockingCallFromThread
1313from twisted .python import failure
1414
@@ -120,6 +120,56 @@ def decorator_apply(dec, func):
120120 dict (decfunc = dec (func )), __wrapped__ = func )
121121
122122
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 ()
123173def inlineCallbacks (f ):
124174 """
125175 Mark as inline callbacks test for pytest-twisted processing and apply
@@ -136,6 +186,7 @@ def inlineCallbacks(f):
136186 return decorated
137187
138188
189+ @_optional_arguments ()
139190def ensureDeferred (f ):
140191 """
141192 Mark as async test for pytest-twisted processing.
@@ -178,7 +229,8 @@ def _set_mark(o, mark):
178229
179230def _marked_async_fixture (mark ):
180231 @functools .wraps (pytest .fixture )
181- def fixture (* args , ** kwargs ):
232+ @_optional_arguments ()
233+ def fixture (f , * args , ** kwargs ):
182234 try :
183235 scope = args [0 ]
184236 except IndexError :
@@ -198,13 +250,10 @@ def fixture(*args, **kwargs):
198250 # https://github.com/pytest-dev/pytest-twisted/issues/56
199251 raise AsyncFixtureUnsupportedScopeError .from_scope (scope = scope )
200252
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 )
206255
207- return decorator
256+ return result
208257
209258 return fixture
210259
@@ -274,12 +323,6 @@ def tear_it_down(deferred):
274323 yield deferred
275324 except StopAsyncIteration :
276325 return
277- except Exception : # as e:
278- pass
279- # e = e
280- else :
281- pass
282- # e = None
283326
284327 # TODO: six.raise_from()
285328 raise AsyncGeneratorFixtureDidNotStopError .from_generator (
@@ -401,14 +444,9 @@ def init_asyncio_reactor():
401444
402445def _install_reactor (reactor_installer , reactor_type ):
403446 """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 :
409448 reactor_installer ()
410- # except error.ReactorAlreadyInstalledError:
411- else :
449+ except error .ReactorAlreadyInstalledError :
412450 import twisted .internet .reactor
413451
414452 if not isinstance (twisted .internet .reactor , reactor_type ):
0 commit comments