Skip to content
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

UrlDispatcher.add_route with partial coroutine handler #814

Merged
merged 2 commits into from Mar 12, 2016
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
9 changes: 8 additions & 1 deletion aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ def __init__(self, method, handler, *,
issubclass(handler, AbstractView)):
pass
else:
handler = asyncio.coroutine(handler)
@asyncio.coroutine
def handler_wrapper(*args, **kwargs):
result = old_handler(*args, **kwargs)
if asyncio.iscoroutine(result):
Copy link
Member

Choose a reason for hiding this comment

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

Please add a check for asyncio.Future also.

Copy link
Author

Choose a reason for hiding this comment

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

according to current documentation (for UrlDispatcher.add_route and Resource.add_route) handler should be callable which excludes asyncio.Future

result = yield from result
return result
old_handler = handler
handler = handler_wrapper

self._method = method
self._handler = handler
Expand Down
19 changes: 19 additions & 0 deletions tests/test_web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import os
import shutil
import tempfile
import functools
import asyncio
import aiohttp.web


@pytest.fixture(scope='function')
Expand Down Expand Up @@ -44,3 +47,19 @@ def test_access_root_of_static_handler(tmp_dir_path, create_app_and_client):
assert r.status == 404
# data = (yield from r.read())
yield from r.release()


@pytest.mark.run_loop
def test_partialy_applied_handler(create_app_and_client):
app, client = yield from create_app_and_client()

@asyncio.coroutine
def handler(data, request):
return aiohttp.web.Response(body=data)

app.router.add_route('GET', '/', functools.partial(handler, b'hello'))

r = yield from client.get('/')
data = (yield from r.read())
assert data == b'hello'
yield from r.release()