-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-87901: os.popen: Fix new encoding
argument.
#92415
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
Conversation
encoding
keyword onlyencoding
argument.
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.
Looks good, thanks for fixing!
You should also add a unit test that uses this new parameter. |
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.
Is there a What's New entry?
Lib/test/test_os.py
Outdated
@@ -998,7 +998,7 @@ def _empty_mapping(self): | |||
def test_update2(self): | |||
os.environ.clear() | |||
os.environ.update(HELLO="World") | |||
with os.popen("%s -c 'echo $HELLO'" % unix_shell) as popen: | |||
with os.popen("%s -c 'echo $HELLO'" % unix_shell, encoding="utf-8") as popen: |
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.
It is not a test for os.popen
. It works without explicit encoding
, so no need to change it.
Instead look at test_popen
. Needed tests for encoding
and errors
, and they should fail if revert changes in os.popen
, so use non-ASCII data and encodings which give different result in comparison with common locale encodings: utf-8, latin1 or cp1252.
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.
Would it make sense to add a new test for the private attribute to check for the encoding? To check encoding="utf-8".
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.
I would prefer not to rely on private attributes if possible. Checking the result of the decoding is enough.
Lib/test/test_os.py
Outdated
@@ -998,7 +998,7 @@ def _empty_mapping(self): | |||
def test_update2(self): | |||
os.environ.clear() | |||
os.environ.update(HELLO="World") | |||
with os.popen("%s -c 'echo $HELLO'" % unix_shell) as popen: | |||
with os.popen("%s -c 'echo $HELLO'" % unix_shell, encoding="utf-8") as popen: |
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.
Would it make sense to add a new test for the private attribute to check for the encoding? To check encoding="utf-8".
There is a dedicated test Test:
Include tests for the locale encoding and the filesystem encoding if they are relevant. |
os.popen() is just a simple wrapper of subprocess.Popen() |
Or should we revert the |
I dislike this API since it always use a shell and so is more likely to be vulnerable to shell code injection. It may also be less efficient than avoiding a shell. I would prefer to deprecate this function, but I was never brave enough to propose deprecating it. At least, I removed platform.popen() ;-) |
I created #92836 that remove |
#87901