From 24af48d468dbbc24b1aeb3ed6804356f91cc2b4a Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Sat, 13 May 2023 16:14:29 +0200 Subject: [PATCH 1/2] Drop support for botocore <1.11.0 --- tests/integration/test_boto3.py | 19 ---------------- vcr/patch.py | 40 +++++++-------------------------- vcr/stubs/boto3_stubs.py | 15 ++----------- 3 files changed, 10 insertions(+), 64 deletions(-) diff --git a/tests/integration/test_boto3.py b/tests/integration/test_boto3.py index cff712a8..0bdb6a01 100644 --- a/tests/integration/test_boto3.py +++ b/tests/integration/test_boto3.py @@ -57,25 +57,6 @@ def _get_user(client=None, user_name=IAM_USER_NAME): return _get_user -@boto3_skip_vendored_requests -def test_boto_vendored_stubs(tmpdir): - with vcr.use_cassette(str(tmpdir.join("boto3-stubs.yml"))): - # Perform the imports within the patched context so that - # HTTPConnection, VerifiedHTTPSConnection refers to the patched version. - from botocore.vendored.requests.packages.urllib3.connectionpool import ( - HTTPConnection, - VerifiedHTTPSConnection, - ) - - from vcr.stubs.boto3_stubs import VCRRequestsHTTPConnection, VCRRequestsHTTPSConnection - - # Prove that the class was patched by the stub and that we can instantiate it. - assert issubclass(HTTPConnection, VCRRequestsHTTPConnection) - assert issubclass(VerifiedHTTPSConnection, VCRRequestsHTTPSConnection) - HTTPConnection("hostname.does.not.matter") - VerifiedHTTPSConnection("hostname.does.not.matter") - - @pytest.mark.skipif( os.environ.get("TRAVIS_PULL_REQUEST") != "false", reason="Encrypted Environment Variables from Travis Repository Settings" diff --git a/vcr/patch.py b/vcr/patch.py index a283287f..169d1948 100644 --- a/vcr/patch.py +++ b/vcr/patch.py @@ -16,15 +16,16 @@ # Try to save the original types for boto3 try: from botocore.awsrequest import AWSHTTPConnection, AWSHTTPSConnection -except ImportError: +except ImportError as e: try: - import botocore.vendored.requests.packages.urllib3.connectionpool as cpool + import botocore.vendored.requests # noqa: F401 except ImportError: # pragma: no cover pass else: - _Boto3VerifiedHTTPSConnection = cpool.VerifiedHTTPSConnection - _cpoolBoto3HTTPConnection = cpool.HTTPConnection - _cpoolBoto3HTTPSConnection = cpool.HTTPSConnection + raise RuntimeError( + "vcrpy >=4.2.2 and botocore <1.11.0 are not compatible" + "; please upgrade botocore (or downgrade vcrpy)" + ) from e else: _Boto3VerifiedHTTPSConnection = AWSHTTPSConnection _cpoolBoto3HTTPConnection = AWSHTTPConnection @@ -203,15 +204,7 @@ def _boto3(self): # botocore using awsrequest import botocore.awsrequest as cpool except ImportError: # pragma: no cover - try: - # botocore using vendored requests - import botocore.vendored.requests.packages.urllib3.connectionpool as cpool - except ImportError: # pragma: no cover - pass - else: - from .stubs import boto3_stubs - - yield self._urllib3_patchers(cpool, boto3_stubs) + pass else: from .stubs import boto3_stubs @@ -460,24 +453,7 @@ def reset_patchers(): # unpatch botocore with awsrequest import botocore.awsrequest as cpool except ImportError: # pragma: no cover - try: - # unpatch botocore with vendored requests - import botocore.vendored.requests.packages.urllib3.connectionpool as cpool - except ImportError: # pragma: no cover - pass - else: - # unpatch requests v1.x - yield mock.patch.object(cpool, "VerifiedHTTPSConnection", _Boto3VerifiedHTTPSConnection) - yield mock.patch.object(cpool, "HTTPConnection", _cpoolBoto3HTTPConnection) - # unpatch requests v2.x - if hasattr(cpool.HTTPConnectionPool, "ConnectionCls"): - yield mock.patch.object(cpool.HTTPConnectionPool, "ConnectionCls", _cpoolBoto3HTTPConnection) - yield mock.patch.object( - cpool.HTTPSConnectionPool, "ConnectionCls", _cpoolBoto3HTTPSConnection - ) - - if hasattr(cpool, "HTTPSConnection"): - yield mock.patch.object(cpool, "HTTPSConnection", _cpoolBoto3HTTPSConnection) + pass else: if hasattr(cpool.AWSHTTPConnectionPool, "ConnectionCls"): yield mock.patch.object(cpool.AWSHTTPConnectionPool, "ConnectionCls", _cpoolBoto3HTTPConnection) diff --git a/vcr/stubs/boto3_stubs.py b/vcr/stubs/boto3_stubs.py index a4d349aa..37fd9e32 100644 --- a/vcr/stubs/boto3_stubs.py +++ b/vcr/stubs/boto3_stubs.py @@ -1,17 +1,6 @@ """Stubs for boto3""" -try: - # boto using awsrequest - from botocore.awsrequest import AWSHTTPConnection as HTTPConnection - from botocore.awsrequest import AWSHTTPSConnection as VerifiedHTTPSConnection - -except ImportError: # pragma: nocover - # boto using vendored requests - # urllib3 defines its own HTTPConnection classes, which boto3 goes ahead and assumes - # you're using. It includes some polyfills for newer features missing in older pythons. - try: - from urllib3.connectionpool import HTTPConnection, VerifiedHTTPSConnection - except ImportError: # pragma: nocover - from requests.packages.urllib3.connectionpool import HTTPConnection, VerifiedHTTPSConnection +from botocore.awsrequest import AWSHTTPConnection as HTTPConnection +from botocore.awsrequest import AWSHTTPSConnection as VerifiedHTTPSConnection from ..stubs import VCRHTTPConnection, VCRHTTPSConnection From 31c8dc0a1e260591722e67494bd07b9b4991a18d Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Sat, 13 May 2023 16:27:56 +0200 Subject: [PATCH 2/2] Drop support for requests <2.16.2 --- vcr/patch.py | 42 +++++++------------------------------ vcr/stubs/requests_stubs.py | 5 +---- 2 files changed, 8 insertions(+), 39 deletions(-) diff --git a/vcr/patch.py b/vcr/patch.py index 169d1948..83cd01a3 100644 --- a/vcr/patch.py +++ b/vcr/patch.py @@ -44,14 +44,16 @@ # Try to save the original types for requests try: - if not cpool: - import requests.packages.urllib3.connectionpool as cpool + import requests except ImportError: # pragma: no cover pass else: - _VerifiedHTTPSConnection = cpool.VerifiedHTTPSConnection - _cpoolHTTPConnection = cpool.HTTPConnection - _cpoolHTTPSConnection = cpool.HTTPSConnection + if requests.__build__ < 0x021602: + raise RuntimeError( + "vcrpy >=4.2.2 and requests <2.16.2 are not compatible" + "; please upgrade requests (or downgrade vcrpy)" + ) + # Try to save the original types for httplib2 try: @@ -407,36 +409,6 @@ def reset_patchers(): yield mock.patch.object(httplib, "HTTPConnection", _HTTPConnection) yield mock.patch.object(httplib, "HTTPSConnection", _HTTPSConnection) - try: - import requests - - if requests.__build__ < 0x021603: - # Avoid double unmock if requests 2.16.3 - # First, this is pointless, requests.packages.urllib3 *IS* urllib3 (see packages.py) - # Second, this is unmocking twice the same classes with different namespaces - # and is creating weird issues and bugs: - # > AssertionError: assert - # > is - # This assert should work!!! - # Note that this also means that now, requests.packages is never imported - # if requests 2.16.3 or greater is used with VCRPy. - import requests.packages.urllib3.connectionpool as cpool - else: - raise ImportError("Skip requests not vendored anymore") - except ImportError: # pragma: no cover - pass - else: - # unpatch requests v1.x - yield mock.patch.object(cpool, "VerifiedHTTPSConnection", _VerifiedHTTPSConnection) - yield mock.patch.object(cpool, "HTTPConnection", _cpoolHTTPConnection) - # unpatch requests v2.x - if hasattr(cpool.HTTPConnectionPool, "ConnectionCls"): - yield mock.patch.object(cpool.HTTPConnectionPool, "ConnectionCls", _cpoolHTTPConnection) - yield mock.patch.object(cpool.HTTPSConnectionPool, "ConnectionCls", _cpoolHTTPSConnection) - - if hasattr(cpool, "HTTPSConnection"): - yield mock.patch.object(cpool, "HTTPSConnection", _cpoolHTTPSConnection) - try: import urllib3.connectionpool as cpool except ImportError: # pragma: no cover diff --git a/vcr/stubs/requests_stubs.py b/vcr/stubs/requests_stubs.py index e547b811..eb70f1a8 100644 --- a/vcr/stubs/requests_stubs.py +++ b/vcr/stubs/requests_stubs.py @@ -1,9 +1,6 @@ """Stubs for requests""" -try: - from urllib3.connectionpool import HTTPConnection, VerifiedHTTPSConnection -except ImportError: - from requests.packages.urllib3.connectionpool import HTTPConnection, VerifiedHTTPSConnection +from urllib3.connectionpool import HTTPConnection, VerifiedHTTPSConnection from ..stubs import VCRHTTPConnection, VCRHTTPSConnection