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.

encoding.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. """
  3. kombu.utils.encoding
  4. ~~~~~~~~~~~~~~~~~~~~~
  5. Utilities to encode text, and to safely emit text from running
  6. applications without crashing with the infamous :exc:`UnicodeDecodeError`
  7. exception.
  8. """
  9. from __future__ import absolute_import
  10. import sys
  11. import traceback
  12. from kombu.five import text_t
  13. is_py3k = sys.version_info >= (3, 0)
  14. #: safe_str takes encoding from this file by default.
  15. #: :func:`set_default_encoding_file` can used to set the
  16. #: default output file.
  17. default_encoding_file = None
  18. def set_default_encoding_file(file):
  19. global default_encoding_file
  20. default_encoding_file = file
  21. def get_default_encoding_file():
  22. return default_encoding_file
  23. if sys.platform.startswith('java'): # pragma: no cover
  24. def default_encoding(file=None):
  25. return 'utf-8'
  26. else:
  27. def default_encoding(file=None): # noqa
  28. file = file or get_default_encoding_file()
  29. return getattr(file, 'encoding', None) or sys.getfilesystemencoding()
  30. if is_py3k: # pragma: no cover
  31. def str_to_bytes(s):
  32. if isinstance(s, str):
  33. return s.encode()
  34. return s
  35. def bytes_to_str(s):
  36. if isinstance(s, bytes):
  37. return s.decode()
  38. return s
  39. def from_utf8(s, *args, **kwargs):
  40. return s
  41. def ensure_bytes(s):
  42. if not isinstance(s, bytes):
  43. return str_to_bytes(s)
  44. return s
  45. def default_encode(obj):
  46. return obj
  47. str_t = str
  48. else:
  49. def str_to_bytes(s): # noqa
  50. if isinstance(s, unicode):
  51. return s.encode()
  52. return s
  53. def bytes_to_str(s): # noqa
  54. return s
  55. def from_utf8(s, *args, **kwargs): # noqa
  56. return s.encode('utf-8', *args, **kwargs)
  57. def default_encode(obj, file=None): # noqa
  58. return unicode(obj, default_encoding(file))
  59. str_t = unicode
  60. ensure_bytes = str_to_bytes
  61. try:
  62. bytes_t = bytes
  63. except NameError: # pragma: no cover
  64. bytes_t = str # noqa
  65. def safe_str(s, errors='replace'):
  66. s = bytes_to_str(s)
  67. if not isinstance(s, (text_t, bytes)):
  68. return safe_repr(s, errors)
  69. return _safe_str(s, errors)
  70. if is_py3k:
  71. def _safe_str(s, errors='replace', file=None):
  72. if isinstance(s, str):
  73. return s
  74. try:
  75. return str(s)
  76. except Exception as exc:
  77. return '<Unrepresentable {0!r}: {1!r} {2!r}>'.format(
  78. type(s), exc, '\n'.join(traceback.format_stack()))
  79. else:
  80. def _safe_str(s, errors='replace', file=None): # noqa
  81. encoding = default_encoding(file)
  82. try:
  83. if isinstance(s, unicode):
  84. return s.encode(encoding, errors)
  85. return unicode(s, encoding, errors)
  86. except Exception as exc:
  87. return '<Unrepresentable {0!r}: {1!r} {2!r}>'.format(
  88. type(s), exc, '\n'.join(traceback.format_stack()))
  89. def safe_repr(o, errors='replace'):
  90. try:
  91. return repr(o)
  92. except Exception:
  93. return _safe_str(o, errors)