stdio.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """Windows-safe stdio configuration.
  2. Forces UTF-8 on the Python side and also flips the console's code page to UTF-8 (65001). Both
  3. matter: Python-level only helps when Python's stdout is a real TTY; code-page flipping lets
  4. subprocesses and child Python ``print()`` calls agree on encoding.
  5. """
  6. from __future__ import annotations
  7. import os
  8. import sys
  9. __all__ = ["configure_windows_stdio", "is_windows"]
  10. _CONFIGURED = False
  11. def is_windows() -> bool:
  12. """Return True iff running on native Windows (not WSL)."""
  13. return sys.platform == "win32"
  14. def _flip_console_code_page_to_utf8() -> None:
  15. """``SetConsoleCP``/``SetConsoleOutputCP`` to CP_UTF8 (65001). Silent on failure: without an
  16. attached console (redirected stdout, service, PTY-less CI) the calls return 0 and we move on."""
  17. try:
  18. import ctypes
  19. kernel32 = ctypes.windll.kernel32 # type: ignore[attr-defined]
  20. kernel32.SetConsoleCP(65001)
  21. kernel32.SetConsoleOutputCP(65001)
  22. except Exception:
  23. pass
  24. def _reconfigure_stream(stream, *, encoding: str = "utf-8", errors: str = "replace") -> None:
  25. """Reconfigure a text stream to UTF-8 in place; skips streams without ``reconfigure`` (e.g. an
  26. ``io.StringIO`` substituted during tests)."""
  27. try:
  28. reconfigure = getattr(stream, "reconfigure", None)
  29. if reconfigure is not None:
  30. reconfigure(encoding=encoding, errors=errors)
  31. except Exception:
  32. pass
  33. def configure_windows_stdio() -> bool:
  34. """Force UTF-8 stdio on Windows. No-op elsewhere.
  35. Idempotent; returns ``True`` only when something actually changed. Set
  36. ``HERMES_DISABLE_WINDOWS_UTF8=1`` to opt out (forces the old cp1252 path for diagnosing
  37. encoding bugs). Also sets a default ``EDITOR`` on Windows if none is set.
  38. """
  39. global _CONFIGURED
  40. if _CONFIGURED:
  41. return False
  42. if not is_windows() or os.environ.get("HERMES_DISABLE_WINDOWS_UTF8") in {"1", "true", "True", "yes"}:
  43. _CONFIGURED = True # repeated calls on POSIX / opted-out are true no-ops
  44. return False
  45. # Make child Python processes use UTF-8 stdio too (PYTHONIOENCODING wins over the locale
  46. # default; PYTHONUTF8=1 enables UTF-8 Mode, PEP 540). Never override an explicit user setting.
  47. os.environ.setdefault("PYTHONIOENCODING", "utf-8")
  48. os.environ.setdefault("PYTHONUTF8", "1")
  49. # prompt_toolkit's ``open_in_editor`` falls back to POSIX-only paths (/usr/bin/nano, /usr/bin/vi)
  50. # that don't exist on Windows — Ctrl+X Ctrl+E and ``/edit`` silently do nothing there
  51. # otherwise, even with full Git for Windows installed.
  52. _default_editor = _default_windows_editor()
  53. if _default_editor and not os.environ.get("EDITOR") and not os.environ.get("VISUAL"):
  54. os.environ["EDITOR"] = _default_editor
  55. _augment_path_with_known_tools()
  56. # Flip the console code page first so any subprocess inheriting the console also sees CP_UTF8.
  57. _flip_console_code_page_to_utf8()
  58. # ``errors="replace"``: a genuinely unencodable sequence prints ``?`` rather than crashing the
  59. # interpreter. stdin is included for batch/pipe input (prompt_toolkit manages its own encoding).
  60. for stream in (sys.stdout, sys.stderr, sys.stdin):
  61. _reconfigure_stream(stream)
  62. _CONFIGURED = True
  63. return True
  64. def _default_windows_editor() -> str:
  65. """Windows default for ``$EDITOR``: ``notepad`` (ships with every install, blocks until the
  66. window closes). The bare name keeps prompt_toolkit's shlex split away from paths with spaces;
  67. "" when even notepad is missing (WinPE, Nano Server) so prompt_toolkit's no-op applies."""
  68. import shutil
  69. return "notepad" if shutil.which("notepad") else ""
  70. def _augment_path_with_known_tools() -> None:
  71. r"""Prepend Hermes-managed tool directories to ``PATH`` (no-op on POSIX / missing dirs).
  72. install.ps1 adds entries like ``%LOCALAPPDATA%\hermes\git\bin`` to the User PATH via
  73. ``SetEnvironmentVariable``, but already-running shells never see that broadcast, so a hermes
  74. launched from the install session would not find rg / bash / grep. Prepending the known dirs
  75. at startup closes that first-launch gap.
  76. """
  77. if not is_windows():
  78. return
  79. local_appdata = os.environ.get("LOCALAPPDATA", "")
  80. if not local_appdata:
  81. return
  82. # Kept in sync with the PATH entries scripts/install.ps1 adds to User scope. The venv Scripts
  83. # dir hosts hermes.exe + pip console scripts; WinGet\Links is where ``winget install`` drops
  84. # CLI shims (ripgrep lands there as rg.exe).
  85. candidate_dirs = [
  86. os.path.join(local_appdata, "hermes", "git", "cmd"),
  87. os.path.join(local_appdata, "hermes", "git", "bin"),
  88. os.path.join(local_appdata, "hermes", "git", "usr", "bin"),
  89. os.path.join(local_appdata, "hermes", "hermes-agent", "venv", "Scripts"),
  90. os.path.join(local_appdata, "Microsoft", "WinGet", "Links")]
  91. existing = os.environ.get("PATH", "")
  92. existing_lower = {p.lower() for p in existing.split(os.pathsep) if p}
  93. prepend = [d for d in candidate_dirs if os.path.isdir(d) and d.lower() not in existing_lower]
  94. if prepend:
  95. os.environ["PATH"] = os.pathsep.join([*prepend, existing])