Skip to content

Commit e37c536

Browse files
committed
disable os.popen and impacted tests on VxWorks
1 parent a60c31b commit e37c536

File tree

8 files changed

+59
-50
lines changed

8 files changed

+59
-50
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 & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
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"]
40+
41+
VXWORKS = (sys.platform == 'vxworks')
4042

4143
def _exists(name):
4244
return name in globals()
@@ -970,50 +972,53 @@ def spawnlpe(mode, file, *args):
970972
__all__.extend(["spawnlp", "spawnlpe"])
971973

972974

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

10181023
# Supply os.fdopen()
10191024
def fdopen(fd, *args, **kwargs):

Lib/test/support/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def requires_lzma(reason='requires lzma'):
421421

422422
is_android = hasattr(sys, 'getandroidapilevel')
423423

424-
if sys.platform not in ('win32', 'vxworks'):
424+
if sys.platform != 'win32':
425425
unix_shell = '/system/bin/sh' if is_android else '/bin/sh'
426426
else:
427427
unix_shell = None

Lib/test/test_os.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ def _empty_mapping(self):
987987
# Bug 1110478
988988
@unittest.skipUnless(unix_shell and os.path.exists(unix_shell),
989989
'requires a shell')
990+
@unittest.skipUnless(hasattr(os, 'popen'), "needs os.popen()")
990991
def test_update2(self):
991992
os.environ.clear()
992993
os.environ.update(HELLO="World")
@@ -996,6 +997,7 @@ def test_update2(self):
996997

997998
@unittest.skipUnless(unix_shell and os.path.exists(unix_shell),
998999
'requires a shell')
1000+
@unittest.skipUnless(hasattr(os, 'popen'), "needs os.popen()")
9991001
def test_os_popen_iter(self):
10001002
with os.popen("%s -c 'echo \"line1\nline2\nline3\"'"
10011003
% 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("os.popen not defined")
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from test.support import os_helper
66
from test.support import warnings_helper
77
from test.support.script_helper import assert_python_ok
8-
from test.support import unix_shell
98

109
# Skip these tests if there is no posix module.
1110
posix = import_helper.import_module('posix')
@@ -1041,8 +1040,7 @@ def test_getgrouplist(self):
10411040

10421041

10431042
@unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
1044-
@unittest.skipUnless(unix_shell and os.path.exists(unix_shell),
1045-
'requires a shell')
1043+
@unittest.skipUnless(hasattr(os, 'popen'), "test needs os.popen()")
10461044
def test_getgroups(self):
10471045
with os.popen('id -G 2>/dev/null') as idg:
10481046
groups = idg.read().strip()

Lib/test/test_select.py

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

47+
@unittest.skipUnless(hasattr(os, 'popen'), "need os.popen()")
4748
def test_select(self):
4849
cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done'
4950
with os.popen(cmd) as p:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add unix_shell requirement checking for test_posix.PosixTester.test_getgroups
1+
Disable os.popen and impacted tests on VxWorks

0 commit comments

Comments
 (0)