blob: 160ccfeb16b583aec5f3c9996515ade555c59836 [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
Martin Panter1edccfa2016-05-13 01:54:44 +0000288 def test_stderr_redirect_with_no_stdout_redirect(self):
289 # test stderr=STDOUT while stdout=None (not set)
290
291 # - grandchild prints to stderr
292 # - child redirects grandchild's stderr to its stdout
293 # - the parent should get grandchild's stderr in child's stdout
294 p = subprocess.Popen([sys.executable, "-c",
295 'import sys, subprocess;'
296 'rc = subprocess.call([sys.executable, "-c",'
297 ' "import sys;"'
298 ' "sys.stderr.write(\'42\')"],'
299 ' stderr=subprocess.STDOUT);'
300 'sys.exit(rc)'],
301 stdout=subprocess.PIPE,
302 stderr=subprocess.PIPE)
303 stdout, stderr = p.communicate()
304 #NOTE: stdout should get stderr from grandchild
305 self.assertStderrEqual(stdout, b'42')
306 self.assertStderrEqual(stderr, b'') # should be empty
307 self.assertEqual(p.returncode, 0)
308
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000309 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000310 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000311 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000312 'import sys;'
313 'sys.stdout.write("apple");'
314 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000315 'sys.stderr.write("orange")'],
316 stdout=subprocess.PIPE,
317 stderr=subprocess.STDOUT)
Brian Curtind117b562010-11-05 04:09:09 +0000318 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000319 self.assertStderrEqual(p.stdout.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000320
321 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000322 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000323 tf = tempfile.TemporaryFile()
324 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000325 'import sys;'
326 'sys.stdout.write("apple");'
327 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000328 'sys.stderr.write("orange")'],
329 stdout=tf,
330 stderr=tf)
331 p.wait()
332 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000333 self.assertStderrEqual(tf.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000334
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000335 def test_stdout_filedes_of_stdout(self):
336 # stdout is set to 1 (#1531862).
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200337 # To avoid printing the text on stdout, we do something similar to
Ezio Melottiefaad092013-03-11 00:34:33 +0200338 # test_stdout_none (see above). The parent subprocess calls the child
339 # subprocess passing stdout=1, and this test uses stdout=PIPE in
340 # order to capture and check the output of the parent. See #11963.
341 code = ('import sys, subprocess; '
342 'rc = subprocess.call([sys.executable, "-c", '
343 ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), '
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200344 '\'test with stdout=1\'))"], stdout=1); '
345 'assert rc == 18')
Ezio Melottiefaad092013-03-11 00:34:33 +0200346 p = subprocess.Popen([sys.executable, "-c", code],
347 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
348 self.addCleanup(p.stdout.close)
349 self.addCleanup(p.stderr.close)
350 out, err = p.communicate()
351 self.assertEqual(p.returncode, 0, err)
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200352 self.assertEqual(out.rstrip(), 'test with stdout=1')
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000353
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000354 def test_cwd(self):
Guido van Rossume9a0e882007-12-20 17:28:10 +0000355 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000356 # We cannot use os.path.realpath to canonicalize the path,
357 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
358 cwd = os.getcwd()
359 os.chdir(tmpdir)
360 tmpdir = os.getcwd()
361 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000362 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000363 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000364 'sys.stdout.write(os.getcwd())'],
365 stdout=subprocess.PIPE,
366 cwd=tmpdir)
Brian Curtind117b562010-11-05 04:09:09 +0000367 self.addCleanup(p.stdout.close)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000368 normcase = os.path.normcase
369 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000370
371 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000372 newenv = os.environ.copy()
373 newenv["FRUIT"] = "orange"
374 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000375 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000376 'sys.stdout.write(os.getenv("FRUIT"))'],
377 stdout=subprocess.PIPE,
378 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000379 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000380 self.assertEqual(p.stdout.read(), "orange")
381
Peter Astrandcbac93c2005-03-03 20:24:28 +0000382 def test_communicate_stdin(self):
383 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000384 'import sys;'
385 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000386 stdin=subprocess.PIPE)
387 p.communicate("pear")
388 self.assertEqual(p.returncode, 1)
389
390 def test_communicate_stdout(self):
391 p = subprocess.Popen([sys.executable, "-c",
392 'import sys; sys.stdout.write("pineapple")'],
393 stdout=subprocess.PIPE)
394 (stdout, stderr) = p.communicate()
395 self.assertEqual(stdout, "pineapple")
396 self.assertEqual(stderr, None)
397
398 def test_communicate_stderr(self):
399 p = subprocess.Popen([sys.executable, "-c",
400 'import sys; sys.stderr.write("pineapple")'],
401 stderr=subprocess.PIPE)
402 (stdout, stderr) = p.communicate()
403 self.assertEqual(stdout, None)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000404 self.assertStderrEqual(stderr, "pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000405
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000406 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000407 p = subprocess.Popen([sys.executable, "-c",
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000408 'import sys,os;'
409 'sys.stderr.write("pineapple");'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000410 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000411 stdin=subprocess.PIPE,
412 stdout=subprocess.PIPE,
413 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000414 self.addCleanup(p.stdout.close)
415 self.addCleanup(p.stderr.close)
416 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000417 (stdout, stderr) = p.communicate("banana")
418 self.assertEqual(stdout, "banana")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000419 self.assertStderrEqual(stderr, "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000420
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000421 # This test is Linux specific for simplicity to at least have
422 # some coverage. It is not a platform specific bug.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000423 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
424 "Linux specific")
425 # Test for the fd leak reported in http://bugs.python.org/issue2791.
426 def test_communicate_pipe_fd_leak(self):
427 fd_directory = '/proc/%d/fd' % os.getpid()
428 num_fds_before_popen = len(os.listdir(fd_directory))
Martin Panterad6a99c2016-09-10 10:38:22 +0000429 p = subprocess.Popen([sys.executable, "-c", "print('')"],
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000430 stdout=subprocess.PIPE)
431 p.communicate()
432 num_fds_after_communicate = len(os.listdir(fd_directory))
433 del p
434 num_fds_after_destruction = len(os.listdir(fd_directory))
435 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
436 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000437
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000438 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000439 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000440 p = subprocess.Popen([sys.executable, "-c",
441 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000442 (stdout, stderr) = p.communicate()
443 self.assertEqual(stdout, None)
444 self.assertEqual(stderr, None)
445
446 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000447 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000448 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000449 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000450 x, y = os.pipe()
451 if mswindows:
452 pipe_buf = 512
453 else:
454 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
455 os.close(x)
456 os.close(y)
457 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000458 'import sys,os;'
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000459 'sys.stdout.write(sys.stdin.read(47));'
460 'sys.stderr.write("xyz"*%d);'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000461 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000462 stdin=subprocess.PIPE,
463 stdout=subprocess.PIPE,
464 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000465 self.addCleanup(p.stdout.close)
466 self.addCleanup(p.stderr.close)
467 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000468 string_to_write = "abc"*pipe_buf
469 (stdout, stderr) = p.communicate(string_to_write)
470 self.assertEqual(stdout, string_to_write)
471
472 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000473 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000474 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000475 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000476 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000477 stdin=subprocess.PIPE,
478 stdout=subprocess.PIPE,
479 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000480 self.addCleanup(p.stdout.close)
481 self.addCleanup(p.stderr.close)
482 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000483 p.stdin.write("banana")
484 (stdout, stderr) = p.communicate("split")
485 self.assertEqual(stdout, "bananasplit")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000486 self.assertStderrEqual(stderr, "")
Tim Peterse718f612004-10-12 21:51:32 +0000487
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000488 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000489 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000490 'import sys,os;' + SETBINARY +
491 'sys.stdout.write("line1\\n");'
492 'sys.stdout.flush();'
493 'sys.stdout.write("line2\\r");'
494 'sys.stdout.flush();'
495 'sys.stdout.write("line3\\r\\n");'
496 'sys.stdout.flush();'
497 'sys.stdout.write("line4\\r");'
498 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000499 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000500 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000501 'sys.stdout.write("\\nline6");'],
502 stdout=subprocess.PIPE,
503 universal_newlines=1)
Brian Curtind117b562010-11-05 04:09:09 +0000504 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000505 stdout = p.stdout.read()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000506 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000507 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000508 self.assertEqual(stdout,
509 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000510 else:
511 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000512 self.assertEqual(stdout,
513 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000514
515 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000516 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000517 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000518 'import sys,os;' + SETBINARY +
519 'sys.stdout.write("line1\\n");'
520 'sys.stdout.flush();'
521 'sys.stdout.write("line2\\r");'
522 'sys.stdout.flush();'
523 'sys.stdout.write("line3\\r\\n");'
524 'sys.stdout.flush();'
525 'sys.stdout.write("line4\\r");'
526 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000527 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000528 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000529 'sys.stdout.write("\\nline6");'],
530 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
531 universal_newlines=1)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000532 self.addCleanup(p.stdout.close)
533 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000534 (stdout, stderr) = p.communicate()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000535 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000536 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000537 self.assertEqual(stdout,
538 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000539 else:
540 # Interpreter without universal newline support
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000541 self.assertEqual(stdout,
542 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000543
544 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000545 # Make sure we leak no resources
Antoine Pitroub0b3bff2010-09-18 22:42:30 +0000546 if not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000547 max_handles = 1026 # too much for most UNIX systems
548 else:
Antoine Pitroub0b3bff2010-09-18 22:42:30 +0000549 max_handles = 2050 # too much for (at least some) Windows setups
550 handles = []
551 try:
552 for i in range(max_handles):
553 try:
554 handles.append(os.open(test_support.TESTFN,
555 os.O_WRONLY | os.O_CREAT))
556 except OSError as e:
557 if e.errno != errno.EMFILE:
558 raise
559 break
560 else:
561 self.skipTest("failed to reach the file descriptor limit "
562 "(tried %d)" % max_handles)
563 # Close a couple of them (should be enough for a subprocess)
564 for i in range(10):
565 os.close(handles.pop())
566 # Loop creating some subprocesses. If one of them leaks some fds,
567 # the next loop iteration will fail by reaching the max fd limit.
568 for i in range(15):
569 p = subprocess.Popen([sys.executable, "-c",
570 "import sys;"
571 "sys.stdout.write(sys.stdin.read())"],
572 stdin=subprocess.PIPE,
573 stdout=subprocess.PIPE,
574 stderr=subprocess.PIPE)
575 data = p.communicate(b"lime")[0]
576 self.assertEqual(data, b"lime")
577 finally:
578 for h in handles:
579 os.close(h)
Mark Dickinson313dc9b2012-10-07 15:41:38 +0100580 test_support.unlink(test_support.TESTFN)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000581
582 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000583 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
584 '"a b c" d e')
585 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
586 'ab\\"c \\ d')
Gregory P. Smithe047e6d2008-01-19 20:49:02 +0000587 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
588 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000589 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
590 'a\\\\\\b "de fg" h')
591 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
592 'a\\\\\\"b c d')
593 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
594 '"a\\\\b c" d e')
595 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
596 '"a\\\\b\\ c" d e')
Peter Astrand10514a72007-01-13 22:35:35 +0000597 self.assertEqual(subprocess.list2cmdline(['ab', '']),
598 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000599
600
601 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000602 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000603 "-c", "import time; time.sleep(1)"])
604 count = 0
605 while p.poll() is None:
606 time.sleep(0.1)
607 count += 1
608 # We expect that the poll loop probably went around about 10 times,
609 # but, based on system scheduling we can't control, it's possible
610 # poll() never returned None. It "should be" very rare that it
611 # didn't go around at least twice.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000612 self.assertGreaterEqual(count, 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000613 # Subsequent invocations should just return the returncode
614 self.assertEqual(p.poll(), 0)
615
616
617 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000618 p = subprocess.Popen([sys.executable,
619 "-c", "import time; time.sleep(2)"])
620 self.assertEqual(p.wait(), 0)
621 # Subsequent invocations should just return the returncode
622 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000623
Peter Astrand738131d2004-11-30 21:04:45 +0000624
625 def test_invalid_bufsize(self):
626 # an invalid type of the bufsize argument should raise
627 # TypeError.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000628 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +0000629 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +0000630
Georg Brandlf3715d22009-02-14 17:01:36 +0000631 def test_leaking_fds_on_error(self):
632 # see bug #5179: Popen leaks file descriptors to PIPEs if
633 # the child fails to execute; this will eventually exhaust
634 # the maximum number of open fds. 1024 seems a very common
635 # value for that limit, but Windows has 2048, so we loop
636 # 1024 times (each call leaked two fds).
637 for i in range(1024):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000638 # Windows raises IOError. Others raise OSError.
639 with self.assertRaises(EnvironmentError) as c:
Georg Brandlf3715d22009-02-14 17:01:36 +0000640 subprocess.Popen(['nonexisting_i_hope'],
641 stdout=subprocess.PIPE,
642 stderr=subprocess.PIPE)
R David Murraycdd5fc92011-03-13 22:37:18 -0400643 # ignore errors that indicate the command was not found
644 if c.exception.errno not in (errno.ENOENT, errno.EACCES):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000645 raise c.exception
Georg Brandlf3715d22009-02-14 17:01:36 +0000646
Antoine Pitrou33fc7442013-08-30 23:38:13 +0200647 @unittest.skipIf(threading is None, "threading required")
648 def test_double_close_on_error(self):
649 # Issue #18851
650 fds = []
651 def open_fds():
652 for i in range(20):
653 fds.extend(os.pipe())
654 time.sleep(0.001)
655 t = threading.Thread(target=open_fds)
656 t.start()
657 try:
658 with self.assertRaises(EnvironmentError):
659 subprocess.Popen(['nonexisting_i_hope'],
660 stdin=subprocess.PIPE,
661 stdout=subprocess.PIPE,
662 stderr=subprocess.PIPE)
663 finally:
664 t.join()
665 exc = None
666 for fd in fds:
667 # If a double close occurred, some of those fds will
668 # already have been closed by mistake, and os.close()
669 # here will raise.
670 try:
671 os.close(fd)
672 except OSError as e:
673 exc = e
674 if exc is not None:
675 raise exc
676
Tim Golden90374f52010-08-06 13:14:33 +0000677 def test_handles_closed_on_exception(self):
678 # If CreateProcess exits with an error, ensure the
679 # duplicate output handles are released
Berker Peksagb7c35152015-09-28 15:37:57 +0300680 ifhandle, ifname = tempfile.mkstemp()
681 ofhandle, ofname = tempfile.mkstemp()
682 efhandle, efname = tempfile.mkstemp()
Tim Golden90374f52010-08-06 13:14:33 +0000683 try:
684 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
685 stderr=efhandle)
686 except OSError:
687 os.close(ifhandle)
688 os.remove(ifname)
689 os.close(ofhandle)
690 os.remove(ofname)
691 os.close(efhandle)
692 os.remove(efname)
693 self.assertFalse(os.path.exists(ifname))
694 self.assertFalse(os.path.exists(ofname))
695 self.assertFalse(os.path.exists(efname))
696
Ross Lagerwall104c3f12011-04-05 15:24:34 +0200697 def test_communicate_epipe(self):
698 # Issue 10963: communicate() should hide EPIPE
699 p = subprocess.Popen([sys.executable, "-c", 'pass'],
700 stdin=subprocess.PIPE,
701 stdout=subprocess.PIPE,
702 stderr=subprocess.PIPE)
703 self.addCleanup(p.stdout.close)
704 self.addCleanup(p.stderr.close)
705 self.addCleanup(p.stdin.close)
706 p.communicate("x" * 2**20)
707
708 def test_communicate_epipe_only_stdin(self):
709 # Issue 10963: communicate() should hide EPIPE
710 p = subprocess.Popen([sys.executable, "-c", 'pass'],
711 stdin=subprocess.PIPE)
712 self.addCleanup(p.stdin.close)
713 time.sleep(2)
714 p.communicate("x" * 2**20)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000715
Gregory P. Smith9d3b6e92012-11-10 22:49:03 -0800716 # This test is Linux-ish specific for simplicity to at least have
717 # some coverage. It is not a platform specific bug.
718 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
719 "Linux specific")
720 def test_failed_child_execute_fd_leak(self):
721 """Test for the fork() failure fd leak reported in issue16327."""
722 fd_directory = '/proc/%d/fd' % os.getpid()
723 fds_before_popen = os.listdir(fd_directory)
724 with self.assertRaises(PopenTestException):
725 PopenExecuteChildRaises(
726 [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE,
727 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
728
729 # NOTE: This test doesn't verify that the real _execute_child
730 # does not close the file descriptors itself on the way out
731 # during an exception. Code inspection has confirmed that.
732
733 fds_after_exception = os.listdir(fd_directory)
734 self.assertEqual(fds_before_popen, fds_after_exception)
735
736
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000737# context manager
738class _SuppressCoreFiles(object):
739 """Try to prevent core files from being created."""
740 old_limit = None
741
742 def __enter__(self):
743 """Try to save previous ulimit, then set it to (0, 0)."""
Benjamin Peterson8b59c232011-12-10 12:31:42 -0500744 if resource is not None:
745 try:
746 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
747 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
748 except (ValueError, resource.error):
749 pass
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000750
Ronald Oussoren21b44e02010-07-23 12:26:30 +0000751 if sys.platform == 'darwin':
752 # Check if the 'Crash Reporter' on OSX was configured
753 # in 'Developer' mode and warn that it will get triggered
754 # when it is.
755 #
756 # This assumes that this context manager is used in tests
757 # that might trigger the next manager.
758 value = subprocess.Popen(['/usr/bin/defaults', 'read',
759 'com.apple.CrashReporter', 'DialogType'],
760 stdout=subprocess.PIPE).communicate()[0]
761 if value.strip() == b'developer':
762 print "this tests triggers the Crash Reporter, that is intentional"
763 sys.stdout.flush()
764
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000765 def __exit__(self, *args):
766 """Return core file behavior to default."""
767 if self.old_limit is None:
768 return
Benjamin Peterson8b59c232011-12-10 12:31:42 -0500769 if resource is not None:
770 try:
771 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
772 except (ValueError, resource.error):
773 pass
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000774
Victor Stinnerb78fed92011-07-05 14:50:35 +0200775 @unittest.skipUnless(hasattr(signal, 'SIGALRM'),
776 "Requires signal.SIGALRM")
Victor Stinnere7901312011-07-05 14:08:01 +0200777 def test_communicate_eintr(self):
778 # Issue #12493: communicate() should handle EINTR
779 def handler(signum, frame):
780 pass
781 old_handler = signal.signal(signal.SIGALRM, handler)
782 self.addCleanup(signal.signal, signal.SIGALRM, old_handler)
783
784 # the process is running for 2 seconds
785 args = [sys.executable, "-c", 'import time; time.sleep(2)']
786 for stream in ('stdout', 'stderr'):
787 kw = {stream: subprocess.PIPE}
788 with subprocess.Popen(args, **kw) as process:
789 signal.alarm(1)
790 # communicate() will be interrupted by SIGALRM
791 process.communicate()
792
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000793
Florent Xiclunabab22a72010-03-04 19:40:48 +0000794@unittest.skipIf(mswindows, "POSIX specific tests")
Florent Xiclunafc4d6d72010-03-23 14:36:45 +0000795class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaab5e17f2010-03-04 21:31:58 +0000796
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000797 def test_exceptions(self):
798 # caught & re-raised exceptions
799 with self.assertRaises(OSError) as c:
800 p = subprocess.Popen([sys.executable, "-c", ""],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000801 cwd="/this/path/does/not/exist")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000802 # The attribute child_traceback should contain "os.chdir" somewhere.
803 self.assertIn("os.chdir", c.exception.child_traceback)
Tim Peterse718f612004-10-12 21:51:32 +0000804
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000805 def test_run_abort(self):
806 # returncode handles signal termination
807 with _SuppressCoreFiles():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000808 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000809 "import os; os.abort()"])
810 p.wait()
811 self.assertEqual(-p.returncode, signal.SIGABRT)
812
813 def test_preexec(self):
814 # preexec function
815 p = subprocess.Popen([sys.executable, "-c",
816 "import sys, os;"
817 "sys.stdout.write(os.getenv('FRUIT'))"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000818 stdout=subprocess.PIPE,
819 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Brian Curtind117b562010-11-05 04:09:09 +0000820 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000821 self.assertEqual(p.stdout.read(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000822
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800823 class _TestExecuteChildPopen(subprocess.Popen):
824 """Used to test behavior at the end of _execute_child."""
825 def __init__(self, testcase, *args, **kwargs):
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800826 self._testcase = testcase
827 subprocess.Popen.__init__(self, *args, **kwargs)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800828
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800829 def _execute_child(
830 self, args, executable, preexec_fn, close_fds, cwd, env,
Antoine Pitrou33fc7442013-08-30 23:38:13 +0200831 universal_newlines, startupinfo, creationflags, shell, to_close,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800832 p2cread, p2cwrite,
833 c2pread, c2pwrite,
834 errread, errwrite):
835 try:
836 subprocess.Popen._execute_child(
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800837 self, args, executable, preexec_fn, close_fds,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800838 cwd, env, universal_newlines,
Antoine Pitrou33fc7442013-08-30 23:38:13 +0200839 startupinfo, creationflags, shell, to_close,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800840 p2cread, p2cwrite,
841 c2pread, c2pwrite,
842 errread, errwrite)
843 finally:
844 # Open a bunch of file descriptors and verify that
845 # none of them are the same as the ones the Popen
846 # instance is using for stdin/stdout/stderr.
847 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
848 for _ in range(8)]
849 try:
850 for fd in devzero_fds:
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800851 self._testcase.assertNotIn(
852 fd, (p2cwrite, c2pread, errread))
Gregory P. Smith211248b2012-11-11 02:00:49 -0800853 finally:
Richard Oudkerk045e4572013-06-10 16:27:45 +0100854 for fd in devzero_fds:
855 os.close(fd)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800856
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800857 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
858 def test_preexec_errpipe_does_not_double_close_pipes(self):
859 """Issue16140: Don't double close pipes on preexec error."""
860
861 def raise_it():
862 raise RuntimeError("force the _execute_child() errpipe_data path.")
Gregory P. Smith211248b2012-11-11 02:00:49 -0800863
864 with self.assertRaises(RuntimeError):
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800865 self._TestExecuteChildPopen(
866 self, [sys.executable, "-c", "pass"],
867 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
868 stderr=subprocess.PIPE, preexec_fn=raise_it)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800869
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000870 def test_args_string(self):
871 # args is a string
Berker Peksagb7c35152015-09-28 15:37:57 +0300872 f, fname = tempfile.mkstemp()
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000873 os.write(f, "#!/bin/sh\n")
874 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
875 sys.executable)
876 os.close(f)
877 os.chmod(fname, 0o700)
878 p = subprocess.Popen(fname)
879 p.wait()
880 os.remove(fname)
881 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000882
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000883 def test_invalid_args(self):
884 # invalid arguments should raise ValueError
885 self.assertRaises(ValueError, subprocess.call,
886 [sys.executable, "-c",
887 "import sys; sys.exit(47)"],
888 startupinfo=47)
889 self.assertRaises(ValueError, subprocess.call,
890 [sys.executable, "-c",
891 "import sys; sys.exit(47)"],
892 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000893
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000894 def test_shell_sequence(self):
895 # Run command through the shell (sequence)
896 newenv = os.environ.copy()
897 newenv["FRUIT"] = "apple"
898 p = subprocess.Popen(["echo $FRUIT"], shell=1,
899 stdout=subprocess.PIPE,
900 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000901 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000902 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000903
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000904 def test_shell_string(self):
905 # Run command through the shell (string)
906 newenv = os.environ.copy()
907 newenv["FRUIT"] = "apple"
908 p = subprocess.Popen("echo $FRUIT", shell=1,
909 stdout=subprocess.PIPE,
910 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000911 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000912 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000913
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000914 def test_call_string(self):
915 # call() function with string argument on UNIX
Berker Peksagb7c35152015-09-28 15:37:57 +0300916 f, fname = tempfile.mkstemp()
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000917 os.write(f, "#!/bin/sh\n")
918 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
919 sys.executable)
920 os.close(f)
921 os.chmod(fname, 0700)
922 rc = subprocess.call(fname)
923 os.remove(fname)
924 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000925
Stefan Krahe9a6a7d2010-07-19 14:41:08 +0000926 def test_specific_shell(self):
927 # Issue #9265: Incorrect name passed as arg[0].
928 shells = []
929 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
930 for name in ['bash', 'ksh']:
931 sh = os.path.join(prefix, name)
932 if os.path.isfile(sh):
933 shells.append(sh)
934 if not shells: # Will probably work for any shell but csh.
935 self.skipTest("bash or ksh required for this test")
936 sh = '/bin/sh'
937 if os.path.isfile(sh) and not os.path.islink(sh):
938 # Test will fail if /bin/sh is a symlink to csh.
939 shells.append(sh)
940 for sh in shells:
941 p = subprocess.Popen("echo $0", executable=sh, shell=True,
942 stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000943 self.addCleanup(p.stdout.close)
Stefan Krahe9a6a7d2010-07-19 14:41:08 +0000944 self.assertEqual(p.stdout.read().strip(), sh)
945
Florent Xiclunac0838642010-03-07 15:27:39 +0000946 def _kill_process(self, method, *args):
Florent Xiclunacecef392010-03-05 19:31:21 +0000947 # Do not inherit file handles from the parent.
948 # It should fix failures on some platforms.
Antoine Pitroua6166da2010-09-20 11:20:44 +0000949 p = subprocess.Popen([sys.executable, "-c", """if 1:
950 import sys, time
951 sys.stdout.write('x\\n')
952 sys.stdout.flush()
953 time.sleep(30)
954 """],
955 close_fds=True,
956 stdin=subprocess.PIPE,
957 stdout=subprocess.PIPE,
958 stderr=subprocess.PIPE)
959 # Wait for the interpreter to be completely initialized before
960 # sending any signal.
961 p.stdout.read(1)
962 getattr(p, method)(*args)
Florent Xiclunac0838642010-03-07 15:27:39 +0000963 return p
964
Charles-François Natalief2bd672013-01-12 16:52:20 +0100965 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
966 "Due to known OS bug (issue #16762)")
Antoine Pitrouf60845b2012-03-11 19:29:12 +0100967 def _kill_dead_process(self, method, *args):
968 # Do not inherit file handles from the parent.
969 # It should fix failures on some platforms.
970 p = subprocess.Popen([sys.executable, "-c", """if 1:
971 import sys, time
972 sys.stdout.write('x\\n')
973 sys.stdout.flush()
974 """],
975 close_fds=True,
976 stdin=subprocess.PIPE,
977 stdout=subprocess.PIPE,
978 stderr=subprocess.PIPE)
979 # Wait for the interpreter to be completely initialized before
980 # sending any signal.
981 p.stdout.read(1)
982 # The process should end after this
983 time.sleep(1)
984 # This shouldn't raise even though the child is now dead
985 getattr(p, method)(*args)
986 p.communicate()
987
Florent Xiclunac0838642010-03-07 15:27:39 +0000988 def test_send_signal(self):
989 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunafc4d6d72010-03-23 14:36:45 +0000990 _, stderr = p.communicate()
Florent Xicluna3c919cf2010-03-23 19:19:16 +0000991 self.assertIn('KeyboardInterrupt', stderr)
Florent Xicluna446ff142010-03-23 15:05:30 +0000992 self.assertNotEqual(p.wait(), 0)
Christian Heimese74c8f22008-04-19 02:23:57 +0000993
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000994 def test_kill(self):
Florent Xiclunac0838642010-03-07 15:27:39 +0000995 p = self._kill_process('kill')
Florent Xicluna446ff142010-03-23 15:05:30 +0000996 _, stderr = p.communicate()
997 self.assertStderrEqual(stderr, '')
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000998 self.assertEqual(p.wait(), -signal.SIGKILL)
Christian Heimese74c8f22008-04-19 02:23:57 +0000999
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001000 def test_terminate(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001001 p = self._kill_process('terminate')
Florent Xicluna446ff142010-03-23 15:05:30 +00001002 _, stderr = p.communicate()
1003 self.assertStderrEqual(stderr, '')
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001004 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +00001005
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001006 def test_send_signal_dead(self):
1007 # Sending a signal to a dead process
1008 self._kill_dead_process('send_signal', signal.SIGINT)
1009
1010 def test_kill_dead(self):
1011 # Killing a dead process
1012 self._kill_dead_process('kill')
1013
1014 def test_terminate_dead(self):
1015 # Terminating a dead process
1016 self._kill_dead_process('terminate')
1017
Antoine Pitrou91ce0d92011-01-03 18:45:09 +00001018 def check_close_std_fds(self, fds):
1019 # Issue #9905: test that subprocess pipes still work properly with
1020 # some standard fds closed
1021 stdin = 0
1022 newfds = []
1023 for a in fds:
1024 b = os.dup(a)
1025 newfds.append(b)
1026 if a == 0:
1027 stdin = b
1028 try:
1029 for fd in fds:
1030 os.close(fd)
1031 out, err = subprocess.Popen([sys.executable, "-c",
1032 'import sys;'
1033 'sys.stdout.write("apple");'
1034 'sys.stdout.flush();'
1035 'sys.stderr.write("orange")'],
1036 stdin=stdin,
1037 stdout=subprocess.PIPE,
1038 stderr=subprocess.PIPE).communicate()
1039 err = test_support.strip_python_stderr(err)
1040 self.assertEqual((out, err), (b'apple', b'orange'))
1041 finally:
1042 for b, a in zip(newfds, fds):
1043 os.dup2(b, a)
1044 for b in newfds:
1045 os.close(b)
1046
1047 def test_close_fd_0(self):
1048 self.check_close_std_fds([0])
1049
1050 def test_close_fd_1(self):
1051 self.check_close_std_fds([1])
1052
1053 def test_close_fd_2(self):
1054 self.check_close_std_fds([2])
1055
1056 def test_close_fds_0_1(self):
1057 self.check_close_std_fds([0, 1])
1058
1059 def test_close_fds_0_2(self):
1060 self.check_close_std_fds([0, 2])
1061
1062 def test_close_fds_1_2(self):
1063 self.check_close_std_fds([1, 2])
1064
1065 def test_close_fds_0_1_2(self):
1066 # Issue #10806: test that subprocess pipes still work properly with
1067 # all standard fds closed.
1068 self.check_close_std_fds([0, 1, 2])
1069
Ross Lagerwalld8e39012011-07-27 18:54:53 +02001070 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
1071 # open up some temporary files
Berker Peksagb7c35152015-09-28 15:37:57 +03001072 temps = [tempfile.mkstemp() for i in range(3)]
Ross Lagerwalld8e39012011-07-27 18:54:53 +02001073 temp_fds = [fd for fd, fname in temps]
1074 try:
1075 # unlink the files -- we won't need to reopen them
1076 for fd, fname in temps:
1077 os.unlink(fname)
1078
1079 # save a copy of the standard file descriptors
1080 saved_fds = [os.dup(fd) for fd in range(3)]
1081 try:
1082 # duplicate the temp files over the standard fd's 0, 1, 2
1083 for fd, temp_fd in enumerate(temp_fds):
1084 os.dup2(temp_fd, fd)
1085
1086 # write some data to what will become stdin, and rewind
1087 os.write(stdin_no, b"STDIN")
1088 os.lseek(stdin_no, 0, 0)
1089
1090 # now use those files in the given order, so that subprocess
1091 # has to rearrange them in the child
1092 p = subprocess.Popen([sys.executable, "-c",
1093 'import sys; got = sys.stdin.read();'
1094 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
1095 stdin=stdin_no,
1096 stdout=stdout_no,
1097 stderr=stderr_no)
1098 p.wait()
1099
1100 for fd in temp_fds:
1101 os.lseek(fd, 0, 0)
1102
1103 out = os.read(stdout_no, 1024)
1104 err = test_support.strip_python_stderr(os.read(stderr_no, 1024))
1105 finally:
1106 for std, saved in enumerate(saved_fds):
1107 os.dup2(saved, std)
1108 os.close(saved)
1109
1110 self.assertEqual(out, b"got STDIN")
1111 self.assertEqual(err, b"err")
1112
1113 finally:
1114 for fd in temp_fds:
1115 os.close(fd)
1116
1117 # When duping fds, if there arises a situation where one of the fds is
1118 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
1119 # This tests all combinations of this.
1120 def test_swap_fds(self):
1121 self.check_swap_fds(0, 1, 2)
1122 self.check_swap_fds(0, 2, 1)
1123 self.check_swap_fds(1, 0, 2)
1124 self.check_swap_fds(1, 2, 0)
1125 self.check_swap_fds(2, 0, 1)
1126 self.check_swap_fds(2, 1, 0)
1127
Gregory P. Smith312efbc2010-12-14 15:02:53 +00001128 def test_wait_when_sigchild_ignored(self):
1129 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
1130 sigchild_ignore = test_support.findfile("sigchild_ignore.py",
1131 subdir="subprocessdata")
1132 p = subprocess.Popen([sys.executable, sigchild_ignore],
1133 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1134 stdout, stderr = p.communicate()
1135 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
1136 " non-zero with this error:\n%s" % stderr)
1137
Charles-François Natali100df0f2011-08-18 17:56:02 +02001138 def test_zombie_fast_process_del(self):
1139 # Issue #12650: on Unix, if Popen.__del__() was called before the
1140 # process exited, it wouldn't be added to subprocess._active, and would
1141 # remain a zombie.
1142 # spawn a Popen, and delete its reference before it exits
1143 p = subprocess.Popen([sys.executable, "-c",
1144 'import sys, time;'
1145 'time.sleep(0.2)'],
1146 stdout=subprocess.PIPE,
1147 stderr=subprocess.PIPE)
Nadeem Vawda86059362011-08-19 05:22:24 +02001148 self.addCleanup(p.stdout.close)
1149 self.addCleanup(p.stderr.close)
Charles-François Natali100df0f2011-08-18 17:56:02 +02001150 ident = id(p)
1151 pid = p.pid
1152 del p
1153 # check that p is in the active processes list
1154 self.assertIn(ident, [id(o) for o in subprocess._active])
1155
Charles-François Natali100df0f2011-08-18 17:56:02 +02001156 def test_leak_fast_process_del_killed(self):
1157 # Issue #12650: on Unix, if Popen.__del__() was called before the
1158 # process exited, and the process got killed by a signal, it would never
1159 # be removed from subprocess._active, which triggered a FD and memory
1160 # leak.
1161 # spawn a Popen, delete its reference and kill it
1162 p = subprocess.Popen([sys.executable, "-c",
1163 'import time;'
1164 'time.sleep(3)'],
1165 stdout=subprocess.PIPE,
1166 stderr=subprocess.PIPE)
Nadeem Vawda86059362011-08-19 05:22:24 +02001167 self.addCleanup(p.stdout.close)
1168 self.addCleanup(p.stderr.close)
Charles-François Natali100df0f2011-08-18 17:56:02 +02001169 ident = id(p)
1170 pid = p.pid
1171 del p
1172 os.kill(pid, signal.SIGKILL)
1173 # check that p is in the active processes list
1174 self.assertIn(ident, [id(o) for o in subprocess._active])
1175
1176 # let some time for the process to exit, and create a new Popen: this
1177 # should trigger the wait() of p
1178 time.sleep(0.2)
1179 with self.assertRaises(EnvironmentError) as c:
1180 with subprocess.Popen(['nonexisting_i_hope'],
1181 stdout=subprocess.PIPE,
1182 stderr=subprocess.PIPE) as proc:
1183 pass
1184 # p should have been wait()ed on, and removed from the _active list
1185 self.assertRaises(OSError, os.waitpid, pid, 0)
1186 self.assertNotIn(ident, [id(o) for o in subprocess._active])
1187
Charles-François Natali2a34eb32011-08-25 21:20:54 +02001188 def test_pipe_cloexec(self):
1189 # Issue 12786: check that the communication pipes' FDs are set CLOEXEC,
1190 # and are not inherited by another child process.
1191 p1 = subprocess.Popen([sys.executable, "-c",
1192 'import os;'
1193 'os.read(0, 1)'
1194 ],
1195 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1196 stderr=subprocess.PIPE)
1197
1198 p2 = subprocess.Popen([sys.executable, "-c", """if True:
1199 import os, errno, sys
1200 for fd in %r:
1201 try:
1202 os.close(fd)
1203 except OSError as e:
1204 if e.errno != errno.EBADF:
1205 raise
1206 else:
1207 sys.exit(1)
1208 sys.exit(0)
1209 """ % [f.fileno() for f in (p1.stdin, p1.stdout,
1210 p1.stderr)]
1211 ],
1212 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1213 stderr=subprocess.PIPE, close_fds=False)
1214 p1.communicate('foo')
1215 _, stderr = p2.communicate()
1216
1217 self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr))
1218
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001219
Florent Xiclunabab22a72010-03-04 19:40:48 +00001220@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunafc4d6d72010-03-23 14:36:45 +00001221class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaab5e17f2010-03-04 21:31:58 +00001222
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001223 def test_startupinfo(self):
1224 # startupinfo argument
1225 # We uses hardcoded constants, because we do not want to
1226 # depend on win32all.
1227 STARTF_USESHOWWINDOW = 1
1228 SW_MAXIMIZE = 3
1229 startupinfo = subprocess.STARTUPINFO()
1230 startupinfo.dwFlags = STARTF_USESHOWWINDOW
1231 startupinfo.wShowWindow = SW_MAXIMIZE
1232 # Since Python is a console process, it won't be affected
1233 # by wShowWindow, but the argument should be silently
1234 # ignored
1235 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001236 startupinfo=startupinfo)
1237
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001238 def test_creationflags(self):
1239 # creationflags argument
1240 CREATE_NEW_CONSOLE = 16
1241 sys.stderr.write(" a DOS box should flash briefly ...\n")
1242 subprocess.call(sys.executable +
1243 ' -c "import time; time.sleep(0.25)"',
1244 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001245
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001246 def test_invalid_args(self):
1247 # invalid arguments should raise ValueError
1248 self.assertRaises(ValueError, subprocess.call,
1249 [sys.executable, "-c",
1250 "import sys; sys.exit(47)"],
1251 preexec_fn=lambda: 1)
1252 self.assertRaises(ValueError, subprocess.call,
1253 [sys.executable, "-c",
1254 "import sys; sys.exit(47)"],
1255 stdout=subprocess.PIPE,
1256 close_fds=True)
1257
1258 def test_close_fds(self):
1259 # close file descriptors
1260 rc = subprocess.call([sys.executable, "-c",
1261 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001262 close_fds=True)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001263 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001264
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001265 def test_shell_sequence(self):
1266 # Run command through the shell (sequence)
1267 newenv = os.environ.copy()
1268 newenv["FRUIT"] = "physalis"
1269 p = subprocess.Popen(["set"], shell=1,
1270 stdout=subprocess.PIPE,
1271 env=newenv)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001272 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001273 self.assertIn("physalis", p.stdout.read())
Peter Astrand81a191b2007-05-26 22:18:20 +00001274
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001275 def test_shell_string(self):
1276 # Run command through the shell (string)
1277 newenv = os.environ.copy()
1278 newenv["FRUIT"] = "physalis"
1279 p = subprocess.Popen("set", shell=1,
1280 stdout=subprocess.PIPE,
1281 env=newenv)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001282 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001283 self.assertIn("physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001284
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001285 def test_call_string(self):
1286 # call() function with string argument on Windows
1287 rc = subprocess.call(sys.executable +
1288 ' -c "import sys; sys.exit(47)"')
1289 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001290
Florent Xiclunac0838642010-03-07 15:27:39 +00001291 def _kill_process(self, method, *args):
Florent Xicluna400efc22010-03-07 17:12:23 +00001292 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroudee00972010-09-24 19:00:29 +00001293 p = subprocess.Popen([sys.executable, "-c", """if 1:
1294 import sys, time
1295 sys.stdout.write('x\\n')
1296 sys.stdout.flush()
1297 time.sleep(30)
1298 """],
1299 stdin=subprocess.PIPE,
1300 stdout=subprocess.PIPE,
1301 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001302 self.addCleanup(p.stdout.close)
1303 self.addCleanup(p.stderr.close)
1304 self.addCleanup(p.stdin.close)
Antoine Pitroudee00972010-09-24 19:00:29 +00001305 # Wait for the interpreter to be completely initialized before
1306 # sending any signal.
1307 p.stdout.read(1)
1308 getattr(p, method)(*args)
Florent Xicluna446ff142010-03-23 15:05:30 +00001309 _, stderr = p.communicate()
1310 self.assertStderrEqual(stderr, '')
Antoine Pitroudee00972010-09-24 19:00:29 +00001311 returncode = p.wait()
Florent Xiclunafaf17532010-03-08 10:59:33 +00001312 self.assertNotEqual(returncode, 0)
Florent Xiclunac0838642010-03-07 15:27:39 +00001313
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001314 def _kill_dead_process(self, method, *args):
1315 p = subprocess.Popen([sys.executable, "-c", """if 1:
1316 import sys, time
1317 sys.stdout.write('x\\n')
1318 sys.stdout.flush()
1319 sys.exit(42)
1320 """],
1321 stdin=subprocess.PIPE,
1322 stdout=subprocess.PIPE,
1323 stderr=subprocess.PIPE)
1324 self.addCleanup(p.stdout.close)
1325 self.addCleanup(p.stderr.close)
1326 self.addCleanup(p.stdin.close)
1327 # Wait for the interpreter to be completely initialized before
1328 # sending any signal.
1329 p.stdout.read(1)
1330 # The process should end after this
1331 time.sleep(1)
1332 # This shouldn't raise even though the child is now dead
1333 getattr(p, method)(*args)
1334 _, stderr = p.communicate()
1335 self.assertStderrEqual(stderr, b'')
1336 rc = p.wait()
1337 self.assertEqual(rc, 42)
1338
Florent Xiclunac0838642010-03-07 15:27:39 +00001339 def test_send_signal(self):
1340 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimese74c8f22008-04-19 02:23:57 +00001341
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001342 def test_kill(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001343 self._kill_process('kill')
Christian Heimese74c8f22008-04-19 02:23:57 +00001344
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001345 def test_terminate(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001346 self._kill_process('terminate')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001347
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001348 def test_send_signal_dead(self):
1349 self._kill_dead_process('send_signal', signal.SIGTERM)
1350
1351 def test_kill_dead(self):
1352 self._kill_dead_process('kill')
1353
1354 def test_terminate_dead(self):
1355 self._kill_dead_process('terminate')
1356
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001357
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001358@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
1359 "poll system call not supported")
1360class ProcessTestCaseNoPoll(ProcessTestCase):
1361 def setUp(self):
1362 subprocess._has_poll = False
1363 ProcessTestCase.setUp(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001364
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001365 def tearDown(self):
1366 subprocess._has_poll = True
1367 ProcessTestCase.tearDown(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001368
1369
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001370class HelperFunctionTests(unittest.TestCase):
Gregory P. Smithc1baf4a2010-03-01 02:53:24 +00001371 @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001372 def test_eintr_retry_call(self):
1373 record_calls = []
1374 def fake_os_func(*args):
1375 record_calls.append(args)
1376 if len(record_calls) == 2:
1377 raise OSError(errno.EINTR, "fake interrupted system call")
1378 return tuple(reversed(args))
1379
1380 self.assertEqual((999, 256),
1381 subprocess._eintr_retry_call(fake_os_func, 256, 999))
1382 self.assertEqual([(256, 999)], record_calls)
1383 # This time there will be an EINTR so it will loop once.
1384 self.assertEqual((666,),
1385 subprocess._eintr_retry_call(fake_os_func, 666))
1386 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
1387
Tim Golden8e4756c2010-08-12 11:00:35 +00001388@unittest.skipUnless(mswindows, "mswindows only")
1389class CommandsWithSpaces (BaseTestCase):
1390
1391 def setUp(self):
1392 super(CommandsWithSpaces, self).setUp()
Berker Peksagb7c35152015-09-28 15:37:57 +03001393 f, fname = tempfile.mkstemp(".py", "te st")
Tim Golden8e4756c2010-08-12 11:00:35 +00001394 self.fname = fname.lower ()
1395 os.write(f, b"import sys;"
1396 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
1397 )
1398 os.close(f)
1399
1400 def tearDown(self):
1401 os.remove(self.fname)
1402 super(CommandsWithSpaces, self).tearDown()
1403
1404 def with_spaces(self, *args, **kwargs):
1405 kwargs['stdout'] = subprocess.PIPE
1406 p = subprocess.Popen(*args, **kwargs)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001407 self.addCleanup(p.stdout.close)
Tim Golden8e4756c2010-08-12 11:00:35 +00001408 self.assertEqual(
1409 p.stdout.read ().decode("mbcs"),
1410 "2 [%r, 'ab cd']" % self.fname
1411 )
1412
1413 def test_shell_string_with_spaces(self):
1414 # call() function with string argument with spaces on Windows
Brian Curtine8c49202010-08-13 21:01:52 +00001415 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1416 "ab cd"), shell=1)
Tim Golden8e4756c2010-08-12 11:00:35 +00001417
1418 def test_shell_sequence_with_spaces(self):
1419 # call() function with sequence argument with spaces on Windows
Brian Curtine8c49202010-08-13 21:01:52 +00001420 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden8e4756c2010-08-12 11:00:35 +00001421
1422 def test_noshell_string_with_spaces(self):
1423 # call() function with string argument with spaces on Windows
1424 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1425 "ab cd"))
1426
1427 def test_noshell_sequence_with_spaces(self):
1428 # call() function with sequence argument with spaces on Windows
1429 self.with_spaces([sys.executable, self.fname, "ab cd"])
1430
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001431def test_main():
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001432 unit_tests = (ProcessTestCase,
1433 POSIXProcessTestCase,
1434 Win32ProcessTestCase,
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001435 ProcessTestCaseNoPoll,
Tim Golden8e4756c2010-08-12 11:00:35 +00001436 HelperFunctionTests,
1437 CommandsWithSpaces)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001438
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001439 test_support.run_unittest(*unit_tests)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001440 test_support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001441
1442if __name__ == "__main__":
1443 test_main()