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