encoding.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # coding: utf-8
  2. """
  3. Utilities for dealing with text encodings
  4. """
  5. #-----------------------------------------------------------------------------
  6. # Copyright (C) 2008-2012 The IPython Development Team
  7. #
  8. # Distributed under the terms of the BSD License. The full license is in
  9. # the file COPYING, distributed as part of this software.
  10. #-----------------------------------------------------------------------------
  11. #-----------------------------------------------------------------------------
  12. # Imports
  13. #-----------------------------------------------------------------------------
  14. import sys
  15. import locale
  16. import warnings
  17. # to deal with the possibility of sys.std* not being a stream at all
  18. def get_stream_enc(stream, default=None):
  19. """Return the given stream's encoding or a default.
  20. There are cases where ``sys.std*`` might not actually be a stream, so
  21. check for the encoding attribute prior to returning it, and return
  22. a default if it doesn't exist or evaluates as False. ``default``
  23. is None if not provided.
  24. """
  25. if not hasattr(stream, 'encoding') or not stream.encoding:
  26. return default
  27. else:
  28. return stream.encoding
  29. # Less conservative replacement for sys.getdefaultencoding, that will try
  30. # to match the environment.
  31. # Defined here as central function, so if we find better choices, we
  32. # won't need to make changes all over IPython.
  33. def getdefaultencoding(prefer_stream=True):
  34. """Return IPython's guess for the default encoding for bytes as text.
  35. If prefer_stream is True (default), asks for stdin.encoding first,
  36. to match the calling Terminal, but that is often None for subprocesses.
  37. Then fall back on locale.getpreferredencoding(),
  38. which should be a sensible platform default (that respects LANG environment),
  39. and finally to sys.getdefaultencoding() which is the most conservative option,
  40. and usually ASCII on Python 2 or UTF8 on Python 3.
  41. """
  42. enc = None
  43. if prefer_stream:
  44. enc = get_stream_enc(sys.stdin)
  45. if not enc or enc=='ascii':
  46. try:
  47. # There are reports of getpreferredencoding raising errors
  48. # in some cases, which may well be fixed, but let's be conservative here.
  49. enc = locale.getpreferredencoding()
  50. except Exception:
  51. pass
  52. enc = enc or sys.getdefaultencoding()
  53. # On windows `cp0` can be returned to indicate that there is no code page.
  54. # Since cp0 is an invalid encoding return instead cp1252 which is the
  55. # Western European default.
  56. if enc == 'cp0':
  57. warnings.warn(
  58. "Invalid code page cp0 detected - using cp1252 instead."
  59. "If cp1252 is incorrect please ensure a valid code page "
  60. "is defined for the process.", RuntimeWarning)
  61. return 'cp1252'
  62. return enc
  63. DEFAULT_ENCODING = getdefaultencoding()