fix(design): keep logo generate.py from crashing on a non-UTF-8 stdout (#592)

Batch mode prints check/cross marks. With stdout or stderr on a cp1252 pipe or file (Windows without PYTHONUTF8) the loop raises UnicodeEncodeError after the first variant, so a batch whose first variant succeeds would stop after that image and never print its summary. Reconfigure both streams to UTF-8 as cip/generate.py does, mirror the change into cli/assets via sync-assets, and add a regression test.
This commit is contained in:
Lupin133 2026-11-23 17:06:28 +01:00 committed by GitHub
parent 7e935ad920
commit f823ab5fb9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 110 additions and 0 deletions

View File

@ -56,6 +56,15 @@ from urllib.parse import urlparse
from urllib.request import HTTPRedirectHandler, Request, build_opener
# Batch mode prints ✓/✗, which raises UnicodeEncodeError when stdout/stderr is
# a cp1252 pipe or file (Windows without PYTHONUTF8). Same guard as the one in
# cip/generate.py.
for _stream in (sys.stdout, sys.stderr):
_encoding = getattr(_stream, "encoding", None)
if _encoding and _encoding.lower().replace("-", "") != "utf8" and hasattr(_stream, "reconfigure"):
_stream.reconfigure(encoding="utf-8", errors=_stream.errors)
# Load environment variables
def load_env():
"""Load .env files in priority order"""

View File

@ -0,0 +1,46 @@
"""Batch mode prints check marks; it must not crash when stdout is a cp1252 pipe or file."""
import os
import subprocess
import sys
from pathlib import Path
GENERATE = Path(__file__).resolve().parents[1] / "generate.py"
def test_batch_survives_non_utf8_stdout(tmp_path):
# load_env() reads .env files from the skill folder and from ~/.claude and never
# overrides a variable that is already set. Point the home directory at tmp_path
# and set every provider key to an empty string (treated as unset) so no local
# key can be picked up and no request is ever made: each variant fails offline
# and the loop reaches its "failed" line.
env = dict(os.environ)
env.update({
"HOME": str(tmp_path),
"USERPROFILE": str(tmp_path),
"APPDATA": str(tmp_path),
"GEMINI_API_KEY": "",
"GOOGLE_API_KEY": "",
"ATLASCLOUD_API_KEY": "",
"MUAPI_API_KEY": "",
"UBERLLM_API_KEY": "",
# PYTHONIOENCODING=cp1252 reproduces a Windows console/pipe on any platform.
"PYTHONUTF8": "0",
"PYTHONIOENCODING": "cp1252",
})
result = subprocess.run(
[sys.executable, str(GENERATE), "--brand", "Cafe", "--batch", "1",
"--output-dir", str(tmp_path / "logos")],
capture_output=True,
stdin=subprocess.DEVNULL,
env=env,
cwd=tmp_path,
encoding="utf-8",
errors="replace",
)
assert "UnicodeEncodeError" not in result.stderr, result.stderr
assert "Failed: minimalist" in result.stdout
assert "BATCH COMPLETE: 0/1 logos generated" in result.stdout
assert "✗" in result.stdout
assert result.returncode == 1

View File

@ -56,6 +56,15 @@ from urllib.parse import urlparse
from urllib.request import HTTPRedirectHandler, Request, build_opener
# Batch mode prints ✓/✗, which raises UnicodeEncodeError when stdout/stderr is
# a cp1252 pipe or file (Windows without PYTHONUTF8). Same guard as the one in
# cip/generate.py.
for _stream in (sys.stdout, sys.stderr):
_encoding = getattr(_stream, "encoding", None)
if _encoding and _encoding.lower().replace("-", "") != "utf8" and hasattr(_stream, "reconfigure"):
_stream.reconfigure(encoding="utf-8", errors=_stream.errors)
# Load environment variables
def load_env():
"""Load .env files in priority order"""

View File

@ -0,0 +1,46 @@
"""Batch mode prints check marks; it must not crash when stdout is a cp1252 pipe or file."""
import os
import subprocess
import sys
from pathlib import Path
GENERATE = Path(__file__).resolve().parents[1] / "generate.py"
def test_batch_survives_non_utf8_stdout(tmp_path):
# load_env() reads .env files from the skill folder and from ~/.claude and never
# overrides a variable that is already set. Point the home directory at tmp_path
# and set every provider key to an empty string (treated as unset) so no local
# key can be picked up and no request is ever made: each variant fails offline
# and the loop reaches its "failed" line.
env = dict(os.environ)
env.update({
"HOME": str(tmp_path),
"USERPROFILE": str(tmp_path),
"APPDATA": str(tmp_path),
"GEMINI_API_KEY": "",
"GOOGLE_API_KEY": "",
"ATLASCLOUD_API_KEY": "",
"MUAPI_API_KEY": "",
"UBERLLM_API_KEY": "",
# PYTHONIOENCODING=cp1252 reproduces a Windows console/pipe on any platform.
"PYTHONUTF8": "0",
"PYTHONIOENCODING": "cp1252",
})
result = subprocess.run(
[sys.executable, str(GENERATE), "--brand", "Cafe", "--batch", "1",
"--output-dir", str(tmp_path / "logos")],
capture_output=True,
stdin=subprocess.DEVNULL,
env=env,
cwd=tmp_path,
encoding="utf-8",
errors="replace",
)
assert "UnicodeEncodeError" not in result.stderr, result.stderr
assert "Failed: minimalist" in result.stdout
assert "BATCH COMPLETE: 0/1 logos generated" in result.stdout
assert "✗" in result.stdout
assert result.returncode == 1