Development of an internal social media platform with personalised dashboards for students
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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. """
  3. requests._internal_utils
  4. ~~~~~~~~~~~~~~
  5. Provides utility functions that are consumed internally by Requests
  6. which depend on extremely few external helpers (such as compat)
  7. """
  8. from .compat import is_py2, builtin_str, str
  9. def to_native_string(string, encoding='ascii'):
  10. """Given a string object, regardless of type, returns a representation of
  11. that string in the native string type, encoding and decoding where
  12. necessary. This assumes ASCII unless told otherwise.
  13. """
  14. if isinstance(string, builtin_str):
  15. out = string
  16. else:
  17. if is_py2:
  18. out = string.encode(encoding)
  19. else:
  20. out = string.decode(encoding)
  21. return out
  22. def unicode_is_ascii(u_string):
  23. """Determine if unicode string only contains ASCII characters.
  24. :param str u_string: unicode string to check. Must be unicode
  25. and not Python 2 `str`.
  26. :rtype: bool
  27. """
  28. assert isinstance(u_string, str)
  29. try:
  30. u_string.encode('ascii')
  31. return True
  32. except UnicodeEncodeError:
  33. return False