Skip to content

Fix: pio pkg list crashes when its output stream cannot encode the tree characters - #5487

Open
knQzx wants to merge 1 commit into
platformio:developfrom
knQzx:fix-5053-pkg-list-unicode
Open

knQzx wants to merge 1 commit into
platformio:developfrom
knQzx:fix-5053-pkg-list-unicode

Conversation

@knQzx

@knQzx knQzx commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

pio pkg list draws the dependency tree with the box-drawing characters, which raises UnicodeEncodeError when the output stream cannot encode them - redirecting to a file on Windows is the case in the issue, since the console path goes through WriteConsoleW but a redirected stream falls back to the locale encoding

as noted in the issue, click does not modify sys.stdout, so it does not help here. I confirmed that on a cp1252 stream click.echo still raises

rather 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 in platformio/compat.py next to get_locale_encoding and is_terminal

after 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 show prints , 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 here

Resolve #5053

…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.

Copilot AI 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.

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 whether sys.stdout can encode specific characters.
  • Introduced get_tree_glyphs() and updated pio pkg list tree 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(
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.

Redirecting pio pkg list output crashes

2 participants