repositori / bagas / requests

requests public ready

https://github.com/psf/requests commit 3c7baaa 48 berkas · 374 potongan 2026-11-21T15:17:50Z
48berkas
36berkas python
374potongan
python 36 markdown 5 yaml 2 text 2 toml 1 css 1 html 1
src/requests/compat.pypython 120 baris ditampilkan
1
"""
2
requests.compat
3
~~~~~~~~~~~~~~~
4
5
This module previously handled import compatibility issues
6
between Python 2 and Python 3. It remains for backwards
7
compatibility until the next major version.
8
"""
9
10
# pyright: reportUnusedImport=false
11
12
from __future__ import annotations
13
14
import importlib
15
import sys
16
from types import ModuleType
17
from typing import TYPE_CHECKING
18
19
# -------
20
# urllib3
21
# -------
22
from urllib3 import __version__ as urllib3_version
23
24
# Detect which major version of urllib3 is being used.
25
try:
26
is_urllib3_1 = int(urllib3_version.split(".")[0]) == 1
27
except (TypeError, AttributeError):
28
# If we can't discern a version, prefer old functionality.
29
is_urllib3_1 = True
30
31
# -------------------
32
# Character Detection
33
# -------------------
34
35
36
def _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
46
47
48
if TYPE_CHECKING:
49
import chardet
50
else:
51
chardet = _resolve_char_detection()
52
53
# -------
54
# Pythons
55
# -------
56
57
# Syntax sugar.
58
_ver = sys.version_info
59
60
#: Python 2.x?
61
is_py2 = _ver[0] == 2
62
63
#: Python 3.x?
64
is_py3 = _ver[0] == 3
65
66
# json/simplejson module import resolution
67
has_simplejson = False
68
try:
69
import simplejson as json # type: ignore[import-not-found]
65
66
# json/simplejson module import resolution
67
has_simplejson = False
68
try:
69
import simplejson as json # type: ignore[import-not-found]
70
71
has_simplejson = True
72
except ImportError:
73
import json
74
75
if has_simplejson:
76
from simplejson import JSONDecodeError # type: ignore[import-not-found]
77
else:
78
from json import JSONDecodeError
79
80
# Keep OrderedDict for backwards compatibility.
81
from collections import OrderedDict
82
from collections.abc import Callable, Mapping, MutableMapping
83
from http import cookiejar as cookielib
84
from http.cookies import Morsel
85
from io import StringIO
86
87
# --------------
88
# Legacy Imports
89
# --------------
90
from 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
)
102
from 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
)
109
110
builtin_str = str
111
str = str
112
bytes = bytes
113
basestring = (str, bytes)
114
numeric_types = (int, float)
115
integer_types = (int,)