From 496cec0b29ccaa61d0af0062b3c9a6c8ab128171 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sun, 3 Jan 2016 23:21:04 +0200 Subject: [PATCH] Fix for #714 --- aiohttp/abc.py | 9 +++++++++ aiohttp/web_urldispatcher.py | 8 ++++++++ tests/test_py35/test_cbv35.py | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 tests/test_py35/test_cbv35.py diff --git a/aiohttp/abc.py b/aiohttp/abc.py index 1296ea1be5c..535b4720951 100644 --- a/aiohttp/abc.py +++ b/aiohttp/abc.py @@ -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 @@ -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 diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index cce91f83557..3bfcaaac50d 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -6,6 +6,7 @@ import mimetypes import re import os +import sys import inspect from collections.abc import Sized, Iterable, Container @@ -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): @@ -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) diff --git a/tests/test_py35/test_cbv35.py b/tests/test_py35/test_cbv35.py new file mode 100644 index 00000000000..b3687bedafa --- /dev/null +++ b/tests/test_py35/test_cbv35.py @@ -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