Conversation
…haracters // Resolve platformio#5053 The dependency tree is drawn with the box-drawing characters, which raises UnicodeEncodeError on a console whose encoding cannot represent them (cp1252 and similar). Fall back to ASCII only when the output stream cannot encode them, so the tree still renders normally everywhere else.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR prevents pio pkg list from crashing when its output is redirected to a non-Unicode-capable stream (common on Windows), by selecting ASCII dependency-tree glyphs only when the active stdout encoding can’t represent the box-drawing characters.
Changes:
- Added a
stdout_encodes()compatibility helper to detect whethersys.stdoutcan encode specific characters. - Introduced
get_tree_glyphs()and updatedpio pkg listtree rendering to use Unicode or ASCII glyphs based on encoding capability. - Added tests to verify Unicode glyphs on UTF-8 and ASCII fallback on non-Unicode encodings.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
platformio/compat.py |
Adds stdout_encodes() to probe stdout encoding support. |
platformio/package/commands/list.py |
Routes dependency-tree glyph selection through an encoding-aware helper. |
tests/commands/pkg/test_list.py |
Adds coverage to ensure ASCII fallback on non-Unicode stdout encodings. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
105
to
109
| printed_pkgs.append(pkg.path) | ||
| pm.memcache_set("__printed_pkgs", printed_pkgs) | ||
|
|
||
| indent, tee, elbow = get_tree_glyphs() | ||
| click.echo( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pio pkg listdraws the dependency tree with the box-drawing characters, which raisesUnicodeEncodeErrorwhen the output stream cannot encode them - redirecting to a file on Windows is the case in the issue, since the console path goes throughWriteConsoleWbut a redirected stream falls back to the locale encodingas noted in the issue, click does not modify
sys.stdout, so it does not help here. I confirmed that on a cp1252 streamclick.echostill raisesrather than dropping the tree characters for everyone, this checks whether the output stream can encode them and falls back to ASCII (
|,|--,`--) only when it cannot, so the output is unchanged on a UTF-8 capable terminal. The check lives inplatformio/compat.pynext toget_locale_encodingandis_terminalafter the change the box-drawing characters are only produced through that guarded helper, so the command has no other way to hit this
one related spot I left alone:
pio pkg showprints•, which fails the same way on cp437/cp866/cp932 (though not on cp1252, so it is a different set of consoles). Happy to fix that too if you want it in hereResolve #5053