Skip to content

Add support for qt5reactor #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Mar 3, 2018
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion pytest_twisted.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import decorator
import greenlet
import pytest
Expand Down Expand Up @@ -51,14 +53,33 @@ def stop_twisted_greenlet():
gr_twisted.switch()


def pytest_addhooks(pluginmanager):
@pytest.hookimpl(trylast=True)
def pytest_configure(config):
global gr_twisted
global reactor

if not gr_twisted and not reactor.running:
if config.getoption('qt5reactor'):
if 'twisted.internet.reactor' in sys.modules:
del sys.modules['twisted.internet.reactor']

import qt5reactor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi guys, pytest-qt author here. 😁

IIUC, qt5reactor.install() must be called after pytest-qt's qapp fixture correct? Then I think the correct way is to inject a new fixture into the global namespace which depends on qapp at this point, something like this:

if config.getoption('qt5reactor'):
    
    class Qt5ReactorPlugin(object):
        
        @pytest.fixture(scope='session', autouse=True)
        def qt5reactor(qapp):
            import qt5reactor
            qt5reactor.install()

    config.pluginmanager.register(Qt5ReactorPlugin())   

This is the recommended way of creating new fixtures based on command-line options, and will ensure the QApplication object is created before qt5reactor.install is called.

But now that I think about it, perhaps pytest-twisted authors would also like to create a generic reactor fixture which is selected from a --reactor command-line option? Full disclosure, I've never used twisted and have only a vague idea of what a "reactor" is, so excuse me if that last thought didn't make much sense.

qt5reactor.install()

import twisted.internet.reactor
reactor = twisted.internet.reactor

gr_twisted = greenlet.greenlet(reactor.run)
# give me better tracebacks:
failure.Failure.cleanFailure = lambda self: None


def pytest_addoption(parser):
group = parser.getgroup('twisted')
group.addoption('--qt5reactor', dest='qt5reactor', action='store_true',
help='prepare for use with qt5reactor')


@pytest.fixture(scope="session", autouse=True)
def twisted_greenlet(request):
request.addfinalizer(stop_twisted_greenlet)
Expand Down