]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Fix compat with requests 2.16 (unvendored urllib3)
authorKevin McCarthy <me@kevinmccarthy.org>
Sun, 28 May 2017 01:11:06 +0000 (15:11 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Sun, 28 May 2017 01:40:38 +0000 (15:40 -1000)
The new version of requests un-vendors urllib3, with a nifty hack:
https://github.com/kennethreitz/requests/blob/master/requests/packages.py

Unfortunately messing directly with sys.modules causes some weird
behavior that I don't entirely understand.  Avoiding the extra import to
requests.packages as part of VCR's initialization seems to sidestep the
issue.

Closes #311

vcr/patch.py
vcr/stubs/requests_stubs.py

index ada3c922f255806f3c60e45cd39e35fb0e59ba1f..e61b153e9d265b7d05075fce957a68e5cc645c34 100644 (file)
@@ -12,16 +12,6 @@ _HTTPConnection = httplib.HTTPConnection
 _HTTPSConnection = httplib.HTTPSConnection
 
 
-# Try to save the original types for requests
-try:
-    import requests.packages.urllib3.connectionpool as cpool
-except ImportError:  # pragma: no cover
-    pass
-else:
-    _VerifiedHTTPSConnection = cpool.VerifiedHTTPSConnection
-    _cpoolHTTPConnection = cpool.HTTPConnection
-    _cpoolHTTPSConnection = cpool.HTTPSConnection
-
 # Try to save the original types for boto3
 try:
     import botocore.vendored.requests.packages.urllib3.connectionpool as cpool
@@ -32,14 +22,30 @@ else:
     _cpoolBoto3HTTPConnection = cpool.HTTPConnection
     _cpoolBoto3HTTPSConnection = cpool.HTTPSConnection
 
-
+cpool = None
 # Try to save the original types for urllib3
 try:
-    import urllib3
+    import urllib3.connectionpool as cpool
 except ImportError:  # pragma: no cover
     pass
 else:
-    _VerifiedHTTPSConnection = urllib3.connectionpool.VerifiedHTTPSConnection
+    _VerifiedHTTPSConnection = cpool.VerifiedHTTPSConnection
+    _cpoolHTTPConnection = cpool.HTTPConnection
+    _cpoolHTTPSConnection = cpool.HTTPSConnection
+
+# Try to save the original types for requests
+try:
+    if not cpool:
+        import requests.packages.urllib3.connectionpool as cpool
+except ImportError:  # pragma: no cover
+    pass
+else:
+    _VerifiedHTTPSConnection = cpool.VerifiedHTTPSConnection
+    _cpoolHTTPConnection = cpool.HTTPConnection
+    _cpoolHTTPSConnection = cpool.HTTPSConnection
+
+
+
 
 
 # Try to save the original types for httplib2
@@ -176,10 +182,9 @@ class CassettePatcherBuilder(object):
 
     def _requests(self):
         try:
-            import requests.packages.urllib3.connectionpool as cpool
-        except ImportError:  # pragma: no cover
+            from .stubs import requests_stubs
+        except ImportError: # pragma: no cover
             return ()
-        from .stubs import requests_stubs
         return self._urllib3_patchers(cpool, requests_stubs)
 
     def _boto3(self):
index 8cae533bd16b80b570b4af0fa63436bbbf95640e..4aa2c82c7cdc58156bafd9a5409cdbb4deddba13 100644 (file)
@@ -1,9 +1,9 @@
 '''Stubs for requests'''
 
 try:
-    from requests.packages.urllib3.connectionpool import HTTPConnection, VerifiedHTTPSConnection
-except ImportError:
     from urllib3.connectionpool import HTTPConnection, VerifiedHTTPSConnection
+except ImportError:
+    from requests.packages.urllib3.connectionpool import HTTPConnection, VerifiedHTTPSConnection
 
 from ..stubs import VCRHTTPConnection, VCRHTTPSConnection