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