Skip to content

Commit 19b3990

Browse files
committed
Fix urllib3 warning with conditional import
This will fix the warning for urllib3 1.26.x. To support urllib3 2.x we will need to vendor PyOpenSSLContext instead.
1 parent 06f1053 commit 19b3990

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

requests_toolbelt/_compat.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,6 @@
4949
except ImportError:
5050
from urllib3.contrib import appengine as gaecontrib
5151

52-
if requests.__build__ < 0x021200:
53-
PyOpenSSLContext = None
54-
else:
55-
try:
56-
from requests.packages.urllib3.contrib.pyopenssl \
57-
import PyOpenSSLContext
58-
except ImportError:
59-
try:
60-
from urllib3.contrib.pyopenssl import PyOpenSSLContext
61-
except ImportError:
62-
PyOpenSSLContext = None
63-
6452
PY3 = sys.version_info > (3, 0)
6553

6654
if PY3:

requests_toolbelt/adapters/x509.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from requests.adapters import HTTPAdapter
1919
import requests
2020

21-
from .._compat import PyOpenSSLContext
2221
from .. import exceptions as exc
2322

2423
"""
@@ -32,6 +31,9 @@
3231
from _ssl import PROTOCOL_SSLv23 as PROTOCOL
3332

3433

34+
PyOpenSSLContext = None
35+
36+
3537
class X509Adapter(HTTPAdapter):
3638
r"""Adapter for use with X.509 certificates.
3739
@@ -81,6 +83,7 @@ class X509Adapter(HTTPAdapter):
8183
"""
8284

8385
def __init__(self, *args, **kwargs):
86+
self._import_pyopensslcontext()
8487
self._check_version()
8588
cert_bytes = kwargs.pop('cert_bytes', None)
8689
pk_bytes = kwargs.pop('pk_bytes', None)
@@ -118,6 +121,21 @@ def proxy_manager_for(self, *args, **kwargs):
118121
kwargs['ssl_context'] = self.ssl_context
119122
return super(X509Adapter, self).proxy_manager_for(*args, **kwargs)
120123

124+
def _import_pyopensslcontext(self):
125+
global PyOpenSSLContext
126+
127+
if requests.__build__ < 0x021200:
128+
PyOpenSSLContext = None
129+
else:
130+
try:
131+
from requests.packages.urllib3.contrib.pyopenssl \
132+
import PyOpenSSLContext
133+
except ImportError:
134+
try:
135+
from urllib3.contrib.pyopenssl import PyOpenSSLContext
136+
except ImportError:
137+
PyOpenSSLContext = None
138+
121139
def _check_version(self):
122140
if PyOpenSSLContext is None:
123141
raise exc.VersionMismatchError(

tests/test_x509_adapter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
REQUESTS_SUPPORTS_SSL_CONTEXT = requests.__build__ >= 0x021200
2626

27+
pytestmark = pytest.mark.filterwarnings(
28+
"ignore:'urllib3.contrib.pyopenssl' module is deprecated:DeprecationWarning")
29+
2730

2831
class TestX509Adapter(unittest.TestCase):
2932
"""Tests a simple requests.get() call using a .p12 cert.

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ deps =
2020
betamax>0.5.0
2121
trustme
2222
commands =
23-
py.test {posargs}
23+
pytest -W error::DeprecationWarning {posargs}
2424

2525
[testenv:noopenssl]
2626
basepython = python3.7
@@ -31,7 +31,7 @@ deps =
3131
mock;python_version<"3.3"
3232
betamax>0.5.0
3333
commands =
34-
py.test {posargs}
34+
pytest -W error::DeprecationWarning {posargs}
3535

3636
[testenv:py27-flake8]
3737
basepython = python2.7

0 commit comments

Comments
 (0)