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.

repr.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """
  2. """
  3. # Created on 2015.07.09
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2015 - 2018 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. from binascii import hexlify
  25. from .. import STRING_TYPES
  26. try:
  27. from sys import stdout
  28. repr_encoding = stdout.encoding # get the encoding of the stdout for printing (repr)
  29. if not repr_encoding:
  30. repr_encoding = 'ascii' # default
  31. except Exception:
  32. repr_encoding = 'ascii' # default
  33. def to_stdout_encoding(value):
  34. if not isinstance(value, STRING_TYPES):
  35. value = str(value)
  36. if str is bytes: # Python 2
  37. try:
  38. return value.encode(repr_encoding, 'backslashreplace')
  39. except UnicodeDecodeError: # Python 2.6
  40. return hexlify(value)
  41. else: # Python 3
  42. try:
  43. return value.encode(repr_encoding, errors='backslashreplace').decode(repr_encoding, errors='backslashreplace')
  44. except UnicodeDecodeError:
  45. return hexlify(value)