_internal_utils.py 1.4 KB

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