Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_internal_utils.py 1.5KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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_STR = (_VALID_HEADER_NAME_RE_STR, _VALID_HEADER_VALUE_RE_STR)
  14. _HEADER_VALIDATORS_BYTE = (_VALID_HEADER_NAME_RE_BYTE, _VALID_HEADER_VALUE_RE_BYTE)
  15. HEADER_VALIDATORS = {
  16. bytes: _HEADER_VALIDATORS_BYTE,
  17. str: _HEADER_VALIDATORS_STR,
  18. }
  19. def to_native_string(string, encoding="ascii"):
  20. """Given a string object, regardless of type, returns a representation of
  21. that string in the native string type, encoding and decoding where
  22. necessary. This assumes ASCII unless told otherwise.
  23. """
  24. if isinstance(string, builtin_str):
  25. out = string
  26. else:
  27. out = string.decode(encoding)
  28. return out
  29. def unicode_is_ascii(u_string):
  30. """Determine if unicode string only contains ASCII characters.
  31. :param str u_string: unicode string to check. Must be unicode
  32. and not Python 2 `str`.
  33. :rtype: bool
  34. """
  35. assert isinstance(u_string, str)
  36. try:
  37. u_string.encode("ascii")
  38. return True
  39. except UnicodeEncodeError:
  40. return False