2
requests._internal_utils
5
Provides utility functions that are consumed internally by Requests
6
which depend on extremely few external helpers (such as compat)
11
from .compat import builtin_str
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")
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)
21
bytes: _HEADER_VALIDATORS_BYTE,
22
str: _HEADER_VALIDATORS_STR,
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.
31
if isinstance(string, builtin_str):
34
out = string.decode(encoding)
39
def unicode_is_ascii(u_string: str) -> bool:
40
"""Determine if unicode string only contains ASCII characters.
42
:param str u_string: unicode string to check. Must be unicode
43
and not Python 2 `str`.
46
assert isinstance(u_string, str)
42
:param str u_string: unicode string to check. Must be unicode
43
and not Python 2 `str`.
46
assert isinstance(u_string, str)
48
u_string.encode("ascii")
50
except UnicodeEncodeError: