fix: forcage UTF-8 + ask() resistant aux erreurs d'encodage (anti-crash SSH)

This commit is contained in:
2026-06-22 11:22:49 +02:00
parent fcd2180c6b
commit f864c97f2a
+17
View File
@@ -44,6 +44,15 @@ import requests
import yaml import yaml
from dotenv import load_dotenv from dotenv import load_dotenv
# ── Forçage UTF-8 des entrées/sorties ────────────────────────────────────────
# Évite les UnicodeDecodeError sur les accents en session SSH dont la locale
# n'est pas en UTF-8. Indépendant de LANG/LC_ALL du système.
for _stream in (sys.stdin, sys.stdout, sys.stderr):
try:
_stream.reconfigure(encoding="utf-8", errors="replace")
except (AttributeError, ValueError):
pass # flux non reconfigurable (rare) — on continue
load_dotenv() load_dotenv()
# ───────────────────────────────────────────────────────────────────────────── # ─────────────────────────────────────────────────────────────────────────────
@@ -115,6 +124,11 @@ def ask(prompt: str) -> str:
except (EOFError, KeyboardInterrupt): except (EOFError, KeyboardInterrupt):
print("\n Session interrompue.") print("\n Session interrompue.")
sys.exit(0) sys.exit(0)
except UnicodeDecodeError:
# Faute d'encodage (locale SSH non-UTF-8) : on ne tue pas la session.
warn("Caractère mal encodé ignoré. Relance ta saisie "
"(ou préfixe la commande par PYTHONUTF8=1 au prochain lancement).")
return ""
def ask_multiline(end_marker: str = ".") -> str: def ask_multiline(end_marker: str = ".") -> str:
@@ -128,6 +142,9 @@ def ask_multiline(end_marker: str = ".") -> str:
line = input() line = input()
except (EOFError, KeyboardInterrupt): except (EOFError, KeyboardInterrupt):
break break
except UnicodeDecodeError:
warn("Ligne mal encodée ignorée.")
continue
if line.strip() == end_marker: if line.strip() == end_marker:
break break
lines.append(line) lines.append(line)