4
4
import greenlet
5
5
import pytest
6
6
7
- from twisted .internet import error , defer , reactor
7
+ from twisted .internet import error , defer
8
8
from twisted .internet .threads import blockingCallFromThread
9
9
from twisted .python import failure
10
10
11
11
gr_twisted = None
12
+ reactor = None
12
13
13
14
14
15
def blockon (d ):
@@ -46,7 +47,6 @@ def block_from_thread(d):
46
47
47
48
@decorator .decorator
48
49
def inlineCallbacks (fun , * args , ** kw ):
49
- print ('checkpoint' , 'pytest-twisted' , 'inlineCallbacks' )
50
50
return defer .inlineCallbacks (fun )(* args , ** kw )
51
51
52
52
@@ -63,6 +63,9 @@ def stop_twisted_greenlet():
63
63
64
64
def create_twisted_greenlet ():
65
65
global gr_twisted
66
+ if reactor is None :
67
+ return
68
+
66
69
if not gr_twisted and not reactor .running :
67
70
gr_twisted = greenlet .greenlet (reactor .run )
68
71
# give me better tracebacks:
@@ -82,17 +85,17 @@ def pytest_addoption(parser):
82
85
def pytest_configure (config ):
83
86
# TODO: why is the parameter needed?
84
87
def default_reactor ():
85
- print ('checkpoint' , 'pytest-twisted' , 'reactor (default)' )
86
88
global reactor
87
89
from twisted .internet import reactor
88
90
create_twisted_greenlet ()
89
91
90
92
def qt5_reactor (qapp ):
91
- print ('checkpoint' , 'pytest-twisted' , 'reactor (qt5)' )
92
93
global gr_twisted
93
94
global reactor
94
95
import qt5reactor
95
96
97
+ reinstalled = False
98
+
96
99
try :
97
100
qt5reactor .install ()
98
101
except error .ReactorAlreadyInstalledError :
@@ -101,11 +104,14 @@ def qt5_reactor(qapp):
101
104
gr_twisted = None
102
105
del sys .modules ['twisted.internet.reactor' ]
103
106
qt5reactor .install ()
104
- print ('checkpoint' , 'pytest-twisted' , 'qt5reactor installed' )
105
- from twisted .internet import reactor
106
-
107
- create_twisted_greenlet ()
107
+ reinstalled = True
108
108
else :
109
+ reinstalled = True
110
+
111
+ if reinstalled :
112
+ import twisted .internet .reactor
113
+ reactor = twisted .internet .reactor
114
+
109
115
create_twisted_greenlet ()
110
116
111
117
if config .getoption ('qt5reactor' ):
@@ -123,13 +129,11 @@ class ReactorPlugin(object):
123
129
124
130
@pytest .fixture (scope = "session" , autouse = True )
125
131
def twisted_greenlet (request , reactor ):
126
- print ('checkpoint' , 'pytest-twisted' , 'twisted_greenlet' )
127
132
request .addfinalizer (stop_twisted_greenlet )
128
133
return gr_twisted
129
134
130
135
131
136
def _pytest_pyfunc_call (pyfuncitem ):
132
- print ('checkpoint' , 'pytest-twisted' , '_pytest_pyfunc_call' )
133
137
testfunction = pyfuncitem .obj
134
138
if pyfuncitem ._isyieldedfunction ():
135
139
return testfunction (* pyfuncitem ._args )
@@ -145,7 +149,6 @@ def _pytest_pyfunc_call(pyfuncitem):
145
149
146
150
147
151
def pytest_pyfunc_call (pyfuncitem ):
148
- print ('checkpoint' , 'pytest-twisted' , 'pytest_pyfunc_call' )
149
152
if gr_twisted is not None :
150
153
if gr_twisted .dead :
151
154
raise RuntimeError ("twisted reactor has stopped" )
0 commit comments