PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/sys/interp_encoding.py

https://bitbucket.org/pypy/pypy/
Python | 69 lines | 55 code | 5 blank | 9 comment | 6 complexity | 404123155160bbed237a93730ba11db2 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import sys
  2. from rpython.rlib import rlocale
  3. from rpython.rlib.objectmodel import we_are_translated
  4. def getdefaultencoding(space):
  5. """Return the current default string encoding used by the Unicode
  6. implementation."""
  7. return space.wrap(space.sys.defaultencoding)
  8. def setdefaultencoding(space, w_encoding):
  9. """Set the current default string encoding used by the Unicode
  10. implementation."""
  11. encoding = space.str_w(w_encoding)
  12. mod = space.getbuiltinmodule("_codecs")
  13. w_lookup = space.getattr(mod, space.wrap("lookup"))
  14. # check whether the encoding is there
  15. space.call_function(w_lookup, w_encoding)
  16. space.sys.w_default_encoder = None
  17. space.sys.defaultencoding = encoding
  18. def get_w_default_encoder(space):
  19. assert not (space.config.translating and not we_are_translated()), \
  20. "get_w_default_encoder() should not be called during translation"
  21. w_encoding = space.wrap(space.sys.defaultencoding)
  22. mod = space.getbuiltinmodule("_codecs")
  23. w_lookup = space.getattr(mod, space.wrap("lookup"))
  24. w_functuple = space.call_function(w_lookup, w_encoding)
  25. w_encoder = space.getitem(w_functuple, space.wrap(0))
  26. space.sys.w_default_encoder = w_encoder # cache it
  27. return w_encoder
  28. if sys.platform == "win32":
  29. base_encoding = "mbcs"
  30. elif sys.platform == "darwin":
  31. base_encoding = "utf-8"
  32. else:
  33. # In CPython, the default base encoding is NULL. This is paired with a
  34. # comment that says "If non-NULL, this is different than the default
  35. # encoding for strings". Therefore, the default filesystem encoding is the
  36. # default encoding for strings, which is ASCII.
  37. base_encoding = "ascii"
  38. def _getfilesystemencoding(space):
  39. encoding = base_encoding
  40. if rlocale.HAVE_LANGINFO:
  41. try:
  42. oldlocale = rlocale.setlocale(rlocale.LC_CTYPE, None)
  43. rlocale.setlocale(rlocale.LC_CTYPE, "")
  44. try:
  45. loc_codeset = rlocale.nl_langinfo(rlocale.CODESET)
  46. if loc_codeset:
  47. codecmod = space.getbuiltinmodule('_codecs')
  48. w_res = space.call_method(codecmod, 'lookup',
  49. space.wrap(loc_codeset))
  50. if space.is_true(w_res):
  51. encoding = loc_codeset
  52. finally:
  53. rlocale.setlocale(rlocale.LC_CTYPE, oldlocale)
  54. except rlocale.LocaleError:
  55. pass
  56. return encoding
  57. def getfilesystemencoding(space):
  58. """Return the encoding used to convert Unicode filenames in
  59. operating system filenames.
  60. """
  61. if space.sys.filesystemencoding is None:
  62. space.sys.filesystemencoding = _getfilesystemencoding(space)
  63. return space.wrap(space.sys.filesystemencoding)