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