From 2a358e1775f94d3639a818103e195fd009e82fef Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Tue, 24 Aug 2021 23:03:25 -0700 Subject: [PATCH] add backwards compatibility (#881) --- CHANGES.rst | 4 ++++ aiobotocore/__init__.py | 16 +++++++++++++++- aiobotocore/httpsession.py | 22 ++++++++++++++++++++++ tests/test_basic_s3.py | 11 ++++++++--- tests/test_session.py | 9 ++++++--- 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 1c948ba9..04b01c3f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,5 +1,9 @@ Changes ------- +1.4.1 (2021-08-24) +^^^^^^^^^^^^^^^^^^ +* put backwards incompatible changes behind ``AIOBOTOCORE_DEPRECATED_1_4_0_APIS`` env var. This means that `#876 `_ will not work unless this env var has been set to 0. + 1.4.0 (2021-08-20) ^^^^^^^^^^^^^^^^^^ * fix retries via config `#877 `_ diff --git a/aiobotocore/__init__.py b/aiobotocore/__init__.py index 96e3ce8d..8f62f61b 100644 --- a/aiobotocore/__init__.py +++ b/aiobotocore/__init__.py @@ -1 +1,15 @@ -__version__ = '1.4.0' +# NOTE: These imports are deprecated and will be removed in 2.x +import os + +# Enabling this will enable the old http exception behavior that exposed raw aiohttp +# exceptions and old session variables available via __init__. Disabling will swap to +# botocore exceptions and will not have these imports to match botocore. +# NOTE: without setting this to 0, retries may not work, see #876 +DEPRECATED_1_4_0_APIS = int(os.getenv('AIOBOTOCORE_DEPRECATED_1_4_0_APIS', '1')) + +if DEPRECATED_1_4_0_APIS: + from .session import get_session, AioSession + + __all__ = ['get_session', 'AioSession'] + +__version__ = '1.4.1' diff --git a/aiobotocore/httpsession.py b/aiobotocore/httpsession.py index 21992b8f..a58a4684 100644 --- a/aiobotocore/httpsession.py +++ b/aiobotocore/httpsession.py @@ -14,6 +14,7 @@ EndpointConnectionError, ProxyConnectionError, ConnectTimeoutError, \ ConnectionClosedError, HTTPClientError, ReadTimeoutError, logger, get_cert_path +from aiobotocore import DEPRECATED_1_4_0_APIS from aiobotocore._endpoint_helpers import _text, _IOBaseWrapper, \ ClientResponseProxy @@ -162,22 +163,43 @@ async def send(self, request): return resp except ClientSSLError as e: + if DEPRECATED_1_4_0_APIS: + raise + raise SSLError(endpoint_url=request.url, error=e) except (ClientConnectorError, socket.gaierror) as e: + if DEPRECATED_1_4_0_APIS: + raise + raise EndpointConnectionError(endpoint_url=request.url, error=e) except (ClientProxyConnectionError, ClientHttpProxyError) as e: + if DEPRECATED_1_4_0_APIS: + raise + raise ProxyConnectionError(proxy_url=proxy_url, error=e) except ServerTimeoutError as e: + if DEPRECATED_1_4_0_APIS: + raise + raise ConnectTimeoutError(endpoint_url=request.url, error=e) except asyncio.TimeoutError as e: + if DEPRECATED_1_4_0_APIS: + raise + raise ReadTimeoutError(endpoint_url=request.url, error=e) except ServerDisconnectedError as e: + if DEPRECATED_1_4_0_APIS: + raise + raise ConnectionClosedError( error=e, request=request, endpoint_url=request.url ) except Exception as e: + if DEPRECATED_1_4_0_APIS: + raise + message = 'Exception received when sending urllib3 HTTP request' logger.debug(message, exc_info=True) raise HTTPClientError(error=e) diff --git a/tests/test_basic_s3.py b/tests/test_basic_s3.py index 2e37927c..a96ed323 100644 --- a/tests/test_basic_s3.py +++ b/tests/test_basic_s3.py @@ -3,7 +3,8 @@ import pytest import aioitertools -from botocore.exceptions import EndpointConnectionError + +from aiobotocore import httpsession async def fetch_all(pages): @@ -26,10 +27,14 @@ async def test_can_make_request(s3_client): @pytest.mark.moto @pytest.mark.asyncio -async def test_fail_proxy_request(aa_fail_proxy_config, s3_client): +async def test_fail_proxy_request(aa_fail_proxy_config, s3_client, monkeypatch): # based on test_can_make_request - with pytest.raises(EndpointConnectionError): + with pytest.raises(httpsession.ClientProxyConnectionError): + await s3_client.list_buckets() + + monkeypatch.setattr(httpsession, 'DEPRECATED_1_4_0_APIS', 0) + with pytest.raises(httpsession.EndpointConnectionError): await s3_client.list_buckets() diff --git a/tests/test_session.py b/tests/test_session.py index a17ce69d..c56cdb5a 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -1,10 +1,11 @@ import logging import pytest -from botocore.exceptions import EndpointConnectionError +from _pytest.logging import LogCaptureFixture from aiobotocore.session import AioSession from aiobotocore.config import AioConfig +from aiobotocore import httpsession @pytest.mark.moto @@ -24,7 +25,7 @@ def handler(**kwargs): @pytest.mark.moto @pytest.mark.asyncio -async def test_retry(session: AioSession, caplog): +async def test_retry(session: AioSession, caplog: LogCaptureFixture, monkeypatch): caplog.set_level(logging.DEBUG) config = AioConfig( @@ -42,7 +43,9 @@ async def test_retry(session: AioSession, caplog): 's3', config=config, aws_secret_access_key="xxx", aws_access_key_id="xxx", endpoint_url='http://localhost:7878') as client: - with pytest.raises(EndpointConnectionError): + # this needs the new style exceptions to work + monkeypatch.setattr(httpsession, 'DEPRECATED_1_4_0_APIS', 0) + with pytest.raises(httpsession.EndpointConnectionError): await client.get_object(Bucket='foo', Key='bar') assert 'sleeping for' in caplog.text