Skip to content

On Python 3, use more flexible encoding to send bytes to stdout#2398

Merged
sampsyo merged 2 commits intomasterfrom
stdout-bytes
Mar 7, 2017
Merged

On Python 3, use more flexible encoding to send bytes to stdout#2398
sampsyo merged 2 commits intomasterfrom
stdout-bytes

Conversation

@sampsyo
Copy link
Copy Markdown
Member

@sampsyo sampsyo commented Jan 19, 2017

As explained in #2393, the last big issue in Python 3 is that print or sys.stdout.write can crash depending on the system's locale configuration. This change restores the Python 2 behavior, which avoids potential crashes by (a) using the replace error handler and (b) allowing the user to override the stdout encoding in the configuration.

Unfortunately, this breaks out test harnesses that capture stdout. One way to work around this might be to detect when we're in a test harness and just record the text strings directly.

This is a little ugly. Eventually, it would be nice to create a single output
stream set up with the appropriate encoding settings *at startup* and use that
repeatedly, instead of having `print_` check the settings every time. This
output stream could be cleanly replaced with a mock harness for testing.

Yet another reason to desire a big "beets context" object...
@sampsyo
Copy link
Copy Markdown
Member Author

sampsyo commented Jan 19, 2017

Well, at least the tests pass! Does this seem crazy to you, @jrobeson?

@ghost
Copy link
Copy Markdown

ghost commented Jan 19, 2017

wouldn't the test harness break in the same way when run by the user with a C locale if that was done?

@sampsyo
Copy link
Copy Markdown
Member Author

sampsyo commented Jan 19, 2017

Hmm, I don't quite see how... in the tests under Python 3, this change makes it so the locale encoding isn't used at all (everything just stays as strs).

@sampsyo sampsyo merged commit a99d5d2 into master Mar 7, 2017
sampsyo added a commit that referenced this pull request Mar 7, 2017
On Python 3, use more flexible encoding to send bytes to stdout
sampsyo added a commit that referenced this pull request Mar 26, 2017
Pointed out in #2490, this is a regression introduced by #2398. We need to
flush the buffer to faithfully emulate the "real" print() function.
djl pushed a commit that referenced this pull request Apr 3, 2017
Pointed out in #2490, this is a regression introduced by #2398. We need to
flush the buffer to faithfully emulate the "real" print() function.
antlarr pushed a commit to antlarr/beets that referenced this pull request May 8, 2017
Pointed out in beetbox#2490, this is a regression introduced by beetbox#2398. We need to
flush the buffer to faithfully emulate the "real" print() function.
@arcresu arcresu deleted the stdout-bytes branch April 24, 2019 05:03
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.

1 participant