blob: f43b51caf832e20f074828bdc221c5b55bcd18aa [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
17
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000018mswindows = (sys.platform == "win32")
19
20#
21# Depends on the following external programs: Python
22#
23
24if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000025 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
26 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000027else:
28 SETBINARY = ''
29
Florent Xicluna98e3fc32010-02-27 19:20:50 +000030
31try:
32 mkstemp = tempfile.mkstemp
33except AttributeError:
34 # tempfile.mkstemp is not available
35 def mkstemp():
36 """Replacement for mkstemp, calling mktemp."""
37 fname = tempfile.mktemp()
38 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
39
Tim Peters3761e8d2004-10-13 04:07:12 +000040
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000041class BaseTestCase(unittest.TestCase):
Neal Norwitzb15ac312006-06-29 04:10:08 +000042 def setUp(self):
Tim Peters38ff36c2006-06-30 06:18:39 +000043 # Try to minimize the number of children we have so this test
44 # doesn't crash on some buildbots (Alphas in particular).
Florent Xicluna98e3fc32010-02-27 19:20:50 +000045 test_support.reap_children()
Neal Norwitzb15ac312006-06-29 04:10:08 +000046
Florent Xiclunaab5e17f2010-03-04 21:31:58 +000047 def tearDown(self):
48 for inst in subprocess._active:
49 inst.wait()
50 subprocess._cleanup()
51 self.assertFalse(subprocess._active, "subprocess._active not empty")
52
Florent Xicluna98e3fc32010-02-27 19:20:50 +000053 def assertStderrEqual(self, stderr, expected, msg=None):
54 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
55 # shutdown time. That frustrates tests trying to check stderr produced
56 # from a spawned Python process.
57 actual = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
58 self.assertEqual(actual, expected, msg)
Neal Norwitzb15ac312006-06-29 04:10:08 +000059
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000060
Gregory P. Smith9d3b6e92012-11-10 22:49:03 -080061class PopenTestException(Exception):
62 pass
63
64
65class PopenExecuteChildRaises(subprocess.Popen):
66 """Popen subclass for testing cleanup of subprocess.PIPE filehandles when
67 _execute_child fails.
68 """
69 def _execute_child(self, *args, **kwargs):
70 raise PopenTestException("Forced Exception for Test")
71
72
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000073class ProcessTestCase(BaseTestCase):
74
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000075 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000076 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000077 rc = subprocess.call([sys.executable, "-c",
78 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000079 self.assertEqual(rc, 47)
80
Peter Astrand454f7672005-01-01 09:36:35 +000081 def test_check_call_zero(self):
82 # check_call() function with zero return code
83 rc = subprocess.check_call([sys.executable, "-c",
84 "import sys; sys.exit(0)"])
85 self.assertEqual(rc, 0)
86
87 def test_check_call_nonzero(self):
88 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +000089 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +000090 subprocess.check_call([sys.executable, "-c",
91 "import sys; sys.exit(47)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +000092 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000093
Gregory P. Smith26576802008-12-05 02:27:01 +000094 def test_check_output(self):
95 # check_output() function with zero return code
96 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000097 [sys.executable, "-c", "print 'BDFL'"])
Ezio Melottiaa980582010-01-23 23:04:36 +000098 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000099
Gregory P. Smith26576802008-12-05 02:27:01 +0000100 def test_check_output_nonzero(self):
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000101 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000102 with self.assertRaises(subprocess.CalledProcessError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +0000103 subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000104 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000105 self.assertEqual(c.exception.returncode, 5)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000106
Gregory P. Smith26576802008-12-05 02:27:01 +0000107 def test_check_output_stderr(self):
108 # check_output() function stderr redirected to stdout
109 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000110 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
111 stderr=subprocess.STDOUT)
Ezio Melottiaa980582010-01-23 23:04:36 +0000112 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000113
Gregory P. Smith26576802008-12-05 02:27:01 +0000114 def test_check_output_stdout_arg(self):
115 # check_output() function stderr redirected to stdout
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000116 with self.assertRaises(ValueError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +0000117 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000118 [sys.executable, "-c", "print 'will not be run'"],
119 stdout=sys.stdout)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000120 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000121 self.assertIn('stdout', c.exception.args[0])
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000122
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000123 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000124 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000125 newenv = os.environ.copy()
126 newenv["FRUIT"] = "banana"
127 rc = subprocess.call([sys.executable, "-c",
Florent Xiclunabab22a72010-03-04 19:40:48 +0000128 'import sys, os;'
129 'sys.exit(os.getenv("FRUIT")=="banana")'],
130 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000131 self.assertEqual(rc, 1)
132
Victor Stinner776e69b2011-06-01 01:03:00 +0200133 def test_invalid_args(self):
134 # Popen() called with invalid arguments should raise TypeError
135 # but Popen.__del__ should not complain (issue #12085)
Victor Stinnere9b185f2011-06-01 01:57:48 +0200136 with test_support.captured_stderr() as s:
Victor Stinner776e69b2011-06-01 01:03:00 +0200137 self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
138 argcount = subprocess.Popen.__init__.__code__.co_argcount
139 too_many_args = [0] * (argcount + 1)
140 self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
141 self.assertEqual(s.getvalue(), '')
142
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000143 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000144 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000145 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
146 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000147 self.addCleanup(p.stdout.close)
148 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000149 p.wait()
150 self.assertEqual(p.stdin, None)
151
152 def test_stdout_none(self):
Ezio Melottiefaad092013-03-11 00:34:33 +0200153 # .stdout is None when not redirected, and the child's stdout will
154 # be inherited from the parent. In order to test this we run a
155 # subprocess in a subprocess:
156 # this_test
157 # \-- subprocess created by this test (parent)
158 # \-- subprocess created by the parent subprocess (child)
159 # The parent doesn't specify stdout, so the child will use the
160 # parent's stdout. This test checks that the message printed by the
161 # child goes to the parent stdout. The parent also checks that the
162 # child's stdout is None. See #11963.
163 code = ('import sys; from subprocess import Popen, PIPE;'
164 'p = Popen([sys.executable, "-c", "print \'test_stdout_none\'"],'
165 ' stdin=PIPE, stderr=PIPE);'
166 'p.wait(); assert p.stdout is None;')
167 p = subprocess.Popen([sys.executable, "-c", code],
168 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
169 self.addCleanup(p.stdout.close)
Brian Curtind117b562010-11-05 04:09:09 +0000170 self.addCleanup(p.stderr.close)
Ezio Melottiefaad092013-03-11 00:34:33 +0200171 out, err = p.communicate()
172 self.assertEqual(p.returncode, 0, err)
173 self.assertEqual(out.rstrip(), 'test_stdout_none')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000174
175 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000176 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000177 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
178 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000179 self.addCleanup(p.stdout.close)
180 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000181 p.wait()
182 self.assertEqual(p.stderr, None)
183
Ezio Melotti8f6a2872010-02-10 21:40:33 +0000184 def test_executable_with_cwd(self):
Florent Xicluna63763702010-03-11 01:50:48 +0000185 python_dir = os.path.dirname(os.path.realpath(sys.executable))
Ezio Melotti8f6a2872010-02-10 21:40:33 +0000186 p = subprocess.Popen(["somethingyoudonthave", "-c",
187 "import sys; sys.exit(47)"],
188 executable=sys.executable, cwd=python_dir)
189 p.wait()
190 self.assertEqual(p.returncode, 47)
191
192 @unittest.skipIf(sysconfig.is_python_build(),
193 "need an installed Python. See #7774")
194 def test_executable_without_cwd(self):
195 # For a normal installation, it should work without 'cwd'
196 # argument. For test runs in the build directory, see #7774.
197 p = subprocess.Popen(["somethingyoudonthave", "-c",
198 "import sys; sys.exit(47)"],
Tim Peters3b01a702004-10-12 22:19:32 +0000199 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000200 p.wait()
201 self.assertEqual(p.returncode, 47)
202
203 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000204 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000205 p = subprocess.Popen([sys.executable, "-c",
206 'import sys; sys.exit(sys.stdin.read() == "pear")'],
207 stdin=subprocess.PIPE)
208 p.stdin.write("pear")
209 p.stdin.close()
210 p.wait()
211 self.assertEqual(p.returncode, 1)
212
213 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000214 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000215 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000216 d = tf.fileno()
217 os.write(d, "pear")
218 os.lseek(d, 0, 0)
219 p = subprocess.Popen([sys.executable, "-c",
220 'import sys; sys.exit(sys.stdin.read() == "pear")'],
221 stdin=d)
222 p.wait()
223 self.assertEqual(p.returncode, 1)
224
225 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000226 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000227 tf = tempfile.TemporaryFile()
228 tf.write("pear")
229 tf.seek(0)
230 p = subprocess.Popen([sys.executable, "-c",
231 'import sys; sys.exit(sys.stdin.read() == "pear")'],
232 stdin=tf)
233 p.wait()
234 self.assertEqual(p.returncode, 1)
235
236 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000237 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000238 p = subprocess.Popen([sys.executable, "-c",
239 'import sys; sys.stdout.write("orange")'],
240 stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000241 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000242 self.assertEqual(p.stdout.read(), "orange")
243
244 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000245 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000246 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000247 d = tf.fileno()
248 p = subprocess.Popen([sys.executable, "-c",
249 'import sys; sys.stdout.write("orange")'],
250 stdout=d)
251 p.wait()
252 os.lseek(d, 0, 0)
253 self.assertEqual(os.read(d, 1024), "orange")
254
255 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000256 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000257 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000258 p = subprocess.Popen([sys.executable, "-c",
259 'import sys; sys.stdout.write("orange")'],
260 stdout=tf)
261 p.wait()
262 tf.seek(0)
263 self.assertEqual(tf.read(), "orange")
264
265 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000266 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000267 p = subprocess.Popen([sys.executable, "-c",
268 'import sys; sys.stderr.write("strawberry")'],
269 stderr=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000270 self.addCleanup(p.stderr.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000271 self.assertStderrEqual(p.stderr.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000272
273 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000274 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000275 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000276 d = tf.fileno()
277 p = subprocess.Popen([sys.executable, "-c",
278 'import sys; sys.stderr.write("strawberry")'],
279 stderr=d)
280 p.wait()
281 os.lseek(d, 0, 0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000282 self.assertStderrEqual(os.read(d, 1024), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000283
284 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000285 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000286 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000287 p = subprocess.Popen([sys.executable, "-c",
288 'import sys; sys.stderr.write("strawberry")'],
289 stderr=tf)
290 p.wait()
291 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000292 self.assertStderrEqual(tf.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000293
294 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000295 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000296 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000297 'import sys;'
298 'sys.stdout.write("apple");'
299 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000300 'sys.stderr.write("orange")'],
301 stdout=subprocess.PIPE,
302 stderr=subprocess.STDOUT)
Brian Curtind117b562010-11-05 04:09:09 +0000303 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000304 self.assertStderrEqual(p.stdout.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000305
306 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000307 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000308 tf = tempfile.TemporaryFile()
309 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000310 'import sys;'
311 'sys.stdout.write("apple");'
312 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000313 'sys.stderr.write("orange")'],
314 stdout=tf,
315 stderr=tf)
316 p.wait()
317 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000318 self.assertStderrEqual(tf.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000319
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000320 def test_stdout_filedes_of_stdout(self):
321 # stdout is set to 1 (#1531862).
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200322 # To avoid printing the text on stdout, we do something similar to
Ezio Melottiefaad092013-03-11 00:34:33 +0200323 # test_stdout_none (see above). The parent subprocess calls the child
324 # subprocess passing stdout=1, and this test uses stdout=PIPE in
325 # order to capture and check the output of the parent. See #11963.
326 code = ('import sys, subprocess; '
327 'rc = subprocess.call([sys.executable, "-c", '
328 ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), '
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200329 '\'test with stdout=1\'))"], stdout=1); '
330 'assert rc == 18')
Ezio Melottiefaad092013-03-11 00:34:33 +0200331 p = subprocess.Popen([sys.executable, "-c", code],
332 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
333 self.addCleanup(p.stdout.close)
334 self.addCleanup(p.stderr.close)
335 out, err = p.communicate()
336 self.assertEqual(p.returncode, 0, err)
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200337 self.assertEqual(out.rstrip(), 'test with stdout=1')
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000338
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000339 def test_cwd(self):
Guido van Rossume9a0e882007-12-20 17:28:10 +0000340 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000341 # We cannot use os.path.realpath to canonicalize the path,
342 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
343 cwd = os.getcwd()
344 os.chdir(tmpdir)
345 tmpdir = os.getcwd()
346 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000347 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000348 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000349 'sys.stdout.write(os.getcwd())'],
350 stdout=subprocess.PIPE,
351 cwd=tmpdir)
Brian Curtind117b562010-11-05 04:09:09 +0000352 self.addCleanup(p.stdout.close)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000353 normcase = os.path.normcase
354 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000355
356 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000357 newenv = os.environ.copy()
358 newenv["FRUIT"] = "orange"
359 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000360 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000361 'sys.stdout.write(os.getenv("FRUIT"))'],
362 stdout=subprocess.PIPE,
363 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000364 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000365 self.assertEqual(p.stdout.read(), "orange")
366
Peter Astrandcbac93c2005-03-03 20:24:28 +0000367 def test_communicate_stdin(self):
368 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000369 'import sys;'
370 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000371 stdin=subprocess.PIPE)
372 p.communicate("pear")
373 self.assertEqual(p.returncode, 1)
374
375 def test_communicate_stdout(self):
376 p = subprocess.Popen([sys.executable, "-c",
377 'import sys; sys.stdout.write("pineapple")'],
378 stdout=subprocess.PIPE)
379 (stdout, stderr) = p.communicate()
380 self.assertEqual(stdout, "pineapple")
381 self.assertEqual(stderr, None)
382
383 def test_communicate_stderr(self):
384 p = subprocess.Popen([sys.executable, "-c",
385 'import sys; sys.stderr.write("pineapple")'],
386 stderr=subprocess.PIPE)
387 (stdout, stderr) = p.communicate()
388 self.assertEqual(stdout, None)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000389 self.assertStderrEqual(stderr, "pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000390
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000391 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000392 p = subprocess.Popen([sys.executable, "-c",
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000393 'import sys,os;'
394 'sys.stderr.write("pineapple");'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000395 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000396 stdin=subprocess.PIPE,
397 stdout=subprocess.PIPE,
398 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000399 self.addCleanup(p.stdout.close)
400 self.addCleanup(p.stderr.close)
401 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000402 (stdout, stderr) = p.communicate("banana")
403 self.assertEqual(stdout, "banana")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000404 self.assertStderrEqual(stderr, "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000405
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000406 # This test is Linux specific for simplicity to at least have
407 # some coverage. It is not a platform specific bug.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000408 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
409 "Linux specific")
410 # Test for the fd leak reported in http://bugs.python.org/issue2791.
411 def test_communicate_pipe_fd_leak(self):
412 fd_directory = '/proc/%d/fd' % os.getpid()
413 num_fds_before_popen = len(os.listdir(fd_directory))
414 p = subprocess.Popen([sys.executable, "-c", "print()"],
415 stdout=subprocess.PIPE)
416 p.communicate()
417 num_fds_after_communicate = len(os.listdir(fd_directory))
418 del p
419 num_fds_after_destruction = len(os.listdir(fd_directory))
420 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
421 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000422
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000423 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000424 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000425 p = subprocess.Popen([sys.executable, "-c",
426 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000427 (stdout, stderr) = p.communicate()
428 self.assertEqual(stdout, None)
429 self.assertEqual(stderr, None)
430
431 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000432 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000433 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000434 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000435 x, y = os.pipe()
436 if mswindows:
437 pipe_buf = 512
438 else:
439 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
440 os.close(x)
441 os.close(y)
442 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000443 'import sys,os;'
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000444 'sys.stdout.write(sys.stdin.read(47));'
445 'sys.stderr.write("xyz"*%d);'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000446 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000447 stdin=subprocess.PIPE,
448 stdout=subprocess.PIPE,
449 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000450 self.addCleanup(p.stdout.close)
451 self.addCleanup(p.stderr.close)
452 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000453 string_to_write = "abc"*pipe_buf
454 (stdout, stderr) = p.communicate(string_to_write)
455 self.assertEqual(stdout, string_to_write)
456
457 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000458 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000459 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000460 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000461 'sys.stdout.write(sys.stdin.read())'],
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 p.stdin.write("banana")
469 (stdout, stderr) = p.communicate("split")
470 self.assertEqual(stdout, "bananasplit")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000471 self.assertStderrEqual(stderr, "")
Tim Peterse718f612004-10-12 21:51:32 +0000472
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000473 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000474 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000475 'import sys,os;' + SETBINARY +
476 'sys.stdout.write("line1\\n");'
477 'sys.stdout.flush();'
478 'sys.stdout.write("line2\\r");'
479 'sys.stdout.flush();'
480 'sys.stdout.write("line3\\r\\n");'
481 'sys.stdout.flush();'
482 'sys.stdout.write("line4\\r");'
483 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000484 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000485 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000486 'sys.stdout.write("\\nline6");'],
487 stdout=subprocess.PIPE,
488 universal_newlines=1)
Brian Curtind117b562010-11-05 04:09:09 +0000489 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000490 stdout = p.stdout.read()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000491 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000492 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000493 self.assertEqual(stdout,
494 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000495 else:
496 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000497 self.assertEqual(stdout,
498 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000499
500 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000501 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000502 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000503 'import sys,os;' + SETBINARY +
504 'sys.stdout.write("line1\\n");'
505 'sys.stdout.flush();'
506 'sys.stdout.write("line2\\r");'
507 'sys.stdout.flush();'
508 'sys.stdout.write("line3\\r\\n");'
509 'sys.stdout.flush();'
510 'sys.stdout.write("line4\\r");'
511 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000512 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000513 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000514 'sys.stdout.write("\\nline6");'],
515 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
516 universal_newlines=1)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000517 self.addCleanup(p.stdout.close)
518 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000519 (stdout, stderr) = p.communicate()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000520 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000521 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000522 self.assertEqual(stdout,
523 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000524 else:
525 # Interpreter without universal newline support
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000526 self.assertEqual(stdout,
527 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000528
529 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000530 # Make sure we leak no resources
Antoine Pitroub0b3bff2010-09-18 22:42:30 +0000531 if not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000532 max_handles = 1026 # too much for most UNIX systems
533 else:
Antoine Pitroub0b3bff2010-09-18 22:42:30 +0000534 max_handles = 2050 # too much for (at least some) Windows setups
535 handles = []
536 try:
537 for i in range(max_handles):
538 try:
539 handles.append(os.open(test_support.TESTFN,
540 os.O_WRONLY | os.O_CREAT))
541 except OSError as e:
542 if e.errno != errno.EMFILE:
543 raise
544 break
545 else:
546 self.skipTest("failed to reach the file descriptor limit "
547 "(tried %d)" % max_handles)
548 # Close a couple of them (should be enough for a subprocess)
549 for i in range(10):
550 os.close(handles.pop())
551 # Loop creating some subprocesses. If one of them leaks some fds,
552 # the next loop iteration will fail by reaching the max fd limit.
553 for i in range(15):
554 p = subprocess.Popen([sys.executable, "-c",
555 "import sys;"
556 "sys.stdout.write(sys.stdin.read())"],
557 stdin=subprocess.PIPE,
558 stdout=subprocess.PIPE,
559 stderr=subprocess.PIPE)
560 data = p.communicate(b"lime")[0]
561 self.assertEqual(data, b"lime")
562 finally:
563 for h in handles:
564 os.close(h)
Mark Dickinson313dc9b2012-10-07 15:41:38 +0100565 test_support.unlink(test_support.TESTFN)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000566
567 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000568 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
569 '"a b c" d e')
570 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
571 'ab\\"c \\ d')
Gregory P. Smithe047e6d2008-01-19 20:49:02 +0000572 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
573 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000574 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
575 'a\\\\\\b "de fg" h')
576 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
577 'a\\\\\\"b c d')
578 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
579 '"a\\\\b c" d e')
580 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
581 '"a\\\\b\\ c" d e')
Peter Astrand10514a72007-01-13 22:35:35 +0000582 self.assertEqual(subprocess.list2cmdline(['ab', '']),
583 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000584
585
586 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000587 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000588 "-c", "import time; time.sleep(1)"])
589 count = 0
590 while p.poll() is None:
591 time.sleep(0.1)
592 count += 1
593 # We expect that the poll loop probably went around about 10 times,
594 # but, based on system scheduling we can't control, it's possible
595 # poll() never returned None. It "should be" very rare that it
596 # didn't go around at least twice.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000597 self.assertGreaterEqual(count, 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000598 # Subsequent invocations should just return the returncode
599 self.assertEqual(p.poll(), 0)
600
601
602 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000603 p = subprocess.Popen([sys.executable,
604 "-c", "import time; time.sleep(2)"])
605 self.assertEqual(p.wait(), 0)
606 # Subsequent invocations should just return the returncode
607 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000608
Peter Astrand738131d2004-11-30 21:04:45 +0000609
610 def test_invalid_bufsize(self):
611 # an invalid type of the bufsize argument should raise
612 # TypeError.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000613 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +0000614 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +0000615
Georg Brandlf3715d22009-02-14 17:01:36 +0000616 def test_leaking_fds_on_error(self):
617 # see bug #5179: Popen leaks file descriptors to PIPEs if
618 # the child fails to execute; this will eventually exhaust
619 # the maximum number of open fds. 1024 seems a very common
620 # value for that limit, but Windows has 2048, so we loop
621 # 1024 times (each call leaked two fds).
622 for i in range(1024):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000623 # Windows raises IOError. Others raise OSError.
624 with self.assertRaises(EnvironmentError) as c:
Georg Brandlf3715d22009-02-14 17:01:36 +0000625 subprocess.Popen(['nonexisting_i_hope'],
626 stdout=subprocess.PIPE,
627 stderr=subprocess.PIPE)
R David Murraycdd5fc92011-03-13 22:37:18 -0400628 # ignore errors that indicate the command was not found
629 if c.exception.errno not in (errno.ENOENT, errno.EACCES):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000630 raise c.exception
Georg Brandlf3715d22009-02-14 17:01:36 +0000631
Tim Golden90374f52010-08-06 13:14:33 +0000632 def test_handles_closed_on_exception(self):
633 # If CreateProcess exits with an error, ensure the
634 # duplicate output handles are released
635 ifhandle, ifname = mkstemp()
636 ofhandle, ofname = mkstemp()
637 efhandle, efname = mkstemp()
638 try:
639 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
640 stderr=efhandle)
641 except OSError:
642 os.close(ifhandle)
643 os.remove(ifname)
644 os.close(ofhandle)
645 os.remove(ofname)
646 os.close(efhandle)
647 os.remove(efname)
648 self.assertFalse(os.path.exists(ifname))
649 self.assertFalse(os.path.exists(ofname))
650 self.assertFalse(os.path.exists(efname))
651
Ross Lagerwall104c3f12011-04-05 15:24:34 +0200652 def test_communicate_epipe(self):
653 # Issue 10963: communicate() should hide EPIPE
654 p = subprocess.Popen([sys.executable, "-c", 'pass'],
655 stdin=subprocess.PIPE,
656 stdout=subprocess.PIPE,
657 stderr=subprocess.PIPE)
658 self.addCleanup(p.stdout.close)
659 self.addCleanup(p.stderr.close)
660 self.addCleanup(p.stdin.close)
661 p.communicate("x" * 2**20)
662
663 def test_communicate_epipe_only_stdin(self):
664 # Issue 10963: communicate() should hide EPIPE
665 p = subprocess.Popen([sys.executable, "-c", 'pass'],
666 stdin=subprocess.PIPE)
667 self.addCleanup(p.stdin.close)
668 time.sleep(2)
669 p.communicate("x" * 2**20)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000670
Gregory P. Smith9d3b6e92012-11-10 22:49:03 -0800671 # This test is Linux-ish specific for simplicity to at least have
672 # some coverage. It is not a platform specific bug.
673 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
674 "Linux specific")
675 def test_failed_child_execute_fd_leak(self):
676 """Test for the fork() failure fd leak reported in issue16327."""
677 fd_directory = '/proc/%d/fd' % os.getpid()
678 fds_before_popen = os.listdir(fd_directory)
679 with self.assertRaises(PopenTestException):
680 PopenExecuteChildRaises(
681 [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE,
682 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
683
684 # NOTE: This test doesn't verify that the real _execute_child
685 # does not close the file descriptors itself on the way out
686 # during an exception. Code inspection has confirmed that.
687
688 fds_after_exception = os.listdir(fd_directory)
689 self.assertEqual(fds_before_popen, fds_after_exception)
690
691
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000692# context manager
693class _SuppressCoreFiles(object):
694 """Try to prevent core files from being created."""
695 old_limit = None
696
697 def __enter__(self):
698 """Try to save previous ulimit, then set it to (0, 0)."""
Benjamin Peterson8b59c232011-12-10 12:31:42 -0500699 if resource is not None:
700 try:
701 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
702 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
703 except (ValueError, resource.error):
704 pass
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000705
Ronald Oussoren21b44e02010-07-23 12:26:30 +0000706 if sys.platform == 'darwin':
707 # Check if the 'Crash Reporter' on OSX was configured
708 # in 'Developer' mode and warn that it will get triggered
709 # when it is.
710 #
711 # This assumes that this context manager is used in tests
712 # that might trigger the next manager.
713 value = subprocess.Popen(['/usr/bin/defaults', 'read',
714 'com.apple.CrashReporter', 'DialogType'],
715 stdout=subprocess.PIPE).communicate()[0]
716 if value.strip() == b'developer':
717 print "this tests triggers the Crash Reporter, that is intentional"
718 sys.stdout.flush()
719
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000720 def __exit__(self, *args):
721 """Return core file behavior to default."""
722 if self.old_limit is None:
723 return
Benjamin Peterson8b59c232011-12-10 12:31:42 -0500724 if resource is not None:
725 try:
726 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
727 except (ValueError, resource.error):
728 pass
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000729
Victor Stinnerb78fed92011-07-05 14:50:35 +0200730 @unittest.skipUnless(hasattr(signal, 'SIGALRM'),
731 "Requires signal.SIGALRM")
Victor Stinnere7901312011-07-05 14:08:01 +0200732 def test_communicate_eintr(self):
733 # Issue #12493: communicate() should handle EINTR
734 def handler(signum, frame):
735 pass
736 old_handler = signal.signal(signal.SIGALRM, handler)
737 self.addCleanup(signal.signal, signal.SIGALRM, old_handler)
738
739 # the process is running for 2 seconds
740 args = [sys.executable, "-c", 'import time; time.sleep(2)']
741 for stream in ('stdout', 'stderr'):
742 kw = {stream: subprocess.PIPE}
743 with subprocess.Popen(args, **kw) as process:
744 signal.alarm(1)
745 # communicate() will be interrupted by SIGALRM
746 process.communicate()
747
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000748
Florent Xiclunabab22a72010-03-04 19:40:48 +0000749@unittest.skipIf(mswindows, "POSIX specific tests")
Florent Xiclunafc4d6d72010-03-23 14:36:45 +0000750class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaab5e17f2010-03-04 21:31:58 +0000751
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000752 def test_exceptions(self):
753 # caught & re-raised exceptions
754 with self.assertRaises(OSError) as c:
755 p = subprocess.Popen([sys.executable, "-c", ""],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000756 cwd="/this/path/does/not/exist")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000757 # The attribute child_traceback should contain "os.chdir" somewhere.
758 self.assertIn("os.chdir", c.exception.child_traceback)
Tim Peterse718f612004-10-12 21:51:32 +0000759
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000760 def test_run_abort(self):
761 # returncode handles signal termination
762 with _SuppressCoreFiles():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000763 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000764 "import os; os.abort()"])
765 p.wait()
766 self.assertEqual(-p.returncode, signal.SIGABRT)
767
768 def test_preexec(self):
769 # preexec function
770 p = subprocess.Popen([sys.executable, "-c",
771 "import sys, os;"
772 "sys.stdout.write(os.getenv('FRUIT'))"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000773 stdout=subprocess.PIPE,
774 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Brian Curtind117b562010-11-05 04:09:09 +0000775 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000776 self.assertEqual(p.stdout.read(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000777
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800778 class _TestExecuteChildPopen(subprocess.Popen):
779 """Used to test behavior at the end of _execute_child."""
780 def __init__(self, testcase, *args, **kwargs):
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800781 self._testcase = testcase
782 subprocess.Popen.__init__(self, *args, **kwargs)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800783
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800784 def _execute_child(
785 self, args, executable, preexec_fn, close_fds, cwd, env,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800786 universal_newlines, startupinfo, creationflags, shell,
787 p2cread, p2cwrite,
788 c2pread, c2pwrite,
789 errread, errwrite):
790 try:
791 subprocess.Popen._execute_child(
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800792 self, args, executable, preexec_fn, close_fds,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800793 cwd, env, universal_newlines,
794 startupinfo, creationflags, shell,
795 p2cread, p2cwrite,
796 c2pread, c2pwrite,
797 errread, errwrite)
798 finally:
799 # Open a bunch of file descriptors and verify that
800 # none of them are the same as the ones the Popen
801 # instance is using for stdin/stdout/stderr.
802 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
803 for _ in range(8)]
804 try:
805 for fd in devzero_fds:
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800806 self._testcase.assertNotIn(
807 fd, (p2cwrite, c2pread, errread))
Gregory P. Smith211248b2012-11-11 02:00:49 -0800808 finally:
Richard Oudkerk045e4572013-06-10 16:27:45 +0100809 for fd in devzero_fds:
810 os.close(fd)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800811
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800812 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
813 def test_preexec_errpipe_does_not_double_close_pipes(self):
814 """Issue16140: Don't double close pipes on preexec error."""
815
816 def raise_it():
817 raise RuntimeError("force the _execute_child() errpipe_data path.")
Gregory P. Smith211248b2012-11-11 02:00:49 -0800818
819 with self.assertRaises(RuntimeError):
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800820 self._TestExecuteChildPopen(
821 self, [sys.executable, "-c", "pass"],
822 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
823 stderr=subprocess.PIPE, preexec_fn=raise_it)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800824
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000825 def test_args_string(self):
826 # args is a string
827 f, fname = mkstemp()
828 os.write(f, "#!/bin/sh\n")
829 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
830 sys.executable)
831 os.close(f)
832 os.chmod(fname, 0o700)
833 p = subprocess.Popen(fname)
834 p.wait()
835 os.remove(fname)
836 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000837
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000838 def test_invalid_args(self):
839 # invalid arguments should raise ValueError
840 self.assertRaises(ValueError, subprocess.call,
841 [sys.executable, "-c",
842 "import sys; sys.exit(47)"],
843 startupinfo=47)
844 self.assertRaises(ValueError, subprocess.call,
845 [sys.executable, "-c",
846 "import sys; sys.exit(47)"],
847 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000848
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000849 def test_shell_sequence(self):
850 # Run command through the shell (sequence)
851 newenv = os.environ.copy()
852 newenv["FRUIT"] = "apple"
853 p = subprocess.Popen(["echo $FRUIT"], shell=1,
854 stdout=subprocess.PIPE,
855 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000856 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000857 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000858
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000859 def test_shell_string(self):
860 # Run command through the shell (string)
861 newenv = os.environ.copy()
862 newenv["FRUIT"] = "apple"
863 p = subprocess.Popen("echo $FRUIT", shell=1,
864 stdout=subprocess.PIPE,
865 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000866 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000867 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000868
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000869 def test_call_string(self):
870 # call() function with string argument on UNIX
871 f, fname = mkstemp()
872 os.write(f, "#!/bin/sh\n")
873 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
874 sys.executable)
875 os.close(f)
876 os.chmod(fname, 0700)
877 rc = subprocess.call(fname)
878 os.remove(fname)
879 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000880
Stefan Krahe9a6a7d2010-07-19 14:41:08 +0000881 def test_specific_shell(self):
882 # Issue #9265: Incorrect name passed as arg[0].
883 shells = []
884 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
885 for name in ['bash', 'ksh']:
886 sh = os.path.join(prefix, name)
887 if os.path.isfile(sh):
888 shells.append(sh)
889 if not shells: # Will probably work for any shell but csh.
890 self.skipTest("bash or ksh required for this test")
891 sh = '/bin/sh'
892 if os.path.isfile(sh) and not os.path.islink(sh):
893 # Test will fail if /bin/sh is a symlink to csh.
894 shells.append(sh)
895 for sh in shells:
896 p = subprocess.Popen("echo $0", executable=sh, shell=True,
897 stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000898 self.addCleanup(p.stdout.close)
Stefan Krahe9a6a7d2010-07-19 14:41:08 +0000899 self.assertEqual(p.stdout.read().strip(), sh)
900
Florent Xiclunac0838642010-03-07 15:27:39 +0000901 def _kill_process(self, method, *args):
Florent Xiclunacecef392010-03-05 19:31:21 +0000902 # Do not inherit file handles from the parent.
903 # It should fix failures on some platforms.
Antoine Pitroua6166da2010-09-20 11:20:44 +0000904 p = subprocess.Popen([sys.executable, "-c", """if 1:
905 import sys, time
906 sys.stdout.write('x\\n')
907 sys.stdout.flush()
908 time.sleep(30)
909 """],
910 close_fds=True,
911 stdin=subprocess.PIPE,
912 stdout=subprocess.PIPE,
913 stderr=subprocess.PIPE)
914 # Wait for the interpreter to be completely initialized before
915 # sending any signal.
916 p.stdout.read(1)
917 getattr(p, method)(*args)
Florent Xiclunac0838642010-03-07 15:27:39 +0000918 return p
919
Charles-François Natalief2bd672013-01-12 16:52:20 +0100920 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
921 "Due to known OS bug (issue #16762)")
Antoine Pitrouf60845b2012-03-11 19:29:12 +0100922 def _kill_dead_process(self, method, *args):
923 # Do not inherit file handles from the parent.
924 # It should fix failures on some platforms.
925 p = subprocess.Popen([sys.executable, "-c", """if 1:
926 import sys, time
927 sys.stdout.write('x\\n')
928 sys.stdout.flush()
929 """],
930 close_fds=True,
931 stdin=subprocess.PIPE,
932 stdout=subprocess.PIPE,
933 stderr=subprocess.PIPE)
934 # Wait for the interpreter to be completely initialized before
935 # sending any signal.
936 p.stdout.read(1)
937 # The process should end after this
938 time.sleep(1)
939 # This shouldn't raise even though the child is now dead
940 getattr(p, method)(*args)
941 p.communicate()
942
Florent Xiclunac0838642010-03-07 15:27:39 +0000943 def test_send_signal(self):
944 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunafc4d6d72010-03-23 14:36:45 +0000945 _, stderr = p.communicate()
Florent Xicluna3c919cf2010-03-23 19:19:16 +0000946 self.assertIn('KeyboardInterrupt', stderr)
Florent Xicluna446ff142010-03-23 15:05:30 +0000947 self.assertNotEqual(p.wait(), 0)
Christian Heimese74c8f22008-04-19 02:23:57 +0000948
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000949 def test_kill(self):
Florent Xiclunac0838642010-03-07 15:27:39 +0000950 p = self._kill_process('kill')
Florent Xicluna446ff142010-03-23 15:05:30 +0000951 _, stderr = p.communicate()
952 self.assertStderrEqual(stderr, '')
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000953 self.assertEqual(p.wait(), -signal.SIGKILL)
Christian Heimese74c8f22008-04-19 02:23:57 +0000954
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000955 def test_terminate(self):
Florent Xiclunac0838642010-03-07 15:27:39 +0000956 p = self._kill_process('terminate')
Florent Xicluna446ff142010-03-23 15:05:30 +0000957 _, stderr = p.communicate()
958 self.assertStderrEqual(stderr, '')
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000959 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000960
Antoine Pitrouf60845b2012-03-11 19:29:12 +0100961 def test_send_signal_dead(self):
962 # Sending a signal to a dead process
963 self._kill_dead_process('send_signal', signal.SIGINT)
964
965 def test_kill_dead(self):
966 # Killing a dead process
967 self._kill_dead_process('kill')
968
969 def test_terminate_dead(self):
970 # Terminating a dead process
971 self._kill_dead_process('terminate')
972
Antoine Pitrou91ce0d92011-01-03 18:45:09 +0000973 def check_close_std_fds(self, fds):
974 # Issue #9905: test that subprocess pipes still work properly with
975 # some standard fds closed
976 stdin = 0
977 newfds = []
978 for a in fds:
979 b = os.dup(a)
980 newfds.append(b)
981 if a == 0:
982 stdin = b
983 try:
984 for fd in fds:
985 os.close(fd)
986 out, err = subprocess.Popen([sys.executable, "-c",
987 'import sys;'
988 'sys.stdout.write("apple");'
989 'sys.stdout.flush();'
990 'sys.stderr.write("orange")'],
991 stdin=stdin,
992 stdout=subprocess.PIPE,
993 stderr=subprocess.PIPE).communicate()
994 err = test_support.strip_python_stderr(err)
995 self.assertEqual((out, err), (b'apple', b'orange'))
996 finally:
997 for b, a in zip(newfds, fds):
998 os.dup2(b, a)
999 for b in newfds:
1000 os.close(b)
1001
1002 def test_close_fd_0(self):
1003 self.check_close_std_fds([0])
1004
1005 def test_close_fd_1(self):
1006 self.check_close_std_fds([1])
1007
1008 def test_close_fd_2(self):
1009 self.check_close_std_fds([2])
1010
1011 def test_close_fds_0_1(self):
1012 self.check_close_std_fds([0, 1])
1013
1014 def test_close_fds_0_2(self):
1015 self.check_close_std_fds([0, 2])
1016
1017 def test_close_fds_1_2(self):
1018 self.check_close_std_fds([1, 2])
1019
1020 def test_close_fds_0_1_2(self):
1021 # Issue #10806: test that subprocess pipes still work properly with
1022 # all standard fds closed.
1023 self.check_close_std_fds([0, 1, 2])
1024
Ross Lagerwalld8e39012011-07-27 18:54:53 +02001025 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
1026 # open up some temporary files
1027 temps = [mkstemp() for i in range(3)]
1028 temp_fds = [fd for fd, fname in temps]
1029 try:
1030 # unlink the files -- we won't need to reopen them
1031 for fd, fname in temps:
1032 os.unlink(fname)
1033
1034 # save a copy of the standard file descriptors
1035 saved_fds = [os.dup(fd) for fd in range(3)]
1036 try:
1037 # duplicate the temp files over the standard fd's 0, 1, 2
1038 for fd, temp_fd in enumerate(temp_fds):
1039 os.dup2(temp_fd, fd)
1040
1041 # write some data to what will become stdin, and rewind
1042 os.write(stdin_no, b"STDIN")
1043 os.lseek(stdin_no, 0, 0)
1044
1045 # now use those files in the given order, so that subprocess
1046 # has to rearrange them in the child
1047 p = subprocess.Popen([sys.executable, "-c",
1048 'import sys; got = sys.stdin.read();'
1049 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
1050 stdin=stdin_no,
1051 stdout=stdout_no,
1052 stderr=stderr_no)
1053 p.wait()
1054
1055 for fd in temp_fds:
1056 os.lseek(fd, 0, 0)
1057
1058 out = os.read(stdout_no, 1024)
1059 err = test_support.strip_python_stderr(os.read(stderr_no, 1024))
1060 finally:
1061 for std, saved in enumerate(saved_fds):
1062 os.dup2(saved, std)
1063 os.close(saved)
1064
1065 self.assertEqual(out, b"got STDIN")
1066 self.assertEqual(err, b"err")
1067
1068 finally:
1069 for fd in temp_fds:
1070 os.close(fd)
1071
1072 # When duping fds, if there arises a situation where one of the fds is
1073 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
1074 # This tests all combinations of this.
1075 def test_swap_fds(self):
1076 self.check_swap_fds(0, 1, 2)
1077 self.check_swap_fds(0, 2, 1)
1078 self.check_swap_fds(1, 0, 2)
1079 self.check_swap_fds(1, 2, 0)
1080 self.check_swap_fds(2, 0, 1)
1081 self.check_swap_fds(2, 1, 0)
1082
Gregory P. Smith312efbc2010-12-14 15:02:53 +00001083 def test_wait_when_sigchild_ignored(self):
1084 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
1085 sigchild_ignore = test_support.findfile("sigchild_ignore.py",
1086 subdir="subprocessdata")
1087 p = subprocess.Popen([sys.executable, sigchild_ignore],
1088 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1089 stdout, stderr = p.communicate()
1090 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
1091 " non-zero with this error:\n%s" % stderr)
1092
Charles-François Natali100df0f2011-08-18 17:56:02 +02001093 def test_zombie_fast_process_del(self):
1094 # Issue #12650: on Unix, if Popen.__del__() was called before the
1095 # process exited, it wouldn't be added to subprocess._active, and would
1096 # remain a zombie.
1097 # spawn a Popen, and delete its reference before it exits
1098 p = subprocess.Popen([sys.executable, "-c",
1099 'import sys, time;'
1100 'time.sleep(0.2)'],
1101 stdout=subprocess.PIPE,
1102 stderr=subprocess.PIPE)
Nadeem Vawda86059362011-08-19 05:22:24 +02001103 self.addCleanup(p.stdout.close)
1104 self.addCleanup(p.stderr.close)
Charles-François Natali100df0f2011-08-18 17:56:02 +02001105 ident = id(p)
1106 pid = p.pid
1107 del p
1108 # check that p is in the active processes list
1109 self.assertIn(ident, [id(o) for o in subprocess._active])
1110
Charles-François Natali100df0f2011-08-18 17:56:02 +02001111 def test_leak_fast_process_del_killed(self):
1112 # Issue #12650: on Unix, if Popen.__del__() was called before the
1113 # process exited, and the process got killed by a signal, it would never
1114 # be removed from subprocess._active, which triggered a FD and memory
1115 # leak.
1116 # spawn a Popen, delete its reference and kill it
1117 p = subprocess.Popen([sys.executable, "-c",
1118 'import time;'
1119 'time.sleep(3)'],
1120 stdout=subprocess.PIPE,
1121 stderr=subprocess.PIPE)
Nadeem Vawda86059362011-08-19 05:22:24 +02001122 self.addCleanup(p.stdout.close)
1123 self.addCleanup(p.stderr.close)
Charles-François Natali100df0f2011-08-18 17:56:02 +02001124 ident = id(p)
1125 pid = p.pid
1126 del p
1127 os.kill(pid, signal.SIGKILL)
1128 # check that p is in the active processes list
1129 self.assertIn(ident, [id(o) for o in subprocess._active])
1130
1131 # let some time for the process to exit, and create a new Popen: this
1132 # should trigger the wait() of p
1133 time.sleep(0.2)
1134 with self.assertRaises(EnvironmentError) as c:
1135 with subprocess.Popen(['nonexisting_i_hope'],
1136 stdout=subprocess.PIPE,
1137 stderr=subprocess.PIPE) as proc:
1138 pass
1139 # p should have been wait()ed on, and removed from the _active list
1140 self.assertRaises(OSError, os.waitpid, pid, 0)
1141 self.assertNotIn(ident, [id(o) for o in subprocess._active])
1142
Charles-François Natali2a34eb32011-08-25 21:20:54 +02001143 def test_pipe_cloexec(self):
1144 # Issue 12786: check that the communication pipes' FDs are set CLOEXEC,
1145 # and are not inherited by another child process.
1146 p1 = subprocess.Popen([sys.executable, "-c",
1147 'import os;'
1148 'os.read(0, 1)'
1149 ],
1150 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1151 stderr=subprocess.PIPE)
1152
1153 p2 = subprocess.Popen([sys.executable, "-c", """if True:
1154 import os, errno, sys
1155 for fd in %r:
1156 try:
1157 os.close(fd)
1158 except OSError as e:
1159 if e.errno != errno.EBADF:
1160 raise
1161 else:
1162 sys.exit(1)
1163 sys.exit(0)
1164 """ % [f.fileno() for f in (p1.stdin, p1.stdout,
1165 p1.stderr)]
1166 ],
1167 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1168 stderr=subprocess.PIPE, close_fds=False)
1169 p1.communicate('foo')
1170 _, stderr = p2.communicate()
1171
1172 self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr))
1173
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001174
Florent Xiclunabab22a72010-03-04 19:40:48 +00001175@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunafc4d6d72010-03-23 14:36:45 +00001176class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaab5e17f2010-03-04 21:31:58 +00001177
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001178 def test_startupinfo(self):
1179 # startupinfo argument
1180 # We uses hardcoded constants, because we do not want to
1181 # depend on win32all.
1182 STARTF_USESHOWWINDOW = 1
1183 SW_MAXIMIZE = 3
1184 startupinfo = subprocess.STARTUPINFO()
1185 startupinfo.dwFlags = STARTF_USESHOWWINDOW
1186 startupinfo.wShowWindow = SW_MAXIMIZE
1187 # Since Python is a console process, it won't be affected
1188 # by wShowWindow, but the argument should be silently
1189 # ignored
1190 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001191 startupinfo=startupinfo)
1192
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001193 def test_creationflags(self):
1194 # creationflags argument
1195 CREATE_NEW_CONSOLE = 16
1196 sys.stderr.write(" a DOS box should flash briefly ...\n")
1197 subprocess.call(sys.executable +
1198 ' -c "import time; time.sleep(0.25)"',
1199 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001200
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001201 def test_invalid_args(self):
1202 # invalid arguments should raise ValueError
1203 self.assertRaises(ValueError, subprocess.call,
1204 [sys.executable, "-c",
1205 "import sys; sys.exit(47)"],
1206 preexec_fn=lambda: 1)
1207 self.assertRaises(ValueError, subprocess.call,
1208 [sys.executable, "-c",
1209 "import sys; sys.exit(47)"],
1210 stdout=subprocess.PIPE,
1211 close_fds=True)
1212
1213 def test_close_fds(self):
1214 # close file descriptors
1215 rc = subprocess.call([sys.executable, "-c",
1216 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001217 close_fds=True)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001218 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001219
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001220 def test_shell_sequence(self):
1221 # Run command through the shell (sequence)
1222 newenv = os.environ.copy()
1223 newenv["FRUIT"] = "physalis"
1224 p = subprocess.Popen(["set"], shell=1,
1225 stdout=subprocess.PIPE,
1226 env=newenv)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001227 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001228 self.assertIn("physalis", p.stdout.read())
Peter Astrand81a191b2007-05-26 22:18:20 +00001229
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001230 def test_shell_string(self):
1231 # Run command through the shell (string)
1232 newenv = os.environ.copy()
1233 newenv["FRUIT"] = "physalis"
1234 p = subprocess.Popen("set", shell=1,
1235 stdout=subprocess.PIPE,
1236 env=newenv)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001237 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001238 self.assertIn("physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001239
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001240 def test_call_string(self):
1241 # call() function with string argument on Windows
1242 rc = subprocess.call(sys.executable +
1243 ' -c "import sys; sys.exit(47)"')
1244 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001245
Florent Xiclunac0838642010-03-07 15:27:39 +00001246 def _kill_process(self, method, *args):
Florent Xicluna400efc22010-03-07 17:12:23 +00001247 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroudee00972010-09-24 19:00:29 +00001248 p = subprocess.Popen([sys.executable, "-c", """if 1:
1249 import sys, time
1250 sys.stdout.write('x\\n')
1251 sys.stdout.flush()
1252 time.sleep(30)
1253 """],
1254 stdin=subprocess.PIPE,
1255 stdout=subprocess.PIPE,
1256 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001257 self.addCleanup(p.stdout.close)
1258 self.addCleanup(p.stderr.close)
1259 self.addCleanup(p.stdin.close)
Antoine Pitroudee00972010-09-24 19:00:29 +00001260 # Wait for the interpreter to be completely initialized before
1261 # sending any signal.
1262 p.stdout.read(1)
1263 getattr(p, method)(*args)
Florent Xicluna446ff142010-03-23 15:05:30 +00001264 _, stderr = p.communicate()
1265 self.assertStderrEqual(stderr, '')
Antoine Pitroudee00972010-09-24 19:00:29 +00001266 returncode = p.wait()
Florent Xiclunafaf17532010-03-08 10:59:33 +00001267 self.assertNotEqual(returncode, 0)
Florent Xiclunac0838642010-03-07 15:27:39 +00001268
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001269 def _kill_dead_process(self, method, *args):
1270 p = subprocess.Popen([sys.executable, "-c", """if 1:
1271 import sys, time
1272 sys.stdout.write('x\\n')
1273 sys.stdout.flush()
1274 sys.exit(42)
1275 """],
1276 stdin=subprocess.PIPE,
1277 stdout=subprocess.PIPE,
1278 stderr=subprocess.PIPE)
1279 self.addCleanup(p.stdout.close)
1280 self.addCleanup(p.stderr.close)
1281 self.addCleanup(p.stdin.close)
1282 # Wait for the interpreter to be completely initialized before
1283 # sending any signal.
1284 p.stdout.read(1)
1285 # The process should end after this
1286 time.sleep(1)
1287 # This shouldn't raise even though the child is now dead
1288 getattr(p, method)(*args)
1289 _, stderr = p.communicate()
1290 self.assertStderrEqual(stderr, b'')
1291 rc = p.wait()
1292 self.assertEqual(rc, 42)
1293
Florent Xiclunac0838642010-03-07 15:27:39 +00001294 def test_send_signal(self):
1295 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimese74c8f22008-04-19 02:23:57 +00001296
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001297 def test_kill(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001298 self._kill_process('kill')
Christian Heimese74c8f22008-04-19 02:23:57 +00001299
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001300 def test_terminate(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001301 self._kill_process('terminate')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001302
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001303 def test_send_signal_dead(self):
1304 self._kill_dead_process('send_signal', signal.SIGTERM)
1305
1306 def test_kill_dead(self):
1307 self._kill_dead_process('kill')
1308
1309 def test_terminate_dead(self):
1310 self._kill_dead_process('terminate')
1311
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001312
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001313@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
1314 "poll system call not supported")
1315class ProcessTestCaseNoPoll(ProcessTestCase):
1316 def setUp(self):
1317 subprocess._has_poll = False
1318 ProcessTestCase.setUp(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001319
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001320 def tearDown(self):
1321 subprocess._has_poll = True
1322 ProcessTestCase.tearDown(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001323
1324
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001325class HelperFunctionTests(unittest.TestCase):
Gregory P. Smithc1baf4a2010-03-01 02:53:24 +00001326 @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001327 def test_eintr_retry_call(self):
1328 record_calls = []
1329 def fake_os_func(*args):
1330 record_calls.append(args)
1331 if len(record_calls) == 2:
1332 raise OSError(errno.EINTR, "fake interrupted system call")
1333 return tuple(reversed(args))
1334
1335 self.assertEqual((999, 256),
1336 subprocess._eintr_retry_call(fake_os_func, 256, 999))
1337 self.assertEqual([(256, 999)], record_calls)
1338 # This time there will be an EINTR so it will loop once.
1339 self.assertEqual((666,),
1340 subprocess._eintr_retry_call(fake_os_func, 666))
1341 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
1342
Tim Golden8e4756c2010-08-12 11:00:35 +00001343@unittest.skipUnless(mswindows, "mswindows only")
1344class CommandsWithSpaces (BaseTestCase):
1345
1346 def setUp(self):
1347 super(CommandsWithSpaces, self).setUp()
1348 f, fname = mkstemp(".py", "te st")
1349 self.fname = fname.lower ()
1350 os.write(f, b"import sys;"
1351 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
1352 )
1353 os.close(f)
1354
1355 def tearDown(self):
1356 os.remove(self.fname)
1357 super(CommandsWithSpaces, self).tearDown()
1358
1359 def with_spaces(self, *args, **kwargs):
1360 kwargs['stdout'] = subprocess.PIPE
1361 p = subprocess.Popen(*args, **kwargs)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001362 self.addCleanup(p.stdout.close)
Tim Golden8e4756c2010-08-12 11:00:35 +00001363 self.assertEqual(
1364 p.stdout.read ().decode("mbcs"),
1365 "2 [%r, 'ab cd']" % self.fname
1366 )
1367
1368 def test_shell_string_with_spaces(self):
1369 # call() function with string argument with spaces on Windows
Brian Curtine8c49202010-08-13 21:01:52 +00001370 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1371 "ab cd"), shell=1)
Tim Golden8e4756c2010-08-12 11:00:35 +00001372
1373 def test_shell_sequence_with_spaces(self):
1374 # call() function with sequence argument with spaces on Windows
Brian Curtine8c49202010-08-13 21:01:52 +00001375 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden8e4756c2010-08-12 11:00:35 +00001376
1377 def test_noshell_string_with_spaces(self):
1378 # call() function with string argument with spaces on Windows
1379 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1380 "ab cd"))
1381
1382 def test_noshell_sequence_with_spaces(self):
1383 # call() function with sequence argument with spaces on Windows
1384 self.with_spaces([sys.executable, self.fname, "ab cd"])
1385
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001386def test_main():
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001387 unit_tests = (ProcessTestCase,
1388 POSIXProcessTestCase,
1389 Win32ProcessTestCase,
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001390 ProcessTestCaseNoPoll,
Tim Golden8e4756c2010-08-12 11:00:35 +00001391 HelperFunctionTests,
1392 CommandsWithSpaces)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001393
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001394 test_support.run_unittest(*unit_tests)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001395 test_support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001396
1397if __name__ == "__main__":
1398 test_main()