Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
| 3 | import subprocess |
| 4 | import sys |
| 5 | import signal |
| 6 | import os |
Gregory P. Smith | cce211f | 2010-03-01 00:05:08 +0000 | [diff] [blame] | 7 | import errno |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 8 | import tempfile |
| 9 | import time |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 10 | import re |
Ezio Melotti | 8f6a287 | 2010-02-10 21:40:33 +0000 | [diff] [blame] | 11 | import sysconfig |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 12 | |
Benjamin Peterson | 8b59c23 | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 13 | try: |
| 14 | import resource |
| 15 | except ImportError: |
| 16 | resource = None |
Antoine Pitrou | 33fc744 | 2013-08-30 23:38:13 +0200 | [diff] [blame] | 17 | try: |
| 18 | import threading |
| 19 | except ImportError: |
| 20 | threading = None |
Benjamin Peterson | 8b59c23 | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 21 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 22 | mswindows = (sys.platform == "win32") |
| 23 | |
| 24 | # |
| 25 | # Depends on the following external programs: Python |
| 26 | # |
| 27 | |
| 28 | if mswindows: |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 29 | SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), ' |
| 30 | 'os.O_BINARY);') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 31 | else: |
| 32 | SETBINARY = '' |
| 33 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 34 | |
Florent Xicluna | fc4d6d7 | 2010-03-23 14:36:45 +0000 | [diff] [blame] | 35 | class BaseTestCase(unittest.TestCase): |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 36 | def setUp(self): |
Tim Peters | 38ff36c | 2006-06-30 06:18:39 +0000 | [diff] [blame] | 37 | # Try to minimize the number of children we have so this test |
| 38 | # doesn't crash on some buildbots (Alphas in particular). |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 39 | test_support.reap_children() |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 40 | |
Florent Xicluna | ab5e17f | 2010-03-04 21:31:58 +0000 | [diff] [blame] | 41 | def tearDown(self): |
| 42 | for inst in subprocess._active: |
| 43 | inst.wait() |
| 44 | subprocess._cleanup() |
| 45 | self.assertFalse(subprocess._active, "subprocess._active not empty") |
| 46 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 47 | def assertStderrEqual(self, stderr, expected, msg=None): |
| 48 | # In a debug build, stuff like "[6580 refs]" is printed to stderr at |
| 49 | # shutdown time. That frustrates tests trying to check stderr produced |
| 50 | # from a spawned Python process. |
| 51 | actual = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr) |
| 52 | self.assertEqual(actual, expected, msg) |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 53 | |
Florent Xicluna | fc4d6d7 | 2010-03-23 14:36:45 +0000 | [diff] [blame] | 54 | |
Gregory P. Smith | 9d3b6e9 | 2012-11-10 22:49:03 -0800 | [diff] [blame] | 55 | class PopenTestException(Exception): |
| 56 | pass |
| 57 | |
| 58 | |
| 59 | class PopenExecuteChildRaises(subprocess.Popen): |
| 60 | """Popen subclass for testing cleanup of subprocess.PIPE filehandles when |
| 61 | _execute_child fails. |
| 62 | """ |
| 63 | def _execute_child(self, *args, **kwargs): |
| 64 | raise PopenTestException("Forced Exception for Test") |
| 65 | |
| 66 | |
Florent Xicluna | fc4d6d7 | 2010-03-23 14:36:45 +0000 | [diff] [blame] | 67 | class ProcessTestCase(BaseTestCase): |
| 68 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 69 | def test_call_seq(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 70 | # call() function with sequence argument |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 71 | rc = subprocess.call([sys.executable, "-c", |
| 72 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 73 | self.assertEqual(rc, 47) |
| 74 | |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 75 | def test_check_call_zero(self): |
| 76 | # check_call() function with zero return code |
| 77 | rc = subprocess.check_call([sys.executable, "-c", |
| 78 | "import sys; sys.exit(0)"]) |
| 79 | self.assertEqual(rc, 0) |
| 80 | |
| 81 | def test_check_call_nonzero(self): |
| 82 | # check_call() function with non-zero return code |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 83 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 84 | subprocess.check_call([sys.executable, "-c", |
| 85 | "import sys; sys.exit(47)"]) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 86 | self.assertEqual(c.exception.returncode, 47) |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 87 | |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 88 | def test_check_output(self): |
| 89 | # check_output() function with zero return code |
| 90 | output = subprocess.check_output( |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 91 | [sys.executable, "-c", "print 'BDFL'"]) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 92 | self.assertIn('BDFL', output) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 93 | |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 94 | def test_check_output_nonzero(self): |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 95 | # check_call() function with non-zero return code |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 96 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 97 | subprocess.check_output( |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 98 | [sys.executable, "-c", "import sys; sys.exit(5)"]) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 99 | self.assertEqual(c.exception.returncode, 5) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 100 | |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 101 | def test_check_output_stderr(self): |
| 102 | # check_output() function stderr redirected to stdout |
| 103 | output = subprocess.check_output( |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 104 | [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"], |
| 105 | stderr=subprocess.STDOUT) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 106 | self.assertIn('BDFL', output) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 107 | |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 108 | def test_check_output_stdout_arg(self): |
| 109 | # check_output() function stderr redirected to stdout |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 110 | with self.assertRaises(ValueError) as c: |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 111 | output = subprocess.check_output( |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 112 | [sys.executable, "-c", "print 'will not be run'"], |
| 113 | stdout=sys.stdout) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 114 | self.fail("Expected ValueError when stdout arg supplied.") |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 115 | self.assertIn('stdout', c.exception.args[0]) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 116 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 117 | def test_call_kwargs(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 118 | # call() function with keyword args |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 119 | newenv = os.environ.copy() |
| 120 | newenv["FRUIT"] = "banana" |
| 121 | rc = subprocess.call([sys.executable, "-c", |
Florent Xicluna | bab22a7 | 2010-03-04 19:40:48 +0000 | [diff] [blame] | 122 | 'import sys, os;' |
| 123 | 'sys.exit(os.getenv("FRUIT")=="banana")'], |
| 124 | env=newenv) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 125 | self.assertEqual(rc, 1) |
| 126 | |
Victor Stinner | 776e69b | 2011-06-01 01:03:00 +0200 | [diff] [blame] | 127 | def test_invalid_args(self): |
| 128 | # Popen() called with invalid arguments should raise TypeError |
| 129 | # but Popen.__del__ should not complain (issue #12085) |
Victor Stinner | e9b185f | 2011-06-01 01:57:48 +0200 | [diff] [blame] | 130 | with test_support.captured_stderr() as s: |
Victor Stinner | 776e69b | 2011-06-01 01:03:00 +0200 | [diff] [blame] | 131 | self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1) |
| 132 | argcount = subprocess.Popen.__init__.__code__.co_argcount |
| 133 | too_many_args = [0] * (argcount + 1) |
| 134 | self.assertRaises(TypeError, subprocess.Popen, *too_many_args) |
| 135 | self.assertEqual(s.getvalue(), '') |
| 136 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 137 | def test_stdin_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 138 | # .stdin is None when not redirected |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 139 | p = subprocess.Popen([sys.executable, "-c", 'print "banana"'], |
| 140 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 141 | self.addCleanup(p.stdout.close) |
| 142 | self.addCleanup(p.stderr.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 143 | p.wait() |
| 144 | self.assertEqual(p.stdin, None) |
| 145 | |
| 146 | def test_stdout_none(self): |
Ezio Melotti | efaad09 | 2013-03-11 00:34:33 +0200 | [diff] [blame] | 147 | # .stdout is None when not redirected, and the child's stdout will |
| 148 | # be inherited from the parent. In order to test this we run a |
| 149 | # subprocess in a subprocess: |
| 150 | # this_test |
| 151 | # \-- subprocess created by this test (parent) |
| 152 | # \-- subprocess created by the parent subprocess (child) |
| 153 | # The parent doesn't specify stdout, so the child will use the |
| 154 | # parent's stdout. This test checks that the message printed by the |
| 155 | # child goes to the parent stdout. The parent also checks that the |
| 156 | # child's stdout is None. See #11963. |
| 157 | code = ('import sys; from subprocess import Popen, PIPE;' |
| 158 | 'p = Popen([sys.executable, "-c", "print \'test_stdout_none\'"],' |
| 159 | ' stdin=PIPE, stderr=PIPE);' |
| 160 | 'p.wait(); assert p.stdout is None;') |
| 161 | p = subprocess.Popen([sys.executable, "-c", code], |
| 162 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 163 | self.addCleanup(p.stdout.close) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 164 | self.addCleanup(p.stderr.close) |
Ezio Melotti | efaad09 | 2013-03-11 00:34:33 +0200 | [diff] [blame] | 165 | out, err = p.communicate() |
| 166 | self.assertEqual(p.returncode, 0, err) |
| 167 | self.assertEqual(out.rstrip(), 'test_stdout_none') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 168 | |
| 169 | def test_stderr_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 170 | # .stderr is None when not redirected |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 171 | p = subprocess.Popen([sys.executable, "-c", 'print "banana"'], |
| 172 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 173 | self.addCleanup(p.stdout.close) |
| 174 | self.addCleanup(p.stdin.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 175 | p.wait() |
| 176 | self.assertEqual(p.stderr, None) |
| 177 | |
Ezio Melotti | 8f6a287 | 2010-02-10 21:40:33 +0000 | [diff] [blame] | 178 | def test_executable_with_cwd(self): |
Florent Xicluna | 6376370 | 2010-03-11 01:50:48 +0000 | [diff] [blame] | 179 | python_dir = os.path.dirname(os.path.realpath(sys.executable)) |
Ezio Melotti | 8f6a287 | 2010-02-10 21:40:33 +0000 | [diff] [blame] | 180 | p = subprocess.Popen(["somethingyoudonthave", "-c", |
| 181 | "import sys; sys.exit(47)"], |
| 182 | executable=sys.executable, cwd=python_dir) |
| 183 | p.wait() |
| 184 | self.assertEqual(p.returncode, 47) |
| 185 | |
| 186 | @unittest.skipIf(sysconfig.is_python_build(), |
| 187 | "need an installed Python. See #7774") |
| 188 | def test_executable_without_cwd(self): |
| 189 | # For a normal installation, it should work without 'cwd' |
| 190 | # argument. For test runs in the build directory, see #7774. |
| 191 | p = subprocess.Popen(["somethingyoudonthave", "-c", |
| 192 | "import sys; sys.exit(47)"], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 193 | executable=sys.executable) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 194 | p.wait() |
| 195 | self.assertEqual(p.returncode, 47) |
| 196 | |
| 197 | def test_stdin_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 198 | # stdin redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 199 | p = subprocess.Popen([sys.executable, "-c", |
| 200 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 201 | stdin=subprocess.PIPE) |
| 202 | p.stdin.write("pear") |
| 203 | p.stdin.close() |
| 204 | p.wait() |
| 205 | self.assertEqual(p.returncode, 1) |
| 206 | |
| 207 | def test_stdin_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 208 | # stdin is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 209 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 210 | d = tf.fileno() |
| 211 | os.write(d, "pear") |
| 212 | os.lseek(d, 0, 0) |
| 213 | p = subprocess.Popen([sys.executable, "-c", |
| 214 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 215 | stdin=d) |
| 216 | p.wait() |
| 217 | self.assertEqual(p.returncode, 1) |
| 218 | |
| 219 | def test_stdin_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 220 | # stdin is set to open file object |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 221 | tf = tempfile.TemporaryFile() |
| 222 | tf.write("pear") |
| 223 | tf.seek(0) |
| 224 | p = subprocess.Popen([sys.executable, "-c", |
| 225 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 226 | stdin=tf) |
| 227 | p.wait() |
| 228 | self.assertEqual(p.returncode, 1) |
| 229 | |
| 230 | def test_stdout_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 231 | # stdout redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 232 | p = subprocess.Popen([sys.executable, "-c", |
| 233 | 'import sys; sys.stdout.write("orange")'], |
| 234 | stdout=subprocess.PIPE) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 235 | self.addCleanup(p.stdout.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 236 | self.assertEqual(p.stdout.read(), "orange") |
| 237 | |
| 238 | def test_stdout_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 239 | # stdout is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 240 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 241 | d = tf.fileno() |
| 242 | p = subprocess.Popen([sys.executable, "-c", |
| 243 | 'import sys; sys.stdout.write("orange")'], |
| 244 | stdout=d) |
| 245 | p.wait() |
| 246 | os.lseek(d, 0, 0) |
| 247 | self.assertEqual(os.read(d, 1024), "orange") |
| 248 | |
| 249 | def test_stdout_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 250 | # stdout is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 251 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 252 | p = subprocess.Popen([sys.executable, "-c", |
| 253 | 'import sys; sys.stdout.write("orange")'], |
| 254 | stdout=tf) |
| 255 | p.wait() |
| 256 | tf.seek(0) |
| 257 | self.assertEqual(tf.read(), "orange") |
| 258 | |
| 259 | def test_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 260 | # stderr redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 261 | p = subprocess.Popen([sys.executable, "-c", |
| 262 | 'import sys; sys.stderr.write("strawberry")'], |
| 263 | stderr=subprocess.PIPE) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 264 | self.addCleanup(p.stderr.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 265 | self.assertStderrEqual(p.stderr.read(), "strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 266 | |
| 267 | def test_stderr_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 268 | # stderr is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 269 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 270 | d = tf.fileno() |
| 271 | p = subprocess.Popen([sys.executable, "-c", |
| 272 | 'import sys; sys.stderr.write("strawberry")'], |
| 273 | stderr=d) |
| 274 | p.wait() |
| 275 | os.lseek(d, 0, 0) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 276 | self.assertStderrEqual(os.read(d, 1024), "strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 277 | |
| 278 | def test_stderr_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 279 | # stderr is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 280 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 281 | p = subprocess.Popen([sys.executable, "-c", |
| 282 | 'import sys; sys.stderr.write("strawberry")'], |
| 283 | stderr=tf) |
| 284 | p.wait() |
| 285 | tf.seek(0) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 286 | self.assertStderrEqual(tf.read(), "strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 287 | |
Martin Panter | 1edccfa | 2016-05-13 01:54:44 +0000 | [diff] [blame] | 288 | def test_stderr_redirect_with_no_stdout_redirect(self): |
| 289 | # test stderr=STDOUT while stdout=None (not set) |
| 290 | |
| 291 | # - grandchild prints to stderr |
| 292 | # - child redirects grandchild's stderr to its stdout |
| 293 | # - the parent should get grandchild's stderr in child's stdout |
| 294 | p = subprocess.Popen([sys.executable, "-c", |
| 295 | 'import sys, subprocess;' |
| 296 | 'rc = subprocess.call([sys.executable, "-c",' |
| 297 | ' "import sys;"' |
| 298 | ' "sys.stderr.write(\'42\')"],' |
| 299 | ' stderr=subprocess.STDOUT);' |
| 300 | 'sys.exit(rc)'], |
| 301 | stdout=subprocess.PIPE, |
| 302 | stderr=subprocess.PIPE) |
| 303 | stdout, stderr = p.communicate() |
| 304 | #NOTE: stdout should get stderr from grandchild |
| 305 | self.assertStderrEqual(stdout, b'42') |
| 306 | self.assertStderrEqual(stderr, b'') # should be empty |
| 307 | self.assertEqual(p.returncode, 0) |
| 308 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 309 | def test_stdout_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 310 | # capture stdout and stderr to the same pipe |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 311 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 312 | 'import sys;' |
| 313 | 'sys.stdout.write("apple");' |
| 314 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 315 | 'sys.stderr.write("orange")'], |
| 316 | stdout=subprocess.PIPE, |
| 317 | stderr=subprocess.STDOUT) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 318 | self.addCleanup(p.stdout.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 319 | self.assertStderrEqual(p.stdout.read(), "appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 320 | |
| 321 | def test_stdout_stderr_file(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 322 | # capture stdout and stderr to the same open file |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 323 | tf = tempfile.TemporaryFile() |
| 324 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 325 | 'import sys;' |
| 326 | 'sys.stdout.write("apple");' |
| 327 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 328 | 'sys.stderr.write("orange")'], |
| 329 | stdout=tf, |
| 330 | stderr=tf) |
| 331 | p.wait() |
| 332 | tf.seek(0) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 333 | self.assertStderrEqual(tf.read(), "appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 334 | |
Gustavo Niemeyer | c36bede | 2006-09-07 00:48:33 +0000 | [diff] [blame] | 335 | def test_stdout_filedes_of_stdout(self): |
| 336 | # stdout is set to 1 (#1531862). |
Ezio Melotti | 9b9cd4c | 2013-03-11 03:21:08 +0200 | [diff] [blame] | 337 | # To avoid printing the text on stdout, we do something similar to |
Ezio Melotti | efaad09 | 2013-03-11 00:34:33 +0200 | [diff] [blame] | 338 | # test_stdout_none (see above). The parent subprocess calls the child |
| 339 | # subprocess passing stdout=1, and this test uses stdout=PIPE in |
| 340 | # order to capture and check the output of the parent. See #11963. |
| 341 | code = ('import sys, subprocess; ' |
| 342 | 'rc = subprocess.call([sys.executable, "-c", ' |
| 343 | ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), ' |
Ezio Melotti | 9b9cd4c | 2013-03-11 03:21:08 +0200 | [diff] [blame] | 344 | '\'test with stdout=1\'))"], stdout=1); ' |
| 345 | 'assert rc == 18') |
Ezio Melotti | efaad09 | 2013-03-11 00:34:33 +0200 | [diff] [blame] | 346 | p = subprocess.Popen([sys.executable, "-c", code], |
| 347 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 348 | self.addCleanup(p.stdout.close) |
| 349 | self.addCleanup(p.stderr.close) |
| 350 | out, err = p.communicate() |
| 351 | self.assertEqual(p.returncode, 0, err) |
Ezio Melotti | 9b9cd4c | 2013-03-11 03:21:08 +0200 | [diff] [blame] | 352 | self.assertEqual(out.rstrip(), 'test with stdout=1') |
Gustavo Niemeyer | c36bede | 2006-09-07 00:48:33 +0000 | [diff] [blame] | 353 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 354 | def test_cwd(self): |
Guido van Rossum | e9a0e88 | 2007-12-20 17:28:10 +0000 | [diff] [blame] | 355 | tmpdir = tempfile.gettempdir() |
Peter Astrand | 195404f | 2004-11-12 15:51:48 +0000 | [diff] [blame] | 356 | # We cannot use os.path.realpath to canonicalize the path, |
| 357 | # since it doesn't expand Tru64 {memb} strings. See bug 1063571. |
| 358 | cwd = os.getcwd() |
| 359 | os.chdir(tmpdir) |
| 360 | tmpdir = os.getcwd() |
| 361 | os.chdir(cwd) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 362 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 363 | 'import sys,os;' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 364 | 'sys.stdout.write(os.getcwd())'], |
| 365 | stdout=subprocess.PIPE, |
| 366 | cwd=tmpdir) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 367 | self.addCleanup(p.stdout.close) |
Fredrik Lundh | 59c0559 | 2004-10-13 06:55:40 +0000 | [diff] [blame] | 368 | normcase = os.path.normcase |
| 369 | self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir)) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 370 | |
| 371 | def test_env(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 372 | newenv = os.environ.copy() |
| 373 | newenv["FRUIT"] = "orange" |
| 374 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 375 | 'import sys,os;' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 376 | 'sys.stdout.write(os.getenv("FRUIT"))'], |
| 377 | stdout=subprocess.PIPE, |
| 378 | env=newenv) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 379 | self.addCleanup(p.stdout.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 380 | self.assertEqual(p.stdout.read(), "orange") |
| 381 | |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 382 | def test_communicate_stdin(self): |
| 383 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 384 | 'import sys;' |
| 385 | 'sys.exit(sys.stdin.read() == "pear")'], |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 386 | stdin=subprocess.PIPE) |
| 387 | p.communicate("pear") |
| 388 | self.assertEqual(p.returncode, 1) |
| 389 | |
| 390 | def test_communicate_stdout(self): |
| 391 | p = subprocess.Popen([sys.executable, "-c", |
| 392 | 'import sys; sys.stdout.write("pineapple")'], |
| 393 | stdout=subprocess.PIPE) |
| 394 | (stdout, stderr) = p.communicate() |
| 395 | self.assertEqual(stdout, "pineapple") |
| 396 | self.assertEqual(stderr, None) |
| 397 | |
| 398 | def test_communicate_stderr(self): |
| 399 | p = subprocess.Popen([sys.executable, "-c", |
| 400 | 'import sys; sys.stderr.write("pineapple")'], |
| 401 | stderr=subprocess.PIPE) |
| 402 | (stdout, stderr) = p.communicate() |
| 403 | self.assertEqual(stdout, None) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 404 | self.assertStderrEqual(stderr, "pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 405 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 406 | def test_communicate(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 407 | p = subprocess.Popen([sys.executable, "-c", |
Gregory P. Smith | 4036fd4 | 2008-05-26 20:22:14 +0000 | [diff] [blame] | 408 | 'import sys,os;' |
| 409 | 'sys.stderr.write("pineapple");' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 410 | 'sys.stdout.write(sys.stdin.read())'], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 411 | stdin=subprocess.PIPE, |
| 412 | stdout=subprocess.PIPE, |
| 413 | stderr=subprocess.PIPE) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 414 | self.addCleanup(p.stdout.close) |
| 415 | self.addCleanup(p.stderr.close) |
| 416 | self.addCleanup(p.stdin.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 417 | (stdout, stderr) = p.communicate("banana") |
| 418 | self.assertEqual(stdout, "banana") |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 419 | self.assertStderrEqual(stderr, "pineapple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 420 | |
Gregory P. Smith | 4036fd4 | 2008-05-26 20:22:14 +0000 | [diff] [blame] | 421 | # This test is Linux specific for simplicity to at least have |
| 422 | # some coverage. It is not a platform specific bug. |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 423 | @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()), |
| 424 | "Linux specific") |
| 425 | # Test for the fd leak reported in http://bugs.python.org/issue2791. |
| 426 | def test_communicate_pipe_fd_leak(self): |
| 427 | fd_directory = '/proc/%d/fd' % os.getpid() |
| 428 | num_fds_before_popen = len(os.listdir(fd_directory)) |
Martin Panter | ad6a99c | 2016-09-10 10:38:22 +0000 | [diff] [blame] | 429 | p = subprocess.Popen([sys.executable, "-c", "print('')"], |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 430 | stdout=subprocess.PIPE) |
| 431 | p.communicate() |
| 432 | num_fds_after_communicate = len(os.listdir(fd_directory)) |
| 433 | del p |
| 434 | num_fds_after_destruction = len(os.listdir(fd_directory)) |
| 435 | self.assertEqual(num_fds_before_popen, num_fds_after_destruction) |
| 436 | self.assertEqual(num_fds_before_popen, num_fds_after_communicate) |
Gregory P. Smith | 4036fd4 | 2008-05-26 20:22:14 +0000 | [diff] [blame] | 437 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 438 | def test_communicate_returns(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 439 | # communicate() should return None if no redirection is active |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 440 | p = subprocess.Popen([sys.executable, "-c", |
| 441 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 442 | (stdout, stderr) = p.communicate() |
| 443 | self.assertEqual(stdout, None) |
| 444 | self.assertEqual(stderr, None) |
| 445 | |
| 446 | def test_communicate_pipe_buf(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 447 | # communicate() with writes larger than pipe_buf |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 448 | # This test will probably deadlock rather than fail, if |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 449 | # communicate() does not work properly. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 450 | x, y = os.pipe() |
| 451 | if mswindows: |
| 452 | pipe_buf = 512 |
| 453 | else: |
| 454 | pipe_buf = os.fpathconf(x, "PC_PIPE_BUF") |
| 455 | os.close(x) |
| 456 | os.close(y) |
| 457 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 458 | 'import sys,os;' |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 459 | 'sys.stdout.write(sys.stdin.read(47));' |
| 460 | 'sys.stderr.write("xyz"*%d);' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 461 | 'sys.stdout.write(sys.stdin.read())' % pipe_buf], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 462 | stdin=subprocess.PIPE, |
| 463 | stdout=subprocess.PIPE, |
| 464 | stderr=subprocess.PIPE) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 465 | self.addCleanup(p.stdout.close) |
| 466 | self.addCleanup(p.stderr.close) |
| 467 | self.addCleanup(p.stdin.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 468 | string_to_write = "abc"*pipe_buf |
| 469 | (stdout, stderr) = p.communicate(string_to_write) |
| 470 | self.assertEqual(stdout, string_to_write) |
| 471 | |
| 472 | def test_writes_before_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 473 | # stdin.write before communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 474 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 475 | 'import sys,os;' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 476 | 'sys.stdout.write(sys.stdin.read())'], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 477 | stdin=subprocess.PIPE, |
| 478 | stdout=subprocess.PIPE, |
| 479 | stderr=subprocess.PIPE) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 480 | self.addCleanup(p.stdout.close) |
| 481 | self.addCleanup(p.stderr.close) |
| 482 | self.addCleanup(p.stdin.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 483 | p.stdin.write("banana") |
| 484 | (stdout, stderr) = p.communicate("split") |
| 485 | self.assertEqual(stdout, "bananasplit") |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 486 | self.assertStderrEqual(stderr, "") |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 487 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 488 | def test_universal_newlines(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 489 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 490 | 'import sys,os;' + SETBINARY + |
| 491 | 'sys.stdout.write("line1\\n");' |
| 492 | 'sys.stdout.flush();' |
| 493 | 'sys.stdout.write("line2\\r");' |
| 494 | 'sys.stdout.flush();' |
| 495 | 'sys.stdout.write("line3\\r\\n");' |
| 496 | 'sys.stdout.flush();' |
| 497 | 'sys.stdout.write("line4\\r");' |
| 498 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 499 | 'sys.stdout.write("\\nline5");' |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 500 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 501 | 'sys.stdout.write("\\nline6");'], |
| 502 | stdout=subprocess.PIPE, |
| 503 | universal_newlines=1) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 504 | self.addCleanup(p.stdout.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 505 | stdout = p.stdout.read() |
Neal Norwitz | a6d01ce | 2006-05-02 06:23:22 +0000 | [diff] [blame] | 506 | if hasattr(file, 'newlines'): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 507 | # Interpreter with universal newline support |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 508 | self.assertEqual(stdout, |
| 509 | "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 510 | else: |
| 511 | # Interpreter without universal newline support |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 512 | self.assertEqual(stdout, |
| 513 | "line1\nline2\rline3\r\nline4\r\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 514 | |
| 515 | def test_universal_newlines_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 516 | # universal newlines through communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 517 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 518 | 'import sys,os;' + SETBINARY + |
| 519 | 'sys.stdout.write("line1\\n");' |
| 520 | 'sys.stdout.flush();' |
| 521 | 'sys.stdout.write("line2\\r");' |
| 522 | 'sys.stdout.flush();' |
| 523 | 'sys.stdout.write("line3\\r\\n");' |
| 524 | 'sys.stdout.flush();' |
| 525 | 'sys.stdout.write("line4\\r");' |
| 526 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 527 | 'sys.stdout.write("\\nline5");' |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 528 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 529 | 'sys.stdout.write("\\nline6");'], |
| 530 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 531 | universal_newlines=1) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 532 | self.addCleanup(p.stdout.close) |
| 533 | self.addCleanup(p.stderr.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 534 | (stdout, stderr) = p.communicate() |
Neal Norwitz | a6d01ce | 2006-05-02 06:23:22 +0000 | [diff] [blame] | 535 | if hasattr(file, 'newlines'): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 536 | # Interpreter with universal newline support |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 537 | self.assertEqual(stdout, |
| 538 | "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 539 | else: |
| 540 | # Interpreter without universal newline support |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 541 | self.assertEqual(stdout, |
| 542 | "line1\nline2\rline3\r\nline4\r\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 543 | |
| 544 | def test_no_leaking(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 545 | # Make sure we leak no resources |
Antoine Pitrou | b0b3bff | 2010-09-18 22:42:30 +0000 | [diff] [blame] | 546 | if not mswindows: |
Peter Astrand | f7f1bb7 | 2005-03-03 20:47:37 +0000 | [diff] [blame] | 547 | max_handles = 1026 # too much for most UNIX systems |
| 548 | else: |
Antoine Pitrou | b0b3bff | 2010-09-18 22:42:30 +0000 | [diff] [blame] | 549 | max_handles = 2050 # too much for (at least some) Windows setups |
| 550 | handles = [] |
| 551 | try: |
| 552 | for i in range(max_handles): |
| 553 | try: |
| 554 | handles.append(os.open(test_support.TESTFN, |
| 555 | os.O_WRONLY | os.O_CREAT)) |
| 556 | except OSError as e: |
| 557 | if e.errno != errno.EMFILE: |
| 558 | raise |
| 559 | break |
| 560 | else: |
| 561 | self.skipTest("failed to reach the file descriptor limit " |
| 562 | "(tried %d)" % max_handles) |
| 563 | # Close a couple of them (should be enough for a subprocess) |
| 564 | for i in range(10): |
| 565 | os.close(handles.pop()) |
| 566 | # Loop creating some subprocesses. If one of them leaks some fds, |
| 567 | # the next loop iteration will fail by reaching the max fd limit. |
| 568 | for i in range(15): |
| 569 | p = subprocess.Popen([sys.executable, "-c", |
| 570 | "import sys;" |
| 571 | "sys.stdout.write(sys.stdin.read())"], |
| 572 | stdin=subprocess.PIPE, |
| 573 | stdout=subprocess.PIPE, |
| 574 | stderr=subprocess.PIPE) |
| 575 | data = p.communicate(b"lime")[0] |
| 576 | self.assertEqual(data, b"lime") |
| 577 | finally: |
| 578 | for h in handles: |
| 579 | os.close(h) |
Mark Dickinson | 313dc9b | 2012-10-07 15:41:38 +0100 | [diff] [blame] | 580 | test_support.unlink(test_support.TESTFN) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 581 | |
| 582 | def test_list2cmdline(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 583 | self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), |
| 584 | '"a b c" d e') |
| 585 | self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), |
| 586 | 'ab\\"c \\ d') |
Gregory P. Smith | e047e6d | 2008-01-19 20:49:02 +0000 | [diff] [blame] | 587 | self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']), |
| 588 | 'ab\\"c " \\\\" d') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 589 | self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']), |
| 590 | 'a\\\\\\b "de fg" h') |
| 591 | self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']), |
| 592 | 'a\\\\\\"b c d') |
| 593 | self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']), |
| 594 | '"a\\\\b c" d e') |
| 595 | self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']), |
| 596 | '"a\\\\b\\ c" d e') |
Peter Astrand | 10514a7 | 2007-01-13 22:35:35 +0000 | [diff] [blame] | 597 | self.assertEqual(subprocess.list2cmdline(['ab', '']), |
| 598 | 'ab ""') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 599 | |
| 600 | |
| 601 | def test_poll(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 602 | p = subprocess.Popen([sys.executable, |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 603 | "-c", "import time; time.sleep(1)"]) |
| 604 | count = 0 |
| 605 | while p.poll() is None: |
| 606 | time.sleep(0.1) |
| 607 | count += 1 |
| 608 | # We expect that the poll loop probably went around about 10 times, |
| 609 | # but, based on system scheduling we can't control, it's possible |
| 610 | # poll() never returned None. It "should be" very rare that it |
| 611 | # didn't go around at least twice. |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 612 | self.assertGreaterEqual(count, 2) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 613 | # Subsequent invocations should just return the returncode |
| 614 | self.assertEqual(p.poll(), 0) |
| 615 | |
| 616 | |
| 617 | def test_wait(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 618 | p = subprocess.Popen([sys.executable, |
| 619 | "-c", "import time; time.sleep(2)"]) |
| 620 | self.assertEqual(p.wait(), 0) |
| 621 | # Subsequent invocations should just return the returncode |
| 622 | self.assertEqual(p.wait(), 0) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 623 | |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 624 | |
| 625 | def test_invalid_bufsize(self): |
| 626 | # an invalid type of the bufsize argument should raise |
| 627 | # TypeError. |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 628 | with self.assertRaises(TypeError): |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 629 | subprocess.Popen([sys.executable, "-c", "pass"], "orange") |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 630 | |
Georg Brandl | f3715d2 | 2009-02-14 17:01:36 +0000 | [diff] [blame] | 631 | def test_leaking_fds_on_error(self): |
| 632 | # see bug #5179: Popen leaks file descriptors to PIPEs if |
| 633 | # the child fails to execute; this will eventually exhaust |
| 634 | # the maximum number of open fds. 1024 seems a very common |
| 635 | # value for that limit, but Windows has 2048, so we loop |
| 636 | # 1024 times (each call leaked two fds). |
| 637 | for i in range(1024): |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 638 | # Windows raises IOError. Others raise OSError. |
| 639 | with self.assertRaises(EnvironmentError) as c: |
Georg Brandl | f3715d2 | 2009-02-14 17:01:36 +0000 | [diff] [blame] | 640 | subprocess.Popen(['nonexisting_i_hope'], |
| 641 | stdout=subprocess.PIPE, |
| 642 | stderr=subprocess.PIPE) |
R David Murray | cdd5fc9 | 2011-03-13 22:37:18 -0400 | [diff] [blame] | 643 | # ignore errors that indicate the command was not found |
| 644 | if c.exception.errno not in (errno.ENOENT, errno.EACCES): |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 645 | raise c.exception |
Georg Brandl | f3715d2 | 2009-02-14 17:01:36 +0000 | [diff] [blame] | 646 | |
Antoine Pitrou | 33fc744 | 2013-08-30 23:38:13 +0200 | [diff] [blame] | 647 | @unittest.skipIf(threading is None, "threading required") |
| 648 | def test_double_close_on_error(self): |
| 649 | # Issue #18851 |
| 650 | fds = [] |
| 651 | def open_fds(): |
| 652 | for i in range(20): |
| 653 | fds.extend(os.pipe()) |
| 654 | time.sleep(0.001) |
| 655 | t = threading.Thread(target=open_fds) |
| 656 | t.start() |
| 657 | try: |
| 658 | with self.assertRaises(EnvironmentError): |
| 659 | subprocess.Popen(['nonexisting_i_hope'], |
| 660 | stdin=subprocess.PIPE, |
| 661 | stdout=subprocess.PIPE, |
| 662 | stderr=subprocess.PIPE) |
| 663 | finally: |
| 664 | t.join() |
| 665 | exc = None |
| 666 | for fd in fds: |
| 667 | # If a double close occurred, some of those fds will |
| 668 | # already have been closed by mistake, and os.close() |
| 669 | # here will raise. |
| 670 | try: |
| 671 | os.close(fd) |
| 672 | except OSError as e: |
| 673 | exc = e |
| 674 | if exc is not None: |
| 675 | raise exc |
| 676 | |
Tim Golden | 90374f5 | 2010-08-06 13:14:33 +0000 | [diff] [blame] | 677 | def test_handles_closed_on_exception(self): |
| 678 | # If CreateProcess exits with an error, ensure the |
| 679 | # duplicate output handles are released |
Berker Peksag | b7c3515 | 2015-09-28 15:37:57 +0300 | [diff] [blame] | 680 | ifhandle, ifname = tempfile.mkstemp() |
| 681 | ofhandle, ofname = tempfile.mkstemp() |
| 682 | efhandle, efname = tempfile.mkstemp() |
Tim Golden | 90374f5 | 2010-08-06 13:14:33 +0000 | [diff] [blame] | 683 | try: |
| 684 | subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle, |
| 685 | stderr=efhandle) |
| 686 | except OSError: |
| 687 | os.close(ifhandle) |
| 688 | os.remove(ifname) |
| 689 | os.close(ofhandle) |
| 690 | os.remove(ofname) |
| 691 | os.close(efhandle) |
| 692 | os.remove(efname) |
| 693 | self.assertFalse(os.path.exists(ifname)) |
| 694 | self.assertFalse(os.path.exists(ofname)) |
| 695 | self.assertFalse(os.path.exists(efname)) |
| 696 | |
Ross Lagerwall | 104c3f1 | 2011-04-05 15:24:34 +0200 | [diff] [blame] | 697 | def test_communicate_epipe(self): |
| 698 | # Issue 10963: communicate() should hide EPIPE |
| 699 | p = subprocess.Popen([sys.executable, "-c", 'pass'], |
| 700 | stdin=subprocess.PIPE, |
| 701 | stdout=subprocess.PIPE, |
| 702 | stderr=subprocess.PIPE) |
| 703 | self.addCleanup(p.stdout.close) |
| 704 | self.addCleanup(p.stderr.close) |
| 705 | self.addCleanup(p.stdin.close) |
| 706 | p.communicate("x" * 2**20) |
| 707 | |
| 708 | def test_communicate_epipe_only_stdin(self): |
| 709 | # Issue 10963: communicate() should hide EPIPE |
| 710 | p = subprocess.Popen([sys.executable, "-c", 'pass'], |
| 711 | stdin=subprocess.PIPE) |
| 712 | self.addCleanup(p.stdin.close) |
| 713 | time.sleep(2) |
| 714 | p.communicate("x" * 2**20) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 715 | |
Gregory P. Smith | 9d3b6e9 | 2012-11-10 22:49:03 -0800 | [diff] [blame] | 716 | # This test is Linux-ish specific for simplicity to at least have |
| 717 | # some coverage. It is not a platform specific bug. |
| 718 | @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()), |
| 719 | "Linux specific") |
| 720 | def test_failed_child_execute_fd_leak(self): |
| 721 | """Test for the fork() failure fd leak reported in issue16327.""" |
| 722 | fd_directory = '/proc/%d/fd' % os.getpid() |
| 723 | fds_before_popen = os.listdir(fd_directory) |
| 724 | with self.assertRaises(PopenTestException): |
| 725 | PopenExecuteChildRaises( |
| 726 | [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE, |
| 727 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 728 | |
| 729 | # NOTE: This test doesn't verify that the real _execute_child |
| 730 | # does not close the file descriptors itself on the way out |
| 731 | # during an exception. Code inspection has confirmed that. |
| 732 | |
| 733 | fds_after_exception = os.listdir(fd_directory) |
| 734 | self.assertEqual(fds_before_popen, fds_after_exception) |
| 735 | |
| 736 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 737 | # context manager |
| 738 | class _SuppressCoreFiles(object): |
| 739 | """Try to prevent core files from being created.""" |
| 740 | old_limit = None |
| 741 | |
| 742 | def __enter__(self): |
| 743 | """Try to save previous ulimit, then set it to (0, 0).""" |
Benjamin Peterson | 8b59c23 | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 744 | if resource is not None: |
| 745 | try: |
| 746 | self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) |
| 747 | resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) |
| 748 | except (ValueError, resource.error): |
| 749 | pass |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 750 | |
Ronald Oussoren | 21b44e0 | 2010-07-23 12:26:30 +0000 | [diff] [blame] | 751 | if sys.platform == 'darwin': |
| 752 | # Check if the 'Crash Reporter' on OSX was configured |
| 753 | # in 'Developer' mode and warn that it will get triggered |
| 754 | # when it is. |
| 755 | # |
| 756 | # This assumes that this context manager is used in tests |
| 757 | # that might trigger the next manager. |
| 758 | value = subprocess.Popen(['/usr/bin/defaults', 'read', |
| 759 | 'com.apple.CrashReporter', 'DialogType'], |
| 760 | stdout=subprocess.PIPE).communicate()[0] |
| 761 | if value.strip() == b'developer': |
| 762 | print "this tests triggers the Crash Reporter, that is intentional" |
| 763 | sys.stdout.flush() |
| 764 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 765 | def __exit__(self, *args): |
| 766 | """Return core file behavior to default.""" |
| 767 | if self.old_limit is None: |
| 768 | return |
Benjamin Peterson | 8b59c23 | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 769 | if resource is not None: |
| 770 | try: |
| 771 | resource.setrlimit(resource.RLIMIT_CORE, self.old_limit) |
| 772 | except (ValueError, resource.error): |
| 773 | pass |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 774 | |
Victor Stinner | b78fed9 | 2011-07-05 14:50:35 +0200 | [diff] [blame] | 775 | @unittest.skipUnless(hasattr(signal, 'SIGALRM'), |
| 776 | "Requires signal.SIGALRM") |
Victor Stinner | e790131 | 2011-07-05 14:08:01 +0200 | [diff] [blame] | 777 | def test_communicate_eintr(self): |
| 778 | # Issue #12493: communicate() should handle EINTR |
| 779 | def handler(signum, frame): |
| 780 | pass |
| 781 | old_handler = signal.signal(signal.SIGALRM, handler) |
| 782 | self.addCleanup(signal.signal, signal.SIGALRM, old_handler) |
| 783 | |
| 784 | # the process is running for 2 seconds |
| 785 | args = [sys.executable, "-c", 'import time; time.sleep(2)'] |
| 786 | for stream in ('stdout', 'stderr'): |
| 787 | kw = {stream: subprocess.PIPE} |
| 788 | with subprocess.Popen(args, **kw) as process: |
| 789 | signal.alarm(1) |
| 790 | # communicate() will be interrupted by SIGALRM |
| 791 | process.communicate() |
| 792 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 793 | |
Florent Xicluna | bab22a7 | 2010-03-04 19:40:48 +0000 | [diff] [blame] | 794 | @unittest.skipIf(mswindows, "POSIX specific tests") |
Florent Xicluna | fc4d6d7 | 2010-03-23 14:36:45 +0000 | [diff] [blame] | 795 | class POSIXProcessTestCase(BaseTestCase): |
Florent Xicluna | ab5e17f | 2010-03-04 21:31:58 +0000 | [diff] [blame] | 796 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 797 | def test_exceptions(self): |
| 798 | # caught & re-raised exceptions |
| 799 | with self.assertRaises(OSError) as c: |
| 800 | p = subprocess.Popen([sys.executable, "-c", ""], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 801 | cwd="/this/path/does/not/exist") |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 802 | # The attribute child_traceback should contain "os.chdir" somewhere. |
| 803 | self.assertIn("os.chdir", c.exception.child_traceback) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 804 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 805 | def test_run_abort(self): |
| 806 | # returncode handles signal termination |
| 807 | with _SuppressCoreFiles(): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 808 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 809 | "import os; os.abort()"]) |
| 810 | p.wait() |
| 811 | self.assertEqual(-p.returncode, signal.SIGABRT) |
| 812 | |
| 813 | def test_preexec(self): |
| 814 | # preexec function |
| 815 | p = subprocess.Popen([sys.executable, "-c", |
| 816 | "import sys, os;" |
| 817 | "sys.stdout.write(os.getenv('FRUIT'))"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 818 | stdout=subprocess.PIPE, |
| 819 | preexec_fn=lambda: os.putenv("FRUIT", "apple")) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 820 | self.addCleanup(p.stdout.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 821 | self.assertEqual(p.stdout.read(), "apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 822 | |
Gregory P. Smith | f047ba8 | 2012-11-11 09:49:02 -0800 | [diff] [blame] | 823 | class _TestExecuteChildPopen(subprocess.Popen): |
| 824 | """Used to test behavior at the end of _execute_child.""" |
| 825 | def __init__(self, testcase, *args, **kwargs): |
Gregory P. Smith | f047ba8 | 2012-11-11 09:49:02 -0800 | [diff] [blame] | 826 | self._testcase = testcase |
| 827 | subprocess.Popen.__init__(self, *args, **kwargs) |
Gregory P. Smith | 211248b | 2012-11-11 02:00:49 -0800 | [diff] [blame] | 828 | |
Gregory P. Smith | f047ba8 | 2012-11-11 09:49:02 -0800 | [diff] [blame] | 829 | def _execute_child( |
| 830 | self, args, executable, preexec_fn, close_fds, cwd, env, |
Antoine Pitrou | 33fc744 | 2013-08-30 23:38:13 +0200 | [diff] [blame] | 831 | universal_newlines, startupinfo, creationflags, shell, to_close, |
Gregory P. Smith | 211248b | 2012-11-11 02:00:49 -0800 | [diff] [blame] | 832 | p2cread, p2cwrite, |
| 833 | c2pread, c2pwrite, |
| 834 | errread, errwrite): |
| 835 | try: |
| 836 | subprocess.Popen._execute_child( |
Gregory P. Smith | f047ba8 | 2012-11-11 09:49:02 -0800 | [diff] [blame] | 837 | self, args, executable, preexec_fn, close_fds, |
Gregory P. Smith | 211248b | 2012-11-11 02:00:49 -0800 | [diff] [blame] | 838 | cwd, env, universal_newlines, |
Antoine Pitrou | 33fc744 | 2013-08-30 23:38:13 +0200 | [diff] [blame] | 839 | startupinfo, creationflags, shell, to_close, |
Gregory P. Smith | 211248b | 2012-11-11 02:00:49 -0800 | [diff] [blame] | 840 | p2cread, p2cwrite, |
| 841 | c2pread, c2pwrite, |
| 842 | errread, errwrite) |
| 843 | finally: |
| 844 | # Open a bunch of file descriptors and verify that |
| 845 | # none of them are the same as the ones the Popen |
| 846 | # instance is using for stdin/stdout/stderr. |
| 847 | devzero_fds = [os.open("/dev/zero", os.O_RDONLY) |
| 848 | for _ in range(8)] |
| 849 | try: |
| 850 | for fd in devzero_fds: |
Gregory P. Smith | f047ba8 | 2012-11-11 09:49:02 -0800 | [diff] [blame] | 851 | self._testcase.assertNotIn( |
| 852 | fd, (p2cwrite, c2pread, errread)) |
Gregory P. Smith | 211248b | 2012-11-11 02:00:49 -0800 | [diff] [blame] | 853 | finally: |
Richard Oudkerk | 045e457 | 2013-06-10 16:27:45 +0100 | [diff] [blame] | 854 | for fd in devzero_fds: |
| 855 | os.close(fd) |
Gregory P. Smith | 211248b | 2012-11-11 02:00:49 -0800 | [diff] [blame] | 856 | |
Gregory P. Smith | f047ba8 | 2012-11-11 09:49:02 -0800 | [diff] [blame] | 857 | @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.") |
| 858 | def test_preexec_errpipe_does_not_double_close_pipes(self): |
| 859 | """Issue16140: Don't double close pipes on preexec error.""" |
| 860 | |
| 861 | def raise_it(): |
| 862 | raise RuntimeError("force the _execute_child() errpipe_data path.") |
Gregory P. Smith | 211248b | 2012-11-11 02:00:49 -0800 | [diff] [blame] | 863 | |
| 864 | with self.assertRaises(RuntimeError): |
Gregory P. Smith | f047ba8 | 2012-11-11 09:49:02 -0800 | [diff] [blame] | 865 | self._TestExecuteChildPopen( |
| 866 | self, [sys.executable, "-c", "pass"], |
| 867 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 868 | stderr=subprocess.PIPE, preexec_fn=raise_it) |
Gregory P. Smith | 211248b | 2012-11-11 02:00:49 -0800 | [diff] [blame] | 869 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 870 | def test_args_string(self): |
| 871 | # args is a string |
Berker Peksag | b7c3515 | 2015-09-28 15:37:57 +0300 | [diff] [blame] | 872 | f, fname = tempfile.mkstemp() |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 873 | os.write(f, "#!/bin/sh\n") |
| 874 | os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 875 | sys.executable) |
| 876 | os.close(f) |
| 877 | os.chmod(fname, 0o700) |
| 878 | p = subprocess.Popen(fname) |
| 879 | p.wait() |
| 880 | os.remove(fname) |
| 881 | self.assertEqual(p.returncode, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 882 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 883 | def test_invalid_args(self): |
| 884 | # invalid arguments should raise ValueError |
| 885 | self.assertRaises(ValueError, subprocess.call, |
| 886 | [sys.executable, "-c", |
| 887 | "import sys; sys.exit(47)"], |
| 888 | startupinfo=47) |
| 889 | self.assertRaises(ValueError, subprocess.call, |
| 890 | [sys.executable, "-c", |
| 891 | "import sys; sys.exit(47)"], |
| 892 | creationflags=47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 893 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 894 | def test_shell_sequence(self): |
| 895 | # Run command through the shell (sequence) |
| 896 | newenv = os.environ.copy() |
| 897 | newenv["FRUIT"] = "apple" |
| 898 | p = subprocess.Popen(["echo $FRUIT"], shell=1, |
| 899 | stdout=subprocess.PIPE, |
| 900 | env=newenv) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 901 | self.addCleanup(p.stdout.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 902 | self.assertEqual(p.stdout.read().strip(), "apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 903 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 904 | def test_shell_string(self): |
| 905 | # Run command through the shell (string) |
| 906 | newenv = os.environ.copy() |
| 907 | newenv["FRUIT"] = "apple" |
| 908 | p = subprocess.Popen("echo $FRUIT", shell=1, |
| 909 | stdout=subprocess.PIPE, |
| 910 | env=newenv) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 911 | self.addCleanup(p.stdout.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 912 | self.assertEqual(p.stdout.read().strip(), "apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 913 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 914 | def test_call_string(self): |
| 915 | # call() function with string argument on UNIX |
Berker Peksag | b7c3515 | 2015-09-28 15:37:57 +0300 | [diff] [blame] | 916 | f, fname = tempfile.mkstemp() |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 917 | os.write(f, "#!/bin/sh\n") |
| 918 | os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 919 | sys.executable) |
| 920 | os.close(f) |
| 921 | os.chmod(fname, 0700) |
| 922 | rc = subprocess.call(fname) |
| 923 | os.remove(fname) |
| 924 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 925 | |
Stefan Krah | e9a6a7d | 2010-07-19 14:41:08 +0000 | [diff] [blame] | 926 | def test_specific_shell(self): |
| 927 | # Issue #9265: Incorrect name passed as arg[0]. |
| 928 | shells = [] |
| 929 | for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']: |
| 930 | for name in ['bash', 'ksh']: |
| 931 | sh = os.path.join(prefix, name) |
| 932 | if os.path.isfile(sh): |
| 933 | shells.append(sh) |
| 934 | if not shells: # Will probably work for any shell but csh. |
| 935 | self.skipTest("bash or ksh required for this test") |
| 936 | sh = '/bin/sh' |
| 937 | if os.path.isfile(sh) and not os.path.islink(sh): |
| 938 | # Test will fail if /bin/sh is a symlink to csh. |
| 939 | shells.append(sh) |
| 940 | for sh in shells: |
| 941 | p = subprocess.Popen("echo $0", executable=sh, shell=True, |
| 942 | stdout=subprocess.PIPE) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 943 | self.addCleanup(p.stdout.close) |
Stefan Krah | e9a6a7d | 2010-07-19 14:41:08 +0000 | [diff] [blame] | 944 | self.assertEqual(p.stdout.read().strip(), sh) |
| 945 | |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 946 | def _kill_process(self, method, *args): |
Florent Xicluna | cecef39 | 2010-03-05 19:31:21 +0000 | [diff] [blame] | 947 | # Do not inherit file handles from the parent. |
| 948 | # It should fix failures on some platforms. |
Antoine Pitrou | a6166da | 2010-09-20 11:20:44 +0000 | [diff] [blame] | 949 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 950 | import sys, time |
| 951 | sys.stdout.write('x\\n') |
| 952 | sys.stdout.flush() |
| 953 | time.sleep(30) |
| 954 | """], |
| 955 | close_fds=True, |
| 956 | stdin=subprocess.PIPE, |
| 957 | stdout=subprocess.PIPE, |
| 958 | stderr=subprocess.PIPE) |
| 959 | # Wait for the interpreter to be completely initialized before |
| 960 | # sending any signal. |
| 961 | p.stdout.read(1) |
| 962 | getattr(p, method)(*args) |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 963 | return p |
| 964 | |
Charles-François Natali | ef2bd67 | 2013-01-12 16:52:20 +0100 | [diff] [blame] | 965 | @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')), |
| 966 | "Due to known OS bug (issue #16762)") |
Antoine Pitrou | f60845b | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 967 | def _kill_dead_process(self, method, *args): |
| 968 | # Do not inherit file handles from the parent. |
| 969 | # It should fix failures on some platforms. |
| 970 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 971 | import sys, time |
| 972 | sys.stdout.write('x\\n') |
| 973 | sys.stdout.flush() |
| 974 | """], |
| 975 | close_fds=True, |
| 976 | stdin=subprocess.PIPE, |
| 977 | stdout=subprocess.PIPE, |
| 978 | stderr=subprocess.PIPE) |
| 979 | # Wait for the interpreter to be completely initialized before |
| 980 | # sending any signal. |
| 981 | p.stdout.read(1) |
| 982 | # The process should end after this |
| 983 | time.sleep(1) |
| 984 | # This shouldn't raise even though the child is now dead |
| 985 | getattr(p, method)(*args) |
| 986 | p.communicate() |
| 987 | |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 988 | def test_send_signal(self): |
| 989 | p = self._kill_process('send_signal', signal.SIGINT) |
Florent Xicluna | fc4d6d7 | 2010-03-23 14:36:45 +0000 | [diff] [blame] | 990 | _, stderr = p.communicate() |
Florent Xicluna | 3c919cf | 2010-03-23 19:19:16 +0000 | [diff] [blame] | 991 | self.assertIn('KeyboardInterrupt', stderr) |
Florent Xicluna | 446ff14 | 2010-03-23 15:05:30 +0000 | [diff] [blame] | 992 | self.assertNotEqual(p.wait(), 0) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 993 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 994 | def test_kill(self): |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 995 | p = self._kill_process('kill') |
Florent Xicluna | 446ff14 | 2010-03-23 15:05:30 +0000 | [diff] [blame] | 996 | _, stderr = p.communicate() |
| 997 | self.assertStderrEqual(stderr, '') |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 998 | self.assertEqual(p.wait(), -signal.SIGKILL) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 999 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1000 | def test_terminate(self): |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 1001 | p = self._kill_process('terminate') |
Florent Xicluna | 446ff14 | 2010-03-23 15:05:30 +0000 | [diff] [blame] | 1002 | _, stderr = p.communicate() |
| 1003 | self.assertStderrEqual(stderr, '') |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1004 | self.assertEqual(p.wait(), -signal.SIGTERM) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1005 | |
Antoine Pitrou | f60845b | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 1006 | def test_send_signal_dead(self): |
| 1007 | # Sending a signal to a dead process |
| 1008 | self._kill_dead_process('send_signal', signal.SIGINT) |
| 1009 | |
| 1010 | def test_kill_dead(self): |
| 1011 | # Killing a dead process |
| 1012 | self._kill_dead_process('kill') |
| 1013 | |
| 1014 | def test_terminate_dead(self): |
| 1015 | # Terminating a dead process |
| 1016 | self._kill_dead_process('terminate') |
| 1017 | |
Antoine Pitrou | 91ce0d9 | 2011-01-03 18:45:09 +0000 | [diff] [blame] | 1018 | def check_close_std_fds(self, fds): |
| 1019 | # Issue #9905: test that subprocess pipes still work properly with |
| 1020 | # some standard fds closed |
| 1021 | stdin = 0 |
| 1022 | newfds = [] |
| 1023 | for a in fds: |
| 1024 | b = os.dup(a) |
| 1025 | newfds.append(b) |
| 1026 | if a == 0: |
| 1027 | stdin = b |
| 1028 | try: |
| 1029 | for fd in fds: |
| 1030 | os.close(fd) |
| 1031 | out, err = subprocess.Popen([sys.executable, "-c", |
| 1032 | 'import sys;' |
| 1033 | 'sys.stdout.write("apple");' |
| 1034 | 'sys.stdout.flush();' |
| 1035 | 'sys.stderr.write("orange")'], |
| 1036 | stdin=stdin, |
| 1037 | stdout=subprocess.PIPE, |
| 1038 | stderr=subprocess.PIPE).communicate() |
| 1039 | err = test_support.strip_python_stderr(err) |
| 1040 | self.assertEqual((out, err), (b'apple', b'orange')) |
| 1041 | finally: |
| 1042 | for b, a in zip(newfds, fds): |
| 1043 | os.dup2(b, a) |
| 1044 | for b in newfds: |
| 1045 | os.close(b) |
| 1046 | |
| 1047 | def test_close_fd_0(self): |
| 1048 | self.check_close_std_fds([0]) |
| 1049 | |
| 1050 | def test_close_fd_1(self): |
| 1051 | self.check_close_std_fds([1]) |
| 1052 | |
| 1053 | def test_close_fd_2(self): |
| 1054 | self.check_close_std_fds([2]) |
| 1055 | |
| 1056 | def test_close_fds_0_1(self): |
| 1057 | self.check_close_std_fds([0, 1]) |
| 1058 | |
| 1059 | def test_close_fds_0_2(self): |
| 1060 | self.check_close_std_fds([0, 2]) |
| 1061 | |
| 1062 | def test_close_fds_1_2(self): |
| 1063 | self.check_close_std_fds([1, 2]) |
| 1064 | |
| 1065 | def test_close_fds_0_1_2(self): |
| 1066 | # Issue #10806: test that subprocess pipes still work properly with |
| 1067 | # all standard fds closed. |
| 1068 | self.check_close_std_fds([0, 1, 2]) |
| 1069 | |
Ross Lagerwall | d8e3901 | 2011-07-27 18:54:53 +0200 | [diff] [blame] | 1070 | def check_swap_fds(self, stdin_no, stdout_no, stderr_no): |
| 1071 | # open up some temporary files |
Berker Peksag | b7c3515 | 2015-09-28 15:37:57 +0300 | [diff] [blame] | 1072 | temps = [tempfile.mkstemp() for i in range(3)] |
Ross Lagerwall | d8e3901 | 2011-07-27 18:54:53 +0200 | [diff] [blame] | 1073 | temp_fds = [fd for fd, fname in temps] |
| 1074 | try: |
| 1075 | # unlink the files -- we won't need to reopen them |
| 1076 | for fd, fname in temps: |
| 1077 | os.unlink(fname) |
| 1078 | |
| 1079 | # save a copy of the standard file descriptors |
| 1080 | saved_fds = [os.dup(fd) for fd in range(3)] |
| 1081 | try: |
| 1082 | # duplicate the temp files over the standard fd's 0, 1, 2 |
| 1083 | for fd, temp_fd in enumerate(temp_fds): |
| 1084 | os.dup2(temp_fd, fd) |
| 1085 | |
| 1086 | # write some data to what will become stdin, and rewind |
| 1087 | os.write(stdin_no, b"STDIN") |
| 1088 | os.lseek(stdin_no, 0, 0) |
| 1089 | |
| 1090 | # now use those files in the given order, so that subprocess |
| 1091 | # has to rearrange them in the child |
| 1092 | p = subprocess.Popen([sys.executable, "-c", |
| 1093 | 'import sys; got = sys.stdin.read();' |
| 1094 | 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'], |
| 1095 | stdin=stdin_no, |
| 1096 | stdout=stdout_no, |
| 1097 | stderr=stderr_no) |
| 1098 | p.wait() |
| 1099 | |
| 1100 | for fd in temp_fds: |
| 1101 | os.lseek(fd, 0, 0) |
| 1102 | |
| 1103 | out = os.read(stdout_no, 1024) |
| 1104 | err = test_support.strip_python_stderr(os.read(stderr_no, 1024)) |
| 1105 | finally: |
| 1106 | for std, saved in enumerate(saved_fds): |
| 1107 | os.dup2(saved, std) |
| 1108 | os.close(saved) |
| 1109 | |
| 1110 | self.assertEqual(out, b"got STDIN") |
| 1111 | self.assertEqual(err, b"err") |
| 1112 | |
| 1113 | finally: |
| 1114 | for fd in temp_fds: |
| 1115 | os.close(fd) |
| 1116 | |
| 1117 | # When duping fds, if there arises a situation where one of the fds is |
| 1118 | # either 0, 1 or 2, it is possible that it is overwritten (#12607). |
| 1119 | # This tests all combinations of this. |
| 1120 | def test_swap_fds(self): |
| 1121 | self.check_swap_fds(0, 1, 2) |
| 1122 | self.check_swap_fds(0, 2, 1) |
| 1123 | self.check_swap_fds(1, 0, 2) |
| 1124 | self.check_swap_fds(1, 2, 0) |
| 1125 | self.check_swap_fds(2, 0, 1) |
| 1126 | self.check_swap_fds(2, 1, 0) |
| 1127 | |
Gregory P. Smith | 312efbc | 2010-12-14 15:02:53 +0000 | [diff] [blame] | 1128 | def test_wait_when_sigchild_ignored(self): |
| 1129 | # NOTE: sigchild_ignore.py may not be an effective test on all OSes. |
| 1130 | sigchild_ignore = test_support.findfile("sigchild_ignore.py", |
| 1131 | subdir="subprocessdata") |
| 1132 | p = subprocess.Popen([sys.executable, sigchild_ignore], |
| 1133 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 1134 | stdout, stderr = p.communicate() |
| 1135 | self.assertEqual(0, p.returncode, "sigchild_ignore.py exited" |
| 1136 | " non-zero with this error:\n%s" % stderr) |
| 1137 | |
Charles-François Natali | 100df0f | 2011-08-18 17:56:02 +0200 | [diff] [blame] | 1138 | def test_zombie_fast_process_del(self): |
| 1139 | # Issue #12650: on Unix, if Popen.__del__() was called before the |
| 1140 | # process exited, it wouldn't be added to subprocess._active, and would |
| 1141 | # remain a zombie. |
| 1142 | # spawn a Popen, and delete its reference before it exits |
| 1143 | p = subprocess.Popen([sys.executable, "-c", |
| 1144 | 'import sys, time;' |
| 1145 | 'time.sleep(0.2)'], |
| 1146 | stdout=subprocess.PIPE, |
| 1147 | stderr=subprocess.PIPE) |
Nadeem Vawda | 8605936 | 2011-08-19 05:22:24 +0200 | [diff] [blame] | 1148 | self.addCleanup(p.stdout.close) |
| 1149 | self.addCleanup(p.stderr.close) |
Charles-François Natali | 100df0f | 2011-08-18 17:56:02 +0200 | [diff] [blame] | 1150 | ident = id(p) |
| 1151 | pid = p.pid |
| 1152 | del p |
| 1153 | # check that p is in the active processes list |
| 1154 | self.assertIn(ident, [id(o) for o in subprocess._active]) |
| 1155 | |
Charles-François Natali | 100df0f | 2011-08-18 17:56:02 +0200 | [diff] [blame] | 1156 | def test_leak_fast_process_del_killed(self): |
| 1157 | # Issue #12650: on Unix, if Popen.__del__() was called before the |
| 1158 | # process exited, and the process got killed by a signal, it would never |
| 1159 | # be removed from subprocess._active, which triggered a FD and memory |
| 1160 | # leak. |
| 1161 | # spawn a Popen, delete its reference and kill it |
| 1162 | p = subprocess.Popen([sys.executable, "-c", |
| 1163 | 'import time;' |
| 1164 | 'time.sleep(3)'], |
| 1165 | stdout=subprocess.PIPE, |
| 1166 | stderr=subprocess.PIPE) |
Nadeem Vawda | 8605936 | 2011-08-19 05:22:24 +0200 | [diff] [blame] | 1167 | self.addCleanup(p.stdout.close) |
| 1168 | self.addCleanup(p.stderr.close) |
Charles-François Natali | 100df0f | 2011-08-18 17:56:02 +0200 | [diff] [blame] | 1169 | ident = id(p) |
| 1170 | pid = p.pid |
| 1171 | del p |
| 1172 | os.kill(pid, signal.SIGKILL) |
| 1173 | # check that p is in the active processes list |
| 1174 | self.assertIn(ident, [id(o) for o in subprocess._active]) |
| 1175 | |
| 1176 | # let some time for the process to exit, and create a new Popen: this |
| 1177 | # should trigger the wait() of p |
| 1178 | time.sleep(0.2) |
| 1179 | with self.assertRaises(EnvironmentError) as c: |
| 1180 | with subprocess.Popen(['nonexisting_i_hope'], |
| 1181 | stdout=subprocess.PIPE, |
| 1182 | stderr=subprocess.PIPE) as proc: |
| 1183 | pass |
| 1184 | # p should have been wait()ed on, and removed from the _active list |
| 1185 | self.assertRaises(OSError, os.waitpid, pid, 0) |
| 1186 | self.assertNotIn(ident, [id(o) for o in subprocess._active]) |
| 1187 | |
Charles-François Natali | 2a34eb3 | 2011-08-25 21:20:54 +0200 | [diff] [blame] | 1188 | def test_pipe_cloexec(self): |
| 1189 | # Issue 12786: check that the communication pipes' FDs are set CLOEXEC, |
| 1190 | # and are not inherited by another child process. |
| 1191 | p1 = subprocess.Popen([sys.executable, "-c", |
| 1192 | 'import os;' |
| 1193 | 'os.read(0, 1)' |
| 1194 | ], |
| 1195 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 1196 | stderr=subprocess.PIPE) |
| 1197 | |
| 1198 | p2 = subprocess.Popen([sys.executable, "-c", """if True: |
| 1199 | import os, errno, sys |
| 1200 | for fd in %r: |
| 1201 | try: |
| 1202 | os.close(fd) |
| 1203 | except OSError as e: |
| 1204 | if e.errno != errno.EBADF: |
| 1205 | raise |
| 1206 | else: |
| 1207 | sys.exit(1) |
| 1208 | sys.exit(0) |
| 1209 | """ % [f.fileno() for f in (p1.stdin, p1.stdout, |
| 1210 | p1.stderr)] |
| 1211 | ], |
| 1212 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 1213 | stderr=subprocess.PIPE, close_fds=False) |
| 1214 | p1.communicate('foo') |
| 1215 | _, stderr = p2.communicate() |
| 1216 | |
| 1217 | self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr)) |
| 1218 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1219 | |
Florent Xicluna | bab22a7 | 2010-03-04 19:40:48 +0000 | [diff] [blame] | 1220 | @unittest.skipUnless(mswindows, "Windows specific tests") |
Florent Xicluna | fc4d6d7 | 2010-03-23 14:36:45 +0000 | [diff] [blame] | 1221 | class Win32ProcessTestCase(BaseTestCase): |
Florent Xicluna | ab5e17f | 2010-03-04 21:31:58 +0000 | [diff] [blame] | 1222 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1223 | def test_startupinfo(self): |
| 1224 | # startupinfo argument |
| 1225 | # We uses hardcoded constants, because we do not want to |
| 1226 | # depend on win32all. |
| 1227 | STARTF_USESHOWWINDOW = 1 |
| 1228 | SW_MAXIMIZE = 3 |
| 1229 | startupinfo = subprocess.STARTUPINFO() |
| 1230 | startupinfo.dwFlags = STARTF_USESHOWWINDOW |
| 1231 | startupinfo.wShowWindow = SW_MAXIMIZE |
| 1232 | # Since Python is a console process, it won't be affected |
| 1233 | # by wShowWindow, but the argument should be silently |
| 1234 | # ignored |
| 1235 | subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1236 | startupinfo=startupinfo) |
| 1237 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1238 | def test_creationflags(self): |
| 1239 | # creationflags argument |
| 1240 | CREATE_NEW_CONSOLE = 16 |
| 1241 | sys.stderr.write(" a DOS box should flash briefly ...\n") |
| 1242 | subprocess.call(sys.executable + |
| 1243 | ' -c "import time; time.sleep(0.25)"', |
| 1244 | creationflags=CREATE_NEW_CONSOLE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1245 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1246 | def test_invalid_args(self): |
| 1247 | # invalid arguments should raise ValueError |
| 1248 | self.assertRaises(ValueError, subprocess.call, |
| 1249 | [sys.executable, "-c", |
| 1250 | "import sys; sys.exit(47)"], |
| 1251 | preexec_fn=lambda: 1) |
| 1252 | self.assertRaises(ValueError, subprocess.call, |
| 1253 | [sys.executable, "-c", |
| 1254 | "import sys; sys.exit(47)"], |
| 1255 | stdout=subprocess.PIPE, |
| 1256 | close_fds=True) |
| 1257 | |
| 1258 | def test_close_fds(self): |
| 1259 | # close file descriptors |
| 1260 | rc = subprocess.call([sys.executable, "-c", |
| 1261 | "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1262 | close_fds=True) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1263 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1264 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1265 | def test_shell_sequence(self): |
| 1266 | # Run command through the shell (sequence) |
| 1267 | newenv = os.environ.copy() |
| 1268 | newenv["FRUIT"] = "physalis" |
| 1269 | p = subprocess.Popen(["set"], shell=1, |
| 1270 | stdout=subprocess.PIPE, |
| 1271 | env=newenv) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 1272 | self.addCleanup(p.stdout.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1273 | self.assertIn("physalis", p.stdout.read()) |
Peter Astrand | 81a191b | 2007-05-26 22:18:20 +0000 | [diff] [blame] | 1274 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1275 | def test_shell_string(self): |
| 1276 | # Run command through the shell (string) |
| 1277 | newenv = os.environ.copy() |
| 1278 | newenv["FRUIT"] = "physalis" |
| 1279 | p = subprocess.Popen("set", shell=1, |
| 1280 | stdout=subprocess.PIPE, |
| 1281 | env=newenv) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 1282 | self.addCleanup(p.stdout.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1283 | self.assertIn("physalis", p.stdout.read()) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1284 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1285 | def test_call_string(self): |
| 1286 | # call() function with string argument on Windows |
| 1287 | rc = subprocess.call(sys.executable + |
| 1288 | ' -c "import sys; sys.exit(47)"') |
| 1289 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1290 | |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 1291 | def _kill_process(self, method, *args): |
Florent Xicluna | 400efc2 | 2010-03-07 17:12:23 +0000 | [diff] [blame] | 1292 | # Some win32 buildbot raises EOFError if stdin is inherited |
Antoine Pitrou | dee0097 | 2010-09-24 19:00:29 +0000 | [diff] [blame] | 1293 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 1294 | import sys, time |
| 1295 | sys.stdout.write('x\\n') |
| 1296 | sys.stdout.flush() |
| 1297 | time.sleep(30) |
| 1298 | """], |
| 1299 | stdin=subprocess.PIPE, |
| 1300 | stdout=subprocess.PIPE, |
| 1301 | stderr=subprocess.PIPE) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 1302 | self.addCleanup(p.stdout.close) |
| 1303 | self.addCleanup(p.stderr.close) |
| 1304 | self.addCleanup(p.stdin.close) |
Antoine Pitrou | dee0097 | 2010-09-24 19:00:29 +0000 | [diff] [blame] | 1305 | # Wait for the interpreter to be completely initialized before |
| 1306 | # sending any signal. |
| 1307 | p.stdout.read(1) |
| 1308 | getattr(p, method)(*args) |
Florent Xicluna | 446ff14 | 2010-03-23 15:05:30 +0000 | [diff] [blame] | 1309 | _, stderr = p.communicate() |
| 1310 | self.assertStderrEqual(stderr, '') |
Antoine Pitrou | dee0097 | 2010-09-24 19:00:29 +0000 | [diff] [blame] | 1311 | returncode = p.wait() |
Florent Xicluna | faf1753 | 2010-03-08 10:59:33 +0000 | [diff] [blame] | 1312 | self.assertNotEqual(returncode, 0) |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 1313 | |
Antoine Pitrou | f60845b | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 1314 | def _kill_dead_process(self, method, *args): |
| 1315 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 1316 | import sys, time |
| 1317 | sys.stdout.write('x\\n') |
| 1318 | sys.stdout.flush() |
| 1319 | sys.exit(42) |
| 1320 | """], |
| 1321 | stdin=subprocess.PIPE, |
| 1322 | stdout=subprocess.PIPE, |
| 1323 | stderr=subprocess.PIPE) |
| 1324 | self.addCleanup(p.stdout.close) |
| 1325 | self.addCleanup(p.stderr.close) |
| 1326 | self.addCleanup(p.stdin.close) |
| 1327 | # Wait for the interpreter to be completely initialized before |
| 1328 | # sending any signal. |
| 1329 | p.stdout.read(1) |
| 1330 | # The process should end after this |
| 1331 | time.sleep(1) |
| 1332 | # This shouldn't raise even though the child is now dead |
| 1333 | getattr(p, method)(*args) |
| 1334 | _, stderr = p.communicate() |
| 1335 | self.assertStderrEqual(stderr, b'') |
| 1336 | rc = p.wait() |
| 1337 | self.assertEqual(rc, 42) |
| 1338 | |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 1339 | def test_send_signal(self): |
| 1340 | self._kill_process('send_signal', signal.SIGTERM) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 1341 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1342 | def test_kill(self): |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 1343 | self._kill_process('kill') |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 1344 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1345 | def test_terminate(self): |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 1346 | self._kill_process('terminate') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1347 | |
Antoine Pitrou | f60845b | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 1348 | def test_send_signal_dead(self): |
| 1349 | self._kill_dead_process('send_signal', signal.SIGTERM) |
| 1350 | |
| 1351 | def test_kill_dead(self): |
| 1352 | self._kill_dead_process('kill') |
| 1353 | |
| 1354 | def test_terminate_dead(self): |
| 1355 | self._kill_dead_process('terminate') |
| 1356 | |
Gregory P. Smith | dd7ca24 | 2009-07-04 01:49:29 +0000 | [diff] [blame] | 1357 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1358 | @unittest.skipUnless(getattr(subprocess, '_has_poll', False), |
| 1359 | "poll system call not supported") |
| 1360 | class ProcessTestCaseNoPoll(ProcessTestCase): |
| 1361 | def setUp(self): |
| 1362 | subprocess._has_poll = False |
| 1363 | ProcessTestCase.setUp(self) |
Gregory P. Smith | dd7ca24 | 2009-07-04 01:49:29 +0000 | [diff] [blame] | 1364 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1365 | def tearDown(self): |
| 1366 | subprocess._has_poll = True |
| 1367 | ProcessTestCase.tearDown(self) |
Gregory P. Smith | dd7ca24 | 2009-07-04 01:49:29 +0000 | [diff] [blame] | 1368 | |
| 1369 | |
Gregory P. Smith | cce211f | 2010-03-01 00:05:08 +0000 | [diff] [blame] | 1370 | class HelperFunctionTests(unittest.TestCase): |
Gregory P. Smith | c1baf4a | 2010-03-01 02:53:24 +0000 | [diff] [blame] | 1371 | @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows") |
Gregory P. Smith | cce211f | 2010-03-01 00:05:08 +0000 | [diff] [blame] | 1372 | def test_eintr_retry_call(self): |
| 1373 | record_calls = [] |
| 1374 | def fake_os_func(*args): |
| 1375 | record_calls.append(args) |
| 1376 | if len(record_calls) == 2: |
| 1377 | raise OSError(errno.EINTR, "fake interrupted system call") |
| 1378 | return tuple(reversed(args)) |
| 1379 | |
| 1380 | self.assertEqual((999, 256), |
| 1381 | subprocess._eintr_retry_call(fake_os_func, 256, 999)) |
| 1382 | self.assertEqual([(256, 999)], record_calls) |
| 1383 | # This time there will be an EINTR so it will loop once. |
| 1384 | self.assertEqual((666,), |
| 1385 | subprocess._eintr_retry_call(fake_os_func, 666)) |
| 1386 | self.assertEqual([(256, 999), (666,), (666,)], record_calls) |
| 1387 | |
Tim Golden | 8e4756c | 2010-08-12 11:00:35 +0000 | [diff] [blame] | 1388 | @unittest.skipUnless(mswindows, "mswindows only") |
| 1389 | class CommandsWithSpaces (BaseTestCase): |
| 1390 | |
| 1391 | def setUp(self): |
| 1392 | super(CommandsWithSpaces, self).setUp() |
Berker Peksag | b7c3515 | 2015-09-28 15:37:57 +0300 | [diff] [blame] | 1393 | f, fname = tempfile.mkstemp(".py", "te st") |
Tim Golden | 8e4756c | 2010-08-12 11:00:35 +0000 | [diff] [blame] | 1394 | self.fname = fname.lower () |
| 1395 | os.write(f, b"import sys;" |
| 1396 | b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))" |
| 1397 | ) |
| 1398 | os.close(f) |
| 1399 | |
| 1400 | def tearDown(self): |
| 1401 | os.remove(self.fname) |
| 1402 | super(CommandsWithSpaces, self).tearDown() |
| 1403 | |
| 1404 | def with_spaces(self, *args, **kwargs): |
| 1405 | kwargs['stdout'] = subprocess.PIPE |
| 1406 | p = subprocess.Popen(*args, **kwargs) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 1407 | self.addCleanup(p.stdout.close) |
Tim Golden | 8e4756c | 2010-08-12 11:00:35 +0000 | [diff] [blame] | 1408 | self.assertEqual( |
| 1409 | p.stdout.read ().decode("mbcs"), |
| 1410 | "2 [%r, 'ab cd']" % self.fname |
| 1411 | ) |
| 1412 | |
| 1413 | def test_shell_string_with_spaces(self): |
| 1414 | # call() function with string argument with spaces on Windows |
Brian Curtin | e8c4920 | 2010-08-13 21:01:52 +0000 | [diff] [blame] | 1415 | self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, |
| 1416 | "ab cd"), shell=1) |
Tim Golden | 8e4756c | 2010-08-12 11:00:35 +0000 | [diff] [blame] | 1417 | |
| 1418 | def test_shell_sequence_with_spaces(self): |
| 1419 | # call() function with sequence argument with spaces on Windows |
Brian Curtin | e8c4920 | 2010-08-13 21:01:52 +0000 | [diff] [blame] | 1420 | self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1) |
Tim Golden | 8e4756c | 2010-08-12 11:00:35 +0000 | [diff] [blame] | 1421 | |
| 1422 | def test_noshell_string_with_spaces(self): |
| 1423 | # call() function with string argument with spaces on Windows |
| 1424 | self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, |
| 1425 | "ab cd")) |
| 1426 | |
| 1427 | def test_noshell_sequence_with_spaces(self): |
| 1428 | # call() function with sequence argument with spaces on Windows |
| 1429 | self.with_spaces([sys.executable, self.fname, "ab cd"]) |
| 1430 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1431 | def test_main(): |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1432 | unit_tests = (ProcessTestCase, |
| 1433 | POSIXProcessTestCase, |
| 1434 | Win32ProcessTestCase, |
Gregory P. Smith | cce211f | 2010-03-01 00:05:08 +0000 | [diff] [blame] | 1435 | ProcessTestCaseNoPoll, |
Tim Golden | 8e4756c | 2010-08-12 11:00:35 +0000 | [diff] [blame] | 1436 | HelperFunctionTests, |
| 1437 | CommandsWithSpaces) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1438 | |
Gregory P. Smith | dd7ca24 | 2009-07-04 01:49:29 +0000 | [diff] [blame] | 1439 | test_support.run_unittest(*unit_tests) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1440 | test_support.reap_children() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1441 | |
| 1442 | if __name__ == "__main__": |
| 1443 | test_main() |