Skip to content

Commit e1e3c2d

Browse files
authored
bpo-31904: Disable os.popen and popen test cases on VxWorks (GH-21687)
1 parent 9cc8fa6 commit e1e3c2d

File tree

7 files changed

+58
-46
lines changed

7 files changed

+58
-46
lines changed

Doc/library/os.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Notes on the availability of these functions:
3232
objects, and result in an object of the same type, if a path or file name is
3333
returned.
3434

35-
* On VxWorks, os.fork, os.execv and os.spawn*p* are not supported.
35+
* On VxWorks, os.popen, os.fork, os.execv and os.spawn*p* are not supported.
3636

3737
.. note::
3838

Lib/os.py

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
__all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep",
3737
"defpath", "name", "path", "devnull", "SEEK_SET", "SEEK_CUR",
3838
"SEEK_END", "fsencode", "fsdecode", "get_exec_path", "fdopen",
39-
"popen", "extsep"]
39+
"extsep"]
4040

4141
def _exists(name):
4242
return name in globals()
@@ -969,51 +969,55 @@ def spawnlpe(mode, file, *args):
969969

970970
__all__.extend(["spawnlp", "spawnlpe"])
971971

972-
973-
# Supply os.popen()
974-
def popen(cmd, mode="r", buffering=-1):
975-
if not isinstance(cmd, str):
976-
raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
977-
if mode not in ("r", "w"):
978-
raise ValueError("invalid mode %r" % mode)
979-
if buffering == 0 or buffering is None:
980-
raise ValueError("popen() does not support unbuffered streams")
981-
import subprocess, io
982-
if mode == "r":
983-
proc = subprocess.Popen(cmd,
984-
shell=True,
985-
stdout=subprocess.PIPE,
986-
bufsize=buffering)
987-
return _wrap_close(io.TextIOWrapper(proc.stdout), proc)
988-
else:
989-
proc = subprocess.Popen(cmd,
990-
shell=True,
991-
stdin=subprocess.PIPE,
992-
bufsize=buffering)
993-
return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
994-
995-
# Helper for popen() -- a proxy for a file whose close waits for the process
996-
class _wrap_close:
997-
def __init__(self, stream, proc):
998-
self._stream = stream
999-
self._proc = proc
1000-
def close(self):
1001-
self._stream.close()
1002-
returncode = self._proc.wait()
1003-
if returncode == 0:
1004-
return None
1005-
if name == 'nt':
1006-
return returncode
972+
# VxWorks has no user space shell provided. As a result, running
973+
# command in a shell can't be supported.
974+
if sys.platform != 'vxworks':
975+
# Supply os.popen()
976+
def popen(cmd, mode="r", buffering=-1):
977+
if not isinstance(cmd, str):
978+
raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
979+
if mode not in ("r", "w"):
980+
raise ValueError("invalid mode %r" % mode)
981+
if buffering == 0 or buffering is None:
982+
raise ValueError("popen() does not support unbuffered streams")
983+
import subprocess, io
984+
if mode == "r":
985+
proc = subprocess.Popen(cmd,
986+
shell=True,
987+
stdout=subprocess.PIPE,
988+
bufsize=buffering)
989+
return _wrap_close(io.TextIOWrapper(proc.stdout), proc)
1007990
else:
1008-
return returncode << 8 # Shift left to match old behavior
1009-
def __enter__(self):
1010-
return self
1011-
def __exit__(self, *args):
1012-
self.close()
1013-
def __getattr__(self, name):
1014-
return getattr(self._stream, name)
1015-
def __iter__(self):
1016-
return iter(self._stream)
991+
proc = subprocess.Popen(cmd,
992+
shell=True,
993+
stdin=subprocess.PIPE,
994+
bufsize=buffering)
995+
return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
996+
997+
# Helper for popen() -- a proxy for a file whose close waits for the process
998+
class _wrap_close:
999+
def __init__(self, stream, proc):
1000+
self._stream = stream
1001+
self._proc = proc
1002+
def close(self):
1003+
self._stream.close()
1004+
returncode = self._proc.wait()
1005+
if returncode == 0:
1006+
return None
1007+
if name == 'nt':
1008+
return returncode
1009+
else:
1010+
return returncode << 8 # Shift left to match old behavior
1011+
def __enter__(self):
1012+
return self
1013+
def __exit__(self, *args):
1014+
self.close()
1015+
def __getattr__(self, name):
1016+
return getattr(self._stream, name)
1017+
def __iter__(self):
1018+
return iter(self._stream)
1019+
1020+
__all__.append("popen")
10171021

10181022
# Supply os.fdopen()
10191023
def fdopen(fd, *args, **kwargs):

Lib/test/test_os.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,7 @@ def _empty_mapping(self):
991991
# Bug 1110478
992992
@unittest.skipUnless(unix_shell and os.path.exists(unix_shell),
993993
'requires a shell')
994+
@unittest.skipUnless(hasattr(os, 'popen'), "needs os.popen()")
994995
def test_update2(self):
995996
os.environ.clear()
996997
os.environ.update(HELLO="World")
@@ -1000,6 +1001,7 @@ def test_update2(self):
10001001

10011002
@unittest.skipUnless(unix_shell and os.path.exists(unix_shell),
10021003
'requires a shell')
1004+
@unittest.skipUnless(hasattr(os, 'popen'), "needs os.popen()")
10031005
def test_os_popen_iter(self):
10041006
with os.popen("%s -c 'echo \"line1\nline2\nline3\"'"
10051007
% unix_shell) as popen:

Lib/test/test_popen.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from test import support
88
import os, sys
99

10+
if not hasattr(os, 'popen'):
11+
raise unittest.SkipTest("need os.popen()")
12+
1013
# Test that command-lines get down as we expect.
1114
# To do this we execute:
1215
# python -c "import sys;print(sys.argv)" {rest_of_commandline}

Lib/test/test_posix.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ def test_getgrouplist(self):
10451045

10461046

10471047
@unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
1048+
@unittest.skipUnless(hasattr(os, 'popen'), "test needs os.popen()")
10481049
def test_getgroups(self):
10491050
with os.popen('id -G 2>/dev/null') as idg:
10501051
groups = idg.read().strip()

Lib/test/test_select.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def test_returned_list_identity(self):
4646
self.assertIsNot(r, x)
4747
self.assertIsNot(w, x)
4848

49+
@unittest.skipUnless(hasattr(os, 'popen'), "need os.popen()")
4950
def test_select(self):
5051
code = textwrap.dedent('''
5152
import time
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disable os.popen and impacted tests on VxWorks

0 commit comments

Comments
 (0)