Skip to content

Commit

Permalink
Fix #2540: Drop await aiohttp.request() syntax for sake of context ma…
Browse files Browse the repository at this point in the history
…nager
  • Loading branch information
asvetlov committed Nov 20, 2017
1 parent 8e7985a commit dda5766
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGES/2540.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Drop `resp = await aiohttp.request(...)` syntax for sake of `async
with aiohttp.request(...) as resp:`.
28 changes: 11 additions & 17 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,28 +757,22 @@ def __aexit__(self, exc_type, exc, tb):
yield from self._resp.close()


class _SessionRequestContextManager(_RequestContextManager):
class _SessionRequestContextManager:

__slots__ = _RequestContextManager.__slots__ + ('_session', )
__slots__ = ('_coro', '_resp', '_session')

def __init__(self, coro, session):
super().__init__(coro)
self._coro = coro
self._resp = None
self._session = session

@asyncio.coroutine
def __iter__(self):
try:
return (yield from self._coro)
except Exception:
yield from self._session.close()
raise
async def __aenter__(self):
self._resp = await self._coro()
return self._resp

def __await__(self):
try:
return (yield from self._coro)
except Exception:
yield from self._session.close()
raise
async def __exit__(self, exc_type, exc_val, exc_tb):
self._resp.close()
await self._session.close()


def request(method, url, *,
Expand Down Expand Up @@ -859,4 +853,4 @@ def request(method, url, *,
read_until_eof=read_until_eof,
proxy=proxy,
proxy_auth=proxy_auth,),
session=session)
session)
17 changes: 12 additions & 5 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -2232,7 +2232,7 @@ async def close(self):
assert session.closed


async def test_aiohttp_request(loop, test_server):
async def test_aiohttp_request_context_manager(loop, test_server):
async def handler(request):
return web.Response()

Expand All @@ -2244,7 +2244,14 @@ async def handler(request):
await resp.read()
assert resp.status == 200

resp = await aiohttp.request('GET', server.make_url('/'), loop=loop)
await resp.read()
assert resp.status == 200
assert resp.connection is None

async def test_aiohttp_request_coroutine(loop, test_server):
async def handler(request):
return web.Response()

app = web.Application()
app.router.add_get('/', handler)
server = await test_server(app)

with pytest.raises(TypeError):
await aiohttp.request('GET', server.make_url('/'), loop=loop)
6 changes: 4 additions & 2 deletions tests/test_client_functional_oldstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def test_POST_DATA_with_charset(self):
self.assertEqual('текст', field['data'])
self.assertEqual(r.status, 200)
r.close()
session.close()
self.loop.run_until_complete(session.close())

def test_POST_DATA_with_charset_pub_request(self):
with run_server(self.loop, router=Functional) as httpd:
Expand All @@ -383,8 +383,9 @@ def test_POST_DATA_with_charset_pub_request(self):
form.add_field('name', 'текст',
content_type='text/plain; charset=koi8-r')

session = client.ClientSession(loop=self.loop)
r = self.loop.run_until_complete(
aiohttp.request('post', url, data=form, loop=self.loop))
session.request('post', url, data=form))
content = self.loop.run_until_complete(r.json())

self.assertEqual(1, len(content['multipart-data']))
Expand All @@ -393,6 +394,7 @@ def test_POST_DATA_with_charset_pub_request(self):
self.assertEqual('текст', field['data'])
self.assertEqual(r.status, 200)
r.close()
self.loop.run_until_complete(session.close())

def test_POST_DATA_with_content_transfer_encoding(self):
with run_server(self.loop, router=Functional) as httpd:
Expand Down

0 comments on commit dda5766

Please sign in to comment.