refactor(hermes_cli): group D — is_relative_to/closing/suppress collapses, dedupe external paths via dict, drop redundant snap_dir/guards, compact bang_shell/archive_safe/__init__ layout

This commit is contained in:
Teknium
2026-09-02 22:51:30 -07:00
parent 0ebee818e1
commit bf1300b27c
5 changed files with 51 additions and 123 deletions
+6 -12
View File
@@ -15,32 +15,26 @@ def _ensure_utf8():
`hermes setup` on a fresh Pi).
"""
repaired = False
for stream_name in ("stdout", "stderr"):
stream = getattr(sys, stream_name, None)
if stream is None:
continue
try:
encoding = (getattr(stream, "encoding", "") or "").lower().replace("-", "")
if encoding == "utf8":
if (getattr(stream, "encoding", "") or "").lower().replace("-", "") == "utf8":
continue
# Preferred: reconfigure in place, preserving object identity so code already holding
# a reference to the old sys.stdout benefits from the repair too.
reconfigure = getattr(stream, "reconfigure", None)
if callable(reconfigure):
reconfigure(encoding="utf-8", errors="replace")
repaired = True
continue
# Fallback for streams without reconfigure(): reopen the fd as UTF-8 (closefd=False
# keeps the original fd open).
new_stream = open(stream.fileno(), "w", encoding="utf-8", errors="replace", buffering=1, closefd=False)
setattr(sys, stream_name, new_stream)
else:
# No reconfigure(): reopen the fd as UTF-8 (closefd=False keeps the original fd open).
new_stream = open(stream.fileno(), "w", encoding="utf-8", errors="replace",
buffering=1, closefd=False)
setattr(sys, stream_name, new_stream)
repaired = True
except (AttributeError, OSError, ValueError):
pass
# Only nudge child processes toward UTF-8 when a non-UTF-8 locale was actually detected; on a
# healthy UTF-8 host children inherit it from the locale already.
if repaired: