jpayne@69: """ jpayne@69: requests.compat jpayne@69: ~~~~~~~~~~~~~~~ jpayne@69: jpayne@69: This module previously handled import compatibility issues jpayne@69: between Python 2 and Python 3. It remains for backwards jpayne@69: compatibility until the next major version. jpayne@69: """ jpayne@69: jpayne@69: import importlib jpayne@69: import sys jpayne@69: jpayne@69: # ------------------- jpayne@69: # Character Detection jpayne@69: # ------------------- jpayne@69: jpayne@69: jpayne@69: def _resolve_char_detection(): jpayne@69: """Find supported character detection libraries.""" jpayne@69: chardet = None jpayne@69: for lib in ("chardet", "charset_normalizer"): jpayne@69: if chardet is None: jpayne@69: try: jpayne@69: chardet = importlib.import_module(lib) jpayne@69: except ImportError: jpayne@69: pass jpayne@69: return chardet jpayne@69: jpayne@69: jpayne@69: chardet = _resolve_char_detection() jpayne@69: jpayne@69: # ------- jpayne@69: # Pythons jpayne@69: # ------- jpayne@69: jpayne@69: # Syntax sugar. jpayne@69: _ver = sys.version_info jpayne@69: jpayne@69: #: Python 2.x? jpayne@69: is_py2 = _ver[0] == 2 jpayne@69: jpayne@69: #: Python 3.x? jpayne@69: is_py3 = _ver[0] == 3 jpayne@69: jpayne@69: # json/simplejson module import resolution jpayne@69: has_simplejson = False jpayne@69: try: jpayne@69: import simplejson as json jpayne@69: jpayne@69: has_simplejson = True jpayne@69: except ImportError: jpayne@69: import json jpayne@69: jpayne@69: if has_simplejson: jpayne@69: from simplejson import JSONDecodeError jpayne@69: else: jpayne@69: from json import JSONDecodeError jpayne@69: jpayne@69: # Keep OrderedDict for backwards compatibility. jpayne@69: from collections import OrderedDict jpayne@69: from collections.abc import Callable, Mapping, MutableMapping jpayne@69: from http import cookiejar as cookielib jpayne@69: from http.cookies import Morsel jpayne@69: from io import StringIO jpayne@69: jpayne@69: # -------------- jpayne@69: # Legacy Imports jpayne@69: # -------------- jpayne@69: from urllib.parse import ( jpayne@69: quote, jpayne@69: quote_plus, jpayne@69: unquote, jpayne@69: unquote_plus, jpayne@69: urldefrag, jpayne@69: urlencode, jpayne@69: urljoin, jpayne@69: urlparse, jpayne@69: urlsplit, jpayne@69: urlunparse, jpayne@69: ) jpayne@69: from urllib.request import ( jpayne@69: getproxies, jpayne@69: getproxies_environment, jpayne@69: parse_http_list, jpayne@69: proxy_bypass, jpayne@69: proxy_bypass_environment, jpayne@69: ) jpayne@69: jpayne@69: builtin_str = str jpayne@69: str = str jpayne@69: bytes = bytes jpayne@69: basestring = (str, bytes) jpayne@69: numeric_types = (int, float) jpayne@69: integer_types = (int,)