blob: 0efcdbf25347fb1cdab5b05246bd2be6d23ea571 [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
35try:
36 mkstemp = tempfile.mkstemp
37except AttributeError:
38 # tempfile.mkstemp is not available
39 def mkstemp():
40 """Replacement for mkstemp, calling mktemp."""
41 fname = tempfile.mktemp()
42 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
43
Tim Peters3761e8d2004-10-13 04:07:12 +000044
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000045class BaseTestCase(unittest.TestCase):
Neal Norwitzb15ac312006-06-29 04:10:08 +000046 def setUp(self):
Tim Peters38ff36c2006-06-30 06:18:39 +000047 # Try to minimize the number of children we have so this test
48 # doesn't crash on some buildbots (Alphas in particular).
Florent Xicluna98e3fc32010-02-27 19:20:50 +000049 test_support.reap_children()
Neal Norwitzb15ac312006-06-29 04:10:08 +000050
Florent Xiclunaab5e17f2010-03-04 21:31:58 +000051 def tearDown(self):
52 for inst in subprocess._active:
53 inst.wait()
54 subprocess._cleanup()
55 self.assertFalse(subprocess._active, "subprocess._active not empty")
56
Florent Xicluna98e3fc32010-02-27 19:20:50 +000057 def assertStderrEqual(self, stderr, expected, msg=None):
58 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
59 # shutdown time. That frustrates tests trying to check stderr produced
60 # from a spawned Python process.
61 actual = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
62 self.assertEqual(actual, expected, msg)
Neal Norwitzb15ac312006-06-29 04:10:08 +000063
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000064
Gregory P. Smith9d3b6e92012-11-10 22:49:03 -080065class PopenTestException(Exception):
66 pass
67
68
69class PopenExecuteChildRaises(subprocess.Popen):
70 """Popen subclass for testing cleanup of subprocess.PIPE filehandles when
71 _execute_child fails.
72 """
73 def _execute_child(self, *args, **kwargs):
74 raise PopenTestException("Forced Exception for Test")
75
76
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000077class ProcessTestCase(BaseTestCase):
78
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000079 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000080 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000081 rc = subprocess.call([sys.executable, "-c",
82 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000083 self.assertEqual(rc, 47)
84
Peter Astrand454f7672005-01-01 09:36:35 +000085 def test_check_call_zero(self):
86 # check_call() function with zero return code
87 rc = subprocess.check_call([sys.executable, "-c",
88 "import sys; sys.exit(0)"])
89 self.assertEqual(rc, 0)
90
91 def test_check_call_nonzero(self):
92 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +000093 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +000094 subprocess.check_call([sys.executable, "-c",
95 "import sys; sys.exit(47)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +000096 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000097
Gregory P. Smith26576802008-12-05 02:27:01 +000098 def test_check_output(self):
99 # check_output() function with zero return code
100 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000101 [sys.executable, "-c", "print 'BDFL'"])
Ezio Melottiaa980582010-01-23 23:04:36 +0000102 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000103
Gregory P. Smith26576802008-12-05 02:27:01 +0000104 def test_check_output_nonzero(self):
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000105 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000106 with self.assertRaises(subprocess.CalledProcessError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +0000107 subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000108 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000109 self.assertEqual(c.exception.returncode, 5)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000110
Gregory P. Smith26576802008-12-05 02:27:01 +0000111 def test_check_output_stderr(self):
112 # check_output() function stderr redirected to stdout
113 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000114 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
115 stderr=subprocess.STDOUT)
Ezio Melottiaa980582010-01-23 23:04:36 +0000116 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000117
Gregory P. Smith26576802008-12-05 02:27:01 +0000118 def test_check_output_stdout_arg(self):
119 # check_output() function stderr redirected to stdout
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000120 with self.assertRaises(ValueError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +0000121 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000122 [sys.executable, "-c", "print 'will not be run'"],
123 stdout=sys.stdout)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000124 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000125 self.assertIn('stdout', c.exception.args[0])
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000126
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000127 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000128 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000129 newenv = os.environ.copy()
130 newenv["FRUIT"] = "banana"
131 rc = subprocess.call([sys.executable, "-c",
Florent Xiclunabab22a72010-03-04 19:40:48 +0000132 'import sys, os;'
133 'sys.exit(os.getenv("FRUIT")=="banana")'],
134 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000135 self.assertEqual(rc, 1)
136
Victor Stinner776e69b2011-06-01 01:03:00 +0200137 def test_invalid_args(self):
138 # Popen() called with invalid arguments should raise TypeError
139 # but Popen.__del__ should not complain (issue #12085)
Victor Stinnere9b185f2011-06-01 01:57:48 +0200140 with test_support.captured_stderr() as s:
Victor Stinner776e69b2011-06-01 01:03:00 +0200141 self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
142 argcount = subprocess.Popen.__init__.__code__.co_argcount
143 too_many_args = [0] * (argcount + 1)
144 self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
145 self.assertEqual(s.getvalue(), '')
146
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000147 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000148 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000149 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
150 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000151 self.addCleanup(p.stdout.close)
152 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000153 p.wait()
154 self.assertEqual(p.stdin, None)
155
156 def test_stdout_none(self):
Ezio Melottiefaad092013-03-11 00:34:33 +0200157 # .stdout is None when not redirected, and the child's stdout will
158 # be inherited from the parent. In order to test this we run a
159 # subprocess in a subprocess:
160 # this_test
161 # \-- subprocess created by this test (parent)
162 # \-- subprocess created by the parent subprocess (child)
163 # The parent doesn't specify stdout, so the child will use the
164 # parent's stdout. This test checks that the message printed by the
165 # child goes to the parent stdout. The parent also checks that the
166 # child's stdout is None. See #11963.
167 code = ('import sys; from subprocess import Popen, PIPE;'
168 'p = Popen([sys.executable, "-c", "print \'test_stdout_none\'"],'
169 ' stdin=PIPE, stderr=PIPE);'
170 'p.wait(); assert p.stdout is None;')
171 p = subprocess.Popen([sys.executable, "-c", code],
172 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
173 self.addCleanup(p.stdout.close)
Brian Curtind117b562010-11-05 04:09:09 +0000174 self.addCleanup(p.stderr.close)
Ezio Melottiefaad092013-03-11 00:34:33 +0200175 out, err = p.communicate()
176 self.assertEqual(p.returncode, 0, err)
177 self.assertEqual(out.rstrip(), 'test_stdout_none')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000178
179 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000180 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000181 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
182 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000183 self.addCleanup(p.stdout.close)
184 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000185 p.wait()
186 self.assertEqual(p.stderr, None)
187
Ezio Melotti8f6a2872010-02-10 21:40:33 +0000188 def test_executable_with_cwd(self):
Florent Xicluna63763702010-03-11 01:50:48 +0000189 python_dir = os.path.dirname(os.path.realpath(sys.executable))
Ezio Melotti8f6a2872010-02-10 21:40:33 +0000190 p = subprocess.Popen(["somethingyoudonthave", "-c",
191 "import sys; sys.exit(47)"],
192 executable=sys.executable, cwd=python_dir)
193 p.wait()
194 self.assertEqual(p.returncode, 47)
195
196 @unittest.skipIf(sysconfig.is_python_build(),
197 "need an installed Python. See #7774")
198 def test_executable_without_cwd(self):
199 # For a normal installation, it should work without 'cwd'
200 # argument. For test runs in the build directory, see #7774.
201 p = subprocess.Popen(["somethingyoudonthave", "-c",
202 "import sys; sys.exit(47)"],
Tim Peters3b01a702004-10-12 22:19:32 +0000203 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000204 p.wait()
205 self.assertEqual(p.returncode, 47)
206
207 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000208 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000209 p = subprocess.Popen([sys.executable, "-c",
210 'import sys; sys.exit(sys.stdin.read() == "pear")'],
211 stdin=subprocess.PIPE)
212 p.stdin.write("pear")
213 p.stdin.close()
214 p.wait()
215 self.assertEqual(p.returncode, 1)
216
217 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000218 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000219 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000220 d = tf.fileno()
221 os.write(d, "pear")
222 os.lseek(d, 0, 0)
223 p = subprocess.Popen([sys.executable, "-c",
224 'import sys; sys.exit(sys.stdin.read() == "pear")'],
225 stdin=d)
226 p.wait()
227 self.assertEqual(p.returncode, 1)
228
229 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000230 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000231 tf = tempfile.TemporaryFile()
232 tf.write("pear")
233 tf.seek(0)
234 p = subprocess.Popen([sys.executable, "-c",
235 'import sys; sys.exit(sys.stdin.read() == "pear")'],
236 stdin=tf)
237 p.wait()
238 self.assertEqual(p.returncode, 1)
239
240 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000241 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000242 p = subprocess.Popen([sys.executable, "-c",
243 'import sys; sys.stdout.write("orange")'],
244 stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000245 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000246 self.assertEqual(p.stdout.read(), "orange")
247
248 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000249 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000250 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000251 d = tf.fileno()
252 p = subprocess.Popen([sys.executable, "-c",
253 'import sys; sys.stdout.write("orange")'],
254 stdout=d)
255 p.wait()
256 os.lseek(d, 0, 0)
257 self.assertEqual(os.read(d, 1024), "orange")
258
259 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000260 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000261 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000262 p = subprocess.Popen([sys.executable, "-c",
263 'import sys; sys.stdout.write("orange")'],
264 stdout=tf)
265 p.wait()
266 tf.seek(0)
267 self.assertEqual(tf.read(), "orange")
268
269 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000270 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000271 p = subprocess.Popen([sys.executable, "-c",
272 'import sys; sys.stderr.write("strawberry")'],
273 stderr=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000274 self.addCleanup(p.stderr.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000275 self.assertStderrEqual(p.stderr.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000276
277 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000278 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000279 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000280 d = tf.fileno()
281 p = subprocess.Popen([sys.executable, "-c",
282 'import sys; sys.stderr.write("strawberry")'],
283 stderr=d)
284 p.wait()
285 os.lseek(d, 0, 0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000286 self.assertStderrEqual(os.read(d, 1024), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000287
288 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000289 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000290 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000291 p = subprocess.Popen([sys.executable, "-c",
292 'import sys; sys.stderr.write("strawberry")'],
293 stderr=tf)
294 p.wait()
295 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000296 self.assertStderrEqual(tf.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000297
298 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000299 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000300 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000301 'import sys;'
302 'sys.stdout.write("apple");'
303 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000304 'sys.stderr.write("orange")'],
305 stdout=subprocess.PIPE,
306 stderr=subprocess.STDOUT)
Brian Curtind117b562010-11-05 04:09:09 +0000307 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000308 self.assertStderrEqual(p.stdout.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000309
310 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000311 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000312 tf = tempfile.TemporaryFile()
313 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000314 'import sys;'
315 'sys.stdout.write("apple");'
316 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000317 'sys.stderr.write("orange")'],
318 stdout=tf,
319 stderr=tf)
320 p.wait()
321 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000322 self.assertStderrEqual(tf.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000323
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000324 def test_stdout_filedes_of_stdout(self):
325 # stdout is set to 1 (#1531862).
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200326 # To avoid printing the text on stdout, we do something similar to
Ezio Melottiefaad092013-03-11 00:34:33 +0200327 # test_stdout_none (see above). The parent subprocess calls the child
328 # subprocess passing stdout=1, and this test uses stdout=PIPE in
329 # order to capture and check the output of the parent. See #11963.
330 code = ('import sys, subprocess; '
331 'rc = subprocess.call([sys.executable, "-c", '
332 ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), '
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200333 '\'test with stdout=1\'))"], stdout=1); '
334 'assert rc == 18')
Ezio Melottiefaad092013-03-11 00:34:33 +0200335 p = subprocess.Popen([sys.executable, "-c", code],
336 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
337 self.addCleanup(p.stdout.close)
338 self.addCleanup(p.stderr.close)
339 out, err = p.communicate()
340 self.assertEqual(p.returncode, 0, err)
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200341 self.assertEqual(out.rstrip(), 'test with stdout=1')
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000342
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000343 def test_cwd(self):
Guido van Rossume9a0e882007-12-20 17:28:10 +0000344 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000345 # We cannot use os.path.realpath to canonicalize the path,
346 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
347 cwd = os.getcwd()
348 os.chdir(tmpdir)
349 tmpdir = os.getcwd()
350 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000351 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000352 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000353 'sys.stdout.write(os.getcwd())'],
354 stdout=subprocess.PIPE,
355 cwd=tmpdir)
Brian Curtind117b562010-11-05 04:09:09 +0000356 self.addCleanup(p.stdout.close)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000357 normcase = os.path.normcase
358 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000359
360 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000361 newenv = os.environ.copy()
362 newenv["FRUIT"] = "orange"
363 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000364 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000365 'sys.stdout.write(os.getenv("FRUIT"))'],
366 stdout=subprocess.PIPE,
367 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000368 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000369 self.assertEqual(p.stdout.read(), "orange")
370
Peter Astrandcbac93c2005-03-03 20:24:28 +0000371 def test_communicate_stdin(self):
372 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000373 'import sys;'
374 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000375 stdin=subprocess.PIPE)
376 p.communicate("pear")
377 self.assertEqual(p.returncode, 1)
378
379 def test_communicate_stdout(self):
380 p = subprocess.Popen([sys.executable, "-c",
381 'import sys; sys.stdout.write("pineapple")'],
382 stdout=subprocess.PIPE)
383 (stdout, stderr) = p.communicate()
384 self.assertEqual(stdout, "pineapple")
385 self.assertEqual(stderr, None)
386
387 def test_communicate_stderr(self):
388 p = subprocess.Popen([sys.executable, "-c",
389 'import sys; sys.stderr.write("pineapple")'],
390 stderr=subprocess.PIPE)
391 (stdout, stderr) = p.communicate()
392 self.assertEqual(stdout, None)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000393 self.assertStderrEqual(stderr, "pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000394
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000395 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000396 p = subprocess.Popen([sys.executable, "-c",
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000397 'import sys,os;'
398 'sys.stderr.write("pineapple");'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000399 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000400 stdin=subprocess.PIPE,
401 stdout=subprocess.PIPE,
402 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000403 self.addCleanup(p.stdout.close)
404 self.addCleanup(p.stderr.close)
405 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000406 (stdout, stderr) = p.communicate("banana")
407 self.assertEqual(stdout, "banana")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000408 self.assertStderrEqual(stderr, "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000409
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000410 # This test is Linux specific for simplicity to at least have
411 # some coverage. It is not a platform specific bug.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000412 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
413 "Linux specific")
414 # Test for the fd leak reported in http://bugs.python.org/issue2791.
415 def test_communicate_pipe_fd_leak(self):
416 fd_directory = '/proc/%d/fd' % os.getpid()
417 num_fds_before_popen = len(os.listdir(fd_directory))
418 p = subprocess.Popen([sys.executable, "-c", "print()"],
419 stdout=subprocess.PIPE)
420 p.communicate()
421 num_fds_after_communicate = len(os.listdir(fd_directory))
422 del p
423 num_fds_after_destruction = len(os.listdir(fd_directory))
424 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
425 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000426
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000427 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000428 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000429 p = subprocess.Popen([sys.executable, "-c",
430 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000431 (stdout, stderr) = p.communicate()
432 self.assertEqual(stdout, None)
433 self.assertEqual(stderr, None)
434
435 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000436 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000437 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000438 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000439 x, y = os.pipe()
440 if mswindows:
441 pipe_buf = 512
442 else:
443 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
444 os.close(x)
445 os.close(y)
446 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000447 'import sys,os;'
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000448 'sys.stdout.write(sys.stdin.read(47));'
449 'sys.stderr.write("xyz"*%d);'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000450 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000451 stdin=subprocess.PIPE,
452 stdout=subprocess.PIPE,
453 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000454 self.addCleanup(p.stdout.close)
455 self.addCleanup(p.stderr.close)
456 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000457 string_to_write = "abc"*pipe_buf
458 (stdout, stderr) = p.communicate(string_to_write)
459 self.assertEqual(stdout, string_to_write)
460
461 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000462 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000463 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000464 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000465 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000466 stdin=subprocess.PIPE,
467 stdout=subprocess.PIPE,
468 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000469 self.addCleanup(p.stdout.close)
470 self.addCleanup(p.stderr.close)
471 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000472 p.stdin.write("banana")
473 (stdout, stderr) = p.communicate("split")
474 self.assertEqual(stdout, "bananasplit")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000475 self.assertStderrEqual(stderr, "")
Tim Peterse718f612004-10-12 21:51:32 +0000476
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000477 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000478 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000479 'import sys,os;' + SETBINARY +
480 'sys.stdout.write("line1\\n");'
481 'sys.stdout.flush();'
482 'sys.stdout.write("line2\\r");'
483 'sys.stdout.flush();'
484 'sys.stdout.write("line3\\r\\n");'
485 'sys.stdout.flush();'
486 'sys.stdout.write("line4\\r");'
487 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000488 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000489 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000490 'sys.stdout.write("\\nline6");'],
491 stdout=subprocess.PIPE,
492 universal_newlines=1)
Brian Curtind117b562010-11-05 04:09:09 +0000493 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000494 stdout = p.stdout.read()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000495 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000496 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000497 self.assertEqual(stdout,
498 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000499 else:
500 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000501 self.assertEqual(stdout,
502 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000503
504 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000505 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000506 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000507 'import sys,os;' + SETBINARY +
508 'sys.stdout.write("line1\\n");'
509 'sys.stdout.flush();'
510 'sys.stdout.write("line2\\r");'
511 'sys.stdout.flush();'
512 'sys.stdout.write("line3\\r\\n");'
513 'sys.stdout.flush();'
514 'sys.stdout.write("line4\\r");'
515 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000516 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000517 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000518 'sys.stdout.write("\\nline6");'],
519 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
520 universal_newlines=1)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000521 self.addCleanup(p.stdout.close)
522 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000523 (stdout, stderr) = p.communicate()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000524 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000525 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000526 self.assertEqual(stdout,
527 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000528 else:
529 # Interpreter without universal newline support
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000530 self.assertEqual(stdout,
531 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000532
533 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000534 # Make sure we leak no resources
Antoine Pitroub0b3bff2010-09-18 22:42:30 +0000535 if not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000536 max_handles = 1026 # too much for most UNIX systems
537 else:
Antoine Pitroub0b3bff2010-09-18 22:42:30 +0000538 max_handles = 2050 # too much for (at least some) Windows setups
539 handles = []
540 try:
541 for i in range(max_handles):
542 try:
543 handles.append(os.open(test_support.TESTFN,
544 os.O_WRONLY | os.O_CREAT))
545 except OSError as e:
546 if e.errno != errno.EMFILE:
547 raise
548 break
549 else:
550 self.skipTest("failed to reach the file descriptor limit "
551 "(tried %d)" % max_handles)
552 # Close a couple of them (should be enough for a subprocess)
553 for i in range(10):
554 os.close(handles.pop())
555 # Loop creating some subprocesses. If one of them leaks some fds,
556 # the next loop iteration will fail by reaching the max fd limit.
557 for i in range(15):
558 p = subprocess.Popen([sys.executable, "-c",
559 "import sys;"
560 "sys.stdout.write(sys.stdin.read())"],
561 stdin=subprocess.PIPE,
562 stdout=subprocess.PIPE,
563 stderr=subprocess.PIPE)
564 data = p.communicate(b"lime")[0]
565 self.assertEqual(data, b"lime")
566 finally:
567 for h in handles:
568 os.close(h)
Mark Dickinson313dc9b2012-10-07 15:41:38 +0100569 test_support.unlink(test_support.TESTFN)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000570
571 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000572 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
573 '"a b c" d e')
574 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
575 'ab\\"c \\ d')
Gregory P. Smithe047e6d2008-01-19 20:49:02 +0000576 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
577 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000578 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
579 'a\\\\\\b "de fg" h')
580 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
581 'a\\\\\\"b c d')
582 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
583 '"a\\\\b c" d e')
584 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
585 '"a\\\\b\\ c" d e')
Peter Astrand10514a72007-01-13 22:35:35 +0000586 self.assertEqual(subprocess.list2cmdline(['ab', '']),
587 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000588
589
590 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000591 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000592 "-c", "import time; time.sleep(1)"])
593 count = 0
594 while p.poll() is None:
595 time.sleep(0.1)
596 count += 1
597 # We expect that the poll loop probably went around about 10 times,
598 # but, based on system scheduling we can't control, it's possible
599 # poll() never returned None. It "should be" very rare that it
600 # didn't go around at least twice.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000601 self.assertGreaterEqual(count, 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000602 # Subsequent invocations should just return the returncode
603 self.assertEqual(p.poll(), 0)
604
605
606 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000607 p = subprocess.Popen([sys.executable,
608 "-c", "import time; time.sleep(2)"])
609 self.assertEqual(p.wait(), 0)
610 # Subsequent invocations should just return the returncode
611 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000612
Peter Astrand738131d2004-11-30 21:04:45 +0000613
614 def test_invalid_bufsize(self):
615 # an invalid type of the bufsize argument should raise
616 # TypeError.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000617 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +0000618 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +0000619
Georg Brandlf3715d22009-02-14 17:01:36 +0000620 def test_leaking_fds_on_error(self):
621 # see bug #5179: Popen leaks file descriptors to PIPEs if
622 # the child fails to execute; this will eventually exhaust
623 # the maximum number of open fds. 1024 seems a very common
624 # value for that limit, but Windows has 2048, so we loop
625 # 1024 times (each call leaked two fds).
626 for i in range(1024):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000627 # Windows raises IOError. Others raise OSError.
628 with self.assertRaises(EnvironmentError) as c:
Georg Brandlf3715d22009-02-14 17:01:36 +0000629 subprocess.Popen(['nonexisting_i_hope'],
630 stdout=subprocess.PIPE,
631 stderr=subprocess.PIPE)
R David Murraycdd5fc92011-03-13 22:37:18 -0400632 # ignore errors that indicate the command was not found
633 if c.exception.errno not in (errno.ENOENT, errno.EACCES):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000634 raise c.exception
Georg Brandlf3715d22009-02-14 17:01:36 +0000635
Antoine Pitrou33fc7442013-08-30 23:38:13 +0200636 @unittest.skipIf(threading is None, "threading required")
637 def test_double_close_on_error(self):
638 # Issue #18851
639 fds = []
640 def open_fds():
641 for i in range(20):
642 fds.extend(os.pipe())
643 time.sleep(0.001)
644 t = threading.Thread(target=open_fds)
645 t.start()
646 try:
647 with self.assertRaises(EnvironmentError):
648 subprocess.Popen(['nonexisting_i_hope'],
649 stdin=subprocess.PIPE,
650 stdout=subprocess.PIPE,
651 stderr=subprocess.PIPE)
652 finally:
653 t.join()
654 exc = None
655 for fd in fds:
656 # If a double close occurred, some of those fds will
657 # already have been closed by mistake, and os.close()
658 # here will raise.
659 try:
660 os.close(fd)
661 except OSError as e:
662 exc = e
663 if exc is not None:
664 raise exc
665
Tim Golden90374f52010-08-06 13:14:33 +0000666 def test_handles_closed_on_exception(self):
667 # If CreateProcess exits with an error, ensure the
668 # duplicate output handles are released
669 ifhandle, ifname = mkstemp()
670 ofhandle, ofname = mkstemp()
671 efhandle, efname = mkstemp()
672 try:
673 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
674 stderr=efhandle)
675 except OSError:
676 os.close(ifhandle)
677 os.remove(ifname)
678 os.close(ofhandle)
679 os.remove(ofname)
680 os.close(efhandle)
681 os.remove(efname)
682 self.assertFalse(os.path.exists(ifname))
683 self.assertFalse(os.path.exists(ofname))
684 self.assertFalse(os.path.exists(efname))
685
Ross Lagerwall104c3f12011-04-05 15:24:34 +0200686 def test_communicate_epipe(self):
687 # Issue 10963: communicate() should hide EPIPE
688 p = subprocess.Popen([sys.executable, "-c", 'pass'],
689 stdin=subprocess.PIPE,
690 stdout=subprocess.PIPE,
691 stderr=subprocess.PIPE)
692 self.addCleanup(p.stdout.close)
693 self.addCleanup(p.stderr.close)
694 self.addCleanup(p.stdin.close)
695 p.communicate("x" * 2**20)
696
697 def test_communicate_epipe_only_stdin(self):
698 # Issue 10963: communicate() should hide EPIPE
699 p = subprocess.Popen([sys.executable, "-c", 'pass'],
700 stdin=subprocess.PIPE)
701 self.addCleanup(p.stdin.close)
702 time.sleep(2)
703 p.communicate("x" * 2**20)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000704
Gregory P. Smith9d3b6e92012-11-10 22:49:03 -0800705 # This test is Linux-ish specific for simplicity to at least have
706 # some coverage. It is not a platform specific bug.
707 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
708 "Linux specific")
709 def test_failed_child_execute_fd_leak(self):
710 """Test for the fork() failure fd leak reported in issue16327."""
711 fd_directory = '/proc/%d/fd' % os.getpid()
712 fds_before_popen = os.listdir(fd_directory)
713 with self.assertRaises(PopenTestException):
714 PopenExecuteChildRaises(
715 [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE,
716 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
717
718 # NOTE: This test doesn't verify that the real _execute_child
719 # does not close the file descriptors itself on the way out
720 # during an exception. Code inspection has confirmed that.
721
722 fds_after_exception = os.listdir(fd_directory)
723 self.assertEqual(fds_before_popen, fds_after_exception)
724
725
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000726# context manager
727class _SuppressCoreFiles(object):
728 """Try to prevent core files from being created."""
729 old_limit = None
730
731 def __enter__(self):
732 """Try to save previous ulimit, then set it to (0, 0)."""
Benjamin Peterson8b59c232011-12-10 12:31:42 -0500733 if resource is not None:
734 try:
735 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
736 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
737 except (ValueError, resource.error):
738 pass
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000739
Ronald Oussoren21b44e02010-07-23 12:26:30 +0000740 if sys.platform == 'darwin':
741 # Check if the 'Crash Reporter' on OSX was configured
742 # in 'Developer' mode and warn that it will get triggered
743 # when it is.
744 #
745 # This assumes that this context manager is used in tests
746 # that might trigger the next manager.
747 value = subprocess.Popen(['/usr/bin/defaults', 'read',
748 'com.apple.CrashReporter', 'DialogType'],
749 stdout=subprocess.PIPE).communicate()[0]
750 if value.strip() == b'developer':
751 print "this tests triggers the Crash Reporter, that is intentional"
752 sys.stdout.flush()
753
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000754 def __exit__(self, *args):
755 """Return core file behavior to default."""
756 if self.old_limit is None:
757 return
Benjamin Peterson8b59c232011-12-10 12:31:42 -0500758 if resource is not None:
759 try:
760 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
761 except (ValueError, resource.error):
762 pass
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000763
Victor Stinnerb78fed92011-07-05 14:50:35 +0200764 @unittest.skipUnless(hasattr(signal, 'SIGALRM'),
765 "Requires signal.SIGALRM")
Victor Stinnere7901312011-07-05 14:08:01 +0200766 def test_communicate_eintr(self):
767 # Issue #12493: communicate() should handle EINTR
768 def handler(signum, frame):
769 pass
770 old_handler = signal.signal(signal.SIGALRM, handler)
771 self.addCleanup(signal.signal, signal.SIGALRM, old_handler)
772
773 # the process is running for 2 seconds
774 args = [sys.executable, "-c", 'import time; time.sleep(2)']
775 for stream in ('stdout', 'stderr'):
776 kw = {stream: subprocess.PIPE}
777 with subprocess.Popen(args, **kw) as process:
778 signal.alarm(1)
779 # communicate() will be interrupted by SIGALRM
780 process.communicate()
781
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000782
Florent Xiclunabab22a72010-03-04 19:40:48 +0000783@unittest.skipIf(mswindows, "POSIX specific tests")
Florent Xiclunafc4d6d72010-03-23 14:36:45 +0000784class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaab5e17f2010-03-04 21:31:58 +0000785
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000786 def test_exceptions(self):
787 # caught & re-raised exceptions
788 with self.assertRaises(OSError) as c:
789 p = subprocess.Popen([sys.executable, "-c", ""],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000790 cwd="/this/path/does/not/exist")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000791 # The attribute child_traceback should contain "os.chdir" somewhere.
792 self.assertIn("os.chdir", c.exception.child_traceback)
Tim Peterse718f612004-10-12 21:51:32 +0000793
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000794 def test_run_abort(self):
795 # returncode handles signal termination
796 with _SuppressCoreFiles():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000797 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000798 "import os; os.abort()"])
799 p.wait()
800 self.assertEqual(-p.returncode, signal.SIGABRT)
801
802 def test_preexec(self):
803 # preexec function
804 p = subprocess.Popen([sys.executable, "-c",
805 "import sys, os;"
806 "sys.stdout.write(os.getenv('FRUIT'))"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000807 stdout=subprocess.PIPE,
808 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Brian Curtind117b562010-11-05 04:09:09 +0000809 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000810 self.assertEqual(p.stdout.read(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000811
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800812 class _TestExecuteChildPopen(subprocess.Popen):
813 """Used to test behavior at the end of _execute_child."""
814 def __init__(self, testcase, *args, **kwargs):
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800815 self._testcase = testcase
816 subprocess.Popen.__init__(self, *args, **kwargs)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800817
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800818 def _execute_child(
819 self, args, executable, preexec_fn, close_fds, cwd, env,
Antoine Pitrou33fc7442013-08-30 23:38:13 +0200820 universal_newlines, startupinfo, creationflags, shell, to_close,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800821 p2cread, p2cwrite,
822 c2pread, c2pwrite,
823 errread, errwrite):
824 try:
825 subprocess.Popen._execute_child(
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800826 self, args, executable, preexec_fn, close_fds,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800827 cwd, env, universal_newlines,
Antoine Pitrou33fc7442013-08-30 23:38:13 +0200828 startupinfo, creationflags, shell, to_close,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800829 p2cread, p2cwrite,
830 c2pread, c2pwrite,
831 errread, errwrite)
832 finally:
833 # Open a bunch of file descriptors and verify that
834 # none of them are the same as the ones the Popen
835 # instance is using for stdin/stdout/stderr.
836 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
837 for _ in range(8)]
838 try:
839 for fd in devzero_fds:
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800840 self._testcase.assertNotIn(
841 fd, (p2cwrite, c2pread, errread))
Gregory P. Smith211248b2012-11-11 02:00:49 -0800842 finally:
Richard Oudkerk045e4572013-06-10 16:27:45 +0100843 for fd in devzero_fds:
844 os.close(fd)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800845
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800846 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
847 def test_preexec_errpipe_does_not_double_close_pipes(self):
848 """Issue16140: Don't double close pipes on preexec error."""
849
850 def raise_it():
851 raise RuntimeError("force the _execute_child() errpipe_data path.")
Gregory P. Smith211248b2012-11-11 02:00:49 -0800852
853 with self.assertRaises(RuntimeError):
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800854 self._TestExecuteChildPopen(
855 self, [sys.executable, "-c", "pass"],
856 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
857 stderr=subprocess.PIPE, preexec_fn=raise_it)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800858
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000859 def test_args_string(self):
860 # args is a string
861 f, fname = mkstemp()
862 os.write(f, "#!/bin/sh\n")
863 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
864 sys.executable)
865 os.close(f)
866 os.chmod(fname, 0o700)
867 p = subprocess.Popen(fname)
868 p.wait()
869 os.remove(fname)
870 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000871
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000872 def test_invalid_args(self):
873 # invalid arguments should raise ValueError
874 self.assertRaises(ValueError, subprocess.call,
875 [sys.executable, "-c",
876 "import sys; sys.exit(47)"],
877 startupinfo=47)
878 self.assertRaises(ValueError, subprocess.call,
879 [sys.executable, "-c",
880 "import sys; sys.exit(47)"],
881 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000882
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000883 def test_shell_sequence(self):
884 # Run command through the shell (sequence)
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_shell_string(self):
894 # Run command through the shell (string)
895 newenv = os.environ.copy()
896 newenv["FRUIT"] = "apple"
897 p = subprocess.Popen("echo $FRUIT", shell=1,
898 stdout=subprocess.PIPE,
899 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000900 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000901 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000902
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000903 def test_call_string(self):
904 # call() function with string argument on UNIX
905 f, fname = mkstemp()
906 os.write(f, "#!/bin/sh\n")
907 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
908 sys.executable)
909 os.close(f)
910 os.chmod(fname, 0700)
911 rc = subprocess.call(fname)
912 os.remove(fname)
913 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000914
Stefan Krahe9a6a7d2010-07-19 14:41:08 +0000915 def test_specific_shell(self):
916 # Issue #9265: Incorrect name passed as arg[0].
917 shells = []
918 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
919 for name in ['bash', 'ksh']:
920 sh = os.path.join(prefix, name)
921 if os.path.isfile(sh):
922 shells.append(sh)
923 if not shells: # Will probably work for any shell but csh.
924 self.skipTest("bash or ksh required for this test")
925 sh = '/bin/sh'
926 if os.path.isfile(sh) and not os.path.islink(sh):
927 # Test will fail if /bin/sh is a symlink to csh.
928 shells.append(sh)
929 for sh in shells:
930 p = subprocess.Popen("echo $0", executable=sh, shell=True,
931 stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000932 self.addCleanup(p.stdout.close)
Stefan Krahe9a6a7d2010-07-19 14:41:08 +0000933 self.assertEqual(p.stdout.read().strip(), sh)
934
Florent Xiclunac0838642010-03-07 15:27:39 +0000935 def _kill_process(self, method, *args):
Florent Xiclunacecef392010-03-05 19:31:21 +0000936 # Do not inherit file handles from the parent.
937 # It should fix failures on some platforms.
Antoine Pitroua6166da2010-09-20 11:20:44 +0000938 p = subprocess.Popen([sys.executable, "-c", """if 1:
939 import sys, time
940 sys.stdout.write('x\\n')
941 sys.stdout.flush()
942 time.sleep(30)
943 """],
944 close_fds=True,
945 stdin=subprocess.PIPE,
946 stdout=subprocess.PIPE,
947 stderr=subprocess.PIPE)
948 # Wait for the interpreter to be completely initialized before
949 # sending any signal.
950 p.stdout.read(1)
951 getattr(p, method)(*args)
Florent Xiclunac0838642010-03-07 15:27:39 +0000952 return p
953
Charles-François Natalief2bd672013-01-12 16:52:20 +0100954 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
955 "Due to known OS bug (issue #16762)")
Antoine Pitrouf60845b2012-03-11 19:29:12 +0100956 def _kill_dead_process(self, method, *args):
957 # Do not inherit file handles from the parent.
958 # It should fix failures on some platforms.
959 p = subprocess.Popen([sys.executable, "-c", """if 1:
960 import sys, time
961 sys.stdout.write('x\\n')
962 sys.stdout.flush()
963 """],
964 close_fds=True,
965 stdin=subprocess.PIPE,
966 stdout=subprocess.PIPE,
967 stderr=subprocess.PIPE)
968 # Wait for the interpreter to be completely initialized before
969 # sending any signal.
970 p.stdout.read(1)
971 # The process should end after this
972 time.sleep(1)
973 # This shouldn't raise even though the child is now dead
974 getattr(p, method)(*args)
975 p.communicate()
976
Florent Xiclunac0838642010-03-07 15:27:39 +0000977 def test_send_signal(self):
978 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunafc4d6d72010-03-23 14:36:45 +0000979 _, stderr = p.communicate()
Florent Xicluna3c919cf2010-03-23 19:19:16 +0000980 self.assertIn('KeyboardInterrupt', stderr)
Florent Xicluna446ff142010-03-23 15:05:30 +0000981 self.assertNotEqual(p.wait(), 0)
Christian Heimese74c8f22008-04-19 02:23:57 +0000982
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000983 def test_kill(self):
Florent Xiclunac0838642010-03-07 15:27:39 +0000984 p = self._kill_process('kill')
Florent Xicluna446ff142010-03-23 15:05:30 +0000985 _, stderr = p.communicate()
986 self.assertStderrEqual(stderr, '')
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000987 self.assertEqual(p.wait(), -signal.SIGKILL)
Christian Heimese74c8f22008-04-19 02:23:57 +0000988
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000989 def test_terminate(self):
Florent Xiclunac0838642010-03-07 15:27:39 +0000990 p = self._kill_process('terminate')
Florent Xicluna446ff142010-03-23 15:05:30 +0000991 _, stderr = p.communicate()
992 self.assertStderrEqual(stderr, '')
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000993 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000994
Antoine Pitrouf60845b2012-03-11 19:29:12 +0100995 def test_send_signal_dead(self):
996 # Sending a signal to a dead process
997 self._kill_dead_process('send_signal', signal.SIGINT)
998
999 def test_kill_dead(self):
1000 # Killing a dead process
1001 self._kill_dead_process('kill')
1002
1003 def test_terminate_dead(self):
1004 # Terminating a dead process
1005 self._kill_dead_process('terminate')
1006
Antoine Pitrou91ce0d92011-01-03 18:45:09 +00001007 def check_close_std_fds(self, fds):
1008 # Issue #9905: test that subprocess pipes still work properly with
1009 # some standard fds closed
1010 stdin = 0
1011 newfds = []
1012 for a in fds:
1013 b = os.dup(a)
1014 newfds.append(b)
1015 if a == 0:
1016 stdin = b
1017 try:
1018 for fd in fds:
1019 os.close(fd)
1020 out, err = subprocess.Popen([sys.executable, "-c",
1021 'import sys;'
1022 'sys.stdout.write("apple");'
1023 'sys.stdout.flush();'
1024 'sys.stderr.write("orange")'],
1025 stdin=stdin,
1026 stdout=subprocess.PIPE,
1027 stderr=subprocess.PIPE).communicate()
1028 err = test_support.strip_python_stderr(err)
1029 self.assertEqual((out, err), (b'apple', b'orange'))
1030 finally:
1031 for b, a in zip(newfds, fds):
1032 os.dup2(b, a)
1033 for b in newfds:
1034 os.close(b)
1035
1036 def test_close_fd_0(self):
1037 self.check_close_std_fds([0])
1038
1039 def test_close_fd_1(self):
1040 self.check_close_std_fds([1])
1041
1042 def test_close_fd_2(self):
1043 self.check_close_std_fds([2])
1044
1045 def test_close_fds_0_1(self):
1046 self.check_close_std_fds([0, 1])
1047
1048 def test_close_fds_0_2(self):
1049 self.check_close_std_fds([0, 2])
1050
1051 def test_close_fds_1_2(self):
1052 self.check_close_std_fds([1, 2])
1053
1054 def test_close_fds_0_1_2(self):
1055 # Issue #10806: test that subprocess pipes still work properly with
1056 # all standard fds closed.
1057 self.check_close_std_fds([0, 1, 2])
1058
Ross Lagerwalld8e39012011-07-27 18:54:53 +02001059 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
1060 # open up some temporary files
1061 temps = [mkstemp() for i in range(3)]
1062 temp_fds = [fd for fd, fname in temps]
1063 try:
1064 # unlink the files -- we won't need to reopen them
1065 for fd, fname in temps:
1066 os.unlink(fname)
1067
1068 # save a copy of the standard file descriptors
1069 saved_fds = [os.dup(fd) for fd in range(3)]
1070 try:
1071 # duplicate the temp files over the standard fd's 0, 1, 2
1072 for fd, temp_fd in enumerate(temp_fds):
1073 os.dup2(temp_fd, fd)
1074
1075 # write some data to what will become stdin, and rewind
1076 os.write(stdin_no, b"STDIN")
1077 os.lseek(stdin_no, 0, 0)
1078
1079 # now use those files in the given order, so that subprocess
1080 # has to rearrange them in the child
1081 p = subprocess.Popen([sys.executable, "-c",
1082 'import sys; got = sys.stdin.read();'
1083 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
1084 stdin=stdin_no,
1085 stdout=stdout_no,
1086 stderr=stderr_no)
1087 p.wait()
1088
1089 for fd in temp_fds:
1090 os.lseek(fd, 0, 0)
1091
1092 out = os.read(stdout_no, 1024)
1093 err = test_support.strip_python_stderr(os.read(stderr_no, 1024))
1094 finally:
1095 for std, saved in enumerate(saved_fds):
1096 os.dup2(saved, std)
1097 os.close(saved)
1098
1099 self.assertEqual(out, b"got STDIN")
1100 self.assertEqual(err, b"err")
1101
1102 finally:
1103 for fd in temp_fds:
1104 os.close(fd)
1105
1106 # When duping fds, if there arises a situation where one of the fds is
1107 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
1108 # This tests all combinations of this.
1109 def test_swap_fds(self):
1110 self.check_swap_fds(0, 1, 2)
1111 self.check_swap_fds(0, 2, 1)
1112 self.check_swap_fds(1, 0, 2)
1113 self.check_swap_fds(1, 2, 0)
1114 self.check_swap_fds(2, 0, 1)
1115 self.check_swap_fds(2, 1, 0)
1116
Gregory P. Smith312efbc2010-12-14 15:02:53 +00001117 def test_wait_when_sigchild_ignored(self):
1118 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
1119 sigchild_ignore = test_support.findfile("sigchild_ignore.py",
1120 subdir="subprocessdata")
1121 p = subprocess.Popen([sys.executable, sigchild_ignore],
1122 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1123 stdout, stderr = p.communicate()
1124 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
1125 " non-zero with this error:\n%s" % stderr)
1126
Charles-François Natali100df0f2011-08-18 17:56:02 +02001127 def test_zombie_fast_process_del(self):
1128 # Issue #12650: on Unix, if Popen.__del__() was called before the
1129 # process exited, it wouldn't be added to subprocess._active, and would
1130 # remain a zombie.
1131 # spawn a Popen, and delete its reference before it exits
1132 p = subprocess.Popen([sys.executable, "-c",
1133 'import sys, time;'
1134 'time.sleep(0.2)'],
1135 stdout=subprocess.PIPE,
1136 stderr=subprocess.PIPE)
Nadeem Vawda86059362011-08-19 05:22:24 +02001137 self.addCleanup(p.stdout.close)
1138 self.addCleanup(p.stderr.close)
Charles-François Natali100df0f2011-08-18 17:56:02 +02001139 ident = id(p)
1140 pid = p.pid
1141 del p
1142 # check that p is in the active processes list
1143 self.assertIn(ident, [id(o) for o in subprocess._active])
1144
Charles-François Natali100df0f2011-08-18 17:56:02 +02001145 def test_leak_fast_process_del_killed(self):
1146 # Issue #12650: on Unix, if Popen.__del__() was called before the
1147 # process exited, and the process got killed by a signal, it would never
1148 # be removed from subprocess._active, which triggered a FD and memory
1149 # leak.
1150 # spawn a Popen, delete its reference and kill it
1151 p = subprocess.Popen([sys.executable, "-c",
1152 'import time;'
1153 'time.sleep(3)'],
1154 stdout=subprocess.PIPE,
1155 stderr=subprocess.PIPE)
Nadeem Vawda86059362011-08-19 05:22:24 +02001156 self.addCleanup(p.stdout.close)
1157 self.addCleanup(p.stderr.close)
Charles-François Natali100df0f2011-08-18 17:56:02 +02001158 ident = id(p)
1159 pid = p.pid
1160 del p
1161 os.kill(pid, signal.SIGKILL)
1162 # check that p is in the active processes list
1163 self.assertIn(ident, [id(o) for o in subprocess._active])
1164
1165 # let some time for the process to exit, and create a new Popen: this
1166 # should trigger the wait() of p
1167 time.sleep(0.2)
1168 with self.assertRaises(EnvironmentError) as c:
1169 with subprocess.Popen(['nonexisting_i_hope'],
1170 stdout=subprocess.PIPE,
1171 stderr=subprocess.PIPE) as proc:
1172 pass
1173 # p should have been wait()ed on, and removed from the _active list
1174 self.assertRaises(OSError, os.waitpid, pid, 0)
1175 self.assertNotIn(ident, [id(o) for o in subprocess._active])
1176
Charles-François Natali2a34eb32011-08-25 21:20:54 +02001177 def test_pipe_cloexec(self):
1178 # Issue 12786: check that the communication pipes' FDs are set CLOEXEC,
1179 # and are not inherited by another child process.
1180 p1 = subprocess.Popen([sys.executable, "-c",
1181 'import os;'
1182 'os.read(0, 1)'
1183 ],
1184 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1185 stderr=subprocess.PIPE)
1186
1187 p2 = subprocess.Popen([sys.executable, "-c", """if True:
1188 import os, errno, sys
1189 for fd in %r:
1190 try:
1191 os.close(fd)
1192 except OSError as e:
1193 if e.errno != errno.EBADF:
1194 raise
1195 else:
1196 sys.exit(1)
1197 sys.exit(0)
1198 """ % [f.fileno() for f in (p1.stdin, p1.stdout,
1199 p1.stderr)]
1200 ],
1201 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1202 stderr=subprocess.PIPE, close_fds=False)
1203 p1.communicate('foo')
1204 _, stderr = p2.communicate()
1205
1206 self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr))
1207
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001208
Florent Xiclunabab22a72010-03-04 19:40:48 +00001209@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunafc4d6d72010-03-23 14:36:45 +00001210class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaab5e17f2010-03-04 21:31:58 +00001211
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001212 def test_startupinfo(self):
1213 # startupinfo argument
1214 # We uses hardcoded constants, because we do not want to
1215 # depend on win32all.
1216 STARTF_USESHOWWINDOW = 1
1217 SW_MAXIMIZE = 3
1218 startupinfo = subprocess.STARTUPINFO()
1219 startupinfo.dwFlags = STARTF_USESHOWWINDOW
1220 startupinfo.wShowWindow = SW_MAXIMIZE
1221 # Since Python is a console process, it won't be affected
1222 # by wShowWindow, but the argument should be silently
1223 # ignored
1224 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001225 startupinfo=startupinfo)
1226
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001227 def test_creationflags(self):
1228 # creationflags argument
1229 CREATE_NEW_CONSOLE = 16
1230 sys.stderr.write(" a DOS box should flash briefly ...\n")
1231 subprocess.call(sys.executable +
1232 ' -c "import time; time.sleep(0.25)"',
1233 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001234
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001235 def test_invalid_args(self):
1236 # invalid arguments should raise ValueError
1237 self.assertRaises(ValueError, subprocess.call,
1238 [sys.executable, "-c",
1239 "import sys; sys.exit(47)"],
1240 preexec_fn=lambda: 1)
1241 self.assertRaises(ValueError, subprocess.call,
1242 [sys.executable, "-c",
1243 "import sys; sys.exit(47)"],
1244 stdout=subprocess.PIPE,
1245 close_fds=True)
1246
1247 def test_close_fds(self):
1248 # close file descriptors
1249 rc = subprocess.call([sys.executable, "-c",
1250 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001251 close_fds=True)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001252 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001253
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001254 def test_shell_sequence(self):
1255 # Run command through the shell (sequence)
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())
Peter Astrand81a191b2007-05-26 22:18:20 +00001263
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001264 def test_shell_string(self):
1265 # Run command through the shell (string)
1266 newenv = os.environ.copy()
1267 newenv["FRUIT"] = "physalis"
1268 p = subprocess.Popen("set", shell=1,
1269 stdout=subprocess.PIPE,
1270 env=newenv)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001271 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001272 self.assertIn("physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001273
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001274 def test_call_string(self):
1275 # call() function with string argument on Windows
1276 rc = subprocess.call(sys.executable +
1277 ' -c "import sys; sys.exit(47)"')
1278 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001279
Florent Xiclunac0838642010-03-07 15:27:39 +00001280 def _kill_process(self, method, *args):
Florent Xicluna400efc22010-03-07 17:12:23 +00001281 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroudee00972010-09-24 19:00:29 +00001282 p = subprocess.Popen([sys.executable, "-c", """if 1:
1283 import sys, time
1284 sys.stdout.write('x\\n')
1285 sys.stdout.flush()
1286 time.sleep(30)
1287 """],
1288 stdin=subprocess.PIPE,
1289 stdout=subprocess.PIPE,
1290 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001291 self.addCleanup(p.stdout.close)
1292 self.addCleanup(p.stderr.close)
1293 self.addCleanup(p.stdin.close)
Antoine Pitroudee00972010-09-24 19:00:29 +00001294 # Wait for the interpreter to be completely initialized before
1295 # sending any signal.
1296 p.stdout.read(1)
1297 getattr(p, method)(*args)
Florent Xicluna446ff142010-03-23 15:05:30 +00001298 _, stderr = p.communicate()
1299 self.assertStderrEqual(stderr, '')
Antoine Pitroudee00972010-09-24 19:00:29 +00001300 returncode = p.wait()
Florent Xiclunafaf17532010-03-08 10:59:33 +00001301 self.assertNotEqual(returncode, 0)
Florent Xiclunac0838642010-03-07 15:27:39 +00001302
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001303 def _kill_dead_process(self, method, *args):
1304 p = subprocess.Popen([sys.executable, "-c", """if 1:
1305 import sys, time
1306 sys.stdout.write('x\\n')
1307 sys.stdout.flush()
1308 sys.exit(42)
1309 """],
1310 stdin=subprocess.PIPE,
1311 stdout=subprocess.PIPE,
1312 stderr=subprocess.PIPE)
1313 self.addCleanup(p.stdout.close)
1314 self.addCleanup(p.stderr.close)
1315 self.addCleanup(p.stdin.close)
1316 # Wait for the interpreter to be completely initialized before
1317 # sending any signal.
1318 p.stdout.read(1)
1319 # The process should end after this
1320 time.sleep(1)
1321 # This shouldn't raise even though the child is now dead
1322 getattr(p, method)(*args)
1323 _, stderr = p.communicate()
1324 self.assertStderrEqual(stderr, b'')
1325 rc = p.wait()
1326 self.assertEqual(rc, 42)
1327
Florent Xiclunac0838642010-03-07 15:27:39 +00001328 def test_send_signal(self):
1329 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimese74c8f22008-04-19 02:23:57 +00001330
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001331 def test_kill(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001332 self._kill_process('kill')
Christian Heimese74c8f22008-04-19 02:23:57 +00001333
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001334 def test_terminate(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001335 self._kill_process('terminate')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001336
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001337 def test_send_signal_dead(self):
1338 self._kill_dead_process('send_signal', signal.SIGTERM)
1339
1340 def test_kill_dead(self):
1341 self._kill_dead_process('kill')
1342
1343 def test_terminate_dead(self):
1344 self._kill_dead_process('terminate')
1345
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001346
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001347@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
1348 "poll system call not supported")
1349class ProcessTestCaseNoPoll(ProcessTestCase):
1350 def setUp(self):
1351 subprocess._has_poll = False
1352 ProcessTestCase.setUp(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001353
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001354 def tearDown(self):
1355 subprocess._has_poll = True
1356 ProcessTestCase.tearDown(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001357
1358
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001359class HelperFunctionTests(unittest.TestCase):
Gregory P. Smithc1baf4a2010-03-01 02:53:24 +00001360 @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001361 def test_eintr_retry_call(self):
1362 record_calls = []
1363 def fake_os_func(*args):
1364 record_calls.append(args)
1365 if len(record_calls) == 2:
1366 raise OSError(errno.EINTR, "fake interrupted system call")
1367 return tuple(reversed(args))
1368
1369 self.assertEqual((999, 256),
1370 subprocess._eintr_retry_call(fake_os_func, 256, 999))
1371 self.assertEqual([(256, 999)], record_calls)
1372 # This time there will be an EINTR so it will loop once.
1373 self.assertEqual((666,),
1374 subprocess._eintr_retry_call(fake_os_func, 666))
1375 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
1376
Tim Golden8e4756c2010-08-12 11:00:35 +00001377@unittest.skipUnless(mswindows, "mswindows only")
1378class CommandsWithSpaces (BaseTestCase):
1379
1380 def setUp(self):
1381 super(CommandsWithSpaces, self).setUp()
1382 f, fname = mkstemp(".py", "te st")
1383 self.fname = fname.lower ()
1384 os.write(f, b"import sys;"
1385 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
1386 )
1387 os.close(f)
1388
1389 def tearDown(self):
1390 os.remove(self.fname)
1391 super(CommandsWithSpaces, self).tearDown()
1392
1393 def with_spaces(self, *args, **kwargs):
1394 kwargs['stdout'] = subprocess.PIPE
1395 p = subprocess.Popen(*args, **kwargs)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001396 self.addCleanup(p.stdout.close)
Tim Golden8e4756c2010-08-12 11:00:35 +00001397 self.assertEqual(
1398 p.stdout.read ().decode("mbcs"),
1399 "2 [%r, 'ab cd']" % self.fname
1400 )
1401
1402 def test_shell_string_with_spaces(self):
1403 # call() function with string argument with spaces on Windows
Brian Curtine8c49202010-08-13 21:01:52 +00001404 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1405 "ab cd"), shell=1)
Tim Golden8e4756c2010-08-12 11:00:35 +00001406
1407 def test_shell_sequence_with_spaces(self):
1408 # call() function with sequence argument with spaces on Windows
Brian Curtine8c49202010-08-13 21:01:52 +00001409 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden8e4756c2010-08-12 11:00:35 +00001410
1411 def test_noshell_string_with_spaces(self):
1412 # call() function with string argument with spaces on Windows
1413 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1414 "ab cd"))
1415
1416 def test_noshell_sequence_with_spaces(self):
1417 # call() function with sequence argument with spaces on Windows
1418 self.with_spaces([sys.executable, self.fname, "ab cd"])
1419
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001420def test_main():
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001421 unit_tests = (ProcessTestCase,
1422 POSIXProcessTestCase,
1423 Win32ProcessTestCase,
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001424 ProcessTestCaseNoPoll,
Tim Golden8e4756c2010-08-12 11:00:35 +00001425 HelperFunctionTests,
1426 CommandsWithSpaces)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001427
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001428 test_support.run_unittest(*unit_tests)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001429 test_support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001430
1431if __name__ == "__main__":
1432 test_main()