blob: 4719773b67b7d91aa107ed06ed027d128773b7fd [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001import unittest
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)1ef8c7e2016-06-04 00:22:17 +00002from unittest import mock
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00004import subprocess
5import sys
Gregory P. Smith50e16e32017-01-22 17:28:38 -08006import platform
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00007import signal
Gregory P. Smith112bb3a2011-03-15 14:55:17 -04008import io
Alexey Izbyshev0e7144b2018-03-26 22:49:35 +03009import itertools
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000010import os
Gregory P. Smitha59c59f2010-03-01 00:17:40 +000011import errno
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000012import tempfile
13import time
Charles-François Natali3a4586a2013-11-08 19:56:59 +010014import selectors
Ezio Melotti184bdfb2010-02-18 09:37:05 +000015import sysconfig
Gregory P. Smith51ee2702010-12-13 07:59:39 +000016import select
Gregory P. Smith81ce6852011-03-15 02:04:11 -040017import shutil
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020018import threading
Benjamin Petersonb870aa12011-12-10 12:44:25 -050019import gc
Andrew Svetlov47ec25d2012-08-19 16:25:37 +030020import textwrap
Serhiy Storchakab21d1552018-03-02 11:53:51 +020021from test.support import FakePath
Benjamin Peterson964561b2011-12-10 12:31:42 -050022
23try:
Gregory P. Smith1fa08bc2017-01-22 22:19:38 -080024 import ctypes
25except ImportError:
26 ctypes = None
Gregory P. Smith56bc3b72017-05-23 07:49:13 -070027else:
28 import ctypes.util
Gregory P. Smith1fa08bc2017-01-22 22:19:38 -080029
30try:
Victor Stinner7b7c6dc2017-08-10 12:37:39 +020031 import _testcapi
32except ImportError:
33 _testcapi = None
34
Steve Dower22d06982016-09-06 19:38:15 -070035if support.PGO:
36 raise unittest.SkipTest("test is not helpful for PGO")
37
Victor Stinner937ee9e2018-06-26 02:11:06 +020038mswindows = (sys.platform == "win32")
39
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000040#
41# Depends on the following external programs: Python
42#
43
Victor Stinner937ee9e2018-06-26 02:11:06 +020044if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000045 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
46 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000047else:
48 SETBINARY = ''
49
Victor Stinner9a83f652017-08-21 23:51:31 +020050NONEXISTING_CMD = ('nonexisting_i_hope',)
Victor Stinnerb31206a2018-01-25 19:06:05 +010051# Ignore errors that indicate the command was not found
52NONEXISTING_ERRORS = (FileNotFoundError, NotADirectoryError, PermissionError)
Victor Stinner9a83f652017-08-21 23:51:31 +020053
Florent Xiclunab1e94e82010-02-27 22:12:37 +000054
Florent Xiclunac049d872010-03-27 22:47:23 +000055class BaseTestCase(unittest.TestCase):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056 def setUp(self):
57 # Try to minimize the number of children we have so this test
58 # doesn't crash on some buildbots (Alphas in particular).
Florent Xiclunab1e94e82010-02-27 22:12:37 +000059 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000060
Florent Xiclunaf0cbd822010-03-04 21:50:56 +000061 def tearDown(self):
62 for inst in subprocess._active:
63 inst.wait()
64 subprocess._cleanup()
65 self.assertFalse(subprocess._active, "subprocess._active not empty")
Victor Stinnercc42c122017-07-28 18:00:22 +020066 self.doCleanups()
67 support.reap_children()
Florent Xiclunaf0cbd822010-03-04 21:50:56 +000068
Florent Xiclunab1e94e82010-02-27 22:12:37 +000069 def assertStderrEqual(self, stderr, expected, msg=None):
70 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
71 # shutdown time. That frustrates tests trying to check stderr produced
72 # from a spawned Python process.
Antoine Pitrou62f68ed2010-08-04 11:48:56 +000073 actual = support.strip_python_stderr(stderr)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -040074 # strip_python_stderr also strips whitespace, so we do too.
75 expected = expected.strip()
Florent Xiclunab1e94e82010-02-27 22:12:37 +000076 self.assertEqual(actual, expected, msg)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077
Florent Xiclunac049d872010-03-27 22:47:23 +000078
Gregory P. Smith3d8e7762012-11-10 22:32:22 -080079class PopenTestException(Exception):
80 pass
81
82
83class PopenExecuteChildRaises(subprocess.Popen):
84 """Popen subclass for testing cleanup of subprocess.PIPE filehandles when
85 _execute_child fails.
86 """
87 def _execute_child(self, *args, **kwargs):
88 raise PopenTestException("Forced Exception for Test")
89
90
Florent Xiclunac049d872010-03-27 22:47:23 +000091class ProcessTestCase(BaseTestCase):
92
Gregory P. Smitha1ed5392013-03-23 11:44:25 -070093 def test_io_buffered_by_default(self):
94 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
95 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
96 stderr=subprocess.PIPE)
97 try:
98 self.assertIsInstance(p.stdin, io.BufferedIOBase)
99 self.assertIsInstance(p.stdout, io.BufferedIOBase)
100 self.assertIsInstance(p.stderr, io.BufferedIOBase)
101 finally:
Gregory P. Smitha1b9ed32013-03-23 11:54:22 -0700102 p.stdin.close()
103 p.stdout.close()
104 p.stderr.close()
Gregory P. Smitha1ed5392013-03-23 11:44:25 -0700105 p.wait()
106
107 def test_io_unbuffered_works(self):
108 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
109 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
110 stderr=subprocess.PIPE, bufsize=0)
111 try:
112 self.assertIsInstance(p.stdin, io.RawIOBase)
113 self.assertIsInstance(p.stdout, io.RawIOBase)
114 self.assertIsInstance(p.stderr, io.RawIOBase)
115 finally:
Gregory P. Smitha1b9ed32013-03-23 11:54:22 -0700116 p.stdin.close()
117 p.stdout.close()
118 p.stderr.close()
Gregory P. Smitha1ed5392013-03-23 11:44:25 -0700119 p.wait()
120
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000121 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000122 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +0000123 rc = subprocess.call([sys.executable, "-c",
124 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000125 self.assertEqual(rc, 47)
126
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400127 def test_call_timeout(self):
128 # call() function with timeout argument; we want to test that the child
129 # process gets killed when the timeout expires. If the child isn't
130 # killed, this call will deadlock since subprocess.call waits for the
131 # child.
132 self.assertRaises(subprocess.TimeoutExpired, subprocess.call,
133 [sys.executable, "-c", "while True: pass"],
134 timeout=0.1)
135
Peter Astrand454f7672005-01-01 09:36:35 +0000136 def test_check_call_zero(self):
137 # check_call() function with zero return code
138 rc = subprocess.check_call([sys.executable, "-c",
139 "import sys; sys.exit(0)"])
140 self.assertEqual(rc, 0)
141
142 def test_check_call_nonzero(self):
143 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000144 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +0000145 subprocess.check_call([sys.executable, "-c",
146 "import sys; sys.exit(47)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000147 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +0000148
Georg Brandlf9734072008-12-07 15:30:06 +0000149 def test_check_output(self):
150 # check_output() function with zero return code
151 output = subprocess.check_output(
152 [sys.executable, "-c", "print('BDFL')"])
Benjamin Peterson577473f2010-01-19 00:09:57 +0000153 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +0000154
155 def test_check_output_nonzero(self):
156 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000157 with self.assertRaises(subprocess.CalledProcessError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +0000158 subprocess.check_output(
159 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000160 self.assertEqual(c.exception.returncode, 5)
Georg Brandlf9734072008-12-07 15:30:06 +0000161
162 def test_check_output_stderr(self):
163 # check_output() function stderr redirected to stdout
164 output = subprocess.check_output(
165 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
166 stderr=subprocess.STDOUT)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000167 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +0000168
Serhiy Storchakafcd9f222013-04-22 20:20:54 +0300169 def test_check_output_stdin_arg(self):
170 # check_output() can be called with stdin set to a file
171 tf = tempfile.TemporaryFile()
172 self.addCleanup(tf.close)
173 tf.write(b'pear')
174 tf.seek(0)
175 output = subprocess.check_output(
176 [sys.executable, "-c",
177 "import sys; sys.stdout.write(sys.stdin.read().upper())"],
178 stdin=tf)
179 self.assertIn(b'PEAR', output)
180
181 def test_check_output_input_arg(self):
182 # check_output() can be called with input set to a string
183 output = subprocess.check_output(
184 [sys.executable, "-c",
185 "import sys; sys.stdout.write(sys.stdin.read().upper())"],
186 input=b'pear')
187 self.assertIn(b'PEAR', output)
188
Georg Brandlf9734072008-12-07 15:30:06 +0000189 def test_check_output_stdout_arg(self):
Serhiy Storchakafcd9f222013-04-22 20:20:54 +0300190 # check_output() refuses to accept 'stdout' argument
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000191 with self.assertRaises(ValueError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +0000192 output = subprocess.check_output(
193 [sys.executable, "-c", "print('will not be run')"],
194 stdout=sys.stdout)
Georg Brandlf9734072008-12-07 15:30:06 +0000195 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000196 self.assertIn('stdout', c.exception.args[0])
Georg Brandlf9734072008-12-07 15:30:06 +0000197
Serhiy Storchakafcd9f222013-04-22 20:20:54 +0300198 def test_check_output_stdin_with_input_arg(self):
199 # check_output() refuses to accept 'stdin' with 'input'
200 tf = tempfile.TemporaryFile()
201 self.addCleanup(tf.close)
202 tf.write(b'pear')
203 tf.seek(0)
204 with self.assertRaises(ValueError) as c:
205 output = subprocess.check_output(
206 [sys.executable, "-c", "print('will not be run')"],
207 stdin=tf, input=b'hare')
208 self.fail("Expected ValueError when stdin and input args supplied.")
209 self.assertIn('stdin', c.exception.args[0])
210 self.assertIn('input', c.exception.args[0])
211
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400212 def test_check_output_timeout(self):
213 # check_output() function with timeout arg
214 with self.assertRaises(subprocess.TimeoutExpired) as c:
215 output = subprocess.check_output(
216 [sys.executable, "-c",
Victor Stinner149b1c72011-06-06 23:43:02 +0200217 "import sys, time\n"
218 "sys.stdout.write('BDFL')\n"
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400219 "sys.stdout.flush()\n"
Victor Stinner149b1c72011-06-06 23:43:02 +0200220 "time.sleep(3600)"],
Reid Klecknerda9ac722011-03-16 17:08:21 -0400221 # Some heavily loaded buildbots (sparc Debian 3.x) require
222 # this much time to start and print.
223 timeout=3)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400224 self.fail("Expected TimeoutExpired.")
225 self.assertEqual(c.exception.output, b'BDFL')
226
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000227 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000228 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000229 newenv = os.environ.copy()
230 newenv["FRUIT"] = "banana"
231 rc = subprocess.call([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000232 'import sys, os;'
233 'sys.exit(os.getenv("FRUIT")=="banana")'],
234 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000235 self.assertEqual(rc, 1)
236
Victor Stinner87b9bc32011-06-01 00:57:47 +0200237 def test_invalid_args(self):
238 # Popen() called with invalid arguments should raise TypeError
239 # but Popen.__del__ should not complain (issue #12085)
240 with support.captured_stderr() as s:
241 self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
242 argcount = subprocess.Popen.__init__.__code__.co_argcount
243 too_many_args = [0] * (argcount + 1)
244 self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
245 self.assertEqual(s.getvalue(), '')
246
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000247 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000248 # .stdin is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000249 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000250 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000251 self.addCleanup(p.stdout.close)
252 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000253 p.wait()
254 self.assertEqual(p.stdin, None)
255
256 def test_stdout_none(self):
Ezio Melotti42a541b2013-03-11 05:53:34 +0200257 # .stdout is None when not redirected, and the child's stdout will
258 # be inherited from the parent. In order to test this we run a
259 # subprocess in a subprocess:
260 # this_test
261 # \-- subprocess created by this test (parent)
262 # \-- subprocess created by the parent subprocess (child)
263 # The parent doesn't specify stdout, so the child will use the
264 # parent's stdout. This test checks that the message printed by the
265 # child goes to the parent stdout. The parent also checks that the
266 # child's stdout is None. See #11963.
267 code = ('import sys; from subprocess import Popen, PIPE;'
268 'p = Popen([sys.executable, "-c", "print(\'test_stdout_none\')"],'
269 ' stdin=PIPE, stderr=PIPE);'
270 'p.wait(); assert p.stdout is None;')
271 p = subprocess.Popen([sys.executable, "-c", code],
272 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
273 self.addCleanup(p.stdout.close)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000274 self.addCleanup(p.stderr.close)
Ezio Melotti42a541b2013-03-11 05:53:34 +0200275 out, err = p.communicate()
276 self.assertEqual(p.returncode, 0, err)
277 self.assertEqual(out.rstrip(), b'test_stdout_none')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000278
279 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000280 # .stderr is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000281 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000282 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000283 self.addCleanup(p.stdout.close)
284 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000285 p.wait()
286 self.assertEqual(p.stderr, None)
287
Chris Jerdonek776cb192012-10-08 15:56:43 -0700288 def _assert_python(self, pre_args, **kwargs):
289 # We include sys.exit() to prevent the test runner from hanging
290 # whenever python is found.
291 args = pre_args + ["import sys; sys.exit(47)"]
292 p = subprocess.Popen(args, **kwargs)
293 p.wait()
294 self.assertEqual(47, p.returncode)
295
296 def test_executable(self):
297 # Check that the executable argument works.
Chris Jerdonek86b0fb22012-10-09 13:17:49 -0700298 #
299 # On Unix (non-Mac and non-Windows), Python looks at args[0] to
300 # determine where its standard library is, so we need the directory
301 # of args[0] to be valid for the Popen() call to Python to succeed.
302 # See also issue #16170 and issue #7774.
303 doesnotexist = os.path.join(os.path.dirname(sys.executable),
304 "doesnotexist")
305 self._assert_python([doesnotexist, "-c"], executable=sys.executable)
Chris Jerdonek776cb192012-10-08 15:56:43 -0700306
307 def test_executable_takes_precedence(self):
308 # Check that the executable argument takes precedence over args[0].
309 #
310 # Verify first that the call succeeds without the executable arg.
311 pre_args = [sys.executable, "-c"]
312 self._assert_python(pre_args)
Victor Stinnerb31206a2018-01-25 19:06:05 +0100313 self.assertRaises(NONEXISTING_ERRORS,
Xavier de Gaye38c8b7d2016-11-14 17:14:42 +0100314 self._assert_python, pre_args,
Victor Stinnerb31206a2018-01-25 19:06:05 +0100315 executable=NONEXISTING_CMD[0])
Chris Jerdonek776cb192012-10-08 15:56:43 -0700316
Victor Stinner937ee9e2018-06-26 02:11:06 +0200317 @unittest.skipIf(mswindows, "executable argument replaces shell")
Chris Jerdonek776cb192012-10-08 15:56:43 -0700318 def test_executable_replaces_shell(self):
319 # Check that the executable argument replaces the default shell
320 # when shell=True.
321 self._assert_python([], executable=sys.executable, shell=True)
322
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700323 # For use in the test_cwd* tests below.
324 def _normalize_cwd(self, cwd):
325 # Normalize an expected cwd (for Tru64 support).
326 # We can't use os.path.realpath since it doesn't expand Tru64 {memb}
327 # strings. See bug #1063571.
Serhiy Storchaka2a23adf2015-09-06 14:13:25 +0300328 with support.change_cwd(cwd):
329 return os.getcwd()
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700330
331 # For use in the test_cwd* tests below.
332 def _split_python_path(self):
333 # Return normalized (python_dir, python_base).
334 python_path = os.path.realpath(sys.executable)
335 return os.path.split(python_path)
336
337 # For use in the test_cwd* tests below.
338 def _assert_cwd(self, expected_cwd, python_arg, **kwargs):
339 # Invoke Python via Popen, and assert that (1) the call succeeds,
340 # and that (2) the current working directory of the child process
341 # matches *expected_cwd*.
342 p = subprocess.Popen([python_arg, "-c",
343 "import os, sys; "
344 "sys.stdout.write(os.getcwd()); "
345 "sys.exit(47)"],
346 stdout=subprocess.PIPE,
347 **kwargs)
348 self.addCleanup(p.stdout.close)
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000349 p.wait()
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700350 self.assertEqual(47, p.returncode)
351 normcase = os.path.normcase
352 self.assertEqual(normcase(expected_cwd),
353 normcase(p.stdout.read().decode("utf-8")))
354
355 def test_cwd(self):
356 # Check that cwd changes the cwd for the child process.
357 temp_dir = tempfile.gettempdir()
358 temp_dir = self._normalize_cwd(temp_dir)
359 self._assert_cwd(temp_dir, sys.executable, cwd=temp_dir)
360
Sayan Chowdhuryd5c11f72017-02-26 22:36:10 +0530361 def test_cwd_with_pathlike(self):
362 temp_dir = tempfile.gettempdir()
363 temp_dir = self._normalize_cwd(temp_dir)
Serhiy Storchakab21d1552018-03-02 11:53:51 +0200364 self._assert_cwd(temp_dir, sys.executable, cwd=FakePath(temp_dir))
Sayan Chowdhuryd5c11f72017-02-26 22:36:10 +0530365
Victor Stinner937ee9e2018-06-26 02:11:06 +0200366 @unittest.skipIf(mswindows, "pending resolution of issue #15533")
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700367 def test_cwd_with_relative_arg(self):
368 # Check that Popen looks for args[0] relative to cwd if args[0]
369 # is relative.
370 python_dir, python_base = self._split_python_path()
371 rel_python = os.path.join(os.curdir, python_base)
372 with support.temp_cwd() as wrong_dir:
373 # Before calling with the correct cwd, confirm that the call fails
374 # without cwd and with the wrong cwd.
Chris Jerdonek28714c82012-09-30 02:15:37 -0700375 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700376 [rel_python])
Chris Jerdonek28714c82012-09-30 02:15:37 -0700377 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700378 [rel_python], cwd=wrong_dir)
379 python_dir = self._normalize_cwd(python_dir)
380 self._assert_cwd(python_dir, rel_python, cwd=python_dir)
381
Victor Stinner937ee9e2018-06-26 02:11:06 +0200382 @unittest.skipIf(mswindows, "pending resolution of issue #15533")
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700383 def test_cwd_with_relative_executable(self):
384 # Check that Popen looks for executable relative to cwd if executable
385 # is relative (and that executable takes precedence over args[0]).
386 python_dir, python_base = self._split_python_path()
387 rel_python = os.path.join(os.curdir, python_base)
388 doesntexist = "somethingyoudonthave"
389 with support.temp_cwd() as wrong_dir:
390 # Before calling with the correct cwd, confirm that the call fails
391 # without cwd and with the wrong cwd.
Chris Jerdonek28714c82012-09-30 02:15:37 -0700392 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700393 [doesntexist], executable=rel_python)
Chris Jerdonek28714c82012-09-30 02:15:37 -0700394 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700395 [doesntexist], executable=rel_python,
396 cwd=wrong_dir)
397 python_dir = self._normalize_cwd(python_dir)
398 self._assert_cwd(python_dir, doesntexist, executable=rel_python,
399 cwd=python_dir)
400
401 def test_cwd_with_absolute_arg(self):
402 # Check that Popen can find the executable when the cwd is wrong
403 # if args[0] is an absolute path.
404 python_dir, python_base = self._split_python_path()
405 abs_python = os.path.join(python_dir, python_base)
406 rel_python = os.path.join(os.curdir, python_base)
Berker Peksagce643912015-05-06 06:33:17 +0300407 with support.temp_dir() as wrong_dir:
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700408 # Before calling with an absolute path, confirm that using a
409 # relative path fails.
Chris Jerdonek28714c82012-09-30 02:15:37 -0700410 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700411 [rel_python], cwd=wrong_dir)
412 wrong_dir = self._normalize_cwd(wrong_dir)
413 self._assert_cwd(wrong_dir, abs_python, cwd=wrong_dir)
414
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100415 @unittest.skipIf(sys.base_prefix != sys.prefix,
416 'Test is not venv-compatible')
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000417 def test_executable_with_cwd(self):
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700418 python_dir, python_base = self._split_python_path()
419 python_dir = self._normalize_cwd(python_dir)
420 self._assert_cwd(python_dir, "somethingyoudonthave",
421 executable=sys.executable, cwd=python_dir)
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000422
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100423 @unittest.skipIf(sys.base_prefix != sys.prefix,
424 'Test is not venv-compatible')
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000425 @unittest.skipIf(sysconfig.is_python_build(),
426 "need an installed Python. See #7774")
427 def test_executable_without_cwd(self):
428 # For a normal installation, it should work without 'cwd'
429 # argument. For test runs in the build directory, see #7774.
Ned Deilye92dfbf2013-08-02 18:02:21 -0700430 self._assert_cwd(os.getcwd(), "somethingyoudonthave",
431 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000432
433 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000434 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000435 p = subprocess.Popen([sys.executable, "-c",
436 'import sys; sys.exit(sys.stdin.read() == "pear")'],
437 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000438 p.stdin.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000439 p.stdin.close()
440 p.wait()
441 self.assertEqual(p.returncode, 1)
442
443 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000444 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000445 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000446 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000447 d = tf.fileno()
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000448 os.write(d, b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000449 os.lseek(d, 0, 0)
450 p = subprocess.Popen([sys.executable, "-c",
451 'import sys; sys.exit(sys.stdin.read() == "pear")'],
452 stdin=d)
453 p.wait()
454 self.assertEqual(p.returncode, 1)
455
456 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000457 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000458 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000459 self.addCleanup(tf.close)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000460 tf.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000461 tf.seek(0)
462 p = subprocess.Popen([sys.executable, "-c",
463 'import sys; sys.exit(sys.stdin.read() == "pear")'],
464 stdin=tf)
465 p.wait()
466 self.assertEqual(p.returncode, 1)
467
468 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000469 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000470 p = subprocess.Popen([sys.executable, "-c",
471 'import sys; sys.stdout.write("orange")'],
472 stdout=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +0200473 with p:
474 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000475
476 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000477 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000478 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000479 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000480 d = tf.fileno()
481 p = subprocess.Popen([sys.executable, "-c",
482 'import sys; sys.stdout.write("orange")'],
483 stdout=d)
484 p.wait()
485 os.lseek(d, 0, 0)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000486 self.assertEqual(os.read(d, 1024), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000487
488 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000489 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000490 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000491 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000492 p = subprocess.Popen([sys.executable, "-c",
493 'import sys; sys.stdout.write("orange")'],
494 stdout=tf)
495 p.wait()
496 tf.seek(0)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000497 self.assertEqual(tf.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000498
499 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000500 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000501 p = subprocess.Popen([sys.executable, "-c",
502 'import sys; sys.stderr.write("strawberry")'],
503 stderr=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +0200504 with p:
505 self.assertStderrEqual(p.stderr.read(), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000506
507 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000508 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000509 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000510 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000511 d = tf.fileno()
512 p = subprocess.Popen([sys.executable, "-c",
513 'import sys; sys.stderr.write("strawberry")'],
514 stderr=d)
515 p.wait()
516 os.lseek(d, 0, 0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000517 self.assertStderrEqual(os.read(d, 1024), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000518
519 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000520 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000521 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000522 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000523 p = subprocess.Popen([sys.executable, "-c",
524 'import sys; sys.stderr.write("strawberry")'],
525 stderr=tf)
526 p.wait()
527 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000528 self.assertStderrEqual(tf.read(), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000529
Martin Panterc7635892016-05-13 01:54:44 +0000530 def test_stderr_redirect_with_no_stdout_redirect(self):
531 # test stderr=STDOUT while stdout=None (not set)
532
533 # - grandchild prints to stderr
534 # - child redirects grandchild's stderr to its stdout
535 # - the parent should get grandchild's stderr in child's stdout
536 p = subprocess.Popen([sys.executable, "-c",
537 'import sys, subprocess;'
538 'rc = subprocess.call([sys.executable, "-c",'
539 ' "import sys;"'
540 ' "sys.stderr.write(\'42\')"],'
541 ' stderr=subprocess.STDOUT);'
542 'sys.exit(rc)'],
543 stdout=subprocess.PIPE,
544 stderr=subprocess.PIPE)
545 stdout, stderr = p.communicate()
546 #NOTE: stdout should get stderr from grandchild
547 self.assertStderrEqual(stdout, b'42')
548 self.assertStderrEqual(stderr, b'') # should be empty
549 self.assertEqual(p.returncode, 0)
550
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000551 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000552 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000553 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000554 'import sys;'
555 'sys.stdout.write("apple");'
556 'sys.stdout.flush();'
557 'sys.stderr.write("orange")'],
558 stdout=subprocess.PIPE,
559 stderr=subprocess.STDOUT)
Victor Stinner7438c612016-05-20 12:43:15 +0200560 with p:
561 self.assertStderrEqual(p.stdout.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000562
563 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000564 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000565 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000566 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000567 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000568 'import sys;'
569 'sys.stdout.write("apple");'
570 'sys.stdout.flush();'
571 'sys.stderr.write("orange")'],
572 stdout=tf,
573 stderr=tf)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000574 p.wait()
575 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000576 self.assertStderrEqual(tf.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000577
Thomas Wouters89f507f2006-12-13 04:49:30 +0000578 def test_stdout_filedes_of_stdout(self):
579 # stdout is set to 1 (#1531862).
Ezio Melotti42a541b2013-03-11 05:53:34 +0200580 # To avoid printing the text on stdout, we do something similar to
581 # test_stdout_none (see above). The parent subprocess calls the child
582 # subprocess passing stdout=1, and this test uses stdout=PIPE in
583 # order to capture and check the output of the parent. See #11963.
584 code = ('import sys, subprocess; '
585 'rc = subprocess.call([sys.executable, "-c", '
586 ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), '
587 'b\'test with stdout=1\'))"], stdout=1); '
588 'assert rc == 18')
589 p = subprocess.Popen([sys.executable, "-c", code],
590 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
591 self.addCleanup(p.stdout.close)
592 self.addCleanup(p.stderr.close)
593 out, err = p.communicate()
594 self.assertEqual(p.returncode, 0, err)
595 self.assertEqual(out.rstrip(), b'test with stdout=1')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000596
Ross Lagerwallba102ec2011-03-16 18:40:25 +0200597 def test_stdout_devnull(self):
598 p = subprocess.Popen([sys.executable, "-c",
599 'for i in range(10240):'
600 'print("x" * 1024)'],
601 stdout=subprocess.DEVNULL)
602 p.wait()
603 self.assertEqual(p.stdout, None)
604
605 def test_stderr_devnull(self):
606 p = subprocess.Popen([sys.executable, "-c",
607 'import sys\n'
608 'for i in range(10240):'
609 'sys.stderr.write("x" * 1024)'],
610 stderr=subprocess.DEVNULL)
611 p.wait()
612 self.assertEqual(p.stderr, None)
613
614 def test_stdin_devnull(self):
615 p = subprocess.Popen([sys.executable, "-c",
616 'import sys;'
617 'sys.stdin.read(1)'],
618 stdin=subprocess.DEVNULL)
619 p.wait()
620 self.assertEqual(p.stdin, None)
621
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000622 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000623 newenv = os.environ.copy()
624 newenv["FRUIT"] = "orange"
Victor Stinnerf1512a22011-06-21 17:18:38 +0200625 with subprocess.Popen([sys.executable, "-c",
626 'import sys,os;'
627 'sys.stdout.write(os.getenv("FRUIT"))'],
628 stdout=subprocess.PIPE,
629 env=newenv) as p:
630 stdout, stderr = p.communicate()
631 self.assertEqual(stdout, b"orange")
632
Victor Stinner62d51182011-06-23 01:02:25 +0200633 # Windows requires at least the SYSTEMROOT environment variable to start
634 # Python
635 @unittest.skipIf(sys.platform == 'win32',
636 'cannot test an empty env on Windows')
Gregory P. Smithb3512482017-05-30 14:40:37 -0700637 @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') == 1,
638 'The Python shared library cannot be loaded '
639 'with an empty environment.')
Victor Stinnerf1512a22011-06-21 17:18:38 +0200640 def test_empty_env(self):
Gregory P. Smithb3512482017-05-30 14:40:37 -0700641 """Verify that env={} is as empty as possible."""
642
Gregory P. Smith85aba232017-05-30 16:21:47 -0700643 def is_env_var_to_ignore(n):
Gregory P. Smithb3512482017-05-30 14:40:37 -0700644 """Determine if an environment variable is under our control."""
645 # This excludes some __CF_* and VERSIONER_* keys MacOS insists
646 # on adding even when the environment in exec is empty.
647 # Gentoo sandboxes also force LD_PRELOAD and SANDBOX_* to exist.
Gregory P. Smith85aba232017-05-30 16:21:47 -0700648 return ('VERSIONER' in n or '__CF' in n or # MacOS
Ned Deily918edc02017-09-04 00:00:21 -0400649 '__PYVENV_LAUNCHER__' in n or # MacOS framework build
Nick Coghlan6ea41862017-06-11 13:16:15 +1000650 n == 'LD_PRELOAD' or n.startswith('SANDBOX') or # Gentoo
651 n == 'LC_CTYPE') # Locale coercion triggered
Gregory P. Smithb3512482017-05-30 14:40:37 -0700652
Victor Stinnerf1512a22011-06-21 17:18:38 +0200653 with subprocess.Popen([sys.executable, "-c",
Gregory P. Smithb3512482017-05-30 14:40:37 -0700654 'import os; print(list(os.environ.keys()))'],
655 stdout=subprocess.PIPE, env={}) as p:
Victor Stinnerf1512a22011-06-21 17:18:38 +0200656 stdout, stderr = p.communicate()
Gregory P. Smithb3512482017-05-30 14:40:37 -0700657 child_env_names = eval(stdout.strip())
658 self.assertIsInstance(child_env_names, list)
659 child_env_names = [k for k in child_env_names
660 if not is_env_var_to_ignore(k)]
661 self.assertEqual(child_env_names, [])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000662
Serhiy Storchakad174d242017-06-23 19:39:27 +0300663 def test_invalid_cmd(self):
664 # null character in the command name
665 cmd = sys.executable + '\0'
666 with self.assertRaises(ValueError):
667 subprocess.Popen([cmd, "-c", "pass"])
668
669 # null character in the command argument
670 with self.assertRaises(ValueError):
671 subprocess.Popen([sys.executable, "-c", "pass#\0"])
672
673 def test_invalid_env(self):
Ville Skyttä49b27342017-08-03 09:00:59 +0300674 # null character in the environment variable name
Serhiy Storchakad174d242017-06-23 19:39:27 +0300675 newenv = os.environ.copy()
676 newenv["FRUIT\0VEGETABLE"] = "cabbage"
677 with self.assertRaises(ValueError):
678 subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
679
Ville Skyttä49b27342017-08-03 09:00:59 +0300680 # null character in the environment variable value
Serhiy Storchakad174d242017-06-23 19:39:27 +0300681 newenv = os.environ.copy()
682 newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
683 with self.assertRaises(ValueError):
684 subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
685
Ville Skyttä49b27342017-08-03 09:00:59 +0300686 # equal character in the environment variable name
Serhiy Storchakad174d242017-06-23 19:39:27 +0300687 newenv = os.environ.copy()
688 newenv["FRUIT=ORANGE"] = "lemon"
689 with self.assertRaises(ValueError):
690 subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
691
Ville Skyttä49b27342017-08-03 09:00:59 +0300692 # equal character in the environment variable value
Serhiy Storchakad174d242017-06-23 19:39:27 +0300693 newenv = os.environ.copy()
694 newenv["FRUIT"] = "orange=lemon"
695 with subprocess.Popen([sys.executable, "-c",
696 'import sys, os;'
697 'sys.stdout.write(os.getenv("FRUIT"))'],
698 stdout=subprocess.PIPE,
699 env=newenv) as p:
700 stdout, stderr = p.communicate()
701 self.assertEqual(stdout, b"orange=lemon")
702
Peter Astrandcbac93c2005-03-03 20:24:28 +0000703 def test_communicate_stdin(self):
704 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000705 'import sys;'
706 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000707 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000708 p.communicate(b"pear")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000709 self.assertEqual(p.returncode, 1)
710
711 def test_communicate_stdout(self):
712 p = subprocess.Popen([sys.executable, "-c",
713 'import sys; sys.stdout.write("pineapple")'],
714 stdout=subprocess.PIPE)
715 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000716 self.assertEqual(stdout, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000717 self.assertEqual(stderr, None)
718
719 def test_communicate_stderr(self):
720 p = subprocess.Popen([sys.executable, "-c",
721 'import sys; sys.stderr.write("pineapple")'],
722 stderr=subprocess.PIPE)
723 (stdout, stderr) = p.communicate()
724 self.assertEqual(stdout, None)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000725 self.assertStderrEqual(stderr, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000726
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000727 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000728 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000729 'import sys,os;'
730 'sys.stderr.write("pineapple");'
731 'sys.stdout.write(sys.stdin.read())'],
732 stdin=subprocess.PIPE,
733 stdout=subprocess.PIPE,
734 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000735 self.addCleanup(p.stdout.close)
736 self.addCleanup(p.stderr.close)
737 self.addCleanup(p.stdin.close)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000738 (stdout, stderr) = p.communicate(b"banana")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000739 self.assertEqual(stdout, b"banana")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000740 self.assertStderrEqual(stderr, b"pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000741
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400742 def test_communicate_timeout(self):
743 p = subprocess.Popen([sys.executable, "-c",
744 'import sys,os,time;'
745 'sys.stderr.write("pineapple\\n");'
746 'time.sleep(1);'
747 'sys.stderr.write("pear\\n");'
748 'sys.stdout.write(sys.stdin.read())'],
749 universal_newlines=True,
750 stdin=subprocess.PIPE,
751 stdout=subprocess.PIPE,
752 stderr=subprocess.PIPE)
753 self.assertRaises(subprocess.TimeoutExpired, p.communicate, "banana",
754 timeout=0.3)
755 # Make sure we can keep waiting for it, and that we get the whole output
756 # after it completes.
757 (stdout, stderr) = p.communicate()
758 self.assertEqual(stdout, "banana")
759 self.assertStderrEqual(stderr.encode(), b"pineapple\npear\n")
760
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700761 def test_communicate_timeout_large_output(self):
Ross Lagerwall003c7a32012-02-12 09:02:01 +0200762 # Test an expiring timeout while the child is outputting lots of data.
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400763 p = subprocess.Popen([sys.executable, "-c",
764 'import sys,os,time;'
765 'sys.stdout.write("a" * (64 * 1024));'
766 'time.sleep(0.2);'
767 'sys.stdout.write("a" * (64 * 1024));'
768 'time.sleep(0.2);'
769 'sys.stdout.write("a" * (64 * 1024));'
770 'time.sleep(0.2);'
771 'sys.stdout.write("a" * (64 * 1024));'],
772 stdout=subprocess.PIPE)
773 self.assertRaises(subprocess.TimeoutExpired, p.communicate, timeout=0.4)
774 (stdout, _) = p.communicate()
775 self.assertEqual(len(stdout), 4 * 64 * 1024)
776
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000777 # Test for the fd leak reported in http://bugs.python.org/issue2791.
778 def test_communicate_pipe_fd_leak(self):
Victor Stinner667d4b52010-12-25 22:40:32 +0000779 for stdin_pipe in (False, True):
780 for stdout_pipe in (False, True):
781 for stderr_pipe in (False, True):
782 options = {}
783 if stdin_pipe:
784 options['stdin'] = subprocess.PIPE
785 if stdout_pipe:
786 options['stdout'] = subprocess.PIPE
787 if stderr_pipe:
788 options['stderr'] = subprocess.PIPE
789 if not options:
790 continue
791 p = subprocess.Popen((sys.executable, "-c", "pass"), **options)
792 p.communicate()
793 if p.stdin is not None:
794 self.assertTrue(p.stdin.closed)
795 if p.stdout is not None:
796 self.assertTrue(p.stdout.closed)
797 if p.stderr is not None:
798 self.assertTrue(p.stderr.closed)
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000799
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000800 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000801 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000802 p = subprocess.Popen([sys.executable, "-c",
803 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000804 (stdout, stderr) = p.communicate()
805 self.assertEqual(stdout, None)
806 self.assertEqual(stderr, None)
807
808 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000809 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000810 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000811 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000812 x, y = os.pipe()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000813 os.close(x)
814 os.close(y)
815 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000816 'import sys,os;'
817 'sys.stdout.write(sys.stdin.read(47));'
Charles-François Natali2d517212011-05-29 16:36:44 +0200818 'sys.stderr.write("x" * %d);'
819 'sys.stdout.write(sys.stdin.read())' %
820 support.PIPE_MAX_SIZE],
Guido van Rossum98297ee2007-11-06 21:34:58 +0000821 stdin=subprocess.PIPE,
822 stdout=subprocess.PIPE,
823 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000824 self.addCleanup(p.stdout.close)
825 self.addCleanup(p.stderr.close)
826 self.addCleanup(p.stdin.close)
Charles-François Natali2d517212011-05-29 16:36:44 +0200827 string_to_write = b"a" * support.PIPE_MAX_SIZE
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000828 (stdout, stderr) = p.communicate(string_to_write)
829 self.assertEqual(stdout, string_to_write)
830
831 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000832 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000833 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000834 'import sys,os;'
835 'sys.stdout.write(sys.stdin.read())'],
836 stdin=subprocess.PIPE,
837 stdout=subprocess.PIPE,
838 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000839 self.addCleanup(p.stdout.close)
840 self.addCleanup(p.stderr.close)
841 self.addCleanup(p.stdin.close)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000842 p.stdin.write(b"banana")
843 (stdout, stderr) = p.communicate(b"split")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000844 self.assertEqual(stdout, b"bananasplit")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000845 self.assertStderrEqual(stderr, b"")
Tim Peterse718f612004-10-12 21:51:32 +0000846
andyclegg7fed7bd2017-10-23 03:01:19 +0100847 def test_universal_newlines_and_text(self):
848 args = [
849 sys.executable, "-c",
850 'import sys,os;' + SETBINARY +
851 'buf = sys.stdout.buffer;'
852 'buf.write(sys.stdin.readline().encode());'
853 'buf.flush();'
854 'buf.write(b"line2\\n");'
855 'buf.flush();'
856 'buf.write(sys.stdin.read().encode());'
857 'buf.flush();'
858 'buf.write(b"line4\\n");'
859 'buf.flush();'
860 'buf.write(b"line5\\r\\n");'
861 'buf.flush();'
862 'buf.write(b"line6\\r");'
863 'buf.flush();'
864 'buf.write(b"\\nline7");'
865 'buf.flush();'
866 'buf.write(b"\\nline8");']
867
868 for extra_kwarg in ('universal_newlines', 'text'):
869 p = subprocess.Popen(args, **{'stdin': subprocess.PIPE,
870 'stdout': subprocess.PIPE,
871 extra_kwarg: True})
872 with p:
873 p.stdin.write("line1\n")
874 p.stdin.flush()
875 self.assertEqual(p.stdout.readline(), "line1\n")
876 p.stdin.write("line3\n")
877 p.stdin.close()
878 self.addCleanup(p.stdout.close)
879 self.assertEqual(p.stdout.readline(),
880 "line2\n")
881 self.assertEqual(p.stdout.read(6),
882 "line3\n")
883 self.assertEqual(p.stdout.read(),
884 "line4\nline5\nline6\nline7\nline8")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000885
886 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000887 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000888 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000889 'import sys,os;' + SETBINARY +
Antoine Pitrouec2d2692012-08-05 00:23:40 +0200890 'buf = sys.stdout.buffer;'
891 'buf.write(b"line2\\n");'
892 'buf.flush();'
893 'buf.write(b"line4\\n");'
894 'buf.flush();'
895 'buf.write(b"line5\\r\\n");'
896 'buf.flush();'
897 'buf.write(b"line6\\r");'
898 'buf.flush();'
899 'buf.write(b"\\nline7");'
900 'buf.flush();'
901 'buf.write(b"\\nline8");'],
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200902 stderr=subprocess.PIPE,
903 stdout=subprocess.PIPE,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000904 universal_newlines=1)
Brian Curtin19a53792010-11-05 17:09:05 +0000905 self.addCleanup(p.stdout.close)
906 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000907 (stdout, stderr) = p.communicate()
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200908 self.assertEqual(stdout,
909 "line2\nline4\nline5\nline6\nline7\nline8")
910
911 def test_universal_newlines_communicate_stdin(self):
912 # universal newlines through communicate(), with only stdin
913 p = subprocess.Popen([sys.executable, "-c",
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300914 'import sys,os;' + SETBINARY + textwrap.dedent('''
915 s = sys.stdin.readline()
916 assert s == "line1\\n", repr(s)
917 s = sys.stdin.read()
918 assert s == "line3\\n", repr(s)
919 ''')],
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200920 stdin=subprocess.PIPE,
921 universal_newlines=1)
922 (stdout, stderr) = p.communicate("line1\nline3\n")
923 self.assertEqual(p.returncode, 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000924
Andrew Svetlovf3765072012-08-14 18:35:17 +0300925 def test_universal_newlines_communicate_input_none(self):
926 # Test communicate(input=None) with universal newlines.
927 #
928 # We set stdout to PIPE because, as of this writing, a different
929 # code path is tested when the number of pipes is zero or one.
930 p = subprocess.Popen([sys.executable, "-c", "pass"],
931 stdin=subprocess.PIPE,
932 stdout=subprocess.PIPE,
933 universal_newlines=True)
934 p.communicate()
935 self.assertEqual(p.returncode, 0)
936
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300937 def test_universal_newlines_communicate_stdin_stdout_stderr(self):
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300938 # universal newlines through communicate(), with stdin, stdout, stderr
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300939 p = subprocess.Popen([sys.executable, "-c",
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300940 'import sys,os;' + SETBINARY + textwrap.dedent('''
941 s = sys.stdin.buffer.readline()
942 sys.stdout.buffer.write(s)
943 sys.stdout.buffer.write(b"line2\\r")
944 sys.stderr.buffer.write(b"eline2\\n")
945 s = sys.stdin.buffer.read()
946 sys.stdout.buffer.write(s)
947 sys.stdout.buffer.write(b"line4\\n")
948 sys.stdout.buffer.write(b"line5\\r\\n")
949 sys.stderr.buffer.write(b"eline6\\r")
950 sys.stderr.buffer.write(b"eline7\\r\\nz")
951 ''')],
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300952 stdin=subprocess.PIPE,
953 stderr=subprocess.PIPE,
954 stdout=subprocess.PIPE,
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300955 universal_newlines=True)
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300956 self.addCleanup(p.stdout.close)
957 self.addCleanup(p.stderr.close)
958 (stdout, stderr) = p.communicate("line1\nline3\n")
959 self.assertEqual(p.returncode, 0)
Andrew Svetlov943c5b32012-08-16 20:17:47 +0300960 self.assertEqual("line1\nline2\nline3\nline4\nline5\n", stdout)
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300961 # Python debug build push something like "[42442 refs]\n"
962 # to stderr at exit of subprocess.
Andrew Svetlov943c5b32012-08-16 20:17:47 +0300963 # Don't use assertStderrEqual because it strips CR and LF from output.
964 self.assertTrue(stderr.startswith("eline2\neline6\neline7\n"))
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300965
Andrew Svetlov82860712012-08-19 22:13:41 +0300966 def test_universal_newlines_communicate_encodings(self):
967 # Check that universal newlines mode works for various encodings,
968 # in particular for encodings in the UTF-16 and UTF-32 families.
969 # See issue #15595.
970 #
971 # UTF-16 and UTF-32-BE are sufficient to check both with BOM and
972 # without, and UTF-16 and UTF-32.
973 for encoding in ['utf-16', 'utf-32-be']:
Andrew Svetlov82860712012-08-19 22:13:41 +0300974 code = ("import sys; "
975 r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" %
976 encoding)
977 args = [sys.executable, '-c', code]
Steve Dower050acae2016-09-06 20:16:17 -0700978 # We set stdin to be non-None because, as of this writing,
979 # a different code path is used when the number of pipes is
980 # zero or one.
981 popen = subprocess.Popen(args,
982 stdin=subprocess.PIPE,
983 stdout=subprocess.PIPE,
984 encoding=encoding)
985 stdout, stderr = popen.communicate(input='')
Andrew Svetlov82860712012-08-19 22:13:41 +0300986 self.assertEqual(stdout, '1\n2\n3\n4')
987
Steve Dower050acae2016-09-06 20:16:17 -0700988 def test_communicate_errors(self):
989 for errors, expected in [
990 ('ignore', ''),
991 ('replace', '\ufffd\ufffd'),
992 ('surrogateescape', '\udc80\udc80'),
993 ('backslashreplace', '\\x80\\x80'),
994 ]:
995 code = ("import sys; "
996 r"sys.stdout.buffer.write(b'[\x80\x80]')")
997 args = [sys.executable, '-c', code]
998 # We set stdin to be non-None because, as of this writing,
999 # a different code path is used when the number of pipes is
1000 # zero or one.
1001 popen = subprocess.Popen(args,
1002 stdin=subprocess.PIPE,
1003 stdout=subprocess.PIPE,
1004 encoding='utf-8',
1005 errors=errors)
1006 stdout, stderr = popen.communicate(input='')
1007 self.assertEqual(stdout, '[{}]'.format(expected))
1008
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001009 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +00001010 # Make sure we leak no resources
Victor Stinner937ee9e2018-06-26 02:11:06 +02001011 if not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +00001012 max_handles = 1026 # too much for most UNIX systems
1013 else:
Antoine Pitrou8db30272010-09-18 22:38:48 +00001014 max_handles = 2050 # too much for (at least some) Windows setups
1015 handles = []
Gregory P. Smith81ce6852011-03-15 02:04:11 -04001016 tmpdir = tempfile.mkdtemp()
Antoine Pitrou8db30272010-09-18 22:38:48 +00001017 try:
1018 for i in range(max_handles):
1019 try:
Gregory P. Smith81ce6852011-03-15 02:04:11 -04001020 tmpfile = os.path.join(tmpdir, support.TESTFN)
1021 handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT))
Antoine Pitrou8db30272010-09-18 22:38:48 +00001022 except OSError as e:
1023 if e.errno != errno.EMFILE:
1024 raise
1025 break
1026 else:
1027 self.skipTest("failed to reach the file descriptor limit "
1028 "(tried %d)" % max_handles)
1029 # Close a couple of them (should be enough for a subprocess)
1030 for i in range(10):
1031 os.close(handles.pop())
1032 # Loop creating some subprocesses. If one of them leaks some fds,
1033 # the next loop iteration will fail by reaching the max fd limit.
1034 for i in range(15):
1035 p = subprocess.Popen([sys.executable, "-c",
1036 "import sys;"
1037 "sys.stdout.write(sys.stdin.read())"],
1038 stdin=subprocess.PIPE,
1039 stdout=subprocess.PIPE,
1040 stderr=subprocess.PIPE)
1041 data = p.communicate(b"lime")[0]
1042 self.assertEqual(data, b"lime")
1043 finally:
1044 for h in handles:
1045 os.close(h)
Gregory P. Smith81ce6852011-03-15 02:04:11 -04001046 shutil.rmtree(tmpdir)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001047
1048 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001049 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
1050 '"a b c" d e')
1051 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
1052 'ab\\"c \\ d')
Christian Heimesfdab48e2008-01-20 09:06:41 +00001053 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
1054 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001055 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
1056 'a\\\\\\b "de fg" h')
1057 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
1058 'a\\\\\\"b c d')
1059 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
1060 '"a\\\\b c" d e')
1061 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
1062 '"a\\\\b\\ c" d e')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001063 self.assertEqual(subprocess.list2cmdline(['ab', '']),
1064 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001065
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001066 def test_poll(self):
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001067 p = subprocess.Popen([sys.executable, "-c",
Ross Lagerwalle7ad4192012-02-22 06:02:07 +02001068 "import os; os.read(0, 1)"],
1069 stdin=subprocess.PIPE)
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001070 self.addCleanup(p.stdin.close)
1071 self.assertIsNone(p.poll())
1072 os.write(p.stdin.fileno(), b'A')
1073 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001074 # Subsequent invocations should just return the returncode
1075 self.assertEqual(p.poll(), 0)
1076
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001077 def test_wait(self):
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001078 p = subprocess.Popen([sys.executable, "-c", "pass"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001079 self.assertEqual(p.wait(), 0)
1080 # Subsequent invocations should just return the returncode
1081 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +00001082
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001083 def test_wait_timeout(self):
1084 p = subprocess.Popen([sys.executable,
Antoine Pitroudc49b2b2013-05-19 15:55:40 +02001085 "-c", "import time; time.sleep(0.3)"])
Reid Kleckner2b228f02011-03-16 16:57:54 -04001086 with self.assertRaises(subprocess.TimeoutExpired) as c:
Antoine Pitroudc49b2b2013-05-19 15:55:40 +02001087 p.wait(timeout=0.0001)
1088 self.assertIn("0.0001", str(c.exception)) # For coverage of __str__.
Reid Klecknerda9ac722011-03-16 17:08:21 -04001089 # Some heavily loaded buildbots (sparc Debian 3.x) require this much
1090 # time to start.
1091 self.assertEqual(p.wait(timeout=3), 0)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001092
Peter Astrand738131d2004-11-30 21:04:45 +00001093 def test_invalid_bufsize(self):
1094 # an invalid type of the bufsize argument should raise
1095 # TypeError.
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001096 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +00001097 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +00001098
Guido van Rossum46a05a72007-06-07 21:56:45 +00001099 def test_bufsize_is_none(self):
1100 # bufsize=None should be the same as bufsize=0.
1101 p = subprocess.Popen([sys.executable, "-c", "pass"], None)
1102 self.assertEqual(p.wait(), 0)
1103 # Again with keyword arg
1104 p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None)
1105 self.assertEqual(p.wait(), 0)
1106
Antoine Pitrouafe8d062014-09-21 21:10:56 +02001107 def _test_bufsize_equal_one(self, line, expected, universal_newlines):
1108 # subprocess may deadlock with bufsize=1, see issue #21332
1109 with subprocess.Popen([sys.executable, "-c", "import sys;"
1110 "sys.stdout.write(sys.stdin.readline());"
1111 "sys.stdout.flush()"],
1112 stdin=subprocess.PIPE,
1113 stdout=subprocess.PIPE,
1114 stderr=subprocess.DEVNULL,
1115 bufsize=1,
1116 universal_newlines=universal_newlines) as p:
1117 p.stdin.write(line) # expect that it flushes the line in text mode
1118 os.close(p.stdin.fileno()) # close it without flushing the buffer
1119 read_line = p.stdout.readline()
Segev Finer4d385172017-08-18 16:18:13 +03001120 with support.SuppressCrashReport():
1121 try:
1122 p.stdin.close()
1123 except OSError:
1124 pass
Antoine Pitrouafe8d062014-09-21 21:10:56 +02001125 p.stdin = None
1126 self.assertEqual(p.returncode, 0)
1127 self.assertEqual(read_line, expected)
1128
1129 def test_bufsize_equal_one_text_mode(self):
1130 # line is flushed in text mode with bufsize=1.
1131 # we should get the full line in return
1132 line = "line\n"
1133 self._test_bufsize_equal_one(line, line, universal_newlines=True)
1134
1135 def test_bufsize_equal_one_binary_mode(self):
1136 # line is not flushed in binary mode with bufsize=1.
1137 # we should get empty response
1138 line = b'line' + os.linesep.encode() # assume ascii-based locale
1139 self._test_bufsize_equal_one(line, b'', universal_newlines=False)
1140
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001141 def test_leaking_fds_on_error(self):
1142 # see bug #5179: Popen leaks file descriptors to PIPEs if
1143 # the child fails to execute; this will eventually exhaust
1144 # the maximum number of open fds. 1024 seems a very common
1145 # value for that limit, but Windows has 2048, so we loop
1146 # 1024 times (each call leaked two fds).
1147 for i in range(1024):
Victor Stinnerb31206a2018-01-25 19:06:05 +01001148 with self.assertRaises(NONEXISTING_ERRORS):
Victor Stinner9a83f652017-08-21 23:51:31 +02001149 subprocess.Popen(NONEXISTING_CMD,
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001150 stdout=subprocess.PIPE,
1151 stderr=subprocess.PIPE)
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001152
Victor Stinner9a83f652017-08-21 23:51:31 +02001153 def test_nonexisting_with_pipes(self):
1154 # bpo-30121: Popen with pipes must close properly pipes on error.
1155 # Previously, os.close() was called with a Windows handle which is not
1156 # a valid file descriptor.
1157 #
1158 # Run the test in a subprocess to control how the CRT reports errors
1159 # and to get stderr content.
1160 try:
1161 import msvcrt
1162 msvcrt.CrtSetReportMode
1163 except (AttributeError, ImportError):
1164 self.skipTest("need msvcrt.CrtSetReportMode")
1165
1166 code = textwrap.dedent(f"""
1167 import msvcrt
1168 import subprocess
1169
1170 cmd = {NONEXISTING_CMD!r}
1171
1172 for report_type in [msvcrt.CRT_WARN,
1173 msvcrt.CRT_ERROR,
1174 msvcrt.CRT_ASSERT]:
1175 msvcrt.CrtSetReportMode(report_type, msvcrt.CRTDBG_MODE_FILE)
1176 msvcrt.CrtSetReportFile(report_type, msvcrt.CRTDBG_FILE_STDERR)
1177
1178 try:
Zachary Ware55376462018-02-19 14:02:38 -06001179 subprocess.Popen(cmd,
Victor Stinner9a83f652017-08-21 23:51:31 +02001180 stdout=subprocess.PIPE,
1181 stderr=subprocess.PIPE)
1182 except OSError:
1183 pass
1184 """)
1185 cmd = [sys.executable, "-c", code]
1186 proc = subprocess.Popen(cmd,
1187 stderr=subprocess.PIPE,
1188 universal_newlines=True)
1189 with proc:
1190 stderr = proc.communicate()[1]
1191 self.assertEqual(stderr, "")
1192 self.assertEqual(proc.returncode, 0)
1193
Antoine Pitroua8392712013-08-30 23:38:13 +02001194 def test_double_close_on_error(self):
1195 # Issue #18851
1196 fds = []
1197 def open_fds():
1198 for i in range(20):
1199 fds.extend(os.pipe())
1200 time.sleep(0.001)
1201 t = threading.Thread(target=open_fds)
1202 t.start()
1203 try:
1204 with self.assertRaises(EnvironmentError):
Victor Stinner9a83f652017-08-21 23:51:31 +02001205 subprocess.Popen(NONEXISTING_CMD,
Antoine Pitroua8392712013-08-30 23:38:13 +02001206 stdin=subprocess.PIPE,
1207 stdout=subprocess.PIPE,
1208 stderr=subprocess.PIPE)
1209 finally:
1210 t.join()
1211 exc = None
1212 for fd in fds:
1213 # If a double close occurred, some of those fds will
1214 # already have been closed by mistake, and os.close()
1215 # here will raise.
1216 try:
1217 os.close(fd)
1218 except OSError as e:
1219 exc = e
1220 if exc is not None:
1221 raise exc
1222
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001223 def test_threadsafe_wait(self):
1224 """Issue21291: Popen.wait() needs to be threadsafe for returncode."""
1225 proc = subprocess.Popen([sys.executable, '-c',
1226 'import time; time.sleep(12)'])
1227 self.assertEqual(proc.returncode, None)
1228 results = []
1229
1230 def kill_proc_timer_thread():
1231 results.append(('thread-start-poll-result', proc.poll()))
1232 # terminate it from the thread and wait for the result.
1233 proc.kill()
1234 proc.wait()
1235 results.append(('thread-after-kill-and-wait', proc.returncode))
1236 # this wait should be a no-op given the above.
1237 proc.wait()
1238 results.append(('thread-after-second-wait', proc.returncode))
1239
1240 # This is a timing sensitive test, the failure mode is
1241 # triggered when both the main thread and this thread are in
1242 # the wait() call at once. The delay here is to allow the
1243 # main thread to most likely be blocked in its wait() call.
1244 t = threading.Timer(0.2, kill_proc_timer_thread)
1245 t.start()
1246
Victor Stinner937ee9e2018-06-26 02:11:06 +02001247 if mswindows:
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001248 expected_errorcode = 1
1249 else:
1250 # Should be -9 because of the proc.kill() from the thread.
1251 expected_errorcode = -9
1252
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001253 # Wait for the process to finish; the thread should kill it
1254 # long before it finishes on its own. Supplying a timeout
1255 # triggers a different code path for better coverage.
1256 proc.wait(timeout=20)
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001257 self.assertEqual(proc.returncode, expected_errorcode,
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001258 msg="unexpected result in wait from main thread")
1259
1260 # This should be a no-op with no change in returncode.
1261 proc.wait()
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001262 self.assertEqual(proc.returncode, expected_errorcode,
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001263 msg="unexpected result in second main wait.")
1264
1265 t.join()
1266 # Ensure that all of the thread results are as expected.
1267 # When a race condition occurs in wait(), the returncode could
1268 # be set by the wrong thread that doesn't actually have it
1269 # leading to an incorrect value.
1270 self.assertEqual([('thread-start-poll-result', None),
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001271 ('thread-after-kill-and-wait', expected_errorcode),
1272 ('thread-after-second-wait', expected_errorcode)],
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001273 results)
1274
Victor Stinnerb3693582010-05-21 20:13:12 +00001275 def test_issue8780(self):
1276 # Ensure that stdout is inherited from the parent
1277 # if stdout=PIPE is not used
1278 code = ';'.join((
1279 'import subprocess, sys',
1280 'retcode = subprocess.call('
1281 "[sys.executable, '-c', 'print(\"Hello World!\")'])",
1282 'assert retcode == 0'))
1283 output = subprocess.check_output([sys.executable, '-c', code])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001284 self.assertTrue(output.startswith(b'Hello World!'), ascii(output))
Victor Stinnerb3693582010-05-21 20:13:12 +00001285
Tim Goldenaf5ac392010-08-06 13:03:56 +00001286 def test_handles_closed_on_exception(self):
1287 # If CreateProcess exits with an error, ensure the
1288 # duplicate output handles are released
Berker Peksag16a1f282015-09-28 13:33:14 +03001289 ifhandle, ifname = tempfile.mkstemp()
1290 ofhandle, ofname = tempfile.mkstemp()
1291 efhandle, efname = tempfile.mkstemp()
Tim Goldenaf5ac392010-08-06 13:03:56 +00001292 try:
1293 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
1294 stderr=efhandle)
1295 except OSError:
1296 os.close(ifhandle)
1297 os.remove(ifname)
1298 os.close(ofhandle)
1299 os.remove(ofname)
1300 os.close(efhandle)
1301 os.remove(efname)
1302 self.assertFalse(os.path.exists(ifname))
1303 self.assertFalse(os.path.exists(ofname))
1304 self.assertFalse(os.path.exists(efname))
1305
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001306 def test_communicate_epipe(self):
1307 # Issue 10963: communicate() should hide EPIPE
1308 p = subprocess.Popen([sys.executable, "-c", 'pass'],
1309 stdin=subprocess.PIPE,
1310 stdout=subprocess.PIPE,
1311 stderr=subprocess.PIPE)
1312 self.addCleanup(p.stdout.close)
1313 self.addCleanup(p.stderr.close)
1314 self.addCleanup(p.stdin.close)
1315 p.communicate(b"x" * 2**20)
1316
1317 def test_communicate_epipe_only_stdin(self):
1318 # Issue 10963: communicate() should hide EPIPE
1319 p = subprocess.Popen([sys.executable, "-c", 'pass'],
1320 stdin=subprocess.PIPE)
1321 self.addCleanup(p.stdin.close)
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001322 p.wait()
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001323 p.communicate(b"x" * 2**20)
1324
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001325 @unittest.skipUnless(hasattr(signal, 'SIGUSR1'),
1326 "Requires signal.SIGUSR1")
1327 @unittest.skipUnless(hasattr(os, 'kill'),
1328 "Requires os.kill")
1329 @unittest.skipUnless(hasattr(os, 'getppid'),
1330 "Requires os.getppid")
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001331 def test_communicate_eintr(self):
1332 # Issue #12493: communicate() should handle EINTR
1333 def handler(signum, frame):
1334 pass
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001335 old_handler = signal.signal(signal.SIGUSR1, handler)
1336 self.addCleanup(signal.signal, signal.SIGUSR1, old_handler)
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001337
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001338 args = [sys.executable, "-c",
1339 'import os, signal;'
1340 'os.kill(os.getppid(), signal.SIGUSR1)']
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001341 for stream in ('stdout', 'stderr'):
1342 kw = {stream: subprocess.PIPE}
1343 with subprocess.Popen(args, **kw) as process:
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001344 # communicate() will be interrupted by SIGUSR1
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001345 process.communicate()
1346
Tim Peterse718f612004-10-12 21:51:32 +00001347
Gregory P. Smith3d8e7762012-11-10 22:32:22 -08001348 # This test is Linux-ish specific for simplicity to at least have
1349 # some coverage. It is not a platform specific bug.
1350 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
1351 "Linux specific")
1352 def test_failed_child_execute_fd_leak(self):
1353 """Test for the fork() failure fd leak reported in issue16327."""
1354 fd_directory = '/proc/%d/fd' % os.getpid()
1355 fds_before_popen = os.listdir(fd_directory)
1356 with self.assertRaises(PopenTestException):
1357 PopenExecuteChildRaises(
1358 [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE,
1359 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1360
1361 # NOTE: This test doesn't verify that the real _execute_child
1362 # does not close the file descriptors itself on the way out
1363 # during an exception. Code inspection has confirmed that.
1364
1365 fds_after_exception = os.listdir(fd_directory)
1366 self.assertEqual(fds_before_popen, fds_after_exception)
1367
Victor Stinner937ee9e2018-06-26 02:11:06 +02001368 @unittest.skipIf(mswindows, "behavior currently not supported on Windows")
Gregory P. Smith8621bb52017-08-24 14:58:25 -07001369 def test_file_not_found_includes_filename(self):
1370 with self.assertRaises(FileNotFoundError) as c:
1371 subprocess.call(['/opt/nonexistent_binary', 'with', 'some', 'args'])
1372 self.assertEqual(c.exception.filename, '/opt/nonexistent_binary')
1373
Victor Stinner937ee9e2018-06-26 02:11:06 +02001374 @unittest.skipIf(mswindows, "behavior currently not supported on Windows")
Gregory P. Smith8621bb52017-08-24 14:58:25 -07001375 def test_file_not_found_with_bad_cwd(self):
1376 with self.assertRaises(FileNotFoundError) as c:
1377 subprocess.Popen(['exit', '0'], cwd='/some/nonexistent/directory')
1378 self.assertEqual(c.exception.filename, '/some/nonexistent/directory')
1379
Gregory P. Smith6e730002015-04-14 16:14:25 -07001380
1381class RunFuncTestCase(BaseTestCase):
1382 def run_python(self, code, **kwargs):
1383 """Run Python code in a subprocess using subprocess.run"""
1384 argv = [sys.executable, "-c", code]
1385 return subprocess.run(argv, **kwargs)
1386
1387 def test_returncode(self):
1388 # call() function with sequence argument
1389 cp = self.run_python("import sys; sys.exit(47)")
1390 self.assertEqual(cp.returncode, 47)
1391 with self.assertRaises(subprocess.CalledProcessError):
1392 cp.check_returncode()
1393
1394 def test_check(self):
1395 with self.assertRaises(subprocess.CalledProcessError) as c:
1396 self.run_python("import sys; sys.exit(47)", check=True)
1397 self.assertEqual(c.exception.returncode, 47)
1398
1399 def test_check_zero(self):
1400 # check_returncode shouldn't raise when returncode is zero
1401 cp = self.run_python("import sys; sys.exit(0)", check=True)
1402 self.assertEqual(cp.returncode, 0)
1403
1404 def test_timeout(self):
1405 # run() function with timeout argument; we want to test that the child
1406 # process gets killed when the timeout expires. If the child isn't
1407 # killed, this call will deadlock since subprocess.run waits for the
1408 # child.
1409 with self.assertRaises(subprocess.TimeoutExpired):
1410 self.run_python("while True: pass", timeout=0.0001)
1411
1412 def test_capture_stdout(self):
1413 # capture stdout with zero return code
1414 cp = self.run_python("print('BDFL')", stdout=subprocess.PIPE)
1415 self.assertIn(b'BDFL', cp.stdout)
1416
1417 def test_capture_stderr(self):
1418 cp = self.run_python("import sys; sys.stderr.write('BDFL')",
1419 stderr=subprocess.PIPE)
1420 self.assertIn(b'BDFL', cp.stderr)
1421
1422 def test_check_output_stdin_arg(self):
1423 # run() can be called with stdin set to a file
1424 tf = tempfile.TemporaryFile()
1425 self.addCleanup(tf.close)
1426 tf.write(b'pear')
1427 tf.seek(0)
1428 cp = self.run_python(
1429 "import sys; sys.stdout.write(sys.stdin.read().upper())",
1430 stdin=tf, stdout=subprocess.PIPE)
1431 self.assertIn(b'PEAR', cp.stdout)
1432
1433 def test_check_output_input_arg(self):
1434 # check_output() can be called with input set to a string
1435 cp = self.run_python(
1436 "import sys; sys.stdout.write(sys.stdin.read().upper())",
1437 input=b'pear', stdout=subprocess.PIPE)
1438 self.assertIn(b'PEAR', cp.stdout)
1439
1440 def test_check_output_stdin_with_input_arg(self):
1441 # run() refuses to accept 'stdin' with 'input'
1442 tf = tempfile.TemporaryFile()
1443 self.addCleanup(tf.close)
1444 tf.write(b'pear')
1445 tf.seek(0)
1446 with self.assertRaises(ValueError,
1447 msg="Expected ValueError when stdin and input args supplied.") as c:
1448 output = self.run_python("print('will not be run')",
1449 stdin=tf, input=b'hare')
1450 self.assertIn('stdin', c.exception.args[0])
1451 self.assertIn('input', c.exception.args[0])
1452
1453 def test_check_output_timeout(self):
1454 with self.assertRaises(subprocess.TimeoutExpired) as c:
1455 cp = self.run_python((
1456 "import sys, time\n"
1457 "sys.stdout.write('BDFL')\n"
1458 "sys.stdout.flush()\n"
1459 "time.sleep(3600)"),
1460 # Some heavily loaded buildbots (sparc Debian 3.x) require
1461 # this much time to start and print.
1462 timeout=3, stdout=subprocess.PIPE)
1463 self.assertEqual(c.exception.output, b'BDFL')
1464 # output is aliased to stdout
1465 self.assertEqual(c.exception.stdout, b'BDFL')
1466
1467 def test_run_kwargs(self):
1468 newenv = os.environ.copy()
1469 newenv["FRUIT"] = "banana"
1470 cp = self.run_python(('import sys, os;'
1471 'sys.exit(33 if os.getenv("FRUIT")=="banana" else 31)'),
1472 env=newenv)
1473 self.assertEqual(cp.returncode, 33)
1474
Bo Baylesce0f33d2018-01-30 00:40:39 -06001475 def test_capture_output(self):
1476 cp = self.run_python(("import sys;"
1477 "sys.stdout.write('BDFL'); "
1478 "sys.stderr.write('FLUFL')"),
1479 capture_output=True)
1480 self.assertIn(b'BDFL', cp.stdout)
1481 self.assertIn(b'FLUFL', cp.stderr)
1482
1483 def test_stdout_with_capture_output_arg(self):
1484 # run() refuses to accept 'stdout' with 'capture_output'
1485 tf = tempfile.TemporaryFile()
1486 self.addCleanup(tf.close)
1487 with self.assertRaises(ValueError,
1488 msg=("Expected ValueError when stdout and capture_output "
1489 "args supplied.")) as c:
1490 output = self.run_python("print('will not be run')",
1491 capture_output=True, stdout=tf)
1492 self.assertIn('stdout', c.exception.args[0])
1493 self.assertIn('capture_output', c.exception.args[0])
1494
1495 def test_stderr_with_capture_output_arg(self):
1496 # run() refuses to accept 'stderr' with 'capture_output'
1497 tf = tempfile.TemporaryFile()
1498 self.addCleanup(tf.close)
1499 with self.assertRaises(ValueError,
1500 msg=("Expected ValueError when stderr and capture_output "
1501 "args supplied.")) as c:
1502 output = self.run_python("print('will not be run')",
1503 capture_output=True, stderr=tf)
1504 self.assertIn('stderr', c.exception.args[0])
1505 self.assertIn('capture_output', c.exception.args[0])
1506
Gregory P. Smith6e730002015-04-14 16:14:25 -07001507
Victor Stinner937ee9e2018-06-26 02:11:06 +02001508@unittest.skipIf(mswindows, "POSIX specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +00001509class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001510
Gregory P. Smith5591b022012-10-10 03:34:47 -07001511 def setUp(self):
1512 super().setUp()
1513 self._nonexistent_dir = "/_this/pa.th/does/not/exist"
1514
1515 def _get_chdir_exception(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001516 try:
Gregory P. Smith5591b022012-10-10 03:34:47 -07001517 os.chdir(self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001518 except OSError as e:
1519 # This avoids hard coding the errno value or the OS perror()
1520 # string and instead capture the exception that we want to see
1521 # below for comparison.
1522 desired_exception = e
Gregory P. Smith5591b022012-10-10 03:34:47 -07001523 desired_exception.strerror += ': ' + repr(self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001524 else:
Martin Pantereb995702016-07-28 01:11:04 +00001525 self.fail("chdir to nonexistent directory %s succeeded." %
Gregory P. Smith5591b022012-10-10 03:34:47 -07001526 self._nonexistent_dir)
1527 return desired_exception
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001528
Gregory P. Smith5591b022012-10-10 03:34:47 -07001529 def test_exception_cwd(self):
1530 """Test error in the child raised in the parent for a bad cwd."""
1531 desired_exception = self._get_chdir_exception()
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001532 try:
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001533 p = subprocess.Popen([sys.executable, "-c", ""],
Gregory P. Smith5591b022012-10-10 03:34:47 -07001534 cwd=self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001535 except OSError as e:
1536 # Test that the child process chdir failure actually makes
1537 # it up to the parent process as the correct exception.
1538 self.assertEqual(desired_exception.errno, e.errno)
1539 self.assertEqual(desired_exception.strerror, e.strerror)
1540 else:
1541 self.fail("Expected OSError: %s" % desired_exception)
1542
Gregory P. Smith5591b022012-10-10 03:34:47 -07001543 def test_exception_bad_executable(self):
1544 """Test error in the child raised in the parent for a bad executable."""
1545 desired_exception = self._get_chdir_exception()
1546 try:
1547 p = subprocess.Popen([sys.executable, "-c", ""],
1548 executable=self._nonexistent_dir)
1549 except OSError as e:
1550 # Test that the child process exec failure actually makes
1551 # it up to the parent process as the correct exception.
1552 self.assertEqual(desired_exception.errno, e.errno)
1553 self.assertEqual(desired_exception.strerror, e.strerror)
1554 else:
1555 self.fail("Expected OSError: %s" % desired_exception)
1556
1557 def test_exception_bad_args_0(self):
1558 """Test error in the child raised in the parent for a bad args[0]."""
1559 desired_exception = self._get_chdir_exception()
1560 try:
1561 p = subprocess.Popen([self._nonexistent_dir, "-c", ""])
1562 except OSError as e:
1563 # Test that the child process exec failure actually makes
1564 # it up to the parent process as the correct exception.
1565 self.assertEqual(desired_exception.errno, e.errno)
1566 self.assertEqual(desired_exception.strerror, e.strerror)
1567 else:
1568 self.fail("Expected OSError: %s" % desired_exception)
1569
Ammar Askar3fc499b2017-09-06 02:41:30 -04001570 # We mock the __del__ method for Popen in the next two tests
1571 # because it does cleanup based on the pid returned by fork_exec
1572 # along with issuing a resource warning if it still exists. Since
1573 # we don't actually spawn a process in these tests we can forego
1574 # the destructor. An alternative would be to set _child_created to
1575 # False before the destructor is called but there is no easy way
1576 # to do that
1577 class PopenNoDestructor(subprocess.Popen):
1578 def __del__(self):
1579 pass
1580
1581 @mock.patch("subprocess._posixsubprocess.fork_exec")
1582 def test_exception_errpipe_normal(self, fork_exec):
1583 """Test error passing done through errpipe_write in the good case"""
1584 def proper_error(*args):
1585 errpipe_write = args[13]
1586 # Write the hex for the error code EISDIR: 'is a directory'
1587 err_code = '{:x}'.format(errno.EISDIR).encode()
1588 os.write(errpipe_write, b"OSError:" + err_code + b":")
1589 return 0
1590
1591 fork_exec.side_effect = proper_error
1592
Victor Stinner11045c92017-10-05 06:32:53 -07001593 with mock.patch("subprocess.os.waitpid",
1594 side_effect=ChildProcessError):
1595 with self.assertRaises(IsADirectoryError):
1596 self.PopenNoDestructor(["non_existent_command"])
Ammar Askar3fc499b2017-09-06 02:41:30 -04001597
1598 @mock.patch("subprocess._posixsubprocess.fork_exec")
1599 def test_exception_errpipe_bad_data(self, fork_exec):
1600 """Test error passing done through errpipe_write where its not
1601 in the expected format"""
1602 error_data = b"\xFF\x00\xDE\xAD"
1603 def bad_error(*args):
1604 errpipe_write = args[13]
1605 # Anything can be in the pipe, no assumptions should
1606 # be made about its encoding, so we'll write some
1607 # arbitrary hex bytes to test it out
1608 os.write(errpipe_write, error_data)
1609 return 0
1610
1611 fork_exec.side_effect = bad_error
1612
Victor Stinner11045c92017-10-05 06:32:53 -07001613 with mock.patch("subprocess.os.waitpid",
1614 side_effect=ChildProcessError):
1615 with self.assertRaises(subprocess.SubprocessError) as e:
1616 self.PopenNoDestructor(["non_existent_command"])
Ammar Askar3fc499b2017-09-06 02:41:30 -04001617
1618 self.assertIn(repr(error_data), str(e.exception))
1619
Gregory P. Smith5f3d04f2018-06-05 12:00:57 -07001620 @unittest.skipIf(not os.path.exists('/proc/self/status'),
1621 "need /proc/self/status")
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001622 def test_restore_signals(self):
Gregory P. Smith5f3d04f2018-06-05 12:00:57 -07001623 # Blindly assume that cat exists on systems with /proc/self/status...
1624 default_proc_status = subprocess.check_output(
1625 ['cat', '/proc/self/status'],
1626 restore_signals=False)
1627 for line in default_proc_status.splitlines():
1628 if line.startswith(b'SigIgn'):
1629 default_sig_ign_mask = line
1630 break
1631 else:
1632 self.skipTest("SigIgn not found in /proc/self/status.")
1633 restored_proc_status = subprocess.check_output(
1634 ['cat', '/proc/self/status'],
1635 restore_signals=True)
1636 for line in restored_proc_status.splitlines():
1637 if line.startswith(b'SigIgn'):
1638 restored_sig_ign_mask = line
1639 break
1640 self.assertNotEqual(default_sig_ign_mask, restored_sig_ign_mask,
1641 msg="restore_signals=True should've unblocked "
1642 "SIGPIPE and friends.")
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001643
1644 def test_start_new_session(self):
1645 # For code coverage of calling setsid(). We don't care if we get an
1646 # EPERM error from it depending on the test execution environment, that
1647 # still indicates that it was called.
1648 try:
1649 output = subprocess.check_output(
1650 [sys.executable, "-c",
1651 "import os; print(os.getpgid(os.getpid()))"],
1652 start_new_session=True)
1653 except OSError as e:
1654 if e.errno != errno.EPERM:
1655 raise
1656 else:
1657 parent_pgid = os.getpgid(os.getpid())
1658 child_pgid = int(output)
1659 self.assertNotEqual(parent_pgid, child_pgid)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001660
1661 def test_run_abort(self):
1662 # returncode handles signal termination
Antoine Pitrou77e904e2013-10-08 23:04:32 +02001663 with support.SuppressCrashReport():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001664 p = subprocess.Popen([sys.executable, "-c",
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001665 'import os; os.abort()'])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001666 p.wait()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001667 self.assertEqual(-p.returncode, signal.SIGABRT)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001668
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)d6da7602016-06-03 06:14:06 +00001669 def test_CalledProcessError_str_signal(self):
1670 err = subprocess.CalledProcessError(-int(signal.SIGABRT), "fake cmd")
1671 error_string = str(err)
1672 # We're relying on the repr() of the signal.Signals intenum to provide
1673 # the word signal, the signal name and the numeric value.
1674 self.assertIn("signal", error_string.lower())
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)b4149062016-06-03 06:19:35 +00001675 # We're not being specific about the signal name as some signals have
1676 # multiple names and which name is revealed can vary.
1677 self.assertIn("SIG", error_string)
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)d6da7602016-06-03 06:14:06 +00001678 self.assertIn(str(signal.SIGABRT), error_string)
1679
1680 def test_CalledProcessError_str_unknown_signal(self):
1681 err = subprocess.CalledProcessError(-9876543, "fake cmd")
1682 error_string = str(err)
1683 self.assertIn("unknown signal 9876543.", error_string)
1684
1685 def test_CalledProcessError_str_non_zero(self):
1686 err = subprocess.CalledProcessError(2, "fake cmd")
1687 error_string = str(err)
1688 self.assertIn("non-zero exit status 2.", error_string)
1689
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001690 def test_preexec(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001691 # DISCLAIMER: Setting environment variables is *not* a good use
1692 # of a preexec_fn. This is merely a test.
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001693 p = subprocess.Popen([sys.executable, "-c",
1694 'import sys,os;'
1695 'sys.stdout.write(os.getenv("FRUIT"))'],
1696 stdout=subprocess.PIPE,
1697 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Victor Stinner7438c612016-05-20 12:43:15 +02001698 with p:
1699 self.assertEqual(p.stdout.read(), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001700
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001701 def test_preexec_exception(self):
1702 def raise_it():
1703 raise ValueError("What if two swallows carried a coconut?")
1704 try:
1705 p = subprocess.Popen([sys.executable, "-c", ""],
1706 preexec_fn=raise_it)
Gregory P. Smith8d07c262012-11-10 23:53:47 -08001707 except subprocess.SubprocessError as e:
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001708 self.assertTrue(
1709 subprocess._posixsubprocess,
1710 "Expected a ValueError from the preexec_fn")
1711 except ValueError as e:
1712 self.assertIn("coconut", e.args[0])
1713 else:
1714 self.fail("Exception raised by preexec_fn did not make it "
1715 "to the parent process.")
1716
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001717 class _TestExecuteChildPopen(subprocess.Popen):
1718 """Used to test behavior at the end of _execute_child."""
1719 def __init__(self, testcase, *args, **kwargs):
1720 self._testcase = testcase
1721 subprocess.Popen.__init__(self, *args, **kwargs)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001722
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001723 def _execute_child(self, *args, **kwargs):
Gregory P. Smith12489d92012-11-11 01:37:02 -08001724 try:
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001725 subprocess.Popen._execute_child(self, *args, **kwargs)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001726 finally:
1727 # Open a bunch of file descriptors and verify that
1728 # none of them are the same as the ones the Popen
1729 # instance is using for stdin/stdout/stderr.
1730 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
1731 for _ in range(8)]
1732 try:
1733 for fd in devzero_fds:
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001734 self._testcase.assertNotIn(
1735 fd, (self.stdin.fileno(), self.stdout.fileno(),
1736 self.stderr.fileno()),
Gregory P. Smith12489d92012-11-11 01:37:02 -08001737 msg="At least one fd was closed early.")
1738 finally:
Richard Oudkerk0e547b62013-06-10 16:29:19 +01001739 for fd in devzero_fds:
1740 os.close(fd)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001741
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001742 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
1743 def test_preexec_errpipe_does_not_double_close_pipes(self):
1744 """Issue16140: Don't double close pipes on preexec error."""
1745
1746 def raise_it():
Gregory P. Smith65ee6ec2012-11-11 10:12:40 -08001747 raise subprocess.SubprocessError(
1748 "force the _execute_child() errpipe_data path.")
Gregory P. Smith12489d92012-11-11 01:37:02 -08001749
Gregory P. Smithc2c4cb62012-11-11 01:41:49 -08001750 with self.assertRaises(subprocess.SubprocessError):
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001751 self._TestExecuteChildPopen(
1752 self, [sys.executable, "-c", "pass"],
Gregory P. Smith12489d92012-11-11 01:37:02 -08001753 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1754 stderr=subprocess.PIPE, preexec_fn=raise_it)
1755
Gregory P. Smith32ec9da2010-03-19 16:53:08 +00001756 def test_preexec_gc_module_failure(self):
1757 # This tests the code that disables garbage collection if the child
1758 # process will execute any Python.
1759 def raise_runtime_error():
1760 raise RuntimeError("this shouldn't escape")
1761 enabled = gc.isenabled()
1762 orig_gc_disable = gc.disable
1763 orig_gc_isenabled = gc.isenabled
1764 try:
1765 gc.disable()
1766 self.assertFalse(gc.isenabled())
1767 subprocess.call([sys.executable, '-c', ''],
1768 preexec_fn=lambda: None)
1769 self.assertFalse(gc.isenabled(),
1770 "Popen enabled gc when it shouldn't.")
1771
1772 gc.enable()
1773 self.assertTrue(gc.isenabled())
1774 subprocess.call([sys.executable, '-c', ''],
1775 preexec_fn=lambda: None)
1776 self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
1777
1778 gc.disable = raise_runtime_error
1779 self.assertRaises(RuntimeError, subprocess.Popen,
1780 [sys.executable, '-c', ''],
1781 preexec_fn=lambda: None)
1782
1783 del gc.isenabled # force an AttributeError
1784 self.assertRaises(AttributeError, subprocess.Popen,
1785 [sys.executable, '-c', ''],
1786 preexec_fn=lambda: None)
1787 finally:
1788 gc.disable = orig_gc_disable
1789 gc.isenabled = orig_gc_isenabled
1790 if not enabled:
1791 gc.disable()
1792
Martin Panterf7fdbda2015-12-05 09:51:52 +00001793 @unittest.skipIf(
1794 sys.platform == 'darwin', 'setrlimit() seems to fail on OS X')
Martin Panterafdd5132015-11-30 02:21:41 +00001795 def test_preexec_fork_failure(self):
1796 # The internal code did not preserve the previous exception when
1797 # re-enabling garbage collection
1798 try:
1799 from resource import getrlimit, setrlimit, RLIMIT_NPROC
1800 except ImportError as err:
1801 self.skipTest(err) # RLIMIT_NPROC is specific to Linux and BSD
1802 limits = getrlimit(RLIMIT_NPROC)
1803 [_, hard] = limits
1804 setrlimit(RLIMIT_NPROC, (0, hard))
1805 self.addCleanup(setrlimit, RLIMIT_NPROC, limits)
Martin Panter5cf791b2015-12-11 05:40:14 +00001806 try:
Martin Panterafdd5132015-11-30 02:21:41 +00001807 subprocess.call([sys.executable, '-c', ''],
1808 preexec_fn=lambda: None)
Martin Panter5cf791b2015-12-11 05:40:14 +00001809 except BlockingIOError:
1810 # Forking should raise EAGAIN, translated to BlockingIOError
1811 pass
1812 else:
1813 self.skipTest('RLIMIT_NPROC had no effect; probably superuser')
Martin Panterafdd5132015-11-30 02:21:41 +00001814
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001815 def test_args_string(self):
1816 # args is a string
Berker Peksag16a1f282015-09-28 13:33:14 +03001817 fd, fname = tempfile.mkstemp()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001818 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +00001819 with open(fd, "w", errors="surrogateescape") as fobj:
Xavier de Gayed1415312016-07-22 12:15:29 +02001820 fobj.write("#!%s\n" % support.unix_shell)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001821 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
1822 sys.executable)
1823 os.chmod(fname, 0o700)
1824 p = subprocess.Popen(fname)
1825 p.wait()
1826 os.remove(fname)
1827 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001828
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001829 def test_invalid_args(self):
1830 # invalid arguments should raise ValueError
1831 self.assertRaises(ValueError, subprocess.call,
1832 [sys.executable, "-c",
1833 "import sys; sys.exit(47)"],
1834 startupinfo=47)
1835 self.assertRaises(ValueError, subprocess.call,
1836 [sys.executable, "-c",
1837 "import sys; sys.exit(47)"],
1838 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001839
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001840 def test_shell_sequence(self):
1841 # Run command through the shell (sequence)
1842 newenv = os.environ.copy()
1843 newenv["FRUIT"] = "apple"
1844 p = subprocess.Popen(["echo $FRUIT"], shell=1,
1845 stdout=subprocess.PIPE,
1846 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02001847 with p:
1848 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001849
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001850 def test_shell_string(self):
1851 # Run command through the shell (string)
1852 newenv = os.environ.copy()
1853 newenv["FRUIT"] = "apple"
1854 p = subprocess.Popen("echo $FRUIT", shell=1,
1855 stdout=subprocess.PIPE,
1856 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02001857 with p:
1858 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Christian Heimesa342c012008-04-20 21:01:16 +00001859
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001860 def test_call_string(self):
1861 # call() function with string argument on UNIX
Berker Peksag16a1f282015-09-28 13:33:14 +03001862 fd, fname = tempfile.mkstemp()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001863 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +00001864 with open(fd, "w", errors="surrogateescape") as fobj:
Xavier de Gayed1415312016-07-22 12:15:29 +02001865 fobj.write("#!%s\n" % support.unix_shell)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001866 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
1867 sys.executable)
1868 os.chmod(fname, 0o700)
1869 rc = subprocess.call(fname)
1870 os.remove(fname)
1871 self.assertEqual(rc, 47)
Christian Heimesa342c012008-04-20 21:01:16 +00001872
Stefan Krah9542cc62010-07-19 14:20:53 +00001873 def test_specific_shell(self):
1874 # Issue #9265: Incorrect name passed as arg[0].
1875 shells = []
1876 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
1877 for name in ['bash', 'ksh']:
1878 sh = os.path.join(prefix, name)
1879 if os.path.isfile(sh):
1880 shells.append(sh)
1881 if not shells: # Will probably work for any shell but csh.
1882 self.skipTest("bash or ksh required for this test")
1883 sh = '/bin/sh'
1884 if os.path.isfile(sh) and not os.path.islink(sh):
1885 # Test will fail if /bin/sh is a symlink to csh.
1886 shells.append(sh)
1887 for sh in shells:
1888 p = subprocess.Popen("echo $0", executable=sh, shell=True,
1889 stdout=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +02001890 with p:
1891 self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
Stefan Krah9542cc62010-07-19 14:20:53 +00001892
Florent Xicluna4886d242010-03-08 13:27:26 +00001893 def _kill_process(self, method, *args):
Florent Xicluna1d8ee3a2010-03-05 20:26:54 +00001894 # Do not inherit file handles from the parent.
1895 # It should fix failures on some platforms.
Gregory P. Smithdee04342013-08-29 13:35:27 -07001896 # Also set the SIGINT handler to the default to make sure it's not
1897 # being ignored (some tests rely on that.)
1898 old_handler = signal.signal(signal.SIGINT, signal.default_int_handler)
1899 try:
1900 p = subprocess.Popen([sys.executable, "-c", """if 1:
1901 import sys, time
1902 sys.stdout.write('x\\n')
1903 sys.stdout.flush()
1904 time.sleep(30)
1905 """],
1906 close_fds=True,
1907 stdin=subprocess.PIPE,
1908 stdout=subprocess.PIPE,
1909 stderr=subprocess.PIPE)
1910 finally:
1911 signal.signal(signal.SIGINT, old_handler)
Antoine Pitrou3d8580f2010-09-20 01:33:21 +00001912 # Wait for the interpreter to be completely initialized before
1913 # sending any signal.
1914 p.stdout.read(1)
1915 getattr(p, method)(*args)
Florent Xicluna4886d242010-03-08 13:27:26 +00001916 return p
1917
Charles-François Natali53221e32013-01-12 16:52:20 +01001918 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
1919 "Due to known OS bug (issue #16762)")
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001920 def _kill_dead_process(self, method, *args):
1921 # Do not inherit file handles from the parent.
1922 # It should fix failures on some platforms.
1923 p = subprocess.Popen([sys.executable, "-c", """if 1:
1924 import sys, time
1925 sys.stdout.write('x\\n')
1926 sys.stdout.flush()
1927 """],
1928 close_fds=True,
1929 stdin=subprocess.PIPE,
1930 stdout=subprocess.PIPE,
1931 stderr=subprocess.PIPE)
1932 # Wait for the interpreter to be completely initialized before
1933 # sending any signal.
1934 p.stdout.read(1)
1935 # The process should end after this
1936 time.sleep(1)
1937 # This shouldn't raise even though the child is now dead
1938 getattr(p, method)(*args)
1939 p.communicate()
1940
Florent Xicluna4886d242010-03-08 13:27:26 +00001941 def test_send_signal(self):
1942 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunac049d872010-03-27 22:47:23 +00001943 _, stderr = p.communicate()
1944 self.assertIn(b'KeyboardInterrupt', stderr)
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001945 self.assertNotEqual(p.wait(), 0)
Christian Heimesa342c012008-04-20 21:01:16 +00001946
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001947 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001948 p = self._kill_process('kill')
Florent Xiclunac049d872010-03-27 22:47:23 +00001949 _, stderr = p.communicate()
1950 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001951 self.assertEqual(p.wait(), -signal.SIGKILL)
Tim Peterse718f612004-10-12 21:51:32 +00001952
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001953 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001954 p = self._kill_process('terminate')
Florent Xiclunac049d872010-03-27 22:47:23 +00001955 _, stderr = p.communicate()
1956 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001957 self.assertEqual(p.wait(), -signal.SIGTERM)
1958
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001959 def test_send_signal_dead(self):
1960 # Sending a signal to a dead process
1961 self._kill_dead_process('send_signal', signal.SIGINT)
1962
1963 def test_kill_dead(self):
1964 # Killing a dead process
1965 self._kill_dead_process('kill')
1966
1967 def test_terminate_dead(self):
1968 # Terminating a dead process
1969 self._kill_dead_process('terminate')
1970
Victor Stinnerdaf45552013-08-28 00:53:59 +02001971 def _save_fds(self, save_fds):
1972 fds = []
1973 for fd in save_fds:
1974 inheritable = os.get_inheritable(fd)
1975 saved = os.dup(fd)
1976 fds.append((fd, saved, inheritable))
1977 return fds
1978
1979 def _restore_fds(self, fds):
1980 for fd, saved, inheritable in fds:
1981 os.dup2(saved, fd, inheritable=inheritable)
1982 os.close(saved)
1983
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001984 def check_close_std_fds(self, fds):
1985 # Issue #9905: test that subprocess pipes still work properly with
1986 # some standard fds closed
1987 stdin = 0
Victor Stinnerdaf45552013-08-28 00:53:59 +02001988 saved_fds = self._save_fds(fds)
1989 for fd, saved, inheritable in saved_fds:
1990 if fd == 0:
1991 stdin = saved
1992 break
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001993 try:
1994 for fd in fds:
1995 os.close(fd)
1996 out, err = subprocess.Popen([sys.executable, "-c",
1997 'import sys;'
1998 'sys.stdout.write("apple");'
1999 'sys.stdout.flush();'
2000 'sys.stderr.write("orange")'],
2001 stdin=stdin,
2002 stdout=subprocess.PIPE,
2003 stderr=subprocess.PIPE).communicate()
2004 err = support.strip_python_stderr(err)
2005 self.assertEqual((out, err), (b'apple', b'orange'))
2006 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02002007 self._restore_fds(saved_fds)
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00002008
2009 def test_close_fd_0(self):
2010 self.check_close_std_fds([0])
2011
2012 def test_close_fd_1(self):
2013 self.check_close_std_fds([1])
2014
2015 def test_close_fd_2(self):
2016 self.check_close_std_fds([2])
2017
2018 def test_close_fds_0_1(self):
2019 self.check_close_std_fds([0, 1])
2020
2021 def test_close_fds_0_2(self):
2022 self.check_close_std_fds([0, 2])
2023
2024 def test_close_fds_1_2(self):
2025 self.check_close_std_fds([1, 2])
2026
2027 def test_close_fds_0_1_2(self):
2028 # Issue #10806: test that subprocess pipes still work properly with
2029 # all standard fds closed.
2030 self.check_close_std_fds([0, 1, 2])
2031
Gregory P. Smith53dd8162013-12-01 16:03:24 -08002032 def test_small_errpipe_write_fd(self):
2033 """Issue #15798: Popen should work when stdio fds are available."""
2034 new_stdin = os.dup(0)
2035 new_stdout = os.dup(1)
2036 try:
2037 os.close(0)
2038 os.close(1)
2039
2040 # Side test: if errpipe_write fails to have its CLOEXEC
2041 # flag set this should cause the parent to think the exec
2042 # failed. Extremely unlikely: everyone supports CLOEXEC.
2043 subprocess.Popen([
2044 sys.executable, "-c",
2045 "print('AssertionError:0:CLOEXEC failure.')"]).wait()
2046 finally:
2047 # Restore original stdin and stdout
2048 os.dup2(new_stdin, 0)
2049 os.dup2(new_stdout, 1)
2050 os.close(new_stdin)
2051 os.close(new_stdout)
2052
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002053 def test_remapping_std_fds(self):
2054 # open up some temporary files
Berker Peksag16a1f282015-09-28 13:33:14 +03002055 temps = [tempfile.mkstemp() for i in range(3)]
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002056 try:
2057 temp_fds = [fd for fd, fname in temps]
2058
2059 # unlink the files -- we won't need to reopen them
2060 for fd, fname in temps:
2061 os.unlink(fname)
2062
2063 # write some data to what will become stdin, and rewind
2064 os.write(temp_fds[1], b"STDIN")
2065 os.lseek(temp_fds[1], 0, 0)
2066
2067 # move the standard file descriptors out of the way
Victor Stinnerdaf45552013-08-28 00:53:59 +02002068 saved_fds = self._save_fds(range(3))
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002069 try:
2070 # duplicate the file objects over the standard fd's
2071 for fd, temp_fd in enumerate(temp_fds):
2072 os.dup2(temp_fd, fd)
2073
2074 # now use those files in the "wrong" order, so that subprocess
2075 # has to rearrange them in the child
2076 p = subprocess.Popen([sys.executable, "-c",
2077 'import sys; got = sys.stdin.read();'
2078 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
2079 stdin=temp_fds[1],
2080 stdout=temp_fds[2],
2081 stderr=temp_fds[0])
2082 p.wait()
2083 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02002084 self._restore_fds(saved_fds)
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002085
2086 for fd in temp_fds:
2087 os.lseek(fd, 0, 0)
2088
2089 out = os.read(temp_fds[2], 1024)
2090 err = support.strip_python_stderr(os.read(temp_fds[0], 1024))
2091 self.assertEqual(out, b"got STDIN")
2092 self.assertEqual(err, b"err")
2093
2094 finally:
2095 for fd in temp_fds:
2096 os.close(fd)
2097
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002098 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
2099 # open up some temporary files
Berker Peksag16a1f282015-09-28 13:33:14 +03002100 temps = [tempfile.mkstemp() for i in range(3)]
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002101 temp_fds = [fd for fd, fname in temps]
2102 try:
2103 # unlink the files -- we won't need to reopen them
2104 for fd, fname in temps:
2105 os.unlink(fname)
2106
2107 # save a copy of the standard file descriptors
Victor Stinnerdaf45552013-08-28 00:53:59 +02002108 saved_fds = self._save_fds(range(3))
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002109 try:
2110 # duplicate the temp files over the standard fd's 0, 1, 2
2111 for fd, temp_fd in enumerate(temp_fds):
2112 os.dup2(temp_fd, fd)
2113
2114 # write some data to what will become stdin, and rewind
2115 os.write(stdin_no, b"STDIN")
2116 os.lseek(stdin_no, 0, 0)
2117
2118 # now use those files in the given order, so that subprocess
2119 # has to rearrange them in the child
2120 p = subprocess.Popen([sys.executable, "-c",
2121 'import sys; got = sys.stdin.read();'
2122 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
2123 stdin=stdin_no,
2124 stdout=stdout_no,
2125 stderr=stderr_no)
2126 p.wait()
2127
2128 for fd in temp_fds:
2129 os.lseek(fd, 0, 0)
2130
2131 out = os.read(stdout_no, 1024)
2132 err = support.strip_python_stderr(os.read(stderr_no, 1024))
2133 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02002134 self._restore_fds(saved_fds)
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002135
2136 self.assertEqual(out, b"got STDIN")
2137 self.assertEqual(err, b"err")
2138
2139 finally:
2140 for fd in temp_fds:
2141 os.close(fd)
2142
2143 # When duping fds, if there arises a situation where one of the fds is
2144 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
2145 # This tests all combinations of this.
2146 def test_swap_fds(self):
2147 self.check_swap_fds(0, 1, 2)
2148 self.check_swap_fds(0, 2, 1)
2149 self.check_swap_fds(1, 0, 2)
2150 self.check_swap_fds(1, 2, 0)
2151 self.check_swap_fds(2, 0, 1)
2152 self.check_swap_fds(2, 1, 0)
2153
Alexey Izbyshev0e7144b2018-03-26 22:49:35 +03002154 def _check_swap_std_fds_with_one_closed(self, from_fds, to_fds):
2155 saved_fds = self._save_fds(range(3))
2156 try:
2157 for from_fd in from_fds:
2158 with tempfile.TemporaryFile() as f:
2159 os.dup2(f.fileno(), from_fd)
2160
2161 fd_to_close = (set(range(3)) - set(from_fds)).pop()
2162 os.close(fd_to_close)
2163
2164 arg_names = ['stdin', 'stdout', 'stderr']
2165 kwargs = {}
2166 for from_fd, to_fd in zip(from_fds, to_fds):
2167 kwargs[arg_names[to_fd]] = from_fd
2168
2169 code = textwrap.dedent(r'''
2170 import os, sys
2171 skipped_fd = int(sys.argv[1])
2172 for fd in range(3):
2173 if fd != skipped_fd:
2174 os.write(fd, str(fd).encode('ascii'))
2175 ''')
2176
2177 skipped_fd = (set(range(3)) - set(to_fds)).pop()
2178
2179 rc = subprocess.call([sys.executable, '-c', code, str(skipped_fd)],
2180 **kwargs)
2181 self.assertEqual(rc, 0)
2182
2183 for from_fd, to_fd in zip(from_fds, to_fds):
2184 os.lseek(from_fd, 0, os.SEEK_SET)
2185 read_bytes = os.read(from_fd, 1024)
2186 read_fds = list(map(int, read_bytes.decode('ascii')))
2187 msg = textwrap.dedent(f"""
2188 When testing {from_fds} to {to_fds} redirection,
2189 parent descriptor {from_fd} got redirected
2190 to descriptor(s) {read_fds} instead of descriptor {to_fd}.
2191 """)
2192 self.assertEqual([to_fd], read_fds, msg)
2193 finally:
2194 self._restore_fds(saved_fds)
2195
2196 # Check that subprocess can remap std fds correctly even
2197 # if one of them is closed (#32844).
2198 def test_swap_std_fds_with_one_closed(self):
2199 for from_fds in itertools.combinations(range(3), 2):
2200 for to_fds in itertools.permutations(range(3), 2):
2201 self._check_swap_std_fds_with_one_closed(from_fds, to_fds)
2202
Victor Stinner13bb71c2010-04-23 21:41:56 +00002203 def test_surrogates_error_message(self):
Victor Stinner4d078042010-04-23 19:28:32 +00002204 def prepare():
2205 raise ValueError("surrogate:\uDCff")
2206
2207 try:
2208 subprocess.call(
2209 [sys.executable, "-c", "pass"],
2210 preexec_fn=prepare)
2211 except ValueError as err:
2212 # Pure Python implementations keeps the message
2213 self.assertIsNone(subprocess._posixsubprocess)
2214 self.assertEqual(str(err), "surrogate:\uDCff")
Gregory P. Smith8d07c262012-11-10 23:53:47 -08002215 except subprocess.SubprocessError as err:
Victor Stinner4d078042010-04-23 19:28:32 +00002216 # _posixsubprocess uses a default message
2217 self.assertIsNotNone(subprocess._posixsubprocess)
2218 self.assertEqual(str(err), "Exception occurred in preexec_fn.")
2219 else:
Gregory P. Smith8d07c262012-11-10 23:53:47 -08002220 self.fail("Expected ValueError or subprocess.SubprocessError")
Victor Stinner4d078042010-04-23 19:28:32 +00002221
Victor Stinner13bb71c2010-04-23 21:41:56 +00002222 def test_undecodable_env(self):
2223 for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')):
Victor Stinner5323fb02013-11-19 23:46:06 +01002224 encoded_value = value.encode("ascii", "surrogateescape")
2225
Victor Stinner13bb71c2010-04-23 21:41:56 +00002226 # test str with surrogates
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00002227 script = "import os; print(ascii(os.getenv(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002228 env = os.environ.copy()
2229 env[key] = value
Victor Stinner5323fb02013-11-19 23:46:06 +01002230 # Use C locale to get ASCII for the locale encoding to force
Michael Felt89d79b12018-08-26 19:29:36 +02002231 # surrogate-escaping of \xFF in the child process
Victor Stinnerebc78d22010-10-14 10:38:17 +00002232 env['LC_ALL'] = 'C'
Michael Felt89d79b12018-08-26 19:29:36 +02002233 decoded_value = value
Victor Stinner13bb71c2010-04-23 21:41:56 +00002234 stdout = subprocess.check_output(
2235 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002236 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00002237 stdout = stdout.rstrip(b'\n\r')
Victor Stinner5323fb02013-11-19 23:46:06 +01002238 self.assertEqual(stdout.decode('ascii'), ascii(decoded_value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00002239
2240 # test bytes
2241 key = key.encode("ascii", "surrogateescape")
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00002242 script = "import os; print(ascii(os.getenvb(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002243 env = os.environ.copy()
Victor Stinner5323fb02013-11-19 23:46:06 +01002244 env[key] = encoded_value
Victor Stinner13bb71c2010-04-23 21:41:56 +00002245 stdout = subprocess.check_output(
2246 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002247 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00002248 stdout = stdout.rstrip(b'\n\r')
Victor Stinner5323fb02013-11-19 23:46:06 +01002249 self.assertEqual(stdout.decode('ascii'), ascii(encoded_value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00002250
Victor Stinnerb745a742010-05-18 17:17:23 +00002251 def test_bytes_program(self):
2252 abs_program = os.fsencode(sys.executable)
2253 path, program = os.path.split(sys.executable)
2254 program = os.fsencode(program)
2255
2256 # absolute bytes path
2257 exitcode = subprocess.call([abs_program, "-c", "pass"])
Ezio Melottib3aedd42010-11-20 19:04:17 +00002258 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00002259
Victor Stinner7b3b20a2011-03-03 12:54:05 +00002260 # absolute bytes path as a string
2261 cmd = b"'" + abs_program + b"' -c pass"
2262 exitcode = subprocess.call(cmd, shell=True)
2263 self.assertEqual(exitcode, 0)
2264
Victor Stinnerb745a742010-05-18 17:17:23 +00002265 # bytes program, unicode PATH
2266 env = os.environ.copy()
2267 env["PATH"] = path
2268 exitcode = subprocess.call([program, "-c", "pass"], env=env)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002269 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00002270
2271 # bytes program, bytes PATH
2272 envb = os.environb.copy()
2273 envb[b"PATH"] = os.fsencode(path)
2274 exitcode = subprocess.call([program, "-c", "pass"], env=envb)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002275 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00002276
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002277 def test_pipe_cloexec(self):
2278 sleeper = support.findfile("input_reader.py", subdir="subprocessdata")
2279 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2280
2281 p1 = subprocess.Popen([sys.executable, sleeper],
2282 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2283 stderr=subprocess.PIPE, close_fds=False)
2284
2285 self.addCleanup(p1.communicate, b'')
2286
2287 p2 = subprocess.Popen([sys.executable, fd_status],
2288 stdout=subprocess.PIPE, close_fds=False)
2289
2290 output, error = p2.communicate()
2291 result_fds = set(map(int, output.split(b',')))
2292 unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(),
2293 p1.stderr.fileno()])
2294
2295 self.assertFalse(result_fds & unwanted_fds,
2296 "Expected no fds from %r to be open in child, "
2297 "found %r" %
2298 (unwanted_fds, result_fds & unwanted_fds))
2299
2300 def test_pipe_cloexec_real_tools(self):
2301 qcat = support.findfile("qcat.py", subdir="subprocessdata")
2302 qgrep = support.findfile("qgrep.py", subdir="subprocessdata")
2303
2304 subdata = b'zxcvbn'
2305 data = subdata * 4 + b'\n'
2306
2307 p1 = subprocess.Popen([sys.executable, qcat],
2308 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2309 close_fds=False)
2310
2311 p2 = subprocess.Popen([sys.executable, qgrep, subdata],
2312 stdin=p1.stdout, stdout=subprocess.PIPE,
2313 close_fds=False)
2314
2315 self.addCleanup(p1.wait)
2316 self.addCleanup(p2.wait)
Gregory P. Smith886455c2012-01-21 22:05:10 -08002317 def kill_p1():
2318 try:
2319 p1.terminate()
2320 except ProcessLookupError:
2321 pass
2322 def kill_p2():
2323 try:
2324 p2.terminate()
2325 except ProcessLookupError:
2326 pass
2327 self.addCleanup(kill_p1)
2328 self.addCleanup(kill_p2)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002329
2330 p1.stdin.write(data)
2331 p1.stdin.close()
2332
2333 readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10)
2334
2335 self.assertTrue(readfiles, "The child hung")
2336 self.assertEqual(p2.stdout.read(), data)
2337
Victor Stinnerfaa8c132011-01-03 16:36:00 +00002338 p1.stdout.close()
2339 p2.stdout.close()
2340
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002341 def test_close_fds(self):
2342 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2343
2344 fds = os.pipe()
2345 self.addCleanup(os.close, fds[0])
2346 self.addCleanup(os.close, fds[1])
2347
2348 open_fds = set(fds)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002349 # add a bunch more fds
2350 for _ in range(9):
Serhiy Storchaka85c30332015-02-15 13:58:23 +02002351 fd = os.open(os.devnull, os.O_RDONLY)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002352 self.addCleanup(os.close, fd)
2353 open_fds.add(fd)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002354
Victor Stinnerdaf45552013-08-28 00:53:59 +02002355 for fd in open_fds:
2356 os.set_inheritable(fd, True)
2357
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002358 p = subprocess.Popen([sys.executable, fd_status],
2359 stdout=subprocess.PIPE, close_fds=False)
2360 output, ignored = p.communicate()
2361 remaining_fds = set(map(int, output.split(b',')))
2362
2363 self.assertEqual(remaining_fds & open_fds, open_fds,
2364 "Some fds were closed")
2365
2366 p = subprocess.Popen([sys.executable, fd_status],
2367 stdout=subprocess.PIPE, close_fds=True)
2368 output, ignored = p.communicate()
2369 remaining_fds = set(map(int, output.split(b',')))
2370
2371 self.assertFalse(remaining_fds & open_fds,
2372 "Some fds were left open")
2373 self.assertIn(1, remaining_fds, "Subprocess failed")
2374
Gregory P. Smith8facece2012-01-21 14:01:08 -08002375 # Keep some of the fd's we opened open in the subprocess.
2376 # This tests _posixsubprocess.c's proper handling of fds_to_keep.
2377 fds_to_keep = set(open_fds.pop() for _ in range(8))
2378 p = subprocess.Popen([sys.executable, fd_status],
2379 stdout=subprocess.PIPE, close_fds=True,
izbyshev2d8f0632017-12-19 03:26:49 +07002380 pass_fds=fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002381 output, ignored = p.communicate()
2382 remaining_fds = set(map(int, output.split(b',')))
2383
izbyshev2d8f0632017-12-19 03:26:49 +07002384 self.assertFalse((remaining_fds - fds_to_keep) & open_fds,
Gregory P. Smith8facece2012-01-21 14:01:08 -08002385 "Some fds not in pass_fds were left open")
2386 self.assertIn(1, remaining_fds, "Subprocess failed")
2387
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002388
Gregory P. Smithd04f6992014-06-01 15:27:28 -07002389 @unittest.skipIf(sys.platform.startswith("freebsd") and
2390 os.stat("/dev").st_dev == os.stat("/dev/fd").st_dev,
2391 "Requires fdescfs mounted on /dev/fd on FreeBSD.")
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002392 def test_close_fds_when_max_fd_is_lowered(self):
2393 """Confirm that issue21618 is fixed (may fail under valgrind)."""
2394 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2395
Gregory P. Smith634aa682014-06-15 17:51:04 -07002396 # This launches the meat of the test in a child process to
2397 # avoid messing with the larger unittest processes maximum
2398 # number of file descriptors.
2399 # This process launches:
2400 # +--> Process that lowers its RLIMIT_NOFILE aftr setting up
2401 # a bunch of high open fds above the new lower rlimit.
2402 # Those are reported via stdout before launching a new
2403 # process with close_fds=False to run the actual test:
2404 # +--> The TEST: This one launches a fd_status.py
2405 # subprocess with close_fds=True so we can find out if
2406 # any of the fds above the lowered rlimit are still open.
2407 p = subprocess.Popen([sys.executable, '-c', textwrap.dedent(
2408 '''
2409 import os, resource, subprocess, sys, textwrap
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002410 open_fds = set()
2411 # Add a bunch more fds to pass down.
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002412 for _ in range(40):
Serhiy Storchaka85c30332015-02-15 13:58:23 +02002413 fd = os.open(os.devnull, os.O_RDONLY)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002414 open_fds.add(fd)
2415
2416 # Leave a two pairs of low ones available for use by the
2417 # internal child error pipe and the stdout pipe.
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002418 # We also leave 10 more open as some Python buildbots run into
2419 # "too many open files" errors during the test if we do not.
2420 for fd in sorted(open_fds)[:14]:
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002421 os.close(fd)
2422 open_fds.remove(fd)
2423
2424 for fd in open_fds:
Gregory P. Smith634aa682014-06-15 17:51:04 -07002425 #self.addCleanup(os.close, fd)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002426 os.set_inheritable(fd, True)
2427
2428 max_fd_open = max(open_fds)
2429
Gregory P. Smith634aa682014-06-15 17:51:04 -07002430 # Communicate the open_fds to the parent unittest.TestCase process.
2431 print(','.join(map(str, sorted(open_fds))))
2432 sys.stdout.flush()
2433
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002434 rlim_cur, rlim_max = resource.getrlimit(resource.RLIMIT_NOFILE)
2435 try:
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002436 # 29 is lower than the highest fds we are leaving open.
2437 resource.setrlimit(resource.RLIMIT_NOFILE, (29, rlim_max))
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002438 # Launch a new Python interpreter with our low fd rlim_cur that
2439 # inherits open fds above that limit. It then uses subprocess
2440 # with close_fds=True to get a report of open fds in the child.
2441 # An explicit list of fds to check is passed to fd_status.py as
2442 # letting fd_status rely on its default logic would miss the
2443 # fds above rlim_cur as it normally only checks up to that limit.
Gregory P. Smith634aa682014-06-15 17:51:04 -07002444 subprocess.Popen(
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002445 [sys.executable, '-c',
2446 textwrap.dedent("""
2447 import subprocess, sys
Gregory P. Smith634aa682014-06-15 17:51:04 -07002448 subprocess.Popen([sys.executable, %r] +
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002449 [str(x) for x in range({max_fd})],
Gregory P. Smithffd529c2014-06-01 13:46:54 -07002450 close_fds=True).wait()
Gregory P. Smith634aa682014-06-15 17:51:04 -07002451 """.format(max_fd=max_fd_open+1))],
2452 close_fds=False).wait()
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002453 finally:
2454 resource.setrlimit(resource.RLIMIT_NOFILE, (rlim_cur, rlim_max))
Gregory P. Smith634aa682014-06-15 17:51:04 -07002455 ''' % fd_status)], stdout=subprocess.PIPE)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002456
2457 output, unused_stderr = p.communicate()
Gregory P. Smith634aa682014-06-15 17:51:04 -07002458 output_lines = output.splitlines()
2459 self.assertEqual(len(output_lines), 2,
Gregory P. Smith9204e092014-06-15 20:16:01 -07002460 msg="expected exactly two lines of output:\n%r" % output)
Gregory P. Smith634aa682014-06-15 17:51:04 -07002461 opened_fds = set(map(int, output_lines[0].strip().split(b',')))
2462 remaining_fds = set(map(int, output_lines[1].strip().split(b',')))
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002463
Gregory P. Smith634aa682014-06-15 17:51:04 -07002464 self.assertFalse(remaining_fds & opened_fds,
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002465 msg="Some fds were left open.")
2466
2467
Victor Stinner88701e22011-06-01 13:13:04 +02002468 # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
2469 # descriptor of a pipe closed in the parent process is valid in the
2470 # child process according to fstat(), but the mode of the file
2471 # descriptor is invalid, and read or write raise an error.
2472 @support.requires_mac_ver(10, 5)
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00002473 def test_pass_fds(self):
2474 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2475
2476 open_fds = set()
2477
2478 for x in range(5):
2479 fds = os.pipe()
2480 self.addCleanup(os.close, fds[0])
2481 self.addCleanup(os.close, fds[1])
Victor Stinnerdaf45552013-08-28 00:53:59 +02002482 os.set_inheritable(fds[0], True)
2483 os.set_inheritable(fds[1], True)
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00002484 open_fds.update(fds)
2485
2486 for fd in open_fds:
2487 p = subprocess.Popen([sys.executable, fd_status],
2488 stdout=subprocess.PIPE, close_fds=True,
2489 pass_fds=(fd, ))
2490 output, ignored = p.communicate()
2491
2492 remaining_fds = set(map(int, output.split(b',')))
2493 to_be_closed = open_fds - {fd}
2494
2495 self.assertIn(fd, remaining_fds, "fd to be passed not passed")
2496 self.assertFalse(remaining_fds & to_be_closed,
2497 "fd to be closed passed")
2498
2499 # pass_fds overrides close_fds with a warning.
2500 with self.assertWarns(RuntimeWarning) as context:
2501 self.assertFalse(subprocess.call(
2502 [sys.executable, "-c", "import sys; sys.exit(0)"],
2503 close_fds=False, pass_fds=(fd, )))
2504 self.assertIn('overriding close_fds', str(context.warning))
2505
Victor Stinnerdaf45552013-08-28 00:53:59 +02002506 def test_pass_fds_inheritable(self):
Victor Stinnerf6fa22e2013-09-01 10:22:41 +02002507 script = support.findfile("fd_status.py", subdir="subprocessdata")
Victor Stinnerdaf45552013-08-28 00:53:59 +02002508
2509 inheritable, non_inheritable = os.pipe()
2510 self.addCleanup(os.close, inheritable)
2511 self.addCleanup(os.close, non_inheritable)
2512 os.set_inheritable(inheritable, True)
2513 os.set_inheritable(non_inheritable, False)
2514 pass_fds = (inheritable, non_inheritable)
2515 args = [sys.executable, script]
2516 args += list(map(str, pass_fds))
2517
2518 p = subprocess.Popen(args,
2519 stdout=subprocess.PIPE, close_fds=True,
2520 pass_fds=pass_fds)
2521 output, ignored = p.communicate()
2522 fds = set(map(int, output.split(b',')))
2523
2524 # the inheritable file descriptor must be inherited, so its inheritable
2525 # flag must be set in the child process after fork() and before exec()
Victor Stinnerf6fa22e2013-09-01 10:22:41 +02002526 self.assertEqual(fds, set(pass_fds), "output=%a" % output)
Victor Stinnerdaf45552013-08-28 00:53:59 +02002527
2528 # inheritable flag must not be changed in the parent process
2529 self.assertEqual(os.get_inheritable(inheritable), True)
2530 self.assertEqual(os.get_inheritable(non_inheritable), False)
2531
Gregory P. Smith112bb3a2011-03-15 14:55:17 -04002532 def test_stdout_stdin_are_single_inout_fd(self):
2533 with io.open(os.devnull, "r+") as inout:
2534 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2535 stdout=inout, stdin=inout)
2536 p.wait()
2537
2538 def test_stdout_stderr_are_single_inout_fd(self):
2539 with io.open(os.devnull, "r+") as inout:
2540 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2541 stdout=inout, stderr=inout)
2542 p.wait()
2543
2544 def test_stderr_stdin_are_single_inout_fd(self):
2545 with io.open(os.devnull, "r+") as inout:
2546 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2547 stderr=inout, stdin=inout)
2548 p.wait()
2549
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00002550 def test_wait_when_sigchild_ignored(self):
2551 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
2552 sigchild_ignore = support.findfile("sigchild_ignore.py",
2553 subdir="subprocessdata")
2554 p = subprocess.Popen([sys.executable, sigchild_ignore],
2555 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2556 stdout, stderr = p.communicate()
2557 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
Gregory P. Smitha80f4fb2010-12-14 15:23:02 +00002558 " non-zero with this error:\n%s" %
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00002559 stderr.decode('utf-8'))
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00002560
Antoine Pitrou7b98d022011-03-19 17:04:13 +01002561 def test_select_unbuffered(self):
2562 # Issue #11459: bufsize=0 should really set the pipes as
2563 # unbuffered (and therefore let select() work properly).
2564 select = support.import_module("select")
2565 p = subprocess.Popen([sys.executable, "-c",
2566 'import sys;'
2567 'sys.stdout.write("apple")'],
2568 stdout=subprocess.PIPE,
2569 bufsize=0)
2570 f = p.stdout
Ross Lagerwall17ace7a2011-03-26 21:21:46 +02002571 self.addCleanup(f.close)
Antoine Pitrou7b98d022011-03-19 17:04:13 +01002572 try:
2573 self.assertEqual(f.read(4), b"appl")
2574 self.assertIn(f, select.select([f], [], [], 0.0)[0])
2575 finally:
2576 p.wait()
2577
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002578 def test_zombie_fast_process_del(self):
2579 # Issue #12650: on Unix, if Popen.__del__() was called before the
2580 # process exited, it wouldn't be added to subprocess._active, and would
2581 # remain a zombie.
2582 # spawn a Popen, and delete its reference before it exits
2583 p = subprocess.Popen([sys.executable, "-c",
2584 'import sys, time;'
2585 'time.sleep(0.2)'],
2586 stdout=subprocess.PIPE,
2587 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02002588 self.addCleanup(p.stdout.close)
2589 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002590 ident = id(p)
2591 pid = p.pid
Victor Stinner5a48e212016-05-20 12:11:15 +02002592 with support.check_warnings(('', ResourceWarning)):
2593 p = None
2594
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002595 # check that p is in the active processes list
2596 self.assertIn(ident, [id(o) for o in subprocess._active])
2597
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002598 def test_leak_fast_process_del_killed(self):
2599 # Issue #12650: on Unix, if Popen.__del__() was called before the
2600 # process exited, and the process got killed by a signal, it would never
2601 # be removed from subprocess._active, which triggered a FD and memory
2602 # leak.
2603 # spawn a Popen, delete its reference and kill it
2604 p = subprocess.Popen([sys.executable, "-c",
2605 'import time;'
2606 'time.sleep(3)'],
2607 stdout=subprocess.PIPE,
2608 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02002609 self.addCleanup(p.stdout.close)
2610 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002611 ident = id(p)
2612 pid = p.pid
Victor Stinner5a48e212016-05-20 12:11:15 +02002613 with support.check_warnings(('', ResourceWarning)):
2614 p = None
2615
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002616 os.kill(pid, signal.SIGKILL)
2617 # check that p is in the active processes list
2618 self.assertIn(ident, [id(o) for o in subprocess._active])
2619
2620 # let some time for the process to exit, and create a new Popen: this
2621 # should trigger the wait() of p
2622 time.sleep(0.2)
Victor Stinnerb31206a2018-01-25 19:06:05 +01002623 with self.assertRaises(OSError):
Victor Stinner9a83f652017-08-21 23:51:31 +02002624 with subprocess.Popen(NONEXISTING_CMD,
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002625 stdout=subprocess.PIPE,
2626 stderr=subprocess.PIPE) as proc:
2627 pass
2628 # p should have been wait()ed on, and removed from the _active list
2629 self.assertRaises(OSError, os.waitpid, pid, 0)
2630 self.assertNotIn(ident, [id(o) for o in subprocess._active])
2631
Charles-François Natali249cdc32013-08-25 18:24:45 +02002632 def test_close_fds_after_preexec(self):
2633 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2634
2635 # this FD is used as dup2() target by preexec_fn, and should be closed
2636 # in the child process
2637 fd = os.dup(1)
2638 self.addCleanup(os.close, fd)
2639
2640 p = subprocess.Popen([sys.executable, fd_status],
2641 stdout=subprocess.PIPE, close_fds=True,
2642 preexec_fn=lambda: os.dup2(1, fd))
2643 output, ignored = p.communicate()
2644
2645 remaining_fds = set(map(int, output.split(b',')))
2646
2647 self.assertNotIn(fd, remaining_fds)
2648
Victor Stinner8f437aa2014-10-05 17:25:19 +02002649 @support.cpython_only
2650 def test_fork_exec(self):
2651 # Issue #22290: fork_exec() must not crash on memory allocation failure
2652 # or other errors
2653 import _posixsubprocess
2654 gc_enabled = gc.isenabled()
2655 try:
2656 # Use a preexec function and enable the garbage collector
2657 # to force fork_exec() to re-enable the garbage collector
2658 # on error.
2659 func = lambda: None
2660 gc.enable()
2661
Victor Stinner8f437aa2014-10-05 17:25:19 +02002662 for args, exe_list, cwd, env_list in (
2663 (123, [b"exe"], None, [b"env"]),
2664 ([b"arg"], 123, None, [b"env"]),
2665 ([b"arg"], [b"exe"], 123, [b"env"]),
2666 ([b"arg"], [b"exe"], None, 123),
2667 ):
2668 with self.assertRaises(TypeError):
2669 _posixsubprocess.fork_exec(
2670 args, exe_list,
Serhiy Storchaka66bffd12017-04-19 21:12:46 +03002671 True, (), cwd, env_list,
Victor Stinner8f437aa2014-10-05 17:25:19 +02002672 -1, -1, -1, -1,
2673 1, 2, 3, 4,
2674 True, True, func)
2675 finally:
2676 if not gc_enabled:
2677 gc.disable()
2678
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002679 @support.cpython_only
2680 def test_fork_exec_sorted_fd_sanity_check(self):
2681 # Issue #23564: sanity check the fork_exec() fds_to_keep sanity check.
2682 import _posixsubprocess
Serhiy Storchaka66bffd12017-04-19 21:12:46 +03002683 class BadInt:
2684 first = True
2685 def __init__(self, value):
2686 self.value = value
2687 def __int__(self):
2688 if self.first:
2689 self.first = False
2690 return self.value
2691 raise ValueError
2692
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002693 gc_enabled = gc.isenabled()
2694 try:
2695 gc.enable()
2696
2697 for fds_to_keep in (
2698 (-1, 2, 3, 4, 5), # Negative number.
2699 ('str', 4), # Not an int.
2700 (18, 23, 42, 2**63), # Out of range.
2701 (5, 4), # Not sorted.
2702 (6, 7, 7, 8), # Duplicate.
Serhiy Storchaka66bffd12017-04-19 21:12:46 +03002703 (BadInt(1), BadInt(2)),
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002704 ):
2705 with self.assertRaises(
2706 ValueError,
2707 msg='fds_to_keep={}'.format(fds_to_keep)) as c:
2708 _posixsubprocess.fork_exec(
2709 [b"false"], [b"false"],
2710 True, fds_to_keep, None, [b"env"],
2711 -1, -1, -1, -1,
2712 1, 2, 3, 4,
2713 True, True, None)
2714 self.assertIn('fds_to_keep', str(c.exception))
2715 finally:
2716 if not gc_enabled:
2717 gc.disable()
Victor Stinner8f437aa2014-10-05 17:25:19 +02002718
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)2daf8e72016-06-05 02:57:47 +00002719 def test_communicate_BrokenPipeError_stdin_close(self):
2720 # By not setting stdout or stderr or a timeout we force the fast path
2721 # that just calls _stdin_write() internally due to our mock.
2722 proc = subprocess.Popen([sys.executable, '-c', 'pass'])
2723 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
2724 mock_proc_stdin.close.side_effect = BrokenPipeError
2725 proc.communicate() # Should swallow BrokenPipeError from close.
2726 mock_proc_stdin.close.assert_called_with()
2727
2728 def test_communicate_BrokenPipeError_stdin_write(self):
2729 # By not setting stdout or stderr or a timeout we force the fast path
2730 # that just calls _stdin_write() internally due to our mock.
2731 proc = subprocess.Popen([sys.executable, '-c', 'pass'])
2732 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
2733 mock_proc_stdin.write.side_effect = BrokenPipeError
2734 proc.communicate(b'stuff') # Should swallow the BrokenPipeError.
2735 mock_proc_stdin.write.assert_called_once_with(b'stuff')
2736 mock_proc_stdin.close.assert_called_once_with()
2737
2738 def test_communicate_BrokenPipeError_stdin_flush(self):
2739 # Setting stdin and stdout forces the ._communicate() code path.
2740 # python -h exits faster than python -c pass (but spams stdout).
2741 proc = subprocess.Popen([sys.executable, '-h'],
2742 stdin=subprocess.PIPE,
2743 stdout=subprocess.PIPE)
2744 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin, \
2745 open(os.devnull, 'wb') as dev_null:
2746 mock_proc_stdin.flush.side_effect = BrokenPipeError
2747 # because _communicate registers a selector using proc.stdin...
2748 mock_proc_stdin.fileno.return_value = dev_null.fileno()
2749 # _communicate() should swallow BrokenPipeError from flush.
2750 proc.communicate(b'stuff')
2751 mock_proc_stdin.flush.assert_called_once_with()
2752
2753 def test_communicate_BrokenPipeError_stdin_close_with_timeout(self):
2754 # Setting stdin and stdout forces the ._communicate() code path.
2755 # python -h exits faster than python -c pass (but spams stdout).
2756 proc = subprocess.Popen([sys.executable, '-h'],
2757 stdin=subprocess.PIPE,
2758 stdout=subprocess.PIPE)
2759 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
2760 mock_proc_stdin.close.side_effect = BrokenPipeError
2761 # _communicate() should swallow BrokenPipeError from close.
2762 proc.communicate(timeout=999)
2763 mock_proc_stdin.close.assert_called_once_with()
2764
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002765 @unittest.skipUnless(_testcapi is not None
2766 and hasattr(_testcapi, 'W_STOPCODE'),
2767 'need _testcapi.W_STOPCODE')
2768 def test_stopped(self):
Gregory P. Smith50e16e32017-01-22 17:28:38 -08002769 """Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002770 args = [sys.executable, '-c', 'pass']
2771 proc = subprocess.Popen(args)
Victor Stinnercdee3f12017-06-26 17:23:03 +02002772
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002773 # Wait until the real process completes to avoid zombie process
2774 pid = proc.pid
2775 pid, status = os.waitpid(pid, 0)
2776 self.assertEqual(status, 0)
Victor Stinnercdee3f12017-06-26 17:23:03 +02002777
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002778 status = _testcapi.W_STOPCODE(3)
2779 with mock.patch('subprocess.os.waitpid', return_value=(pid, status)):
2780 returncode = proc.wait()
Victor Stinnercdee3f12017-06-26 17:23:03 +02002781
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002782 self.assertEqual(returncode, -3)
Gregory P. Smith50e16e32017-01-22 17:28:38 -08002783
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002784
Victor Stinner937ee9e2018-06-26 02:11:06 +02002785@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +00002786class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00002787
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002788 def test_startupinfo(self):
2789 # startupinfo argument
2790 # We uses hardcoded constants, because we do not want to
2791 # depend on win32all.
2792 STARTF_USESHOWWINDOW = 1
2793 SW_MAXIMIZE = 3
2794 startupinfo = subprocess.STARTUPINFO()
2795 startupinfo.dwFlags = STARTF_USESHOWWINDOW
2796 startupinfo.wShowWindow = SW_MAXIMIZE
2797 # Since Python is a console process, it won't be affected
2798 # by wShowWindow, but the argument should be silently
2799 # ignored
2800 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002801 startupinfo=startupinfo)
2802
Subhendu Ghoshae160bb2017-02-25 20:29:05 +05302803 def test_startupinfo_keywords(self):
2804 # startupinfo argument
2805 # We use hardcoded constants, because we do not want to
2806 # depend on win32all.
2807 STARTF_USERSHOWWINDOW = 1
2808 SW_MAXIMIZE = 3
2809 startupinfo = subprocess.STARTUPINFO(
2810 dwFlags=STARTF_USERSHOWWINDOW,
2811 wShowWindow=SW_MAXIMIZE
2812 )
2813 # Since Python is a console process, it won't be affected
2814 # by wShowWindow, but the argument should be silently
2815 # ignored
2816 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
2817 startupinfo=startupinfo)
2818
Victor Stinner483422f2018-07-05 22:54:17 +02002819 def test_startupinfo_copy(self):
2820 # bpo-34044: Popen must not modify input STARTUPINFO structure
2821 startupinfo = subprocess.STARTUPINFO()
2822 startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW
2823 startupinfo.wShowWindow = subprocess.SW_HIDE
2824
2825 # Call Popen() twice with the same startupinfo object to make sure
2826 # that it's not modified
2827 for _ in range(2):
2828 cmd = [sys.executable, "-c", "pass"]
2829 with open(os.devnull, 'w') as null:
2830 proc = subprocess.Popen(cmd,
2831 stdout=null,
2832 stderr=subprocess.STDOUT,
2833 startupinfo=startupinfo)
2834 with proc:
2835 proc.communicate()
2836 self.assertEqual(proc.returncode, 0)
2837
2838 self.assertEqual(startupinfo.dwFlags,
2839 subprocess.STARTF_USESHOWWINDOW)
2840 self.assertIsNone(startupinfo.hStdInput)
2841 self.assertIsNone(startupinfo.hStdOutput)
2842 self.assertIsNone(startupinfo.hStdError)
2843 self.assertEqual(startupinfo.wShowWindow, subprocess.SW_HIDE)
2844 self.assertEqual(startupinfo.lpAttributeList, {"handle_list": []})
2845
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002846 def test_creationflags(self):
2847 # creationflags argument
2848 CREATE_NEW_CONSOLE = 16
2849 sys.stderr.write(" a DOS box should flash briefly ...\n")
2850 subprocess.call(sys.executable +
2851 ' -c "import time; time.sleep(0.25)"',
2852 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002853
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002854 def test_invalid_args(self):
2855 # invalid arguments should raise ValueError
2856 self.assertRaises(ValueError, subprocess.call,
2857 [sys.executable, "-c",
2858 "import sys; sys.exit(47)"],
2859 preexec_fn=lambda: 1)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002860
Oren Milman0b3a87e2017-09-14 22:30:28 +03002861 @support.cpython_only
2862 def test_issue31471(self):
2863 # There shouldn't be an assertion failure in Popen() in case the env
2864 # argument has a bad keys() method.
2865 class BadEnv(dict):
2866 keys = None
2867 with self.assertRaises(TypeError):
2868 subprocess.Popen([sys.executable, "-c", "pass"], env=BadEnv())
2869
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002870 def test_close_fds(self):
2871 # close file descriptors
2872 rc = subprocess.call([sys.executable, "-c",
2873 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002874 close_fds=True)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002875 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002876
Segev Finerb2a60832017-12-18 11:28:19 +02002877 def test_close_fds_with_stdio(self):
2878 import msvcrt
2879
2880 fds = os.pipe()
2881 self.addCleanup(os.close, fds[0])
2882 self.addCleanup(os.close, fds[1])
2883
2884 handles = []
2885 for fd in fds:
2886 os.set_inheritable(fd, True)
2887 handles.append(msvcrt.get_osfhandle(fd))
2888
2889 p = subprocess.Popen([sys.executable, "-c",
2890 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2891 stdout=subprocess.PIPE, close_fds=False)
2892 stdout, stderr = p.communicate()
2893 self.assertEqual(p.returncode, 0)
2894 int(stdout.strip()) # Check that stdout is an integer
2895
2896 p = subprocess.Popen([sys.executable, "-c",
2897 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2898 stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
2899 stdout, stderr = p.communicate()
2900 self.assertEqual(p.returncode, 1)
2901 self.assertIn(b"OSError", stderr)
2902
2903 # The same as the previous call, but with an empty handle_list
2904 handle_list = []
2905 startupinfo = subprocess.STARTUPINFO()
2906 startupinfo.lpAttributeList = {"handle_list": handle_list}
2907 p = subprocess.Popen([sys.executable, "-c",
2908 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2909 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
2910 startupinfo=startupinfo, close_fds=True)
2911 stdout, stderr = p.communicate()
2912 self.assertEqual(p.returncode, 1)
2913 self.assertIn(b"OSError", stderr)
2914
2915 # Check for a warning due to using handle_list and close_fds=False
2916 with support.check_warnings((".*overriding close_fds", RuntimeWarning)):
2917 startupinfo = subprocess.STARTUPINFO()
2918 startupinfo.lpAttributeList = {"handle_list": handles[:]}
2919 p = subprocess.Popen([sys.executable, "-c",
2920 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2921 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
2922 startupinfo=startupinfo, close_fds=False)
2923 stdout, stderr = p.communicate()
2924 self.assertEqual(p.returncode, 0)
2925
2926 def test_empty_attribute_list(self):
2927 startupinfo = subprocess.STARTUPINFO()
2928 startupinfo.lpAttributeList = {}
2929 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
2930 startupinfo=startupinfo)
2931
2932 def test_empty_handle_list(self):
2933 startupinfo = subprocess.STARTUPINFO()
2934 startupinfo.lpAttributeList = {"handle_list": []}
2935 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
2936 startupinfo=startupinfo)
2937
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002938 def test_shell_sequence(self):
2939 # Run command through the shell (sequence)
2940 newenv = os.environ.copy()
2941 newenv["FRUIT"] = "physalis"
2942 p = subprocess.Popen(["set"], shell=1,
2943 stdout=subprocess.PIPE,
2944 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02002945 with p:
2946 self.assertIn(b"physalis", p.stdout.read())
Guido van Rossume7ba4952007-06-06 23:52:48 +00002947
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002948 def test_shell_string(self):
2949 # Run command through the shell (string)
2950 newenv = os.environ.copy()
2951 newenv["FRUIT"] = "physalis"
2952 p = subprocess.Popen("set", shell=1,
2953 stdout=subprocess.PIPE,
2954 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02002955 with p:
2956 self.assertIn(b"physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002957
Steve Dower050acae2016-09-06 20:16:17 -07002958 def test_shell_encodings(self):
2959 # Run command through the shell (string)
2960 for enc in ['ansi', 'oem']:
2961 newenv = os.environ.copy()
2962 newenv["FRUIT"] = "physalis"
2963 p = subprocess.Popen("set", shell=1,
2964 stdout=subprocess.PIPE,
2965 env=newenv,
2966 encoding=enc)
2967 with p:
2968 self.assertIn("physalis", p.stdout.read(), enc)
2969
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002970 def test_call_string(self):
2971 # call() function with string argument on Windows
2972 rc = subprocess.call(sys.executable +
2973 ' -c "import sys; sys.exit(47)"')
2974 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002975
Florent Xicluna4886d242010-03-08 13:27:26 +00002976 def _kill_process(self, method, *args):
2977 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroua4024e22010-09-24 18:57:01 +00002978 p = subprocess.Popen([sys.executable, "-c", """if 1:
2979 import sys, time
2980 sys.stdout.write('x\\n')
2981 sys.stdout.flush()
2982 time.sleep(30)
2983 """],
2984 stdin=subprocess.PIPE,
2985 stdout=subprocess.PIPE,
2986 stderr=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +02002987 with p:
2988 # Wait for the interpreter to be completely initialized before
2989 # sending any signal.
2990 p.stdout.read(1)
2991 getattr(p, method)(*args)
2992 _, stderr = p.communicate()
2993 self.assertStderrEqual(stderr, b'')
2994 returncode = p.wait()
Florent Xicluna4886d242010-03-08 13:27:26 +00002995 self.assertNotEqual(returncode, 0)
2996
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01002997 def _kill_dead_process(self, method, *args):
2998 p = subprocess.Popen([sys.executable, "-c", """if 1:
2999 import sys, time
3000 sys.stdout.write('x\\n')
3001 sys.stdout.flush()
3002 sys.exit(42)
3003 """],
3004 stdin=subprocess.PIPE,
3005 stdout=subprocess.PIPE,
3006 stderr=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +02003007 with p:
3008 # Wait for the interpreter to be completely initialized before
3009 # sending any signal.
3010 p.stdout.read(1)
3011 # The process should end after this
3012 time.sleep(1)
3013 # This shouldn't raise even though the child is now dead
3014 getattr(p, method)(*args)
3015 _, stderr = p.communicate()
3016 self.assertStderrEqual(stderr, b'')
3017 rc = p.wait()
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01003018 self.assertEqual(rc, 42)
3019
Florent Xicluna4886d242010-03-08 13:27:26 +00003020 def test_send_signal(self):
3021 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimesa342c012008-04-20 21:01:16 +00003022
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003023 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00003024 self._kill_process('kill')
Christian Heimesa342c012008-04-20 21:01:16 +00003025
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003026 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00003027 self._kill_process('terminate')
Christian Heimesa342c012008-04-20 21:01:16 +00003028
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01003029 def test_send_signal_dead(self):
3030 self._kill_dead_process('send_signal', signal.SIGTERM)
3031
3032 def test_kill_dead(self):
3033 self._kill_dead_process('kill')
3034
3035 def test_terminate_dead(self):
3036 self._kill_dead_process('terminate')
3037
Martin Panter23172bd2016-04-16 11:28:10 +00003038class MiscTests(unittest.TestCase):
Gregory P. Smithf4d644f2018-01-29 21:27:39 -08003039
3040 class RecordingPopen(subprocess.Popen):
3041 """A Popen that saves a reference to each instance for testing."""
3042 instances_created = []
3043
3044 def __init__(self, *args, **kwargs):
3045 super().__init__(*args, **kwargs)
3046 self.instances_created.append(self)
3047
3048 @mock.patch.object(subprocess.Popen, "_communicate")
3049 def _test_keyboardinterrupt_no_kill(self, popener, mock__communicate,
3050 **kwargs):
3051 """Fake a SIGINT happening during Popen._communicate() and ._wait().
3052
3053 This avoids the need to actually try and get test environments to send
3054 and receive signals reliably across platforms. The net effect of a ^C
3055 happening during a blocking subprocess execution which we want to clean
3056 up from is a KeyboardInterrupt coming out of communicate() or wait().
3057 """
3058
3059 mock__communicate.side_effect = KeyboardInterrupt
3060 try:
3061 with mock.patch.object(subprocess.Popen, "_wait") as mock__wait:
3062 # We patch out _wait() as no signal was involved so the
3063 # child process isn't actually going to exit rapidly.
3064 mock__wait.side_effect = KeyboardInterrupt
3065 with mock.patch.object(subprocess, "Popen",
3066 self.RecordingPopen):
3067 with self.assertRaises(KeyboardInterrupt):
3068 popener([sys.executable, "-c",
3069 "import time\ntime.sleep(9)\nimport sys\n"
3070 "sys.stderr.write('\\n!runaway child!\\n')"],
3071 stdout=subprocess.DEVNULL, **kwargs)
3072 for call in mock__wait.call_args_list[1:]:
3073 self.assertNotEqual(
3074 call, mock.call(timeout=None),
3075 "no open-ended wait() after the first allowed: "
3076 f"{mock__wait.call_args_list}")
3077 sigint_calls = []
3078 for call in mock__wait.call_args_list:
3079 if call == mock.call(timeout=0.25): # from Popen.__init__
3080 sigint_calls.append(call)
3081 self.assertLessEqual(mock__wait.call_count, 2,
3082 msg=mock__wait.call_args_list)
3083 self.assertEqual(len(sigint_calls), 1,
3084 msg=mock__wait.call_args_list)
3085 finally:
3086 # cleanup the forgotten (due to our mocks) child process
3087 process = self.RecordingPopen.instances_created.pop()
3088 process.kill()
3089 process.wait()
3090 self.assertEqual([], self.RecordingPopen.instances_created)
3091
3092 def test_call_keyboardinterrupt_no_kill(self):
3093 self._test_keyboardinterrupt_no_kill(subprocess.call, timeout=6.282)
3094
3095 def test_run_keyboardinterrupt_no_kill(self):
3096 self._test_keyboardinterrupt_no_kill(subprocess.run, timeout=6.282)
3097
3098 def test_context_manager_keyboardinterrupt_no_kill(self):
3099 def popen_via_context_manager(*args, **kwargs):
3100 with subprocess.Popen(*args, **kwargs) as unused_process:
3101 raise KeyboardInterrupt # Test how __exit__ handles ^C.
3102 self._test_keyboardinterrupt_no_kill(popen_via_context_manager)
3103
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003104 def test_getoutput(self):
3105 self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
3106 self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
3107 (0, 'xyzzy'))
Brett Cannona23810f2008-05-26 19:04:21 +00003108
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003109 # we use mkdtemp in the next line to create an empty directory
3110 # under our exclusive control; from that, we can invent a pathname
3111 # that we _know_ won't exist. This is guaranteed to fail.
3112 dir = None
3113 try:
3114 dir = tempfile.mkdtemp()
3115 name = os.path.join(dir, "foo")
Tim Goldene0041752013-11-03 12:53:17 +00003116 status, output = subprocess.getstatusoutput(
Victor Stinner937ee9e2018-06-26 02:11:06 +02003117 ("type " if mswindows else "cat ") + name)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003118 self.assertNotEqual(status, 0)
3119 finally:
3120 if dir is not None:
3121 os.rmdir(dir)
Brett Cannona23810f2008-05-26 19:04:21 +00003122
Gregory P. Smithace55862015-04-07 15:57:54 -07003123 def test__all__(self):
3124 """Ensure that __all__ is populated properly."""
Martin Panter528619b2016-04-16 23:42:37 +00003125 intentionally_excluded = {"list2cmdline", "Handle"}
Gregory P. Smithace55862015-04-07 15:57:54 -07003126 exported = set(subprocess.__all__)
3127 possible_exports = set()
3128 import types
3129 for name, value in subprocess.__dict__.items():
3130 if name.startswith('_'):
3131 continue
3132 if isinstance(value, (types.ModuleType,)):
3133 continue
3134 possible_exports.add(name)
3135 self.assertEqual(exported, possible_exports - intentionally_excluded)
3136
3137
Martin Panter23172bd2016-04-16 11:28:10 +00003138@unittest.skipUnless(hasattr(selectors, 'PollSelector'),
3139 "Test needs selectors.PollSelector")
3140class ProcessTestCaseNoPoll(ProcessTestCase):
3141 def setUp(self):
3142 self.orig_selector = subprocess._PopenSelector
3143 subprocess._PopenSelector = selectors.SelectSelector
3144 ProcessTestCase.setUp(self)
3145
3146 def tearDown(self):
3147 subprocess._PopenSelector = self.orig_selector
3148 ProcessTestCase.tearDown(self)
3149
Gregory P. Smithd06fa472009-07-04 02:46:54 +00003150
Victor Stinner937ee9e2018-06-26 02:11:06 +02003151@unittest.skipUnless(mswindows, "Windows-specific tests")
Tim Golden126c2962010-08-11 14:20:40 +00003152class CommandsWithSpaces (BaseTestCase):
3153
3154 def setUp(self):
3155 super().setUp()
Berker Peksag16a1f282015-09-28 13:33:14 +03003156 f, fname = tempfile.mkstemp(".py", "te st")
Tim Golden126c2962010-08-11 14:20:40 +00003157 self.fname = fname.lower ()
3158 os.write(f, b"import sys;"
3159 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
3160 )
3161 os.close(f)
3162
3163 def tearDown(self):
3164 os.remove(self.fname)
3165 super().tearDown()
3166
3167 def with_spaces(self, *args, **kwargs):
3168 kwargs['stdout'] = subprocess.PIPE
3169 p = subprocess.Popen(*args, **kwargs)
Victor Stinner7438c612016-05-20 12:43:15 +02003170 with p:
3171 self.assertEqual(
3172 p.stdout.read ().decode("mbcs"),
3173 "2 [%r, 'ab cd']" % self.fname
3174 )
Tim Golden126c2962010-08-11 14:20:40 +00003175
3176 def test_shell_string_with_spaces(self):
3177 # call() function with string argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00003178 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
3179 "ab cd"), shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00003180
3181 def test_shell_sequence_with_spaces(self):
3182 # call() function with sequence argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00003183 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00003184
3185 def test_noshell_string_with_spaces(self):
3186 # call() function with string argument with spaces on Windows
3187 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
3188 "ab cd"))
3189
3190 def test_noshell_sequence_with_spaces(self):
3191 # call() function with sequence argument with spaces on Windows
3192 self.with_spaces([sys.executable, self.fname, "ab cd"])
3193
Brian Curtin79cdb662010-12-03 02:46:02 +00003194
Georg Brandla86b2622012-02-20 21:34:57 +01003195class ContextManagerTests(BaseTestCase):
Brian Curtin79cdb662010-12-03 02:46:02 +00003196
3197 def test_pipe(self):
3198 with subprocess.Popen([sys.executable, "-c",
3199 "import sys;"
3200 "sys.stdout.write('stdout');"
3201 "sys.stderr.write('stderr');"],
3202 stdout=subprocess.PIPE,
3203 stderr=subprocess.PIPE) as proc:
3204 self.assertEqual(proc.stdout.read(), b"stdout")
3205 self.assertStderrEqual(proc.stderr.read(), b"stderr")
3206
3207 self.assertTrue(proc.stdout.closed)
3208 self.assertTrue(proc.stderr.closed)
3209
3210 def test_returncode(self):
3211 with subprocess.Popen([sys.executable, "-c",
3212 "import sys; sys.exit(100)"]) as proc:
Gregory P. Smith6b657452011-05-11 21:42:08 -07003213 pass
3214 # __exit__ calls wait(), so the returncode should be set
Brian Curtin79cdb662010-12-03 02:46:02 +00003215 self.assertEqual(proc.returncode, 100)
3216
3217 def test_communicate_stdin(self):
3218 with subprocess.Popen([sys.executable, "-c",
3219 "import sys;"
3220 "sys.exit(sys.stdin.read() == 'context')"],
3221 stdin=subprocess.PIPE) as proc:
3222 proc.communicate(b"context")
3223 self.assertEqual(proc.returncode, 1)
3224
3225 def test_invalid_args(self):
Victor Stinnerb31206a2018-01-25 19:06:05 +01003226 with self.assertRaises(NONEXISTING_ERRORS):
Victor Stinner9a83f652017-08-21 23:51:31 +02003227 with subprocess.Popen(NONEXISTING_CMD,
Brian Curtin79cdb662010-12-03 02:46:02 +00003228 stdout=subprocess.PIPE,
3229 stderr=subprocess.PIPE) as proc:
3230 pass
3231
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003232 def test_broken_pipe_cleanup(self):
3233 """Broken pipe error should not prevent wait() (Issue 21619)"""
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003234 proc = subprocess.Popen([sys.executable, '-c', 'pass'],
Victor Stinner20f4bd42015-03-05 02:38:41 +01003235 stdin=subprocess.PIPE,
Victor Stinner20f4bd42015-03-05 02:38:41 +01003236 bufsize=support.PIPE_MAX_SIZE*2)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003237 proc = proc.__enter__()
3238 # Prepare to send enough data to overflow any OS pipe buffering and
3239 # guarantee a broken pipe error. Data is held in BufferedWriter
3240 # buffer until closed.
3241 proc.stdin.write(b'x' * support.PIPE_MAX_SIZE)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003242 self.assertIsNone(proc.returncode)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003243 # EPIPE expected under POSIX; EINVAL under Windows
Serhiy Storchakacf265fd2015-02-28 13:27:54 +02003244 self.assertRaises(OSError, proc.__exit__, None, None, None)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003245 self.assertEqual(proc.returncode, 0)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003246 self.assertTrue(proc.stdin.closed)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003247
Brian Curtin79cdb662010-12-03 02:46:02 +00003248
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00003249if __name__ == "__main__":
Gregory P. Smith112bb3a2011-03-15 14:55:17 -04003250 unittest.main()