Skip to content

Fix incomplete support for stderr in Legacy Windows layer#4072

Open
Jackenmen wants to merge 6 commits into
Textualize:mainfrom
Jackenmen:support_stderr_in_legacy_windows_layer
Open

Fix incomplete support for stderr in Legacy Windows layer#4072
Jackenmen wants to merge 6 commits into
Textualize:mainfrom
Jackenmen:support_stderr_in_legacy_windows_layer

Conversation

@Jackenmen

@Jackenmen Jackenmen commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other

AI?

  • AI was used to generate this PR

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate (see note about typos above).
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

Description

I've grepped through the code for argless calls to GetStdHandle() and, where appropriate, I've updated them to use the stderr handle (-12 per https://learn.microsoft.com/en-us/windows/console/getstdhandle#parameters), when operating on stderr.
Specifically:

  • Console now detects terminal features for stderr correctly, when stdout is redirected (before, stderr was always treated as legacy Windows when stdout is redirected, even if stderr was attached to Windows Terminal, not conhost)
  • Console(stderr=True, legacy_windows=True) now actually operates on stderr for features that require Windows Console API instead of stdout
  • public get_windows_console_features() and detect_legacy_windows() functions in rich.console module now accept an optional file argument

I didn't write any tests because it does not seem like the functions/methods that I edited had any existing ones. I assume that means it's probably okay, and I don't exactly have a good idea of what would need to be tested here anyway.

You'll notice I moved the legacy_windows assignment to the end of the init function. This was needed because I had to be able to pass self.file to detect_legacy_windows(). The width, height, and color system detection depend on its value, so they had to be moved as well.

Fixes #4071

@Jackenmen Jackenmen marked this pull request as ready for review April 10, 2026 20:47
@Jackenmen Jackenmen marked this pull request as draft April 10, 2026 20:48
@Jackenmen Jackenmen force-pushed the support_stderr_in_legacy_windows_layer branch from 6ac393e to 49c4589 Compare April 10, 2026 20:53
@codecov-commenter

codecov-commenter commented Apr 10, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 87.87879% with 4 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@46cebbb). Learn more about missing BASE report.

Files with missing lines Patch % Lines
rich/_win32_console.py 71.42% 4 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4072   +/-   ##
=======================================
  Coverage        ?   97.77%           
=======================================
  Files           ?       96           
  Lines           ?     8397           
  Branches        ?        0           
=======================================
  Hits            ?     8210           
  Misses          ?      187           
  Partials        ?        0           
Flag Coverage Δ
unittests 97.77% <87.87%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Jackenmen Jackenmen force-pushed the support_stderr_in_legacy_windows_layer branch from 665ddae to d8fc847 Compare April 10, 2026 21:07
@Jackenmen Jackenmen marked this pull request as ready for review April 10, 2026 21:07
@Jackenmen Jackenmen marked this pull request as draft April 10, 2026 21:09
@Jackenmen Jackenmen force-pushed the support_stderr_in_legacy_windows_layer branch from d8fc847 to 9506abc Compare April 10, 2026 21:12
@Jackenmen Jackenmen marked this pull request as ready for review April 10, 2026 21:23
@Jackenmen

Copy link
Copy Markdown
Contributor Author

Sorry for a bit of churn, I struggled with appeasing mypy a bit...
This part in particular is a bit annoying, but I couldn't think of a better way to avoid mypy reporting an issue here:
b159cea

I guess strictly speaking, STDERR_FILENO and STDOUT_FILENO are used both in console and _win32_console module - let me know if I should just create a new module for them (module name suggestions welcome 😄).

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.

[BUG] Legacy Windows layer always uses stdout handle, even when stderr stream is used

2 participants