@@ -23,7 +23,7 @@ class _instances:
23
23
24
24
25
25
def pytest_namespace ():
26
- return {' inlineCallbacks' : inlineCallbacks , ' blockon' : blockon }
26
+ return {" inlineCallbacks" : inlineCallbacks , " blockon" : blockon }
27
27
28
28
29
29
def blockon (d ):
@@ -35,8 +35,9 @@ def blockon(d):
35
35
36
36
def blockon_default (d ):
37
37
current = greenlet .getcurrent ()
38
- assert current is not _instances .gr_twisted , \
39
- 'blockon cannot be called from the twisted greenlet'
38
+ assert (
39
+ current is not _instances .gr_twisted
40
+ ), "blockon cannot be called from the twisted greenlet"
40
41
result = []
41
42
42
43
def cb (r ):
@@ -47,7 +48,7 @@ def cb(r):
47
48
d .addCallbacks (cb , cb )
48
49
if not result :
49
50
_result = _instances .gr_twisted .switch ()
50
- assert _result is result , ' illegal switch in blockon'
51
+ assert _result is result , " illegal switch in blockon"
51
52
52
53
if isinstance (result [0 ], failure .Failure ):
53
54
result [0 ].raiseException ()
@@ -88,7 +89,7 @@ def _pytest_pyfunc_call(pyfuncitem):
88
89
return testfunction (* pyfuncitem ._args )
89
90
else :
90
91
funcargs = pyfuncitem .funcargs
91
- if hasattr (pyfuncitem , ' _fixtureinfo' ):
92
+ if hasattr (pyfuncitem , " _fixtureinfo" ):
92
93
testargs = {}
93
94
for arg in pyfuncitem ._fixtureinfo .argnames :
94
95
testargs [arg ] = funcargs [arg ]
@@ -100,7 +101,7 @@ def _pytest_pyfunc_call(pyfuncitem):
100
101
def pytest_pyfunc_call (pyfuncitem ):
101
102
if _instances .gr_twisted is not None :
102
103
if _instances .gr_twisted .dead :
103
- raise RuntimeError (' twisted reactor has stopped' )
104
+ raise RuntimeError (" twisted reactor has stopped" )
104
105
105
106
def in_reactor (d , f , * args ):
106
107
return defer .maybeDeferred (f , * args ).chainDeferred (d )
@@ -112,14 +113,14 @@ def in_reactor(d, f, *args):
112
113
blockon_default (d )
113
114
else :
114
115
if not _instances .reactor .running :
115
- raise RuntimeError (' twisted reactor is not running' )
116
+ raise RuntimeError (" twisted reactor is not running" )
116
117
blockingCallFromThread (
117
118
_instances .reactor , _pytest_pyfunc_call , pyfuncitem
118
119
)
119
120
return True
120
121
121
122
122
- @pytest .fixture (scope = ' session' , autouse = True )
123
+ @pytest .fixture (scope = " session" , autouse = True )
123
124
def twisted_greenlet (request ):
124
125
request .addfinalizer (stop_twisted_greenlet )
125
126
return _instances .gr_twisted
@@ -130,32 +131,27 @@ def init_default_reactor():
130
131
131
132
module = inspect .getmodule (twisted .internet .default .install )
132
133
133
- module_name = module .__name__ .split ('.' )[- 1 ]
134
- reactor_type_name , = (
135
- x
136
- for x in dir (module )
137
- if x .lower () == module_name
138
- )
134
+ module_name = module .__name__ .split ("." )[- 1 ]
135
+ reactor_type_name , = (x for x in dir (module ) if x .lower () == module_name )
139
136
reactor_type = getattr (module , reactor_type_name )
140
137
141
138
_install_reactor (
142
139
reactor_installer = twisted .internet .default .install ,
143
- reactor_type = reactor_type
140
+ reactor_type = reactor_type ,
144
141
)
145
142
146
143
147
144
def init_qt5_reactor ():
148
145
import qt5reactor
149
146
150
147
_install_reactor (
151
- reactor_installer = qt5reactor .install ,
152
- reactor_type = qt5reactor .QtReactor
148
+ reactor_installer = qt5reactor .install , reactor_type = qt5reactor .QtReactor
153
149
)
154
150
155
151
156
152
reactor_installers = {
157
- ' default' : init_default_reactor ,
158
- ' qt5reactor' : init_qt5_reactor
153
+ " default" : init_default_reactor ,
154
+ " qt5reactor" : init_qt5_reactor ,
159
155
}
160
156
161
157
@@ -164,26 +160,28 @@ def _install_reactor(reactor_installer, reactor_type):
164
160
reactor_installer ()
165
161
except error .ReactorAlreadyInstalledError :
166
162
import twisted .internet .reactor
163
+
167
164
if not isinstance (twisted .internet .reactor , reactor_type ):
168
165
raise WrongReactorAlreadyInstalledError (
169
- 'expected {} but found {}' .format (
170
- reactor_type ,
171
- type (twisted .internet .reactor )
166
+ "expected {} but found {}" .format (
167
+ reactor_type , type (twisted .internet .reactor )
172
168
)
173
169
)
170
+
174
171
import twisted .internet .reactor
172
+
175
173
_instances .reactor = twisted .internet .reactor
176
174
init_twisted_greenlet ()
177
175
178
176
179
177
def pytest_addoption (parser ):
180
- group = parser .getgroup (' twisted' )
178
+ group = parser .getgroup (" twisted" )
181
179
group .addoption (
182
- ' --reactor' ,
183
- default = ' default' ,
184
- choices = tuple (reactor_installers .keys ())
180
+ " --reactor" ,
181
+ default = " default" ,
182
+ choices = tuple (reactor_installers .keys ()),
185
183
)
186
184
187
185
188
186
def pytest_configure (config ):
189
- reactor_installers [config .getoption (' reactor' )]()
187
+ reactor_installers [config .getoption (" reactor" )]()
0 commit comments