Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1 | import unittest |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 2 | from test import script_helper |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 4 | import subprocess |
| 5 | import sys |
| 6 | import signal |
Gregory P. Smith | e14e9c2 | 2011-03-15 14:55:17 -0400 | [diff] [blame] | 7 | import io |
Andrew Svetlov | 8286071 | 2012-08-19 22:13:41 +0300 | [diff] [blame] | 8 | import locale |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 9 | import os |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +0000 | [diff] [blame] | 10 | import errno |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 11 | import tempfile |
| 12 | import time |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 13 | import re |
Ezio Melotti | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 14 | import sysconfig |
Gregory P. Smith | d23047b | 2010-12-04 09:10:44 +0000 | [diff] [blame] | 15 | import warnings |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 16 | import select |
Gregory P. Smith | 81ce685 | 2011-03-15 02:04:11 -0400 | [diff] [blame] | 17 | import shutil |
Benjamin Peterson | b870aa1 | 2011-12-10 12:44:25 -0500 | [diff] [blame] | 18 | import gc |
Benjamin Peterson | 964561b | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 19 | |
| 20 | try: |
| 21 | import resource |
| 22 | except ImportError: |
| 23 | resource = None |
| 24 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 25 | mswindows = (sys.platform == "win32") |
| 26 | |
| 27 | # |
| 28 | # Depends on the following external programs: Python |
| 29 | # |
| 30 | |
| 31 | if mswindows: |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 32 | SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), ' |
| 33 | 'os.O_BINARY);') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 34 | else: |
| 35 | SETBINARY = '' |
| 36 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 37 | |
| 38 | try: |
| 39 | mkstemp = tempfile.mkstemp |
| 40 | except AttributeError: |
| 41 | # tempfile.mkstemp is not available |
| 42 | def mkstemp(): |
| 43 | """Replacement for mkstemp, calling mktemp.""" |
| 44 | fname = tempfile.mktemp() |
| 45 | return os.open(fname, os.O_RDWR|os.O_CREAT), fname |
| 46 | |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 47 | |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 48 | class BaseTestCase(unittest.TestCase): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 49 | def setUp(self): |
| 50 | # Try to minimize the number of children we have so this test |
| 51 | # doesn't crash on some buildbots (Alphas in particular). |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 52 | support.reap_children() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 53 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 54 | def tearDown(self): |
| 55 | for inst in subprocess._active: |
| 56 | inst.wait() |
| 57 | subprocess._cleanup() |
| 58 | self.assertFalse(subprocess._active, "subprocess._active not empty") |
| 59 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 60 | def assertStderrEqual(self, stderr, expected, msg=None): |
| 61 | # In a debug build, stuff like "[6580 refs]" is printed to stderr at |
| 62 | # shutdown time. That frustrates tests trying to check stderr produced |
| 63 | # from a spawned Python process. |
Antoine Pitrou | 62f68ed | 2010-08-04 11:48:56 +0000 | [diff] [blame] | 64 | actual = support.strip_python_stderr(stderr) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 65 | self.assertEqual(actual, expected, msg) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 67 | |
| 68 | class ProcessTestCase(BaseTestCase): |
| 69 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 70 | def test_call_seq(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 71 | # call() function with sequence argument |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 72 | rc = subprocess.call([sys.executable, "-c", |
| 73 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 74 | self.assertEqual(rc, 47) |
| 75 | |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 76 | def test_check_call_zero(self): |
| 77 | # check_call() function with zero return code |
| 78 | rc = subprocess.check_call([sys.executable, "-c", |
| 79 | "import sys; sys.exit(0)"]) |
| 80 | self.assertEqual(rc, 0) |
| 81 | |
| 82 | def test_check_call_nonzero(self): |
| 83 | # check_call() function with non-zero return code |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 84 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 85 | subprocess.check_call([sys.executable, "-c", |
| 86 | "import sys; sys.exit(47)"]) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 87 | self.assertEqual(c.exception.returncode, 47) |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 88 | |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 89 | def test_check_output(self): |
| 90 | # check_output() function with zero return code |
| 91 | output = subprocess.check_output( |
| 92 | [sys.executable, "-c", "print('BDFL')"]) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 93 | self.assertIn(b'BDFL', output) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 94 | |
| 95 | def test_check_output_nonzero(self): |
| 96 | # check_call() function with non-zero return code |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 97 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 98 | subprocess.check_output( |
| 99 | [sys.executable, "-c", "import sys; sys.exit(5)"]) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 100 | self.assertEqual(c.exception.returncode, 5) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 101 | |
| 102 | def test_check_output_stderr(self): |
| 103 | # check_output() function stderr redirected to stdout |
| 104 | output = subprocess.check_output( |
| 105 | [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"], |
| 106 | stderr=subprocess.STDOUT) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 107 | self.assertIn(b'BDFL', output) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 108 | |
| 109 | def test_check_output_stdout_arg(self): |
| 110 | # check_output() function stderr redirected to stdout |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 111 | with self.assertRaises(ValueError) as c: |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 112 | output = subprocess.check_output( |
| 113 | [sys.executable, "-c", "print('will not be run')"], |
| 114 | stdout=sys.stdout) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 115 | self.fail("Expected ValueError when stdout arg supplied.") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 116 | self.assertIn('stdout', c.exception.args[0]) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 117 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 118 | def test_call_kwargs(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 119 | # call() function with keyword args |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 120 | newenv = os.environ.copy() |
| 121 | newenv["FRUIT"] = "banana" |
| 122 | rc = subprocess.call([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 123 | 'import sys, os;' |
| 124 | 'sys.exit(os.getenv("FRUIT")=="banana")'], |
| 125 | env=newenv) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 126 | self.assertEqual(rc, 1) |
| 127 | |
Victor Stinner | 87b9bc3 | 2011-06-01 00:57:47 +0200 | [diff] [blame] | 128 | def test_invalid_args(self): |
| 129 | # Popen() called with invalid arguments should raise TypeError |
| 130 | # but Popen.__del__ should not complain (issue #12085) |
| 131 | with support.captured_stderr() as s: |
| 132 | self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1) |
| 133 | argcount = subprocess.Popen.__init__.__code__.co_argcount |
| 134 | too_many_args = [0] * (argcount + 1) |
| 135 | self.assertRaises(TypeError, subprocess.Popen, *too_many_args) |
| 136 | self.assertEqual(s.getvalue(), '') |
| 137 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 138 | def test_stdin_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 139 | # .stdin is None when not redirected |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 140 | p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 141 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 142 | self.addCleanup(p.stdout.close) |
| 143 | self.addCleanup(p.stderr.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 144 | p.wait() |
| 145 | self.assertEqual(p.stdin, None) |
| 146 | |
| 147 | def test_stdout_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 148 | # .stdout is None when not redirected |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 149 | p = subprocess.Popen([sys.executable, "-c", |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 150 | 'print(" this bit of output is from a ' |
Tim Peters | 4052fe5 | 2004-10-13 03:29:54 +0000 | [diff] [blame] | 151 | 'test of stdout in a different ' |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 152 | 'process ...")'], |
Tim Peters | 4052fe5 | 2004-10-13 03:29:54 +0000 | [diff] [blame] | 153 | stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 154 | self.addCleanup(p.stdin.close) |
| 155 | self.addCleanup(p.stderr.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 156 | p.wait() |
| 157 | self.assertEqual(p.stdout, None) |
| 158 | |
| 159 | def test_stderr_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 160 | # .stderr is None when not redirected |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 161 | p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 162 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 163 | self.addCleanup(p.stdout.close) |
| 164 | self.addCleanup(p.stdin.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 165 | p.wait() |
| 166 | self.assertEqual(p.stderr, None) |
| 167 | |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 168 | # For use in the test_cwd* tests below. |
| 169 | def _normalize_cwd(self, cwd): |
| 170 | # Normalize an expected cwd (for Tru64 support). |
| 171 | # We can't use os.path.realpath since it doesn't expand Tru64 {memb} |
| 172 | # strings. See bug #1063571. |
| 173 | original_cwd = os.getcwd() |
| 174 | os.chdir(cwd) |
| 175 | cwd = os.getcwd() |
| 176 | os.chdir(original_cwd) |
| 177 | return cwd |
| 178 | |
| 179 | # For use in the test_cwd* tests below. |
| 180 | def _split_python_path(self): |
| 181 | # Return normalized (python_dir, python_base). |
| 182 | python_path = os.path.realpath(sys.executable) |
| 183 | return os.path.split(python_path) |
| 184 | |
| 185 | # For use in the test_cwd* tests below. |
| 186 | def _assert_cwd(self, expected_cwd, python_arg, **kwargs): |
| 187 | # Invoke Python via Popen, and assert that (1) the call succeeds, |
| 188 | # and that (2) the current working directory of the child process |
| 189 | # matches *expected_cwd*. |
| 190 | p = subprocess.Popen([python_arg, "-c", |
| 191 | "import os, sys; " |
| 192 | "sys.stdout.write(os.getcwd()); " |
| 193 | "sys.exit(47)"], |
| 194 | stdout=subprocess.PIPE, |
| 195 | **kwargs) |
| 196 | self.addCleanup(p.stdout.close) |
Ezio Melotti | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 197 | p.wait() |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 198 | self.assertEqual(47, p.returncode) |
| 199 | normcase = os.path.normcase |
| 200 | self.assertEqual(normcase(expected_cwd), |
| 201 | normcase(p.stdout.read().decode("utf-8"))) |
| 202 | |
| 203 | def test_cwd(self): |
| 204 | # Check that cwd changes the cwd for the child process. |
| 205 | temp_dir = tempfile.gettempdir() |
| 206 | temp_dir = self._normalize_cwd(temp_dir) |
| 207 | self._assert_cwd(temp_dir, sys.executable, cwd=temp_dir) |
| 208 | |
Chris Jerdonek | c2cd626 | 2012-09-30 09:45:00 -0700 | [diff] [blame] | 209 | @unittest.skipIf(mswindows, "pending resolution of issue #15533") |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 210 | def test_cwd_with_relative_arg(self): |
| 211 | # Check that Popen looks for args[0] relative to cwd if args[0] |
| 212 | # is relative. |
| 213 | python_dir, python_base = self._split_python_path() |
| 214 | rel_python = os.path.join(os.curdir, python_base) |
| 215 | with support.temp_cwd() as wrong_dir: |
| 216 | # Before calling with the correct cwd, confirm that the call fails |
| 217 | # without cwd and with the wrong cwd. |
| 218 | self.assertRaises(OSError, subprocess.Popen, |
| 219 | [rel_python]) |
| 220 | self.assertRaises(OSError, subprocess.Popen, |
| 221 | [rel_python], cwd=wrong_dir) |
| 222 | python_dir = self._normalize_cwd(python_dir) |
| 223 | self._assert_cwd(python_dir, rel_python, cwd=python_dir) |
| 224 | |
Chris Jerdonek | c2cd626 | 2012-09-30 09:45:00 -0700 | [diff] [blame] | 225 | @unittest.skipIf(mswindows, "pending resolution of issue #15533") |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 226 | def test_cwd_with_relative_executable(self): |
| 227 | # Check that Popen looks for executable relative to cwd if executable |
| 228 | # is relative (and that executable takes precedence over args[0]). |
| 229 | python_dir, python_base = self._split_python_path() |
| 230 | rel_python = os.path.join(os.curdir, python_base) |
| 231 | doesntexist = "somethingyoudonthave" |
| 232 | with support.temp_cwd() as wrong_dir: |
| 233 | # Before calling with the correct cwd, confirm that the call fails |
| 234 | # without cwd and with the wrong cwd. |
| 235 | self.assertRaises(OSError, subprocess.Popen, |
| 236 | [doesntexist], executable=rel_python) |
| 237 | self.assertRaises(OSError, subprocess.Popen, |
| 238 | [doesntexist], executable=rel_python, |
| 239 | cwd=wrong_dir) |
| 240 | python_dir = self._normalize_cwd(python_dir) |
| 241 | self._assert_cwd(python_dir, doesntexist, executable=rel_python, |
| 242 | cwd=python_dir) |
| 243 | |
| 244 | def test_cwd_with_absolute_arg(self): |
| 245 | # Check that Popen can find the executable when the cwd is wrong |
| 246 | # if args[0] is an absolute path. |
| 247 | python_dir, python_base = self._split_python_path() |
| 248 | abs_python = os.path.join(python_dir, python_base) |
| 249 | rel_python = os.path.join(os.curdir, python_base) |
| 250 | with script_helper.temp_dir() as wrong_dir: |
| 251 | # Before calling with an absolute path, confirm that using a |
| 252 | # relative path fails. |
| 253 | self.assertRaises(OSError, subprocess.Popen, |
| 254 | [rel_python], cwd=wrong_dir) |
| 255 | wrong_dir = self._normalize_cwd(wrong_dir) |
| 256 | self._assert_cwd(wrong_dir, abs_python, cwd=wrong_dir) |
| 257 | |
| 258 | def test_executable_with_cwd(self): |
| 259 | python_dir, python_base = self._split_python_path() |
| 260 | python_dir = self._normalize_cwd(python_dir) |
| 261 | self._assert_cwd(python_dir, "somethingyoudonthave", |
| 262 | executable=sys.executable, cwd=python_dir) |
Ezio Melotti | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 263 | |
| 264 | @unittest.skipIf(sysconfig.is_python_build(), |
| 265 | "need an installed Python. See #7774") |
| 266 | def test_executable_without_cwd(self): |
| 267 | # For a normal installation, it should work without 'cwd' |
| 268 | # argument. For test runs in the build directory, see #7774. |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 269 | self._assert_cwd('', "somethingyoudonthave", executable=sys.executable) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 270 | |
| 271 | def test_stdin_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 272 | # stdin redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 273 | p = subprocess.Popen([sys.executable, "-c", |
| 274 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 275 | stdin=subprocess.PIPE) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 276 | p.stdin.write(b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 277 | p.stdin.close() |
| 278 | p.wait() |
| 279 | self.assertEqual(p.returncode, 1) |
| 280 | |
| 281 | def test_stdin_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 282 | # stdin is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 283 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 284 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 285 | d = tf.fileno() |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 286 | os.write(d, b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 287 | os.lseek(d, 0, 0) |
| 288 | p = subprocess.Popen([sys.executable, "-c", |
| 289 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 290 | stdin=d) |
| 291 | p.wait() |
| 292 | self.assertEqual(p.returncode, 1) |
| 293 | |
| 294 | def test_stdin_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 295 | # stdin is set to open file object |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 296 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 297 | self.addCleanup(tf.close) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 298 | tf.write(b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 299 | tf.seek(0) |
| 300 | p = subprocess.Popen([sys.executable, "-c", |
| 301 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 302 | stdin=tf) |
| 303 | p.wait() |
| 304 | self.assertEqual(p.returncode, 1) |
| 305 | |
| 306 | def test_stdout_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 307 | # stdout redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 308 | p = subprocess.Popen([sys.executable, "-c", |
| 309 | 'import sys; sys.stdout.write("orange")'], |
| 310 | stdout=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 311 | self.addCleanup(p.stdout.close) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 312 | self.assertEqual(p.stdout.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 313 | |
| 314 | def test_stdout_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 315 | # stdout is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 316 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 317 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 318 | d = tf.fileno() |
| 319 | p = subprocess.Popen([sys.executable, "-c", |
| 320 | 'import sys; sys.stdout.write("orange")'], |
| 321 | stdout=d) |
| 322 | p.wait() |
| 323 | os.lseek(d, 0, 0) |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 324 | self.assertEqual(os.read(d, 1024), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 325 | |
| 326 | def test_stdout_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 327 | # stdout is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 328 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 329 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 330 | p = subprocess.Popen([sys.executable, "-c", |
| 331 | 'import sys; sys.stdout.write("orange")'], |
| 332 | stdout=tf) |
| 333 | p.wait() |
| 334 | tf.seek(0) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 335 | self.assertEqual(tf.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 336 | |
| 337 | def test_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 338 | # stderr redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 339 | p = subprocess.Popen([sys.executable, "-c", |
| 340 | 'import sys; sys.stderr.write("strawberry")'], |
| 341 | stderr=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 342 | self.addCleanup(p.stderr.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 343 | self.assertStderrEqual(p.stderr.read(), b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 344 | |
| 345 | def test_stderr_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 346 | # stderr is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 347 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 348 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 349 | d = tf.fileno() |
| 350 | p = subprocess.Popen([sys.executable, "-c", |
| 351 | 'import sys; sys.stderr.write("strawberry")'], |
| 352 | stderr=d) |
| 353 | p.wait() |
| 354 | os.lseek(d, 0, 0) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 355 | self.assertStderrEqual(os.read(d, 1024), b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 356 | |
| 357 | def test_stderr_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 358 | # stderr is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 359 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 360 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 361 | p = subprocess.Popen([sys.executable, "-c", |
| 362 | 'import sys; sys.stderr.write("strawberry")'], |
| 363 | stderr=tf) |
| 364 | p.wait() |
| 365 | tf.seek(0) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 366 | self.assertStderrEqual(tf.read(), b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 367 | |
| 368 | def test_stdout_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 369 | # capture stdout and stderr to the same pipe |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 370 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 371 | 'import sys;' |
| 372 | 'sys.stdout.write("apple");' |
| 373 | 'sys.stdout.flush();' |
| 374 | 'sys.stderr.write("orange")'], |
| 375 | stdout=subprocess.PIPE, |
| 376 | stderr=subprocess.STDOUT) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 377 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 378 | self.assertStderrEqual(p.stdout.read(), b"appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 379 | |
| 380 | def test_stdout_stderr_file(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 381 | # capture stdout and stderr to the same open file |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 382 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 383 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 384 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 385 | 'import sys;' |
| 386 | 'sys.stdout.write("apple");' |
| 387 | 'sys.stdout.flush();' |
| 388 | 'sys.stderr.write("orange")'], |
| 389 | stdout=tf, |
| 390 | stderr=tf) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 391 | p.wait() |
| 392 | tf.seek(0) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 393 | self.assertStderrEqual(tf.read(), b"appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 394 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 395 | def test_stdout_filedes_of_stdout(self): |
| 396 | # stdout is set to 1 (#1531862). |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 397 | cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), b'.\n'))" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 398 | rc = subprocess.call([sys.executable, "-c", cmd], stdout=1) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 399 | self.assertEqual(rc, 2) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 400 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 401 | def test_env(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 402 | newenv = os.environ.copy() |
| 403 | newenv["FRUIT"] = "orange" |
Victor Stinner | f1512a2 | 2011-06-21 17:18:38 +0200 | [diff] [blame] | 404 | with subprocess.Popen([sys.executable, "-c", |
| 405 | 'import sys,os;' |
| 406 | 'sys.stdout.write(os.getenv("FRUIT"))'], |
| 407 | stdout=subprocess.PIPE, |
| 408 | env=newenv) as p: |
| 409 | stdout, stderr = p.communicate() |
| 410 | self.assertEqual(stdout, b"orange") |
| 411 | |
Victor Stinner | 62d5118 | 2011-06-23 01:02:25 +0200 | [diff] [blame] | 412 | # Windows requires at least the SYSTEMROOT environment variable to start |
| 413 | # Python |
| 414 | @unittest.skipIf(sys.platform == 'win32', |
| 415 | 'cannot test an empty env on Windows') |
Victor Stinner | 237e5cb | 2011-06-22 21:28:43 +0200 | [diff] [blame] | 416 | @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') is not None, |
Victor Stinner | 372309a | 2011-06-21 21:59:06 +0200 | [diff] [blame] | 417 | 'the python library cannot be loaded ' |
| 418 | 'with an empty environment') |
Victor Stinner | f1512a2 | 2011-06-21 17:18:38 +0200 | [diff] [blame] | 419 | def test_empty_env(self): |
| 420 | with subprocess.Popen([sys.executable, "-c", |
| 421 | 'import os; ' |
Victor Stinner | 372309a | 2011-06-21 21:59:06 +0200 | [diff] [blame] | 422 | 'print(list(os.environ.keys()))'], |
Victor Stinner | f1512a2 | 2011-06-21 17:18:38 +0200 | [diff] [blame] | 423 | stdout=subprocess.PIPE, |
| 424 | env={}) as p: |
| 425 | stdout, stderr = p.communicate() |
Victor Stinner | 237e5cb | 2011-06-22 21:28:43 +0200 | [diff] [blame] | 426 | self.assertIn(stdout.strip(), |
| 427 | (b"[]", |
| 428 | # Mac OS X adds __CF_USER_TEXT_ENCODING variable to an empty |
| 429 | # environment |
| 430 | b"['__CF_USER_TEXT_ENCODING']")) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 431 | |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 432 | def test_communicate_stdin(self): |
| 433 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 434 | 'import sys;' |
| 435 | 'sys.exit(sys.stdin.read() == "pear")'], |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 436 | stdin=subprocess.PIPE) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 437 | p.communicate(b"pear") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 438 | self.assertEqual(p.returncode, 1) |
| 439 | |
| 440 | def test_communicate_stdout(self): |
| 441 | p = subprocess.Popen([sys.executable, "-c", |
| 442 | 'import sys; sys.stdout.write("pineapple")'], |
| 443 | stdout=subprocess.PIPE) |
| 444 | (stdout, stderr) = p.communicate() |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 445 | self.assertEqual(stdout, b"pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 446 | self.assertEqual(stderr, None) |
| 447 | |
| 448 | def test_communicate_stderr(self): |
| 449 | p = subprocess.Popen([sys.executable, "-c", |
| 450 | 'import sys; sys.stderr.write("pineapple")'], |
| 451 | stderr=subprocess.PIPE) |
| 452 | (stdout, stderr) = p.communicate() |
| 453 | self.assertEqual(stdout, None) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 454 | self.assertStderrEqual(stderr, b"pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 455 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 456 | def test_communicate(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 457 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 458 | 'import sys,os;' |
| 459 | 'sys.stderr.write("pineapple");' |
| 460 | 'sys.stdout.write(sys.stdin.read())'], |
| 461 | stdin=subprocess.PIPE, |
| 462 | stdout=subprocess.PIPE, |
| 463 | stderr=subprocess.PIPE) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 464 | self.addCleanup(p.stdout.close) |
| 465 | self.addCleanup(p.stderr.close) |
| 466 | self.addCleanup(p.stdin.close) |
Georg Brandl | 1abcbf8 | 2008-07-01 19:28:43 +0000 | [diff] [blame] | 467 | (stdout, stderr) = p.communicate(b"banana") |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 468 | self.assertEqual(stdout, b"banana") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 469 | self.assertStderrEqual(stderr, b"pineapple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 470 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 471 | # Test for the fd leak reported in http://bugs.python.org/issue2791. |
| 472 | def test_communicate_pipe_fd_leak(self): |
Victor Stinner | 667d4b5 | 2010-12-25 22:40:32 +0000 | [diff] [blame] | 473 | for stdin_pipe in (False, True): |
| 474 | for stdout_pipe in (False, True): |
| 475 | for stderr_pipe in (False, True): |
| 476 | options = {} |
| 477 | if stdin_pipe: |
| 478 | options['stdin'] = subprocess.PIPE |
| 479 | if stdout_pipe: |
| 480 | options['stdout'] = subprocess.PIPE |
| 481 | if stderr_pipe: |
| 482 | options['stderr'] = subprocess.PIPE |
| 483 | if not options: |
| 484 | continue |
| 485 | p = subprocess.Popen((sys.executable, "-c", "pass"), **options) |
| 486 | p.communicate() |
| 487 | if p.stdin is not None: |
| 488 | self.assertTrue(p.stdin.closed) |
| 489 | if p.stdout is not None: |
| 490 | self.assertTrue(p.stdout.closed) |
| 491 | if p.stderr is not None: |
| 492 | self.assertTrue(p.stderr.closed) |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 493 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 494 | def test_communicate_returns(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 495 | # communicate() should return None if no redirection is active |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 496 | p = subprocess.Popen([sys.executable, "-c", |
| 497 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 498 | (stdout, stderr) = p.communicate() |
| 499 | self.assertEqual(stdout, None) |
| 500 | self.assertEqual(stderr, None) |
| 501 | |
| 502 | def test_communicate_pipe_buf(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 503 | # communicate() with writes larger than pipe_buf |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 504 | # This test will probably deadlock rather than fail, if |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 505 | # communicate() does not work properly. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 506 | x, y = os.pipe() |
| 507 | if mswindows: |
| 508 | pipe_buf = 512 |
| 509 | else: |
| 510 | pipe_buf = os.fpathconf(x, "PC_PIPE_BUF") |
| 511 | os.close(x) |
| 512 | os.close(y) |
| 513 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 514 | 'import sys,os;' |
| 515 | 'sys.stdout.write(sys.stdin.read(47));' |
| 516 | 'sys.stderr.write("xyz"*%d);' |
| 517 | 'sys.stdout.write(sys.stdin.read())' % pipe_buf], |
| 518 | stdin=subprocess.PIPE, |
| 519 | stdout=subprocess.PIPE, |
| 520 | stderr=subprocess.PIPE) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 521 | self.addCleanup(p.stdout.close) |
| 522 | self.addCleanup(p.stderr.close) |
| 523 | self.addCleanup(p.stdin.close) |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 524 | string_to_write = b"abc"*pipe_buf |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 525 | (stdout, stderr) = p.communicate(string_to_write) |
| 526 | self.assertEqual(stdout, string_to_write) |
| 527 | |
| 528 | def test_writes_before_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 529 | # stdin.write before communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 530 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 531 | 'import sys,os;' |
| 532 | 'sys.stdout.write(sys.stdin.read())'], |
| 533 | stdin=subprocess.PIPE, |
| 534 | stdout=subprocess.PIPE, |
| 535 | stderr=subprocess.PIPE) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 536 | self.addCleanup(p.stdout.close) |
| 537 | self.addCleanup(p.stderr.close) |
| 538 | self.addCleanup(p.stdin.close) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 539 | p.stdin.write(b"banana") |
| 540 | (stdout, stderr) = p.communicate(b"split") |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 541 | self.assertEqual(stdout, b"bananasplit") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 542 | self.assertStderrEqual(stderr, b"") |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 543 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 544 | def test_universal_newlines(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 545 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 546 | 'import sys,os;' + SETBINARY + |
Antoine Pitrou | ec2d269 | 2012-08-05 00:23:40 +0200 | [diff] [blame] | 547 | 'buf = sys.stdout.buffer;' |
| 548 | 'buf.write(sys.stdin.readline().encode());' |
| 549 | 'buf.flush();' |
| 550 | 'buf.write(b"line2\\n");' |
| 551 | 'buf.flush();' |
| 552 | 'buf.write(sys.stdin.read().encode());' |
| 553 | 'buf.flush();' |
| 554 | 'buf.write(b"line4\\n");' |
| 555 | 'buf.flush();' |
| 556 | 'buf.write(b"line5\\r\\n");' |
| 557 | 'buf.flush();' |
| 558 | 'buf.write(b"line6\\r");' |
| 559 | 'buf.flush();' |
| 560 | 'buf.write(b"\\nline7");' |
| 561 | 'buf.flush();' |
| 562 | 'buf.write(b"\\nline8");'], |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 563 | stdin=subprocess.PIPE, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 564 | stdout=subprocess.PIPE, |
| 565 | universal_newlines=1) |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 566 | p.stdin.write("line1\n") |
| 567 | self.assertEqual(p.stdout.readline(), "line1\n") |
| 568 | p.stdin.write("line3\n") |
| 569 | p.stdin.close() |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 570 | self.addCleanup(p.stdout.close) |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 571 | self.assertEqual(p.stdout.readline(), |
| 572 | "line2\n") |
| 573 | self.assertEqual(p.stdout.read(6), |
| 574 | "line3\n") |
| 575 | self.assertEqual(p.stdout.read(), |
| 576 | "line4\nline5\nline6\nline7\nline8") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 577 | |
| 578 | def test_universal_newlines_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 579 | # universal newlines through communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 580 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 581 | 'import sys,os;' + SETBINARY + |
Antoine Pitrou | ec2d269 | 2012-08-05 00:23:40 +0200 | [diff] [blame] | 582 | 'buf = sys.stdout.buffer;' |
| 583 | 'buf.write(b"line2\\n");' |
| 584 | 'buf.flush();' |
| 585 | 'buf.write(b"line4\\n");' |
| 586 | 'buf.flush();' |
| 587 | 'buf.write(b"line5\\r\\n");' |
| 588 | 'buf.flush();' |
| 589 | 'buf.write(b"line6\\r");' |
| 590 | 'buf.flush();' |
| 591 | 'buf.write(b"\\nline7");' |
| 592 | 'buf.flush();' |
| 593 | 'buf.write(b"\\nline8");'], |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 594 | stderr=subprocess.PIPE, |
| 595 | stdout=subprocess.PIPE, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 596 | universal_newlines=1) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 597 | self.addCleanup(p.stdout.close) |
| 598 | self.addCleanup(p.stderr.close) |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 599 | # BUG: can't give a non-empty stdin because it breaks both the |
| 600 | # select- and poll-based communicate() implementations. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 601 | (stdout, stderr) = p.communicate() |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 602 | self.assertEqual(stdout, |
| 603 | "line2\nline4\nline5\nline6\nline7\nline8") |
| 604 | |
| 605 | def test_universal_newlines_communicate_stdin(self): |
| 606 | # universal newlines through communicate(), with only stdin |
| 607 | p = subprocess.Popen([sys.executable, "-c", |
| 608 | 'import sys,os;' + SETBINARY + '''\nif True: |
| 609 | s = sys.stdin.readline() |
| 610 | assert s == "line1\\n", repr(s) |
| 611 | s = sys.stdin.read() |
| 612 | assert s == "line3\\n", repr(s) |
| 613 | '''], |
| 614 | stdin=subprocess.PIPE, |
| 615 | universal_newlines=1) |
| 616 | (stdout, stderr) = p.communicate("line1\nline3\n") |
| 617 | self.assertEqual(p.returncode, 0) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 618 | |
Andrew Svetlov | f376507 | 2012-08-14 18:35:17 +0300 | [diff] [blame] | 619 | def test_universal_newlines_communicate_input_none(self): |
| 620 | # Test communicate(input=None) with universal newlines. |
| 621 | # |
| 622 | # We set stdout to PIPE because, as of this writing, a different |
| 623 | # code path is tested when the number of pipes is zero or one. |
| 624 | p = subprocess.Popen([sys.executable, "-c", "pass"], |
| 625 | stdin=subprocess.PIPE, |
| 626 | stdout=subprocess.PIPE, |
| 627 | universal_newlines=True) |
| 628 | p.communicate() |
| 629 | self.assertEqual(p.returncode, 0) |
| 630 | |
Andrew Svetlov | 8286071 | 2012-08-19 22:13:41 +0300 | [diff] [blame] | 631 | def test_universal_newlines_communicate_encodings(self): |
| 632 | # Check that universal newlines mode works for various encodings, |
| 633 | # in particular for encodings in the UTF-16 and UTF-32 families. |
| 634 | # See issue #15595. |
| 635 | # |
| 636 | # UTF-16 and UTF-32-BE are sufficient to check both with BOM and |
| 637 | # without, and UTF-16 and UTF-32. |
| 638 | for encoding in ['utf-16', 'utf-32-be']: |
| 639 | old_getpreferredencoding = locale.getpreferredencoding |
| 640 | # Indirectly via io.TextIOWrapper, Popen() defaults to |
| 641 | # locale.getpreferredencoding(False) and earlier in Python 3.2 to |
| 642 | # locale.getpreferredencoding(). |
| 643 | def getpreferredencoding(do_setlocale=True): |
| 644 | return encoding |
| 645 | code = ("import sys; " |
| 646 | r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" % |
| 647 | encoding) |
| 648 | args = [sys.executable, '-c', code] |
| 649 | try: |
| 650 | locale.getpreferredencoding = getpreferredencoding |
| 651 | # We set stdin to be non-None because, as of this writing, |
| 652 | # a different code path is used when the number of pipes is |
| 653 | # zero or one. |
| 654 | popen = subprocess.Popen(args, universal_newlines=True, |
| 655 | stdin=subprocess.PIPE, |
| 656 | stdout=subprocess.PIPE) |
| 657 | stdout, stderr = popen.communicate(input='') |
| 658 | finally: |
| 659 | locale.getpreferredencoding = old_getpreferredencoding |
| 660 | |
| 661 | self.assertEqual(stdout, '1\n2\n3\n4') |
| 662 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 663 | def test_no_leaking(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 664 | # Make sure we leak no resources |
Antoine Pitrou | 8db3027 | 2010-09-18 22:38:48 +0000 | [diff] [blame] | 665 | if not mswindows: |
Peter Astrand | f7f1bb7 | 2005-03-03 20:47:37 +0000 | [diff] [blame] | 666 | max_handles = 1026 # too much for most UNIX systems |
| 667 | else: |
Antoine Pitrou | 8db3027 | 2010-09-18 22:38:48 +0000 | [diff] [blame] | 668 | max_handles = 2050 # too much for (at least some) Windows setups |
| 669 | handles = [] |
Gregory P. Smith | 81ce685 | 2011-03-15 02:04:11 -0400 | [diff] [blame] | 670 | tmpdir = tempfile.mkdtemp() |
Antoine Pitrou | 8db3027 | 2010-09-18 22:38:48 +0000 | [diff] [blame] | 671 | try: |
| 672 | for i in range(max_handles): |
| 673 | try: |
Gregory P. Smith | 81ce685 | 2011-03-15 02:04:11 -0400 | [diff] [blame] | 674 | tmpfile = os.path.join(tmpdir, support.TESTFN) |
| 675 | handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT)) |
Antoine Pitrou | 8db3027 | 2010-09-18 22:38:48 +0000 | [diff] [blame] | 676 | except OSError as e: |
| 677 | if e.errno != errno.EMFILE: |
| 678 | raise |
| 679 | break |
| 680 | else: |
| 681 | self.skipTest("failed to reach the file descriptor limit " |
| 682 | "(tried %d)" % max_handles) |
| 683 | # Close a couple of them (should be enough for a subprocess) |
| 684 | for i in range(10): |
| 685 | os.close(handles.pop()) |
| 686 | # Loop creating some subprocesses. If one of them leaks some fds, |
| 687 | # the next loop iteration will fail by reaching the max fd limit. |
| 688 | for i in range(15): |
| 689 | p = subprocess.Popen([sys.executable, "-c", |
| 690 | "import sys;" |
| 691 | "sys.stdout.write(sys.stdin.read())"], |
| 692 | stdin=subprocess.PIPE, |
| 693 | stdout=subprocess.PIPE, |
| 694 | stderr=subprocess.PIPE) |
| 695 | data = p.communicate(b"lime")[0] |
| 696 | self.assertEqual(data, b"lime") |
| 697 | finally: |
| 698 | for h in handles: |
| 699 | os.close(h) |
Gregory P. Smith | 81ce685 | 2011-03-15 02:04:11 -0400 | [diff] [blame] | 700 | shutil.rmtree(tmpdir) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 701 | |
| 702 | def test_list2cmdline(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 703 | self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), |
| 704 | '"a b c" d e') |
| 705 | self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), |
| 706 | 'ab\\"c \\ d') |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 707 | self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']), |
| 708 | 'ab\\"c " \\\\" d') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 709 | self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']), |
| 710 | 'a\\\\\\b "de fg" h') |
| 711 | self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']), |
| 712 | 'a\\\\\\"b c d') |
| 713 | self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']), |
| 714 | '"a\\\\b c" d e') |
| 715 | self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']), |
| 716 | '"a\\\\b\\ c" d e') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 717 | self.assertEqual(subprocess.list2cmdline(['ab', '']), |
| 718 | 'ab ""') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 719 | |
| 720 | |
| 721 | def test_poll(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 722 | p = subprocess.Popen([sys.executable, |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 723 | "-c", "import time; time.sleep(1)"]) |
| 724 | count = 0 |
| 725 | while p.poll() is None: |
| 726 | time.sleep(0.1) |
| 727 | count += 1 |
| 728 | # We expect that the poll loop probably went around about 10 times, |
| 729 | # but, based on system scheduling we can't control, it's possible |
| 730 | # poll() never returned None. It "should be" very rare that it |
| 731 | # didn't go around at least twice. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 732 | self.assertGreaterEqual(count, 2) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 733 | # Subsequent invocations should just return the returncode |
| 734 | self.assertEqual(p.poll(), 0) |
| 735 | |
| 736 | |
| 737 | def test_wait(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 738 | p = subprocess.Popen([sys.executable, |
| 739 | "-c", "import time; time.sleep(2)"]) |
| 740 | self.assertEqual(p.wait(), 0) |
| 741 | # Subsequent invocations should just return the returncode |
| 742 | self.assertEqual(p.wait(), 0) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 743 | |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 744 | |
| 745 | def test_invalid_bufsize(self): |
| 746 | # an invalid type of the bufsize argument should raise |
| 747 | # TypeError. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 748 | with self.assertRaises(TypeError): |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 749 | subprocess.Popen([sys.executable, "-c", "pass"], "orange") |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 750 | |
Guido van Rossum | 46a05a7 | 2007-06-07 21:56:45 +0000 | [diff] [blame] | 751 | def test_bufsize_is_none(self): |
| 752 | # bufsize=None should be the same as bufsize=0. |
| 753 | p = subprocess.Popen([sys.executable, "-c", "pass"], None) |
| 754 | self.assertEqual(p.wait(), 0) |
| 755 | # Again with keyword arg |
| 756 | p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None) |
| 757 | self.assertEqual(p.wait(), 0) |
| 758 | |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 759 | def test_leaking_fds_on_error(self): |
| 760 | # see bug #5179: Popen leaks file descriptors to PIPEs if |
| 761 | # the child fails to execute; this will eventually exhaust |
| 762 | # the maximum number of open fds. 1024 seems a very common |
| 763 | # value for that limit, but Windows has 2048, so we loop |
| 764 | # 1024 times (each call leaked two fds). |
| 765 | for i in range(1024): |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 766 | # Windows raises IOError. Others raise OSError. |
| 767 | with self.assertRaises(EnvironmentError) as c: |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 768 | subprocess.Popen(['nonexisting_i_hope'], |
| 769 | stdout=subprocess.PIPE, |
| 770 | stderr=subprocess.PIPE) |
R David Murray | 384069c | 2011-03-13 22:26:53 -0400 | [diff] [blame] | 771 | # ignore errors that indicate the command was not found |
R David Murray | 6924bd7 | 2011-03-13 22:48:55 -0400 | [diff] [blame] | 772 | if c.exception.errno not in (errno.ENOENT, errno.EACCES): |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 773 | raise c.exception |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 774 | |
Victor Stinner | b369358 | 2010-05-21 20:13:12 +0000 | [diff] [blame] | 775 | def test_issue8780(self): |
| 776 | # Ensure that stdout is inherited from the parent |
| 777 | # if stdout=PIPE is not used |
| 778 | code = ';'.join(( |
| 779 | 'import subprocess, sys', |
| 780 | 'retcode = subprocess.call(' |
| 781 | "[sys.executable, '-c', 'print(\"Hello World!\")'])", |
| 782 | 'assert retcode == 0')) |
| 783 | output = subprocess.check_output([sys.executable, '-c', code]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 784 | self.assertTrue(output.startswith(b'Hello World!'), ascii(output)) |
Victor Stinner | b369358 | 2010-05-21 20:13:12 +0000 | [diff] [blame] | 785 | |
Tim Golden | af5ac39 | 2010-08-06 13:03:56 +0000 | [diff] [blame] | 786 | def test_handles_closed_on_exception(self): |
| 787 | # If CreateProcess exits with an error, ensure the |
| 788 | # duplicate output handles are released |
| 789 | ifhandle, ifname = mkstemp() |
| 790 | ofhandle, ofname = mkstemp() |
| 791 | efhandle, efname = mkstemp() |
| 792 | try: |
| 793 | subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle, |
| 794 | stderr=efhandle) |
| 795 | except OSError: |
| 796 | os.close(ifhandle) |
| 797 | os.remove(ifname) |
| 798 | os.close(ofhandle) |
| 799 | os.remove(ofname) |
| 800 | os.close(efhandle) |
| 801 | os.remove(efname) |
| 802 | self.assertFalse(os.path.exists(ifname)) |
| 803 | self.assertFalse(os.path.exists(ofname)) |
| 804 | self.assertFalse(os.path.exists(efname)) |
| 805 | |
Ross Lagerwall | 4f61b02 | 2011-04-05 15:34:00 +0200 | [diff] [blame] | 806 | def test_communicate_epipe(self): |
| 807 | # Issue 10963: communicate() should hide EPIPE |
| 808 | p = subprocess.Popen([sys.executable, "-c", 'pass'], |
| 809 | stdin=subprocess.PIPE, |
| 810 | stdout=subprocess.PIPE, |
| 811 | stderr=subprocess.PIPE) |
| 812 | self.addCleanup(p.stdout.close) |
| 813 | self.addCleanup(p.stderr.close) |
| 814 | self.addCleanup(p.stdin.close) |
| 815 | p.communicate(b"x" * 2**20) |
| 816 | |
| 817 | def test_communicate_epipe_only_stdin(self): |
| 818 | # Issue 10963: communicate() should hide EPIPE |
| 819 | p = subprocess.Popen([sys.executable, "-c", 'pass'], |
| 820 | stdin=subprocess.PIPE) |
| 821 | self.addCleanup(p.stdin.close) |
| 822 | time.sleep(2) |
| 823 | p.communicate(b"x" * 2**20) |
| 824 | |
Victor Stinner | 1848db8 | 2011-07-05 14:49:46 +0200 | [diff] [blame] | 825 | @unittest.skipUnless(hasattr(signal, 'SIGALRM'), |
| 826 | "Requires signal.SIGALRM") |
Victor Stinner | 2cfb6f3 | 2011-07-05 14:00:56 +0200 | [diff] [blame] | 827 | def test_communicate_eintr(self): |
| 828 | # Issue #12493: communicate() should handle EINTR |
| 829 | def handler(signum, frame): |
| 830 | pass |
| 831 | old_handler = signal.signal(signal.SIGALRM, handler) |
| 832 | self.addCleanup(signal.signal, signal.SIGALRM, old_handler) |
| 833 | |
| 834 | # the process is running for 2 seconds |
| 835 | args = [sys.executable, "-c", 'import time; time.sleep(2)'] |
| 836 | for stream in ('stdout', 'stderr'): |
| 837 | kw = {stream: subprocess.PIPE} |
| 838 | with subprocess.Popen(args, **kw) as process: |
| 839 | signal.alarm(1) |
| 840 | # communicate() will be interrupted by SIGALRM |
| 841 | process.communicate() |
| 842 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 843 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 844 | # context manager |
| 845 | class _SuppressCoreFiles(object): |
| 846 | """Try to prevent core files from being created.""" |
| 847 | old_limit = None |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 848 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 849 | def __enter__(self): |
| 850 | """Try to save previous ulimit, then set it to (0, 0).""" |
Benjamin Peterson | 964561b | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 851 | if resource is not None: |
| 852 | try: |
| 853 | self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) |
| 854 | resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) |
| 855 | except (ValueError, resource.error): |
| 856 | pass |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 857 | |
Ronald Oussoren | 102d11a | 2010-07-23 09:50:05 +0000 | [diff] [blame] | 858 | if sys.platform == 'darwin': |
| 859 | # Check if the 'Crash Reporter' on OSX was configured |
| 860 | # in 'Developer' mode and warn that it will get triggered |
| 861 | # when it is. |
| 862 | # |
| 863 | # This assumes that this context manager is used in tests |
| 864 | # that might trigger the next manager. |
| 865 | value = subprocess.Popen(['/usr/bin/defaults', 'read', |
| 866 | 'com.apple.CrashReporter', 'DialogType'], |
| 867 | stdout=subprocess.PIPE).communicate()[0] |
| 868 | if value.strip() == b'developer': |
| 869 | print("this tests triggers the Crash Reporter, " |
| 870 | "that is intentional", end='') |
| 871 | sys.stdout.flush() |
| 872 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 873 | def __exit__(self, *args): |
| 874 | """Return core file behavior to default.""" |
| 875 | if self.old_limit is None: |
| 876 | return |
Benjamin Peterson | 964561b | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 877 | if resource is not None: |
| 878 | try: |
| 879 | resource.setrlimit(resource.RLIMIT_CORE, self.old_limit) |
| 880 | except (ValueError, resource.error): |
| 881 | pass |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 882 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 883 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 884 | @unittest.skipIf(mswindows, "POSIX specific tests") |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 885 | class POSIXProcessTestCase(BaseTestCase): |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 886 | |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame^] | 887 | def setUp(self): |
| 888 | super().setUp() |
| 889 | self._nonexistent_dir = "/_this/pa.th/does/not/exist" |
| 890 | |
| 891 | def _get_chdir_exception(self): |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 892 | try: |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame^] | 893 | os.chdir(self._nonexistent_dir) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 894 | except OSError as e: |
| 895 | # This avoids hard coding the errno value or the OS perror() |
| 896 | # string and instead capture the exception that we want to see |
| 897 | # below for comparison. |
| 898 | desired_exception = e |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame^] | 899 | desired_exception.strerror += ': ' + repr(self._nonexistent_dir) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 900 | else: |
| 901 | self.fail("chdir to nonexistant directory %s succeeded." % |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame^] | 902 | self._nonexistent_dir) |
| 903 | return desired_exception |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 904 | |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame^] | 905 | def test_exception_cwd(self): |
| 906 | """Test error in the child raised in the parent for a bad cwd.""" |
| 907 | desired_exception = self._get_chdir_exception() |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 908 | try: |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 909 | p = subprocess.Popen([sys.executable, "-c", ""], |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame^] | 910 | cwd=self._nonexistent_dir) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 911 | except OSError as e: |
| 912 | # Test that the child process chdir failure actually makes |
| 913 | # it up to the parent process as the correct exception. |
| 914 | self.assertEqual(desired_exception.errno, e.errno) |
| 915 | self.assertEqual(desired_exception.strerror, e.strerror) |
| 916 | else: |
| 917 | self.fail("Expected OSError: %s" % desired_exception) |
| 918 | |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame^] | 919 | def test_exception_bad_executable(self): |
| 920 | """Test error in the child raised in the parent for a bad executable.""" |
| 921 | desired_exception = self._get_chdir_exception() |
| 922 | try: |
| 923 | p = subprocess.Popen([sys.executable, "-c", ""], |
| 924 | executable=self._nonexistent_dir) |
| 925 | except OSError as e: |
| 926 | # Test that the child process exec failure actually makes |
| 927 | # it up to the parent process as the correct exception. |
| 928 | self.assertEqual(desired_exception.errno, e.errno) |
| 929 | self.assertEqual(desired_exception.strerror, e.strerror) |
| 930 | else: |
| 931 | self.fail("Expected OSError: %s" % desired_exception) |
| 932 | |
| 933 | def test_exception_bad_args_0(self): |
| 934 | """Test error in the child raised in the parent for a bad args[0].""" |
| 935 | desired_exception = self._get_chdir_exception() |
| 936 | try: |
| 937 | p = subprocess.Popen([self._nonexistent_dir, "-c", ""]) |
| 938 | except OSError as e: |
| 939 | # Test that the child process exec failure actually makes |
| 940 | # it up to the parent process as the correct exception. |
| 941 | self.assertEqual(desired_exception.errno, e.errno) |
| 942 | self.assertEqual(desired_exception.strerror, e.strerror) |
| 943 | else: |
| 944 | self.fail("Expected OSError: %s" % desired_exception) |
| 945 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 946 | def test_restore_signals(self): |
| 947 | # Code coverage for both values of restore_signals to make sure it |
| 948 | # at least does not blow up. |
| 949 | # A test for behavior would be complex. Contributions welcome. |
| 950 | subprocess.call([sys.executable, "-c", ""], restore_signals=True) |
| 951 | subprocess.call([sys.executable, "-c", ""], restore_signals=False) |
| 952 | |
| 953 | def test_start_new_session(self): |
| 954 | # For code coverage of calling setsid(). We don't care if we get an |
| 955 | # EPERM error from it depending on the test execution environment, that |
| 956 | # still indicates that it was called. |
| 957 | try: |
| 958 | output = subprocess.check_output( |
| 959 | [sys.executable, "-c", |
| 960 | "import os; print(os.getpgid(os.getpid()))"], |
| 961 | start_new_session=True) |
| 962 | except OSError as e: |
| 963 | if e.errno != errno.EPERM: |
| 964 | raise |
| 965 | else: |
| 966 | parent_pgid = os.getpgid(os.getpid()) |
| 967 | child_pgid = int(output) |
| 968 | self.assertNotEqual(parent_pgid, child_pgid) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 969 | |
| 970 | def test_run_abort(self): |
| 971 | # returncode handles signal termination |
| 972 | with _SuppressCoreFiles(): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 973 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 974 | 'import os; os.abort()']) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 975 | p.wait() |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 976 | self.assertEqual(-p.returncode, signal.SIGABRT) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 977 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 978 | def test_preexec(self): |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 979 | # DISCLAIMER: Setting environment variables is *not* a good use |
| 980 | # of a preexec_fn. This is merely a test. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 981 | p = subprocess.Popen([sys.executable, "-c", |
| 982 | 'import sys,os;' |
| 983 | 'sys.stdout.write(os.getenv("FRUIT"))'], |
| 984 | stdout=subprocess.PIPE, |
| 985 | preexec_fn=lambda: os.putenv("FRUIT", "apple")) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 986 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 987 | self.assertEqual(p.stdout.read(), b"apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 988 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 989 | def test_preexec_exception(self): |
| 990 | def raise_it(): |
| 991 | raise ValueError("What if two swallows carried a coconut?") |
| 992 | try: |
| 993 | p = subprocess.Popen([sys.executable, "-c", ""], |
| 994 | preexec_fn=raise_it) |
| 995 | except RuntimeError as e: |
| 996 | self.assertTrue( |
| 997 | subprocess._posixsubprocess, |
| 998 | "Expected a ValueError from the preexec_fn") |
| 999 | except ValueError as e: |
| 1000 | self.assertIn("coconut", e.args[0]) |
| 1001 | else: |
| 1002 | self.fail("Exception raised by preexec_fn did not make it " |
| 1003 | "to the parent process.") |
| 1004 | |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 1005 | def test_preexec_gc_module_failure(self): |
| 1006 | # This tests the code that disables garbage collection if the child |
| 1007 | # process will execute any Python. |
| 1008 | def raise_runtime_error(): |
| 1009 | raise RuntimeError("this shouldn't escape") |
| 1010 | enabled = gc.isenabled() |
| 1011 | orig_gc_disable = gc.disable |
| 1012 | orig_gc_isenabled = gc.isenabled |
| 1013 | try: |
| 1014 | gc.disable() |
| 1015 | self.assertFalse(gc.isenabled()) |
| 1016 | subprocess.call([sys.executable, '-c', ''], |
| 1017 | preexec_fn=lambda: None) |
| 1018 | self.assertFalse(gc.isenabled(), |
| 1019 | "Popen enabled gc when it shouldn't.") |
| 1020 | |
| 1021 | gc.enable() |
| 1022 | self.assertTrue(gc.isenabled()) |
| 1023 | subprocess.call([sys.executable, '-c', ''], |
| 1024 | preexec_fn=lambda: None) |
| 1025 | self.assertTrue(gc.isenabled(), "Popen left gc disabled.") |
| 1026 | |
| 1027 | gc.disable = raise_runtime_error |
| 1028 | self.assertRaises(RuntimeError, subprocess.Popen, |
| 1029 | [sys.executable, '-c', ''], |
| 1030 | preexec_fn=lambda: None) |
| 1031 | |
| 1032 | del gc.isenabled # force an AttributeError |
| 1033 | self.assertRaises(AttributeError, subprocess.Popen, |
| 1034 | [sys.executable, '-c', ''], |
| 1035 | preexec_fn=lambda: None) |
| 1036 | finally: |
| 1037 | gc.disable = orig_gc_disable |
| 1038 | gc.isenabled = orig_gc_isenabled |
| 1039 | if not enabled: |
| 1040 | gc.disable() |
| 1041 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1042 | def test_args_string(self): |
| 1043 | # args is a string |
| 1044 | fd, fname = mkstemp() |
| 1045 | # reopen in text mode |
Victor Stinner | f6782ac | 2010-10-16 23:46:43 +0000 | [diff] [blame] | 1046 | with open(fd, "w", errors="surrogateescape") as fobj: |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1047 | fobj.write("#!/bin/sh\n") |
| 1048 | fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 1049 | sys.executable) |
| 1050 | os.chmod(fname, 0o700) |
| 1051 | p = subprocess.Popen(fname) |
| 1052 | p.wait() |
| 1053 | os.remove(fname) |
| 1054 | self.assertEqual(p.returncode, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1055 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1056 | def test_invalid_args(self): |
| 1057 | # invalid arguments should raise ValueError |
| 1058 | self.assertRaises(ValueError, subprocess.call, |
| 1059 | [sys.executable, "-c", |
| 1060 | "import sys; sys.exit(47)"], |
| 1061 | startupinfo=47) |
| 1062 | self.assertRaises(ValueError, subprocess.call, |
| 1063 | [sys.executable, "-c", |
| 1064 | "import sys; sys.exit(47)"], |
| 1065 | creationflags=47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1066 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1067 | def test_shell_sequence(self): |
| 1068 | # Run command through the shell (sequence) |
| 1069 | newenv = os.environ.copy() |
| 1070 | newenv["FRUIT"] = "apple" |
| 1071 | p = subprocess.Popen(["echo $FRUIT"], shell=1, |
| 1072 | stdout=subprocess.PIPE, |
| 1073 | env=newenv) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 1074 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1075 | self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1076 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1077 | def test_shell_string(self): |
| 1078 | # Run command through the shell (string) |
| 1079 | newenv = os.environ.copy() |
| 1080 | newenv["FRUIT"] = "apple" |
| 1081 | p = subprocess.Popen("echo $FRUIT", shell=1, |
| 1082 | stdout=subprocess.PIPE, |
| 1083 | env=newenv) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 1084 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1085 | self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple") |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1086 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1087 | def test_call_string(self): |
| 1088 | # call() function with string argument on UNIX |
| 1089 | fd, fname = mkstemp() |
| 1090 | # reopen in text mode |
Victor Stinner | f6782ac | 2010-10-16 23:46:43 +0000 | [diff] [blame] | 1091 | with open(fd, "w", errors="surrogateescape") as fobj: |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1092 | fobj.write("#!/bin/sh\n") |
| 1093 | fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 1094 | sys.executable) |
| 1095 | os.chmod(fname, 0o700) |
| 1096 | rc = subprocess.call(fname) |
| 1097 | os.remove(fname) |
| 1098 | self.assertEqual(rc, 47) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1099 | |
Stefan Krah | 9542cc6 | 2010-07-19 14:20:53 +0000 | [diff] [blame] | 1100 | def test_specific_shell(self): |
| 1101 | # Issue #9265: Incorrect name passed as arg[0]. |
| 1102 | shells = [] |
| 1103 | for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']: |
| 1104 | for name in ['bash', 'ksh']: |
| 1105 | sh = os.path.join(prefix, name) |
| 1106 | if os.path.isfile(sh): |
| 1107 | shells.append(sh) |
| 1108 | if not shells: # Will probably work for any shell but csh. |
| 1109 | self.skipTest("bash or ksh required for this test") |
| 1110 | sh = '/bin/sh' |
| 1111 | if os.path.isfile(sh) and not os.path.islink(sh): |
| 1112 | # Test will fail if /bin/sh is a symlink to csh. |
| 1113 | shells.append(sh) |
| 1114 | for sh in shells: |
| 1115 | p = subprocess.Popen("echo $0", executable=sh, shell=True, |
| 1116 | stdout=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 1117 | self.addCleanup(p.stdout.close) |
Stefan Krah | 9542cc6 | 2010-07-19 14:20:53 +0000 | [diff] [blame] | 1118 | self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii')) |
| 1119 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1120 | def _kill_process(self, method, *args): |
Florent Xicluna | 1d8ee3a | 2010-03-05 20:26:54 +0000 | [diff] [blame] | 1121 | # Do not inherit file handles from the parent. |
| 1122 | # It should fix failures on some platforms. |
Antoine Pitrou | 3d8580f | 2010-09-20 01:33:21 +0000 | [diff] [blame] | 1123 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 1124 | import sys, time |
| 1125 | sys.stdout.write('x\\n') |
| 1126 | sys.stdout.flush() |
| 1127 | time.sleep(30) |
| 1128 | """], |
| 1129 | close_fds=True, |
| 1130 | stdin=subprocess.PIPE, |
| 1131 | stdout=subprocess.PIPE, |
| 1132 | stderr=subprocess.PIPE) |
| 1133 | # Wait for the interpreter to be completely initialized before |
| 1134 | # sending any signal. |
| 1135 | p.stdout.read(1) |
| 1136 | getattr(p, method)(*args) |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1137 | return p |
| 1138 | |
Antoine Pitrou | 1f9a835 | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 1139 | def _kill_dead_process(self, method, *args): |
| 1140 | # Do not inherit file handles from the parent. |
| 1141 | # It should fix failures on some platforms. |
| 1142 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 1143 | import sys, time |
| 1144 | sys.stdout.write('x\\n') |
| 1145 | sys.stdout.flush() |
| 1146 | """], |
| 1147 | close_fds=True, |
| 1148 | stdin=subprocess.PIPE, |
| 1149 | stdout=subprocess.PIPE, |
| 1150 | stderr=subprocess.PIPE) |
| 1151 | # Wait for the interpreter to be completely initialized before |
| 1152 | # sending any signal. |
| 1153 | p.stdout.read(1) |
| 1154 | # The process should end after this |
| 1155 | time.sleep(1) |
| 1156 | # This shouldn't raise even though the child is now dead |
| 1157 | getattr(p, method)(*args) |
| 1158 | p.communicate() |
| 1159 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1160 | def test_send_signal(self): |
| 1161 | p = self._kill_process('send_signal', signal.SIGINT) |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 1162 | _, stderr = p.communicate() |
| 1163 | self.assertIn(b'KeyboardInterrupt', stderr) |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 1164 | self.assertNotEqual(p.wait(), 0) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1165 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1166 | def test_kill(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1167 | p = self._kill_process('kill') |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 1168 | _, stderr = p.communicate() |
| 1169 | self.assertStderrEqual(stderr, b'') |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1170 | self.assertEqual(p.wait(), -signal.SIGKILL) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1171 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1172 | def test_terminate(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1173 | p = self._kill_process('terminate') |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 1174 | _, stderr = p.communicate() |
| 1175 | self.assertStderrEqual(stderr, b'') |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1176 | self.assertEqual(p.wait(), -signal.SIGTERM) |
| 1177 | |
Antoine Pitrou | 1f9a835 | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 1178 | def test_send_signal_dead(self): |
| 1179 | # Sending a signal to a dead process |
| 1180 | self._kill_dead_process('send_signal', signal.SIGINT) |
| 1181 | |
| 1182 | def test_kill_dead(self): |
| 1183 | # Killing a dead process |
| 1184 | self._kill_dead_process('kill') |
| 1185 | |
| 1186 | def test_terminate_dead(self): |
| 1187 | # Terminating a dead process |
| 1188 | self._kill_dead_process('terminate') |
| 1189 | |
Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 1190 | def check_close_std_fds(self, fds): |
| 1191 | # Issue #9905: test that subprocess pipes still work properly with |
| 1192 | # some standard fds closed |
| 1193 | stdin = 0 |
| 1194 | newfds = [] |
| 1195 | for a in fds: |
| 1196 | b = os.dup(a) |
| 1197 | newfds.append(b) |
| 1198 | if a == 0: |
| 1199 | stdin = b |
| 1200 | try: |
| 1201 | for fd in fds: |
| 1202 | os.close(fd) |
| 1203 | out, err = subprocess.Popen([sys.executable, "-c", |
| 1204 | 'import sys;' |
| 1205 | 'sys.stdout.write("apple");' |
| 1206 | 'sys.stdout.flush();' |
| 1207 | 'sys.stderr.write("orange")'], |
| 1208 | stdin=stdin, |
| 1209 | stdout=subprocess.PIPE, |
| 1210 | stderr=subprocess.PIPE).communicate() |
| 1211 | err = support.strip_python_stderr(err) |
| 1212 | self.assertEqual((out, err), (b'apple', b'orange')) |
| 1213 | finally: |
| 1214 | for b, a in zip(newfds, fds): |
| 1215 | os.dup2(b, a) |
| 1216 | for b in newfds: |
| 1217 | os.close(b) |
| 1218 | |
| 1219 | def test_close_fd_0(self): |
| 1220 | self.check_close_std_fds([0]) |
| 1221 | |
| 1222 | def test_close_fd_1(self): |
| 1223 | self.check_close_std_fds([1]) |
| 1224 | |
| 1225 | def test_close_fd_2(self): |
| 1226 | self.check_close_std_fds([2]) |
| 1227 | |
| 1228 | def test_close_fds_0_1(self): |
| 1229 | self.check_close_std_fds([0, 1]) |
| 1230 | |
| 1231 | def test_close_fds_0_2(self): |
| 1232 | self.check_close_std_fds([0, 2]) |
| 1233 | |
| 1234 | def test_close_fds_1_2(self): |
| 1235 | self.check_close_std_fds([1, 2]) |
| 1236 | |
| 1237 | def test_close_fds_0_1_2(self): |
| 1238 | # Issue #10806: test that subprocess pipes still work properly with |
| 1239 | # all standard fds closed. |
| 1240 | self.check_close_std_fds([0, 1, 2]) |
| 1241 | |
Antoine Pitrou | 95aaeee | 2011-01-03 21:15:48 +0000 | [diff] [blame] | 1242 | def test_remapping_std_fds(self): |
| 1243 | # open up some temporary files |
| 1244 | temps = [mkstemp() for i in range(3)] |
| 1245 | try: |
| 1246 | temp_fds = [fd for fd, fname in temps] |
| 1247 | |
| 1248 | # unlink the files -- we won't need to reopen them |
| 1249 | for fd, fname in temps: |
| 1250 | os.unlink(fname) |
| 1251 | |
| 1252 | # write some data to what will become stdin, and rewind |
| 1253 | os.write(temp_fds[1], b"STDIN") |
| 1254 | os.lseek(temp_fds[1], 0, 0) |
| 1255 | |
| 1256 | # move the standard file descriptors out of the way |
| 1257 | saved_fds = [os.dup(fd) for fd in range(3)] |
| 1258 | try: |
| 1259 | # duplicate the file objects over the standard fd's |
| 1260 | for fd, temp_fd in enumerate(temp_fds): |
| 1261 | os.dup2(temp_fd, fd) |
| 1262 | |
| 1263 | # now use those files in the "wrong" order, so that subprocess |
| 1264 | # has to rearrange them in the child |
| 1265 | p = subprocess.Popen([sys.executable, "-c", |
| 1266 | 'import sys; got = sys.stdin.read();' |
| 1267 | 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'], |
| 1268 | stdin=temp_fds[1], |
| 1269 | stdout=temp_fds[2], |
| 1270 | stderr=temp_fds[0]) |
| 1271 | p.wait() |
| 1272 | finally: |
| 1273 | # restore the original fd's underneath sys.stdin, etc. |
| 1274 | for std, saved in enumerate(saved_fds): |
| 1275 | os.dup2(saved, std) |
| 1276 | os.close(saved) |
| 1277 | |
| 1278 | for fd in temp_fds: |
| 1279 | os.lseek(fd, 0, 0) |
| 1280 | |
| 1281 | out = os.read(temp_fds[2], 1024) |
| 1282 | err = support.strip_python_stderr(os.read(temp_fds[0], 1024)) |
| 1283 | self.assertEqual(out, b"got STDIN") |
| 1284 | self.assertEqual(err, b"err") |
| 1285 | |
| 1286 | finally: |
| 1287 | for fd in temp_fds: |
| 1288 | os.close(fd) |
| 1289 | |
Ross Lagerwall | d98646e | 2011-07-27 07:16:31 +0200 | [diff] [blame] | 1290 | def check_swap_fds(self, stdin_no, stdout_no, stderr_no): |
| 1291 | # open up some temporary files |
| 1292 | temps = [mkstemp() for i in range(3)] |
| 1293 | temp_fds = [fd for fd, fname in temps] |
| 1294 | try: |
| 1295 | # unlink the files -- we won't need to reopen them |
| 1296 | for fd, fname in temps: |
| 1297 | os.unlink(fname) |
| 1298 | |
| 1299 | # save a copy of the standard file descriptors |
| 1300 | saved_fds = [os.dup(fd) for fd in range(3)] |
| 1301 | try: |
| 1302 | # duplicate the temp files over the standard fd's 0, 1, 2 |
| 1303 | for fd, temp_fd in enumerate(temp_fds): |
| 1304 | os.dup2(temp_fd, fd) |
| 1305 | |
| 1306 | # write some data to what will become stdin, and rewind |
| 1307 | os.write(stdin_no, b"STDIN") |
| 1308 | os.lseek(stdin_no, 0, 0) |
| 1309 | |
| 1310 | # now use those files in the given order, so that subprocess |
| 1311 | # has to rearrange them in the child |
| 1312 | p = subprocess.Popen([sys.executable, "-c", |
| 1313 | 'import sys; got = sys.stdin.read();' |
| 1314 | 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'], |
| 1315 | stdin=stdin_no, |
| 1316 | stdout=stdout_no, |
| 1317 | stderr=stderr_no) |
| 1318 | p.wait() |
| 1319 | |
| 1320 | for fd in temp_fds: |
| 1321 | os.lseek(fd, 0, 0) |
| 1322 | |
| 1323 | out = os.read(stdout_no, 1024) |
| 1324 | err = support.strip_python_stderr(os.read(stderr_no, 1024)) |
| 1325 | finally: |
| 1326 | for std, saved in enumerate(saved_fds): |
| 1327 | os.dup2(saved, std) |
| 1328 | os.close(saved) |
| 1329 | |
| 1330 | self.assertEqual(out, b"got STDIN") |
| 1331 | self.assertEqual(err, b"err") |
| 1332 | |
| 1333 | finally: |
| 1334 | for fd in temp_fds: |
| 1335 | os.close(fd) |
| 1336 | |
| 1337 | # When duping fds, if there arises a situation where one of the fds is |
| 1338 | # either 0, 1 or 2, it is possible that it is overwritten (#12607). |
| 1339 | # This tests all combinations of this. |
| 1340 | def test_swap_fds(self): |
| 1341 | self.check_swap_fds(0, 1, 2) |
| 1342 | self.check_swap_fds(0, 2, 1) |
| 1343 | self.check_swap_fds(1, 0, 2) |
| 1344 | self.check_swap_fds(1, 2, 0) |
| 1345 | self.check_swap_fds(2, 0, 1) |
| 1346 | self.check_swap_fds(2, 1, 0) |
| 1347 | |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1348 | def test_surrogates_error_message(self): |
Victor Stinner | 4d07804 | 2010-04-23 19:28:32 +0000 | [diff] [blame] | 1349 | def prepare(): |
| 1350 | raise ValueError("surrogate:\uDCff") |
| 1351 | |
| 1352 | try: |
| 1353 | subprocess.call( |
| 1354 | [sys.executable, "-c", "pass"], |
| 1355 | preexec_fn=prepare) |
| 1356 | except ValueError as err: |
| 1357 | # Pure Python implementations keeps the message |
| 1358 | self.assertIsNone(subprocess._posixsubprocess) |
| 1359 | self.assertEqual(str(err), "surrogate:\uDCff") |
| 1360 | except RuntimeError as err: |
| 1361 | # _posixsubprocess uses a default message |
| 1362 | self.assertIsNotNone(subprocess._posixsubprocess) |
| 1363 | self.assertEqual(str(err), "Exception occurred in preexec_fn.") |
| 1364 | else: |
| 1365 | self.fail("Expected ValueError or RuntimeError") |
| 1366 | |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1367 | def test_undecodable_env(self): |
| 1368 | for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')): |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1369 | # test str with surrogates |
Antoine Pitrou | fb8db8f | 2010-09-19 22:46:05 +0000 | [diff] [blame] | 1370 | script = "import os; print(ascii(os.getenv(%s)))" % repr(key) |
Victor Stinner | ce2d24d | 2010-04-23 22:55:39 +0000 | [diff] [blame] | 1371 | env = os.environ.copy() |
| 1372 | env[key] = value |
Victor Stinner | 89f3ad1 | 2010-10-14 10:43:31 +0000 | [diff] [blame] | 1373 | # Use C locale to get ascii for the locale encoding to force |
| 1374 | # surrogate-escaping of \xFF in the child process; otherwise it can |
| 1375 | # be decoded as-is if the default locale is latin-1. |
Victor Stinner | ebc78d2 | 2010-10-14 10:38:17 +0000 | [diff] [blame] | 1376 | env['LC_ALL'] = 'C' |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1377 | stdout = subprocess.check_output( |
| 1378 | [sys.executable, "-c", script], |
Victor Stinner | ce2d24d | 2010-04-23 22:55:39 +0000 | [diff] [blame] | 1379 | env=env) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1380 | stdout = stdout.rstrip(b'\n\r') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1381 | self.assertEqual(stdout.decode('ascii'), ascii(value)) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1382 | |
| 1383 | # test bytes |
| 1384 | key = key.encode("ascii", "surrogateescape") |
| 1385 | value = value.encode("ascii", "surrogateescape") |
Antoine Pitrou | fb8db8f | 2010-09-19 22:46:05 +0000 | [diff] [blame] | 1386 | script = "import os; print(ascii(os.getenvb(%s)))" % repr(key) |
Victor Stinner | ce2d24d | 2010-04-23 22:55:39 +0000 | [diff] [blame] | 1387 | env = os.environ.copy() |
| 1388 | env[key] = value |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1389 | stdout = subprocess.check_output( |
| 1390 | [sys.executable, "-c", script], |
Victor Stinner | ce2d24d | 2010-04-23 22:55:39 +0000 | [diff] [blame] | 1391 | env=env) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1392 | stdout = stdout.rstrip(b'\n\r') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1393 | self.assertEqual(stdout.decode('ascii'), ascii(value)) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1394 | |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1395 | def test_bytes_program(self): |
| 1396 | abs_program = os.fsencode(sys.executable) |
| 1397 | path, program = os.path.split(sys.executable) |
| 1398 | program = os.fsencode(program) |
| 1399 | |
| 1400 | # absolute bytes path |
| 1401 | exitcode = subprocess.call([abs_program, "-c", "pass"]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1402 | self.assertEqual(exitcode, 0) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1403 | |
| 1404 | # bytes program, unicode PATH |
| 1405 | env = os.environ.copy() |
| 1406 | env["PATH"] = path |
| 1407 | exitcode = subprocess.call([program, "-c", "pass"], env=env) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1408 | self.assertEqual(exitcode, 0) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1409 | |
| 1410 | # bytes program, bytes PATH |
| 1411 | envb = os.environb.copy() |
| 1412 | envb[b"PATH"] = os.fsencode(path) |
| 1413 | exitcode = subprocess.call([program, "-c", "pass"], env=envb) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1414 | self.assertEqual(exitcode, 0) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1415 | |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 1416 | def test_pipe_cloexec(self): |
| 1417 | sleeper = support.findfile("input_reader.py", subdir="subprocessdata") |
| 1418 | fd_status = support.findfile("fd_status.py", subdir="subprocessdata") |
| 1419 | |
| 1420 | p1 = subprocess.Popen([sys.executable, sleeper], |
| 1421 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 1422 | stderr=subprocess.PIPE, close_fds=False) |
| 1423 | |
| 1424 | self.addCleanup(p1.communicate, b'') |
| 1425 | |
| 1426 | p2 = subprocess.Popen([sys.executable, fd_status], |
| 1427 | stdout=subprocess.PIPE, close_fds=False) |
| 1428 | |
| 1429 | output, error = p2.communicate() |
| 1430 | result_fds = set(map(int, output.split(b','))) |
| 1431 | unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(), |
| 1432 | p1.stderr.fileno()]) |
| 1433 | |
| 1434 | self.assertFalse(result_fds & unwanted_fds, |
| 1435 | "Expected no fds from %r to be open in child, " |
| 1436 | "found %r" % |
| 1437 | (unwanted_fds, result_fds & unwanted_fds)) |
| 1438 | |
| 1439 | def test_pipe_cloexec_real_tools(self): |
| 1440 | qcat = support.findfile("qcat.py", subdir="subprocessdata") |
| 1441 | qgrep = support.findfile("qgrep.py", subdir="subprocessdata") |
| 1442 | |
| 1443 | subdata = b'zxcvbn' |
| 1444 | data = subdata * 4 + b'\n' |
| 1445 | |
| 1446 | p1 = subprocess.Popen([sys.executable, qcat], |
| 1447 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 1448 | close_fds=False) |
| 1449 | |
| 1450 | p2 = subprocess.Popen([sys.executable, qgrep, subdata], |
| 1451 | stdin=p1.stdout, stdout=subprocess.PIPE, |
| 1452 | close_fds=False) |
| 1453 | |
| 1454 | self.addCleanup(p1.wait) |
| 1455 | self.addCleanup(p2.wait) |
Gregory P. Smith | 886455c | 2012-01-21 22:05:10 -0800 | [diff] [blame] | 1456 | def kill_p1(): |
| 1457 | try: |
| 1458 | p1.terminate() |
| 1459 | except ProcessLookupError: |
| 1460 | pass |
| 1461 | def kill_p2(): |
| 1462 | try: |
| 1463 | p2.terminate() |
| 1464 | except ProcessLookupError: |
| 1465 | pass |
| 1466 | self.addCleanup(kill_p1) |
| 1467 | self.addCleanup(kill_p2) |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 1468 | |
| 1469 | p1.stdin.write(data) |
| 1470 | p1.stdin.close() |
| 1471 | |
| 1472 | readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10) |
| 1473 | |
| 1474 | self.assertTrue(readfiles, "The child hung") |
| 1475 | self.assertEqual(p2.stdout.read(), data) |
| 1476 | |
Victor Stinner | faa8c13 | 2011-01-03 16:36:00 +0000 | [diff] [blame] | 1477 | p1.stdout.close() |
| 1478 | p2.stdout.close() |
| 1479 | |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 1480 | def test_close_fds(self): |
| 1481 | fd_status = support.findfile("fd_status.py", subdir="subprocessdata") |
| 1482 | |
| 1483 | fds = os.pipe() |
| 1484 | self.addCleanup(os.close, fds[0]) |
| 1485 | self.addCleanup(os.close, fds[1]) |
| 1486 | |
| 1487 | open_fds = set(fds) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 1488 | # add a bunch more fds |
| 1489 | for _ in range(9): |
| 1490 | fd = os.open("/dev/null", os.O_RDONLY) |
| 1491 | self.addCleanup(os.close, fd) |
| 1492 | open_fds.add(fd) |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 1493 | |
| 1494 | p = subprocess.Popen([sys.executable, fd_status], |
| 1495 | stdout=subprocess.PIPE, close_fds=False) |
| 1496 | output, ignored = p.communicate() |
| 1497 | remaining_fds = set(map(int, output.split(b','))) |
| 1498 | |
| 1499 | self.assertEqual(remaining_fds & open_fds, open_fds, |
| 1500 | "Some fds were closed") |
| 1501 | |
| 1502 | p = subprocess.Popen([sys.executable, fd_status], |
| 1503 | stdout=subprocess.PIPE, close_fds=True) |
| 1504 | output, ignored = p.communicate() |
| 1505 | remaining_fds = set(map(int, output.split(b','))) |
| 1506 | |
| 1507 | self.assertFalse(remaining_fds & open_fds, |
| 1508 | "Some fds were left open") |
| 1509 | self.assertIn(1, remaining_fds, "Subprocess failed") |
| 1510 | |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 1511 | # Keep some of the fd's we opened open in the subprocess. |
| 1512 | # This tests _posixsubprocess.c's proper handling of fds_to_keep. |
| 1513 | fds_to_keep = set(open_fds.pop() for _ in range(8)) |
| 1514 | p = subprocess.Popen([sys.executable, fd_status], |
| 1515 | stdout=subprocess.PIPE, close_fds=True, |
| 1516 | pass_fds=()) |
| 1517 | output, ignored = p.communicate() |
| 1518 | remaining_fds = set(map(int, output.split(b','))) |
| 1519 | |
| 1520 | self.assertFalse(remaining_fds & fds_to_keep & open_fds, |
| 1521 | "Some fds not in pass_fds were left open") |
| 1522 | self.assertIn(1, remaining_fds, "Subprocess failed") |
| 1523 | |
Victor Stinner | 88701e2 | 2011-06-01 13:13:04 +0200 | [diff] [blame] | 1524 | # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file |
| 1525 | # descriptor of a pipe closed in the parent process is valid in the |
| 1526 | # child process according to fstat(), but the mode of the file |
| 1527 | # descriptor is invalid, and read or write raise an error. |
| 1528 | @support.requires_mac_ver(10, 5) |
Gregory P. Smith | 8edd99d | 2010-12-14 13:43:30 +0000 | [diff] [blame] | 1529 | def test_pass_fds(self): |
| 1530 | fd_status = support.findfile("fd_status.py", subdir="subprocessdata") |
| 1531 | |
| 1532 | open_fds = set() |
| 1533 | |
| 1534 | for x in range(5): |
| 1535 | fds = os.pipe() |
| 1536 | self.addCleanup(os.close, fds[0]) |
| 1537 | self.addCleanup(os.close, fds[1]) |
| 1538 | open_fds.update(fds) |
| 1539 | |
| 1540 | for fd in open_fds: |
| 1541 | p = subprocess.Popen([sys.executable, fd_status], |
| 1542 | stdout=subprocess.PIPE, close_fds=True, |
| 1543 | pass_fds=(fd, )) |
| 1544 | output, ignored = p.communicate() |
| 1545 | |
| 1546 | remaining_fds = set(map(int, output.split(b','))) |
| 1547 | to_be_closed = open_fds - {fd} |
| 1548 | |
| 1549 | self.assertIn(fd, remaining_fds, "fd to be passed not passed") |
| 1550 | self.assertFalse(remaining_fds & to_be_closed, |
| 1551 | "fd to be closed passed") |
| 1552 | |
| 1553 | # pass_fds overrides close_fds with a warning. |
| 1554 | with self.assertWarns(RuntimeWarning) as context: |
| 1555 | self.assertFalse(subprocess.call( |
| 1556 | [sys.executable, "-c", "import sys; sys.exit(0)"], |
| 1557 | close_fds=False, pass_fds=(fd, ))) |
| 1558 | self.assertIn('overriding close_fds', str(context.warning)) |
| 1559 | |
Gregory P. Smith | e14e9c2 | 2011-03-15 14:55:17 -0400 | [diff] [blame] | 1560 | def test_stdout_stdin_are_single_inout_fd(self): |
| 1561 | with io.open(os.devnull, "r+") as inout: |
| 1562 | p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], |
| 1563 | stdout=inout, stdin=inout) |
| 1564 | p.wait() |
| 1565 | |
| 1566 | def test_stdout_stderr_are_single_inout_fd(self): |
| 1567 | with io.open(os.devnull, "r+") as inout: |
| 1568 | p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], |
| 1569 | stdout=inout, stderr=inout) |
| 1570 | p.wait() |
| 1571 | |
| 1572 | def test_stderr_stdin_are_single_inout_fd(self): |
| 1573 | with io.open(os.devnull, "r+") as inout: |
| 1574 | p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], |
| 1575 | stderr=inout, stdin=inout) |
| 1576 | p.wait() |
| 1577 | |
Gregory P. Smith | e85db2b | 2010-12-14 14:38:00 +0000 | [diff] [blame] | 1578 | def test_wait_when_sigchild_ignored(self): |
| 1579 | # NOTE: sigchild_ignore.py may not be an effective test on all OSes. |
| 1580 | sigchild_ignore = support.findfile("sigchild_ignore.py", |
| 1581 | subdir="subprocessdata") |
| 1582 | p = subprocess.Popen([sys.executable, sigchild_ignore], |
| 1583 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 1584 | stdout, stderr = p.communicate() |
| 1585 | self.assertEqual(0, p.returncode, "sigchild_ignore.py exited" |
Gregory P. Smith | a80f4fb | 2010-12-14 15:23:02 +0000 | [diff] [blame] | 1586 | " non-zero with this error:\n%s" % |
| 1587 | stderr.decode('utf8')) |
Gregory P. Smith | e85db2b | 2010-12-14 14:38:00 +0000 | [diff] [blame] | 1588 | |
Antoine Pitrou | 7b98d02 | 2011-03-19 17:04:13 +0100 | [diff] [blame] | 1589 | def test_select_unbuffered(self): |
| 1590 | # Issue #11459: bufsize=0 should really set the pipes as |
| 1591 | # unbuffered (and therefore let select() work properly). |
| 1592 | select = support.import_module("select") |
| 1593 | p = subprocess.Popen([sys.executable, "-c", |
| 1594 | 'import sys;' |
| 1595 | 'sys.stdout.write("apple")'], |
| 1596 | stdout=subprocess.PIPE, |
| 1597 | bufsize=0) |
| 1598 | f = p.stdout |
Ross Lagerwall | 17ace7a | 2011-03-26 21:21:46 +0200 | [diff] [blame] | 1599 | self.addCleanup(f.close) |
Antoine Pitrou | 7b98d02 | 2011-03-19 17:04:13 +0100 | [diff] [blame] | 1600 | try: |
| 1601 | self.assertEqual(f.read(4), b"appl") |
| 1602 | self.assertIn(f, select.select([f], [], [], 0.0)[0]) |
| 1603 | finally: |
| 1604 | p.wait() |
| 1605 | |
Charles-François Natali | 134a8ba | 2011-08-18 18:49:39 +0200 | [diff] [blame] | 1606 | def test_zombie_fast_process_del(self): |
| 1607 | # Issue #12650: on Unix, if Popen.__del__() was called before the |
| 1608 | # process exited, it wouldn't be added to subprocess._active, and would |
| 1609 | # remain a zombie. |
| 1610 | # spawn a Popen, and delete its reference before it exits |
| 1611 | p = subprocess.Popen([sys.executable, "-c", |
| 1612 | 'import sys, time;' |
| 1613 | 'time.sleep(0.2)'], |
| 1614 | stdout=subprocess.PIPE, |
| 1615 | stderr=subprocess.PIPE) |
Nadeem Vawda | 0d7cda3 | 2011-08-19 05:12:01 +0200 | [diff] [blame] | 1616 | self.addCleanup(p.stdout.close) |
| 1617 | self.addCleanup(p.stderr.close) |
Charles-François Natali | 134a8ba | 2011-08-18 18:49:39 +0200 | [diff] [blame] | 1618 | ident = id(p) |
| 1619 | pid = p.pid |
| 1620 | del p |
| 1621 | # check that p is in the active processes list |
| 1622 | self.assertIn(ident, [id(o) for o in subprocess._active]) |
| 1623 | |
Charles-François Natali | 134a8ba | 2011-08-18 18:49:39 +0200 | [diff] [blame] | 1624 | def test_leak_fast_process_del_killed(self): |
| 1625 | # Issue #12650: on Unix, if Popen.__del__() was called before the |
| 1626 | # process exited, and the process got killed by a signal, it would never |
| 1627 | # be removed from subprocess._active, which triggered a FD and memory |
| 1628 | # leak. |
| 1629 | # spawn a Popen, delete its reference and kill it |
| 1630 | p = subprocess.Popen([sys.executable, "-c", |
| 1631 | 'import time;' |
| 1632 | 'time.sleep(3)'], |
| 1633 | stdout=subprocess.PIPE, |
| 1634 | stderr=subprocess.PIPE) |
Nadeem Vawda | 0d7cda3 | 2011-08-19 05:12:01 +0200 | [diff] [blame] | 1635 | self.addCleanup(p.stdout.close) |
| 1636 | self.addCleanup(p.stderr.close) |
Charles-François Natali | 134a8ba | 2011-08-18 18:49:39 +0200 | [diff] [blame] | 1637 | ident = id(p) |
| 1638 | pid = p.pid |
| 1639 | del p |
| 1640 | os.kill(pid, signal.SIGKILL) |
| 1641 | # check that p is in the active processes list |
| 1642 | self.assertIn(ident, [id(o) for o in subprocess._active]) |
| 1643 | |
| 1644 | # let some time for the process to exit, and create a new Popen: this |
| 1645 | # should trigger the wait() of p |
| 1646 | time.sleep(0.2) |
| 1647 | with self.assertRaises(EnvironmentError) as c: |
| 1648 | with subprocess.Popen(['nonexisting_i_hope'], |
| 1649 | stdout=subprocess.PIPE, |
| 1650 | stderr=subprocess.PIPE) as proc: |
| 1651 | pass |
| 1652 | # p should have been wait()ed on, and removed from the _active list |
| 1653 | self.assertRaises(OSError, os.waitpid, pid, 0) |
| 1654 | self.assertNotIn(ident, [id(o) for o in subprocess._active]) |
| 1655 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1656 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 1657 | @unittest.skipUnless(mswindows, "Windows specific tests") |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 1658 | class Win32ProcessTestCase(BaseTestCase): |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 1659 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1660 | def test_startupinfo(self): |
| 1661 | # startupinfo argument |
| 1662 | # We uses hardcoded constants, because we do not want to |
| 1663 | # depend on win32all. |
| 1664 | STARTF_USESHOWWINDOW = 1 |
| 1665 | SW_MAXIMIZE = 3 |
| 1666 | startupinfo = subprocess.STARTUPINFO() |
| 1667 | startupinfo.dwFlags = STARTF_USESHOWWINDOW |
| 1668 | startupinfo.wShowWindow = SW_MAXIMIZE |
| 1669 | # Since Python is a console process, it won't be affected |
| 1670 | # by wShowWindow, but the argument should be silently |
| 1671 | # ignored |
| 1672 | subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1673 | startupinfo=startupinfo) |
| 1674 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1675 | def test_creationflags(self): |
| 1676 | # creationflags argument |
| 1677 | CREATE_NEW_CONSOLE = 16 |
| 1678 | sys.stderr.write(" a DOS box should flash briefly ...\n") |
| 1679 | subprocess.call(sys.executable + |
| 1680 | ' -c "import time; time.sleep(0.25)"', |
| 1681 | creationflags=CREATE_NEW_CONSOLE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1682 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1683 | def test_invalid_args(self): |
| 1684 | # invalid arguments should raise ValueError |
| 1685 | self.assertRaises(ValueError, subprocess.call, |
| 1686 | [sys.executable, "-c", |
| 1687 | "import sys; sys.exit(47)"], |
| 1688 | preexec_fn=lambda: 1) |
| 1689 | self.assertRaises(ValueError, subprocess.call, |
| 1690 | [sys.executable, "-c", |
| 1691 | "import sys; sys.exit(47)"], |
| 1692 | stdout=subprocess.PIPE, |
| 1693 | close_fds=True) |
| 1694 | |
| 1695 | def test_close_fds(self): |
| 1696 | # close file descriptors |
| 1697 | rc = subprocess.call([sys.executable, "-c", |
| 1698 | "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1699 | close_fds=True) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1700 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1701 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1702 | def test_shell_sequence(self): |
| 1703 | # Run command through the shell (sequence) |
| 1704 | newenv = os.environ.copy() |
| 1705 | newenv["FRUIT"] = "physalis" |
| 1706 | p = subprocess.Popen(["set"], shell=1, |
| 1707 | stdout=subprocess.PIPE, |
| 1708 | env=newenv) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 1709 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1710 | self.assertIn(b"physalis", p.stdout.read()) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1711 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1712 | def test_shell_string(self): |
| 1713 | # Run command through the shell (string) |
| 1714 | newenv = os.environ.copy() |
| 1715 | newenv["FRUIT"] = "physalis" |
| 1716 | p = subprocess.Popen("set", shell=1, |
| 1717 | stdout=subprocess.PIPE, |
| 1718 | env=newenv) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 1719 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1720 | self.assertIn(b"physalis", p.stdout.read()) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1721 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1722 | def test_call_string(self): |
| 1723 | # call() function with string argument on Windows |
| 1724 | rc = subprocess.call(sys.executable + |
| 1725 | ' -c "import sys; sys.exit(47)"') |
| 1726 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1727 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1728 | def _kill_process(self, method, *args): |
| 1729 | # Some win32 buildbot raises EOFError if stdin is inherited |
Antoine Pitrou | a4024e2 | 2010-09-24 18:57:01 +0000 | [diff] [blame] | 1730 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 1731 | import sys, time |
| 1732 | sys.stdout.write('x\\n') |
| 1733 | sys.stdout.flush() |
| 1734 | time.sleep(30) |
| 1735 | """], |
| 1736 | stdin=subprocess.PIPE, |
| 1737 | stdout=subprocess.PIPE, |
| 1738 | stderr=subprocess.PIPE) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 1739 | self.addCleanup(p.stdout.close) |
| 1740 | self.addCleanup(p.stderr.close) |
| 1741 | self.addCleanup(p.stdin.close) |
Antoine Pitrou | a4024e2 | 2010-09-24 18:57:01 +0000 | [diff] [blame] | 1742 | # Wait for the interpreter to be completely initialized before |
| 1743 | # sending any signal. |
| 1744 | p.stdout.read(1) |
| 1745 | getattr(p, method)(*args) |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 1746 | _, stderr = p.communicate() |
| 1747 | self.assertStderrEqual(stderr, b'') |
Antoine Pitrou | a4024e2 | 2010-09-24 18:57:01 +0000 | [diff] [blame] | 1748 | returncode = p.wait() |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1749 | self.assertNotEqual(returncode, 0) |
| 1750 | |
Antoine Pitrou | 1f9a835 | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 1751 | def _kill_dead_process(self, method, *args): |
| 1752 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 1753 | import sys, time |
| 1754 | sys.stdout.write('x\\n') |
| 1755 | sys.stdout.flush() |
| 1756 | sys.exit(42) |
| 1757 | """], |
| 1758 | stdin=subprocess.PIPE, |
| 1759 | stdout=subprocess.PIPE, |
| 1760 | stderr=subprocess.PIPE) |
| 1761 | self.addCleanup(p.stdout.close) |
| 1762 | self.addCleanup(p.stderr.close) |
| 1763 | self.addCleanup(p.stdin.close) |
| 1764 | # Wait for the interpreter to be completely initialized before |
| 1765 | # sending any signal. |
| 1766 | p.stdout.read(1) |
| 1767 | # The process should end after this |
| 1768 | time.sleep(1) |
| 1769 | # This shouldn't raise even though the child is now dead |
| 1770 | getattr(p, method)(*args) |
| 1771 | _, stderr = p.communicate() |
| 1772 | self.assertStderrEqual(stderr, b'') |
| 1773 | rc = p.wait() |
| 1774 | self.assertEqual(rc, 42) |
| 1775 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1776 | def test_send_signal(self): |
| 1777 | self._kill_process('send_signal', signal.SIGTERM) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1778 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1779 | def test_kill(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1780 | self._kill_process('kill') |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1781 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1782 | def test_terminate(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1783 | self._kill_process('terminate') |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1784 | |
Antoine Pitrou | 1f9a835 | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 1785 | def test_send_signal_dead(self): |
| 1786 | self._kill_dead_process('send_signal', signal.SIGTERM) |
| 1787 | |
| 1788 | def test_kill_dead(self): |
| 1789 | self._kill_dead_process('kill') |
| 1790 | |
| 1791 | def test_terminate_dead(self): |
| 1792 | self._kill_dead_process('terminate') |
| 1793 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1794 | |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 1795 | # The module says: |
| 1796 | # "NB This only works (and is only relevant) for UNIX." |
| 1797 | # |
| 1798 | # Actually, getoutput should work on any platform with an os.popen, but |
| 1799 | # I'll take the comment as given, and skip this suite. |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 1800 | @unittest.skipUnless(os.name == 'posix', "only relevant for UNIX") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1801 | class CommandTests(unittest.TestCase): |
| 1802 | def test_getoutput(self): |
| 1803 | self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy') |
| 1804 | self.assertEqual(subprocess.getstatusoutput('echo xyzzy'), |
| 1805 | (0, 'xyzzy')) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 1806 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1807 | # we use mkdtemp in the next line to create an empty directory |
| 1808 | # under our exclusive control; from that, we can invent a pathname |
| 1809 | # that we _know_ won't exist. This is guaranteed to fail. |
| 1810 | dir = None |
| 1811 | try: |
| 1812 | dir = tempfile.mkdtemp() |
| 1813 | name = os.path.join(dir, "foo") |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 1814 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1815 | status, output = subprocess.getstatusoutput('cat ' + name) |
| 1816 | self.assertNotEqual(status, 0) |
| 1817 | finally: |
| 1818 | if dir is not None: |
| 1819 | os.rmdir(dir) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 1820 | |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 1821 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1822 | @unittest.skipUnless(getattr(subprocess, '_has_poll', False), |
| 1823 | "poll system call not supported") |
| 1824 | class ProcessTestCaseNoPoll(ProcessTestCase): |
| 1825 | def setUp(self): |
| 1826 | subprocess._has_poll = False |
| 1827 | ProcessTestCase.setUp(self) |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 1828 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1829 | def tearDown(self): |
| 1830 | subprocess._has_poll = True |
| 1831 | ProcessTestCase.tearDown(self) |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 1832 | |
| 1833 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1834 | @unittest.skipUnless(getattr(subprocess, '_posixsubprocess', False), |
| 1835 | "_posixsubprocess extension module not found.") |
| 1836 | class ProcessTestCasePOSIXPurePython(ProcessTestCase, POSIXProcessTestCase): |
Gregory P. Smith | 7439e7b | 2011-05-28 09:06:02 -0700 | [diff] [blame] | 1837 | @classmethod |
| 1838 | def setUpClass(cls): |
| 1839 | global subprocess |
| 1840 | assert subprocess._posixsubprocess |
| 1841 | # Reimport subprocess while forcing _posixsubprocess to not exist. |
| 1842 | with support.check_warnings(('.*_posixsubprocess .* not being used.*', |
| 1843 | RuntimeWarning)): |
| 1844 | subprocess = support.import_fresh_module( |
| 1845 | 'subprocess', blocked=['_posixsubprocess']) |
| 1846 | assert not subprocess._posixsubprocess |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1847 | |
Gregory P. Smith | 7439e7b | 2011-05-28 09:06:02 -0700 | [diff] [blame] | 1848 | @classmethod |
| 1849 | def tearDownClass(cls): |
| 1850 | global subprocess |
| 1851 | # Reimport subprocess as it should be, restoring order to the universe. |
| 1852 | subprocess = support.import_fresh_module('subprocess') |
| 1853 | assert subprocess._posixsubprocess |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1854 | |
| 1855 | |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +0000 | [diff] [blame] | 1856 | class HelperFunctionTests(unittest.TestCase): |
Gregory P. Smith | af6d3b8 | 2010-03-01 02:56:44 +0000 | [diff] [blame] | 1857 | @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows") |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +0000 | [diff] [blame] | 1858 | def test_eintr_retry_call(self): |
| 1859 | record_calls = [] |
| 1860 | def fake_os_func(*args): |
| 1861 | record_calls.append(args) |
| 1862 | if len(record_calls) == 2: |
| 1863 | raise OSError(errno.EINTR, "fake interrupted system call") |
| 1864 | return tuple(reversed(args)) |
| 1865 | |
| 1866 | self.assertEqual((999, 256), |
| 1867 | subprocess._eintr_retry_call(fake_os_func, 256, 999)) |
| 1868 | self.assertEqual([(256, 999)], record_calls) |
| 1869 | # This time there will be an EINTR so it will loop once. |
| 1870 | self.assertEqual((666,), |
| 1871 | subprocess._eintr_retry_call(fake_os_func, 666)) |
| 1872 | self.assertEqual([(256, 999), (666,), (666,)], record_calls) |
| 1873 | |
| 1874 | |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 1875 | @unittest.skipUnless(mswindows, "Windows-specific tests") |
| 1876 | class CommandsWithSpaces (BaseTestCase): |
| 1877 | |
| 1878 | def setUp(self): |
| 1879 | super().setUp() |
| 1880 | f, fname = mkstemp(".py", "te st") |
| 1881 | self.fname = fname.lower () |
| 1882 | os.write(f, b"import sys;" |
| 1883 | b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))" |
| 1884 | ) |
| 1885 | os.close(f) |
| 1886 | |
| 1887 | def tearDown(self): |
| 1888 | os.remove(self.fname) |
| 1889 | super().tearDown() |
| 1890 | |
| 1891 | def with_spaces(self, *args, **kwargs): |
| 1892 | kwargs['stdout'] = subprocess.PIPE |
| 1893 | p = subprocess.Popen(*args, **kwargs) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 1894 | self.addCleanup(p.stdout.close) |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 1895 | self.assertEqual( |
| 1896 | p.stdout.read ().decode("mbcs"), |
| 1897 | "2 [%r, 'ab cd']" % self.fname |
| 1898 | ) |
| 1899 | |
| 1900 | def test_shell_string_with_spaces(self): |
| 1901 | # call() function with string argument with spaces on Windows |
Brian Curtin | d835cf1 | 2010-08-13 20:42:57 +0000 | [diff] [blame] | 1902 | self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, |
| 1903 | "ab cd"), shell=1) |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 1904 | |
| 1905 | def test_shell_sequence_with_spaces(self): |
| 1906 | # call() function with sequence argument with spaces on Windows |
Brian Curtin | d835cf1 | 2010-08-13 20:42:57 +0000 | [diff] [blame] | 1907 | self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1) |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 1908 | |
| 1909 | def test_noshell_string_with_spaces(self): |
| 1910 | # call() function with string argument with spaces on Windows |
| 1911 | self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, |
| 1912 | "ab cd")) |
| 1913 | |
| 1914 | def test_noshell_sequence_with_spaces(self): |
| 1915 | # call() function with sequence argument with spaces on Windows |
| 1916 | self.with_spaces([sys.executable, self.fname, "ab cd"]) |
| 1917 | |
Brian Curtin | 79cdb66 | 2010-12-03 02:46:02 +0000 | [diff] [blame] | 1918 | |
Georg Brandl | a86b262 | 2012-02-20 21:34:57 +0100 | [diff] [blame] | 1919 | class ContextManagerTests(BaseTestCase): |
Brian Curtin | 79cdb66 | 2010-12-03 02:46:02 +0000 | [diff] [blame] | 1920 | |
| 1921 | def test_pipe(self): |
| 1922 | with subprocess.Popen([sys.executable, "-c", |
| 1923 | "import sys;" |
| 1924 | "sys.stdout.write('stdout');" |
| 1925 | "sys.stderr.write('stderr');"], |
| 1926 | stdout=subprocess.PIPE, |
| 1927 | stderr=subprocess.PIPE) as proc: |
| 1928 | self.assertEqual(proc.stdout.read(), b"stdout") |
| 1929 | self.assertStderrEqual(proc.stderr.read(), b"stderr") |
| 1930 | |
| 1931 | self.assertTrue(proc.stdout.closed) |
| 1932 | self.assertTrue(proc.stderr.closed) |
| 1933 | |
| 1934 | def test_returncode(self): |
| 1935 | with subprocess.Popen([sys.executable, "-c", |
| 1936 | "import sys; sys.exit(100)"]) as proc: |
Gregory P. Smith | c9557af | 2011-05-11 22:18:23 -0700 | [diff] [blame] | 1937 | pass |
| 1938 | # __exit__ calls wait(), so the returncode should be set |
Brian Curtin | 79cdb66 | 2010-12-03 02:46:02 +0000 | [diff] [blame] | 1939 | self.assertEqual(proc.returncode, 100) |
| 1940 | |
| 1941 | def test_communicate_stdin(self): |
| 1942 | with subprocess.Popen([sys.executable, "-c", |
| 1943 | "import sys;" |
| 1944 | "sys.exit(sys.stdin.read() == 'context')"], |
| 1945 | stdin=subprocess.PIPE) as proc: |
| 1946 | proc.communicate(b"context") |
| 1947 | self.assertEqual(proc.returncode, 1) |
| 1948 | |
| 1949 | def test_invalid_args(self): |
| 1950 | with self.assertRaises(EnvironmentError) as c: |
| 1951 | with subprocess.Popen(['nonexisting_i_hope'], |
| 1952 | stdout=subprocess.PIPE, |
| 1953 | stderr=subprocess.PIPE) as proc: |
| 1954 | pass |
| 1955 | |
| 1956 | if c.exception.errno != errno.ENOENT: # ignore "no such file" |
| 1957 | raise c.exception |
| 1958 | |
| 1959 | |
Gregory P. Smith | 961e0e8 | 2011-03-15 15:43:39 -0400 | [diff] [blame] | 1960 | def test_main(): |
| 1961 | unit_tests = (ProcessTestCase, |
| 1962 | POSIXProcessTestCase, |
| 1963 | Win32ProcessTestCase, |
| 1964 | ProcessTestCasePOSIXPurePython, |
| 1965 | CommandTests, |
| 1966 | ProcessTestCaseNoPoll, |
| 1967 | HelperFunctionTests, |
| 1968 | CommandsWithSpaces, |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 1969 | ContextManagerTests, |
| 1970 | ) |
Gregory P. Smith | 961e0e8 | 2011-03-15 15:43:39 -0400 | [diff] [blame] | 1971 | |
| 1972 | support.run_unittest(*unit_tests) |
| 1973 | support.reap_children() |
| 1974 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1975 | if __name__ == "__main__": |
Gregory P. Smith | e14e9c2 | 2011-03-15 14:55:17 -0400 | [diff] [blame] | 1976 | unittest.main() |