blob: 06de10851e83996ee32e7127271a17723593c9bd [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001import unittest
2from test import test_support
3import subprocess
4import sys
5import signal
6import os
Gregory P. Smithcce211f2010-03-01 00:05:08 +00007import errno
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00008import tempfile
9import time
Tim Peters3761e8d2004-10-13 04:07:12 +000010import re
Ezio Melotti8f6a2872010-02-10 21:40:33 +000011import sysconfig
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000012
Benjamin Peterson8b59c232011-12-10 12:31:42 -050013try:
14 import resource
15except ImportError:
16 resource = None
Antoine Pitrou33fc7442013-08-30 23:38:13 +020017try:
18 import threading
19except ImportError:
20 threading = None
Benjamin Peterson8b59c232011-12-10 12:31:42 -050021
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000022mswindows = (sys.platform == "win32")
23
24#
25# Depends on the following external programs: Python
26#
27
28if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000029 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
30 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000031else:
32 SETBINARY = ''
33
Florent Xicluna98e3fc32010-02-27 19:20:50 +000034
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000035class BaseTestCase(unittest.TestCase):
Neal Norwitzb15ac312006-06-29 04:10:08 +000036 def setUp(self):
Tim Peters38ff36c2006-06-30 06:18:39 +000037 # Try to minimize the number of children we have so this test
38 # doesn't crash on some buildbots (Alphas in particular).
Florent Xicluna98e3fc32010-02-27 19:20:50 +000039 test_support.reap_children()
Neal Norwitzb15ac312006-06-29 04:10:08 +000040
Florent Xiclunaab5e17f2010-03-04 21:31:58 +000041 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 Xicluna98e3fc32010-02-27 19:20:50 +000047 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 Norwitzb15ac312006-06-29 04:10:08 +000053
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000054
Gregory P. Smith9d3b6e92012-11-10 22:49:03 -080055class PopenTestException(Exception):
56 pass
57
58
59class 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 Xiclunafc4d6d72010-03-23 14:36:45 +000067class ProcessTestCase(BaseTestCase):
68
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000069 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000070 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000071 rc = subprocess.call([sys.executable, "-c",
72 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000073 self.assertEqual(rc, 47)
74
Peter Astrand454f7672005-01-01 09:36:35 +000075 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 Xicluna98e3fc32010-02-27 19:20:50 +000083 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +000084 subprocess.check_call([sys.executable, "-c",
85 "import sys; sys.exit(47)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +000086 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000087
Gregory P. Smith26576802008-12-05 02:27:01 +000088 def test_check_output(self):
89 # check_output() function with zero return code
90 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000091 [sys.executable, "-c", "print 'BDFL'"])
Ezio Melottiaa980582010-01-23 23:04:36 +000092 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000093
Gregory P. Smith26576802008-12-05 02:27:01 +000094 def test_check_output_nonzero(self):
Gregory P. Smith97f49f42008-12-04 20:21:09 +000095 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +000096 with self.assertRaises(subprocess.CalledProcessError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +000097 subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000098 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +000099 self.assertEqual(c.exception.returncode, 5)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000100
Gregory P. Smith26576802008-12-05 02:27:01 +0000101 def test_check_output_stderr(self):
102 # check_output() function stderr redirected to stdout
103 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000104 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
105 stderr=subprocess.STDOUT)
Ezio Melottiaa980582010-01-23 23:04:36 +0000106 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000107
Gregory P. Smith26576802008-12-05 02:27:01 +0000108 def test_check_output_stdout_arg(self):
109 # check_output() function stderr redirected to stdout
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000110 with self.assertRaises(ValueError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +0000111 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000112 [sys.executable, "-c", "print 'will not be run'"],
113 stdout=sys.stdout)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000114 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000115 self.assertIn('stdout', c.exception.args[0])
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000116
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000117 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000118 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000119 newenv = os.environ.copy()
120 newenv["FRUIT"] = "banana"
121 rc = subprocess.call([sys.executable, "-c",
Florent Xiclunabab22a72010-03-04 19:40:48 +0000122 'import sys, os;'
123 'sys.exit(os.getenv("FRUIT")=="banana")'],
124 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000125 self.assertEqual(rc, 1)
126
Victor Stinner776e69b2011-06-01 01:03:00 +0200127 def test_invalid_args(self):
128 # Popen() called with invalid arguments should raise TypeError
129 # but Popen.__del__ should not complain (issue #12085)
Victor Stinnere9b185f2011-06-01 01:57:48 +0200130 with test_support.captured_stderr() as s:
Victor Stinner776e69b2011-06-01 01:03:00 +0200131 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 Lundh5b3687d2004-10-12 15:26:28 +0000137 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000138 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000139 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
140 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000141 self.addCleanup(p.stdout.close)
142 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000143 p.wait()
144 self.assertEqual(p.stdin, None)
145
146 def test_stdout_none(self):
Ezio Melottiefaad092013-03-11 00:34:33 +0200147 # .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 Curtind117b562010-11-05 04:09:09 +0000164 self.addCleanup(p.stderr.close)
Ezio Melottiefaad092013-03-11 00:34:33 +0200165 out, err = p.communicate()
166 self.assertEqual(p.returncode, 0, err)
167 self.assertEqual(out.rstrip(), 'test_stdout_none')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000168
169 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000170 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000171 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
172 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000173 self.addCleanup(p.stdout.close)
174 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000175 p.wait()
176 self.assertEqual(p.stderr, None)
177
Ezio Melotti8f6a2872010-02-10 21:40:33 +0000178 def test_executable_with_cwd(self):
Florent Xicluna63763702010-03-11 01:50:48 +0000179 python_dir = os.path.dirname(os.path.realpath(sys.executable))
Ezio Melotti8f6a2872010-02-10 21:40:33 +0000180 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 Peters3b01a702004-10-12 22:19:32 +0000193 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000194 p.wait()
195 self.assertEqual(p.returncode, 47)
196
197 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000198 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000199 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 Peters7b759da2004-10-12 22:29:54 +0000208 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000209 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000210 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 Peters7b759da2004-10-12 22:29:54 +0000220 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000221 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 Peters7b759da2004-10-12 22:29:54 +0000231 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000232 p = subprocess.Popen([sys.executable, "-c",
233 'import sys; sys.stdout.write("orange")'],
234 stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000235 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000236 self.assertEqual(p.stdout.read(), "orange")
237
238 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000239 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000240 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000241 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 Peters7b759da2004-10-12 22:29:54 +0000250 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000251 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000252 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 Peters7b759da2004-10-12 22:29:54 +0000260 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000261 p = subprocess.Popen([sys.executable, "-c",
262 'import sys; sys.stderr.write("strawberry")'],
263 stderr=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000264 self.addCleanup(p.stderr.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000265 self.assertStderrEqual(p.stderr.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000266
267 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000268 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000269 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000270 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 Xicluna98e3fc32010-02-27 19:20:50 +0000276 self.assertStderrEqual(os.read(d, 1024), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000277
278 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000279 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000280 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000281 p = subprocess.Popen([sys.executable, "-c",
282 'import sys; sys.stderr.write("strawberry")'],
283 stderr=tf)
284 p.wait()
285 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000286 self.assertStderrEqual(tf.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000287
288 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000289 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000290 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000291 'import sys;'
292 'sys.stdout.write("apple");'
293 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000294 'sys.stderr.write("orange")'],
295 stdout=subprocess.PIPE,
296 stderr=subprocess.STDOUT)
Brian Curtind117b562010-11-05 04:09:09 +0000297 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000298 self.assertStderrEqual(p.stdout.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000299
300 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000301 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000302 tf = tempfile.TemporaryFile()
303 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000304 'import sys;'
305 'sys.stdout.write("apple");'
306 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000307 'sys.stderr.write("orange")'],
308 stdout=tf,
309 stderr=tf)
310 p.wait()
311 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000312 self.assertStderrEqual(tf.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000313
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000314 def test_stdout_filedes_of_stdout(self):
315 # stdout is set to 1 (#1531862).
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200316 # To avoid printing the text on stdout, we do something similar to
Ezio Melottiefaad092013-03-11 00:34:33 +0200317 # 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 Melotti9b9cd4c2013-03-11 03:21:08 +0200323 '\'test with stdout=1\'))"], stdout=1); '
324 'assert rc == 18')
Ezio Melottiefaad092013-03-11 00:34:33 +0200325 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 Melotti9b9cd4c2013-03-11 03:21:08 +0200331 self.assertEqual(out.rstrip(), 'test with stdout=1')
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000332
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000333 def test_cwd(self):
Guido van Rossume9a0e882007-12-20 17:28:10 +0000334 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000335 # 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 Lundh5b3687d2004-10-12 15:26:28 +0000341 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000342 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000343 'sys.stdout.write(os.getcwd())'],
344 stdout=subprocess.PIPE,
345 cwd=tmpdir)
Brian Curtind117b562010-11-05 04:09:09 +0000346 self.addCleanup(p.stdout.close)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000347 normcase = os.path.normcase
348 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000349
350 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000351 newenv = os.environ.copy()
352 newenv["FRUIT"] = "orange"
353 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000354 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000355 'sys.stdout.write(os.getenv("FRUIT"))'],
356 stdout=subprocess.PIPE,
357 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000358 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000359 self.assertEqual(p.stdout.read(), "orange")
360
Peter Astrandcbac93c2005-03-03 20:24:28 +0000361 def test_communicate_stdin(self):
362 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000363 'import sys;'
364 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000365 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 Xicluna98e3fc32010-02-27 19:20:50 +0000383 self.assertStderrEqual(stderr, "pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000384
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000385 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000386 p = subprocess.Popen([sys.executable, "-c",
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000387 'import sys,os;'
388 'sys.stderr.write("pineapple");'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000389 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000390 stdin=subprocess.PIPE,
391 stdout=subprocess.PIPE,
392 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000393 self.addCleanup(p.stdout.close)
394 self.addCleanup(p.stderr.close)
395 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000396 (stdout, stderr) = p.communicate("banana")
397 self.assertEqual(stdout, "banana")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000398 self.assertStderrEqual(stderr, "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000399
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000400 # This test is Linux specific for simplicity to at least have
401 # some coverage. It is not a platform specific bug.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000402 @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. Smith4036fd42008-05-26 20:22:14 +0000416
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000417 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000418 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000419 p = subprocess.Popen([sys.executable, "-c",
420 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000421 (stdout, stderr) = p.communicate()
422 self.assertEqual(stdout, None)
423 self.assertEqual(stderr, None)
424
425 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000426 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000427 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000428 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000429 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 Peterse718f612004-10-12 21:51:32 +0000437 'import sys,os;'
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000438 'sys.stdout.write(sys.stdin.read(47));'
439 'sys.stderr.write("xyz"*%d);'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000440 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000441 stdin=subprocess.PIPE,
442 stdout=subprocess.PIPE,
443 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000444 self.addCleanup(p.stdout.close)
445 self.addCleanup(p.stderr.close)
446 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000447 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 Peters7b759da2004-10-12 22:29:54 +0000452 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000453 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000454 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000455 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000456 stdin=subprocess.PIPE,
457 stdout=subprocess.PIPE,
458 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000459 self.addCleanup(p.stdout.close)
460 self.addCleanup(p.stderr.close)
461 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000462 p.stdin.write("banana")
463 (stdout, stderr) = p.communicate("split")
464 self.assertEqual(stdout, "bananasplit")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000465 self.assertStderrEqual(stderr, "")
Tim Peterse718f612004-10-12 21:51:32 +0000466
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000467 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000468 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000469 '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 Lundh5b3687d2004-10-12 15:26:28 +0000478 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000479 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000480 'sys.stdout.write("\\nline6");'],
481 stdout=subprocess.PIPE,
482 universal_newlines=1)
Brian Curtind117b562010-11-05 04:09:09 +0000483 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000484 stdout = p.stdout.read()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000485 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000486 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000487 self.assertEqual(stdout,
488 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000489 else:
490 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000491 self.assertEqual(stdout,
492 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000493
494 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000495 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000496 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000497 '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 Lundh5b3687d2004-10-12 15:26:28 +0000506 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000507 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000508 'sys.stdout.write("\\nline6");'],
509 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
510 universal_newlines=1)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000511 self.addCleanup(p.stdout.close)
512 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000513 (stdout, stderr) = p.communicate()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000514 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000515 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000516 self.assertEqual(stdout,
517 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000518 else:
519 # Interpreter without universal newline support
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000520 self.assertEqual(stdout,
521 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000522
523 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000524 # Make sure we leak no resources
Antoine Pitroub0b3bff2010-09-18 22:42:30 +0000525 if not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000526 max_handles = 1026 # too much for most UNIX systems
527 else:
Antoine Pitroub0b3bff2010-09-18 22:42:30 +0000528 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 Dickinson313dc9b2012-10-07 15:41:38 +0100559 test_support.unlink(test_support.TESTFN)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000560
561 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000562 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. Smithe047e6d2008-01-19 20:49:02 +0000566 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
567 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000568 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 Astrand10514a72007-01-13 22:35:35 +0000576 self.assertEqual(subprocess.list2cmdline(['ab', '']),
577 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000578
579
580 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000581 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000582 "-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 Xicluna98e3fc32010-02-27 19:20:50 +0000591 self.assertGreaterEqual(count, 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000592 # Subsequent invocations should just return the returncode
593 self.assertEqual(p.poll(), 0)
594
595
596 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000597 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 Peterse718f612004-10-12 21:51:32 +0000602
Peter Astrand738131d2004-11-30 21:04:45 +0000603
604 def test_invalid_bufsize(self):
605 # an invalid type of the bufsize argument should raise
606 # TypeError.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000607 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +0000608 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +0000609
Georg Brandlf3715d22009-02-14 17:01:36 +0000610 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 Xicluna98e3fc32010-02-27 19:20:50 +0000617 # Windows raises IOError. Others raise OSError.
618 with self.assertRaises(EnvironmentError) as c:
Georg Brandlf3715d22009-02-14 17:01:36 +0000619 subprocess.Popen(['nonexisting_i_hope'],
620 stdout=subprocess.PIPE,
621 stderr=subprocess.PIPE)
R David Murraycdd5fc92011-03-13 22:37:18 -0400622 # ignore errors that indicate the command was not found
623 if c.exception.errno not in (errno.ENOENT, errno.EACCES):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000624 raise c.exception
Georg Brandlf3715d22009-02-14 17:01:36 +0000625
Antoine Pitrou33fc7442013-08-30 23:38:13 +0200626 @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 Golden90374f52010-08-06 13:14:33 +0000656 def test_handles_closed_on_exception(self):
657 # If CreateProcess exits with an error, ensure the
658 # duplicate output handles are released
Berker Peksagb7c35152015-09-28 15:37:57 +0300659 ifhandle, ifname = tempfile.mkstemp()
660 ofhandle, ofname = tempfile.mkstemp()
661 efhandle, efname = tempfile.mkstemp()
Tim Golden90374f52010-08-06 13:14:33 +0000662 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 Lagerwall104c3f12011-04-05 15:24:34 +0200676 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 Xicluna98e3fc32010-02-27 19:20:50 +0000694
Gregory P. Smith9d3b6e92012-11-10 22:49:03 -0800695 # 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 Xicluna98e3fc32010-02-27 19:20:50 +0000716# context manager
717class _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 Peterson8b59c232011-12-10 12:31:42 -0500723 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 Xicluna98e3fc32010-02-27 19:20:50 +0000729
Ronald Oussoren21b44e02010-07-23 12:26:30 +0000730 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 Xicluna98e3fc32010-02-27 19:20:50 +0000744 def __exit__(self, *args):
745 """Return core file behavior to default."""
746 if self.old_limit is None:
747 return
Benjamin Peterson8b59c232011-12-10 12:31:42 -0500748 if resource is not None:
749 try:
750 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
751 except (ValueError, resource.error):
752 pass
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000753
Victor Stinnerb78fed92011-07-05 14:50:35 +0200754 @unittest.skipUnless(hasattr(signal, 'SIGALRM'),
755 "Requires signal.SIGALRM")
Victor Stinnere7901312011-07-05 14:08:01 +0200756 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 Xicluna98e3fc32010-02-27 19:20:50 +0000772
Florent Xiclunabab22a72010-03-04 19:40:48 +0000773@unittest.skipIf(mswindows, "POSIX specific tests")
Florent Xiclunafc4d6d72010-03-23 14:36:45 +0000774class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaab5e17f2010-03-04 21:31:58 +0000775
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000776 def test_exceptions(self):
777 # caught & re-raised exceptions
778 with self.assertRaises(OSError) as c:
779 p = subprocess.Popen([sys.executable, "-c", ""],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000780 cwd="/this/path/does/not/exist")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000781 # The attribute child_traceback should contain "os.chdir" somewhere.
782 self.assertIn("os.chdir", c.exception.child_traceback)
Tim Peterse718f612004-10-12 21:51:32 +0000783
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000784 def test_run_abort(self):
785 # returncode handles signal termination
786 with _SuppressCoreFiles():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000787 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000788 "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 Lundh5b3687d2004-10-12 15:26:28 +0000797 stdout=subprocess.PIPE,
798 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Brian Curtind117b562010-11-05 04:09:09 +0000799 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000800 self.assertEqual(p.stdout.read(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000801
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800802 class _TestExecuteChildPopen(subprocess.Popen):
803 """Used to test behavior at the end of _execute_child."""
804 def __init__(self, testcase, *args, **kwargs):
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800805 self._testcase = testcase
806 subprocess.Popen.__init__(self, *args, **kwargs)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800807
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800808 def _execute_child(
809 self, args, executable, preexec_fn, close_fds, cwd, env,
Antoine Pitrou33fc7442013-08-30 23:38:13 +0200810 universal_newlines, startupinfo, creationflags, shell, to_close,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800811 p2cread, p2cwrite,
812 c2pread, c2pwrite,
813 errread, errwrite):
814 try:
815 subprocess.Popen._execute_child(
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800816 self, args, executable, preexec_fn, close_fds,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800817 cwd, env, universal_newlines,
Antoine Pitrou33fc7442013-08-30 23:38:13 +0200818 startupinfo, creationflags, shell, to_close,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800819 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. Smithf047ba82012-11-11 09:49:02 -0800830 self._testcase.assertNotIn(
831 fd, (p2cwrite, c2pread, errread))
Gregory P. Smith211248b2012-11-11 02:00:49 -0800832 finally:
Richard Oudkerk045e4572013-06-10 16:27:45 +0100833 for fd in devzero_fds:
834 os.close(fd)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800835
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800836 @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. Smith211248b2012-11-11 02:00:49 -0800842
843 with self.assertRaises(RuntimeError):
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800844 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. Smith211248b2012-11-11 02:00:49 -0800848
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000849 def test_args_string(self):
850 # args is a string
Berker Peksagb7c35152015-09-28 15:37:57 +0300851 f, fname = tempfile.mkstemp()
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000852 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 Lundh5b3687d2004-10-12 15:26:28 +0000861
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000862 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 Lundh5b3687d2004-10-12 15:26:28 +0000872
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000873 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 Curtind117b562010-11-05 04:09:09 +0000880 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000881 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000882
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000883 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 Curtind117b562010-11-05 04:09:09 +0000890 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000891 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000892
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000893 def test_call_string(self):
894 # call() function with string argument on UNIX
Berker Peksagb7c35152015-09-28 15:37:57 +0300895 f, fname = tempfile.mkstemp()
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000896 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 Lundh5b3687d2004-10-12 15:26:28 +0000904
Stefan Krahe9a6a7d2010-07-19 14:41:08 +0000905 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 Curtind117b562010-11-05 04:09:09 +0000922 self.addCleanup(p.stdout.close)
Stefan Krahe9a6a7d2010-07-19 14:41:08 +0000923 self.assertEqual(p.stdout.read().strip(), sh)
924
Florent Xiclunac0838642010-03-07 15:27:39 +0000925 def _kill_process(self, method, *args):
Florent Xiclunacecef392010-03-05 19:31:21 +0000926 # Do not inherit file handles from the parent.
927 # It should fix failures on some platforms.
Antoine Pitroua6166da2010-09-20 11:20:44 +0000928 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 Xiclunac0838642010-03-07 15:27:39 +0000942 return p
943
Charles-François Natalief2bd672013-01-12 16:52:20 +0100944 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
945 "Due to known OS bug (issue #16762)")
Antoine Pitrouf60845b2012-03-11 19:29:12 +0100946 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 Xiclunac0838642010-03-07 15:27:39 +0000967 def test_send_signal(self):
968 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunafc4d6d72010-03-23 14:36:45 +0000969 _, stderr = p.communicate()
Florent Xicluna3c919cf2010-03-23 19:19:16 +0000970 self.assertIn('KeyboardInterrupt', stderr)
Florent Xicluna446ff142010-03-23 15:05:30 +0000971 self.assertNotEqual(p.wait(), 0)
Christian Heimese74c8f22008-04-19 02:23:57 +0000972
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000973 def test_kill(self):
Florent Xiclunac0838642010-03-07 15:27:39 +0000974 p = self._kill_process('kill')
Florent Xicluna446ff142010-03-23 15:05:30 +0000975 _, stderr = p.communicate()
976 self.assertStderrEqual(stderr, '')
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000977 self.assertEqual(p.wait(), -signal.SIGKILL)
Christian Heimese74c8f22008-04-19 02:23:57 +0000978
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000979 def test_terminate(self):
Florent Xiclunac0838642010-03-07 15:27:39 +0000980 p = self._kill_process('terminate')
Florent Xicluna446ff142010-03-23 15:05:30 +0000981 _, stderr = p.communicate()
982 self.assertStderrEqual(stderr, '')
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000983 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000984
Antoine Pitrouf60845b2012-03-11 19:29:12 +0100985 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 Pitrou91ce0d92011-01-03 18:45:09 +0000997 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 Lagerwalld8e39012011-07-27 18:54:53 +02001049 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
1050 # open up some temporary files
Berker Peksagb7c35152015-09-28 15:37:57 +03001051 temps = [tempfile.mkstemp() for i in range(3)]
Ross Lagerwalld8e39012011-07-27 18:54:53 +02001052 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. Smith312efbc2010-12-14 15:02:53 +00001107 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 Natali100df0f2011-08-18 17:56:02 +02001117 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 Vawda86059362011-08-19 05:22:24 +02001127 self.addCleanup(p.stdout.close)
1128 self.addCleanup(p.stderr.close)
Charles-François Natali100df0f2011-08-18 17:56:02 +02001129 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 Natali100df0f2011-08-18 17:56:02 +02001135 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 Vawda86059362011-08-19 05:22:24 +02001146 self.addCleanup(p.stdout.close)
1147 self.addCleanup(p.stderr.close)
Charles-François Natali100df0f2011-08-18 17:56:02 +02001148 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 Natali2a34eb32011-08-25 21:20:54 +02001167 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 Xicluna98e3fc32010-02-27 19:20:50 +00001198
Florent Xiclunabab22a72010-03-04 19:40:48 +00001199@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunafc4d6d72010-03-23 14:36:45 +00001200class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaab5e17f2010-03-04 21:31:58 +00001201
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001202 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 Lundh5b3687d2004-10-12 15:26:28 +00001215 startupinfo=startupinfo)
1216
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001217 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 Lundh5b3687d2004-10-12 15:26:28 +00001224
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001225 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 Lundh5b3687d2004-10-12 15:26:28 +00001241 close_fds=True)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001242 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001243
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001244 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 Curtin7fe045e2010-11-05 17:19:38 +00001251 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001252 self.assertIn("physalis", p.stdout.read())
Peter Astrand81a191b2007-05-26 22:18:20 +00001253
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001254 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 Curtin7fe045e2010-11-05 17:19:38 +00001261 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001262 self.assertIn("physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001263
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001264 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 Lundh5b3687d2004-10-12 15:26:28 +00001269
Florent Xiclunac0838642010-03-07 15:27:39 +00001270 def _kill_process(self, method, *args):
Florent Xicluna400efc22010-03-07 17:12:23 +00001271 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroudee00972010-09-24 19:00:29 +00001272 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 Curtin7fe045e2010-11-05 17:19:38 +00001281 self.addCleanup(p.stdout.close)
1282 self.addCleanup(p.stderr.close)
1283 self.addCleanup(p.stdin.close)
Antoine Pitroudee00972010-09-24 19:00:29 +00001284 # Wait for the interpreter to be completely initialized before
1285 # sending any signal.
1286 p.stdout.read(1)
1287 getattr(p, method)(*args)
Florent Xicluna446ff142010-03-23 15:05:30 +00001288 _, stderr = p.communicate()
1289 self.assertStderrEqual(stderr, '')
Antoine Pitroudee00972010-09-24 19:00:29 +00001290 returncode = p.wait()
Florent Xiclunafaf17532010-03-08 10:59:33 +00001291 self.assertNotEqual(returncode, 0)
Florent Xiclunac0838642010-03-07 15:27:39 +00001292
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001293 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 Xiclunac0838642010-03-07 15:27:39 +00001318 def test_send_signal(self):
1319 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimese74c8f22008-04-19 02:23:57 +00001320
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001321 def test_kill(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001322 self._kill_process('kill')
Christian Heimese74c8f22008-04-19 02:23:57 +00001323
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001324 def test_terminate(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001325 self._kill_process('terminate')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001326
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001327 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. Smithdd7ca242009-07-04 01:49:29 +00001336
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001337@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
1338 "poll system call not supported")
1339class ProcessTestCaseNoPoll(ProcessTestCase):
1340 def setUp(self):
1341 subprocess._has_poll = False
1342 ProcessTestCase.setUp(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001343
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001344 def tearDown(self):
1345 subprocess._has_poll = True
1346 ProcessTestCase.tearDown(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001347
1348
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001349class HelperFunctionTests(unittest.TestCase):
Gregory P. Smithc1baf4a2010-03-01 02:53:24 +00001350 @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001351 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 Golden8e4756c2010-08-12 11:00:35 +00001367@unittest.skipUnless(mswindows, "mswindows only")
1368class CommandsWithSpaces (BaseTestCase):
1369
1370 def setUp(self):
1371 super(CommandsWithSpaces, self).setUp()
Berker Peksagb7c35152015-09-28 15:37:57 +03001372 f, fname = tempfile.mkstemp(".py", "te st")
Tim Golden8e4756c2010-08-12 11:00:35 +00001373 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 Curtin7fe045e2010-11-05 17:19:38 +00001386 self.addCleanup(p.stdout.close)
Tim Golden8e4756c2010-08-12 11:00:35 +00001387 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 Curtine8c49202010-08-13 21:01:52 +00001394 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1395 "ab cd"), shell=1)
Tim Golden8e4756c2010-08-12 11:00:35 +00001396
1397 def test_shell_sequence_with_spaces(self):
1398 # call() function with sequence argument with spaces on Windows
Brian Curtine8c49202010-08-13 21:01:52 +00001399 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden8e4756c2010-08-12 11:00:35 +00001400
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 Lundh5b3687d2004-10-12 15:26:28 +00001410def test_main():
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001411 unit_tests = (ProcessTestCase,
1412 POSIXProcessTestCase,
1413 Win32ProcessTestCase,
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001414 ProcessTestCaseNoPoll,
Tim Golden8e4756c2010-08-12 11:00:35 +00001415 HelperFunctionTests,
1416 CommandsWithSpaces)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001417
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001418 test_support.run_unittest(*unit_tests)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001419 test_support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001420
1421if __name__ == "__main__":
1422 test_main()