-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Conversation
It's common to redirect `sys.std*`. Currently, if you've redirected `sys.stdout` using a custom class and that class does not have an `encoding` attribute set, `trainer.test` can fail with an `AttributeError`. Since the `sys.stdout.encoding` check here concerns terminals, I propose using `sys.__stdout__` instead.
@nihir27 Thanks for sending the PR. Do you have a way to demonstrate this using a small runnable code example? I'm just generally curious. |
@@ -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: |
There was a problem hiding this comment.
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__
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
@awaelchli For example, below would raise an import sys
class Logger(object):
def __init__(self):
self.terminal = sys.__stdout__
self.log = open("logfile.log", "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
sys.stdout = Logger() |
@nihir27 Would you mind adding this example as a unit test case in the file |
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as off-topic.
This comment was marked as off-topic.
@leoleoasd If you find the time, would you mind taking a look at the failing test |
Are you mentioning the right person? |
Nope 🤣 Looks like both of us have too many browser pages open. I meant to ping @nihir27 :) |
@nihir27 One test is failing here:
Could you take another look? Let us know if it doesn't work and we can help. |
sys.__stdout__
for terminal encoding check
@awaelchli, what is the situation here? :) |
|
GitGuardian id | Secret | Commit | Filename | |
---|---|---|---|---|
- | Generic High Entropy Secret | 78fa3af | tests/tests_app/utilities/test_login.py | View secret |
- | Base64 Basic Authentication | 78fa3af | tests/tests_app/utilities/test_login.py | View secret |
🛠 Guidelines to remediate hardcoded secrets
- Understand the implications of revoking this secret by investigating where it is used in your code.
- Replace and store your secret safely. Learn here the best practices.
- Revoke and rotate this secret.
- If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.
To avoid such incidents in the future consider
- following these best practices for managing and storing secrets including API keys and other credentials
- install secret detection on pre-commit to catch secret before it leaves your machine and ease remediation.
🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.
Our GitHub checks need improvements? Share your feedbacks!
What does this PR do?
It's common to redirect
sys.std*
. Currently, if you've redirectedsys.stdout
using a custom class and that class does not have anencoding
attribute set,trainer.test
can fail with anAttributeError
. Since thesys.stdout.encoding
check here concerns terminals, I propose usingsys.__stdout__
instead.Does your PR introduce any breaking changes? If yes, please list them.
Before submitting
PR review
Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:
Did you have fun?
Make sure you had fun coding 🙃