unicode_helper.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # -*- coding: utf-8 -*-
  2. # *****************************************************************************
  3. # Copyright (C) 2006-2020 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
  4. # Copyright (C) 2020 Bassem Girgis. <brgirgis@gmail.com>
  5. #
  6. # Distributed under the terms of the BSD License. The full license is in
  7. # the file COPYING, distributed as part of this software.
  8. # *****************************************************************************
  9. import sys
  10. from typing import Iterable, Union
  11. #: Also support non-latin chars.
  12. _pyreadline_fallback_codepage = "utf-8"
  13. try:
  14. pyreadline_codepage = sys.stdout.encoding
  15. except AttributeError:
  16. # This error occurs when pdb imports readline and doctest has replaced
  17. # stdout with stdout collector. We will assume ascii codepage
  18. pyreadline_codepage = _pyreadline_fallback_codepage
  19. if pyreadline_codepage is None:
  20. pyreadline_codepage = _pyreadline_fallback_codepage
  21. def ensure_unicode(text: Union[str, bytes]) -> str:
  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(errors="replace")
  28. return text
  29. def ensure_str(text: Union[str, bytes]) -> bytes:
  30. """Convert unicode to str using pyreadline_codepage"""
  31. if isinstance(text, str):
  32. try:
  33. return text.encode(pyreadline_codepage, "replace")
  34. except (LookupError, TypeError):
  35. return text.encode(errors="replace")
  36. return text
  37. def biter(text: bytes) -> Iterable[bytes]:
  38. if isinstance(text, bytes):
  39. return (s.to_bytes(1, "big") for s in text)
  40. return iter(text)