Skip to content

Commit

Permalink
Fix urllib3 warning with conditional import
Browse files Browse the repository at this point in the history
This will fix the warning for urllib3 1.26.x. To support urllib3
2.x we will need to vendor PyOpenSSLContext instead.
  • Loading branch information
pquentin committed Oct 24, 2022
1 parent 06f1053 commit 19b3990
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
12 changes: 0 additions & 12 deletions requests_toolbelt/_compat.py
Expand Up @@ -49,18 +49,6 @@
except ImportError:
from urllib3.contrib import appengine as gaecontrib

if requests.__build__ < 0x021200:
PyOpenSSLContext = None
else:
try:
from requests.packages.urllib3.contrib.pyopenssl \
import PyOpenSSLContext
except ImportError:
try:
from urllib3.contrib.pyopenssl import PyOpenSSLContext
except ImportError:
PyOpenSSLContext = None

PY3 = sys.version_info > (3, 0)

if PY3:
Expand Down
20 changes: 19 additions & 1 deletion requests_toolbelt/adapters/x509.py
Expand Up @@ -18,7 +18,6 @@
from requests.adapters import HTTPAdapter
import requests

from .._compat import PyOpenSSLContext
from .. import exceptions as exc

"""
Expand All @@ -32,6 +31,9 @@
from _ssl import PROTOCOL_SSLv23 as PROTOCOL


PyOpenSSLContext = None


class X509Adapter(HTTPAdapter):
r"""Adapter for use with X.509 certificates.
Expand Down Expand Up @@ -81,6 +83,7 @@ class X509Adapter(HTTPAdapter):
"""

def __init__(self, *args, **kwargs):
self._import_pyopensslcontext()
self._check_version()
cert_bytes = kwargs.pop('cert_bytes', None)
pk_bytes = kwargs.pop('pk_bytes', None)
Expand Down Expand Up @@ -118,6 +121,21 @@ def proxy_manager_for(self, *args, **kwargs):
kwargs['ssl_context'] = self.ssl_context
return super(X509Adapter, self).proxy_manager_for(*args, **kwargs)

def _import_pyopensslcontext(self):
global PyOpenSSLContext

if requests.__build__ < 0x021200:
PyOpenSSLContext = None
else:
try:
from requests.packages.urllib3.contrib.pyopenssl \
import PyOpenSSLContext
except ImportError:
try:
from urllib3.contrib.pyopenssl import PyOpenSSLContext
except ImportError:
PyOpenSSLContext = None

def _check_version(self):
if PyOpenSSLContext is None:
raise exc.VersionMismatchError(
Expand Down
3 changes: 3 additions & 0 deletions tests/test_x509_adapter.py
Expand Up @@ -24,6 +24,9 @@

REQUESTS_SUPPORTS_SSL_CONTEXT = requests.__build__ >= 0x021200

pytestmark = pytest.mark.filterwarnings(
"ignore:'urllib3.contrib.pyopenssl' module is deprecated:DeprecationWarning")


class TestX509Adapter(unittest.TestCase):
"""Tests a simple requests.get() call using a .p12 cert.
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Expand Up @@ -20,7 +20,7 @@ deps =
betamax>0.5.0
trustme
commands =
py.test {posargs}
pytest -W error::DeprecationWarning {posargs}

[testenv:noopenssl]
basepython = python3.7
Expand All @@ -31,7 +31,7 @@ deps =
mock;python_version<"3.3"
betamax>0.5.0
commands =
py.test {posargs}
pytest -W error::DeprecationWarning {posargs}

[testenv:py27-flake8]
basepython = python2.7
Expand Down

0 comments on commit 19b3990

Please sign in to comment.