Skip to content

fix: read and write standard streams and text files as UTF-8 - #505

Merged
jimisola merged 3 commits into
mainfrom
fix/validate-utf8-stdout
Nov 23, 2026
Merged

jimisola merged 3 commits into
mainfrom
fix/validate-utf8-stdout

Conversation

@jimisola

@jimisola jimisola commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Requested by Jimisola · project thread

Follow-up to #502, which fixed the same class of bug for report only.

What & why

On Windows, redirected stdio and open() without encoding= use cp1252. Found by an encoding audit across the reqstool repos:

  • reqstool validate crashed on redirected stdout (UnicodeEncodeError on ⚠/✓), exit 1. Reproduced with PYTHONIOENCODING=cp1252.
  • reqstool enrich could not decode UTF-8 on stdin.
  • LSP and config reads (lsp/yaml_schema.py, lsp/features/definition.py, lsp/root_discovery.py, common/reqstool_ai_config.py) decoded UTF-8 files with the locale codec. Mostly that gives mojibake in non-ID text, but a file containing bytes cp1252 leaves undefined raises UnicodeDecodeError; root_discovery._quick_parse swallows it, so such a requirements.yml was silently not discovered.

Changes

  • main() reconfigures stdin/stdout/stderr to UTF-8 once (_use_utf8_standard_streams); the report-specific reconfigure is removed. Streams that cannot be reconfigured are left alone.
  • encoding="utf-8" on the remaining text-file reads: LSP schema/definition/root discovery, .reqstool-ai.yaml, the bundled JSON schemas (syntax_validator), and the LSP log FileHandler (extracted as _log_handlers).

Not changed: YAML datasets are read via requests + FileAdapter and response.text, which guesses the charset when no HTTP header is present. It is not locale-dependent and detected UTF-8 correctly on a Swedish sample, so I left it out; decoding response.content as UTF-8 would be a separate change.

Test plan

  • New tests fail on main for the right reasons: validate (UnicodeEncodeError), enrich stdin (UnicodeDecodeError), stream helper (ImportError), and 8 file-read tests (UnicodeDecodeError, None from _quick_parse, ImportError for _log_handlers). status/report/export stdout guard tests pass on both.
  • Simulated the legacy default with PYTHONUTF8=0 PYTHONIOENCODING=cp1252 (stdio) and a strict-ASCII open() default (file reads). Not run on a real Windows runner; that is what the smoke jobs in ci: smoke-test the built wheel on macOS and Windows #500 are for.
  • hatch run dev:pytest tests/unit: 933 passed, black and flake8 clean; changed lines covered.
  • Regression smoke against main: status, report --format asciidoc and validate byte-identical on test_standard/ms-001, test_basic/ms-101 and reqstool-demo.

Rerun: python -m pytest tests/unit/reqstool/test_command.py tests/unit/reqstool/test_default_encoding.py -k "cp1252 or utf8 or encoding"

🤖 Generated with Claude Code

https://claude.ai/code/session_01Mt2FAXZTXnbKG2Ho6GYy1z

Redirected stdio on Windows uses cp1252, so `reqstool validate > file`
crashed with UnicodeEncodeError on the status symbols it prints, and
`reqstool enrich` could not decode UTF-8 on stdin. Only `report` had a
per-command workaround (#502).

- Reconfigure stdin/stdout/stderr to UTF-8 once at the start of main()
  and drop the report-specific reconfigure.
- Pass encoding="utf-8" to the remaining open()/read_text() calls on text
  files: LSP schema, definition and root discovery reads, the
  .reqstool-ai.yaml config, the bundled JSON schemas, and the LSP log
  file handler. A UTF-8 requirements.yml was silently skipped by LSP
  root discovery under a legacy default encoding.

Signed-off-by: Jimisola Laursen <jimisola@jimisola.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mt2FAXZTXnbKG2Ho6GYy1z
@jimisola
jimisola marked this pull request as ready for review November 22, 2026 08:25
@jimisola
jimisola merged commit 9947a32 into main Nov 23, 2026
12 checks passed
@jimisola
jimisola deleted the fix/validate-utf8-stdout branch November 23, 2026 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant