fix(s07): resolve Windows subprocess UnicodeDecodeError with UTF-8/GBK fallback

This commit is contained in:
xrmr
2026-08-11 16:14:08 +08:00
parent 7b564c3ee6
commit 7b76e254aa

View File

@@ -119,11 +119,23 @@ def safe_path(p: str) -> Path:
raise ValueError(f"Path escapes workspace: {p}") raise ValueError(f"Path escapes workspace: {p}")
return path return path
# s07: Fix Windows subprocess encoding crash (fallback UTF-8 -> GBK with error replacement)
def safe_decode(data: bytes) -> str:
if not data:
return ""
try:
return data.decode('utf-8')
except UnicodeDecodeError:
return data.decode('gbk')
def run_bash(command: str) -> str: def run_bash(command: str) -> str:
try: try:
r = subprocess.run(command, shell=True, cwd=WORKDIR, r = subprocess.run(command, shell=True, cwd=WORKDIR,
capture_output=True, text=True, timeout=120) capture_output=True, timeout=120)
out = (r.stdout + r.stderr).strip() stdout = safe_decode(r.stdout)
stderr = safe_decode(r.stderr)
out = (stdout + stderr).strip()
return out[:50000] if out else "(no output)" return out[:50000] if out else "(no output)"
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
return "Error: Timeout (120s)" return "Error: Timeout (120s)"