Skip to content

Commit

Permalink
use asyncio.current_task in python 3.7 (#24)
Browse files Browse the repository at this point in the history
Also:
- randomized tests
- fixed dependent tests
- updated ci requirements
- added tox.ini
  • Loading branch information
diefans authored and argaen committed Sep 26, 2018
1 parent 5fb7dbd commit 67108c9
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 23 deletions.
14 changes: 9 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ python:
- "3.5"
- "3.6"

install:
- pip install -r requirements-ci.txt
matrix:
include:
- python: 3.7
dist: xenial
sudo: true

script:
install: pip install tox-travis flake8

script: tox
- make syntax
- python setup.py develop
- make test
- tox
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syntax:
flake8 --exclude=examples/
flake8 --exclude=examples/,.tox,.eggs

cov:
pytest --cov-report term-missing --cov=aiotask_context -sv tests/test_context.py
Expand Down
33 changes: 25 additions & 8 deletions aiotask_context/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import asyncio
import logging
import sys
from collections import ChainMap
from functools import partial
from copy import deepcopy
from functools import partial

PY37 = sys.version_info >= (3, 7)

if PY37:
def asyncio_current_task(loop=None):
"""Return the current task or None."""
try:
return asyncio.current_task(loop)
except RuntimeError:
# simulate old behaviour
return None
else:
asyncio_current_task = asyncio.Task.current_task


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -54,7 +68,7 @@ def task_factory(loop, coro, copy_context=False, context_factory=None):
del task._source_traceback[-1]

try:
context = asyncio.Task.current_task(loop=loop).context
context = asyncio_current_task(loop=loop).context
except AttributeError:
context = None

Expand Down Expand Up @@ -95,10 +109,11 @@ def get(key, default=None):
:param default: None by default, returned in case key is not found.
:return: Value stored inside the dict[key].
"""
if not asyncio.Task.current_task():
current_task = asyncio_current_task()
if not current_task:
raise ValueError(NO_LOOP_EXCEPTION_MSG.format(key))

return asyncio.Task.current_task().context.get(key, default)
return current_task.context.get(key, default)


def set(key, value):
Expand All @@ -109,10 +124,11 @@ def set(key, value):
:param value: value to store inside context[key].
:raises
"""
if not asyncio.Task.current_task():
current_task = asyncio_current_task()
if not current_task:
raise ValueError(NO_LOOP_EXCEPTION_MSG.format(key))

asyncio.Task.current_task().context[key] = value
current_task.context[key] = value


def clear():
Expand All @@ -121,7 +137,8 @@ def clear():
:raises ValueError: if no current task.
"""
if not asyncio.Task.current_task():
current_task = asyncio_current_task()
if not current_task:
raise ValueError("No event loop found")

asyncio.Task.current_task().context.clear()
current_task.context.clear()
11 changes: 6 additions & 5 deletions requirements-ci.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pytest==3.0.1
pytest-asyncio==0.4.1
pytest-cov==2.3.1
flake8==3.0.4
uvloop==0.8.0
pytest==3.8.0
pytest-randomly==1.2.3
pytest-asyncio==0.9.0
pytest-cov==2.6.0
flake8==3.5.0
uvloop==0.11.2
12 changes: 9 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@

@pytest.fixture()
def asyncio_loop():
loop = asyncio.get_event_loop_policy().new_event_loop()
asyncio.set_event_loop_policy(None)
loop = asyncio.get_event_loop()
yield loop
loop.close()
# restore the virgin state
asyncio.set_event_loop_policy(None)


@pytest.fixture()
def uvloop_loop():
loop = uvloop.new_event_loop()
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.get_event_loop()
yield loop
loop.close()
# restore the virgin state
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())


@pytest.fixture(params=[
'asyncio_loop',
'uvloop_loop'
])
def event_loop(request):
return request.getfuncargvalue(request.param)
return request.getfixturevalue(request.param)


@pytest.fixture(autouse=True)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_acceptance.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def dummy2(a, b):
def dummy1(n_tasks):
context.set("key", str(uuid.uuid4()))
tasks = [
asyncio.ensure_future(dummy2(id(asyncio.Task.current_task()), n)) for n in range(n_tasks)]
asyncio.ensure_future(
dummy2(id(context.asyncio_current_task()), n)) for n in range(n_tasks)]
results = yield from asyncio.gather(*tasks)
info = defaultdict(list)
for taskid, n, key in results:
Expand Down
35 changes: 35 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[tox]
skipsdist = True
envlist =
setup
py35
py36
py37
report

[testenv:setup]
deps = coverage
setenv =
COVERAGE_FILE = .coverage
commands =
coverage erase

[testenv]
setenv =
COVERAGE_FILE = .coverage.{envname}
deps =
coverage
pytest
commands =
pip install -e .
pip install -r requirements-ci.txt
coverage run --source aiotask_context -m pytest

[testenv:report]
deps = coverage
setenv =
COVERAGE_FILE = .coverage
commands =
coverage combine
coverage report -m
coverage xml

0 comments on commit 67108c9

Please sign in to comment.