From 14a864a63700668cfe84565bcef0dd26846afabb Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 29 Jan 2017 13:15:49 +0900 Subject: [PATCH] Support requests-2.0.0 (experimental) (refs: #3367) --- CHANGES | 2 ++ setup.py | 2 +- sphinx/util/requests.py | 37 ++++++++++++++++++++++++++----------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/CHANGES b/CHANGES index b3c70ee33..783da5bbd 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,8 @@ Deprecated Features added -------------- +* Support requests-2.0.0 (experimental) (refs: #3367) + Bugs fixed ---------- diff --git a/setup.py b/setup.py index fa0be674f..177ae8da0 100644 --- a/setup.py +++ b/setup.py @@ -50,7 +50,7 @@ requires = [ 'babel>=1.3,!=2.0', 'alabaster>=0.7,<0.8', 'imagesize', - 'requests>=2.4.0', + 'requests>=2.0.0', ] extras_require = { # Environment Marker works for wheel 0.24 or later diff --git a/sphinx/util/requests.py b/sphinx/util/requests.py index 8b7204c2f..674e00900 100644 --- a/sphinx/util/requests.py +++ b/sphinx/util/requests.py @@ -11,18 +11,30 @@ from __future__ import absolute_import -import requests import warnings +from contextlib import contextmanager + +import requests import pkg_resources from six import string_types from six.moves.urllib.parse import urlsplit try: - from requests.packages.urllib3.exceptions import SSLError, InsecureRequestWarning + from requests.packages.urllib3.exceptions import SSLError except ImportError: # python-requests package in Debian jessie does not provide ``requests.packages.urllib3``. # So try to import the exceptions from urllib3 package. - from urllib3.exceptions import SSLError, InsecureRequestWarning + from urllib3.exceptions import SSLError + +try: + from requests.packages.urllib3.exceptions import InsecureRequestWarning +except ImportError: + try: + # for Debian-jessie + from urllib3.exceptions import InsecureRequestWarning + except ImportError: + # for requests < 2.4.0 + InsecureRequestWarning = None # try to load requests[security] try: @@ -65,6 +77,15 @@ def is_ssl_error(exc): return False +@contextmanager +def ignore_insecure_warning(**kwargs): + with warnings.catch_warnings(): + if not kwargs.get('verify') and InsecureRequestWarning: + # ignore InsecureRequestWarning if verify=False + warnings.filterwarnings("ignore", category=InsecureRequestWarning) + yield + + def _get_tls_cacert(url, config): """Get addiotinal CA cert for a specific URL. @@ -96,10 +117,7 @@ def get(url, **kwargs): if config: kwargs.setdefault('verify', _get_tls_cacert(url, config)) - with warnings.catch_warnings(): - if not kwargs.get('verify'): - # ignore InsecureRequestWarning if verify=False - warnings.filterwarnings("ignore", category=InsecureRequestWarning) + with ignore_insecure_warning(**kwargs): return requests.get(url, **kwargs) @@ -112,8 +130,5 @@ def head(url, **kwargs): if config: kwargs.setdefault('verify', _get_tls_cacert(url, config)) - with warnings.catch_warnings(): - if not kwargs.get('verify'): - # ignore InsecureRequestWarning if verify=False - warnings.filterwarnings("ignore", category=InsecureRequestWarning) + with ignore_insecure_warning(**kwargs): return requests.get(url, **kwargs)