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