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/_internal_utils.pypython 57 baris ditampilkan
1
"""
2
requests._internal_utils
3
~~~~~~~~~~~~~~
4
5
Provides utility functions that are consumed internally by Requests
6
which depend on extremely few external helpers (such as compat)
7
"""
8
9
import re
10
11
from .compat import builtin_str
12
13
_VALID_HEADER_NAME_RE_BYTE = re.compile(rb"^[^:\s][^:\r\n]*\Z")
14
_VALID_HEADER_NAME_RE_STR = re.compile(r"^[^:\s][^:\r\n]*\Z")
15
_VALID_HEADER_VALUE_RE_BYTE = re.compile(rb"^\S[^\r\n]*\Z|^\Z")
16
_VALID_HEADER_VALUE_RE_STR = re.compile(r"^\S[^\r\n]*\Z|^\Z")
17
18
_HEADER_VALIDATORS_STR = (_VALID_HEADER_NAME_RE_STR, _VALID_HEADER_VALUE_RE_STR)
19
_HEADER_VALIDATORS_BYTE = (_VALID_HEADER_NAME_RE_BYTE, _VALID_HEADER_VALUE_RE_BYTE)
20
HEADER_VALIDATORS = {
21
bytes: _HEADER_VALIDATORS_BYTE,
22
str: _HEADER_VALIDATORS_STR,
23
}
24
25
26
def to_native_string(string: str | bytes, encoding: str = "ascii") -> str:
27
"""Given a string object, regardless of type, returns a representation of
28
that string in the native string type, encoding and decoding where
29
necessary. This assumes ASCII unless told otherwise.
30
"""
31
if isinstance(string, builtin_str):
32
out = string
33
else:
34
out = string.decode(encoding)
35
36
return out
37
38
39
def unicode_is_ascii(u_string: str) -> bool:
40
"""Determine if unicode string only contains ASCII characters.
41
42
:param str u_string: unicode string to check. Must be unicode
43
and not Python 2 `str`.
44
:rtype: bool
45
"""
46
assert isinstance(u_string, str)
41
42
:param str u_string: unicode string to check. Must be unicode
43
and not Python 2 `str`.
44
:rtype: bool
45
"""
46
assert isinstance(u_string, str)
47
try:
48
u_string.encode("ascii")
49
return True
50
except UnicodeEncodeError:
51
return False