blob: b0b6b06e92759e3c0b3a0a5816e667524fba0351 [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
Alexey Izbysheva2670562018-10-20 03:22:31 +03001139 with self.assertWarnsRegex(RuntimeWarning, 'line buffering'):
1140 self._test_bufsize_equal_one(line, b'', universal_newlines=False)
Antoine Pitrouafe8d062014-09-21 21:10:56 +02001141
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001142 def test_leaking_fds_on_error(self):
1143 # see bug #5179: Popen leaks file descriptors to PIPEs if
1144 # the child fails to execute; this will eventually exhaust
1145 # the maximum number of open fds. 1024 seems a very common
1146 # value for that limit, but Windows has 2048, so we loop
1147 # 1024 times (each call leaked two fds).
1148 for i in range(1024):
Victor Stinnerb31206a2018-01-25 19:06:05 +01001149 with self.assertRaises(NONEXISTING_ERRORS):
Victor Stinner9a83f652017-08-21 23:51:31 +02001150 subprocess.Popen(NONEXISTING_CMD,
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001151 stdout=subprocess.PIPE,
1152 stderr=subprocess.PIPE)
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001153
Victor Stinner9a83f652017-08-21 23:51:31 +02001154 def test_nonexisting_with_pipes(self):
1155 # bpo-30121: Popen with pipes must close properly pipes on error.
1156 # Previously, os.close() was called with a Windows handle which is not
1157 # a valid file descriptor.
1158 #
1159 # Run the test in a subprocess to control how the CRT reports errors
1160 # and to get stderr content.
1161 try:
1162 import msvcrt
1163 msvcrt.CrtSetReportMode
1164 except (AttributeError, ImportError):
1165 self.skipTest("need msvcrt.CrtSetReportMode")
1166
1167 code = textwrap.dedent(f"""
1168 import msvcrt
1169 import subprocess
1170
1171 cmd = {NONEXISTING_CMD!r}
1172
1173 for report_type in [msvcrt.CRT_WARN,
1174 msvcrt.CRT_ERROR,
1175 msvcrt.CRT_ASSERT]:
1176 msvcrt.CrtSetReportMode(report_type, msvcrt.CRTDBG_MODE_FILE)
1177 msvcrt.CrtSetReportFile(report_type, msvcrt.CRTDBG_FILE_STDERR)
1178
1179 try:
Zachary Ware55376462018-02-19 14:02:38 -06001180 subprocess.Popen(cmd,
Victor Stinner9a83f652017-08-21 23:51:31 +02001181 stdout=subprocess.PIPE,
1182 stderr=subprocess.PIPE)
1183 except OSError:
1184 pass
1185 """)
1186 cmd = [sys.executable, "-c", code]
1187 proc = subprocess.Popen(cmd,
1188 stderr=subprocess.PIPE,
1189 universal_newlines=True)
1190 with proc:
1191 stderr = proc.communicate()[1]
1192 self.assertEqual(stderr, "")
1193 self.assertEqual(proc.returncode, 0)
1194
Antoine Pitroua8392712013-08-30 23:38:13 +02001195 def test_double_close_on_error(self):
1196 # Issue #18851
1197 fds = []
1198 def open_fds():
1199 for i in range(20):
1200 fds.extend(os.pipe())
1201 time.sleep(0.001)
1202 t = threading.Thread(target=open_fds)
1203 t.start()
1204 try:
1205 with self.assertRaises(EnvironmentError):
Victor Stinner9a83f652017-08-21 23:51:31 +02001206 subprocess.Popen(NONEXISTING_CMD,
Antoine Pitroua8392712013-08-30 23:38:13 +02001207 stdin=subprocess.PIPE,
1208 stdout=subprocess.PIPE,
1209 stderr=subprocess.PIPE)
1210 finally:
1211 t.join()
1212 exc = None
1213 for fd in fds:
1214 # If a double close occurred, some of those fds will
1215 # already have been closed by mistake, and os.close()
1216 # here will raise.
1217 try:
1218 os.close(fd)
1219 except OSError as e:
1220 exc = e
1221 if exc is not None:
1222 raise exc
1223
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001224 def test_threadsafe_wait(self):
1225 """Issue21291: Popen.wait() needs to be threadsafe for returncode."""
1226 proc = subprocess.Popen([sys.executable, '-c',
1227 'import time; time.sleep(12)'])
1228 self.assertEqual(proc.returncode, None)
1229 results = []
1230
1231 def kill_proc_timer_thread():
1232 results.append(('thread-start-poll-result', proc.poll()))
1233 # terminate it from the thread and wait for the result.
1234 proc.kill()
1235 proc.wait()
1236 results.append(('thread-after-kill-and-wait', proc.returncode))
1237 # this wait should be a no-op given the above.
1238 proc.wait()
1239 results.append(('thread-after-second-wait', proc.returncode))
1240
1241 # This is a timing sensitive test, the failure mode is
1242 # triggered when both the main thread and this thread are in
1243 # the wait() call at once. The delay here is to allow the
1244 # main thread to most likely be blocked in its wait() call.
1245 t = threading.Timer(0.2, kill_proc_timer_thread)
1246 t.start()
1247
Victor Stinner937ee9e2018-06-26 02:11:06 +02001248 if mswindows:
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001249 expected_errorcode = 1
1250 else:
1251 # Should be -9 because of the proc.kill() from the thread.
1252 expected_errorcode = -9
1253
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001254 # Wait for the process to finish; the thread should kill it
1255 # long before it finishes on its own. Supplying a timeout
1256 # triggers a different code path for better coverage.
1257 proc.wait(timeout=20)
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001258 self.assertEqual(proc.returncode, expected_errorcode,
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001259 msg="unexpected result in wait from main thread")
1260
1261 # This should be a no-op with no change in returncode.
1262 proc.wait()
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001263 self.assertEqual(proc.returncode, expected_errorcode,
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001264 msg="unexpected result in second main wait.")
1265
1266 t.join()
1267 # Ensure that all of the thread results are as expected.
1268 # When a race condition occurs in wait(), the returncode could
1269 # be set by the wrong thread that doesn't actually have it
1270 # leading to an incorrect value.
1271 self.assertEqual([('thread-start-poll-result', None),
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001272 ('thread-after-kill-and-wait', expected_errorcode),
1273 ('thread-after-second-wait', expected_errorcode)],
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001274 results)
1275
Victor Stinnerb3693582010-05-21 20:13:12 +00001276 def test_issue8780(self):
1277 # Ensure that stdout is inherited from the parent
1278 # if stdout=PIPE is not used
1279 code = ';'.join((
1280 'import subprocess, sys',
1281 'retcode = subprocess.call('
1282 "[sys.executable, '-c', 'print(\"Hello World!\")'])",
1283 'assert retcode == 0'))
1284 output = subprocess.check_output([sys.executable, '-c', code])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001285 self.assertTrue(output.startswith(b'Hello World!'), ascii(output))
Victor Stinnerb3693582010-05-21 20:13:12 +00001286
Tim Goldenaf5ac392010-08-06 13:03:56 +00001287 def test_handles_closed_on_exception(self):
1288 # If CreateProcess exits with an error, ensure the
1289 # duplicate output handles are released
Berker Peksag16a1f282015-09-28 13:33:14 +03001290 ifhandle, ifname = tempfile.mkstemp()
1291 ofhandle, ofname = tempfile.mkstemp()
1292 efhandle, efname = tempfile.mkstemp()
Tim Goldenaf5ac392010-08-06 13:03:56 +00001293 try:
1294 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
1295 stderr=efhandle)
1296 except OSError:
1297 os.close(ifhandle)
1298 os.remove(ifname)
1299 os.close(ofhandle)
1300 os.remove(ofname)
1301 os.close(efhandle)
1302 os.remove(efname)
1303 self.assertFalse(os.path.exists(ifname))
1304 self.assertFalse(os.path.exists(ofname))
1305 self.assertFalse(os.path.exists(efname))
1306
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001307 def test_communicate_epipe(self):
1308 # Issue 10963: communicate() should hide EPIPE
1309 p = subprocess.Popen([sys.executable, "-c", 'pass'],
1310 stdin=subprocess.PIPE,
1311 stdout=subprocess.PIPE,
1312 stderr=subprocess.PIPE)
1313 self.addCleanup(p.stdout.close)
1314 self.addCleanup(p.stderr.close)
1315 self.addCleanup(p.stdin.close)
1316 p.communicate(b"x" * 2**20)
1317
1318 def test_communicate_epipe_only_stdin(self):
1319 # Issue 10963: communicate() should hide EPIPE
1320 p = subprocess.Popen([sys.executable, "-c", 'pass'],
1321 stdin=subprocess.PIPE)
1322 self.addCleanup(p.stdin.close)
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001323 p.wait()
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001324 p.communicate(b"x" * 2**20)
1325
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001326 @unittest.skipUnless(hasattr(signal, 'SIGUSR1'),
1327 "Requires signal.SIGUSR1")
1328 @unittest.skipUnless(hasattr(os, 'kill'),
1329 "Requires os.kill")
1330 @unittest.skipUnless(hasattr(os, 'getppid'),
1331 "Requires os.getppid")
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001332 def test_communicate_eintr(self):
1333 # Issue #12493: communicate() should handle EINTR
1334 def handler(signum, frame):
1335 pass
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001336 old_handler = signal.signal(signal.SIGUSR1, handler)
1337 self.addCleanup(signal.signal, signal.SIGUSR1, old_handler)
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001338
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001339 args = [sys.executable, "-c",
1340 'import os, signal;'
1341 'os.kill(os.getppid(), signal.SIGUSR1)']
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001342 for stream in ('stdout', 'stderr'):
1343 kw = {stream: subprocess.PIPE}
1344 with subprocess.Popen(args, **kw) as process:
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001345 # communicate() will be interrupted by SIGUSR1
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001346 process.communicate()
1347
Tim Peterse718f612004-10-12 21:51:32 +00001348
Gregory P. Smith3d8e7762012-11-10 22:32:22 -08001349 # This test is Linux-ish specific for simplicity to at least have
1350 # some coverage. It is not a platform specific bug.
1351 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
1352 "Linux specific")
1353 def test_failed_child_execute_fd_leak(self):
1354 """Test for the fork() failure fd leak reported in issue16327."""
1355 fd_directory = '/proc/%d/fd' % os.getpid()
1356 fds_before_popen = os.listdir(fd_directory)
1357 with self.assertRaises(PopenTestException):
1358 PopenExecuteChildRaises(
1359 [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE,
1360 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1361
1362 # NOTE: This test doesn't verify that the real _execute_child
1363 # does not close the file descriptors itself on the way out
1364 # during an exception. Code inspection has confirmed that.
1365
1366 fds_after_exception = os.listdir(fd_directory)
1367 self.assertEqual(fds_before_popen, fds_after_exception)
1368
Victor Stinner937ee9e2018-06-26 02:11:06 +02001369 @unittest.skipIf(mswindows, "behavior currently not supported on Windows")
Gregory P. Smith8621bb52017-08-24 14:58:25 -07001370 def test_file_not_found_includes_filename(self):
1371 with self.assertRaises(FileNotFoundError) as c:
1372 subprocess.call(['/opt/nonexistent_binary', 'with', 'some', 'args'])
1373 self.assertEqual(c.exception.filename, '/opt/nonexistent_binary')
1374
Victor Stinner937ee9e2018-06-26 02:11:06 +02001375 @unittest.skipIf(mswindows, "behavior currently not supported on Windows")
Gregory P. Smith8621bb52017-08-24 14:58:25 -07001376 def test_file_not_found_with_bad_cwd(self):
1377 with self.assertRaises(FileNotFoundError) as c:
1378 subprocess.Popen(['exit', '0'], cwd='/some/nonexistent/directory')
1379 self.assertEqual(c.exception.filename, '/some/nonexistent/directory')
1380
Gregory P. Smith6e730002015-04-14 16:14:25 -07001381
1382class RunFuncTestCase(BaseTestCase):
1383 def run_python(self, code, **kwargs):
1384 """Run Python code in a subprocess using subprocess.run"""
1385 argv = [sys.executable, "-c", code]
1386 return subprocess.run(argv, **kwargs)
1387
1388 def test_returncode(self):
1389 # call() function with sequence argument
1390 cp = self.run_python("import sys; sys.exit(47)")
1391 self.assertEqual(cp.returncode, 47)
1392 with self.assertRaises(subprocess.CalledProcessError):
1393 cp.check_returncode()
1394
1395 def test_check(self):
1396 with self.assertRaises(subprocess.CalledProcessError) as c:
1397 self.run_python("import sys; sys.exit(47)", check=True)
1398 self.assertEqual(c.exception.returncode, 47)
1399
1400 def test_check_zero(self):
1401 # check_returncode shouldn't raise when returncode is zero
1402 cp = self.run_python("import sys; sys.exit(0)", check=True)
1403 self.assertEqual(cp.returncode, 0)
1404
1405 def test_timeout(self):
1406 # run() function with timeout argument; we want to test that the child
1407 # process gets killed when the timeout expires. If the child isn't
1408 # killed, this call will deadlock since subprocess.run waits for the
1409 # child.
1410 with self.assertRaises(subprocess.TimeoutExpired):
1411 self.run_python("while True: pass", timeout=0.0001)
1412
1413 def test_capture_stdout(self):
1414 # capture stdout with zero return code
1415 cp = self.run_python("print('BDFL')", stdout=subprocess.PIPE)
1416 self.assertIn(b'BDFL', cp.stdout)
1417
1418 def test_capture_stderr(self):
1419 cp = self.run_python("import sys; sys.stderr.write('BDFL')",
1420 stderr=subprocess.PIPE)
1421 self.assertIn(b'BDFL', cp.stderr)
1422
1423 def test_check_output_stdin_arg(self):
1424 # run() can be called with stdin set to a file
1425 tf = tempfile.TemporaryFile()
1426 self.addCleanup(tf.close)
1427 tf.write(b'pear')
1428 tf.seek(0)
1429 cp = self.run_python(
1430 "import sys; sys.stdout.write(sys.stdin.read().upper())",
1431 stdin=tf, stdout=subprocess.PIPE)
1432 self.assertIn(b'PEAR', cp.stdout)
1433
1434 def test_check_output_input_arg(self):
1435 # check_output() can be called with input set to a string
1436 cp = self.run_python(
1437 "import sys; sys.stdout.write(sys.stdin.read().upper())",
1438 input=b'pear', stdout=subprocess.PIPE)
1439 self.assertIn(b'PEAR', cp.stdout)
1440
1441 def test_check_output_stdin_with_input_arg(self):
1442 # run() refuses to accept 'stdin' with 'input'
1443 tf = tempfile.TemporaryFile()
1444 self.addCleanup(tf.close)
1445 tf.write(b'pear')
1446 tf.seek(0)
1447 with self.assertRaises(ValueError,
1448 msg="Expected ValueError when stdin and input args supplied.") as c:
1449 output = self.run_python("print('will not be run')",
1450 stdin=tf, input=b'hare')
1451 self.assertIn('stdin', c.exception.args[0])
1452 self.assertIn('input', c.exception.args[0])
1453
1454 def test_check_output_timeout(self):
1455 with self.assertRaises(subprocess.TimeoutExpired) as c:
1456 cp = self.run_python((
1457 "import sys, time\n"
1458 "sys.stdout.write('BDFL')\n"
1459 "sys.stdout.flush()\n"
1460 "time.sleep(3600)"),
1461 # Some heavily loaded buildbots (sparc Debian 3.x) require
1462 # this much time to start and print.
1463 timeout=3, stdout=subprocess.PIPE)
1464 self.assertEqual(c.exception.output, b'BDFL')
1465 # output is aliased to stdout
1466 self.assertEqual(c.exception.stdout, b'BDFL')
1467
1468 def test_run_kwargs(self):
1469 newenv = os.environ.copy()
1470 newenv["FRUIT"] = "banana"
1471 cp = self.run_python(('import sys, os;'
1472 'sys.exit(33 if os.getenv("FRUIT")=="banana" else 31)'),
1473 env=newenv)
1474 self.assertEqual(cp.returncode, 33)
1475
Bo Baylesce0f33d2018-01-30 00:40:39 -06001476 def test_capture_output(self):
1477 cp = self.run_python(("import sys;"
1478 "sys.stdout.write('BDFL'); "
1479 "sys.stderr.write('FLUFL')"),
1480 capture_output=True)
1481 self.assertIn(b'BDFL', cp.stdout)
1482 self.assertIn(b'FLUFL', cp.stderr)
1483
1484 def test_stdout_with_capture_output_arg(self):
1485 # run() refuses to accept 'stdout' with 'capture_output'
1486 tf = tempfile.TemporaryFile()
1487 self.addCleanup(tf.close)
1488 with self.assertRaises(ValueError,
1489 msg=("Expected ValueError when stdout and capture_output "
1490 "args supplied.")) as c:
1491 output = self.run_python("print('will not be run')",
1492 capture_output=True, stdout=tf)
1493 self.assertIn('stdout', c.exception.args[0])
1494 self.assertIn('capture_output', c.exception.args[0])
1495
1496 def test_stderr_with_capture_output_arg(self):
1497 # run() refuses to accept 'stderr' with 'capture_output'
1498 tf = tempfile.TemporaryFile()
1499 self.addCleanup(tf.close)
1500 with self.assertRaises(ValueError,
1501 msg=("Expected ValueError when stderr and capture_output "
1502 "args supplied.")) as c:
1503 output = self.run_python("print('will not be run')",
1504 capture_output=True, stderr=tf)
1505 self.assertIn('stderr', c.exception.args[0])
1506 self.assertIn('capture_output', c.exception.args[0])
1507
Gregory P. Smith6e730002015-04-14 16:14:25 -07001508
Victor Stinner937ee9e2018-06-26 02:11:06 +02001509@unittest.skipIf(mswindows, "POSIX specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +00001510class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001511
Gregory P. Smith5591b022012-10-10 03:34:47 -07001512 def setUp(self):
1513 super().setUp()
1514 self._nonexistent_dir = "/_this/pa.th/does/not/exist"
1515
1516 def _get_chdir_exception(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001517 try:
Gregory P. Smith5591b022012-10-10 03:34:47 -07001518 os.chdir(self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001519 except OSError as e:
1520 # This avoids hard coding the errno value or the OS perror()
1521 # string and instead capture the exception that we want to see
1522 # below for comparison.
1523 desired_exception = e
1524 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)
Zackery Spytz73870bf2018-09-11 09:54:07 -06001540 self.assertEqual(desired_exception.filename, e.filename)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001541 else:
1542 self.fail("Expected OSError: %s" % desired_exception)
1543
Gregory P. Smith5591b022012-10-10 03:34:47 -07001544 def test_exception_bad_executable(self):
1545 """Test error in the child raised in the parent for a bad executable."""
1546 desired_exception = self._get_chdir_exception()
1547 try:
1548 p = subprocess.Popen([sys.executable, "-c", ""],
1549 executable=self._nonexistent_dir)
1550 except OSError as e:
1551 # Test that the child process exec failure actually makes
1552 # it up to the parent process as the correct exception.
1553 self.assertEqual(desired_exception.errno, e.errno)
1554 self.assertEqual(desired_exception.strerror, e.strerror)
Zackery Spytz73870bf2018-09-11 09:54:07 -06001555 self.assertEqual(desired_exception.filename, e.filename)
Gregory P. Smith5591b022012-10-10 03:34:47 -07001556 else:
1557 self.fail("Expected OSError: %s" % desired_exception)
1558
1559 def test_exception_bad_args_0(self):
1560 """Test error in the child raised in the parent for a bad args[0]."""
1561 desired_exception = self._get_chdir_exception()
1562 try:
1563 p = subprocess.Popen([self._nonexistent_dir, "-c", ""])
1564 except OSError as e:
1565 # Test that the child process exec failure actually makes
1566 # it up to the parent process as the correct exception.
1567 self.assertEqual(desired_exception.errno, e.errno)
1568 self.assertEqual(desired_exception.strerror, e.strerror)
Zackery Spytz73870bf2018-09-11 09:54:07 -06001569 self.assertEqual(desired_exception.filename, e.filename)
Gregory P. Smith5591b022012-10-10 03:34:47 -07001570 else:
1571 self.fail("Expected OSError: %s" % desired_exception)
1572
Ammar Askar3fc499b2017-09-06 02:41:30 -04001573 # We mock the __del__ method for Popen in the next two tests
1574 # because it does cleanup based on the pid returned by fork_exec
1575 # along with issuing a resource warning if it still exists. Since
1576 # we don't actually spawn a process in these tests we can forego
1577 # the destructor. An alternative would be to set _child_created to
1578 # False before the destructor is called but there is no easy way
1579 # to do that
1580 class PopenNoDestructor(subprocess.Popen):
1581 def __del__(self):
1582 pass
1583
1584 @mock.patch("subprocess._posixsubprocess.fork_exec")
1585 def test_exception_errpipe_normal(self, fork_exec):
1586 """Test error passing done through errpipe_write in the good case"""
1587 def proper_error(*args):
1588 errpipe_write = args[13]
1589 # Write the hex for the error code EISDIR: 'is a directory'
1590 err_code = '{:x}'.format(errno.EISDIR).encode()
1591 os.write(errpipe_write, b"OSError:" + err_code + b":")
1592 return 0
1593
1594 fork_exec.side_effect = proper_error
1595
Victor Stinner11045c92017-10-05 06:32:53 -07001596 with mock.patch("subprocess.os.waitpid",
1597 side_effect=ChildProcessError):
1598 with self.assertRaises(IsADirectoryError):
1599 self.PopenNoDestructor(["non_existent_command"])
Ammar Askar3fc499b2017-09-06 02:41:30 -04001600
1601 @mock.patch("subprocess._posixsubprocess.fork_exec")
1602 def test_exception_errpipe_bad_data(self, fork_exec):
1603 """Test error passing done through errpipe_write where its not
1604 in the expected format"""
1605 error_data = b"\xFF\x00\xDE\xAD"
1606 def bad_error(*args):
1607 errpipe_write = args[13]
1608 # Anything can be in the pipe, no assumptions should
1609 # be made about its encoding, so we'll write some
1610 # arbitrary hex bytes to test it out
1611 os.write(errpipe_write, error_data)
1612 return 0
1613
1614 fork_exec.side_effect = bad_error
1615
Victor Stinner11045c92017-10-05 06:32:53 -07001616 with mock.patch("subprocess.os.waitpid",
1617 side_effect=ChildProcessError):
1618 with self.assertRaises(subprocess.SubprocessError) as e:
1619 self.PopenNoDestructor(["non_existent_command"])
Ammar Askar3fc499b2017-09-06 02:41:30 -04001620
1621 self.assertIn(repr(error_data), str(e.exception))
1622
Gregory P. Smith5f3d04f2018-06-05 12:00:57 -07001623 @unittest.skipIf(not os.path.exists('/proc/self/status'),
1624 "need /proc/self/status")
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001625 def test_restore_signals(self):
Gregory P. Smith5f3d04f2018-06-05 12:00:57 -07001626 # Blindly assume that cat exists on systems with /proc/self/status...
1627 default_proc_status = subprocess.check_output(
1628 ['cat', '/proc/self/status'],
1629 restore_signals=False)
1630 for line in default_proc_status.splitlines():
1631 if line.startswith(b'SigIgn'):
1632 default_sig_ign_mask = line
1633 break
1634 else:
1635 self.skipTest("SigIgn not found in /proc/self/status.")
1636 restored_proc_status = subprocess.check_output(
1637 ['cat', '/proc/self/status'],
1638 restore_signals=True)
1639 for line in restored_proc_status.splitlines():
1640 if line.startswith(b'SigIgn'):
1641 restored_sig_ign_mask = line
1642 break
1643 self.assertNotEqual(default_sig_ign_mask, restored_sig_ign_mask,
1644 msg="restore_signals=True should've unblocked "
1645 "SIGPIPE and friends.")
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001646
1647 def test_start_new_session(self):
1648 # For code coverage of calling setsid(). We don't care if we get an
1649 # EPERM error from it depending on the test execution environment, that
1650 # still indicates that it was called.
1651 try:
1652 output = subprocess.check_output(
1653 [sys.executable, "-c",
1654 "import os; print(os.getpgid(os.getpid()))"],
1655 start_new_session=True)
1656 except OSError as e:
1657 if e.errno != errno.EPERM:
1658 raise
1659 else:
1660 parent_pgid = os.getpgid(os.getpid())
1661 child_pgid = int(output)
1662 self.assertNotEqual(parent_pgid, child_pgid)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001663
1664 def test_run_abort(self):
1665 # returncode handles signal termination
Antoine Pitrou77e904e2013-10-08 23:04:32 +02001666 with support.SuppressCrashReport():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001667 p = subprocess.Popen([sys.executable, "-c",
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001668 'import os; os.abort()'])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001669 p.wait()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001670 self.assertEqual(-p.returncode, signal.SIGABRT)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001671
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)d6da7602016-06-03 06:14:06 +00001672 def test_CalledProcessError_str_signal(self):
1673 err = subprocess.CalledProcessError(-int(signal.SIGABRT), "fake cmd")
1674 error_string = str(err)
1675 # We're relying on the repr() of the signal.Signals intenum to provide
1676 # the word signal, the signal name and the numeric value.
1677 self.assertIn("signal", error_string.lower())
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)b4149062016-06-03 06:19:35 +00001678 # We're not being specific about the signal name as some signals have
1679 # multiple names and which name is revealed can vary.
1680 self.assertIn("SIG", error_string)
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)d6da7602016-06-03 06:14:06 +00001681 self.assertIn(str(signal.SIGABRT), error_string)
1682
1683 def test_CalledProcessError_str_unknown_signal(self):
1684 err = subprocess.CalledProcessError(-9876543, "fake cmd")
1685 error_string = str(err)
1686 self.assertIn("unknown signal 9876543.", error_string)
1687
1688 def test_CalledProcessError_str_non_zero(self):
1689 err = subprocess.CalledProcessError(2, "fake cmd")
1690 error_string = str(err)
1691 self.assertIn("non-zero exit status 2.", error_string)
1692
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001693 def test_preexec(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001694 # DISCLAIMER: Setting environment variables is *not* a good use
1695 # of a preexec_fn. This is merely a test.
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001696 p = subprocess.Popen([sys.executable, "-c",
1697 'import sys,os;'
1698 'sys.stdout.write(os.getenv("FRUIT"))'],
1699 stdout=subprocess.PIPE,
1700 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Victor Stinner7438c612016-05-20 12:43:15 +02001701 with p:
1702 self.assertEqual(p.stdout.read(), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001703
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001704 def test_preexec_exception(self):
1705 def raise_it():
1706 raise ValueError("What if two swallows carried a coconut?")
1707 try:
1708 p = subprocess.Popen([sys.executable, "-c", ""],
1709 preexec_fn=raise_it)
Gregory P. Smith8d07c262012-11-10 23:53:47 -08001710 except subprocess.SubprocessError as e:
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001711 self.assertTrue(
1712 subprocess._posixsubprocess,
1713 "Expected a ValueError from the preexec_fn")
1714 except ValueError as e:
1715 self.assertIn("coconut", e.args[0])
1716 else:
1717 self.fail("Exception raised by preexec_fn did not make it "
1718 "to the parent process.")
1719
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001720 class _TestExecuteChildPopen(subprocess.Popen):
1721 """Used to test behavior at the end of _execute_child."""
1722 def __init__(self, testcase, *args, **kwargs):
1723 self._testcase = testcase
1724 subprocess.Popen.__init__(self, *args, **kwargs)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001725
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001726 def _execute_child(self, *args, **kwargs):
Gregory P. Smith12489d92012-11-11 01:37:02 -08001727 try:
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001728 subprocess.Popen._execute_child(self, *args, **kwargs)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001729 finally:
1730 # Open a bunch of file descriptors and verify that
1731 # none of them are the same as the ones the Popen
1732 # instance is using for stdin/stdout/stderr.
1733 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
1734 for _ in range(8)]
1735 try:
1736 for fd in devzero_fds:
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001737 self._testcase.assertNotIn(
1738 fd, (self.stdin.fileno(), self.stdout.fileno(),
1739 self.stderr.fileno()),
Gregory P. Smith12489d92012-11-11 01:37:02 -08001740 msg="At least one fd was closed early.")
1741 finally:
Richard Oudkerk0e547b62013-06-10 16:29:19 +01001742 for fd in devzero_fds:
1743 os.close(fd)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001744
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001745 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
1746 def test_preexec_errpipe_does_not_double_close_pipes(self):
1747 """Issue16140: Don't double close pipes on preexec error."""
1748
1749 def raise_it():
Gregory P. Smith65ee6ec2012-11-11 10:12:40 -08001750 raise subprocess.SubprocessError(
1751 "force the _execute_child() errpipe_data path.")
Gregory P. Smith12489d92012-11-11 01:37:02 -08001752
Gregory P. Smithc2c4cb62012-11-11 01:41:49 -08001753 with self.assertRaises(subprocess.SubprocessError):
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001754 self._TestExecuteChildPopen(
1755 self, [sys.executable, "-c", "pass"],
Gregory P. Smith12489d92012-11-11 01:37:02 -08001756 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1757 stderr=subprocess.PIPE, preexec_fn=raise_it)
1758
Gregory P. Smith32ec9da2010-03-19 16:53:08 +00001759 def test_preexec_gc_module_failure(self):
1760 # This tests the code that disables garbage collection if the child
1761 # process will execute any Python.
1762 def raise_runtime_error():
1763 raise RuntimeError("this shouldn't escape")
1764 enabled = gc.isenabled()
1765 orig_gc_disable = gc.disable
1766 orig_gc_isenabled = gc.isenabled
1767 try:
1768 gc.disable()
1769 self.assertFalse(gc.isenabled())
1770 subprocess.call([sys.executable, '-c', ''],
1771 preexec_fn=lambda: None)
1772 self.assertFalse(gc.isenabled(),
1773 "Popen enabled gc when it shouldn't.")
1774
1775 gc.enable()
1776 self.assertTrue(gc.isenabled())
1777 subprocess.call([sys.executable, '-c', ''],
1778 preexec_fn=lambda: None)
1779 self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
1780
1781 gc.disable = raise_runtime_error
1782 self.assertRaises(RuntimeError, subprocess.Popen,
1783 [sys.executable, '-c', ''],
1784 preexec_fn=lambda: None)
1785
1786 del gc.isenabled # force an AttributeError
1787 self.assertRaises(AttributeError, subprocess.Popen,
1788 [sys.executable, '-c', ''],
1789 preexec_fn=lambda: None)
1790 finally:
1791 gc.disable = orig_gc_disable
1792 gc.isenabled = orig_gc_isenabled
1793 if not enabled:
1794 gc.disable()
1795
Martin Panterf7fdbda2015-12-05 09:51:52 +00001796 @unittest.skipIf(
1797 sys.platform == 'darwin', 'setrlimit() seems to fail on OS X')
Martin Panterafdd5132015-11-30 02:21:41 +00001798 def test_preexec_fork_failure(self):
1799 # The internal code did not preserve the previous exception when
1800 # re-enabling garbage collection
1801 try:
1802 from resource import getrlimit, setrlimit, RLIMIT_NPROC
1803 except ImportError as err:
1804 self.skipTest(err) # RLIMIT_NPROC is specific to Linux and BSD
1805 limits = getrlimit(RLIMIT_NPROC)
1806 [_, hard] = limits
1807 setrlimit(RLIMIT_NPROC, (0, hard))
1808 self.addCleanup(setrlimit, RLIMIT_NPROC, limits)
Martin Panter5cf791b2015-12-11 05:40:14 +00001809 try:
Martin Panterafdd5132015-11-30 02:21:41 +00001810 subprocess.call([sys.executable, '-c', ''],
1811 preexec_fn=lambda: None)
Martin Panter5cf791b2015-12-11 05:40:14 +00001812 except BlockingIOError:
1813 # Forking should raise EAGAIN, translated to BlockingIOError
1814 pass
1815 else:
1816 self.skipTest('RLIMIT_NPROC had no effect; probably superuser')
Martin Panterafdd5132015-11-30 02:21:41 +00001817
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001818 def test_args_string(self):
1819 # args is a string
Berker Peksag16a1f282015-09-28 13:33:14 +03001820 fd, fname = tempfile.mkstemp()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001821 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +00001822 with open(fd, "w", errors="surrogateescape") as fobj:
Xavier de Gayed1415312016-07-22 12:15:29 +02001823 fobj.write("#!%s\n" % support.unix_shell)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001824 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
1825 sys.executable)
1826 os.chmod(fname, 0o700)
1827 p = subprocess.Popen(fname)
1828 p.wait()
1829 os.remove(fname)
1830 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001831
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001832 def test_invalid_args(self):
1833 # invalid arguments should raise ValueError
1834 self.assertRaises(ValueError, subprocess.call,
1835 [sys.executable, "-c",
1836 "import sys; sys.exit(47)"],
1837 startupinfo=47)
1838 self.assertRaises(ValueError, subprocess.call,
1839 [sys.executable, "-c",
1840 "import sys; sys.exit(47)"],
1841 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001842
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001843 def test_shell_sequence(self):
1844 # Run command through the shell (sequence)
1845 newenv = os.environ.copy()
1846 newenv["FRUIT"] = "apple"
1847 p = subprocess.Popen(["echo $FRUIT"], shell=1,
1848 stdout=subprocess.PIPE,
1849 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02001850 with p:
1851 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001852
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001853 def test_shell_string(self):
1854 # Run command through the shell (string)
1855 newenv = os.environ.copy()
1856 newenv["FRUIT"] = "apple"
1857 p = subprocess.Popen("echo $FRUIT", shell=1,
1858 stdout=subprocess.PIPE,
1859 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02001860 with p:
1861 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Christian Heimesa342c012008-04-20 21:01:16 +00001862
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001863 def test_call_string(self):
1864 # call() function with string argument on UNIX
Berker Peksag16a1f282015-09-28 13:33:14 +03001865 fd, fname = tempfile.mkstemp()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001866 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +00001867 with open(fd, "w", errors="surrogateescape") as fobj:
Xavier de Gayed1415312016-07-22 12:15:29 +02001868 fobj.write("#!%s\n" % support.unix_shell)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001869 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
1870 sys.executable)
1871 os.chmod(fname, 0o700)
1872 rc = subprocess.call(fname)
1873 os.remove(fname)
1874 self.assertEqual(rc, 47)
Christian Heimesa342c012008-04-20 21:01:16 +00001875
Stefan Krah9542cc62010-07-19 14:20:53 +00001876 def test_specific_shell(self):
1877 # Issue #9265: Incorrect name passed as arg[0].
1878 shells = []
1879 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
1880 for name in ['bash', 'ksh']:
1881 sh = os.path.join(prefix, name)
1882 if os.path.isfile(sh):
1883 shells.append(sh)
1884 if not shells: # Will probably work for any shell but csh.
1885 self.skipTest("bash or ksh required for this test")
1886 sh = '/bin/sh'
1887 if os.path.isfile(sh) and not os.path.islink(sh):
1888 # Test will fail if /bin/sh is a symlink to csh.
1889 shells.append(sh)
1890 for sh in shells:
1891 p = subprocess.Popen("echo $0", executable=sh, shell=True,
1892 stdout=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +02001893 with p:
1894 self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
Stefan Krah9542cc62010-07-19 14:20:53 +00001895
Florent Xicluna4886d242010-03-08 13:27:26 +00001896 def _kill_process(self, method, *args):
Florent Xicluna1d8ee3a2010-03-05 20:26:54 +00001897 # Do not inherit file handles from the parent.
1898 # It should fix failures on some platforms.
Gregory P. Smithdee04342013-08-29 13:35:27 -07001899 # Also set the SIGINT handler to the default to make sure it's not
1900 # being ignored (some tests rely on that.)
1901 old_handler = signal.signal(signal.SIGINT, signal.default_int_handler)
1902 try:
1903 p = subprocess.Popen([sys.executable, "-c", """if 1:
1904 import sys, time
1905 sys.stdout.write('x\\n')
1906 sys.stdout.flush()
1907 time.sleep(30)
1908 """],
1909 close_fds=True,
1910 stdin=subprocess.PIPE,
1911 stdout=subprocess.PIPE,
1912 stderr=subprocess.PIPE)
1913 finally:
1914 signal.signal(signal.SIGINT, old_handler)
Antoine Pitrou3d8580f2010-09-20 01:33:21 +00001915 # Wait for the interpreter to be completely initialized before
1916 # sending any signal.
1917 p.stdout.read(1)
1918 getattr(p, method)(*args)
Florent Xicluna4886d242010-03-08 13:27:26 +00001919 return p
1920
Charles-François Natali53221e32013-01-12 16:52:20 +01001921 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
1922 "Due to known OS bug (issue #16762)")
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001923 def _kill_dead_process(self, method, *args):
1924 # Do not inherit file handles from the parent.
1925 # It should fix failures on some platforms.
1926 p = subprocess.Popen([sys.executable, "-c", """if 1:
1927 import sys, time
1928 sys.stdout.write('x\\n')
1929 sys.stdout.flush()
1930 """],
1931 close_fds=True,
1932 stdin=subprocess.PIPE,
1933 stdout=subprocess.PIPE,
1934 stderr=subprocess.PIPE)
1935 # Wait for the interpreter to be completely initialized before
1936 # sending any signal.
1937 p.stdout.read(1)
1938 # The process should end after this
1939 time.sleep(1)
1940 # This shouldn't raise even though the child is now dead
1941 getattr(p, method)(*args)
1942 p.communicate()
1943
Florent Xicluna4886d242010-03-08 13:27:26 +00001944 def test_send_signal(self):
1945 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunac049d872010-03-27 22:47:23 +00001946 _, stderr = p.communicate()
1947 self.assertIn(b'KeyboardInterrupt', stderr)
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001948 self.assertNotEqual(p.wait(), 0)
Christian Heimesa342c012008-04-20 21:01:16 +00001949
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001950 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001951 p = self._kill_process('kill')
Florent Xiclunac049d872010-03-27 22:47:23 +00001952 _, stderr = p.communicate()
1953 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001954 self.assertEqual(p.wait(), -signal.SIGKILL)
Tim Peterse718f612004-10-12 21:51:32 +00001955
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001956 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001957 p = self._kill_process('terminate')
Florent Xiclunac049d872010-03-27 22:47:23 +00001958 _, stderr = p.communicate()
1959 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001960 self.assertEqual(p.wait(), -signal.SIGTERM)
1961
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001962 def test_send_signal_dead(self):
1963 # Sending a signal to a dead process
1964 self._kill_dead_process('send_signal', signal.SIGINT)
1965
1966 def test_kill_dead(self):
1967 # Killing a dead process
1968 self._kill_dead_process('kill')
1969
1970 def test_terminate_dead(self):
1971 # Terminating a dead process
1972 self._kill_dead_process('terminate')
1973
Victor Stinnerdaf45552013-08-28 00:53:59 +02001974 def _save_fds(self, save_fds):
1975 fds = []
1976 for fd in save_fds:
1977 inheritable = os.get_inheritable(fd)
1978 saved = os.dup(fd)
1979 fds.append((fd, saved, inheritable))
1980 return fds
1981
1982 def _restore_fds(self, fds):
1983 for fd, saved, inheritable in fds:
1984 os.dup2(saved, fd, inheritable=inheritable)
1985 os.close(saved)
1986
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001987 def check_close_std_fds(self, fds):
1988 # Issue #9905: test that subprocess pipes still work properly with
1989 # some standard fds closed
1990 stdin = 0
Victor Stinnerdaf45552013-08-28 00:53:59 +02001991 saved_fds = self._save_fds(fds)
1992 for fd, saved, inheritable in saved_fds:
1993 if fd == 0:
1994 stdin = saved
1995 break
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001996 try:
1997 for fd in fds:
1998 os.close(fd)
1999 out, err = subprocess.Popen([sys.executable, "-c",
2000 'import sys;'
2001 'sys.stdout.write("apple");'
2002 'sys.stdout.flush();'
2003 'sys.stderr.write("orange")'],
2004 stdin=stdin,
2005 stdout=subprocess.PIPE,
2006 stderr=subprocess.PIPE).communicate()
2007 err = support.strip_python_stderr(err)
2008 self.assertEqual((out, err), (b'apple', b'orange'))
2009 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02002010 self._restore_fds(saved_fds)
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00002011
2012 def test_close_fd_0(self):
2013 self.check_close_std_fds([0])
2014
2015 def test_close_fd_1(self):
2016 self.check_close_std_fds([1])
2017
2018 def test_close_fd_2(self):
2019 self.check_close_std_fds([2])
2020
2021 def test_close_fds_0_1(self):
2022 self.check_close_std_fds([0, 1])
2023
2024 def test_close_fds_0_2(self):
2025 self.check_close_std_fds([0, 2])
2026
2027 def test_close_fds_1_2(self):
2028 self.check_close_std_fds([1, 2])
2029
2030 def test_close_fds_0_1_2(self):
2031 # Issue #10806: test that subprocess pipes still work properly with
2032 # all standard fds closed.
2033 self.check_close_std_fds([0, 1, 2])
2034
Gregory P. Smith53dd8162013-12-01 16:03:24 -08002035 def test_small_errpipe_write_fd(self):
2036 """Issue #15798: Popen should work when stdio fds are available."""
2037 new_stdin = os.dup(0)
2038 new_stdout = os.dup(1)
2039 try:
2040 os.close(0)
2041 os.close(1)
2042
2043 # Side test: if errpipe_write fails to have its CLOEXEC
2044 # flag set this should cause the parent to think the exec
2045 # failed. Extremely unlikely: everyone supports CLOEXEC.
2046 subprocess.Popen([
2047 sys.executable, "-c",
2048 "print('AssertionError:0:CLOEXEC failure.')"]).wait()
2049 finally:
2050 # Restore original stdin and stdout
2051 os.dup2(new_stdin, 0)
2052 os.dup2(new_stdout, 1)
2053 os.close(new_stdin)
2054 os.close(new_stdout)
2055
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002056 def test_remapping_std_fds(self):
2057 # open up some temporary files
Berker Peksag16a1f282015-09-28 13:33:14 +03002058 temps = [tempfile.mkstemp() for i in range(3)]
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002059 try:
2060 temp_fds = [fd for fd, fname in temps]
2061
2062 # unlink the files -- we won't need to reopen them
2063 for fd, fname in temps:
2064 os.unlink(fname)
2065
2066 # write some data to what will become stdin, and rewind
2067 os.write(temp_fds[1], b"STDIN")
2068 os.lseek(temp_fds[1], 0, 0)
2069
2070 # move the standard file descriptors out of the way
Victor Stinnerdaf45552013-08-28 00:53:59 +02002071 saved_fds = self._save_fds(range(3))
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002072 try:
2073 # duplicate the file objects over the standard fd's
2074 for fd, temp_fd in enumerate(temp_fds):
2075 os.dup2(temp_fd, fd)
2076
2077 # now use those files in the "wrong" order, so that subprocess
2078 # has to rearrange them in the child
2079 p = subprocess.Popen([sys.executable, "-c",
2080 'import sys; got = sys.stdin.read();'
2081 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
2082 stdin=temp_fds[1],
2083 stdout=temp_fds[2],
2084 stderr=temp_fds[0])
2085 p.wait()
2086 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02002087 self._restore_fds(saved_fds)
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002088
2089 for fd in temp_fds:
2090 os.lseek(fd, 0, 0)
2091
2092 out = os.read(temp_fds[2], 1024)
2093 err = support.strip_python_stderr(os.read(temp_fds[0], 1024))
2094 self.assertEqual(out, b"got STDIN")
2095 self.assertEqual(err, b"err")
2096
2097 finally:
2098 for fd in temp_fds:
2099 os.close(fd)
2100
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002101 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
2102 # open up some temporary files
Berker Peksag16a1f282015-09-28 13:33:14 +03002103 temps = [tempfile.mkstemp() for i in range(3)]
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002104 temp_fds = [fd for fd, fname in temps]
2105 try:
2106 # unlink the files -- we won't need to reopen them
2107 for fd, fname in temps:
2108 os.unlink(fname)
2109
2110 # save a copy of the standard file descriptors
Victor Stinnerdaf45552013-08-28 00:53:59 +02002111 saved_fds = self._save_fds(range(3))
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002112 try:
2113 # duplicate the temp files over the standard fd's 0, 1, 2
2114 for fd, temp_fd in enumerate(temp_fds):
2115 os.dup2(temp_fd, fd)
2116
2117 # write some data to what will become stdin, and rewind
2118 os.write(stdin_no, b"STDIN")
2119 os.lseek(stdin_no, 0, 0)
2120
2121 # now use those files in the given order, so that subprocess
2122 # has to rearrange them in the child
2123 p = subprocess.Popen([sys.executable, "-c",
2124 'import sys; got = sys.stdin.read();'
2125 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
2126 stdin=stdin_no,
2127 stdout=stdout_no,
2128 stderr=stderr_no)
2129 p.wait()
2130
2131 for fd in temp_fds:
2132 os.lseek(fd, 0, 0)
2133
2134 out = os.read(stdout_no, 1024)
2135 err = support.strip_python_stderr(os.read(stderr_no, 1024))
2136 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02002137 self._restore_fds(saved_fds)
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002138
2139 self.assertEqual(out, b"got STDIN")
2140 self.assertEqual(err, b"err")
2141
2142 finally:
2143 for fd in temp_fds:
2144 os.close(fd)
2145
2146 # When duping fds, if there arises a situation where one of the fds is
2147 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
2148 # This tests all combinations of this.
2149 def test_swap_fds(self):
2150 self.check_swap_fds(0, 1, 2)
2151 self.check_swap_fds(0, 2, 1)
2152 self.check_swap_fds(1, 0, 2)
2153 self.check_swap_fds(1, 2, 0)
2154 self.check_swap_fds(2, 0, 1)
2155 self.check_swap_fds(2, 1, 0)
2156
Alexey Izbyshev0e7144b2018-03-26 22:49:35 +03002157 def _check_swap_std_fds_with_one_closed(self, from_fds, to_fds):
2158 saved_fds = self._save_fds(range(3))
2159 try:
2160 for from_fd in from_fds:
2161 with tempfile.TemporaryFile() as f:
2162 os.dup2(f.fileno(), from_fd)
2163
2164 fd_to_close = (set(range(3)) - set(from_fds)).pop()
2165 os.close(fd_to_close)
2166
2167 arg_names = ['stdin', 'stdout', 'stderr']
2168 kwargs = {}
2169 for from_fd, to_fd in zip(from_fds, to_fds):
2170 kwargs[arg_names[to_fd]] = from_fd
2171
2172 code = textwrap.dedent(r'''
2173 import os, sys
2174 skipped_fd = int(sys.argv[1])
2175 for fd in range(3):
2176 if fd != skipped_fd:
2177 os.write(fd, str(fd).encode('ascii'))
2178 ''')
2179
2180 skipped_fd = (set(range(3)) - set(to_fds)).pop()
2181
2182 rc = subprocess.call([sys.executable, '-c', code, str(skipped_fd)],
2183 **kwargs)
2184 self.assertEqual(rc, 0)
2185
2186 for from_fd, to_fd in zip(from_fds, to_fds):
2187 os.lseek(from_fd, 0, os.SEEK_SET)
2188 read_bytes = os.read(from_fd, 1024)
2189 read_fds = list(map(int, read_bytes.decode('ascii')))
2190 msg = textwrap.dedent(f"""
2191 When testing {from_fds} to {to_fds} redirection,
2192 parent descriptor {from_fd} got redirected
2193 to descriptor(s) {read_fds} instead of descriptor {to_fd}.
2194 """)
2195 self.assertEqual([to_fd], read_fds, msg)
2196 finally:
2197 self._restore_fds(saved_fds)
2198
2199 # Check that subprocess can remap std fds correctly even
2200 # if one of them is closed (#32844).
2201 def test_swap_std_fds_with_one_closed(self):
2202 for from_fds in itertools.combinations(range(3), 2):
2203 for to_fds in itertools.permutations(range(3), 2):
2204 self._check_swap_std_fds_with_one_closed(from_fds, to_fds)
2205
Victor Stinner13bb71c2010-04-23 21:41:56 +00002206 def test_surrogates_error_message(self):
Victor Stinner4d078042010-04-23 19:28:32 +00002207 def prepare():
2208 raise ValueError("surrogate:\uDCff")
2209
2210 try:
2211 subprocess.call(
2212 [sys.executable, "-c", "pass"],
2213 preexec_fn=prepare)
2214 except ValueError as err:
2215 # Pure Python implementations keeps the message
2216 self.assertIsNone(subprocess._posixsubprocess)
2217 self.assertEqual(str(err), "surrogate:\uDCff")
Gregory P. Smith8d07c262012-11-10 23:53:47 -08002218 except subprocess.SubprocessError as err:
Victor Stinner4d078042010-04-23 19:28:32 +00002219 # _posixsubprocess uses a default message
2220 self.assertIsNotNone(subprocess._posixsubprocess)
2221 self.assertEqual(str(err), "Exception occurred in preexec_fn.")
2222 else:
Gregory P. Smith8d07c262012-11-10 23:53:47 -08002223 self.fail("Expected ValueError or subprocess.SubprocessError")
Victor Stinner4d078042010-04-23 19:28:32 +00002224
Victor Stinner13bb71c2010-04-23 21:41:56 +00002225 def test_undecodable_env(self):
2226 for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')):
Victor Stinner5323fb02013-11-19 23:46:06 +01002227 encoded_value = value.encode("ascii", "surrogateescape")
2228
Victor Stinner13bb71c2010-04-23 21:41:56 +00002229 # test str with surrogates
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00002230 script = "import os; print(ascii(os.getenv(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002231 env = os.environ.copy()
2232 env[key] = value
Victor Stinner5323fb02013-11-19 23:46:06 +01002233 # Use C locale to get ASCII for the locale encoding to force
Michael Felt89d79b12018-08-26 19:29:36 +02002234 # surrogate-escaping of \xFF in the child process
Victor Stinnerebc78d22010-10-14 10:38:17 +00002235 env['LC_ALL'] = 'C'
Michael Felt89d79b12018-08-26 19:29:36 +02002236 decoded_value = value
Victor Stinner13bb71c2010-04-23 21:41:56 +00002237 stdout = subprocess.check_output(
2238 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002239 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00002240 stdout = stdout.rstrip(b'\n\r')
Victor Stinner5323fb02013-11-19 23:46:06 +01002241 self.assertEqual(stdout.decode('ascii'), ascii(decoded_value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00002242
2243 # test bytes
2244 key = key.encode("ascii", "surrogateescape")
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00002245 script = "import os; print(ascii(os.getenvb(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002246 env = os.environ.copy()
Victor Stinner5323fb02013-11-19 23:46:06 +01002247 env[key] = encoded_value
Victor Stinner13bb71c2010-04-23 21:41:56 +00002248 stdout = subprocess.check_output(
2249 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002250 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00002251 stdout = stdout.rstrip(b'\n\r')
Victor Stinner5323fb02013-11-19 23:46:06 +01002252 self.assertEqual(stdout.decode('ascii'), ascii(encoded_value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00002253
Victor Stinnerb745a742010-05-18 17:17:23 +00002254 def test_bytes_program(self):
2255 abs_program = os.fsencode(sys.executable)
2256 path, program = os.path.split(sys.executable)
2257 program = os.fsencode(program)
2258
2259 # absolute bytes path
2260 exitcode = subprocess.call([abs_program, "-c", "pass"])
Ezio Melottib3aedd42010-11-20 19:04:17 +00002261 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00002262
Victor Stinner7b3b20a2011-03-03 12:54:05 +00002263 # absolute bytes path as a string
2264 cmd = b"'" + abs_program + b"' -c pass"
2265 exitcode = subprocess.call(cmd, shell=True)
2266 self.assertEqual(exitcode, 0)
2267
Victor Stinnerb745a742010-05-18 17:17:23 +00002268 # bytes program, unicode PATH
2269 env = os.environ.copy()
2270 env["PATH"] = path
2271 exitcode = subprocess.call([program, "-c", "pass"], env=env)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002272 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00002273
2274 # bytes program, bytes PATH
2275 envb = os.environb.copy()
2276 envb[b"PATH"] = os.fsencode(path)
2277 exitcode = subprocess.call([program, "-c", "pass"], env=envb)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002278 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00002279
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002280 def test_pipe_cloexec(self):
2281 sleeper = support.findfile("input_reader.py", subdir="subprocessdata")
2282 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2283
2284 p1 = subprocess.Popen([sys.executable, sleeper],
2285 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2286 stderr=subprocess.PIPE, close_fds=False)
2287
2288 self.addCleanup(p1.communicate, b'')
2289
2290 p2 = subprocess.Popen([sys.executable, fd_status],
2291 stdout=subprocess.PIPE, close_fds=False)
2292
2293 output, error = p2.communicate()
2294 result_fds = set(map(int, output.split(b',')))
2295 unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(),
2296 p1.stderr.fileno()])
2297
2298 self.assertFalse(result_fds & unwanted_fds,
2299 "Expected no fds from %r to be open in child, "
2300 "found %r" %
2301 (unwanted_fds, result_fds & unwanted_fds))
2302
2303 def test_pipe_cloexec_real_tools(self):
2304 qcat = support.findfile("qcat.py", subdir="subprocessdata")
2305 qgrep = support.findfile("qgrep.py", subdir="subprocessdata")
2306
2307 subdata = b'zxcvbn'
2308 data = subdata * 4 + b'\n'
2309
2310 p1 = subprocess.Popen([sys.executable, qcat],
2311 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2312 close_fds=False)
2313
2314 p2 = subprocess.Popen([sys.executable, qgrep, subdata],
2315 stdin=p1.stdout, stdout=subprocess.PIPE,
2316 close_fds=False)
2317
2318 self.addCleanup(p1.wait)
2319 self.addCleanup(p2.wait)
Gregory P. Smith886455c2012-01-21 22:05:10 -08002320 def kill_p1():
2321 try:
2322 p1.terminate()
2323 except ProcessLookupError:
2324 pass
2325 def kill_p2():
2326 try:
2327 p2.terminate()
2328 except ProcessLookupError:
2329 pass
2330 self.addCleanup(kill_p1)
2331 self.addCleanup(kill_p2)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002332
2333 p1.stdin.write(data)
2334 p1.stdin.close()
2335
2336 readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10)
2337
2338 self.assertTrue(readfiles, "The child hung")
2339 self.assertEqual(p2.stdout.read(), data)
2340
Victor Stinnerfaa8c132011-01-03 16:36:00 +00002341 p1.stdout.close()
2342 p2.stdout.close()
2343
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002344 def test_close_fds(self):
2345 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2346
2347 fds = os.pipe()
2348 self.addCleanup(os.close, fds[0])
2349 self.addCleanup(os.close, fds[1])
2350
2351 open_fds = set(fds)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002352 # add a bunch more fds
2353 for _ in range(9):
Serhiy Storchaka85c30332015-02-15 13:58:23 +02002354 fd = os.open(os.devnull, os.O_RDONLY)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002355 self.addCleanup(os.close, fd)
2356 open_fds.add(fd)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002357
Victor Stinnerdaf45552013-08-28 00:53:59 +02002358 for fd in open_fds:
2359 os.set_inheritable(fd, True)
2360
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002361 p = subprocess.Popen([sys.executable, fd_status],
2362 stdout=subprocess.PIPE, close_fds=False)
2363 output, ignored = p.communicate()
2364 remaining_fds = set(map(int, output.split(b',')))
2365
2366 self.assertEqual(remaining_fds & open_fds, open_fds,
2367 "Some fds were closed")
2368
2369 p = subprocess.Popen([sys.executable, fd_status],
2370 stdout=subprocess.PIPE, close_fds=True)
2371 output, ignored = p.communicate()
2372 remaining_fds = set(map(int, output.split(b',')))
2373
2374 self.assertFalse(remaining_fds & open_fds,
2375 "Some fds were left open")
2376 self.assertIn(1, remaining_fds, "Subprocess failed")
2377
Gregory P. Smith8facece2012-01-21 14:01:08 -08002378 # Keep some of the fd's we opened open in the subprocess.
2379 # This tests _posixsubprocess.c's proper handling of fds_to_keep.
2380 fds_to_keep = set(open_fds.pop() for _ in range(8))
2381 p = subprocess.Popen([sys.executable, fd_status],
2382 stdout=subprocess.PIPE, close_fds=True,
izbyshev2d8f0632017-12-19 03:26:49 +07002383 pass_fds=fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002384 output, ignored = p.communicate()
2385 remaining_fds = set(map(int, output.split(b',')))
2386
izbyshev2d8f0632017-12-19 03:26:49 +07002387 self.assertFalse((remaining_fds - fds_to_keep) & open_fds,
Gregory P. Smith8facece2012-01-21 14:01:08 -08002388 "Some fds not in pass_fds were left open")
2389 self.assertIn(1, remaining_fds, "Subprocess failed")
2390
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002391
Gregory P. Smithd04f6992014-06-01 15:27:28 -07002392 @unittest.skipIf(sys.platform.startswith("freebsd") and
2393 os.stat("/dev").st_dev == os.stat("/dev/fd").st_dev,
2394 "Requires fdescfs mounted on /dev/fd on FreeBSD.")
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002395 def test_close_fds_when_max_fd_is_lowered(self):
2396 """Confirm that issue21618 is fixed (may fail under valgrind)."""
2397 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2398
Gregory P. Smith634aa682014-06-15 17:51:04 -07002399 # This launches the meat of the test in a child process to
2400 # avoid messing with the larger unittest processes maximum
2401 # number of file descriptors.
2402 # This process launches:
2403 # +--> Process that lowers its RLIMIT_NOFILE aftr setting up
2404 # a bunch of high open fds above the new lower rlimit.
2405 # Those are reported via stdout before launching a new
2406 # process with close_fds=False to run the actual test:
2407 # +--> The TEST: This one launches a fd_status.py
2408 # subprocess with close_fds=True so we can find out if
2409 # any of the fds above the lowered rlimit are still open.
2410 p = subprocess.Popen([sys.executable, '-c', textwrap.dedent(
2411 '''
2412 import os, resource, subprocess, sys, textwrap
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002413 open_fds = set()
2414 # Add a bunch more fds to pass down.
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002415 for _ in range(40):
Serhiy Storchaka85c30332015-02-15 13:58:23 +02002416 fd = os.open(os.devnull, os.O_RDONLY)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002417 open_fds.add(fd)
2418
2419 # Leave a two pairs of low ones available for use by the
2420 # internal child error pipe and the stdout pipe.
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002421 # We also leave 10 more open as some Python buildbots run into
2422 # "too many open files" errors during the test if we do not.
2423 for fd in sorted(open_fds)[:14]:
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002424 os.close(fd)
2425 open_fds.remove(fd)
2426
2427 for fd in open_fds:
Gregory P. Smith634aa682014-06-15 17:51:04 -07002428 #self.addCleanup(os.close, fd)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002429 os.set_inheritable(fd, True)
2430
2431 max_fd_open = max(open_fds)
2432
Gregory P. Smith634aa682014-06-15 17:51:04 -07002433 # Communicate the open_fds to the parent unittest.TestCase process.
2434 print(','.join(map(str, sorted(open_fds))))
2435 sys.stdout.flush()
2436
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002437 rlim_cur, rlim_max = resource.getrlimit(resource.RLIMIT_NOFILE)
2438 try:
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002439 # 29 is lower than the highest fds we are leaving open.
2440 resource.setrlimit(resource.RLIMIT_NOFILE, (29, rlim_max))
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002441 # Launch a new Python interpreter with our low fd rlim_cur that
2442 # inherits open fds above that limit. It then uses subprocess
2443 # with close_fds=True to get a report of open fds in the child.
2444 # An explicit list of fds to check is passed to fd_status.py as
2445 # letting fd_status rely on its default logic would miss the
2446 # fds above rlim_cur as it normally only checks up to that limit.
Gregory P. Smith634aa682014-06-15 17:51:04 -07002447 subprocess.Popen(
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002448 [sys.executable, '-c',
2449 textwrap.dedent("""
2450 import subprocess, sys
Gregory P. Smith634aa682014-06-15 17:51:04 -07002451 subprocess.Popen([sys.executable, %r] +
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002452 [str(x) for x in range({max_fd})],
Gregory P. Smithffd529c2014-06-01 13:46:54 -07002453 close_fds=True).wait()
Gregory P. Smith634aa682014-06-15 17:51:04 -07002454 """.format(max_fd=max_fd_open+1))],
2455 close_fds=False).wait()
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002456 finally:
2457 resource.setrlimit(resource.RLIMIT_NOFILE, (rlim_cur, rlim_max))
Gregory P. Smith634aa682014-06-15 17:51:04 -07002458 ''' % fd_status)], stdout=subprocess.PIPE)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002459
2460 output, unused_stderr = p.communicate()
Gregory P. Smith634aa682014-06-15 17:51:04 -07002461 output_lines = output.splitlines()
2462 self.assertEqual(len(output_lines), 2,
Gregory P. Smith9204e092014-06-15 20:16:01 -07002463 msg="expected exactly two lines of output:\n%r" % output)
Gregory P. Smith634aa682014-06-15 17:51:04 -07002464 opened_fds = set(map(int, output_lines[0].strip().split(b',')))
2465 remaining_fds = set(map(int, output_lines[1].strip().split(b',')))
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002466
Gregory P. Smith634aa682014-06-15 17:51:04 -07002467 self.assertFalse(remaining_fds & opened_fds,
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002468 msg="Some fds were left open.")
2469
2470
Victor Stinner88701e22011-06-01 13:13:04 +02002471 # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
2472 # descriptor of a pipe closed in the parent process is valid in the
2473 # child process according to fstat(), but the mode of the file
2474 # descriptor is invalid, and read or write raise an error.
2475 @support.requires_mac_ver(10, 5)
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00002476 def test_pass_fds(self):
2477 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2478
2479 open_fds = set()
2480
2481 for x in range(5):
2482 fds = os.pipe()
2483 self.addCleanup(os.close, fds[0])
2484 self.addCleanup(os.close, fds[1])
Victor Stinnerdaf45552013-08-28 00:53:59 +02002485 os.set_inheritable(fds[0], True)
2486 os.set_inheritable(fds[1], True)
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00002487 open_fds.update(fds)
2488
2489 for fd in open_fds:
2490 p = subprocess.Popen([sys.executable, fd_status],
2491 stdout=subprocess.PIPE, close_fds=True,
2492 pass_fds=(fd, ))
2493 output, ignored = p.communicate()
2494
2495 remaining_fds = set(map(int, output.split(b',')))
2496 to_be_closed = open_fds - {fd}
2497
2498 self.assertIn(fd, remaining_fds, "fd to be passed not passed")
2499 self.assertFalse(remaining_fds & to_be_closed,
2500 "fd to be closed passed")
2501
2502 # pass_fds overrides close_fds with a warning.
2503 with self.assertWarns(RuntimeWarning) as context:
2504 self.assertFalse(subprocess.call(
2505 [sys.executable, "-c", "import sys; sys.exit(0)"],
2506 close_fds=False, pass_fds=(fd, )))
2507 self.assertIn('overriding close_fds', str(context.warning))
2508
Victor Stinnerdaf45552013-08-28 00:53:59 +02002509 def test_pass_fds_inheritable(self):
Victor Stinnerf6fa22e2013-09-01 10:22:41 +02002510 script = support.findfile("fd_status.py", subdir="subprocessdata")
Victor Stinnerdaf45552013-08-28 00:53:59 +02002511
2512 inheritable, non_inheritable = os.pipe()
2513 self.addCleanup(os.close, inheritable)
2514 self.addCleanup(os.close, non_inheritable)
2515 os.set_inheritable(inheritable, True)
2516 os.set_inheritable(non_inheritable, False)
2517 pass_fds = (inheritable, non_inheritable)
2518 args = [sys.executable, script]
2519 args += list(map(str, pass_fds))
2520
2521 p = subprocess.Popen(args,
2522 stdout=subprocess.PIPE, close_fds=True,
2523 pass_fds=pass_fds)
2524 output, ignored = p.communicate()
2525 fds = set(map(int, output.split(b',')))
2526
2527 # the inheritable file descriptor must be inherited, so its inheritable
2528 # flag must be set in the child process after fork() and before exec()
Victor Stinnerf6fa22e2013-09-01 10:22:41 +02002529 self.assertEqual(fds, set(pass_fds), "output=%a" % output)
Victor Stinnerdaf45552013-08-28 00:53:59 +02002530
2531 # inheritable flag must not be changed in the parent process
2532 self.assertEqual(os.get_inheritable(inheritable), True)
2533 self.assertEqual(os.get_inheritable(non_inheritable), False)
2534
Gregory P. Smithce344102018-09-10 17:46:22 -07002535
2536 # bpo-32270: Ensure that descriptors specified in pass_fds
2537 # are inherited even if they are used in redirections.
2538 # Contributed by @izbyshev.
2539 def test_pass_fds_redirected(self):
2540 """Regression test for https://bugs.python.org/issue32270."""
2541 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2542 pass_fds = []
2543 for _ in range(2):
2544 fd = os.open(os.devnull, os.O_RDWR)
2545 self.addCleanup(os.close, fd)
2546 pass_fds.append(fd)
2547
2548 stdout_r, stdout_w = os.pipe()
2549 self.addCleanup(os.close, stdout_r)
2550 self.addCleanup(os.close, stdout_w)
2551 pass_fds.insert(1, stdout_w)
2552
2553 with subprocess.Popen([sys.executable, fd_status],
2554 stdin=pass_fds[0],
2555 stdout=pass_fds[1],
2556 stderr=pass_fds[2],
2557 close_fds=True,
2558 pass_fds=pass_fds):
2559 output = os.read(stdout_r, 1024)
2560 fds = {int(num) for num in output.split(b',')}
2561
2562 self.assertEqual(fds, {0, 1, 2} | frozenset(pass_fds), f"output={output!a}")
2563
2564
Gregory P. Smith112bb3a2011-03-15 14:55:17 -04002565 def test_stdout_stdin_are_single_inout_fd(self):
2566 with io.open(os.devnull, "r+") as inout:
2567 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2568 stdout=inout, stdin=inout)
2569 p.wait()
2570
2571 def test_stdout_stderr_are_single_inout_fd(self):
2572 with io.open(os.devnull, "r+") as inout:
2573 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2574 stdout=inout, stderr=inout)
2575 p.wait()
2576
2577 def test_stderr_stdin_are_single_inout_fd(self):
2578 with io.open(os.devnull, "r+") as inout:
2579 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2580 stderr=inout, stdin=inout)
2581 p.wait()
2582
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00002583 def test_wait_when_sigchild_ignored(self):
2584 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
2585 sigchild_ignore = support.findfile("sigchild_ignore.py",
2586 subdir="subprocessdata")
2587 p = subprocess.Popen([sys.executable, sigchild_ignore],
2588 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2589 stdout, stderr = p.communicate()
2590 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
Gregory P. Smitha80f4fb2010-12-14 15:23:02 +00002591 " non-zero with this error:\n%s" %
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00002592 stderr.decode('utf-8'))
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00002593
Antoine Pitrou7b98d022011-03-19 17:04:13 +01002594 def test_select_unbuffered(self):
2595 # Issue #11459: bufsize=0 should really set the pipes as
2596 # unbuffered (and therefore let select() work properly).
2597 select = support.import_module("select")
2598 p = subprocess.Popen([sys.executable, "-c",
2599 'import sys;'
2600 'sys.stdout.write("apple")'],
2601 stdout=subprocess.PIPE,
2602 bufsize=0)
2603 f = p.stdout
Ross Lagerwall17ace7a2011-03-26 21:21:46 +02002604 self.addCleanup(f.close)
Antoine Pitrou7b98d022011-03-19 17:04:13 +01002605 try:
2606 self.assertEqual(f.read(4), b"appl")
2607 self.assertIn(f, select.select([f], [], [], 0.0)[0])
2608 finally:
2609 p.wait()
2610
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002611 def test_zombie_fast_process_del(self):
2612 # Issue #12650: on Unix, if Popen.__del__() was called before the
2613 # process exited, it wouldn't be added to subprocess._active, and would
2614 # remain a zombie.
2615 # spawn a Popen, and delete its reference before it exits
2616 p = subprocess.Popen([sys.executable, "-c",
2617 'import sys, time;'
2618 'time.sleep(0.2)'],
2619 stdout=subprocess.PIPE,
2620 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02002621 self.addCleanup(p.stdout.close)
2622 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002623 ident = id(p)
2624 pid = p.pid
Victor Stinner5a48e212016-05-20 12:11:15 +02002625 with support.check_warnings(('', ResourceWarning)):
2626 p = None
2627
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002628 # check that p is in the active processes list
2629 self.assertIn(ident, [id(o) for o in subprocess._active])
2630
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002631 def test_leak_fast_process_del_killed(self):
2632 # Issue #12650: on Unix, if Popen.__del__() was called before the
2633 # process exited, and the process got killed by a signal, it would never
2634 # be removed from subprocess._active, which triggered a FD and memory
2635 # leak.
2636 # spawn a Popen, delete its reference and kill it
2637 p = subprocess.Popen([sys.executable, "-c",
2638 'import time;'
2639 'time.sleep(3)'],
2640 stdout=subprocess.PIPE,
2641 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02002642 self.addCleanup(p.stdout.close)
2643 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002644 ident = id(p)
2645 pid = p.pid
Victor Stinner5a48e212016-05-20 12:11:15 +02002646 with support.check_warnings(('', ResourceWarning)):
2647 p = None
2648
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002649 os.kill(pid, signal.SIGKILL)
2650 # check that p is in the active processes list
2651 self.assertIn(ident, [id(o) for o in subprocess._active])
2652
2653 # let some time for the process to exit, and create a new Popen: this
2654 # should trigger the wait() of p
2655 time.sleep(0.2)
Victor Stinnerb31206a2018-01-25 19:06:05 +01002656 with self.assertRaises(OSError):
Victor Stinner9a83f652017-08-21 23:51:31 +02002657 with subprocess.Popen(NONEXISTING_CMD,
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002658 stdout=subprocess.PIPE,
2659 stderr=subprocess.PIPE) as proc:
2660 pass
2661 # p should have been wait()ed on, and removed from the _active list
2662 self.assertRaises(OSError, os.waitpid, pid, 0)
2663 self.assertNotIn(ident, [id(o) for o in subprocess._active])
2664
Charles-François Natali249cdc32013-08-25 18:24:45 +02002665 def test_close_fds_after_preexec(self):
2666 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2667
2668 # this FD is used as dup2() target by preexec_fn, and should be closed
2669 # in the child process
2670 fd = os.dup(1)
2671 self.addCleanup(os.close, fd)
2672
2673 p = subprocess.Popen([sys.executable, fd_status],
2674 stdout=subprocess.PIPE, close_fds=True,
2675 preexec_fn=lambda: os.dup2(1, fd))
2676 output, ignored = p.communicate()
2677
2678 remaining_fds = set(map(int, output.split(b',')))
2679
2680 self.assertNotIn(fd, remaining_fds)
2681
Victor Stinner8f437aa2014-10-05 17:25:19 +02002682 @support.cpython_only
2683 def test_fork_exec(self):
2684 # Issue #22290: fork_exec() must not crash on memory allocation failure
2685 # or other errors
2686 import _posixsubprocess
2687 gc_enabled = gc.isenabled()
2688 try:
2689 # Use a preexec function and enable the garbage collector
2690 # to force fork_exec() to re-enable the garbage collector
2691 # on error.
2692 func = lambda: None
2693 gc.enable()
2694
Victor Stinner8f437aa2014-10-05 17:25:19 +02002695 for args, exe_list, cwd, env_list in (
2696 (123, [b"exe"], None, [b"env"]),
2697 ([b"arg"], 123, None, [b"env"]),
2698 ([b"arg"], [b"exe"], 123, [b"env"]),
2699 ([b"arg"], [b"exe"], None, 123),
2700 ):
2701 with self.assertRaises(TypeError):
2702 _posixsubprocess.fork_exec(
2703 args, exe_list,
Serhiy Storchaka66bffd12017-04-19 21:12:46 +03002704 True, (), cwd, env_list,
Victor Stinner8f437aa2014-10-05 17:25:19 +02002705 -1, -1, -1, -1,
2706 1, 2, 3, 4,
2707 True, True, func)
2708 finally:
2709 if not gc_enabled:
2710 gc.disable()
2711
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002712 @support.cpython_only
2713 def test_fork_exec_sorted_fd_sanity_check(self):
2714 # Issue #23564: sanity check the fork_exec() fds_to_keep sanity check.
2715 import _posixsubprocess
Serhiy Storchaka66bffd12017-04-19 21:12:46 +03002716 class BadInt:
2717 first = True
2718 def __init__(self, value):
2719 self.value = value
2720 def __int__(self):
2721 if self.first:
2722 self.first = False
2723 return self.value
2724 raise ValueError
2725
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002726 gc_enabled = gc.isenabled()
2727 try:
2728 gc.enable()
2729
2730 for fds_to_keep in (
2731 (-1, 2, 3, 4, 5), # Negative number.
2732 ('str', 4), # Not an int.
2733 (18, 23, 42, 2**63), # Out of range.
2734 (5, 4), # Not sorted.
2735 (6, 7, 7, 8), # Duplicate.
Serhiy Storchaka66bffd12017-04-19 21:12:46 +03002736 (BadInt(1), BadInt(2)),
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002737 ):
2738 with self.assertRaises(
2739 ValueError,
2740 msg='fds_to_keep={}'.format(fds_to_keep)) as c:
2741 _posixsubprocess.fork_exec(
2742 [b"false"], [b"false"],
2743 True, fds_to_keep, None, [b"env"],
2744 -1, -1, -1, -1,
2745 1, 2, 3, 4,
2746 True, True, None)
2747 self.assertIn('fds_to_keep', str(c.exception))
2748 finally:
2749 if not gc_enabled:
2750 gc.disable()
Victor Stinner8f437aa2014-10-05 17:25:19 +02002751
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)2daf8e72016-06-05 02:57:47 +00002752 def test_communicate_BrokenPipeError_stdin_close(self):
2753 # By not setting stdout or stderr or a timeout we force the fast path
2754 # that just calls _stdin_write() internally due to our mock.
2755 proc = subprocess.Popen([sys.executable, '-c', 'pass'])
2756 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
2757 mock_proc_stdin.close.side_effect = BrokenPipeError
2758 proc.communicate() # Should swallow BrokenPipeError from close.
2759 mock_proc_stdin.close.assert_called_with()
2760
2761 def test_communicate_BrokenPipeError_stdin_write(self):
2762 # By not setting stdout or stderr or a timeout we force the fast path
2763 # that just calls _stdin_write() internally due to our mock.
2764 proc = subprocess.Popen([sys.executable, '-c', 'pass'])
2765 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
2766 mock_proc_stdin.write.side_effect = BrokenPipeError
2767 proc.communicate(b'stuff') # Should swallow the BrokenPipeError.
2768 mock_proc_stdin.write.assert_called_once_with(b'stuff')
2769 mock_proc_stdin.close.assert_called_once_with()
2770
2771 def test_communicate_BrokenPipeError_stdin_flush(self):
2772 # Setting stdin and stdout forces the ._communicate() code path.
2773 # python -h exits faster than python -c pass (but spams stdout).
2774 proc = subprocess.Popen([sys.executable, '-h'],
2775 stdin=subprocess.PIPE,
2776 stdout=subprocess.PIPE)
2777 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin, \
2778 open(os.devnull, 'wb') as dev_null:
2779 mock_proc_stdin.flush.side_effect = BrokenPipeError
2780 # because _communicate registers a selector using proc.stdin...
2781 mock_proc_stdin.fileno.return_value = dev_null.fileno()
2782 # _communicate() should swallow BrokenPipeError from flush.
2783 proc.communicate(b'stuff')
2784 mock_proc_stdin.flush.assert_called_once_with()
2785
2786 def test_communicate_BrokenPipeError_stdin_close_with_timeout(self):
2787 # Setting stdin and stdout forces the ._communicate() code path.
2788 # python -h exits faster than python -c pass (but spams stdout).
2789 proc = subprocess.Popen([sys.executable, '-h'],
2790 stdin=subprocess.PIPE,
2791 stdout=subprocess.PIPE)
2792 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
2793 mock_proc_stdin.close.side_effect = BrokenPipeError
2794 # _communicate() should swallow BrokenPipeError from close.
2795 proc.communicate(timeout=999)
2796 mock_proc_stdin.close.assert_called_once_with()
2797
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002798 @unittest.skipUnless(_testcapi is not None
2799 and hasattr(_testcapi, 'W_STOPCODE'),
2800 'need _testcapi.W_STOPCODE')
2801 def test_stopped(self):
Gregory P. Smith50e16e32017-01-22 17:28:38 -08002802 """Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002803 args = [sys.executable, '-c', 'pass']
2804 proc = subprocess.Popen(args)
Victor Stinnercdee3f12017-06-26 17:23:03 +02002805
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002806 # Wait until the real process completes to avoid zombie process
2807 pid = proc.pid
2808 pid, status = os.waitpid(pid, 0)
2809 self.assertEqual(status, 0)
Victor Stinnercdee3f12017-06-26 17:23:03 +02002810
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002811 status = _testcapi.W_STOPCODE(3)
2812 with mock.patch('subprocess.os.waitpid', return_value=(pid, status)):
2813 returncode = proc.wait()
Victor Stinnercdee3f12017-06-26 17:23:03 +02002814
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002815 self.assertEqual(returncode, -3)
Gregory P. Smith50e16e32017-01-22 17:28:38 -08002816
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002817
Victor Stinner937ee9e2018-06-26 02:11:06 +02002818@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +00002819class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00002820
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002821 def test_startupinfo(self):
2822 # startupinfo argument
2823 # We uses hardcoded constants, because we do not want to
2824 # depend on win32all.
2825 STARTF_USESHOWWINDOW = 1
2826 SW_MAXIMIZE = 3
2827 startupinfo = subprocess.STARTUPINFO()
2828 startupinfo.dwFlags = STARTF_USESHOWWINDOW
2829 startupinfo.wShowWindow = SW_MAXIMIZE
2830 # Since Python is a console process, it won't be affected
2831 # by wShowWindow, but the argument should be silently
2832 # ignored
2833 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002834 startupinfo=startupinfo)
2835
Subhendu Ghoshae160bb2017-02-25 20:29:05 +05302836 def test_startupinfo_keywords(self):
2837 # startupinfo argument
2838 # We use hardcoded constants, because we do not want to
2839 # depend on win32all.
2840 STARTF_USERSHOWWINDOW = 1
2841 SW_MAXIMIZE = 3
2842 startupinfo = subprocess.STARTUPINFO(
2843 dwFlags=STARTF_USERSHOWWINDOW,
2844 wShowWindow=SW_MAXIMIZE
2845 )
2846 # Since Python is a console process, it won't be affected
2847 # by wShowWindow, but the argument should be silently
2848 # ignored
2849 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
2850 startupinfo=startupinfo)
2851
Victor Stinner483422f2018-07-05 22:54:17 +02002852 def test_startupinfo_copy(self):
2853 # bpo-34044: Popen must not modify input STARTUPINFO structure
2854 startupinfo = subprocess.STARTUPINFO()
2855 startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW
2856 startupinfo.wShowWindow = subprocess.SW_HIDE
2857
2858 # Call Popen() twice with the same startupinfo object to make sure
2859 # that it's not modified
2860 for _ in range(2):
2861 cmd = [sys.executable, "-c", "pass"]
2862 with open(os.devnull, 'w') as null:
2863 proc = subprocess.Popen(cmd,
2864 stdout=null,
2865 stderr=subprocess.STDOUT,
2866 startupinfo=startupinfo)
2867 with proc:
2868 proc.communicate()
2869 self.assertEqual(proc.returncode, 0)
2870
2871 self.assertEqual(startupinfo.dwFlags,
2872 subprocess.STARTF_USESHOWWINDOW)
2873 self.assertIsNone(startupinfo.hStdInput)
2874 self.assertIsNone(startupinfo.hStdOutput)
2875 self.assertIsNone(startupinfo.hStdError)
2876 self.assertEqual(startupinfo.wShowWindow, subprocess.SW_HIDE)
2877 self.assertEqual(startupinfo.lpAttributeList, {"handle_list": []})
2878
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002879 def test_creationflags(self):
2880 # creationflags argument
2881 CREATE_NEW_CONSOLE = 16
2882 sys.stderr.write(" a DOS box should flash briefly ...\n")
2883 subprocess.call(sys.executable +
2884 ' -c "import time; time.sleep(0.25)"',
2885 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002886
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002887 def test_invalid_args(self):
2888 # invalid arguments should raise ValueError
2889 self.assertRaises(ValueError, subprocess.call,
2890 [sys.executable, "-c",
2891 "import sys; sys.exit(47)"],
2892 preexec_fn=lambda: 1)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002893
Oren Milman0b3a87e2017-09-14 22:30:28 +03002894 @support.cpython_only
2895 def test_issue31471(self):
2896 # There shouldn't be an assertion failure in Popen() in case the env
2897 # argument has a bad keys() method.
2898 class BadEnv(dict):
2899 keys = None
2900 with self.assertRaises(TypeError):
2901 subprocess.Popen([sys.executable, "-c", "pass"], env=BadEnv())
2902
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002903 def test_close_fds(self):
2904 # close file descriptors
2905 rc = subprocess.call([sys.executable, "-c",
2906 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002907 close_fds=True)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002908 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002909
Segev Finerb2a60832017-12-18 11:28:19 +02002910 def test_close_fds_with_stdio(self):
2911 import msvcrt
2912
2913 fds = os.pipe()
2914 self.addCleanup(os.close, fds[0])
2915 self.addCleanup(os.close, fds[1])
2916
2917 handles = []
2918 for fd in fds:
2919 os.set_inheritable(fd, True)
2920 handles.append(msvcrt.get_osfhandle(fd))
2921
2922 p = subprocess.Popen([sys.executable, "-c",
2923 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2924 stdout=subprocess.PIPE, close_fds=False)
2925 stdout, stderr = p.communicate()
2926 self.assertEqual(p.returncode, 0)
2927 int(stdout.strip()) # Check that stdout is an integer
2928
2929 p = subprocess.Popen([sys.executable, "-c",
2930 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2931 stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
2932 stdout, stderr = p.communicate()
2933 self.assertEqual(p.returncode, 1)
2934 self.assertIn(b"OSError", stderr)
2935
2936 # The same as the previous call, but with an empty handle_list
2937 handle_list = []
2938 startupinfo = subprocess.STARTUPINFO()
2939 startupinfo.lpAttributeList = {"handle_list": handle_list}
2940 p = subprocess.Popen([sys.executable, "-c",
2941 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2942 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
2943 startupinfo=startupinfo, close_fds=True)
2944 stdout, stderr = p.communicate()
2945 self.assertEqual(p.returncode, 1)
2946 self.assertIn(b"OSError", stderr)
2947
2948 # Check for a warning due to using handle_list and close_fds=False
2949 with support.check_warnings((".*overriding close_fds", RuntimeWarning)):
2950 startupinfo = subprocess.STARTUPINFO()
2951 startupinfo.lpAttributeList = {"handle_list": handles[:]}
2952 p = subprocess.Popen([sys.executable, "-c",
2953 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2954 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
2955 startupinfo=startupinfo, close_fds=False)
2956 stdout, stderr = p.communicate()
2957 self.assertEqual(p.returncode, 0)
2958
2959 def test_empty_attribute_list(self):
2960 startupinfo = subprocess.STARTUPINFO()
2961 startupinfo.lpAttributeList = {}
2962 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
2963 startupinfo=startupinfo)
2964
2965 def test_empty_handle_list(self):
2966 startupinfo = subprocess.STARTUPINFO()
2967 startupinfo.lpAttributeList = {"handle_list": []}
2968 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
2969 startupinfo=startupinfo)
2970
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002971 def test_shell_sequence(self):
2972 # Run command through the shell (sequence)
2973 newenv = os.environ.copy()
2974 newenv["FRUIT"] = "physalis"
2975 p = subprocess.Popen(["set"], shell=1,
2976 stdout=subprocess.PIPE,
2977 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02002978 with p:
2979 self.assertIn(b"physalis", p.stdout.read())
Guido van Rossume7ba4952007-06-06 23:52:48 +00002980
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002981 def test_shell_string(self):
2982 # Run command through the shell (string)
2983 newenv = os.environ.copy()
2984 newenv["FRUIT"] = "physalis"
2985 p = subprocess.Popen("set", shell=1,
2986 stdout=subprocess.PIPE,
2987 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02002988 with p:
2989 self.assertIn(b"physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002990
Steve Dower050acae2016-09-06 20:16:17 -07002991 def test_shell_encodings(self):
2992 # Run command through the shell (string)
2993 for enc in ['ansi', 'oem']:
2994 newenv = os.environ.copy()
2995 newenv["FRUIT"] = "physalis"
2996 p = subprocess.Popen("set", shell=1,
2997 stdout=subprocess.PIPE,
2998 env=newenv,
2999 encoding=enc)
3000 with p:
3001 self.assertIn("physalis", p.stdout.read(), enc)
3002
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003003 def test_call_string(self):
3004 # call() function with string argument on Windows
3005 rc = subprocess.call(sys.executable +
3006 ' -c "import sys; sys.exit(47)"')
3007 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00003008
Florent Xicluna4886d242010-03-08 13:27:26 +00003009 def _kill_process(self, method, *args):
3010 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroua4024e22010-09-24 18:57:01 +00003011 p = subprocess.Popen([sys.executable, "-c", """if 1:
3012 import sys, time
3013 sys.stdout.write('x\\n')
3014 sys.stdout.flush()
3015 time.sleep(30)
3016 """],
3017 stdin=subprocess.PIPE,
3018 stdout=subprocess.PIPE,
3019 stderr=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +02003020 with p:
3021 # Wait for the interpreter to be completely initialized before
3022 # sending any signal.
3023 p.stdout.read(1)
3024 getattr(p, method)(*args)
3025 _, stderr = p.communicate()
3026 self.assertStderrEqual(stderr, b'')
3027 returncode = p.wait()
Florent Xicluna4886d242010-03-08 13:27:26 +00003028 self.assertNotEqual(returncode, 0)
3029
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01003030 def _kill_dead_process(self, method, *args):
3031 p = subprocess.Popen([sys.executable, "-c", """if 1:
3032 import sys, time
3033 sys.stdout.write('x\\n')
3034 sys.stdout.flush()
3035 sys.exit(42)
3036 """],
3037 stdin=subprocess.PIPE,
3038 stdout=subprocess.PIPE,
3039 stderr=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +02003040 with p:
3041 # Wait for the interpreter to be completely initialized before
3042 # sending any signal.
3043 p.stdout.read(1)
3044 # The process should end after this
3045 time.sleep(1)
3046 # This shouldn't raise even though the child is now dead
3047 getattr(p, method)(*args)
3048 _, stderr = p.communicate()
3049 self.assertStderrEqual(stderr, b'')
3050 rc = p.wait()
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01003051 self.assertEqual(rc, 42)
3052
Florent Xicluna4886d242010-03-08 13:27:26 +00003053 def test_send_signal(self):
3054 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimesa342c012008-04-20 21:01:16 +00003055
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003056 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00003057 self._kill_process('kill')
Christian Heimesa342c012008-04-20 21:01:16 +00003058
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003059 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00003060 self._kill_process('terminate')
Christian Heimesa342c012008-04-20 21:01:16 +00003061
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01003062 def test_send_signal_dead(self):
3063 self._kill_dead_process('send_signal', signal.SIGTERM)
3064
3065 def test_kill_dead(self):
3066 self._kill_dead_process('kill')
3067
3068 def test_terminate_dead(self):
3069 self._kill_dead_process('terminate')
3070
Martin Panter23172bd2016-04-16 11:28:10 +00003071class MiscTests(unittest.TestCase):
Gregory P. Smithf4d644f2018-01-29 21:27:39 -08003072
3073 class RecordingPopen(subprocess.Popen):
3074 """A Popen that saves a reference to each instance for testing."""
3075 instances_created = []
3076
3077 def __init__(self, *args, **kwargs):
3078 super().__init__(*args, **kwargs)
3079 self.instances_created.append(self)
3080
3081 @mock.patch.object(subprocess.Popen, "_communicate")
3082 def _test_keyboardinterrupt_no_kill(self, popener, mock__communicate,
3083 **kwargs):
3084 """Fake a SIGINT happening during Popen._communicate() and ._wait().
3085
3086 This avoids the need to actually try and get test environments to send
3087 and receive signals reliably across platforms. The net effect of a ^C
3088 happening during a blocking subprocess execution which we want to clean
3089 up from is a KeyboardInterrupt coming out of communicate() or wait().
3090 """
3091
3092 mock__communicate.side_effect = KeyboardInterrupt
3093 try:
3094 with mock.patch.object(subprocess.Popen, "_wait") as mock__wait:
3095 # We patch out _wait() as no signal was involved so the
3096 # child process isn't actually going to exit rapidly.
3097 mock__wait.side_effect = KeyboardInterrupt
3098 with mock.patch.object(subprocess, "Popen",
3099 self.RecordingPopen):
3100 with self.assertRaises(KeyboardInterrupt):
3101 popener([sys.executable, "-c",
3102 "import time\ntime.sleep(9)\nimport sys\n"
3103 "sys.stderr.write('\\n!runaway child!\\n')"],
3104 stdout=subprocess.DEVNULL, **kwargs)
3105 for call in mock__wait.call_args_list[1:]:
3106 self.assertNotEqual(
3107 call, mock.call(timeout=None),
3108 "no open-ended wait() after the first allowed: "
3109 f"{mock__wait.call_args_list}")
3110 sigint_calls = []
3111 for call in mock__wait.call_args_list:
3112 if call == mock.call(timeout=0.25): # from Popen.__init__
3113 sigint_calls.append(call)
3114 self.assertLessEqual(mock__wait.call_count, 2,
3115 msg=mock__wait.call_args_list)
3116 self.assertEqual(len(sigint_calls), 1,
3117 msg=mock__wait.call_args_list)
3118 finally:
3119 # cleanup the forgotten (due to our mocks) child process
3120 process = self.RecordingPopen.instances_created.pop()
3121 process.kill()
3122 process.wait()
3123 self.assertEqual([], self.RecordingPopen.instances_created)
3124
3125 def test_call_keyboardinterrupt_no_kill(self):
3126 self._test_keyboardinterrupt_no_kill(subprocess.call, timeout=6.282)
3127
3128 def test_run_keyboardinterrupt_no_kill(self):
3129 self._test_keyboardinterrupt_no_kill(subprocess.run, timeout=6.282)
3130
3131 def test_context_manager_keyboardinterrupt_no_kill(self):
3132 def popen_via_context_manager(*args, **kwargs):
3133 with subprocess.Popen(*args, **kwargs) as unused_process:
3134 raise KeyboardInterrupt # Test how __exit__ handles ^C.
3135 self._test_keyboardinterrupt_no_kill(popen_via_context_manager)
3136
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003137 def test_getoutput(self):
3138 self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
3139 self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
3140 (0, 'xyzzy'))
Brett Cannona23810f2008-05-26 19:04:21 +00003141
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003142 # we use mkdtemp in the next line to create an empty directory
3143 # under our exclusive control; from that, we can invent a pathname
3144 # that we _know_ won't exist. This is guaranteed to fail.
3145 dir = None
3146 try:
3147 dir = tempfile.mkdtemp()
3148 name = os.path.join(dir, "foo")
Tim Goldene0041752013-11-03 12:53:17 +00003149 status, output = subprocess.getstatusoutput(
Victor Stinner937ee9e2018-06-26 02:11:06 +02003150 ("type " if mswindows else "cat ") + name)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003151 self.assertNotEqual(status, 0)
3152 finally:
3153 if dir is not None:
3154 os.rmdir(dir)
Brett Cannona23810f2008-05-26 19:04:21 +00003155
Gregory P. Smithace55862015-04-07 15:57:54 -07003156 def test__all__(self):
3157 """Ensure that __all__ is populated properly."""
Martin Panter528619b2016-04-16 23:42:37 +00003158 intentionally_excluded = {"list2cmdline", "Handle"}
Gregory P. Smithace55862015-04-07 15:57:54 -07003159 exported = set(subprocess.__all__)
3160 possible_exports = set()
3161 import types
3162 for name, value in subprocess.__dict__.items():
3163 if name.startswith('_'):
3164 continue
3165 if isinstance(value, (types.ModuleType,)):
3166 continue
3167 possible_exports.add(name)
3168 self.assertEqual(exported, possible_exports - intentionally_excluded)
3169
3170
Martin Panter23172bd2016-04-16 11:28:10 +00003171@unittest.skipUnless(hasattr(selectors, 'PollSelector'),
3172 "Test needs selectors.PollSelector")
3173class ProcessTestCaseNoPoll(ProcessTestCase):
3174 def setUp(self):
3175 self.orig_selector = subprocess._PopenSelector
3176 subprocess._PopenSelector = selectors.SelectSelector
3177 ProcessTestCase.setUp(self)
3178
3179 def tearDown(self):
3180 subprocess._PopenSelector = self.orig_selector
3181 ProcessTestCase.tearDown(self)
3182
Gregory P. Smithd06fa472009-07-04 02:46:54 +00003183
Victor Stinner937ee9e2018-06-26 02:11:06 +02003184@unittest.skipUnless(mswindows, "Windows-specific tests")
Tim Golden126c2962010-08-11 14:20:40 +00003185class CommandsWithSpaces (BaseTestCase):
3186
3187 def setUp(self):
3188 super().setUp()
Berker Peksag16a1f282015-09-28 13:33:14 +03003189 f, fname = tempfile.mkstemp(".py", "te st")
Tim Golden126c2962010-08-11 14:20:40 +00003190 self.fname = fname.lower ()
3191 os.write(f, b"import sys;"
3192 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
3193 )
3194 os.close(f)
3195
3196 def tearDown(self):
3197 os.remove(self.fname)
3198 super().tearDown()
3199
3200 def with_spaces(self, *args, **kwargs):
3201 kwargs['stdout'] = subprocess.PIPE
3202 p = subprocess.Popen(*args, **kwargs)
Victor Stinner7438c612016-05-20 12:43:15 +02003203 with p:
3204 self.assertEqual(
3205 p.stdout.read ().decode("mbcs"),
3206 "2 [%r, 'ab cd']" % self.fname
3207 )
Tim Golden126c2962010-08-11 14:20:40 +00003208
3209 def test_shell_string_with_spaces(self):
3210 # call() function with string argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00003211 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
3212 "ab cd"), shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00003213
3214 def test_shell_sequence_with_spaces(self):
3215 # call() function with sequence argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00003216 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00003217
3218 def test_noshell_string_with_spaces(self):
3219 # call() function with string argument with spaces on Windows
3220 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
3221 "ab cd"))
3222
3223 def test_noshell_sequence_with_spaces(self):
3224 # call() function with sequence argument with spaces on Windows
3225 self.with_spaces([sys.executable, self.fname, "ab cd"])
3226
Brian Curtin79cdb662010-12-03 02:46:02 +00003227
Georg Brandla86b2622012-02-20 21:34:57 +01003228class ContextManagerTests(BaseTestCase):
Brian Curtin79cdb662010-12-03 02:46:02 +00003229
3230 def test_pipe(self):
3231 with subprocess.Popen([sys.executable, "-c",
3232 "import sys;"
3233 "sys.stdout.write('stdout');"
3234 "sys.stderr.write('stderr');"],
3235 stdout=subprocess.PIPE,
3236 stderr=subprocess.PIPE) as proc:
3237 self.assertEqual(proc.stdout.read(), b"stdout")
3238 self.assertStderrEqual(proc.stderr.read(), b"stderr")
3239
3240 self.assertTrue(proc.stdout.closed)
3241 self.assertTrue(proc.stderr.closed)
3242
3243 def test_returncode(self):
3244 with subprocess.Popen([sys.executable, "-c",
3245 "import sys; sys.exit(100)"]) as proc:
Gregory P. Smith6b657452011-05-11 21:42:08 -07003246 pass
3247 # __exit__ calls wait(), so the returncode should be set
Brian Curtin79cdb662010-12-03 02:46:02 +00003248 self.assertEqual(proc.returncode, 100)
3249
3250 def test_communicate_stdin(self):
3251 with subprocess.Popen([sys.executable, "-c",
3252 "import sys;"
3253 "sys.exit(sys.stdin.read() == 'context')"],
3254 stdin=subprocess.PIPE) as proc:
3255 proc.communicate(b"context")
3256 self.assertEqual(proc.returncode, 1)
3257
3258 def test_invalid_args(self):
Victor Stinnerb31206a2018-01-25 19:06:05 +01003259 with self.assertRaises(NONEXISTING_ERRORS):
Victor Stinner9a83f652017-08-21 23:51:31 +02003260 with subprocess.Popen(NONEXISTING_CMD,
Brian Curtin79cdb662010-12-03 02:46:02 +00003261 stdout=subprocess.PIPE,
3262 stderr=subprocess.PIPE) as proc:
3263 pass
3264
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003265 def test_broken_pipe_cleanup(self):
3266 """Broken pipe error should not prevent wait() (Issue 21619)"""
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003267 proc = subprocess.Popen([sys.executable, '-c', 'pass'],
Victor Stinner20f4bd42015-03-05 02:38:41 +01003268 stdin=subprocess.PIPE,
Victor Stinner20f4bd42015-03-05 02:38:41 +01003269 bufsize=support.PIPE_MAX_SIZE*2)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003270 proc = proc.__enter__()
3271 # Prepare to send enough data to overflow any OS pipe buffering and
3272 # guarantee a broken pipe error. Data is held in BufferedWriter
3273 # buffer until closed.
3274 proc.stdin.write(b'x' * support.PIPE_MAX_SIZE)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003275 self.assertIsNone(proc.returncode)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003276 # EPIPE expected under POSIX; EINVAL under Windows
Serhiy Storchakacf265fd2015-02-28 13:27:54 +02003277 self.assertRaises(OSError, proc.__exit__, None, None, None)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003278 self.assertEqual(proc.returncode, 0)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003279 self.assertTrue(proc.stdin.closed)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003280
Brian Curtin79cdb662010-12-03 02:46:02 +00003281
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00003282if __name__ == "__main__":
Gregory P. Smith112bb3a2011-03-15 14:55:17 -04003283 unittest.main()