From e9515b54fc2767664b51961b86743430125fa3f7 Mon Sep 17 00:00:00 2001 From: Michal Kuffa Date: Sat, 10 Sep 2016 10:53:32 +0200 Subject: [PATCH] Update async generators protocol for Python 3.5.2+ (fixes #136) (#137) * Update async generators protocol for Python 3.5.2+ (fixes #136) * Add python 3.5-dev to .travis.yml --- .travis.yml | 1 + aiopg/cursor.py | 6 ++++-- aiopg/sa/result.py | 7 +++++-- aiopg/utils.py | 14 ++++++++++---- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 66f759de..46a7fe79 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ services: python: - "3.4" - "3.5" + - "3.5-dev" install: - python setup.py install diff --git a/aiopg/cursor.py b/aiopg/cursor.py index f63ee4ab..f3db3304 100644 --- a/aiopg/cursor.py +++ b/aiopg/cursor.py @@ -4,7 +4,7 @@ import psycopg2 from .log import logger -from .utils import PY_35 +from .utils import PY_35, PY_352 class Cursor: @@ -379,10 +379,12 @@ def __iter__(self): if PY_35: # pragma: no branch - @asyncio.coroutine def __aiter__(self): return self + if not PY_352: + __aiter__ = asyncio.coroutine(__aiter__) + @asyncio.coroutine def __anext__(self): ret = yield from self.fetchone() diff --git a/aiopg/sa/result.py b/aiopg/sa/result.py index 2c3c9241..6452814a 100644 --- a/aiopg/sa/result.py +++ b/aiopg/sa/result.py @@ -5,7 +5,7 @@ from sqlalchemy.sql import expression, sqltypes -from ..utils import PY_35 +from ..utils import PY_35, PY_352 from . import exc @@ -324,10 +324,13 @@ def __iter__(self): yield row if PY_35: # pragma: no branch - @asyncio.coroutine + def __aiter__(self): return self + if not PY_352: + __aiter__ = asyncio.coroutine(__aiter__) + @asyncio.coroutine def __anext__(self): ret = yield from self.fetchone() diff --git a/aiopg/utils.py b/aiopg/utils.py index c22120c0..84a768d2 100644 --- a/aiopg/utils.py +++ b/aiopg/utils.py @@ -3,6 +3,8 @@ PY_35 = sys.version_info >= (3, 5) +PY_352 = sys.version_info >= (3, 5, 2) + if PY_35: from collections.abc import Coroutine base = Coroutine @@ -84,10 +86,14 @@ def __aexit__(self, exc_type, exc, tb): class _SAConnectionContextManager(_ContextManager): if PY_35: # pragma: no branch - @asyncio.coroutine - def __aiter__(self): - result = yield from self._coro - return result + if PY_352: + def __aiter__(self): + return self._coro + else: + @asyncio.coroutine + def __aiter__(self): + result = yield from self._coro + return result class _PoolContextManager(_ContextManager):