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

Use sys.__stdout__ for terminal encoding check #15425

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/pytorch_lightning/loops/dataloader/evaluation_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ def _print_results(results: List[_OUT_DICT], stage: str) -> None:

try:
# some terminals do not support this character
if sys.stdout.encoding is not None:
"─".encode(sys.stdout.encoding)
if sys.__stdout__.encoding is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given your motivation, wouldn't it be better to check hasattr(sys.stdout, "encoding")?

After all, sys.stdout is what we will use to print, and __stdout__ is just a reference to the original handle: https://docs.python.org/3/library/sys.html#sys.__stdout__

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasattr(sys.stdout, "encoding") and sys.stdout.encoding is None is an option that prevents an AttributeError being raised but for the case where one is writing to a terminal as well as a log file (like in https://stackoverflow.com/a/14906787) you lose the handling of the character.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That particular pattern may be actually be rare and in that case I would favour just doing hasattr(sys.stdout, "encoding") and sys.stdout.encoding is None.

"─".encode(sys.__stdout__.encoding)
except UnicodeEncodeError:
bar_character = "-"
else:
Expand Down