diff --git a/requests_toolbelt/_compat.py b/requests_toolbelt/_compat.py index 7927a38..21230ee 100644 --- a/requests_toolbelt/_compat.py +++ b/requests_toolbelt/_compat.py @@ -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: diff --git a/requests_toolbelt/adapters/x509.py b/requests_toolbelt/adapters/x509.py index 21bfacb..aff3770 100644 --- a/requests_toolbelt/adapters/x509.py +++ b/requests_toolbelt/adapters/x509.py @@ -18,7 +18,6 @@ from requests.adapters import HTTPAdapter import requests -from .._compat import PyOpenSSLContext from .. import exceptions as exc """ @@ -32,6 +31,9 @@ from _ssl import PROTOCOL_SSLv23 as PROTOCOL +PyOpenSSLContext = None + + class X509Adapter(HTTPAdapter): r"""Adapter for use with X.509 certificates. @@ -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) @@ -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( diff --git a/tests/test_x509_adapter.py b/tests/test_x509_adapter.py index 203bf60..84a10e2 100644 --- a/tests/test_x509_adapter.py +++ b/tests/test_x509_adapter.py @@ -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. diff --git a/tox.ini b/tox.ini index bcf4782..253c8ba 100644 --- a/tox.ini +++ b/tox.ini @@ -20,7 +20,7 @@ deps = betamax>0.5.0 trustme commands = - py.test {posargs} + pytest -W error::DeprecationWarning {posargs} [testenv:noopenssl] basepython = python3.7 @@ -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