Skip to content

fix: decide color support per output stream, not solely from stdout - #1123

Merged
gaborbernat merged 3 commits into
pypa:mainfrom
henryiii:fix/per-stream-color
Jul 2, 2026
Merged

gaborbernat merged 3 commits into
pypa:mainfrom
henryiii:fix/per-stream-color

Conversation

@henryiii

@henryiii henryiii commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

I did a bit of cleanup (see last commit), I think this is ready.

🤖 AI text below 🤖

Summary

Part of the code review in #1116.

  • _init_colors decided color support solely from sys.stdout.isatty(), but nearly all colored output (log lines, warnings, TIP hints, tracebacks, _error) is written to stderr; only the final "Successfully built ..." message goes to stdout. This meant python -m build > log needlessly disabled colors on a tty stderr, and python -m build 2> log wrote raw ANSI escapes into the log while stdout was a tty.
  • _styles now holds a {'stdout': ..., 'stderr': ...} mapping instead of a single style set, and _cprint(fmt, msg, file) picks the style set for its target stream (file or sys.stdout).
  • NO_COLOR still disables color on both streams (keeping the existing warning when both NO_COLOR and FORCE_COLOR are set), and FORCE_COLOR still enables color on both streams. Otherwise each stream gets color independently based on its own .isatty().
  • Updated the direct _styles.get() use in main()'s success message to use the stdout styles.

Test plan

  • Added a regression test (test_colors, extended; test_cprint_selects_style_per_stream, new) covering stdout-tty/stderr-non-tty and the inverse, confirmed it fails on the old single-stream logic and passes after the fix.
  • uv run --group test pytest (285 passed)
  • uv run --group mypy mypy (clean)
  • prek -a --quiet (clean)

henryiii added 3 commits July 1, 2026 16:24
`_init_colors` only checked `sys.stdout.isatty()`, but almost all
colored output (log lines, warnings, TIP hints, tracebacks, errors)
is written to stderr; only the final "Successfully built" line goes
to stdout. This meant `python -m build > log` needlessly disabled
color on a tty stderr, while `python -m build 2> log` wrote raw ANSI
escapes into the redirected log even though stdout was a tty.

`_styles` now holds a stdout/stderr style mapping, and `_cprint`
picks the style set for its target stream. `NO_COLOR`/`FORCE_COLOR`
still apply uniformly to both streams.

Assisted-by: ClaudeCode:claude-sonnet-5
Assisted-by: ClaudeCode:claude-sonnet-5
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
@henryiii
henryiii marked this pull request as ready for review July 2, 2026 14:18

@gaborbernat gaborbernat left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gaborbernat
gaborbernat merged commit 272db6c into pypa:main Jul 2, 2026
59 checks passed
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.

3 participants