Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/requests/_internal_utils.py: 52%
21 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
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]*$|^$")
17_HEADER_VALIDATORS_STR = (_VALID_HEADER_NAME_RE_STR, _VALID_HEADER_VALUE_RE_STR)
18_HEADER_VALIDATORS_BYTE = (_VALID_HEADER_NAME_RE_BYTE, _VALID_HEADER_VALUE_RE_BYTE)
19HEADER_VALIDATORS = {
20 bytes: _HEADER_VALIDATORS_BYTE,
21 str: _HEADER_VALIDATORS_STR,
22}
25def to_native_string(string, encoding="ascii"):
26 """Given a string object, regardless of type, returns a representation of
27 that string in the native string type, encoding and decoding where
28 necessary. This assumes ASCII unless told otherwise.
29 """
30 if isinstance(string, builtin_str):
31 out = string
32 else:
33 out = string.decode(encoding)
35 return out
38def unicode_is_ascii(u_string):
39 """Determine if unicode string only contains ASCII characters.
41 :param str u_string: unicode string to check. Must be unicode
42 and not Python 2 `str`.
43 :rtype: bool
44 """
45 assert isinstance(u_string, str)
46 try:
47 u_string.encode("ascii")
48 return True
49 except UnicodeEncodeError:
50 return False