blob: a5a727efd62d4301f261b34c38623a1be94fdef9 [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001import unittest
2from test import test_support
3import subprocess
4import sys
Gregory P. Smithf0739cb2017-01-22 22:38:28 -08005import platform
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00006import signal
7import os
Gregory P. Smithcce211f2010-03-01 00:05:08 +00008import errno
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00009import tempfile
10import time
Tim Peters3761e8d2004-10-13 04:07:12 +000011import re
Ezio Melotti8f6a2872010-02-10 21:40:33 +000012import sysconfig
Victor Stinner2097b9e2017-06-27 00:00:51 +020013import textwrap
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000014
Benjamin Peterson8b59c232011-12-10 12:31:42 -050015try:
Gregory P. Smithf0739cb2017-01-22 22:38:28 -080016 import ctypes
17except ImportError:
18 ctypes = None
Victor Stinner2097b9e2017-06-27 00:00:51 +020019else:
20 import ctypes.util
Gregory P. Smithf0739cb2017-01-22 22:38:28 -080021
22try:
Benjamin Peterson8b59c232011-12-10 12:31:42 -050023 import resource
24except ImportError:
25 resource = None
Antoine Pitrou33fc7442013-08-30 23:38:13 +020026try:
27 import threading
28except ImportError:
29 threading = None
Benjamin Peterson8b59c232011-12-10 12:31:42 -050030
Victor Stinner4dea0652017-08-11 14:38:37 +020031try:
32 import _testcapi
33except ImportError:
34 _testcapi = None
35
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000036mswindows = (sys.platform == "win32")
37
38#
39# Depends on the following external programs: Python
40#
41
42if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000043 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
44 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000045else:
46 SETBINARY = ''
47
Florent Xicluna98e3fc32010-02-27 19:20:50 +000048
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000049class BaseTestCase(unittest.TestCase):
Neal Norwitzb15ac312006-06-29 04:10:08 +000050 def setUp(self):
Tim Peters38ff36c2006-06-30 06:18:39 +000051 # Try to minimize the number of children we have so this test
52 # doesn't crash on some buildbots (Alphas in particular).
Florent Xicluna98e3fc32010-02-27 19:20:50 +000053 test_support.reap_children()
Neal Norwitzb15ac312006-06-29 04:10:08 +000054
Florent Xiclunaab5e17f2010-03-04 21:31:58 +000055 def tearDown(self):
56 for inst in subprocess._active:
57 inst.wait()
58 subprocess._cleanup()
59 self.assertFalse(subprocess._active, "subprocess._active not empty")
Victor Stinner504939f2017-08-11 17:14:31 +020060 self.doCleanups()
61 test_support.reap_children()
Florent Xiclunaab5e17f2010-03-04 21:31:58 +000062
Florent Xicluna98e3fc32010-02-27 19:20:50 +000063 def assertStderrEqual(self, stderr, expected, msg=None):
64 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
65 # shutdown time. That frustrates tests trying to check stderr produced
66 # from a spawned Python process.
67 actual = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
68 self.assertEqual(actual, expected, msg)
Neal Norwitzb15ac312006-06-29 04:10:08 +000069
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000070
Gregory P. Smith9d3b6e92012-11-10 22:49:03 -080071class PopenTestException(Exception):
72 pass
73
74
75class PopenExecuteChildRaises(subprocess.Popen):
76 """Popen subclass for testing cleanup of subprocess.PIPE filehandles when
77 _execute_child fails.
78 """
79 def _execute_child(self, *args, **kwargs):
80 raise PopenTestException("Forced Exception for Test")
81
82
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000083class ProcessTestCase(BaseTestCase):
84
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000085 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000086 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000087 rc = subprocess.call([sys.executable, "-c",
88 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000089 self.assertEqual(rc, 47)
90
Peter Astrand454f7672005-01-01 09:36:35 +000091 def test_check_call_zero(self):
92 # check_call() function with zero return code
93 rc = subprocess.check_call([sys.executable, "-c",
94 "import sys; sys.exit(0)"])
95 self.assertEqual(rc, 0)
96
97 def test_check_call_nonzero(self):
98 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +000099 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +0000100 subprocess.check_call([sys.executable, "-c",
101 "import sys; sys.exit(47)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000102 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +0000103
Gregory P. Smith26576802008-12-05 02:27:01 +0000104 def test_check_output(self):
105 # check_output() function with zero return code
106 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000107 [sys.executable, "-c", "print 'BDFL'"])
Ezio Melottiaa980582010-01-23 23:04:36 +0000108 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000109
Gregory P. Smith26576802008-12-05 02:27:01 +0000110 def test_check_output_nonzero(self):
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000111 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000112 with self.assertRaises(subprocess.CalledProcessError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +0000113 subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000114 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000115 self.assertEqual(c.exception.returncode, 5)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000116
Gregory P. Smith26576802008-12-05 02:27:01 +0000117 def test_check_output_stderr(self):
118 # check_output() function stderr redirected to stdout
119 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000120 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
121 stderr=subprocess.STDOUT)
Ezio Melottiaa980582010-01-23 23:04:36 +0000122 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000123
Gregory P. Smith26576802008-12-05 02:27:01 +0000124 def test_check_output_stdout_arg(self):
125 # check_output() function stderr redirected to stdout
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000126 with self.assertRaises(ValueError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +0000127 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000128 [sys.executable, "-c", "print 'will not be run'"],
129 stdout=sys.stdout)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000130 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000131 self.assertIn('stdout', c.exception.args[0])
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000132
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000133 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000134 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000135 newenv = os.environ.copy()
136 newenv["FRUIT"] = "banana"
137 rc = subprocess.call([sys.executable, "-c",
Florent Xiclunabab22a72010-03-04 19:40:48 +0000138 'import sys, os;'
139 'sys.exit(os.getenv("FRUIT")=="banana")'],
140 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000141 self.assertEqual(rc, 1)
142
Victor Stinner776e69b2011-06-01 01:03:00 +0200143 def test_invalid_args(self):
144 # Popen() called with invalid arguments should raise TypeError
145 # but Popen.__del__ should not complain (issue #12085)
Victor Stinnere9b185f2011-06-01 01:57:48 +0200146 with test_support.captured_stderr() as s:
Victor Stinner776e69b2011-06-01 01:03:00 +0200147 self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
148 argcount = subprocess.Popen.__init__.__code__.co_argcount
149 too_many_args = [0] * (argcount + 1)
150 self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
151 self.assertEqual(s.getvalue(), '')
152
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000153 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000154 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000155 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
156 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000157 self.addCleanup(p.stdout.close)
158 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000159 p.wait()
160 self.assertEqual(p.stdin, None)
161
162 def test_stdout_none(self):
Ezio Melottiefaad092013-03-11 00:34:33 +0200163 # .stdout is None when not redirected, and the child's stdout will
164 # be inherited from the parent. In order to test this we run a
165 # subprocess in a subprocess:
166 # this_test
167 # \-- subprocess created by this test (parent)
168 # \-- subprocess created by the parent subprocess (child)
169 # The parent doesn't specify stdout, so the child will use the
170 # parent's stdout. This test checks that the message printed by the
171 # child goes to the parent stdout. The parent also checks that the
172 # child's stdout is None. See #11963.
173 code = ('import sys; from subprocess import Popen, PIPE;'
174 'p = Popen([sys.executable, "-c", "print \'test_stdout_none\'"],'
175 ' stdin=PIPE, stderr=PIPE);'
176 'p.wait(); assert p.stdout is None;')
177 p = subprocess.Popen([sys.executable, "-c", code],
178 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
179 self.addCleanup(p.stdout.close)
Brian Curtind117b562010-11-05 04:09:09 +0000180 self.addCleanup(p.stderr.close)
Ezio Melottiefaad092013-03-11 00:34:33 +0200181 out, err = p.communicate()
182 self.assertEqual(p.returncode, 0, err)
183 self.assertEqual(out.rstrip(), 'test_stdout_none')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000184
185 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000186 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000187 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
188 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000189 self.addCleanup(p.stdout.close)
190 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000191 p.wait()
192 self.assertEqual(p.stderr, None)
193
Ezio Melotti8f6a2872010-02-10 21:40:33 +0000194 def test_executable_with_cwd(self):
Florent Xicluna63763702010-03-11 01:50:48 +0000195 python_dir = os.path.dirname(os.path.realpath(sys.executable))
Ezio Melotti8f6a2872010-02-10 21:40:33 +0000196 p = subprocess.Popen(["somethingyoudonthave", "-c",
197 "import sys; sys.exit(47)"],
198 executable=sys.executable, cwd=python_dir)
199 p.wait()
200 self.assertEqual(p.returncode, 47)
201
202 @unittest.skipIf(sysconfig.is_python_build(),
203 "need an installed Python. See #7774")
204 def test_executable_without_cwd(self):
205 # For a normal installation, it should work without 'cwd'
206 # argument. For test runs in the build directory, see #7774.
207 p = subprocess.Popen(["somethingyoudonthave", "-c",
208 "import sys; sys.exit(47)"],
Tim Peters3b01a702004-10-12 22:19:32 +0000209 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000210 p.wait()
211 self.assertEqual(p.returncode, 47)
212
213 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000214 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000215 p = subprocess.Popen([sys.executable, "-c",
216 'import sys; sys.exit(sys.stdin.read() == "pear")'],
217 stdin=subprocess.PIPE)
218 p.stdin.write("pear")
219 p.stdin.close()
220 p.wait()
221 self.assertEqual(p.returncode, 1)
222
223 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000224 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000225 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000226 d = tf.fileno()
227 os.write(d, "pear")
228 os.lseek(d, 0, 0)
229 p = subprocess.Popen([sys.executable, "-c",
230 'import sys; sys.exit(sys.stdin.read() == "pear")'],
231 stdin=d)
232 p.wait()
233 self.assertEqual(p.returncode, 1)
234
235 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000236 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000237 tf = tempfile.TemporaryFile()
238 tf.write("pear")
239 tf.seek(0)
240 p = subprocess.Popen([sys.executable, "-c",
241 'import sys; sys.exit(sys.stdin.read() == "pear")'],
242 stdin=tf)
243 p.wait()
244 self.assertEqual(p.returncode, 1)
245
246 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000247 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000248 p = subprocess.Popen([sys.executable, "-c",
249 'import sys; sys.stdout.write("orange")'],
250 stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000251 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000252 self.assertEqual(p.stdout.read(), "orange")
253
254 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000255 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000256 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000257 d = tf.fileno()
258 p = subprocess.Popen([sys.executable, "-c",
259 'import sys; sys.stdout.write("orange")'],
260 stdout=d)
261 p.wait()
262 os.lseek(d, 0, 0)
263 self.assertEqual(os.read(d, 1024), "orange")
264
265 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000266 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000267 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000268 p = subprocess.Popen([sys.executable, "-c",
269 'import sys; sys.stdout.write("orange")'],
270 stdout=tf)
271 p.wait()
272 tf.seek(0)
273 self.assertEqual(tf.read(), "orange")
274
275 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000276 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000277 p = subprocess.Popen([sys.executable, "-c",
278 'import sys; sys.stderr.write("strawberry")'],
279 stderr=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000280 self.addCleanup(p.stderr.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000281 self.assertStderrEqual(p.stderr.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000282
283 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000284 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000285 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000286 d = tf.fileno()
287 p = subprocess.Popen([sys.executable, "-c",
288 'import sys; sys.stderr.write("strawberry")'],
289 stderr=d)
290 p.wait()
291 os.lseek(d, 0, 0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000292 self.assertStderrEqual(os.read(d, 1024), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000293
294 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000295 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000296 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000297 p = subprocess.Popen([sys.executable, "-c",
298 'import sys; sys.stderr.write("strawberry")'],
299 stderr=tf)
300 p.wait()
301 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000302 self.assertStderrEqual(tf.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000303
Martin Panter1edccfa2016-05-13 01:54:44 +0000304 def test_stderr_redirect_with_no_stdout_redirect(self):
305 # test stderr=STDOUT while stdout=None (not set)
306
307 # - grandchild prints to stderr
308 # - child redirects grandchild's stderr to its stdout
309 # - the parent should get grandchild's stderr in child's stdout
310 p = subprocess.Popen([sys.executable, "-c",
311 'import sys, subprocess;'
312 'rc = subprocess.call([sys.executable, "-c",'
313 ' "import sys;"'
314 ' "sys.stderr.write(\'42\')"],'
315 ' stderr=subprocess.STDOUT);'
316 'sys.exit(rc)'],
317 stdout=subprocess.PIPE,
318 stderr=subprocess.PIPE)
319 stdout, stderr = p.communicate()
320 #NOTE: stdout should get stderr from grandchild
321 self.assertStderrEqual(stdout, b'42')
322 self.assertStderrEqual(stderr, b'') # should be empty
323 self.assertEqual(p.returncode, 0)
324
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000325 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000326 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000327 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000328 'import sys;'
329 'sys.stdout.write("apple");'
330 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000331 'sys.stderr.write("orange")'],
332 stdout=subprocess.PIPE,
333 stderr=subprocess.STDOUT)
Brian Curtind117b562010-11-05 04:09:09 +0000334 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000335 self.assertStderrEqual(p.stdout.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000336
337 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000338 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000339 tf = tempfile.TemporaryFile()
340 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000341 'import sys;'
342 'sys.stdout.write("apple");'
343 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000344 'sys.stderr.write("orange")'],
345 stdout=tf,
346 stderr=tf)
347 p.wait()
348 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000349 self.assertStderrEqual(tf.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000350
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000351 def test_stdout_filedes_of_stdout(self):
352 # stdout is set to 1 (#1531862).
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200353 # To avoid printing the text on stdout, we do something similar to
Ezio Melottiefaad092013-03-11 00:34:33 +0200354 # test_stdout_none (see above). The parent subprocess calls the child
355 # subprocess passing stdout=1, and this test uses stdout=PIPE in
356 # order to capture and check the output of the parent. See #11963.
357 code = ('import sys, subprocess; '
358 'rc = subprocess.call([sys.executable, "-c", '
359 ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), '
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200360 '\'test with stdout=1\'))"], stdout=1); '
361 'assert rc == 18')
Ezio Melottiefaad092013-03-11 00:34:33 +0200362 p = subprocess.Popen([sys.executable, "-c", code],
363 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
364 self.addCleanup(p.stdout.close)
365 self.addCleanup(p.stderr.close)
366 out, err = p.communicate()
367 self.assertEqual(p.returncode, 0, err)
Ezio Melotti9b9cd4c2013-03-11 03:21:08 +0200368 self.assertEqual(out.rstrip(), 'test with stdout=1')
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000369
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000370 def test_cwd(self):
Guido van Rossume9a0e882007-12-20 17:28:10 +0000371 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000372 # We cannot use os.path.realpath to canonicalize the path,
373 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
374 cwd = os.getcwd()
375 os.chdir(tmpdir)
376 tmpdir = os.getcwd()
377 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000378 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000379 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000380 'sys.stdout.write(os.getcwd())'],
381 stdout=subprocess.PIPE,
382 cwd=tmpdir)
Brian Curtind117b562010-11-05 04:09:09 +0000383 self.addCleanup(p.stdout.close)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000384 normcase = os.path.normcase
385 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000386
387 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000388 newenv = os.environ.copy()
389 newenv["FRUIT"] = "orange"
390 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000391 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000392 'sys.stdout.write(os.getenv("FRUIT"))'],
393 stdout=subprocess.PIPE,
394 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000395 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000396 self.assertEqual(p.stdout.read(), "orange")
397
Serhiy Storchaka9dda2ca2017-06-24 11:49:00 +0300398 def test_invalid_cmd(self):
399 # null character in the command name
400 cmd = sys.executable + '\0'
401 with self.assertRaises(TypeError):
402 subprocess.Popen([cmd, "-c", "pass"])
403
404 # null character in the command argument
405 with self.assertRaises(TypeError):
406 subprocess.Popen([sys.executable, "-c", "pass#\0"])
407
408 def test_invalid_env(self):
409 # null character in the enviroment variable name
410 newenv = os.environ.copy()
411 newenv["FRUIT\0VEGETABLE"] = "cabbage"
412 with self.assertRaises(TypeError):
413 subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
414
415 # null character in the enviroment variable value
416 newenv = os.environ.copy()
417 newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
418 with self.assertRaises(TypeError):
419 subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
420
421 # equal character in the enviroment variable name
422 newenv = os.environ.copy()
423 newenv["FRUIT=ORANGE"] = "lemon"
424 with self.assertRaises(ValueError):
425 subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
426
427 # equal character in the enviroment variable value
428 newenv = os.environ.copy()
429 newenv["FRUIT"] = "orange=lemon"
430 p = subprocess.Popen([sys.executable, "-c",
431 'import sys, os;'
432 'sys.stdout.write(os.getenv("FRUIT"))'],
433 stdout=subprocess.PIPE,
434 env=newenv)
435 stdout, stderr = p.communicate()
436 self.assertEqual(stdout, "orange=lemon")
437
Peter Astrandcbac93c2005-03-03 20:24:28 +0000438 def test_communicate_stdin(self):
439 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000440 'import sys;'
441 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000442 stdin=subprocess.PIPE)
443 p.communicate("pear")
444 self.assertEqual(p.returncode, 1)
445
446 def test_communicate_stdout(self):
447 p = subprocess.Popen([sys.executable, "-c",
448 'import sys; sys.stdout.write("pineapple")'],
449 stdout=subprocess.PIPE)
450 (stdout, stderr) = p.communicate()
451 self.assertEqual(stdout, "pineapple")
452 self.assertEqual(stderr, None)
453
454 def test_communicate_stderr(self):
455 p = subprocess.Popen([sys.executable, "-c",
456 'import sys; sys.stderr.write("pineapple")'],
457 stderr=subprocess.PIPE)
458 (stdout, stderr) = p.communicate()
459 self.assertEqual(stdout, None)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000460 self.assertStderrEqual(stderr, "pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000461
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000462 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000463 p = subprocess.Popen([sys.executable, "-c",
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000464 'import sys,os;'
465 'sys.stderr.write("pineapple");'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000466 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000467 stdin=subprocess.PIPE,
468 stdout=subprocess.PIPE,
469 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000470 self.addCleanup(p.stdout.close)
471 self.addCleanup(p.stderr.close)
472 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000473 (stdout, stderr) = p.communicate("banana")
474 self.assertEqual(stdout, "banana")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000475 self.assertStderrEqual(stderr, "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000476
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000477 # This test is Linux specific for simplicity to at least have
478 # some coverage. It is not a platform specific bug.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000479 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
480 "Linux specific")
481 # Test for the fd leak reported in http://bugs.python.org/issue2791.
482 def test_communicate_pipe_fd_leak(self):
483 fd_directory = '/proc/%d/fd' % os.getpid()
484 num_fds_before_popen = len(os.listdir(fd_directory))
Martin Panterad6a99c2016-09-10 10:38:22 +0000485 p = subprocess.Popen([sys.executable, "-c", "print('')"],
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000486 stdout=subprocess.PIPE)
487 p.communicate()
488 num_fds_after_communicate = len(os.listdir(fd_directory))
489 del p
490 num_fds_after_destruction = len(os.listdir(fd_directory))
491 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
492 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000493
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000494 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000495 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000496 p = subprocess.Popen([sys.executable, "-c",
497 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000498 (stdout, stderr) = p.communicate()
499 self.assertEqual(stdout, None)
500 self.assertEqual(stderr, None)
501
502 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000503 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000504 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000505 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000506 x, y = os.pipe()
507 if mswindows:
508 pipe_buf = 512
509 else:
510 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
511 os.close(x)
512 os.close(y)
513 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000514 'import sys,os;'
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000515 'sys.stdout.write(sys.stdin.read(47));'
516 'sys.stderr.write("xyz"*%d);'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000517 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000518 stdin=subprocess.PIPE,
519 stdout=subprocess.PIPE,
520 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000521 self.addCleanup(p.stdout.close)
522 self.addCleanup(p.stderr.close)
523 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000524 string_to_write = "abc"*pipe_buf
525 (stdout, stderr) = p.communicate(string_to_write)
526 self.assertEqual(stdout, string_to_write)
527
528 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000529 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000530 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000531 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000532 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000533 stdin=subprocess.PIPE,
534 stdout=subprocess.PIPE,
535 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000536 self.addCleanup(p.stdout.close)
537 self.addCleanup(p.stderr.close)
538 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000539 p.stdin.write("banana")
540 (stdout, stderr) = p.communicate("split")
541 self.assertEqual(stdout, "bananasplit")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000542 self.assertStderrEqual(stderr, "")
Tim Peterse718f612004-10-12 21:51:32 +0000543
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000544 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000545 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000546 'import sys,os;' + SETBINARY +
547 'sys.stdout.write("line1\\n");'
548 'sys.stdout.flush();'
549 'sys.stdout.write("line2\\r");'
550 'sys.stdout.flush();'
551 'sys.stdout.write("line3\\r\\n");'
552 'sys.stdout.flush();'
553 'sys.stdout.write("line4\\r");'
554 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000555 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000556 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000557 'sys.stdout.write("\\nline6");'],
558 stdout=subprocess.PIPE,
559 universal_newlines=1)
Brian Curtind117b562010-11-05 04:09:09 +0000560 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000561 stdout = p.stdout.read()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000562 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000563 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000564 self.assertEqual(stdout,
565 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000566 else:
567 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000568 self.assertEqual(stdout,
569 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000570
571 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000572 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000573 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000574 'import sys,os;' + SETBINARY +
575 'sys.stdout.write("line1\\n");'
576 'sys.stdout.flush();'
577 'sys.stdout.write("line2\\r");'
578 'sys.stdout.flush();'
579 'sys.stdout.write("line3\\r\\n");'
580 'sys.stdout.flush();'
581 'sys.stdout.write("line4\\r");'
582 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000583 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000584 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000585 'sys.stdout.write("\\nline6");'],
586 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
587 universal_newlines=1)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000588 self.addCleanup(p.stdout.close)
589 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000590 (stdout, stderr) = p.communicate()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000591 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000592 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000593 self.assertEqual(stdout,
594 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000595 else:
596 # Interpreter without universal newline support
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000597 self.assertEqual(stdout,
598 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000599
600 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000601 # Make sure we leak no resources
Antoine Pitroub0b3bff2010-09-18 22:42:30 +0000602 if not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000603 max_handles = 1026 # too much for most UNIX systems
604 else:
Antoine Pitroub0b3bff2010-09-18 22:42:30 +0000605 max_handles = 2050 # too much for (at least some) Windows setups
606 handles = []
607 try:
608 for i in range(max_handles):
609 try:
610 handles.append(os.open(test_support.TESTFN,
611 os.O_WRONLY | os.O_CREAT))
612 except OSError as e:
613 if e.errno != errno.EMFILE:
614 raise
615 break
616 else:
617 self.skipTest("failed to reach the file descriptor limit "
618 "(tried %d)" % max_handles)
619 # Close a couple of them (should be enough for a subprocess)
620 for i in range(10):
621 os.close(handles.pop())
622 # Loop creating some subprocesses. If one of them leaks some fds,
623 # the next loop iteration will fail by reaching the max fd limit.
624 for i in range(15):
625 p = subprocess.Popen([sys.executable, "-c",
626 "import sys;"
627 "sys.stdout.write(sys.stdin.read())"],
628 stdin=subprocess.PIPE,
629 stdout=subprocess.PIPE,
630 stderr=subprocess.PIPE)
631 data = p.communicate(b"lime")[0]
632 self.assertEqual(data, b"lime")
633 finally:
634 for h in handles:
635 os.close(h)
Mark Dickinson313dc9b2012-10-07 15:41:38 +0100636 test_support.unlink(test_support.TESTFN)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000637
638 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000639 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
640 '"a b c" d e')
641 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
642 'ab\\"c \\ d')
Gregory P. Smithe047e6d2008-01-19 20:49:02 +0000643 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
644 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000645 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
646 'a\\\\\\b "de fg" h')
647 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
648 'a\\\\\\"b c d')
649 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
650 '"a\\\\b c" d e')
651 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
652 '"a\\\\b\\ c" d e')
Peter Astrand10514a72007-01-13 22:35:35 +0000653 self.assertEqual(subprocess.list2cmdline(['ab', '']),
654 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000655
656
657 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000658 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000659 "-c", "import time; time.sleep(1)"])
660 count = 0
661 while p.poll() is None:
662 time.sleep(0.1)
663 count += 1
664 # We expect that the poll loop probably went around about 10 times,
665 # but, based on system scheduling we can't control, it's possible
666 # poll() never returned None. It "should be" very rare that it
667 # didn't go around at least twice.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000668 self.assertGreaterEqual(count, 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000669 # Subsequent invocations should just return the returncode
670 self.assertEqual(p.poll(), 0)
671
672
673 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000674 p = subprocess.Popen([sys.executable,
675 "-c", "import time; time.sleep(2)"])
676 self.assertEqual(p.wait(), 0)
677 # Subsequent invocations should just return the returncode
678 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000679
Peter Astrand738131d2004-11-30 21:04:45 +0000680
681 def test_invalid_bufsize(self):
682 # an invalid type of the bufsize argument should raise
683 # TypeError.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000684 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +0000685 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +0000686
Georg Brandlf3715d22009-02-14 17:01:36 +0000687 def test_leaking_fds_on_error(self):
688 # see bug #5179: Popen leaks file descriptors to PIPEs if
689 # the child fails to execute; this will eventually exhaust
690 # the maximum number of open fds. 1024 seems a very common
691 # value for that limit, but Windows has 2048, so we loop
692 # 1024 times (each call leaked two fds).
693 for i in range(1024):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000694 # Windows raises IOError. Others raise OSError.
695 with self.assertRaises(EnvironmentError) as c:
Georg Brandlf3715d22009-02-14 17:01:36 +0000696 subprocess.Popen(['nonexisting_i_hope'],
697 stdout=subprocess.PIPE,
698 stderr=subprocess.PIPE)
R David Murraycdd5fc92011-03-13 22:37:18 -0400699 # ignore errors that indicate the command was not found
700 if c.exception.errno not in (errno.ENOENT, errno.EACCES):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000701 raise c.exception
Georg Brandlf3715d22009-02-14 17:01:36 +0000702
Antoine Pitrou33fc7442013-08-30 23:38:13 +0200703 @unittest.skipIf(threading is None, "threading required")
704 def test_double_close_on_error(self):
705 # Issue #18851
706 fds = []
707 def open_fds():
708 for i in range(20):
709 fds.extend(os.pipe())
710 time.sleep(0.001)
711 t = threading.Thread(target=open_fds)
712 t.start()
713 try:
714 with self.assertRaises(EnvironmentError):
715 subprocess.Popen(['nonexisting_i_hope'],
716 stdin=subprocess.PIPE,
717 stdout=subprocess.PIPE,
718 stderr=subprocess.PIPE)
719 finally:
720 t.join()
721 exc = None
722 for fd in fds:
723 # If a double close occurred, some of those fds will
724 # already have been closed by mistake, and os.close()
725 # here will raise.
726 try:
727 os.close(fd)
728 except OSError as e:
729 exc = e
730 if exc is not None:
731 raise exc
732
Tim Golden90374f52010-08-06 13:14:33 +0000733 def test_handles_closed_on_exception(self):
734 # If CreateProcess exits with an error, ensure the
735 # duplicate output handles are released
Berker Peksagb7c35152015-09-28 15:37:57 +0300736 ifhandle, ifname = tempfile.mkstemp()
737 ofhandle, ofname = tempfile.mkstemp()
738 efhandle, efname = tempfile.mkstemp()
Tim Golden90374f52010-08-06 13:14:33 +0000739 try:
740 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
741 stderr=efhandle)
742 except OSError:
743 os.close(ifhandle)
744 os.remove(ifname)
745 os.close(ofhandle)
746 os.remove(ofname)
747 os.close(efhandle)
748 os.remove(efname)
749 self.assertFalse(os.path.exists(ifname))
750 self.assertFalse(os.path.exists(ofname))
751 self.assertFalse(os.path.exists(efname))
752
Ross Lagerwall104c3f12011-04-05 15:24:34 +0200753 def test_communicate_epipe(self):
754 # Issue 10963: communicate() should hide EPIPE
755 p = subprocess.Popen([sys.executable, "-c", 'pass'],
756 stdin=subprocess.PIPE,
757 stdout=subprocess.PIPE,
758 stderr=subprocess.PIPE)
759 self.addCleanup(p.stdout.close)
760 self.addCleanup(p.stderr.close)
761 self.addCleanup(p.stdin.close)
762 p.communicate("x" * 2**20)
763
764 def test_communicate_epipe_only_stdin(self):
765 # Issue 10963: communicate() should hide EPIPE
766 p = subprocess.Popen([sys.executable, "-c", 'pass'],
767 stdin=subprocess.PIPE)
768 self.addCleanup(p.stdin.close)
769 time.sleep(2)
770 p.communicate("x" * 2**20)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000771
Gregory P. Smith9d3b6e92012-11-10 22:49:03 -0800772 # This test is Linux-ish specific for simplicity to at least have
773 # some coverage. It is not a platform specific bug.
774 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
775 "Linux specific")
776 def test_failed_child_execute_fd_leak(self):
777 """Test for the fork() failure fd leak reported in issue16327."""
778 fd_directory = '/proc/%d/fd' % os.getpid()
779 fds_before_popen = os.listdir(fd_directory)
780 with self.assertRaises(PopenTestException):
781 PopenExecuteChildRaises(
782 [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE,
783 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
784
785 # NOTE: This test doesn't verify that the real _execute_child
786 # does not close the file descriptors itself on the way out
787 # during an exception. Code inspection has confirmed that.
788
789 fds_after_exception = os.listdir(fd_directory)
790 self.assertEqual(fds_before_popen, fds_after_exception)
791
792
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000793# context manager
794class _SuppressCoreFiles(object):
795 """Try to prevent core files from being created."""
796 old_limit = None
797
798 def __enter__(self):
799 """Try to save previous ulimit, then set it to (0, 0)."""
Benjamin Peterson8b59c232011-12-10 12:31:42 -0500800 if resource is not None:
801 try:
802 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
803 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
804 except (ValueError, resource.error):
805 pass
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000806
Ronald Oussoren21b44e02010-07-23 12:26:30 +0000807 if sys.platform == 'darwin':
808 # Check if the 'Crash Reporter' on OSX was configured
809 # in 'Developer' mode and warn that it will get triggered
810 # when it is.
811 #
812 # This assumes that this context manager is used in tests
813 # that might trigger the next manager.
814 value = subprocess.Popen(['/usr/bin/defaults', 'read',
815 'com.apple.CrashReporter', 'DialogType'],
816 stdout=subprocess.PIPE).communicate()[0]
817 if value.strip() == b'developer':
818 print "this tests triggers the Crash Reporter, that is intentional"
819 sys.stdout.flush()
820
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000821 def __exit__(self, *args):
822 """Return core file behavior to default."""
823 if self.old_limit is None:
824 return
Benjamin Peterson8b59c232011-12-10 12:31:42 -0500825 if resource is not None:
826 try:
827 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
828 except (ValueError, resource.error):
829 pass
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000830
Victor Stinnerb78fed92011-07-05 14:50:35 +0200831 @unittest.skipUnless(hasattr(signal, 'SIGALRM'),
832 "Requires signal.SIGALRM")
Victor Stinnere7901312011-07-05 14:08:01 +0200833 def test_communicate_eintr(self):
834 # Issue #12493: communicate() should handle EINTR
835 def handler(signum, frame):
836 pass
837 old_handler = signal.signal(signal.SIGALRM, handler)
838 self.addCleanup(signal.signal, signal.SIGALRM, old_handler)
839
840 # the process is running for 2 seconds
841 args = [sys.executable, "-c", 'import time; time.sleep(2)']
842 for stream in ('stdout', 'stderr'):
843 kw = {stream: subprocess.PIPE}
844 with subprocess.Popen(args, **kw) as process:
845 signal.alarm(1)
846 # communicate() will be interrupted by SIGALRM
847 process.communicate()
848
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000849
Florent Xiclunabab22a72010-03-04 19:40:48 +0000850@unittest.skipIf(mswindows, "POSIX specific tests")
Florent Xiclunafc4d6d72010-03-23 14:36:45 +0000851class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaab5e17f2010-03-04 21:31:58 +0000852
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000853 def test_exceptions(self):
854 # caught & re-raised exceptions
855 with self.assertRaises(OSError) as c:
856 p = subprocess.Popen([sys.executable, "-c", ""],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000857 cwd="/this/path/does/not/exist")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000858 # The attribute child_traceback should contain "os.chdir" somewhere.
859 self.assertIn("os.chdir", c.exception.child_traceback)
Tim Peterse718f612004-10-12 21:51:32 +0000860
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000861 def test_run_abort(self):
862 # returncode handles signal termination
863 with _SuppressCoreFiles():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000864 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000865 "import os; os.abort()"])
866 p.wait()
867 self.assertEqual(-p.returncode, signal.SIGABRT)
868
869 def test_preexec(self):
870 # preexec function
871 p = subprocess.Popen([sys.executable, "-c",
872 "import sys, os;"
873 "sys.stdout.write(os.getenv('FRUIT'))"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000874 stdout=subprocess.PIPE,
875 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Brian Curtind117b562010-11-05 04:09:09 +0000876 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000877 self.assertEqual(p.stdout.read(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000878
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800879 class _TestExecuteChildPopen(subprocess.Popen):
880 """Used to test behavior at the end of _execute_child."""
881 def __init__(self, testcase, *args, **kwargs):
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800882 self._testcase = testcase
883 subprocess.Popen.__init__(self, *args, **kwargs)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800884
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800885 def _execute_child(
886 self, args, executable, preexec_fn, close_fds, cwd, env,
Antoine Pitrou33fc7442013-08-30 23:38:13 +0200887 universal_newlines, startupinfo, creationflags, shell, to_close,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800888 p2cread, p2cwrite,
889 c2pread, c2pwrite,
890 errread, errwrite):
891 try:
892 subprocess.Popen._execute_child(
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800893 self, args, executable, preexec_fn, close_fds,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800894 cwd, env, universal_newlines,
Antoine Pitrou33fc7442013-08-30 23:38:13 +0200895 startupinfo, creationflags, shell, to_close,
Gregory P. Smith211248b2012-11-11 02:00:49 -0800896 p2cread, p2cwrite,
897 c2pread, c2pwrite,
898 errread, errwrite)
899 finally:
900 # Open a bunch of file descriptors and verify that
901 # none of them are the same as the ones the Popen
902 # instance is using for stdin/stdout/stderr.
903 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
904 for _ in range(8)]
905 try:
906 for fd in devzero_fds:
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800907 self._testcase.assertNotIn(
908 fd, (p2cwrite, c2pread, errread))
Gregory P. Smith211248b2012-11-11 02:00:49 -0800909 finally:
Richard Oudkerk045e4572013-06-10 16:27:45 +0100910 for fd in devzero_fds:
911 os.close(fd)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800912
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800913 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
914 def test_preexec_errpipe_does_not_double_close_pipes(self):
915 """Issue16140: Don't double close pipes on preexec error."""
916
917 def raise_it():
918 raise RuntimeError("force the _execute_child() errpipe_data path.")
Gregory P. Smith211248b2012-11-11 02:00:49 -0800919
920 with self.assertRaises(RuntimeError):
Gregory P. Smithf047ba82012-11-11 09:49:02 -0800921 self._TestExecuteChildPopen(
922 self, [sys.executable, "-c", "pass"],
923 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
924 stderr=subprocess.PIPE, preexec_fn=raise_it)
Gregory P. Smith211248b2012-11-11 02:00:49 -0800925
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000926 def test_args_string(self):
927 # args is a string
Berker Peksagb7c35152015-09-28 15:37:57 +0300928 f, fname = tempfile.mkstemp()
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000929 os.write(f, "#!/bin/sh\n")
930 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
931 sys.executable)
932 os.close(f)
933 os.chmod(fname, 0o700)
934 p = subprocess.Popen(fname)
935 p.wait()
936 os.remove(fname)
937 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000938
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000939 def test_invalid_args(self):
940 # invalid arguments should raise ValueError
941 self.assertRaises(ValueError, subprocess.call,
942 [sys.executable, "-c",
943 "import sys; sys.exit(47)"],
944 startupinfo=47)
945 self.assertRaises(ValueError, subprocess.call,
946 [sys.executable, "-c",
947 "import sys; sys.exit(47)"],
948 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000949
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000950 def test_shell_sequence(self):
951 # Run command through the shell (sequence)
952 newenv = os.environ.copy()
953 newenv["FRUIT"] = "apple"
954 p = subprocess.Popen(["echo $FRUIT"], shell=1,
955 stdout=subprocess.PIPE,
956 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000957 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000958 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000959
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000960 def test_shell_string(self):
961 # Run command through the shell (string)
962 newenv = os.environ.copy()
963 newenv["FRUIT"] = "apple"
964 p = subprocess.Popen("echo $FRUIT", shell=1,
965 stdout=subprocess.PIPE,
966 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000967 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000968 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000969
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000970 def test_call_string(self):
971 # call() function with string argument on UNIX
Berker Peksagb7c35152015-09-28 15:37:57 +0300972 f, fname = tempfile.mkstemp()
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000973 os.write(f, "#!/bin/sh\n")
974 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
975 sys.executable)
976 os.close(f)
977 os.chmod(fname, 0700)
978 rc = subprocess.call(fname)
979 os.remove(fname)
980 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000981
Stefan Krahe9a6a7d2010-07-19 14:41:08 +0000982 def test_specific_shell(self):
983 # Issue #9265: Incorrect name passed as arg[0].
984 shells = []
985 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
986 for name in ['bash', 'ksh']:
987 sh = os.path.join(prefix, name)
988 if os.path.isfile(sh):
989 shells.append(sh)
990 if not shells: # Will probably work for any shell but csh.
991 self.skipTest("bash or ksh required for this test")
992 sh = '/bin/sh'
993 if os.path.isfile(sh) and not os.path.islink(sh):
994 # Test will fail if /bin/sh is a symlink to csh.
995 shells.append(sh)
996 for sh in shells:
997 p = subprocess.Popen("echo $0", executable=sh, shell=True,
998 stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000999 self.addCleanup(p.stdout.close)
Stefan Krahe9a6a7d2010-07-19 14:41:08 +00001000 self.assertEqual(p.stdout.read().strip(), sh)
1001
Florent Xiclunac0838642010-03-07 15:27:39 +00001002 def _kill_process(self, method, *args):
Florent Xiclunacecef392010-03-05 19:31:21 +00001003 # Do not inherit file handles from the parent.
1004 # It should fix failures on some platforms.
Antoine Pitroua6166da2010-09-20 11:20:44 +00001005 p = subprocess.Popen([sys.executable, "-c", """if 1:
1006 import sys, time
1007 sys.stdout.write('x\\n')
1008 sys.stdout.flush()
1009 time.sleep(30)
1010 """],
1011 close_fds=True,
1012 stdin=subprocess.PIPE,
1013 stdout=subprocess.PIPE,
1014 stderr=subprocess.PIPE)
1015 # Wait for the interpreter to be completely initialized before
1016 # sending any signal.
1017 p.stdout.read(1)
1018 getattr(p, method)(*args)
Florent Xiclunac0838642010-03-07 15:27:39 +00001019 return p
1020
Charles-François Natalief2bd672013-01-12 16:52:20 +01001021 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
1022 "Due to known OS bug (issue #16762)")
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001023 def _kill_dead_process(self, method, *args):
1024 # Do not inherit file handles from the parent.
1025 # It should fix failures on some platforms.
1026 p = subprocess.Popen([sys.executable, "-c", """if 1:
1027 import sys, time
1028 sys.stdout.write('x\\n')
1029 sys.stdout.flush()
1030 """],
1031 close_fds=True,
1032 stdin=subprocess.PIPE,
1033 stdout=subprocess.PIPE,
1034 stderr=subprocess.PIPE)
1035 # Wait for the interpreter to be completely initialized before
1036 # sending any signal.
1037 p.stdout.read(1)
1038 # The process should end after this
1039 time.sleep(1)
1040 # This shouldn't raise even though the child is now dead
1041 getattr(p, method)(*args)
1042 p.communicate()
1043
Florent Xiclunac0838642010-03-07 15:27:39 +00001044 def test_send_signal(self):
1045 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunafc4d6d72010-03-23 14:36:45 +00001046 _, stderr = p.communicate()
Florent Xicluna3c919cf2010-03-23 19:19:16 +00001047 self.assertIn('KeyboardInterrupt', stderr)
Florent Xicluna446ff142010-03-23 15:05:30 +00001048 self.assertNotEqual(p.wait(), 0)
Christian Heimese74c8f22008-04-19 02:23:57 +00001049
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001050 def test_kill(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001051 p = self._kill_process('kill')
Florent Xicluna446ff142010-03-23 15:05:30 +00001052 _, stderr = p.communicate()
1053 self.assertStderrEqual(stderr, '')
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001054 self.assertEqual(p.wait(), -signal.SIGKILL)
Christian Heimese74c8f22008-04-19 02:23:57 +00001055
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001056 def test_terminate(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001057 p = self._kill_process('terminate')
Florent Xicluna446ff142010-03-23 15:05:30 +00001058 _, stderr = p.communicate()
1059 self.assertStderrEqual(stderr, '')
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001060 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +00001061
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001062 def test_send_signal_dead(self):
1063 # Sending a signal to a dead process
1064 self._kill_dead_process('send_signal', signal.SIGINT)
1065
1066 def test_kill_dead(self):
1067 # Killing a dead process
1068 self._kill_dead_process('kill')
1069
1070 def test_terminate_dead(self):
1071 # Terminating a dead process
1072 self._kill_dead_process('terminate')
1073
Antoine Pitrou91ce0d92011-01-03 18:45:09 +00001074 def check_close_std_fds(self, fds):
1075 # Issue #9905: test that subprocess pipes still work properly with
1076 # some standard fds closed
1077 stdin = 0
1078 newfds = []
1079 for a in fds:
1080 b = os.dup(a)
1081 newfds.append(b)
1082 if a == 0:
1083 stdin = b
1084 try:
1085 for fd in fds:
1086 os.close(fd)
1087 out, err = subprocess.Popen([sys.executable, "-c",
1088 'import sys;'
1089 'sys.stdout.write("apple");'
1090 'sys.stdout.flush();'
1091 'sys.stderr.write("orange")'],
1092 stdin=stdin,
1093 stdout=subprocess.PIPE,
1094 stderr=subprocess.PIPE).communicate()
1095 err = test_support.strip_python_stderr(err)
1096 self.assertEqual((out, err), (b'apple', b'orange'))
1097 finally:
1098 for b, a in zip(newfds, fds):
1099 os.dup2(b, a)
1100 for b in newfds:
1101 os.close(b)
1102
1103 def test_close_fd_0(self):
1104 self.check_close_std_fds([0])
1105
1106 def test_close_fd_1(self):
1107 self.check_close_std_fds([1])
1108
1109 def test_close_fd_2(self):
1110 self.check_close_std_fds([2])
1111
1112 def test_close_fds_0_1(self):
1113 self.check_close_std_fds([0, 1])
1114
1115 def test_close_fds_0_2(self):
1116 self.check_close_std_fds([0, 2])
1117
1118 def test_close_fds_1_2(self):
1119 self.check_close_std_fds([1, 2])
1120
1121 def test_close_fds_0_1_2(self):
1122 # Issue #10806: test that subprocess pipes still work properly with
1123 # all standard fds closed.
1124 self.check_close_std_fds([0, 1, 2])
1125
Ross Lagerwalld8e39012011-07-27 18:54:53 +02001126 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
1127 # open up some temporary files
Berker Peksagb7c35152015-09-28 15:37:57 +03001128 temps = [tempfile.mkstemp() for i in range(3)]
Ross Lagerwalld8e39012011-07-27 18:54:53 +02001129 temp_fds = [fd for fd, fname in temps]
1130 try:
1131 # unlink the files -- we won't need to reopen them
1132 for fd, fname in temps:
1133 os.unlink(fname)
1134
1135 # save a copy of the standard file descriptors
1136 saved_fds = [os.dup(fd) for fd in range(3)]
1137 try:
1138 # duplicate the temp files over the standard fd's 0, 1, 2
1139 for fd, temp_fd in enumerate(temp_fds):
1140 os.dup2(temp_fd, fd)
1141
1142 # write some data to what will become stdin, and rewind
1143 os.write(stdin_no, b"STDIN")
1144 os.lseek(stdin_no, 0, 0)
1145
1146 # now use those files in the given order, so that subprocess
1147 # has to rearrange them in the child
1148 p = subprocess.Popen([sys.executable, "-c",
1149 'import sys; got = sys.stdin.read();'
1150 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
1151 stdin=stdin_no,
1152 stdout=stdout_no,
1153 stderr=stderr_no)
1154 p.wait()
1155
1156 for fd in temp_fds:
1157 os.lseek(fd, 0, 0)
1158
1159 out = os.read(stdout_no, 1024)
1160 err = test_support.strip_python_stderr(os.read(stderr_no, 1024))
1161 finally:
1162 for std, saved in enumerate(saved_fds):
1163 os.dup2(saved, std)
1164 os.close(saved)
1165
1166 self.assertEqual(out, b"got STDIN")
1167 self.assertEqual(err, b"err")
1168
1169 finally:
1170 for fd in temp_fds:
1171 os.close(fd)
1172
1173 # When duping fds, if there arises a situation where one of the fds is
1174 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
1175 # This tests all combinations of this.
1176 def test_swap_fds(self):
1177 self.check_swap_fds(0, 1, 2)
1178 self.check_swap_fds(0, 2, 1)
1179 self.check_swap_fds(1, 0, 2)
1180 self.check_swap_fds(1, 2, 0)
1181 self.check_swap_fds(2, 0, 1)
1182 self.check_swap_fds(2, 1, 0)
1183
Gregory P. Smith312efbc2010-12-14 15:02:53 +00001184 def test_wait_when_sigchild_ignored(self):
1185 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
1186 sigchild_ignore = test_support.findfile("sigchild_ignore.py",
1187 subdir="subprocessdata")
1188 p = subprocess.Popen([sys.executable, sigchild_ignore],
1189 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1190 stdout, stderr = p.communicate()
1191 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
1192 " non-zero with this error:\n%s" % stderr)
1193
Charles-François Natali100df0f2011-08-18 17:56:02 +02001194 def test_zombie_fast_process_del(self):
1195 # Issue #12650: on Unix, if Popen.__del__() was called before the
1196 # process exited, it wouldn't be added to subprocess._active, and would
1197 # remain a zombie.
1198 # spawn a Popen, and delete its reference before it exits
1199 p = subprocess.Popen([sys.executable, "-c",
1200 'import sys, time;'
1201 'time.sleep(0.2)'],
1202 stdout=subprocess.PIPE,
1203 stderr=subprocess.PIPE)
Nadeem Vawda86059362011-08-19 05:22:24 +02001204 self.addCleanup(p.stdout.close)
1205 self.addCleanup(p.stderr.close)
Charles-François Natali100df0f2011-08-18 17:56:02 +02001206 ident = id(p)
1207 pid = p.pid
1208 del p
1209 # check that p is in the active processes list
1210 self.assertIn(ident, [id(o) for o in subprocess._active])
1211
Charles-François Natali100df0f2011-08-18 17:56:02 +02001212 def test_leak_fast_process_del_killed(self):
1213 # Issue #12650: on Unix, if Popen.__del__() was called before the
1214 # process exited, and the process got killed by a signal, it would never
1215 # be removed from subprocess._active, which triggered a FD and memory
1216 # leak.
1217 # spawn a Popen, delete its reference and kill it
1218 p = subprocess.Popen([sys.executable, "-c",
1219 'import time;'
1220 'time.sleep(3)'],
1221 stdout=subprocess.PIPE,
1222 stderr=subprocess.PIPE)
Nadeem Vawda86059362011-08-19 05:22:24 +02001223 self.addCleanup(p.stdout.close)
1224 self.addCleanup(p.stderr.close)
Charles-François Natali100df0f2011-08-18 17:56:02 +02001225 ident = id(p)
1226 pid = p.pid
1227 del p
1228 os.kill(pid, signal.SIGKILL)
1229 # check that p is in the active processes list
1230 self.assertIn(ident, [id(o) for o in subprocess._active])
1231
1232 # let some time for the process to exit, and create a new Popen: this
1233 # should trigger the wait() of p
1234 time.sleep(0.2)
1235 with self.assertRaises(EnvironmentError) as c:
1236 with subprocess.Popen(['nonexisting_i_hope'],
1237 stdout=subprocess.PIPE,
1238 stderr=subprocess.PIPE) as proc:
1239 pass
1240 # p should have been wait()ed on, and removed from the _active list
1241 self.assertRaises(OSError, os.waitpid, pid, 0)
1242 self.assertNotIn(ident, [id(o) for o in subprocess._active])
1243
Charles-François Natali2a34eb32011-08-25 21:20:54 +02001244 def test_pipe_cloexec(self):
1245 # Issue 12786: check that the communication pipes' FDs are set CLOEXEC,
1246 # and are not inherited by another child process.
1247 p1 = subprocess.Popen([sys.executable, "-c",
1248 'import os;'
1249 'os.read(0, 1)'
1250 ],
1251 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1252 stderr=subprocess.PIPE)
1253
1254 p2 = subprocess.Popen([sys.executable, "-c", """if True:
1255 import os, errno, sys
1256 for fd in %r:
1257 try:
1258 os.close(fd)
1259 except OSError as e:
1260 if e.errno != errno.EBADF:
1261 raise
1262 else:
1263 sys.exit(1)
1264 sys.exit(0)
1265 """ % [f.fileno() for f in (p1.stdin, p1.stdout,
1266 p1.stderr)]
1267 ],
1268 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1269 stderr=subprocess.PIPE, close_fds=False)
1270 p1.communicate('foo')
1271 _, stderr = p2.communicate()
1272
1273 self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr))
1274
Victor Stinner4dea0652017-08-11 14:38:37 +02001275 @unittest.skipUnless(_testcapi is not None
1276 and hasattr(_testcapi, 'W_STOPCODE'),
1277 'need _testcapi.W_STOPCODE')
1278 def test_stopped(self):
Gregory P. Smithf0739cb2017-01-22 22:38:28 -08001279 """Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
Victor Stinner4dea0652017-08-11 14:38:37 +02001280 args = [sys.executable, '-c', 'pass']
1281 proc = subprocess.Popen(args)
Victor Stinner2097b9e2017-06-27 00:00:51 +02001282
Victor Stinner4dea0652017-08-11 14:38:37 +02001283 # Wait until the real process completes to avoid zombie process
1284 pid = proc.pid
1285 pid, status = os.waitpid(pid, 0)
1286 self.assertEqual(status, 0)
Victor Stinner2097b9e2017-06-27 00:00:51 +02001287
Victor Stinner4dea0652017-08-11 14:38:37 +02001288 status = _testcapi.W_STOPCODE(3)
Victor Stinner2097b9e2017-06-27 00:00:51 +02001289
Victor Stinner4dea0652017-08-11 14:38:37 +02001290 def mock_waitpid(pid, flags):
1291 return (pid, status)
Victor Stinner2097b9e2017-06-27 00:00:51 +02001292
Victor Stinner4dea0652017-08-11 14:38:37 +02001293 with test_support.swap_attr(os, 'waitpid', mock_waitpid):
1294 returncode = proc.wait()
1295
1296 self.assertEqual(returncode, -3)
Gregory P. Smithf0739cb2017-01-22 22:38:28 -08001297
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001298
Florent Xiclunabab22a72010-03-04 19:40:48 +00001299@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunafc4d6d72010-03-23 14:36:45 +00001300class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaab5e17f2010-03-04 21:31:58 +00001301
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001302 def test_startupinfo(self):
1303 # startupinfo argument
1304 # We uses hardcoded constants, because we do not want to
1305 # depend on win32all.
1306 STARTF_USESHOWWINDOW = 1
1307 SW_MAXIMIZE = 3
1308 startupinfo = subprocess.STARTUPINFO()
1309 startupinfo.dwFlags = STARTF_USESHOWWINDOW
1310 startupinfo.wShowWindow = SW_MAXIMIZE
1311 # Since Python is a console process, it won't be affected
1312 # by wShowWindow, but the argument should be silently
1313 # ignored
1314 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001315 startupinfo=startupinfo)
1316
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001317 def test_creationflags(self):
1318 # creationflags argument
1319 CREATE_NEW_CONSOLE = 16
1320 sys.stderr.write(" a DOS box should flash briefly ...\n")
1321 subprocess.call(sys.executable +
1322 ' -c "import time; time.sleep(0.25)"',
1323 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001324
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001325 def test_invalid_args(self):
1326 # invalid arguments should raise ValueError
1327 self.assertRaises(ValueError, subprocess.call,
1328 [sys.executable, "-c",
1329 "import sys; sys.exit(47)"],
1330 preexec_fn=lambda: 1)
1331 self.assertRaises(ValueError, subprocess.call,
1332 [sys.executable, "-c",
1333 "import sys; sys.exit(47)"],
1334 stdout=subprocess.PIPE,
1335 close_fds=True)
1336
1337 def test_close_fds(self):
1338 # close file descriptors
1339 rc = subprocess.call([sys.executable, "-c",
1340 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001341 close_fds=True)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001342 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001343
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001344 def test_shell_sequence(self):
1345 # Run command through the shell (sequence)
1346 newenv = os.environ.copy()
1347 newenv["FRUIT"] = "physalis"
1348 p = subprocess.Popen(["set"], shell=1,
1349 stdout=subprocess.PIPE,
1350 env=newenv)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001351 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001352 self.assertIn("physalis", p.stdout.read())
Peter Astrand81a191b2007-05-26 22:18:20 +00001353
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001354 def test_shell_string(self):
1355 # Run command through the shell (string)
1356 newenv = os.environ.copy()
1357 newenv["FRUIT"] = "physalis"
1358 p = subprocess.Popen("set", shell=1,
1359 stdout=subprocess.PIPE,
1360 env=newenv)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001361 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001362 self.assertIn("physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001363
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001364 def test_call_string(self):
1365 # call() function with string argument on Windows
1366 rc = subprocess.call(sys.executable +
1367 ' -c "import sys; sys.exit(47)"')
1368 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001369
Florent Xiclunac0838642010-03-07 15:27:39 +00001370 def _kill_process(self, method, *args):
Florent Xicluna400efc22010-03-07 17:12:23 +00001371 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroudee00972010-09-24 19:00:29 +00001372 p = subprocess.Popen([sys.executable, "-c", """if 1:
1373 import sys, time
1374 sys.stdout.write('x\\n')
1375 sys.stdout.flush()
1376 time.sleep(30)
1377 """],
1378 stdin=subprocess.PIPE,
1379 stdout=subprocess.PIPE,
1380 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001381 self.addCleanup(p.stdout.close)
1382 self.addCleanup(p.stderr.close)
1383 self.addCleanup(p.stdin.close)
Antoine Pitroudee00972010-09-24 19:00:29 +00001384 # Wait for the interpreter to be completely initialized before
1385 # sending any signal.
1386 p.stdout.read(1)
1387 getattr(p, method)(*args)
Florent Xicluna446ff142010-03-23 15:05:30 +00001388 _, stderr = p.communicate()
1389 self.assertStderrEqual(stderr, '')
Antoine Pitroudee00972010-09-24 19:00:29 +00001390 returncode = p.wait()
Florent Xiclunafaf17532010-03-08 10:59:33 +00001391 self.assertNotEqual(returncode, 0)
Florent Xiclunac0838642010-03-07 15:27:39 +00001392
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001393 def _kill_dead_process(self, method, *args):
1394 p = subprocess.Popen([sys.executable, "-c", """if 1:
1395 import sys, time
1396 sys.stdout.write('x\\n')
1397 sys.stdout.flush()
1398 sys.exit(42)
1399 """],
1400 stdin=subprocess.PIPE,
1401 stdout=subprocess.PIPE,
1402 stderr=subprocess.PIPE)
1403 self.addCleanup(p.stdout.close)
1404 self.addCleanup(p.stderr.close)
1405 self.addCleanup(p.stdin.close)
1406 # Wait for the interpreter to be completely initialized before
1407 # sending any signal.
1408 p.stdout.read(1)
1409 # The process should end after this
1410 time.sleep(1)
1411 # This shouldn't raise even though the child is now dead
1412 getattr(p, method)(*args)
1413 _, stderr = p.communicate()
1414 self.assertStderrEqual(stderr, b'')
1415 rc = p.wait()
1416 self.assertEqual(rc, 42)
1417
Florent Xiclunac0838642010-03-07 15:27:39 +00001418 def test_send_signal(self):
1419 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimese74c8f22008-04-19 02:23:57 +00001420
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001421 def test_kill(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001422 self._kill_process('kill')
Christian Heimese74c8f22008-04-19 02:23:57 +00001423
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001424 def test_terminate(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001425 self._kill_process('terminate')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001426
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001427 def test_send_signal_dead(self):
1428 self._kill_dead_process('send_signal', signal.SIGTERM)
1429
1430 def test_kill_dead(self):
1431 self._kill_dead_process('kill')
1432
1433 def test_terminate_dead(self):
1434 self._kill_dead_process('terminate')
1435
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001436
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001437@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
1438 "poll system call not supported")
1439class ProcessTestCaseNoPoll(ProcessTestCase):
1440 def setUp(self):
1441 subprocess._has_poll = False
1442 ProcessTestCase.setUp(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001443
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001444 def tearDown(self):
1445 subprocess._has_poll = True
1446 ProcessTestCase.tearDown(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001447
1448
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001449class HelperFunctionTests(unittest.TestCase):
Gregory P. Smithc1baf4a2010-03-01 02:53:24 +00001450 @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001451 def test_eintr_retry_call(self):
1452 record_calls = []
1453 def fake_os_func(*args):
1454 record_calls.append(args)
1455 if len(record_calls) == 2:
1456 raise OSError(errno.EINTR, "fake interrupted system call")
1457 return tuple(reversed(args))
1458
1459 self.assertEqual((999, 256),
1460 subprocess._eintr_retry_call(fake_os_func, 256, 999))
1461 self.assertEqual([(256, 999)], record_calls)
1462 # This time there will be an EINTR so it will loop once.
1463 self.assertEqual((666,),
1464 subprocess._eintr_retry_call(fake_os_func, 666))
1465 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
1466
Tim Golden8e4756c2010-08-12 11:00:35 +00001467@unittest.skipUnless(mswindows, "mswindows only")
1468class CommandsWithSpaces (BaseTestCase):
1469
1470 def setUp(self):
1471 super(CommandsWithSpaces, self).setUp()
Berker Peksagb7c35152015-09-28 15:37:57 +03001472 f, fname = tempfile.mkstemp(".py", "te st")
Tim Golden8e4756c2010-08-12 11:00:35 +00001473 self.fname = fname.lower ()
1474 os.write(f, b"import sys;"
1475 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
1476 )
1477 os.close(f)
1478
1479 def tearDown(self):
1480 os.remove(self.fname)
1481 super(CommandsWithSpaces, self).tearDown()
1482
1483 def with_spaces(self, *args, **kwargs):
1484 kwargs['stdout'] = subprocess.PIPE
1485 p = subprocess.Popen(*args, **kwargs)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001486 self.addCleanup(p.stdout.close)
Tim Golden8e4756c2010-08-12 11:00:35 +00001487 self.assertEqual(
1488 p.stdout.read ().decode("mbcs"),
1489 "2 [%r, 'ab cd']" % self.fname
1490 )
1491
1492 def test_shell_string_with_spaces(self):
1493 # call() function with string argument with spaces on Windows
Brian Curtine8c49202010-08-13 21:01:52 +00001494 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1495 "ab cd"), shell=1)
Tim Golden8e4756c2010-08-12 11:00:35 +00001496
1497 def test_shell_sequence_with_spaces(self):
1498 # call() function with sequence argument with spaces on Windows
Brian Curtine8c49202010-08-13 21:01:52 +00001499 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden8e4756c2010-08-12 11:00:35 +00001500
1501 def test_noshell_string_with_spaces(self):
1502 # call() function with string argument with spaces on Windows
1503 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1504 "ab cd"))
1505
1506 def test_noshell_sequence_with_spaces(self):
1507 # call() function with sequence argument with spaces on Windows
1508 self.with_spaces([sys.executable, self.fname, "ab cd"])
1509
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001510def test_main():
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001511 unit_tests = (ProcessTestCase,
1512 POSIXProcessTestCase,
1513 Win32ProcessTestCase,
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001514 ProcessTestCaseNoPoll,
Tim Golden8e4756c2010-08-12 11:00:35 +00001515 HelperFunctionTests,
1516 CommandsWithSpaces)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001517
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001518 test_support.run_unittest(*unit_tests)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001519 test_support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001520
1521if __name__ == "__main__":
1522 test_main()