codekingpro/portable-devtools
114k
1"""2requests.compat3~~~~~~~~~~~~~~~4 5This module previously handled import compatibility issues6between Python 2 and Python 3. It remains for backwards7compatibility until the next major version.8"""9 10# pyright: reportUnusedImport=false11 12from __future__ import annotations13 14import importlib15import sys16from types import ModuleType17 18# -------19# urllib320# -------21from urllib3 import (22 __version__ as urllib3_version, # type: ignore[reportPrivateImportUsage]23)24 25# Detect which major version of urllib3 is being used.26try:27 is_urllib3_1 = int(urllib3_version.split(".")[0]) == 128except (TypeError, AttributeError):29 # If we can't discern a version, prefer old functionality.30 is_urllib3_1 = True31 32# -------------------33# Character Detection34# -------------------35 36 37def _resolve_char_detection() -> ModuleType | None:38 """Find supported character detection libraries."""39 chardet = None40 for lib in ("chardet", "charset_normalizer"):41 if chardet is None:42 try:43 chardet = importlib.import_module(lib)44 except ImportError:45 pass46 return chardet47 48 49chardet = _resolve_char_detection()50 51# -------52# Pythons53# -------54 55# Syntax sugar.56_ver = sys.version_info57 58#: Python 2.x?59is_py2 = _ver[0] == 260 61#: Python 3.x?62is_py3 = _ver[0] == 363 64# json/simplejson module import resolution65has_simplejson = False66try:67 import simplejson as json # type: ignore[import-not-found]68 69 has_simplejson = True70except ImportError:71 import json72 73if has_simplejson:74 from simplejson import JSONDecodeError # type: ignore[import-not-found]75else:76 from json import JSONDecodeError77 78# Keep OrderedDict for backwards compatibility.79from collections import OrderedDict80from collections.abc import Callable, Mapping, MutableMapping81from http import cookiejar as cookielib82from http.cookies import Morsel83from io import StringIO84 85# --------------86# Legacy Imports87# --------------88from urllib.parse import (89 quote,90 quote_plus,91 unquote,92 unquote_plus,93 urldefrag,94 urlencode,95 urljoin,96 urlparse,97 urlsplit,98 urlunparse,99)100from urllib.request import (101 getproxies,102 getproxies_environment,103 parse_http_list,104 proxy_bypass,105 proxy_bypass_environment, # type: ignore[attr-defined] # https://github.com/python/cpython/issues/145331106)107 108builtin_str = str109str = str110bytes = bytes111basestring = (str, bytes)112numeric_types = (int, float)113integer_types = (int,)114 