Skip to content

Avoid UnicodeEncodeError on narrow-encoding output streams (#1251)#1406

Open
DotChas wants to merge 2 commits intoPyCQA:mainfrom
DotChas:fix-1251-unicode-output
Open

Avoid UnicodeEncodeError on narrow-encoding output streams (#1251)#1406
DotChas wants to merge 2 commits intoPyCQA:mainfrom
DotChas:fix-1251-unicode-output

Conversation

@DotChas
Copy link
Copy Markdown

@DotChas DotChas commented May 2, 2026

Hi,

I fixed #1251

I was looking through the codebase and noticed Bandit can crash with a UnicodeEncodeError when writing reports to output streams using a narrow encoding (for example GBK in some Windows/pre-commit environments).

This PR adds a small wrapper around the output stream so unencodable characters are emitted as backslash-escape sequences (e.g. \U0001f31f) instead of raising and aborting the run.

Note: This PR focuses only on making report output robust against narrow encodings. It does not change any formatter output content/structure beyond escaping characters that cannot be represented by the target encoding.

Tests: tox -e py310

Thanks,
Cevdet Samed / DotChas

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.

Pre-commit hook unable to encode non ascii characters

1 participant