Skip to content

Commit

Permalink
Add extra attrs to CoroGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jul 4, 2017
1 parent 97db67b commit e176536
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 60 deletions.
52 changes: 5 additions & 47 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from .connector import * # noqa
from .connector import TCPConnector
from .cookiejar import CookieJar
from .helpers import (PY_35, CeilTimeout, TimeoutHandle, deprecated_noop,
sentinel)
from .helpers import (PY_35, CeilTimeout, TimeoutHandle, _BaseCoroMixin,
deprecated_noop, sentinel)
from .http import WS_KEY, WebSocketReader, WebSocketWriter
from .streams import FlowControlDataQueue

Expand Down Expand Up @@ -589,63 +589,21 @@ def __aexit__(self, exc_type, exc_val, exc_tb):
self.close()


if PY_35:
from collections.abc import Coroutine
base = Coroutine
else:
base = object
class _BaseRequestContextManager(_BaseCoroMixin):


class _BaseRequestContextManager(base):

__slots__ = ('_coro', '_resp', 'send', 'throw', 'close')
__slots__ = ('_resp',)

def __init__(self, coro):
super().__init__(coro)
self._coro = coro
self._resp = None
self.send = coro.send
self.throw = coro.throw
self.close = coro.close

@property
def gi_frame(self):
return self._coro.gi_frame

@property
def gi_running(self):
return self._coro.gi_running

@property
def gi_code(self):
return self._coro.gi_code

def __next__(self):
return self.send(None)

@asyncio.coroutine
def __iter__(self):
resp = yield from self._coro
return resp

if PY_35:
def __await__(self):
resp = yield from self._coro
return resp

@asyncio.coroutine
def __aenter__(self):
self._resp = yield from self._coro
return self._resp


if not PY_35:
try:
from asyncio import coroutines
coroutines._COROUTINE_TYPES += (_BaseRequestContextManager,)
except: # pragma: no cover
pass # Python 3.4.2 and 3.4.3 has no coroutines._COROUTINE_TYPES


class _RequestContextManager(_BaseRequestContextManager):
if PY_35:
@asyncio.coroutine
Expand Down
69 changes: 56 additions & 13 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,81 @@
TOKEN = CHAR ^ CTL ^ SEPARATORS


class _CoroGuard:
__slots__ = ('_coro', '_msg', '_awaited')
if PY_35:
from collections.abc import Coroutine
base = Coroutine
else:
base = object

def __init__(self, coro, msg):

class _BaseCoroMixin(base):

__slots__ = ('_coro', 'send', 'throw', 'close')

def __init__(self, coro):
self._coro = coro
self.send = coro.send
self.throw = coro.throw
self.close = coro.close

@property
def gi_frame(self):
return self._coro.gi_frame

@property
def gi_running(self):
return self._coro.gi_running

@property
def gi_code(self):
return self._coro.gi_code

def __next__(self):
return self.send(None)

@asyncio.coroutine
def __iter__(self):
ret = yield from self._coro
return ret

if PY_35:
def __await__(self):
ret = yield from self._coro
return ret


if not PY_35:
try:
from asyncio import coroutines
coroutines._COROUTINE_TYPES += (_BaseCoroMixin,)
except: # pragma: no cover
pass # Python 3.4.2 and 3.4.3 has no coroutines._COROUTINE_TYPES


class _CoroGuard(_BaseCoroMixin):
__slots__ = ('_msg', '_awaited')

def __init__(self, coro, msg):
super().__init__(coro)
self._msg = msg
self._awaited = False

@asyncio.coroutine
def __iter__(self):
self._awaited = True
return self._coro.__iter__()
return super().__iter__()

if PY_35:
def __await__(self):
self._awaited = True
return (yield from self._coro)
return super().__await__()

def __del__(self):
self._coro = None
if not self._awaited:
warnings.warn(self._msg, DeprecationWarning)


if not PY_35:
try:
from asyncio import coroutines
coroutines._COROUTINE_TYPES += (_CoroGuard,)
except: # pragma: no cover
pass # Python 3.4.2 and 3.4.3 has no coroutines._COROUTINE_TYPES


coroutines = asyncio.coroutines
old_debug = coroutines._DEBUG
coroutines._DEBUG = False
Expand Down

0 comments on commit e176536

Please sign in to comment.