Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/requests/_internal_utils.py: 43%
19 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1"""
2requests._internal_utils
3~~~~~~~~~~~~~~
5Provides utility functions that are consumed internally by Requests
6which depend on extremely few external helpers (such as compat)
7"""
8import re
10from .compat import builtin_str
12_VALID_HEADER_NAME_RE_BYTE = re.compile(rb"^[^:\s][^:\r\n]*$")
13_VALID_HEADER_NAME_RE_STR = re.compile(r"^[^:\s][^:\r\n]*$")
14_VALID_HEADER_VALUE_RE_BYTE = re.compile(rb"^\S[^\r\n]*$|^$")
15_VALID_HEADER_VALUE_RE_STR = re.compile(r"^\S[^\r\n]*$|^$")
17HEADER_VALIDATORS = {
18 bytes: (_VALID_HEADER_NAME_RE_BYTE, _VALID_HEADER_VALUE_RE_BYTE),
19 str: (_VALID_HEADER_NAME_RE_STR, _VALID_HEADER_VALUE_RE_STR),
20}
23def to_native_string(string, encoding="ascii"):
24 """Given a string object, regardless of type, returns a representation of
25 that string in the native string type, encoding and decoding where
26 necessary. This assumes ASCII unless told otherwise.
27 """
28 if isinstance(string, builtin_str):
29 out = string
30 else:
31 out = string.decode(encoding)
33 return out
36def unicode_is_ascii(u_string):
37 """Determine if unicode string only contains ASCII characters.
39 :param str u_string: unicode string to check. Must be unicode
40 and not Python 2 `str`.
41 :rtype: bool
42 """
43 assert isinstance(u_string, str)
44 try:
45 u_string.encode("ascii")
46 return True
47 except UnicodeEncodeError:
48 return False