Skip to content

Commit

Permalink
Merge PR #6877
Browse files Browse the repository at this point in the history
(cherry picked from commit 5bf9b4a)
  • Loading branch information
webknjaz committed Sep 7, 2022
1 parent 6190065 commit ae40e98
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGES/6757.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Work around the changes in 3.11, e.g. :py:class:`~asyncio.TimeoutError` is an :py:class:`OSError`,
and :py:class:`~unittest.IsolatedAsyncioTestCase` calls :py:function:`~asyncio.set_event_loop`
differently -- by :user:`graingert`.
2 changes: 2 additions & 0 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,8 @@ async def _request(
except ClientError:
raise
except OSError as exc:
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
raise
raise ClientOSError(*exc.args) from exc

self._cookie_jar.update_cookies(resp.cookies, resp.url)
Expand Down
15 changes: 9 additions & 6 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,12 +588,15 @@ async def write_bytes(

await writer.write_eof()
except OSError as exc:
new_exc = ClientOSError(
exc.errno, "Can not write request body for %s" % self.url
)
new_exc.__context__ = exc
new_exc.__cause__ = exc
protocol.set_exception(new_exc)
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
protocol.set_exception(exc)
else:
new_exc = ClientOSError(
exc.errno, "Can not write request body for %s" % self.url
)
new_exc.__context__ = exc
new_exc.__cause__ = exc
protocol.set_exception(new_exc)
except asyncio.CancelledError as exc:
if not conn.closed:
protocol.set_exception(exc)
Expand Down
10 changes: 10 additions & 0 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,8 @@ async def _wrap_create_connection(
except ssl_errors as exc:
raise ClientConnectorSSLError(req.connection_key, exc) from exc
except OSError as exc:
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
raise
raise client_error(req.connection_key, exc) from exc

def _fail_on_no_start_tls(self, req: "ClientRequest") -> None:
Expand Down Expand Up @@ -1104,6 +1106,8 @@ async def _start_tls_connection(
except ssl_errors as exc:
raise ClientConnectorSSLError(req.connection_key, exc) from exc
except OSError as exc:
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
raise
raise client_error(req.connection_key, exc) from exc
except TypeError as type_err:
# Example cause looks like this:
Expand Down Expand Up @@ -1155,6 +1159,8 @@ def drop_exception(fut: "asyncio.Future[List[Dict[str, Any]]]") -> None:
host_resolved.add_done_callback(drop_exception)
raise
except OSError as exc:
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
raise
# in case of proxy it is not ClientProxyConnectionError
# it is problem of resolving proxy ip itself
raise ClientConnectorError(req.connection_key, exc) from exc
Expand Down Expand Up @@ -1373,6 +1379,8 @@ async def _create_connection(
self._factory, self._path
)
except OSError as exc:
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
raise
raise UnixClientConnectorError(self.path, req.connection_key, exc) from exc

return cast(ResponseHandler, proto)
Expand Down Expand Up @@ -1438,6 +1446,8 @@ async def _create_connection(
# other option is to manually set transport like
# `proto.transport = trans`
except OSError as exc:
if exc.errno is None and isinstance(exc, asyncio.TimeoutError):
raise
raise ClientConnectorError(req.connection_key, exc) from exc

return cast(ResponseHandler, proto)
16 changes: 10 additions & 6 deletions aiohttp/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,12 @@ def get_app(self) -> Application:
raise RuntimeError("Did you forget to define get_application()?")

def setUp(self) -> None:
try:
self.loop = asyncio.get_running_loop()
except (AttributeError, RuntimeError): # AttributeError->py36
self.loop = asyncio.get_event_loop_policy().get_event_loop()
if not PY_38:
asyncio.get_event_loop().run_until_complete(self.asyncSetUp())

self.loop.run_until_complete(self.setUpAsync())
async def asyncSetUp(self) -> None:
self.loop = asyncio.get_running_loop()
return await self.setUpAsync()

async def setUpAsync(self) -> None:
self.app = await self.get_application()
Expand All @@ -470,7 +470,11 @@ async def setUpAsync(self) -> None:
await self.client.start_server()

def tearDown(self) -> None:
self.loop.run_until_complete(self.tearDownAsync())
if not PY_38:
self.loop.run_until_complete(self.asyncTearDown())

async def asyncTearDown(self) -> None:
return await self.tearDownAsync()

async def tearDownAsync(self) -> None:
await self.client.close()
Expand Down

0 comments on commit ae40e98

Please sign in to comment.