Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/requests/compat.py: 90%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2requests.compat
3~~~~~~~~~~~~~~~
5This module previously handled import compatibility issues
6between Python 2 and Python 3. It remains for backwards
7compatibility until the next major version.
8"""
10# pyright: reportUnusedImport=false
12from __future__ import annotations
14import importlib
15import sys
16from types import ModuleType
17from typing import TYPE_CHECKING
19# -------
20# urllib3
21# -------
22from urllib3 import __version__ as urllib3_version
24# Detect which major version of urllib3 is being used.
25try:
26 is_urllib3_1 = int(urllib3_version.split(".")[0]) == 1
27except (TypeError, AttributeError):
28 # If we can't discern a version, prefer old functionality.
29 is_urllib3_1 = True
31# -------------------
32# Character Detection
33# -------------------
36def _resolve_char_detection() -> ModuleType | None:
37 """Find supported character detection libraries."""
38 chardet = None
39 for lib in ("chardet", "charset_normalizer"):
40 if chardet is None:
41 try:
42 chardet = importlib.import_module(lib)
43 except ImportError:
44 pass
45 return chardet
48if TYPE_CHECKING:
49 import chardet
50else:
51 chardet = _resolve_char_detection()
53# -------
54# Pythons
55# -------
57# Syntax sugar.
58_ver = sys.version_info
60#: Python 2.x?
61is_py2 = _ver[0] == 2
63#: Python 3.x?
64is_py3 = _ver[0] == 3
66# json/simplejson module import resolution
67has_simplejson = False
68try:
69 import simplejson as json # type: ignore[import-not-found]
71 has_simplejson = True
72except ImportError:
73 import json
75if has_simplejson:
76 from simplejson import JSONDecodeError # type: ignore[import-not-found]
77else:
78 from json import JSONDecodeError
80# Keep OrderedDict for backwards compatibility.
81from collections import OrderedDict
82from collections.abc import Callable, Mapping, MutableMapping
83from http import cookiejar as cookielib
84from http.cookies import Morsel
85from io import StringIO
87# --------------
88# Legacy Imports
89# --------------
90from urllib.parse import (
91 quote,
92 quote_plus,
93 unquote,
94 unquote_plus,
95 urldefrag,
96 urlencode,
97 urljoin,
98 urlparse,
99 urlsplit,
100 urlunparse,
101)
102from urllib.request import (
103 getproxies,
104 getproxies_environment,
105 parse_http_list,
106 proxy_bypass,
107 proxy_bypass_environment, # type: ignore[attr-defined] # https://github.com/python/cpython/issues/145331
108)
110builtin_str = str
111str = str
112bytes = bytes
113basestring = (str, bytes)
114numeric_types = (int, float)
115integer_types = (int,)