Skip to content

Commit

Permalink
Merge pull request #717 from KeepSafe/awaitable_cbv
Browse files Browse the repository at this point in the history
Fix for #714
  • Loading branch information
asvetlov committed Jan 3, 2016
2 parents 35b1a0a + 496cec0 commit 31d4ab6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions aiohttp/abc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import asyncio
import sys
from abc import ABCMeta, abstractmethod


PY_35 = sys.version_info >= (3, 5)


class AbstractRouter(metaclass=ABCMeta):

@asyncio.coroutine # pragma: no branch
Expand Down Expand Up @@ -37,3 +41,8 @@ def request(self):
def __iter__(self):
while False: # pragma: no cover
yield None

if PY_35:
@abstractmethod
def __await__(self):
return
8 changes: 8 additions & 0 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mimetypes
import re
import os
import sys
import inspect

from collections.abc import Sized, Iterable, Container
Expand All @@ -24,6 +25,9 @@
'Route', 'PlainRoute', 'DynamicRoute', 'StaticRoute', 'View')


PY_35 = sys.version_info >= (3, 5)


class UrlMappingMatchInfo(dict, AbstractMatchInfo):

def __init__(self, match_dict, route):
Expand Down Expand Up @@ -386,6 +390,10 @@ def __iter__(self):
resp = yield from method()
return resp

if PY_35:
def __await__(self):
return (yield from self.__iter__())

def _raise_allowed_methods(self):
allowed_methods = {m for m in hdrs.METH_ALL if hasattr(self, m)}
raise HTTPMethodNotAllowed(self.request.method, allowed_methods)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_py35/test_cbv35.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from aiohttp import web
from aiohttp.web_urldispatcher import View
from unittest import mock


@pytest.mark.run_loop
async def test_render_ok():
resp = web.Response(text='OK')

class MyView(View):
async def get(self):
return resp

request = mock.Mock()
request.method = 'GET'
resp2 = await MyView(request)
assert resp is resp2

0 comments on commit 31d4ab6

Please sign in to comment.