Skip to content

Update testing etc for Python 3.8 #75

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 18 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ jobs:
- name: CPython 3.7
tox: py37
action: 3.7
- name: CPython 3.8
tox: py38
action: 3.8
reactor:
- name: default
tox: default
Expand Down
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ matrix:
- python: 3.7
dist: xenial
sudo: true
- python: 3.8
dist: xenial
sudo: true

addons:
apt:
Expand Down
53 changes: 53 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,59 @@ which uses the twisted framework. test functions can return Deferred
objects and pytest will wait for their completion with this plugin.


NOTICE: Python 3.8 with asyncio support
=======================================

In Python 3.8, asyncio changed the default loop implementation to use
their proactor. The proactor does not implement some methods used by
Twisted's asyncio support. The result is a ``NotImplementedError``
exception such as below.

.. code-block:: pytb

<snip>
File "c:\projects\pytest-twisted\.tox\py38-asyncioreactor\lib\site-packages\twisted\internet\asyncioreactor.py", line 320, in install
reactor = AsyncioSelectorReactor(eventloop)
File "c:\projects\pytest-twisted\.tox\py38-asyncioreactor\lib\site-packages\twisted\internet\asyncioreactor.py", line 69, in __init__
super().__init__()
File "c:\projects\pytest-twisted\.tox\py38-asyncioreactor\lib\site-packages\twisted\internet\base.py", line 571, in __init__
self.installWaker()
File "c:\projects\pytest-twisted\.tox\py38-asyncioreactor\lib\site-packages\twisted\internet\posixbase.py", line 286, in installWaker
self.addReader(self.waker)
File "c:\projects\pytest-twisted\.tox\py38-asyncioreactor\lib\site-packages\twisted\internet\asyncioreactor.py", line 151, in addReader
self._asyncioEventloop.add_reader(fd, callWithLogger, reader,
File "C:\Python38-x64\Lib\asyncio\events.py", line 501, in add_reader
raise NotImplementedError
NotImplementedError

The previous default, the selector loop, still works but you have to
explicitly set it and do so early. The following ``conftest.py`` is provided
for reference.

.. code-block:: python3

import sys

import pytest
import pytest_twisted


@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
# https://twistedmatrix.com/trac/ticket/9766
# https://github.com/pytest-dev/pytest-twisted/issues/80

if (
config.getoption("reactor", "default") == "asyncio"
and sys.platform == 'win32'
and sys.version_info >= (3, 8)
):
import asyncio

selector_policy = asyncio.WindowsSelectorEventLoopPolicy()
asyncio.set_event_loop_policy(selector_policy)


Python 2 support plans
======================

Expand Down
6 changes: 6 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ environment:
- TOXENV: py37-defaultreactor, win-py37-qt5reactor, py37-asyncioreactor
PYTHON: "C:\\Python37-x64"

- TOXENV: py38-defaultreactor, win-py38-qt5reactor, py38-asyncioreactor
PYTHON: "C:\\Python38"

- TOXENV: py38-defaultreactor, win-py38-qt5reactor, py38-asyncioreactor
PYTHON: "C:\\Python38-x64"

install:
# https://github.com/pypa/virtualenv/issues/1050
- pip install -U git+https://github.com/pypa/virtualenv@e8163e83a92c9098f51d390289323232ece15e3b
Expand Down
16 changes: 16 additions & 0 deletions pytest_twisted.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import inspect
import sys
import warnings

import decorator
Expand Down Expand Up @@ -326,3 +327,18 @@ def pytest_configure(config):
)(blockon)

reactor_installers[config.getoption("reactor")]()


def _use_asyncio_selector_if_required(config):
# https://twistedmatrix.com/trac/ticket/9766
# https://github.com/pytest-dev/pytest-twisted/issues/80

if (
config.getoption("reactor", "default") == "asyncio"
and sys.platform == 'win32'
and sys.version_info >= (3, 8)
):
import asyncio

selector_policy = asyncio.WindowsSelectorEventLoopPolicy()
asyncio.set_event_loop_policy(selector_policy)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
],
entry_points={"pytest11": ["twisted = pytest_twisted"]},
)
9 changes: 9 additions & 0 deletions testing/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
import pytest
import pytest_twisted


pytest_plugins = "_pytest.pytester"


@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
pytest_twisted._use_asyncio_selector_if_required(config=config)
25 changes: 25 additions & 0 deletions testing/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def format_run_result_output_for_assert(run_result):
)


@pytest.fixture(name="default_conftest", autouse=True)
def _default_conftest(testdir):
testdir.makeconftest(textwrap.dedent("""
import pytest
import pytest_twisted


@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
pytest_twisted._use_asyncio_selector_if_required(config=config)
"""))


def skip_if_reactor_not(request, expected_reactor):
actual_reactor = request.config.getoption("reactor", "default")
if actual_reactor != expected_reactor:
Expand Down Expand Up @@ -630,10 +643,14 @@ def main():
def test_blockon_in_hook_with_asyncio(testdir, cmd_opts, request):
skip_if_reactor_not(request, "asyncio")
conftest_file = """
import pytest
import pytest_twisted as pt
from twisted.internet import defer

@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
pt._use_asyncio_selector_if_required(config=config)

pt.init_asyncio_reactor()
d = defer.Deferred()

Expand All @@ -659,6 +676,14 @@ def test_succeed():
def test_wrong_reactor_with_asyncio(testdir, cmd_opts, request):
skip_if_reactor_not(request, "asyncio")
conftest_file = """
import pytest
import pytest_twisted


@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
pytest_twisted._use_asyncio_selector_if_required(config=config)

def pytest_addhooks():
import twisted.internet.default
twisted.internet.default.install()
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[tox]
envlist=
py{27,35}-defaultreactor
py{35,36,37}-{default,qt5,asyncio}reactor
win-py{35,36,37}-qt5reactor
py{35,36,37,38}-{default,qt5,asyncio}reactor
win-py{35,36,37,38}-qt5reactor
linting

[testenv]
Expand Down