Repository navigation
Don't crash when printing text the terminal cannot encode - #1064
Merged
Merged
Conversation
FakeOutput passed the text on unchanged, so it was only encoded when
curtsies redrew the screen. print('\udcfc') then raised
UnicodeEncodeError in the main loop, which ended bpython and left the
terminal without a cursor.
Encode the text with the real stream's encoding and error handler in
FakeOutput.write instead. With the strict handler of stdout the user's
code gets the UnicodeEncodeError, as in the standard REPL, and with the
backslashreplace handler of stderr the characters are escaped.
Fixes bpython#747
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1064 +/- ##
==========================================
- Coverage 76.51% 75.99% -0.53%
==========================================
Files 62 60 -2
Lines 8308 8581 +273
==========================================
+ Hits 6357 6521 +164
- Misses 1951 2060 +109 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Fixes #747.
print('\udcfc')still ends bpython on currentmain(Python 3.12, UTF-8 terminal):and the terminal is left without a cursor. The standard REPL raises the same
UnicodeEncodeErrorfrom theprint()call and keeps running.print('\udcfc', file=sys.stderr)crashes bpython the same way, while the standard REPL prints\udcfcbecause stderr uses thebackslashreplaceerror handler.Cause
FakeOutput.writepasses the string on unchanged. It is only encoded later, when curtsies writes the screen to the terminal from the main loop, so the error is raised outside of the user's code and nothing catches it.Fix
As suggested in the issue discussion, the text is now encoded in the user's code path:
FakeOutput.writeencodes it with the encoding and error handler of the real stream it stands in for (sys.__stdout__/sys.__stderr__) and decodes it back before passing it on.strict): theUnicodeEncodeErroris raised by theprint()call, so it is shown as a normal traceback and bpython keeps running.backslashreplace): the characters are escaped, as in the standard REPL.The traceback shows one extra frame for
FakeOutput.write, since the error now comes from Python code instead of the Cprint().Not covered here: an exception message that contains such a character (for example
raise ValueError('\udcfc')) goes through the traceback formatter, notFakeOutput, and still crashes. I kept this PR to theprint()case from the issue.Tests
TestFakeOutput: encodable text is unchanged, a strict stream raisesUnicodeEncodeErrorand writes nothing, abackslashreplacestream escapes the character. The last two fail onmain.print('\udcfc')exits with status 1. After it, the traceback is shown, and1+1on the next line still prints2.print('\udcfc', file=sys.stderr)prints\udcfc.pytest: 337 passed, 14 skipped, 3 xfailed.ruff check,ruff format --checkandcodespellon the changed files pass.mypyreports the same single error as onmain(numpy is not installed here).CHANGELOG.rstunder 0.27 fixes.