m2m模型翻译
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.

50 lines
1.6 KiB

6 months ago
  1. # -*- coding: utf-8 -*-
  2. #*****************************************************************************
  3. # Copyright (C) 2007 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
  4. #
  5. # Distributed under the terms of the BSD License. The full license is in
  6. # the file COPYING, distributed as part of this software.
  7. #*****************************************************************************
  8. import sys
  9. from .py3k_compat import unicode, bytes
  10. try:
  11. pyreadline_codepage = sys.stdout.encoding
  12. except AttributeError:
  13. # This error occurs when pdb imports readline and doctest has replaced
  14. # stdout with stdout collector. We will assume ascii codepage
  15. pyreadline_codepage = "ascii"
  16. if pyreadline_codepage is None:
  17. pyreadline_codepage = "ascii"
  18. if sys.version_info < (2, 6):
  19. bytes = str
  20. PY3 = (sys.version_info >= (3, 0))
  21. def ensure_unicode(text):
  22. """helper to ensure that text passed to WriteConsoleW is unicode"""
  23. if isinstance(text, bytes):
  24. try:
  25. return text.decode(pyreadline_codepage, "replace")
  26. except (LookupError, TypeError):
  27. return text.decode("ascii", "replace")
  28. return text
  29. def ensure_str(text):
  30. """Convert unicode to str using pyreadline_codepage"""
  31. if isinstance(text, unicode):
  32. try:
  33. return text.encode(pyreadline_codepage, "replace")
  34. except (LookupError, TypeError):
  35. return text.encode("ascii", "replace")
  36. return text
  37. def biter(text):
  38. if PY3 and isinstance(text, bytes):
  39. return (s.to_bytes(1, 'big') for s in text)
  40. else:
  41. return iter(text)