bpo-35346, platform: replace os.popen() with subprocess#10786
Conversation
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Can subprocess.check_output() be used here?
Sorry, something went wrong.
This function is documented in the "Older high-level API" section of the subprocess documentation. It seems like subprocess.run() is now preferred. subprocess.run() kills the process on exception, IMHO it's a nice enhancement (to not leak a process on error). I modified my PR to use subprocess.run(). |
Sorry, something went wrong.
|
Should not |
Sorry, something went wrong.
|
+1 on using .check_output() instead. It's a special purpose API for exactly this use case and also available in Python 2.7. |
Sorry, something went wrong.
I don't plan to backport this change to other branches.
I chose to keep the code checking manually the returncode. I don't see the point of a raising an exception, since the exception is ignored anyway. |
Sorry, something went wrong.
|
I think the code would be simpler, and therefore more readable, if use |
Sorry, something went wrong.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Visible effect: suppressed the output to stderr. I think it is worth a news entry. And it can be worth backporting.
Sorry, something went wrong.
done
done
My PR only changes _syscmd_ver() when os.uname() doesn't exist, so only impacts Windows. I don't recall that I ever saw any spurious error when using platform on Windows. I would prefer to minimize changes in stable branches. So if someone considers that it's worth it (I don't think so), I suggest to add " 2> %s" % DEV_NULL to _syscmd_ver() command, rather than using check_output(). |
Sorry, something went wrong.
|
Hum, my PR didn't work as expected on Windows. Currently, _syscmd_ver() tries to run "ver" (as a string) with os.open() which uses a shell. My PR avoids shell=True. _syscmd_ver() still worked thanks to "cmd /c ver"... but that's basically like calling "ver" with shell=True, since subprocess uses cmd.exe as the shell: subprocess may not use cmd.exe if COMSPEC is set, but cmd.exe should always be available on Windows, no? |
Sorry, something went wrong.
|
@malemburg, @serhiy-storchaka: Does my PR look better with check_output()? |
Sorry, something went wrong.
|
Thank you, it looks better. But please defer merging it until issues with |
Sorry, something went wrong.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
LGTM if it LGT @malemburg.
Sorry, something went wrong.
|
Marc Andre Lemburg:
@malemburg: My PR now uses check_output() as you requested. Would you mind to review it and make sure that you agree with the change? Note: It hasn't been mentioned explicitely previously, but my change removes platform.DEV_NULL. This constant is not documented. Since Python 3 has os.devnull and subprocess.DEVNULL, I don't think that it's worth it to keep platform.DEV_NULL for backward compatibility. |
Sorry, something went wrong.
|
@malemburg: I plan to merge this PR at the end of the week. |
Sorry, something went wrong.
Replace os.popen() with subprocess.check_output() in the platform module: * platform.uname() (_syscmd_ver() helper function) now redirects stderr to DEVNULL; * Remove platform.DEV_NULL; * _syscmd_uname() and _syscmd_file() no longer catch AttributeError. The "except AttributeError:" was only needed in Python 2, when os.popen() was not always available. In Python 3, subprocess.check_output() is always available.
|
I squashed the 7 commits into a single commit to update the commit message, I rebased my PR and I also replaced os.open() with subprocess.check_output() in test_platform. |
Sorry, something went wrong.
|
I removed the comments of the buildbot failures because they were false positives due to bad git checkouts. I am investigating why this PR that we made to fix that problem did not work. |
Sorry, something went wrong.
Replace os.popen() with subprocess.Popen in the platform module:
https://bugs.python.org/issue35346