Files
scientific-agent-skills/tests/_meta/test_console_output.py
T
Timothy Kassis a0aea743b3 fix(skills): make console diagnostics safe for cp1252
Fixes #292. Use readable ASCII status markers in 42 Python scripts across 20 skills, preserve figure and prompt text, keep copied validators/generators identical, and add static plus real-console regression coverage.

Validation: all 20 affected isolated skill suites and spec checks pass. PyMC required a process-local PYTENSOR_FLAGS=cxx=,linker=py,blas__ldflags= override because the native macOS linker could not find d64.
2026-09-30 16:45:48 -07:00

51 lines
1.9 KiB
Python

"""Catch console glyphs that crash Python on legacy Windows output streams.
Inspect literal print/log messages without importing scientific packages or
constraining Unicode in documentation, image prompts, or generated artifacts.
Dynamic data (such as user-supplied filenames) needs runtime coverage separately.
"""
import ast
from pathlib import Path
SKILLS = Path(__file__).resolve().parents[2] / "skills"
def _literal_output(node):
if isinstance(node, ast.Constant) and isinstance(node.value, str):
yield node
elif isinstance(node, ast.JoinedStr):
for part in node.values:
yield from _literal_output(part)
elif isinstance(node, ast.FormattedValue):
yield from _literal_output(node.value)
elif isinstance(node, ast.IfExp):
yield from _literal_output(node.body)
yield from _literal_output(node.orelse)
# Do not inspect arbitrary calls: their arguments need not be printed,
# and serializers may escape Unicode before returning a console string.
def test_literal_console_messages_fit_cp1252():
failures = []
for path in sorted(SKILLS.rglob("*.py")):
tree = ast.parse(path.read_text(encoding="utf-8"))
for node in ast.walk(tree):
if not isinstance(node, ast.Call):
continue
console_call = (
isinstance(node.func, ast.Name) and node.func.id == "print"
) or (
isinstance(node.func, ast.Attribute) and node.func.attr == "_log"
)
if not console_call:
continue
for argument in node.args:
for literal in _literal_output(argument):
try:
literal.value.encode("cp1252")
except UnicodeEncodeError:
failures.append(f"{path.relative_to(SKILLS)}:{literal.lineno}")
assert not failures, "Console literals cannot encode as cp1252:\n" + "\n".join(failures)