Skip to content

Commit 9a6cb35

Browse files
committed
Attempt to use an autouse reactor fixture
1 parent 81858a9 commit 9a6cb35

File tree

1 file changed

+53
-16
lines changed

1 file changed

+53
-16
lines changed

pytest_twisted.py

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import greenlet
55
import pytest
66

7-
from twisted.internet import defer, reactor
7+
from twisted.internet import error, defer, reactor
88
from twisted.internet.threads import blockingCallFromThread
99
from twisted.python import failure
1010

@@ -39,6 +39,7 @@ def block_from_thread(d):
3939

4040
@decorator.decorator
4141
def inlineCallbacks(fun, *args, **kw):
42+
print('checkpoint', 'pytest-twisted', 'inlineCallbacks')
4243
return defer.inlineCallbacks(fun)(*args, **kw)
4344

4445

@@ -53,40 +54,75 @@ def stop_twisted_greenlet():
5354
gr_twisted.switch()
5455

5556

56-
@pytest.hookimpl(trylast=True)
57-
def pytest_configure(config):
57+
def create_twisted_greenlet():
5858
global gr_twisted
59-
global reactor
60-
6159
if not gr_twisted and not reactor.running:
62-
if config.getoption('qt5reactor'):
63-
if 'twisted.internet.reactor' in sys.modules:
64-
del sys.modules['twisted.internet.reactor']
65-
66-
import qt5reactor
67-
qt5reactor.install()
68-
69-
import twisted.internet.reactor
70-
reactor = twisted.internet.reactor
71-
7260
gr_twisted = greenlet.greenlet(reactor.run)
7361
# give me better tracebacks:
7462
failure.Failure.cleanFailure = lambda self: None
7563

7664

65+
def pytest_addhooks(pluginmanager):
66+
create_twisted_greenlet()
67+
68+
7769
def pytest_addoption(parser):
7870
group = parser.getgroup('twisted')
7971
group.addoption('--qt5reactor', dest='qt5reactor', action='store_true',
8072
help='prepare for use with qt5reactor')
8173

8274

75+
def pytest_configure(config):
76+
# TODO: why is the parameter needed?
77+
def default_reactor(_):
78+
print('checkpoint', 'pytest-twisted', 'reactor (default)')
79+
global reactor
80+
from twisted.internet import reactor
81+
create_twisted_greenlet()
82+
83+
def qt5_reactor(qapp):
84+
print('checkpoint', 'pytest-twisted', 'reactor (qt5)')
85+
global gr_twisted
86+
global reactor
87+
import qt5reactor
88+
89+
try:
90+
qt5reactor.install()
91+
except error.ReactorAlreadyInstalledError:
92+
if not isinstance(reactor, qt5reactor.QtReactor):
93+
stop_twisted_greenlet()
94+
gr_twisted = None
95+
del sys.modules['twisted.internet.reactor']
96+
qt5reactor.install()
97+
print('checkpoint', 'pytest-twisted', 'qt5reactor installed')
98+
from twisted.internet import reactor
99+
100+
create_twisted_greenlet()
101+
else:
102+
create_twisted_greenlet()
103+
104+
if config.getoption('qt5reactor'):
105+
reactor_fixture = qt5_reactor
106+
else:
107+
reactor_fixture = default_reactor
108+
109+
class ReactorPlugin(object):
110+
reactor = (
111+
pytest.fixture(scope='session', autouse=True)(reactor_fixture)
112+
)
113+
114+
config.pluginmanager.register(ReactorPlugin())
115+
116+
83117
@pytest.fixture(scope="session", autouse=True)
84-
def twisted_greenlet(request):
118+
def twisted_greenlet(request, reactor):
119+
print('checkpoint', 'pytest-twisted', 'twisted_greenlet')
85120
request.addfinalizer(stop_twisted_greenlet)
86121
return gr_twisted
87122

88123

89124
def _pytest_pyfunc_call(pyfuncitem):
125+
print('checkpoint', 'pytest-twisted', '_pytest_pyfunc_call')
90126
testfunction = pyfuncitem.obj
91127
if pyfuncitem._isyieldedfunction():
92128
return testfunction(*pyfuncitem._args)
@@ -102,6 +138,7 @@ def _pytest_pyfunc_call(pyfuncitem):
102138

103139

104140
def pytest_pyfunc_call(pyfuncitem):
141+
print('checkpoint', 'pytest-twisted', 'pytest_pyfunc_call')
105142
if gr_twisted is not None:
106143
if gr_twisted.dead:
107144
raise RuntimeError("twisted reactor has stopped")

0 commit comments

Comments
 (0)