bpo-35346, platform: replace os.popen() with subprocess (GH-10786) · python/cpython@3a521f0 · GitHub
Skip to content

Commit 3a521f0

Browse files
authored
bpo-35346, platform: replace os.popen() with subprocess (GH-10786)
Replace os.popen() with subprocess.check_output() in the platform module: * platform.uname() (its _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.
1 parent 9ebe879 commit 3a521f0

3 files changed

Lines changed: 30 additions & 48 deletions

File tree

Lib/platform.py

Lines changed: 19 additions & 39 deletions

Lib/test/test_platform.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,16 @@ def test_mac_ver(self):
222222
res = platform.mac_ver()
223223

224224
if platform.uname().system == 'Darwin':
225-
# We're on a MacOSX system, check that
226-
# the right version information is returned
227-
fd = os.popen('sw_vers', 'r')
228-
real_ver = None
229-
for ln in fd:
230-
if ln.startswith('ProductVersion:'):
231-
real_ver = ln.strip().split()[-1]
225+
# We are on a macOS system, check that the right version
226+
# information is returned
227+
output = subprocess.check_output(['sw_vers'], text=True)
228+
for line in output.splitlines():
229+
if line.startswith('ProductVersion:'):
230+
real_ver = line.strip().split()[-1]
232231
break
233-
fd.close()
234-
self.assertFalse(real_ver is None)
232+
else:
233+
self.fail(f"failed to parse sw_vers output: {output!r}")
234+
235235
result_list = res[0].split('.')
236236
expect_list = real_ver.split('.')
237237
len_diff = len(result_list) - len(expect_list)
Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)