Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle encoding errors when writing to stdout #18292

Merged

Conversation

brianschubert
Copy link
Collaborator

Fixes #12692

Sets the encoding error handler for stdout to "backslashreplace". This prevents mypy from crashing if an error message has a character that can't be represented by the current I/O encoding.

No change is made to stderr since its default is already "backslashreplace".

Before

$ PYTHONIOENCODING=ascii mypy -c "x=γ"
Traceback (most recent call last):
    ...
UnicodeEncodeError: 'ascii' codec can't encode character '\u03b3' in position 50: ordinal not in range(128)

After:

$ PYTHONIOENCODING=ascii mypy -c "x=γ"
<string>:1: error: Name "\u03b3" is not defined  [name-defined]
Found 1 error in 1 file (checked 1 source file)

Externally setting the error handler to something other than "strict" still works. For example:

$ PYTHONIOENCODING=ascii:namereplace mypy -c "x=γ"
<string>:1: error: Name "\N{GREEK SMALL LETTER GAMMA}" is not defined  [name-defined]
Found 1 error in 1 file (checked 1 source file)

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JelleZijlstra JelleZijlstra merged commit 973618a into python:master Dec 14, 2024
19 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.

Error when displaying error that contains unicode characters in Windows
2 participants