fix(design): keep cip scripts from crashing on a non-UTF-8 stdout (#580)

render-html.py and generate.py print emoji. With stdout or stderr on a cp1252 pipe or file (Windows without PYTHONUTF8) they raise UnicodeEncodeError. Reconfigure both streams to UTF-8, mirror the change into cli/assets via sync-assets, and add a regression test.
This commit is contained in:
Lupin133 2026-11-18 11:05:50 +01:00 committed by GitHub
parent 7436a6e0b6
commit f2cc365559
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 164 additions and 0 deletions

View File

@ -33,6 +33,14 @@ from datetime import datetime
sys.path.insert(0, str(Path(__file__).parent))
from core import search, get_cip_brief
# These scripts print emoji, which raises UnicodeEncodeError when stdout/stderr is
# a cp1252 pipe or file (Windows without PYTHONUTF8). Same intent as the guard in
# ui-ux-pro-max/scripts/search.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)
def load_logo_image(logo_path):
"""Load logo image using PIL for Gemini image editing"""

View File

@ -19,6 +19,14 @@ from datetime import datetime
sys.path.insert(0, str(Path(__file__).parent))
from core import search, get_cip_brief
# These scripts print emoji, which raises UnicodeEncodeError when stdout/stderr is
# a cp1252 pipe or file (Windows without PYTHONUTF8). Same intent as the guard in
# ui-ux-pro-max/scripts/search.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)
# Deliverable descriptions for presentation
DELIVERABLE_INFO = {
"business card": {

View File

@ -0,0 +1,66 @@
"""cip scripts print emoji; they must not crash when stdout is a cp1252 pipe or file."""
import base64
import os
import subprocess
import sys
from pathlib import Path
CIP = Path(__file__).resolve().parent.parent / "cip"
# 1x1 transparent PNG
PNG = base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
)
def _run(args, tmp_path):
env = dict(os.environ)
# generate.py's 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 the keys to empty strings (treated as unset)
# so no local key can be picked up and no request is ever made.
env.update({
"HOME": str(tmp_path),
"USERPROFILE": str(tmp_path),
"GEMINI_API_KEY": "",
"GOOGLE_API_KEY": "",
})
# PYTHONIOENCODING=cp1252 reproduces a Windows console/pipe on any platform.
env.update({"PYTHONUTF8": "0", "PYTHONIOENCODING": "cp1252"})
return subprocess.run(
[sys.executable, *map(str, args)],
capture_output=True,
stdin=subprocess.DEVNULL,
env=env,
cwd=tmp_path,
encoding="utf-8",
errors="replace",
)
def test_render_html_survives_non_utf8_stdout(tmp_path):
images = tmp_path / "cip"
images.mkdir()
(images / "a.png").write_bytes(PNG)
out = tmp_path / "out.html"
result = _run(
[CIP / "render-html.py", "--brand", "Cafe", "--images", images, "--output", out],
tmp_path,
)
assert result.returncode == 0, result.stderr
assert "HTML presentation generated" in result.stdout
assert "Cafe" in out.read_text(encoding="utf-8")
def test_generate_logo_warning_survives_non_utf8_stdout(tmp_path):
# No --logo and no --no-logo-prompt: the script prints its emoji warning and
# reads EOF from stdin. It then stops (exit 1) before any network call because
# google-genai or the API key is missing, so this stays offline.
result = _run([CIP / "generate.py", "--brand", "Cafe"], tmp_path)
assert "UnicodeEncodeError" not in result.stderr, result.stderr
assert "No logo image provided" in result.stdout
assert result.returncode == 1

View File

@ -33,6 +33,14 @@ from datetime import datetime
sys.path.insert(0, str(Path(__file__).parent))
from core import search, get_cip_brief
# These scripts print emoji, which raises UnicodeEncodeError when stdout/stderr is
# a cp1252 pipe or file (Windows without PYTHONUTF8). Same intent as the guard in
# ui-ux-pro-max/scripts/search.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)
def load_logo_image(logo_path):
"""Load logo image using PIL for Gemini image editing"""

View File

@ -19,6 +19,14 @@ from datetime import datetime
sys.path.insert(0, str(Path(__file__).parent))
from core import search, get_cip_brief
# These scripts print emoji, which raises UnicodeEncodeError when stdout/stderr is
# a cp1252 pipe or file (Windows without PYTHONUTF8). Same intent as the guard in
# ui-ux-pro-max/scripts/search.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)
# Deliverable descriptions for presentation
DELIVERABLE_INFO = {
"business card": {

View File

@ -0,0 +1,66 @@
"""cip scripts print emoji; they must not crash when stdout is a cp1252 pipe or file."""
import base64
import os
import subprocess
import sys
from pathlib import Path
CIP = Path(__file__).resolve().parent.parent / "cip"
# 1x1 transparent PNG
PNG = base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
)
def _run(args, tmp_path):
env = dict(os.environ)
# generate.py's 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 the keys to empty strings (treated as unset)
# so no local key can be picked up and no request is ever made.
env.update({
"HOME": str(tmp_path),
"USERPROFILE": str(tmp_path),
"GEMINI_API_KEY": "",
"GOOGLE_API_KEY": "",
})
# PYTHONIOENCODING=cp1252 reproduces a Windows console/pipe on any platform.
env.update({"PYTHONUTF8": "0", "PYTHONIOENCODING": "cp1252"})
return subprocess.run(
[sys.executable, *map(str, args)],
capture_output=True,
stdin=subprocess.DEVNULL,
env=env,
cwd=tmp_path,
encoding="utf-8",
errors="replace",
)
def test_render_html_survives_non_utf8_stdout(tmp_path):
images = tmp_path / "cip"
images.mkdir()
(images / "a.png").write_bytes(PNG)
out = tmp_path / "out.html"
result = _run(
[CIP / "render-html.py", "--brand", "Cafe", "--images", images, "--output", out],
tmp_path,
)
assert result.returncode == 0, result.stderr
assert "HTML presentation generated" in result.stdout
assert "Cafe" in out.read_text(encoding="utf-8")
def test_generate_logo_warning_survives_non_utf8_stdout(tmp_path):
# No --logo and no --no-logo-prompt: the script prints its emoji warning and
# reads EOF from stdin. It then stops (exit 1) before any network call because
# google-genai or the API key is missing, so this stays offline.
result = _run([CIP / "generate.py", "--brand", "Cafe"], tmp_path)
assert "UnicodeEncodeError" not in result.stderr, result.stderr
assert "No logo image provided" in result.stdout
assert result.returncode == 1