blob: 569e3e0aa559b259d325c261ddeb7be734e92a39 [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
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000038#
39# Depends on the following external programs: Python
40#
41
Victor Stinner8fbbdf02018-06-22 19:25:44 +020042if support.MS_WINDOWS:
Tim Peters3b01a702004-10-12 22:19:32 +000043 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
44 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000045else:
46 SETBINARY = ''
47
Victor Stinner9a83f652017-08-21 23:51:31 +020048NONEXISTING_CMD = ('nonexisting_i_hope',)
Victor Stinnerb31206a2018-01-25 19:06:05 +010049# Ignore errors that indicate the command was not found
50NONEXISTING_ERRORS = (FileNotFoundError, NotADirectoryError, PermissionError)
Victor Stinner9a83f652017-08-21 23:51:31 +020051
Florent Xiclunab1e94e82010-02-27 22:12:37 +000052
Florent Xiclunac049d872010-03-27 22:47:23 +000053class BaseTestCase(unittest.TestCase):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000054 def setUp(self):
55 # Try to minimize the number of children we have so this test
56 # doesn't crash on some buildbots (Alphas in particular).
Florent Xiclunab1e94e82010-02-27 22:12:37 +000057 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058
Florent Xiclunaf0cbd822010-03-04 21:50:56 +000059 def tearDown(self):
60 for inst in subprocess._active:
61 inst.wait()
62 subprocess._cleanup()
63 self.assertFalse(subprocess._active, "subprocess._active not empty")
Victor Stinnercc42c122017-07-28 18:00:22 +020064 self.doCleanups()
65 support.reap_children()
Florent Xiclunaf0cbd822010-03-04 21:50:56 +000066
Florent Xiclunab1e94e82010-02-27 22:12:37 +000067 def assertStderrEqual(self, stderr, expected, msg=None):
68 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
69 # shutdown time. That frustrates tests trying to check stderr produced
70 # from a spawned Python process.
Antoine Pitrou62f68ed2010-08-04 11:48:56 +000071 actual = support.strip_python_stderr(stderr)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -040072 # strip_python_stderr also strips whitespace, so we do too.
73 expected = expected.strip()
Florent Xiclunab1e94e82010-02-27 22:12:37 +000074 self.assertEqual(actual, expected, msg)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075
Florent Xiclunac049d872010-03-27 22:47:23 +000076
Gregory P. Smith3d8e7762012-11-10 22:32:22 -080077class PopenTestException(Exception):
78 pass
79
80
81class PopenExecuteChildRaises(subprocess.Popen):
82 """Popen subclass for testing cleanup of subprocess.PIPE filehandles when
83 _execute_child fails.
84 """
85 def _execute_child(self, *args, **kwargs):
86 raise PopenTestException("Forced Exception for Test")
87
88
Florent Xiclunac049d872010-03-27 22:47:23 +000089class ProcessTestCase(BaseTestCase):
90
Gregory P. Smitha1ed5392013-03-23 11:44:25 -070091 def test_io_buffered_by_default(self):
92 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
93 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
94 stderr=subprocess.PIPE)
95 try:
96 self.assertIsInstance(p.stdin, io.BufferedIOBase)
97 self.assertIsInstance(p.stdout, io.BufferedIOBase)
98 self.assertIsInstance(p.stderr, io.BufferedIOBase)
99 finally:
Gregory P. Smitha1b9ed32013-03-23 11:54:22 -0700100 p.stdin.close()
101 p.stdout.close()
102 p.stderr.close()
Gregory P. Smitha1ed5392013-03-23 11:44:25 -0700103 p.wait()
104
105 def test_io_unbuffered_works(self):
106 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
107 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
108 stderr=subprocess.PIPE, bufsize=0)
109 try:
110 self.assertIsInstance(p.stdin, io.RawIOBase)
111 self.assertIsInstance(p.stdout, io.RawIOBase)
112 self.assertIsInstance(p.stderr, io.RawIOBase)
113 finally:
Gregory P. Smitha1b9ed32013-03-23 11:54:22 -0700114 p.stdin.close()
115 p.stdout.close()
116 p.stderr.close()
Gregory P. Smitha1ed5392013-03-23 11:44:25 -0700117 p.wait()
118
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000119 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000120 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +0000121 rc = subprocess.call([sys.executable, "-c",
122 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000123 self.assertEqual(rc, 47)
124
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400125 def test_call_timeout(self):
126 # call() function with timeout argument; we want to test that the child
127 # process gets killed when the timeout expires. If the child isn't
128 # killed, this call will deadlock since subprocess.call waits for the
129 # child.
130 self.assertRaises(subprocess.TimeoutExpired, subprocess.call,
131 [sys.executable, "-c", "while True: pass"],
132 timeout=0.1)
133
Peter Astrand454f7672005-01-01 09:36:35 +0000134 def test_check_call_zero(self):
135 # check_call() function with zero return code
136 rc = subprocess.check_call([sys.executable, "-c",
137 "import sys; sys.exit(0)"])
138 self.assertEqual(rc, 0)
139
140 def test_check_call_nonzero(self):
141 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000142 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +0000143 subprocess.check_call([sys.executable, "-c",
144 "import sys; sys.exit(47)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000145 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +0000146
Georg Brandlf9734072008-12-07 15:30:06 +0000147 def test_check_output(self):
148 # check_output() function with zero return code
149 output = subprocess.check_output(
150 [sys.executable, "-c", "print('BDFL')"])
Benjamin Peterson577473f2010-01-19 00:09:57 +0000151 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +0000152
153 def test_check_output_nonzero(self):
154 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000155 with self.assertRaises(subprocess.CalledProcessError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +0000156 subprocess.check_output(
157 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000158 self.assertEqual(c.exception.returncode, 5)
Georg Brandlf9734072008-12-07 15:30:06 +0000159
160 def test_check_output_stderr(self):
161 # check_output() function stderr redirected to stdout
162 output = subprocess.check_output(
163 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
164 stderr=subprocess.STDOUT)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000165 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +0000166
Serhiy Storchakafcd9f222013-04-22 20:20:54 +0300167 def test_check_output_stdin_arg(self):
168 # check_output() can be called with stdin set to a file
169 tf = tempfile.TemporaryFile()
170 self.addCleanup(tf.close)
171 tf.write(b'pear')
172 tf.seek(0)
173 output = subprocess.check_output(
174 [sys.executable, "-c",
175 "import sys; sys.stdout.write(sys.stdin.read().upper())"],
176 stdin=tf)
177 self.assertIn(b'PEAR', output)
178
179 def test_check_output_input_arg(self):
180 # check_output() can be called with input set to a string
181 output = subprocess.check_output(
182 [sys.executable, "-c",
183 "import sys; sys.stdout.write(sys.stdin.read().upper())"],
184 input=b'pear')
185 self.assertIn(b'PEAR', output)
186
Georg Brandlf9734072008-12-07 15:30:06 +0000187 def test_check_output_stdout_arg(self):
Serhiy Storchakafcd9f222013-04-22 20:20:54 +0300188 # check_output() refuses to accept 'stdout' argument
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000189 with self.assertRaises(ValueError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +0000190 output = subprocess.check_output(
191 [sys.executable, "-c", "print('will not be run')"],
192 stdout=sys.stdout)
Georg Brandlf9734072008-12-07 15:30:06 +0000193 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000194 self.assertIn('stdout', c.exception.args[0])
Georg Brandlf9734072008-12-07 15:30:06 +0000195
Serhiy Storchakafcd9f222013-04-22 20:20:54 +0300196 def test_check_output_stdin_with_input_arg(self):
197 # check_output() refuses to accept 'stdin' with 'input'
198 tf = tempfile.TemporaryFile()
199 self.addCleanup(tf.close)
200 tf.write(b'pear')
201 tf.seek(0)
202 with self.assertRaises(ValueError) as c:
203 output = subprocess.check_output(
204 [sys.executable, "-c", "print('will not be run')"],
205 stdin=tf, input=b'hare')
206 self.fail("Expected ValueError when stdin and input args supplied.")
207 self.assertIn('stdin', c.exception.args[0])
208 self.assertIn('input', c.exception.args[0])
209
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400210 def test_check_output_timeout(self):
211 # check_output() function with timeout arg
212 with self.assertRaises(subprocess.TimeoutExpired) as c:
213 output = subprocess.check_output(
214 [sys.executable, "-c",
Victor Stinner149b1c72011-06-06 23:43:02 +0200215 "import sys, time\n"
216 "sys.stdout.write('BDFL')\n"
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400217 "sys.stdout.flush()\n"
Victor Stinner149b1c72011-06-06 23:43:02 +0200218 "time.sleep(3600)"],
Reid Klecknerda9ac722011-03-16 17:08:21 -0400219 # Some heavily loaded buildbots (sparc Debian 3.x) require
220 # this much time to start and print.
221 timeout=3)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400222 self.fail("Expected TimeoutExpired.")
223 self.assertEqual(c.exception.output, b'BDFL')
224
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000225 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000226 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000227 newenv = os.environ.copy()
228 newenv["FRUIT"] = "banana"
229 rc = subprocess.call([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000230 'import sys, os;'
231 'sys.exit(os.getenv("FRUIT")=="banana")'],
232 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000233 self.assertEqual(rc, 1)
234
Victor Stinner87b9bc32011-06-01 00:57:47 +0200235 def test_invalid_args(self):
236 # Popen() called with invalid arguments should raise TypeError
237 # but Popen.__del__ should not complain (issue #12085)
238 with support.captured_stderr() as s:
239 self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
240 argcount = subprocess.Popen.__init__.__code__.co_argcount
241 too_many_args = [0] * (argcount + 1)
242 self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
243 self.assertEqual(s.getvalue(), '')
244
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000245 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000246 # .stdin is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000247 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000248 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000249 self.addCleanup(p.stdout.close)
250 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000251 p.wait()
252 self.assertEqual(p.stdin, None)
253
254 def test_stdout_none(self):
Ezio Melotti42a541b2013-03-11 05:53:34 +0200255 # .stdout is None when not redirected, and the child's stdout will
256 # be inherited from the parent. In order to test this we run a
257 # subprocess in a subprocess:
258 # this_test
259 # \-- subprocess created by this test (parent)
260 # \-- subprocess created by the parent subprocess (child)
261 # The parent doesn't specify stdout, so the child will use the
262 # parent's stdout. This test checks that the message printed by the
263 # child goes to the parent stdout. The parent also checks that the
264 # child's stdout is None. See #11963.
265 code = ('import sys; from subprocess import Popen, PIPE;'
266 'p = Popen([sys.executable, "-c", "print(\'test_stdout_none\')"],'
267 ' stdin=PIPE, stderr=PIPE);'
268 'p.wait(); assert p.stdout is None;')
269 p = subprocess.Popen([sys.executable, "-c", code],
270 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
271 self.addCleanup(p.stdout.close)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000272 self.addCleanup(p.stderr.close)
Ezio Melotti42a541b2013-03-11 05:53:34 +0200273 out, err = p.communicate()
274 self.assertEqual(p.returncode, 0, err)
275 self.assertEqual(out.rstrip(), b'test_stdout_none')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000276
277 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000278 # .stderr is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000279 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000280 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000281 self.addCleanup(p.stdout.close)
282 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000283 p.wait()
284 self.assertEqual(p.stderr, None)
285
Chris Jerdonek776cb192012-10-08 15:56:43 -0700286 def _assert_python(self, pre_args, **kwargs):
287 # We include sys.exit() to prevent the test runner from hanging
288 # whenever python is found.
289 args = pre_args + ["import sys; sys.exit(47)"]
290 p = subprocess.Popen(args, **kwargs)
291 p.wait()
292 self.assertEqual(47, p.returncode)
293
294 def test_executable(self):
295 # Check that the executable argument works.
Chris Jerdonek86b0fb22012-10-09 13:17:49 -0700296 #
297 # On Unix (non-Mac and non-Windows), Python looks at args[0] to
298 # determine where its standard library is, so we need the directory
299 # of args[0] to be valid for the Popen() call to Python to succeed.
300 # See also issue #16170 and issue #7774.
301 doesnotexist = os.path.join(os.path.dirname(sys.executable),
302 "doesnotexist")
303 self._assert_python([doesnotexist, "-c"], executable=sys.executable)
Chris Jerdonek776cb192012-10-08 15:56:43 -0700304
305 def test_executable_takes_precedence(self):
306 # Check that the executable argument takes precedence over args[0].
307 #
308 # Verify first that the call succeeds without the executable arg.
309 pre_args = [sys.executable, "-c"]
310 self._assert_python(pre_args)
Victor Stinnerb31206a2018-01-25 19:06:05 +0100311 self.assertRaises(NONEXISTING_ERRORS,
Xavier de Gaye38c8b7d2016-11-14 17:14:42 +0100312 self._assert_python, pre_args,
Victor Stinnerb31206a2018-01-25 19:06:05 +0100313 executable=NONEXISTING_CMD[0])
Chris Jerdonek776cb192012-10-08 15:56:43 -0700314
Victor Stinner8fbbdf02018-06-22 19:25:44 +0200315 @unittest.skipIf(support.MS_WINDOWS, "executable argument replaces shell")
Chris Jerdonek776cb192012-10-08 15:56:43 -0700316 def test_executable_replaces_shell(self):
317 # Check that the executable argument replaces the default shell
318 # when shell=True.
319 self._assert_python([], executable=sys.executable, shell=True)
320
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700321 # For use in the test_cwd* tests below.
322 def _normalize_cwd(self, cwd):
323 # Normalize an expected cwd (for Tru64 support).
324 # We can't use os.path.realpath since it doesn't expand Tru64 {memb}
325 # strings. See bug #1063571.
Serhiy Storchaka2a23adf2015-09-06 14:13:25 +0300326 with support.change_cwd(cwd):
327 return os.getcwd()
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700328
329 # For use in the test_cwd* tests below.
330 def _split_python_path(self):
331 # Return normalized (python_dir, python_base).
332 python_path = os.path.realpath(sys.executable)
333 return os.path.split(python_path)
334
335 # For use in the test_cwd* tests below.
336 def _assert_cwd(self, expected_cwd, python_arg, **kwargs):
337 # Invoke Python via Popen, and assert that (1) the call succeeds,
338 # and that (2) the current working directory of the child process
339 # matches *expected_cwd*.
340 p = subprocess.Popen([python_arg, "-c",
341 "import os, sys; "
342 "sys.stdout.write(os.getcwd()); "
343 "sys.exit(47)"],
344 stdout=subprocess.PIPE,
345 **kwargs)
346 self.addCleanup(p.stdout.close)
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000347 p.wait()
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700348 self.assertEqual(47, p.returncode)
349 normcase = os.path.normcase
350 self.assertEqual(normcase(expected_cwd),
351 normcase(p.stdout.read().decode("utf-8")))
352
353 def test_cwd(self):
354 # Check that cwd changes the cwd for the child process.
355 temp_dir = tempfile.gettempdir()
356 temp_dir = self._normalize_cwd(temp_dir)
357 self._assert_cwd(temp_dir, sys.executable, cwd=temp_dir)
358
Sayan Chowdhuryd5c11f72017-02-26 22:36:10 +0530359 def test_cwd_with_pathlike(self):
360 temp_dir = tempfile.gettempdir()
361 temp_dir = self._normalize_cwd(temp_dir)
Serhiy Storchakab21d1552018-03-02 11:53:51 +0200362 self._assert_cwd(temp_dir, sys.executable, cwd=FakePath(temp_dir))
Sayan Chowdhuryd5c11f72017-02-26 22:36:10 +0530363
Victor Stinner8fbbdf02018-06-22 19:25:44 +0200364 @unittest.skipIf(support.MS_WINDOWS, "pending resolution of issue #15533")
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700365 def test_cwd_with_relative_arg(self):
366 # Check that Popen looks for args[0] relative to cwd if args[0]
367 # is relative.
368 python_dir, python_base = self._split_python_path()
369 rel_python = os.path.join(os.curdir, python_base)
370 with support.temp_cwd() as wrong_dir:
371 # Before calling with the correct cwd, confirm that the call fails
372 # without cwd and with the wrong cwd.
Chris Jerdonek28714c82012-09-30 02:15:37 -0700373 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700374 [rel_python])
Chris Jerdonek28714c82012-09-30 02:15:37 -0700375 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700376 [rel_python], cwd=wrong_dir)
377 python_dir = self._normalize_cwd(python_dir)
378 self._assert_cwd(python_dir, rel_python, cwd=python_dir)
379
Victor Stinner8fbbdf02018-06-22 19:25:44 +0200380 @unittest.skipIf(support.MS_WINDOWS, "pending resolution of issue #15533")
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700381 def test_cwd_with_relative_executable(self):
382 # Check that Popen looks for executable relative to cwd if executable
383 # is relative (and that executable takes precedence over args[0]).
384 python_dir, python_base = self._split_python_path()
385 rel_python = os.path.join(os.curdir, python_base)
386 doesntexist = "somethingyoudonthave"
387 with support.temp_cwd() as wrong_dir:
388 # Before calling with the correct cwd, confirm that the call fails
389 # without cwd and with the wrong cwd.
Chris Jerdonek28714c82012-09-30 02:15:37 -0700390 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700391 [doesntexist], executable=rel_python)
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,
394 cwd=wrong_dir)
395 python_dir = self._normalize_cwd(python_dir)
396 self._assert_cwd(python_dir, doesntexist, executable=rel_python,
397 cwd=python_dir)
398
399 def test_cwd_with_absolute_arg(self):
400 # Check that Popen can find the executable when the cwd is wrong
401 # if args[0] is an absolute path.
402 python_dir, python_base = self._split_python_path()
403 abs_python = os.path.join(python_dir, python_base)
404 rel_python = os.path.join(os.curdir, python_base)
Berker Peksagce643912015-05-06 06:33:17 +0300405 with support.temp_dir() as wrong_dir:
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700406 # Before calling with an absolute path, confirm that using a
407 # relative path fails.
Chris Jerdonek28714c82012-09-30 02:15:37 -0700408 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700409 [rel_python], cwd=wrong_dir)
410 wrong_dir = self._normalize_cwd(wrong_dir)
411 self._assert_cwd(wrong_dir, abs_python, cwd=wrong_dir)
412
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100413 @unittest.skipIf(sys.base_prefix != sys.prefix,
414 'Test is not venv-compatible')
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000415 def test_executable_with_cwd(self):
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700416 python_dir, python_base = self._split_python_path()
417 python_dir = self._normalize_cwd(python_dir)
418 self._assert_cwd(python_dir, "somethingyoudonthave",
419 executable=sys.executable, cwd=python_dir)
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000420
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100421 @unittest.skipIf(sys.base_prefix != sys.prefix,
422 'Test is not venv-compatible')
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000423 @unittest.skipIf(sysconfig.is_python_build(),
424 "need an installed Python. See #7774")
425 def test_executable_without_cwd(self):
426 # For a normal installation, it should work without 'cwd'
427 # argument. For test runs in the build directory, see #7774.
Ned Deilye92dfbf2013-08-02 18:02:21 -0700428 self._assert_cwd(os.getcwd(), "somethingyoudonthave",
429 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000430
431 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000432 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000433 p = subprocess.Popen([sys.executable, "-c",
434 'import sys; sys.exit(sys.stdin.read() == "pear")'],
435 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000436 p.stdin.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000437 p.stdin.close()
438 p.wait()
439 self.assertEqual(p.returncode, 1)
440
441 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000442 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000443 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000444 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000445 d = tf.fileno()
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000446 os.write(d, b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000447 os.lseek(d, 0, 0)
448 p = subprocess.Popen([sys.executable, "-c",
449 'import sys; sys.exit(sys.stdin.read() == "pear")'],
450 stdin=d)
451 p.wait()
452 self.assertEqual(p.returncode, 1)
453
454 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000455 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000456 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000457 self.addCleanup(tf.close)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000458 tf.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000459 tf.seek(0)
460 p = subprocess.Popen([sys.executable, "-c",
461 'import sys; sys.exit(sys.stdin.read() == "pear")'],
462 stdin=tf)
463 p.wait()
464 self.assertEqual(p.returncode, 1)
465
466 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000467 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000468 p = subprocess.Popen([sys.executable, "-c",
469 'import sys; sys.stdout.write("orange")'],
470 stdout=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +0200471 with p:
472 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000473
474 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000475 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000476 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000477 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000478 d = tf.fileno()
479 p = subprocess.Popen([sys.executable, "-c",
480 'import sys; sys.stdout.write("orange")'],
481 stdout=d)
482 p.wait()
483 os.lseek(d, 0, 0)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000484 self.assertEqual(os.read(d, 1024), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000485
486 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000487 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000488 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000489 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000490 p = subprocess.Popen([sys.executable, "-c",
491 'import sys; sys.stdout.write("orange")'],
492 stdout=tf)
493 p.wait()
494 tf.seek(0)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000495 self.assertEqual(tf.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000496
497 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000498 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000499 p = subprocess.Popen([sys.executable, "-c",
500 'import sys; sys.stderr.write("strawberry")'],
501 stderr=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +0200502 with p:
503 self.assertStderrEqual(p.stderr.read(), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000504
505 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000506 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000507 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000508 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000509 d = tf.fileno()
510 p = subprocess.Popen([sys.executable, "-c",
511 'import sys; sys.stderr.write("strawberry")'],
512 stderr=d)
513 p.wait()
514 os.lseek(d, 0, 0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000515 self.assertStderrEqual(os.read(d, 1024), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000516
517 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000518 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000519 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000520 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000521 p = subprocess.Popen([sys.executable, "-c",
522 'import sys; sys.stderr.write("strawberry")'],
523 stderr=tf)
524 p.wait()
525 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000526 self.assertStderrEqual(tf.read(), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000527
Martin Panterc7635892016-05-13 01:54:44 +0000528 def test_stderr_redirect_with_no_stdout_redirect(self):
529 # test stderr=STDOUT while stdout=None (not set)
530
531 # - grandchild prints to stderr
532 # - child redirects grandchild's stderr to its stdout
533 # - the parent should get grandchild's stderr in child's stdout
534 p = subprocess.Popen([sys.executable, "-c",
535 'import sys, subprocess;'
536 'rc = subprocess.call([sys.executable, "-c",'
537 ' "import sys;"'
538 ' "sys.stderr.write(\'42\')"],'
539 ' stderr=subprocess.STDOUT);'
540 'sys.exit(rc)'],
541 stdout=subprocess.PIPE,
542 stderr=subprocess.PIPE)
543 stdout, stderr = p.communicate()
544 #NOTE: stdout should get stderr from grandchild
545 self.assertStderrEqual(stdout, b'42')
546 self.assertStderrEqual(stderr, b'') # should be empty
547 self.assertEqual(p.returncode, 0)
548
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000549 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000550 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000551 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000552 'import sys;'
553 'sys.stdout.write("apple");'
554 'sys.stdout.flush();'
555 'sys.stderr.write("orange")'],
556 stdout=subprocess.PIPE,
557 stderr=subprocess.STDOUT)
Victor Stinner7438c612016-05-20 12:43:15 +0200558 with p:
559 self.assertStderrEqual(p.stdout.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000560
561 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000562 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000563 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000564 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000565 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000566 'import sys;'
567 'sys.stdout.write("apple");'
568 'sys.stdout.flush();'
569 'sys.stderr.write("orange")'],
570 stdout=tf,
571 stderr=tf)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000572 p.wait()
573 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000574 self.assertStderrEqual(tf.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000575
Thomas Wouters89f507f2006-12-13 04:49:30 +0000576 def test_stdout_filedes_of_stdout(self):
577 # stdout is set to 1 (#1531862).
Ezio Melotti42a541b2013-03-11 05:53:34 +0200578 # To avoid printing the text on stdout, we do something similar to
579 # test_stdout_none (see above). The parent subprocess calls the child
580 # subprocess passing stdout=1, and this test uses stdout=PIPE in
581 # order to capture and check the output of the parent. See #11963.
582 code = ('import sys, subprocess; '
583 'rc = subprocess.call([sys.executable, "-c", '
584 ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), '
585 'b\'test with stdout=1\'))"], stdout=1); '
586 'assert rc == 18')
587 p = subprocess.Popen([sys.executable, "-c", code],
588 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
589 self.addCleanup(p.stdout.close)
590 self.addCleanup(p.stderr.close)
591 out, err = p.communicate()
592 self.assertEqual(p.returncode, 0, err)
593 self.assertEqual(out.rstrip(), b'test with stdout=1')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000594
Ross Lagerwallba102ec2011-03-16 18:40:25 +0200595 def test_stdout_devnull(self):
596 p = subprocess.Popen([sys.executable, "-c",
597 'for i in range(10240):'
598 'print("x" * 1024)'],
599 stdout=subprocess.DEVNULL)
600 p.wait()
601 self.assertEqual(p.stdout, None)
602
603 def test_stderr_devnull(self):
604 p = subprocess.Popen([sys.executable, "-c",
605 'import sys\n'
606 'for i in range(10240):'
607 'sys.stderr.write("x" * 1024)'],
608 stderr=subprocess.DEVNULL)
609 p.wait()
610 self.assertEqual(p.stderr, None)
611
612 def test_stdin_devnull(self):
613 p = subprocess.Popen([sys.executable, "-c",
614 'import sys;'
615 'sys.stdin.read(1)'],
616 stdin=subprocess.DEVNULL)
617 p.wait()
618 self.assertEqual(p.stdin, None)
619
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000620 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000621 newenv = os.environ.copy()
622 newenv["FRUIT"] = "orange"
Victor Stinnerf1512a22011-06-21 17:18:38 +0200623 with subprocess.Popen([sys.executable, "-c",
624 'import sys,os;'
625 'sys.stdout.write(os.getenv("FRUIT"))'],
626 stdout=subprocess.PIPE,
627 env=newenv) as p:
628 stdout, stderr = p.communicate()
629 self.assertEqual(stdout, b"orange")
630
Victor Stinner62d51182011-06-23 01:02:25 +0200631 # Windows requires at least the SYSTEMROOT environment variable to start
632 # Python
633 @unittest.skipIf(sys.platform == 'win32',
634 'cannot test an empty env on Windows')
Gregory P. Smithb3512482017-05-30 14:40:37 -0700635 @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') == 1,
636 'The Python shared library cannot be loaded '
637 'with an empty environment.')
Victor Stinnerf1512a22011-06-21 17:18:38 +0200638 def test_empty_env(self):
Gregory P. Smithb3512482017-05-30 14:40:37 -0700639 """Verify that env={} is as empty as possible."""
640
Gregory P. Smith85aba232017-05-30 16:21:47 -0700641 def is_env_var_to_ignore(n):
Gregory P. Smithb3512482017-05-30 14:40:37 -0700642 """Determine if an environment variable is under our control."""
643 # This excludes some __CF_* and VERSIONER_* keys MacOS insists
644 # on adding even when the environment in exec is empty.
645 # Gentoo sandboxes also force LD_PRELOAD and SANDBOX_* to exist.
Gregory P. Smith85aba232017-05-30 16:21:47 -0700646 return ('VERSIONER' in n or '__CF' in n or # MacOS
Ned Deily918edc02017-09-04 00:00:21 -0400647 '__PYVENV_LAUNCHER__' in n or # MacOS framework build
Nick Coghlan6ea41862017-06-11 13:16:15 +1000648 n == 'LD_PRELOAD' or n.startswith('SANDBOX') or # Gentoo
649 n == 'LC_CTYPE') # Locale coercion triggered
Gregory P. Smithb3512482017-05-30 14:40:37 -0700650
Victor Stinnerf1512a22011-06-21 17:18:38 +0200651 with subprocess.Popen([sys.executable, "-c",
Gregory P. Smithb3512482017-05-30 14:40:37 -0700652 'import os; print(list(os.environ.keys()))'],
653 stdout=subprocess.PIPE, env={}) as p:
Victor Stinnerf1512a22011-06-21 17:18:38 +0200654 stdout, stderr = p.communicate()
Gregory P. Smithb3512482017-05-30 14:40:37 -0700655 child_env_names = eval(stdout.strip())
656 self.assertIsInstance(child_env_names, list)
657 child_env_names = [k for k in child_env_names
658 if not is_env_var_to_ignore(k)]
659 self.assertEqual(child_env_names, [])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000660
Serhiy Storchakad174d242017-06-23 19:39:27 +0300661 def test_invalid_cmd(self):
662 # null character in the command name
663 cmd = sys.executable + '\0'
664 with self.assertRaises(ValueError):
665 subprocess.Popen([cmd, "-c", "pass"])
666
667 # null character in the command argument
668 with self.assertRaises(ValueError):
669 subprocess.Popen([sys.executable, "-c", "pass#\0"])
670
671 def test_invalid_env(self):
Ville Skyttä49b27342017-08-03 09:00:59 +0300672 # null character in the environment variable name
Serhiy Storchakad174d242017-06-23 19:39:27 +0300673 newenv = os.environ.copy()
674 newenv["FRUIT\0VEGETABLE"] = "cabbage"
675 with self.assertRaises(ValueError):
676 subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
677
Ville Skyttä49b27342017-08-03 09:00:59 +0300678 # null character in the environment variable value
Serhiy Storchakad174d242017-06-23 19:39:27 +0300679 newenv = os.environ.copy()
680 newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
681 with self.assertRaises(ValueError):
682 subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
683
Ville Skyttä49b27342017-08-03 09:00:59 +0300684 # equal character in the environment variable name
Serhiy Storchakad174d242017-06-23 19:39:27 +0300685 newenv = os.environ.copy()
686 newenv["FRUIT=ORANGE"] = "lemon"
687 with self.assertRaises(ValueError):
688 subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
689
Ville Skyttä49b27342017-08-03 09:00:59 +0300690 # equal character in the environment variable value
Serhiy Storchakad174d242017-06-23 19:39:27 +0300691 newenv = os.environ.copy()
692 newenv["FRUIT"] = "orange=lemon"
693 with subprocess.Popen([sys.executable, "-c",
694 'import sys, os;'
695 'sys.stdout.write(os.getenv("FRUIT"))'],
696 stdout=subprocess.PIPE,
697 env=newenv) as p:
698 stdout, stderr = p.communicate()
699 self.assertEqual(stdout, b"orange=lemon")
700
Peter Astrandcbac93c2005-03-03 20:24:28 +0000701 def test_communicate_stdin(self):
702 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000703 'import sys;'
704 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000705 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000706 p.communicate(b"pear")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000707 self.assertEqual(p.returncode, 1)
708
709 def test_communicate_stdout(self):
710 p = subprocess.Popen([sys.executable, "-c",
711 'import sys; sys.stdout.write("pineapple")'],
712 stdout=subprocess.PIPE)
713 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000714 self.assertEqual(stdout, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000715 self.assertEqual(stderr, None)
716
717 def test_communicate_stderr(self):
718 p = subprocess.Popen([sys.executable, "-c",
719 'import sys; sys.stderr.write("pineapple")'],
720 stderr=subprocess.PIPE)
721 (stdout, stderr) = p.communicate()
722 self.assertEqual(stdout, None)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000723 self.assertStderrEqual(stderr, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000724
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000725 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000726 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000727 'import sys,os;'
728 'sys.stderr.write("pineapple");'
729 'sys.stdout.write(sys.stdin.read())'],
730 stdin=subprocess.PIPE,
731 stdout=subprocess.PIPE,
732 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000733 self.addCleanup(p.stdout.close)
734 self.addCleanup(p.stderr.close)
735 self.addCleanup(p.stdin.close)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000736 (stdout, stderr) = p.communicate(b"banana")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000737 self.assertEqual(stdout, b"banana")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000738 self.assertStderrEqual(stderr, b"pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000739
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400740 def test_communicate_timeout(self):
741 p = subprocess.Popen([sys.executable, "-c",
742 'import sys,os,time;'
743 'sys.stderr.write("pineapple\\n");'
744 'time.sleep(1);'
745 'sys.stderr.write("pear\\n");'
746 'sys.stdout.write(sys.stdin.read())'],
747 universal_newlines=True,
748 stdin=subprocess.PIPE,
749 stdout=subprocess.PIPE,
750 stderr=subprocess.PIPE)
751 self.assertRaises(subprocess.TimeoutExpired, p.communicate, "banana",
752 timeout=0.3)
753 # Make sure we can keep waiting for it, and that we get the whole output
754 # after it completes.
755 (stdout, stderr) = p.communicate()
756 self.assertEqual(stdout, "banana")
757 self.assertStderrEqual(stderr.encode(), b"pineapple\npear\n")
758
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700759 def test_communicate_timeout_large_output(self):
Ross Lagerwall003c7a32012-02-12 09:02:01 +0200760 # Test an expiring timeout while the child is outputting lots of data.
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400761 p = subprocess.Popen([sys.executable, "-c",
762 'import sys,os,time;'
763 'sys.stdout.write("a" * (64 * 1024));'
764 'time.sleep(0.2);'
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 stdout=subprocess.PIPE)
771 self.assertRaises(subprocess.TimeoutExpired, p.communicate, timeout=0.4)
772 (stdout, _) = p.communicate()
773 self.assertEqual(len(stdout), 4 * 64 * 1024)
774
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000775 # Test for the fd leak reported in http://bugs.python.org/issue2791.
776 def test_communicate_pipe_fd_leak(self):
Victor Stinner667d4b52010-12-25 22:40:32 +0000777 for stdin_pipe in (False, True):
778 for stdout_pipe in (False, True):
779 for stderr_pipe in (False, True):
780 options = {}
781 if stdin_pipe:
782 options['stdin'] = subprocess.PIPE
783 if stdout_pipe:
784 options['stdout'] = subprocess.PIPE
785 if stderr_pipe:
786 options['stderr'] = subprocess.PIPE
787 if not options:
788 continue
789 p = subprocess.Popen((sys.executable, "-c", "pass"), **options)
790 p.communicate()
791 if p.stdin is not None:
792 self.assertTrue(p.stdin.closed)
793 if p.stdout is not None:
794 self.assertTrue(p.stdout.closed)
795 if p.stderr is not None:
796 self.assertTrue(p.stderr.closed)
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000797
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000798 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000799 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000800 p = subprocess.Popen([sys.executable, "-c",
801 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000802 (stdout, stderr) = p.communicate()
803 self.assertEqual(stdout, None)
804 self.assertEqual(stderr, None)
805
806 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000807 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000808 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000809 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000810 x, y = os.pipe()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000811 os.close(x)
812 os.close(y)
813 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000814 'import sys,os;'
815 'sys.stdout.write(sys.stdin.read(47));'
Charles-François Natali2d517212011-05-29 16:36:44 +0200816 'sys.stderr.write("x" * %d);'
817 'sys.stdout.write(sys.stdin.read())' %
818 support.PIPE_MAX_SIZE],
Guido van Rossum98297ee2007-11-06 21:34:58 +0000819 stdin=subprocess.PIPE,
820 stdout=subprocess.PIPE,
821 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000822 self.addCleanup(p.stdout.close)
823 self.addCleanup(p.stderr.close)
824 self.addCleanup(p.stdin.close)
Charles-François Natali2d517212011-05-29 16:36:44 +0200825 string_to_write = b"a" * support.PIPE_MAX_SIZE
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000826 (stdout, stderr) = p.communicate(string_to_write)
827 self.assertEqual(stdout, string_to_write)
828
829 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000830 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000831 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000832 'import sys,os;'
833 'sys.stdout.write(sys.stdin.read())'],
834 stdin=subprocess.PIPE,
835 stdout=subprocess.PIPE,
836 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000837 self.addCleanup(p.stdout.close)
838 self.addCleanup(p.stderr.close)
839 self.addCleanup(p.stdin.close)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000840 p.stdin.write(b"banana")
841 (stdout, stderr) = p.communicate(b"split")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000842 self.assertEqual(stdout, b"bananasplit")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000843 self.assertStderrEqual(stderr, b"")
Tim Peterse718f612004-10-12 21:51:32 +0000844
andyclegg7fed7bd2017-10-23 03:01:19 +0100845 def test_universal_newlines_and_text(self):
846 args = [
847 sys.executable, "-c",
848 'import sys,os;' + SETBINARY +
849 'buf = sys.stdout.buffer;'
850 'buf.write(sys.stdin.readline().encode());'
851 'buf.flush();'
852 'buf.write(b"line2\\n");'
853 'buf.flush();'
854 'buf.write(sys.stdin.read().encode());'
855 'buf.flush();'
856 'buf.write(b"line4\\n");'
857 'buf.flush();'
858 'buf.write(b"line5\\r\\n");'
859 'buf.flush();'
860 'buf.write(b"line6\\r");'
861 'buf.flush();'
862 'buf.write(b"\\nline7");'
863 'buf.flush();'
864 'buf.write(b"\\nline8");']
865
866 for extra_kwarg in ('universal_newlines', 'text'):
867 p = subprocess.Popen(args, **{'stdin': subprocess.PIPE,
868 'stdout': subprocess.PIPE,
869 extra_kwarg: True})
870 with p:
871 p.stdin.write("line1\n")
872 p.stdin.flush()
873 self.assertEqual(p.stdout.readline(), "line1\n")
874 p.stdin.write("line3\n")
875 p.stdin.close()
876 self.addCleanup(p.stdout.close)
877 self.assertEqual(p.stdout.readline(),
878 "line2\n")
879 self.assertEqual(p.stdout.read(6),
880 "line3\n")
881 self.assertEqual(p.stdout.read(),
882 "line4\nline5\nline6\nline7\nline8")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000883
884 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000885 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000886 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000887 'import sys,os;' + SETBINARY +
Antoine Pitrouec2d2692012-08-05 00:23:40 +0200888 'buf = sys.stdout.buffer;'
889 'buf.write(b"line2\\n");'
890 'buf.flush();'
891 'buf.write(b"line4\\n");'
892 'buf.flush();'
893 'buf.write(b"line5\\r\\n");'
894 'buf.flush();'
895 'buf.write(b"line6\\r");'
896 'buf.flush();'
897 'buf.write(b"\\nline7");'
898 'buf.flush();'
899 'buf.write(b"\\nline8");'],
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200900 stderr=subprocess.PIPE,
901 stdout=subprocess.PIPE,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000902 universal_newlines=1)
Brian Curtin19a53792010-11-05 17:09:05 +0000903 self.addCleanup(p.stdout.close)
904 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000905 (stdout, stderr) = p.communicate()
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200906 self.assertEqual(stdout,
907 "line2\nline4\nline5\nline6\nline7\nline8")
908
909 def test_universal_newlines_communicate_stdin(self):
910 # universal newlines through communicate(), with only stdin
911 p = subprocess.Popen([sys.executable, "-c",
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300912 'import sys,os;' + SETBINARY + textwrap.dedent('''
913 s = sys.stdin.readline()
914 assert s == "line1\\n", repr(s)
915 s = sys.stdin.read()
916 assert s == "line3\\n", repr(s)
917 ''')],
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200918 stdin=subprocess.PIPE,
919 universal_newlines=1)
920 (stdout, stderr) = p.communicate("line1\nline3\n")
921 self.assertEqual(p.returncode, 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000922
Andrew Svetlovf3765072012-08-14 18:35:17 +0300923 def test_universal_newlines_communicate_input_none(self):
924 # Test communicate(input=None) with universal newlines.
925 #
926 # We set stdout to PIPE because, as of this writing, a different
927 # code path is tested when the number of pipes is zero or one.
928 p = subprocess.Popen([sys.executable, "-c", "pass"],
929 stdin=subprocess.PIPE,
930 stdout=subprocess.PIPE,
931 universal_newlines=True)
932 p.communicate()
933 self.assertEqual(p.returncode, 0)
934
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300935 def test_universal_newlines_communicate_stdin_stdout_stderr(self):
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300936 # universal newlines through communicate(), with stdin, stdout, stderr
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300937 p = subprocess.Popen([sys.executable, "-c",
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300938 'import sys,os;' + SETBINARY + textwrap.dedent('''
939 s = sys.stdin.buffer.readline()
940 sys.stdout.buffer.write(s)
941 sys.stdout.buffer.write(b"line2\\r")
942 sys.stderr.buffer.write(b"eline2\\n")
943 s = sys.stdin.buffer.read()
944 sys.stdout.buffer.write(s)
945 sys.stdout.buffer.write(b"line4\\n")
946 sys.stdout.buffer.write(b"line5\\r\\n")
947 sys.stderr.buffer.write(b"eline6\\r")
948 sys.stderr.buffer.write(b"eline7\\r\\nz")
949 ''')],
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300950 stdin=subprocess.PIPE,
951 stderr=subprocess.PIPE,
952 stdout=subprocess.PIPE,
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300953 universal_newlines=True)
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300954 self.addCleanup(p.stdout.close)
955 self.addCleanup(p.stderr.close)
956 (stdout, stderr) = p.communicate("line1\nline3\n")
957 self.assertEqual(p.returncode, 0)
Andrew Svetlov943c5b32012-08-16 20:17:47 +0300958 self.assertEqual("line1\nline2\nline3\nline4\nline5\n", stdout)
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300959 # Python debug build push something like "[42442 refs]\n"
960 # to stderr at exit of subprocess.
Andrew Svetlov943c5b32012-08-16 20:17:47 +0300961 # Don't use assertStderrEqual because it strips CR and LF from output.
962 self.assertTrue(stderr.startswith("eline2\neline6\neline7\n"))
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300963
Andrew Svetlov82860712012-08-19 22:13:41 +0300964 def test_universal_newlines_communicate_encodings(self):
965 # Check that universal newlines mode works for various encodings,
966 # in particular for encodings in the UTF-16 and UTF-32 families.
967 # See issue #15595.
968 #
969 # UTF-16 and UTF-32-BE are sufficient to check both with BOM and
970 # without, and UTF-16 and UTF-32.
971 for encoding in ['utf-16', 'utf-32-be']:
Andrew Svetlov82860712012-08-19 22:13:41 +0300972 code = ("import sys; "
973 r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" %
974 encoding)
975 args = [sys.executable, '-c', code]
Steve Dower050acae2016-09-06 20:16:17 -0700976 # We set stdin to be non-None because, as of this writing,
977 # a different code path is used when the number of pipes is
978 # zero or one.
979 popen = subprocess.Popen(args,
980 stdin=subprocess.PIPE,
981 stdout=subprocess.PIPE,
982 encoding=encoding)
983 stdout, stderr = popen.communicate(input='')
Andrew Svetlov82860712012-08-19 22:13:41 +0300984 self.assertEqual(stdout, '1\n2\n3\n4')
985
Steve Dower050acae2016-09-06 20:16:17 -0700986 def test_communicate_errors(self):
987 for errors, expected in [
988 ('ignore', ''),
989 ('replace', '\ufffd\ufffd'),
990 ('surrogateescape', '\udc80\udc80'),
991 ('backslashreplace', '\\x80\\x80'),
992 ]:
993 code = ("import sys; "
994 r"sys.stdout.buffer.write(b'[\x80\x80]')")
995 args = [sys.executable, '-c', code]
996 # We set stdin to be non-None because, as of this writing,
997 # a different code path is used when the number of pipes is
998 # zero or one.
999 popen = subprocess.Popen(args,
1000 stdin=subprocess.PIPE,
1001 stdout=subprocess.PIPE,
1002 encoding='utf-8',
1003 errors=errors)
1004 stdout, stderr = popen.communicate(input='')
1005 self.assertEqual(stdout, '[{}]'.format(expected))
1006
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001007 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +00001008 # Make sure we leak no resources
Victor Stinner8fbbdf02018-06-22 19:25:44 +02001009 if not support.MS_WINDOWS:
Peter Astrandf7f1bb72005-03-03 20:47:37 +00001010 max_handles = 1026 # too much for most UNIX systems
1011 else:
Antoine Pitrou8db30272010-09-18 22:38:48 +00001012 max_handles = 2050 # too much for (at least some) Windows setups
1013 handles = []
Gregory P. Smith81ce6852011-03-15 02:04:11 -04001014 tmpdir = tempfile.mkdtemp()
Antoine Pitrou8db30272010-09-18 22:38:48 +00001015 try:
1016 for i in range(max_handles):
1017 try:
Gregory P. Smith81ce6852011-03-15 02:04:11 -04001018 tmpfile = os.path.join(tmpdir, support.TESTFN)
1019 handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT))
Antoine Pitrou8db30272010-09-18 22:38:48 +00001020 except OSError as e:
1021 if e.errno != errno.EMFILE:
1022 raise
1023 break
1024 else:
1025 self.skipTest("failed to reach the file descriptor limit "
1026 "(tried %d)" % max_handles)
1027 # Close a couple of them (should be enough for a subprocess)
1028 for i in range(10):
1029 os.close(handles.pop())
1030 # Loop creating some subprocesses. If one of them leaks some fds,
1031 # the next loop iteration will fail by reaching the max fd limit.
1032 for i in range(15):
1033 p = subprocess.Popen([sys.executable, "-c",
1034 "import sys;"
1035 "sys.stdout.write(sys.stdin.read())"],
1036 stdin=subprocess.PIPE,
1037 stdout=subprocess.PIPE,
1038 stderr=subprocess.PIPE)
1039 data = p.communicate(b"lime")[0]
1040 self.assertEqual(data, b"lime")
1041 finally:
1042 for h in handles:
1043 os.close(h)
Gregory P. Smith81ce6852011-03-15 02:04:11 -04001044 shutil.rmtree(tmpdir)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001045
1046 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001047 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
1048 '"a b c" d e')
1049 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
1050 'ab\\"c \\ d')
Christian Heimesfdab48e2008-01-20 09:06:41 +00001051 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
1052 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001053 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
1054 'a\\\\\\b "de fg" h')
1055 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
1056 'a\\\\\\"b c d')
1057 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
1058 '"a\\\\b c" d e')
1059 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
1060 '"a\\\\b\\ c" d e')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001061 self.assertEqual(subprocess.list2cmdline(['ab', '']),
1062 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001063
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001064 def test_poll(self):
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001065 p = subprocess.Popen([sys.executable, "-c",
Ross Lagerwalle7ad4192012-02-22 06:02:07 +02001066 "import os; os.read(0, 1)"],
1067 stdin=subprocess.PIPE)
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001068 self.addCleanup(p.stdin.close)
1069 self.assertIsNone(p.poll())
1070 os.write(p.stdin.fileno(), b'A')
1071 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001072 # Subsequent invocations should just return the returncode
1073 self.assertEqual(p.poll(), 0)
1074
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001075 def test_wait(self):
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001076 p = subprocess.Popen([sys.executable, "-c", "pass"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001077 self.assertEqual(p.wait(), 0)
1078 # Subsequent invocations should just return the returncode
1079 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +00001080
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001081 def test_wait_timeout(self):
1082 p = subprocess.Popen([sys.executable,
Antoine Pitroudc49b2b2013-05-19 15:55:40 +02001083 "-c", "import time; time.sleep(0.3)"])
Reid Kleckner2b228f02011-03-16 16:57:54 -04001084 with self.assertRaises(subprocess.TimeoutExpired) as c:
Antoine Pitroudc49b2b2013-05-19 15:55:40 +02001085 p.wait(timeout=0.0001)
1086 self.assertIn("0.0001", str(c.exception)) # For coverage of __str__.
Reid Klecknerda9ac722011-03-16 17:08:21 -04001087 # Some heavily loaded buildbots (sparc Debian 3.x) require this much
1088 # time to start.
1089 self.assertEqual(p.wait(timeout=3), 0)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001090
Peter Astrand738131d2004-11-30 21:04:45 +00001091 def test_invalid_bufsize(self):
1092 # an invalid type of the bufsize argument should raise
1093 # TypeError.
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001094 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +00001095 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +00001096
Guido van Rossum46a05a72007-06-07 21:56:45 +00001097 def test_bufsize_is_none(self):
1098 # bufsize=None should be the same as bufsize=0.
1099 p = subprocess.Popen([sys.executable, "-c", "pass"], None)
1100 self.assertEqual(p.wait(), 0)
1101 # Again with keyword arg
1102 p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None)
1103 self.assertEqual(p.wait(), 0)
1104
Antoine Pitrouafe8d062014-09-21 21:10:56 +02001105 def _test_bufsize_equal_one(self, line, expected, universal_newlines):
1106 # subprocess may deadlock with bufsize=1, see issue #21332
1107 with subprocess.Popen([sys.executable, "-c", "import sys;"
1108 "sys.stdout.write(sys.stdin.readline());"
1109 "sys.stdout.flush()"],
1110 stdin=subprocess.PIPE,
1111 stdout=subprocess.PIPE,
1112 stderr=subprocess.DEVNULL,
1113 bufsize=1,
1114 universal_newlines=universal_newlines) as p:
1115 p.stdin.write(line) # expect that it flushes the line in text mode
1116 os.close(p.stdin.fileno()) # close it without flushing the buffer
1117 read_line = p.stdout.readline()
Segev Finer4d385172017-08-18 16:18:13 +03001118 with support.SuppressCrashReport():
1119 try:
1120 p.stdin.close()
1121 except OSError:
1122 pass
Antoine Pitrouafe8d062014-09-21 21:10:56 +02001123 p.stdin = None
1124 self.assertEqual(p.returncode, 0)
1125 self.assertEqual(read_line, expected)
1126
1127 def test_bufsize_equal_one_text_mode(self):
1128 # line is flushed in text mode with bufsize=1.
1129 # we should get the full line in return
1130 line = "line\n"
1131 self._test_bufsize_equal_one(line, line, universal_newlines=True)
1132
1133 def test_bufsize_equal_one_binary_mode(self):
1134 # line is not flushed in binary mode with bufsize=1.
1135 # we should get empty response
1136 line = b'line' + os.linesep.encode() # assume ascii-based locale
1137 self._test_bufsize_equal_one(line, b'', universal_newlines=False)
1138
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001139 def test_leaking_fds_on_error(self):
1140 # see bug #5179: Popen leaks file descriptors to PIPEs if
1141 # the child fails to execute; this will eventually exhaust
1142 # the maximum number of open fds. 1024 seems a very common
1143 # value for that limit, but Windows has 2048, so we loop
1144 # 1024 times (each call leaked two fds).
1145 for i in range(1024):
Victor Stinnerb31206a2018-01-25 19:06:05 +01001146 with self.assertRaises(NONEXISTING_ERRORS):
Victor Stinner9a83f652017-08-21 23:51:31 +02001147 subprocess.Popen(NONEXISTING_CMD,
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001148 stdout=subprocess.PIPE,
1149 stderr=subprocess.PIPE)
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001150
Victor Stinner9a83f652017-08-21 23:51:31 +02001151 def test_nonexisting_with_pipes(self):
1152 # bpo-30121: Popen with pipes must close properly pipes on error.
1153 # Previously, os.close() was called with a Windows handle which is not
1154 # a valid file descriptor.
1155 #
1156 # Run the test in a subprocess to control how the CRT reports errors
1157 # and to get stderr content.
1158 try:
1159 import msvcrt
1160 msvcrt.CrtSetReportMode
1161 except (AttributeError, ImportError):
1162 self.skipTest("need msvcrt.CrtSetReportMode")
1163
1164 code = textwrap.dedent(f"""
1165 import msvcrt
1166 import subprocess
1167
1168 cmd = {NONEXISTING_CMD!r}
1169
1170 for report_type in [msvcrt.CRT_WARN,
1171 msvcrt.CRT_ERROR,
1172 msvcrt.CRT_ASSERT]:
1173 msvcrt.CrtSetReportMode(report_type, msvcrt.CRTDBG_MODE_FILE)
1174 msvcrt.CrtSetReportFile(report_type, msvcrt.CRTDBG_FILE_STDERR)
1175
1176 try:
Zachary Ware55376462018-02-19 14:02:38 -06001177 subprocess.Popen(cmd,
Victor Stinner9a83f652017-08-21 23:51:31 +02001178 stdout=subprocess.PIPE,
1179 stderr=subprocess.PIPE)
1180 except OSError:
1181 pass
1182 """)
1183 cmd = [sys.executable, "-c", code]
1184 proc = subprocess.Popen(cmd,
1185 stderr=subprocess.PIPE,
1186 universal_newlines=True)
1187 with proc:
1188 stderr = proc.communicate()[1]
1189 self.assertEqual(stderr, "")
1190 self.assertEqual(proc.returncode, 0)
1191
Antoine Pitroua8392712013-08-30 23:38:13 +02001192 def test_double_close_on_error(self):
1193 # Issue #18851
1194 fds = []
1195 def open_fds():
1196 for i in range(20):
1197 fds.extend(os.pipe())
1198 time.sleep(0.001)
1199 t = threading.Thread(target=open_fds)
1200 t.start()
1201 try:
1202 with self.assertRaises(EnvironmentError):
Victor Stinner9a83f652017-08-21 23:51:31 +02001203 subprocess.Popen(NONEXISTING_CMD,
Antoine Pitroua8392712013-08-30 23:38:13 +02001204 stdin=subprocess.PIPE,
1205 stdout=subprocess.PIPE,
1206 stderr=subprocess.PIPE)
1207 finally:
1208 t.join()
1209 exc = None
1210 for fd in fds:
1211 # If a double close occurred, some of those fds will
1212 # already have been closed by mistake, and os.close()
1213 # here will raise.
1214 try:
1215 os.close(fd)
1216 except OSError as e:
1217 exc = e
1218 if exc is not None:
1219 raise exc
1220
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001221 def test_threadsafe_wait(self):
1222 """Issue21291: Popen.wait() needs to be threadsafe for returncode."""
1223 proc = subprocess.Popen([sys.executable, '-c',
1224 'import time; time.sleep(12)'])
1225 self.assertEqual(proc.returncode, None)
1226 results = []
1227
1228 def kill_proc_timer_thread():
1229 results.append(('thread-start-poll-result', proc.poll()))
1230 # terminate it from the thread and wait for the result.
1231 proc.kill()
1232 proc.wait()
1233 results.append(('thread-after-kill-and-wait', proc.returncode))
1234 # this wait should be a no-op given the above.
1235 proc.wait()
1236 results.append(('thread-after-second-wait', proc.returncode))
1237
1238 # This is a timing sensitive test, the failure mode is
1239 # triggered when both the main thread and this thread are in
1240 # the wait() call at once. The delay here is to allow the
1241 # main thread to most likely be blocked in its wait() call.
1242 t = threading.Timer(0.2, kill_proc_timer_thread)
1243 t.start()
1244
Victor Stinner8fbbdf02018-06-22 19:25:44 +02001245 if support.MS_WINDOWS:
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001246 expected_errorcode = 1
1247 else:
1248 # Should be -9 because of the proc.kill() from the thread.
1249 expected_errorcode = -9
1250
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001251 # Wait for the process to finish; the thread should kill it
1252 # long before it finishes on its own. Supplying a timeout
1253 # triggers a different code path for better coverage.
1254 proc.wait(timeout=20)
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001255 self.assertEqual(proc.returncode, expected_errorcode,
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001256 msg="unexpected result in wait from main thread")
1257
1258 # This should be a no-op with no change in returncode.
1259 proc.wait()
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001260 self.assertEqual(proc.returncode, expected_errorcode,
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001261 msg="unexpected result in second main wait.")
1262
1263 t.join()
1264 # Ensure that all of the thread results are as expected.
1265 # When a race condition occurs in wait(), the returncode could
1266 # be set by the wrong thread that doesn't actually have it
1267 # leading to an incorrect value.
1268 self.assertEqual([('thread-start-poll-result', None),
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001269 ('thread-after-kill-and-wait', expected_errorcode),
1270 ('thread-after-second-wait', expected_errorcode)],
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001271 results)
1272
Victor Stinnerb3693582010-05-21 20:13:12 +00001273 def test_issue8780(self):
1274 # Ensure that stdout is inherited from the parent
1275 # if stdout=PIPE is not used
1276 code = ';'.join((
1277 'import subprocess, sys',
1278 'retcode = subprocess.call('
1279 "[sys.executable, '-c', 'print(\"Hello World!\")'])",
1280 'assert retcode == 0'))
1281 output = subprocess.check_output([sys.executable, '-c', code])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001282 self.assertTrue(output.startswith(b'Hello World!'), ascii(output))
Victor Stinnerb3693582010-05-21 20:13:12 +00001283
Tim Goldenaf5ac392010-08-06 13:03:56 +00001284 def test_handles_closed_on_exception(self):
1285 # If CreateProcess exits with an error, ensure the
1286 # duplicate output handles are released
Berker Peksag16a1f282015-09-28 13:33:14 +03001287 ifhandle, ifname = tempfile.mkstemp()
1288 ofhandle, ofname = tempfile.mkstemp()
1289 efhandle, efname = tempfile.mkstemp()
Tim Goldenaf5ac392010-08-06 13:03:56 +00001290 try:
1291 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
1292 stderr=efhandle)
1293 except OSError:
1294 os.close(ifhandle)
1295 os.remove(ifname)
1296 os.close(ofhandle)
1297 os.remove(ofname)
1298 os.close(efhandle)
1299 os.remove(efname)
1300 self.assertFalse(os.path.exists(ifname))
1301 self.assertFalse(os.path.exists(ofname))
1302 self.assertFalse(os.path.exists(efname))
1303
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001304 def test_communicate_epipe(self):
1305 # Issue 10963: communicate() should hide EPIPE
1306 p = subprocess.Popen([sys.executable, "-c", 'pass'],
1307 stdin=subprocess.PIPE,
1308 stdout=subprocess.PIPE,
1309 stderr=subprocess.PIPE)
1310 self.addCleanup(p.stdout.close)
1311 self.addCleanup(p.stderr.close)
1312 self.addCleanup(p.stdin.close)
1313 p.communicate(b"x" * 2**20)
1314
1315 def test_communicate_epipe_only_stdin(self):
1316 # Issue 10963: communicate() should hide EPIPE
1317 p = subprocess.Popen([sys.executable, "-c", 'pass'],
1318 stdin=subprocess.PIPE)
1319 self.addCleanup(p.stdin.close)
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001320 p.wait()
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001321 p.communicate(b"x" * 2**20)
1322
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001323 @unittest.skipUnless(hasattr(signal, 'SIGUSR1'),
1324 "Requires signal.SIGUSR1")
1325 @unittest.skipUnless(hasattr(os, 'kill'),
1326 "Requires os.kill")
1327 @unittest.skipUnless(hasattr(os, 'getppid'),
1328 "Requires os.getppid")
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001329 def test_communicate_eintr(self):
1330 # Issue #12493: communicate() should handle EINTR
1331 def handler(signum, frame):
1332 pass
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001333 old_handler = signal.signal(signal.SIGUSR1, handler)
1334 self.addCleanup(signal.signal, signal.SIGUSR1, old_handler)
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001335
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001336 args = [sys.executable, "-c",
1337 'import os, signal;'
1338 'os.kill(os.getppid(), signal.SIGUSR1)']
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001339 for stream in ('stdout', 'stderr'):
1340 kw = {stream: subprocess.PIPE}
1341 with subprocess.Popen(args, **kw) as process:
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001342 # communicate() will be interrupted by SIGUSR1
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001343 process.communicate()
1344
Tim Peterse718f612004-10-12 21:51:32 +00001345
Gregory P. Smith3d8e7762012-11-10 22:32:22 -08001346 # This test is Linux-ish specific for simplicity to at least have
1347 # some coverage. It is not a platform specific bug.
1348 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
1349 "Linux specific")
1350 def test_failed_child_execute_fd_leak(self):
1351 """Test for the fork() failure fd leak reported in issue16327."""
1352 fd_directory = '/proc/%d/fd' % os.getpid()
1353 fds_before_popen = os.listdir(fd_directory)
1354 with self.assertRaises(PopenTestException):
1355 PopenExecuteChildRaises(
1356 [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE,
1357 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1358
1359 # NOTE: This test doesn't verify that the real _execute_child
1360 # does not close the file descriptors itself on the way out
1361 # during an exception. Code inspection has confirmed that.
1362
1363 fds_after_exception = os.listdir(fd_directory)
1364 self.assertEqual(fds_before_popen, fds_after_exception)
1365
Victor Stinner8fbbdf02018-06-22 19:25:44 +02001366 @unittest.skipIf(support.MS_WINDOWS, "behavior currently not supported on Windows")
Gregory P. Smith8621bb52017-08-24 14:58:25 -07001367 def test_file_not_found_includes_filename(self):
1368 with self.assertRaises(FileNotFoundError) as c:
1369 subprocess.call(['/opt/nonexistent_binary', 'with', 'some', 'args'])
1370 self.assertEqual(c.exception.filename, '/opt/nonexistent_binary')
1371
Victor Stinner8fbbdf02018-06-22 19:25:44 +02001372 @unittest.skipIf(support.MS_WINDOWS, "behavior currently not supported on Windows")
Gregory P. Smith8621bb52017-08-24 14:58:25 -07001373 def test_file_not_found_with_bad_cwd(self):
1374 with self.assertRaises(FileNotFoundError) as c:
1375 subprocess.Popen(['exit', '0'], cwd='/some/nonexistent/directory')
1376 self.assertEqual(c.exception.filename, '/some/nonexistent/directory')
1377
Gregory P. Smith6e730002015-04-14 16:14:25 -07001378
1379class RunFuncTestCase(BaseTestCase):
1380 def run_python(self, code, **kwargs):
1381 """Run Python code in a subprocess using subprocess.run"""
1382 argv = [sys.executable, "-c", code]
1383 return subprocess.run(argv, **kwargs)
1384
1385 def test_returncode(self):
1386 # call() function with sequence argument
1387 cp = self.run_python("import sys; sys.exit(47)")
1388 self.assertEqual(cp.returncode, 47)
1389 with self.assertRaises(subprocess.CalledProcessError):
1390 cp.check_returncode()
1391
1392 def test_check(self):
1393 with self.assertRaises(subprocess.CalledProcessError) as c:
1394 self.run_python("import sys; sys.exit(47)", check=True)
1395 self.assertEqual(c.exception.returncode, 47)
1396
1397 def test_check_zero(self):
1398 # check_returncode shouldn't raise when returncode is zero
1399 cp = self.run_python("import sys; sys.exit(0)", check=True)
1400 self.assertEqual(cp.returncode, 0)
1401
1402 def test_timeout(self):
1403 # run() function with timeout argument; we want to test that the child
1404 # process gets killed when the timeout expires. If the child isn't
1405 # killed, this call will deadlock since subprocess.run waits for the
1406 # child.
1407 with self.assertRaises(subprocess.TimeoutExpired):
1408 self.run_python("while True: pass", timeout=0.0001)
1409
1410 def test_capture_stdout(self):
1411 # capture stdout with zero return code
1412 cp = self.run_python("print('BDFL')", stdout=subprocess.PIPE)
1413 self.assertIn(b'BDFL', cp.stdout)
1414
1415 def test_capture_stderr(self):
1416 cp = self.run_python("import sys; sys.stderr.write('BDFL')",
1417 stderr=subprocess.PIPE)
1418 self.assertIn(b'BDFL', cp.stderr)
1419
1420 def test_check_output_stdin_arg(self):
1421 # run() can be called with stdin set to a file
1422 tf = tempfile.TemporaryFile()
1423 self.addCleanup(tf.close)
1424 tf.write(b'pear')
1425 tf.seek(0)
1426 cp = self.run_python(
1427 "import sys; sys.stdout.write(sys.stdin.read().upper())",
1428 stdin=tf, stdout=subprocess.PIPE)
1429 self.assertIn(b'PEAR', cp.stdout)
1430
1431 def test_check_output_input_arg(self):
1432 # check_output() can be called with input set to a string
1433 cp = self.run_python(
1434 "import sys; sys.stdout.write(sys.stdin.read().upper())",
1435 input=b'pear', stdout=subprocess.PIPE)
1436 self.assertIn(b'PEAR', cp.stdout)
1437
1438 def test_check_output_stdin_with_input_arg(self):
1439 # run() refuses to accept 'stdin' with 'input'
1440 tf = tempfile.TemporaryFile()
1441 self.addCleanup(tf.close)
1442 tf.write(b'pear')
1443 tf.seek(0)
1444 with self.assertRaises(ValueError,
1445 msg="Expected ValueError when stdin and input args supplied.") as c:
1446 output = self.run_python("print('will not be run')",
1447 stdin=tf, input=b'hare')
1448 self.assertIn('stdin', c.exception.args[0])
1449 self.assertIn('input', c.exception.args[0])
1450
1451 def test_check_output_timeout(self):
1452 with self.assertRaises(subprocess.TimeoutExpired) as c:
1453 cp = self.run_python((
1454 "import sys, time\n"
1455 "sys.stdout.write('BDFL')\n"
1456 "sys.stdout.flush()\n"
1457 "time.sleep(3600)"),
1458 # Some heavily loaded buildbots (sparc Debian 3.x) require
1459 # this much time to start and print.
1460 timeout=3, stdout=subprocess.PIPE)
1461 self.assertEqual(c.exception.output, b'BDFL')
1462 # output is aliased to stdout
1463 self.assertEqual(c.exception.stdout, b'BDFL')
1464
1465 def test_run_kwargs(self):
1466 newenv = os.environ.copy()
1467 newenv["FRUIT"] = "banana"
1468 cp = self.run_python(('import sys, os;'
1469 'sys.exit(33 if os.getenv("FRUIT")=="banana" else 31)'),
1470 env=newenv)
1471 self.assertEqual(cp.returncode, 33)
1472
Bo Baylesce0f33d2018-01-30 00:40:39 -06001473 def test_capture_output(self):
1474 cp = self.run_python(("import sys;"
1475 "sys.stdout.write('BDFL'); "
1476 "sys.stderr.write('FLUFL')"),
1477 capture_output=True)
1478 self.assertIn(b'BDFL', cp.stdout)
1479 self.assertIn(b'FLUFL', cp.stderr)
1480
1481 def test_stdout_with_capture_output_arg(self):
1482 # run() refuses to accept 'stdout' with 'capture_output'
1483 tf = tempfile.TemporaryFile()
1484 self.addCleanup(tf.close)
1485 with self.assertRaises(ValueError,
1486 msg=("Expected ValueError when stdout and capture_output "
1487 "args supplied.")) as c:
1488 output = self.run_python("print('will not be run')",
1489 capture_output=True, stdout=tf)
1490 self.assertIn('stdout', c.exception.args[0])
1491 self.assertIn('capture_output', c.exception.args[0])
1492
1493 def test_stderr_with_capture_output_arg(self):
1494 # run() refuses to accept 'stderr' with 'capture_output'
1495 tf = tempfile.TemporaryFile()
1496 self.addCleanup(tf.close)
1497 with self.assertRaises(ValueError,
1498 msg=("Expected ValueError when stderr and capture_output "
1499 "args supplied.")) as c:
1500 output = self.run_python("print('will not be run')",
1501 capture_output=True, stderr=tf)
1502 self.assertIn('stderr', c.exception.args[0])
1503 self.assertIn('capture_output', c.exception.args[0])
1504
Gregory P. Smith6e730002015-04-14 16:14:25 -07001505
Victor Stinner8fbbdf02018-06-22 19:25:44 +02001506@unittest.skipIf(support.MS_WINDOWS, "POSIX specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +00001507class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001508
Gregory P. Smith5591b022012-10-10 03:34:47 -07001509 def setUp(self):
1510 super().setUp()
1511 self._nonexistent_dir = "/_this/pa.th/does/not/exist"
1512
1513 def _get_chdir_exception(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001514 try:
Gregory P. Smith5591b022012-10-10 03:34:47 -07001515 os.chdir(self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001516 except OSError as e:
1517 # This avoids hard coding the errno value or the OS perror()
1518 # string and instead capture the exception that we want to see
1519 # below for comparison.
1520 desired_exception = e
Gregory P. Smith5591b022012-10-10 03:34:47 -07001521 desired_exception.strerror += ': ' + repr(self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001522 else:
Martin Pantereb995702016-07-28 01:11:04 +00001523 self.fail("chdir to nonexistent directory %s succeeded." %
Gregory P. Smith5591b022012-10-10 03:34:47 -07001524 self._nonexistent_dir)
1525 return desired_exception
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001526
Gregory P. Smith5591b022012-10-10 03:34:47 -07001527 def test_exception_cwd(self):
1528 """Test error in the child raised in the parent for a bad cwd."""
1529 desired_exception = self._get_chdir_exception()
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001530 try:
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001531 p = subprocess.Popen([sys.executable, "-c", ""],
Gregory P. Smith5591b022012-10-10 03:34:47 -07001532 cwd=self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001533 except OSError as e:
1534 # Test that the child process chdir failure actually makes
1535 # it up to the parent process as the correct exception.
1536 self.assertEqual(desired_exception.errno, e.errno)
1537 self.assertEqual(desired_exception.strerror, e.strerror)
1538 else:
1539 self.fail("Expected OSError: %s" % desired_exception)
1540
Gregory P. Smith5591b022012-10-10 03:34:47 -07001541 def test_exception_bad_executable(self):
1542 """Test error in the child raised in the parent for a bad executable."""
1543 desired_exception = self._get_chdir_exception()
1544 try:
1545 p = subprocess.Popen([sys.executable, "-c", ""],
1546 executable=self._nonexistent_dir)
1547 except OSError as e:
1548 # Test that the child process exec failure actually makes
1549 # it up to the parent process as the correct exception.
1550 self.assertEqual(desired_exception.errno, e.errno)
1551 self.assertEqual(desired_exception.strerror, e.strerror)
1552 else:
1553 self.fail("Expected OSError: %s" % desired_exception)
1554
1555 def test_exception_bad_args_0(self):
1556 """Test error in the child raised in the parent for a bad args[0]."""
1557 desired_exception = self._get_chdir_exception()
1558 try:
1559 p = subprocess.Popen([self._nonexistent_dir, "-c", ""])
1560 except OSError as e:
1561 # Test that the child process exec failure actually makes
1562 # it up to the parent process as the correct exception.
1563 self.assertEqual(desired_exception.errno, e.errno)
1564 self.assertEqual(desired_exception.strerror, e.strerror)
1565 else:
1566 self.fail("Expected OSError: %s" % desired_exception)
1567
Ammar Askar3fc499b2017-09-06 02:41:30 -04001568 # We mock the __del__ method for Popen in the next two tests
1569 # because it does cleanup based on the pid returned by fork_exec
1570 # along with issuing a resource warning if it still exists. Since
1571 # we don't actually spawn a process in these tests we can forego
1572 # the destructor. An alternative would be to set _child_created to
1573 # False before the destructor is called but there is no easy way
1574 # to do that
1575 class PopenNoDestructor(subprocess.Popen):
1576 def __del__(self):
1577 pass
1578
1579 @mock.patch("subprocess._posixsubprocess.fork_exec")
1580 def test_exception_errpipe_normal(self, fork_exec):
1581 """Test error passing done through errpipe_write in the good case"""
1582 def proper_error(*args):
1583 errpipe_write = args[13]
1584 # Write the hex for the error code EISDIR: 'is a directory'
1585 err_code = '{:x}'.format(errno.EISDIR).encode()
1586 os.write(errpipe_write, b"OSError:" + err_code + b":")
1587 return 0
1588
1589 fork_exec.side_effect = proper_error
1590
Victor Stinner11045c92017-10-05 06:32:53 -07001591 with mock.patch("subprocess.os.waitpid",
1592 side_effect=ChildProcessError):
1593 with self.assertRaises(IsADirectoryError):
1594 self.PopenNoDestructor(["non_existent_command"])
Ammar Askar3fc499b2017-09-06 02:41:30 -04001595
1596 @mock.patch("subprocess._posixsubprocess.fork_exec")
1597 def test_exception_errpipe_bad_data(self, fork_exec):
1598 """Test error passing done through errpipe_write where its not
1599 in the expected format"""
1600 error_data = b"\xFF\x00\xDE\xAD"
1601 def bad_error(*args):
1602 errpipe_write = args[13]
1603 # Anything can be in the pipe, no assumptions should
1604 # be made about its encoding, so we'll write some
1605 # arbitrary hex bytes to test it out
1606 os.write(errpipe_write, error_data)
1607 return 0
1608
1609 fork_exec.side_effect = bad_error
1610
Victor Stinner11045c92017-10-05 06:32:53 -07001611 with mock.patch("subprocess.os.waitpid",
1612 side_effect=ChildProcessError):
1613 with self.assertRaises(subprocess.SubprocessError) as e:
1614 self.PopenNoDestructor(["non_existent_command"])
Ammar Askar3fc499b2017-09-06 02:41:30 -04001615
1616 self.assertIn(repr(error_data), str(e.exception))
1617
Gregory P. Smith5f3d04f2018-06-05 12:00:57 -07001618 @unittest.skipIf(not os.path.exists('/proc/self/status'),
1619 "need /proc/self/status")
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001620 def test_restore_signals(self):
Gregory P. Smith5f3d04f2018-06-05 12:00:57 -07001621 # Blindly assume that cat exists on systems with /proc/self/status...
1622 default_proc_status = subprocess.check_output(
1623 ['cat', '/proc/self/status'],
1624 restore_signals=False)
1625 for line in default_proc_status.splitlines():
1626 if line.startswith(b'SigIgn'):
1627 default_sig_ign_mask = line
1628 break
1629 else:
1630 self.skipTest("SigIgn not found in /proc/self/status.")
1631 restored_proc_status = subprocess.check_output(
1632 ['cat', '/proc/self/status'],
1633 restore_signals=True)
1634 for line in restored_proc_status.splitlines():
1635 if line.startswith(b'SigIgn'):
1636 restored_sig_ign_mask = line
1637 break
1638 self.assertNotEqual(default_sig_ign_mask, restored_sig_ign_mask,
1639 msg="restore_signals=True should've unblocked "
1640 "SIGPIPE and friends.")
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001641
1642 def test_start_new_session(self):
1643 # For code coverage of calling setsid(). We don't care if we get an
1644 # EPERM error from it depending on the test execution environment, that
1645 # still indicates that it was called.
1646 try:
1647 output = subprocess.check_output(
1648 [sys.executable, "-c",
1649 "import os; print(os.getpgid(os.getpid()))"],
1650 start_new_session=True)
1651 except OSError as e:
1652 if e.errno != errno.EPERM:
1653 raise
1654 else:
1655 parent_pgid = os.getpgid(os.getpid())
1656 child_pgid = int(output)
1657 self.assertNotEqual(parent_pgid, child_pgid)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001658
1659 def test_run_abort(self):
1660 # returncode handles signal termination
Antoine Pitrou77e904e2013-10-08 23:04:32 +02001661 with support.SuppressCrashReport():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001662 p = subprocess.Popen([sys.executable, "-c",
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001663 'import os; os.abort()'])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001664 p.wait()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001665 self.assertEqual(-p.returncode, signal.SIGABRT)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001666
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)d6da7602016-06-03 06:14:06 +00001667 def test_CalledProcessError_str_signal(self):
1668 err = subprocess.CalledProcessError(-int(signal.SIGABRT), "fake cmd")
1669 error_string = str(err)
1670 # We're relying on the repr() of the signal.Signals intenum to provide
1671 # the word signal, the signal name and the numeric value.
1672 self.assertIn("signal", error_string.lower())
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)b4149062016-06-03 06:19:35 +00001673 # We're not being specific about the signal name as some signals have
1674 # multiple names and which name is revealed can vary.
1675 self.assertIn("SIG", error_string)
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)d6da7602016-06-03 06:14:06 +00001676 self.assertIn(str(signal.SIGABRT), error_string)
1677
1678 def test_CalledProcessError_str_unknown_signal(self):
1679 err = subprocess.CalledProcessError(-9876543, "fake cmd")
1680 error_string = str(err)
1681 self.assertIn("unknown signal 9876543.", error_string)
1682
1683 def test_CalledProcessError_str_non_zero(self):
1684 err = subprocess.CalledProcessError(2, "fake cmd")
1685 error_string = str(err)
1686 self.assertIn("non-zero exit status 2.", error_string)
1687
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001688 def test_preexec(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001689 # DISCLAIMER: Setting environment variables is *not* a good use
1690 # of a preexec_fn. This is merely a test.
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001691 p = subprocess.Popen([sys.executable, "-c",
1692 'import sys,os;'
1693 'sys.stdout.write(os.getenv("FRUIT"))'],
1694 stdout=subprocess.PIPE,
1695 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Victor Stinner7438c612016-05-20 12:43:15 +02001696 with p:
1697 self.assertEqual(p.stdout.read(), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001698
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001699 def test_preexec_exception(self):
1700 def raise_it():
1701 raise ValueError("What if two swallows carried a coconut?")
1702 try:
1703 p = subprocess.Popen([sys.executable, "-c", ""],
1704 preexec_fn=raise_it)
Gregory P. Smith8d07c262012-11-10 23:53:47 -08001705 except subprocess.SubprocessError as e:
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001706 self.assertTrue(
1707 subprocess._posixsubprocess,
1708 "Expected a ValueError from the preexec_fn")
1709 except ValueError as e:
1710 self.assertIn("coconut", e.args[0])
1711 else:
1712 self.fail("Exception raised by preexec_fn did not make it "
1713 "to the parent process.")
1714
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001715 class _TestExecuteChildPopen(subprocess.Popen):
1716 """Used to test behavior at the end of _execute_child."""
1717 def __init__(self, testcase, *args, **kwargs):
1718 self._testcase = testcase
1719 subprocess.Popen.__init__(self, *args, **kwargs)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001720
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001721 def _execute_child(self, *args, **kwargs):
Gregory P. Smith12489d92012-11-11 01:37:02 -08001722 try:
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001723 subprocess.Popen._execute_child(self, *args, **kwargs)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001724 finally:
1725 # Open a bunch of file descriptors and verify that
1726 # none of them are the same as the ones the Popen
1727 # instance is using for stdin/stdout/stderr.
1728 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
1729 for _ in range(8)]
1730 try:
1731 for fd in devzero_fds:
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001732 self._testcase.assertNotIn(
1733 fd, (self.stdin.fileno(), self.stdout.fileno(),
1734 self.stderr.fileno()),
Gregory P. Smith12489d92012-11-11 01:37:02 -08001735 msg="At least one fd was closed early.")
1736 finally:
Richard Oudkerk0e547b62013-06-10 16:29:19 +01001737 for fd in devzero_fds:
1738 os.close(fd)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001739
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001740 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
1741 def test_preexec_errpipe_does_not_double_close_pipes(self):
1742 """Issue16140: Don't double close pipes on preexec error."""
1743
1744 def raise_it():
Gregory P. Smith65ee6ec2012-11-11 10:12:40 -08001745 raise subprocess.SubprocessError(
1746 "force the _execute_child() errpipe_data path.")
Gregory P. Smith12489d92012-11-11 01:37:02 -08001747
Gregory P. Smithc2c4cb62012-11-11 01:41:49 -08001748 with self.assertRaises(subprocess.SubprocessError):
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001749 self._TestExecuteChildPopen(
1750 self, [sys.executable, "-c", "pass"],
Gregory P. Smith12489d92012-11-11 01:37:02 -08001751 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1752 stderr=subprocess.PIPE, preexec_fn=raise_it)
1753
Gregory P. Smith32ec9da2010-03-19 16:53:08 +00001754 def test_preexec_gc_module_failure(self):
1755 # This tests the code that disables garbage collection if the child
1756 # process will execute any Python.
1757 def raise_runtime_error():
1758 raise RuntimeError("this shouldn't escape")
1759 enabled = gc.isenabled()
1760 orig_gc_disable = gc.disable
1761 orig_gc_isenabled = gc.isenabled
1762 try:
1763 gc.disable()
1764 self.assertFalse(gc.isenabled())
1765 subprocess.call([sys.executable, '-c', ''],
1766 preexec_fn=lambda: None)
1767 self.assertFalse(gc.isenabled(),
1768 "Popen enabled gc when it shouldn't.")
1769
1770 gc.enable()
1771 self.assertTrue(gc.isenabled())
1772 subprocess.call([sys.executable, '-c', ''],
1773 preexec_fn=lambda: None)
1774 self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
1775
1776 gc.disable = raise_runtime_error
1777 self.assertRaises(RuntimeError, subprocess.Popen,
1778 [sys.executable, '-c', ''],
1779 preexec_fn=lambda: None)
1780
1781 del gc.isenabled # force an AttributeError
1782 self.assertRaises(AttributeError, subprocess.Popen,
1783 [sys.executable, '-c', ''],
1784 preexec_fn=lambda: None)
1785 finally:
1786 gc.disable = orig_gc_disable
1787 gc.isenabled = orig_gc_isenabled
1788 if not enabled:
1789 gc.disable()
1790
Martin Panterf7fdbda2015-12-05 09:51:52 +00001791 @unittest.skipIf(
1792 sys.platform == 'darwin', 'setrlimit() seems to fail on OS X')
Martin Panterafdd5132015-11-30 02:21:41 +00001793 def test_preexec_fork_failure(self):
1794 # The internal code did not preserve the previous exception when
1795 # re-enabling garbage collection
1796 try:
1797 from resource import getrlimit, setrlimit, RLIMIT_NPROC
1798 except ImportError as err:
1799 self.skipTest(err) # RLIMIT_NPROC is specific to Linux and BSD
1800 limits = getrlimit(RLIMIT_NPROC)
1801 [_, hard] = limits
1802 setrlimit(RLIMIT_NPROC, (0, hard))
1803 self.addCleanup(setrlimit, RLIMIT_NPROC, limits)
Martin Panter5cf791b2015-12-11 05:40:14 +00001804 try:
Martin Panterafdd5132015-11-30 02:21:41 +00001805 subprocess.call([sys.executable, '-c', ''],
1806 preexec_fn=lambda: None)
Martin Panter5cf791b2015-12-11 05:40:14 +00001807 except BlockingIOError:
1808 # Forking should raise EAGAIN, translated to BlockingIOError
1809 pass
1810 else:
1811 self.skipTest('RLIMIT_NPROC had no effect; probably superuser')
Martin Panterafdd5132015-11-30 02:21:41 +00001812
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001813 def test_args_string(self):
1814 # args is a string
Berker Peksag16a1f282015-09-28 13:33:14 +03001815 fd, fname = tempfile.mkstemp()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001816 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +00001817 with open(fd, "w", errors="surrogateescape") as fobj:
Xavier de Gayed1415312016-07-22 12:15:29 +02001818 fobj.write("#!%s\n" % support.unix_shell)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001819 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
1820 sys.executable)
1821 os.chmod(fname, 0o700)
1822 p = subprocess.Popen(fname)
1823 p.wait()
1824 os.remove(fname)
1825 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001826
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001827 def test_invalid_args(self):
1828 # invalid arguments should raise ValueError
1829 self.assertRaises(ValueError, subprocess.call,
1830 [sys.executable, "-c",
1831 "import sys; sys.exit(47)"],
1832 startupinfo=47)
1833 self.assertRaises(ValueError, subprocess.call,
1834 [sys.executable, "-c",
1835 "import sys; sys.exit(47)"],
1836 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001837
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001838 def test_shell_sequence(self):
1839 # Run command through the shell (sequence)
1840 newenv = os.environ.copy()
1841 newenv["FRUIT"] = "apple"
1842 p = subprocess.Popen(["echo $FRUIT"], shell=1,
1843 stdout=subprocess.PIPE,
1844 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02001845 with p:
1846 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001847
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001848 def test_shell_string(self):
1849 # Run command through the shell (string)
1850 newenv = os.environ.copy()
1851 newenv["FRUIT"] = "apple"
1852 p = subprocess.Popen("echo $FRUIT", shell=1,
1853 stdout=subprocess.PIPE,
1854 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02001855 with p:
1856 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Christian Heimesa342c012008-04-20 21:01:16 +00001857
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001858 def test_call_string(self):
1859 # call() function with string argument on UNIX
Berker Peksag16a1f282015-09-28 13:33:14 +03001860 fd, fname = tempfile.mkstemp()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001861 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +00001862 with open(fd, "w", errors="surrogateescape") as fobj:
Xavier de Gayed1415312016-07-22 12:15:29 +02001863 fobj.write("#!%s\n" % support.unix_shell)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001864 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
1865 sys.executable)
1866 os.chmod(fname, 0o700)
1867 rc = subprocess.call(fname)
1868 os.remove(fname)
1869 self.assertEqual(rc, 47)
Christian Heimesa342c012008-04-20 21:01:16 +00001870
Stefan Krah9542cc62010-07-19 14:20:53 +00001871 def test_specific_shell(self):
1872 # Issue #9265: Incorrect name passed as arg[0].
1873 shells = []
1874 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
1875 for name in ['bash', 'ksh']:
1876 sh = os.path.join(prefix, name)
1877 if os.path.isfile(sh):
1878 shells.append(sh)
1879 if not shells: # Will probably work for any shell but csh.
1880 self.skipTest("bash or ksh required for this test")
1881 sh = '/bin/sh'
1882 if os.path.isfile(sh) and not os.path.islink(sh):
1883 # Test will fail if /bin/sh is a symlink to csh.
1884 shells.append(sh)
1885 for sh in shells:
1886 p = subprocess.Popen("echo $0", executable=sh, shell=True,
1887 stdout=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +02001888 with p:
1889 self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
Stefan Krah9542cc62010-07-19 14:20:53 +00001890
Florent Xicluna4886d242010-03-08 13:27:26 +00001891 def _kill_process(self, method, *args):
Florent Xicluna1d8ee3a2010-03-05 20:26:54 +00001892 # Do not inherit file handles from the parent.
1893 # It should fix failures on some platforms.
Gregory P. Smithdee04342013-08-29 13:35:27 -07001894 # Also set the SIGINT handler to the default to make sure it's not
1895 # being ignored (some tests rely on that.)
1896 old_handler = signal.signal(signal.SIGINT, signal.default_int_handler)
1897 try:
1898 p = subprocess.Popen([sys.executable, "-c", """if 1:
1899 import sys, time
1900 sys.stdout.write('x\\n')
1901 sys.stdout.flush()
1902 time.sleep(30)
1903 """],
1904 close_fds=True,
1905 stdin=subprocess.PIPE,
1906 stdout=subprocess.PIPE,
1907 stderr=subprocess.PIPE)
1908 finally:
1909 signal.signal(signal.SIGINT, old_handler)
Antoine Pitrou3d8580f2010-09-20 01:33:21 +00001910 # Wait for the interpreter to be completely initialized before
1911 # sending any signal.
1912 p.stdout.read(1)
1913 getattr(p, method)(*args)
Florent Xicluna4886d242010-03-08 13:27:26 +00001914 return p
1915
Charles-François Natali53221e32013-01-12 16:52:20 +01001916 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
1917 "Due to known OS bug (issue #16762)")
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001918 def _kill_dead_process(self, method, *args):
1919 # Do not inherit file handles from the parent.
1920 # It should fix failures on some platforms.
1921 p = subprocess.Popen([sys.executable, "-c", """if 1:
1922 import sys, time
1923 sys.stdout.write('x\\n')
1924 sys.stdout.flush()
1925 """],
1926 close_fds=True,
1927 stdin=subprocess.PIPE,
1928 stdout=subprocess.PIPE,
1929 stderr=subprocess.PIPE)
1930 # Wait for the interpreter to be completely initialized before
1931 # sending any signal.
1932 p.stdout.read(1)
1933 # The process should end after this
1934 time.sleep(1)
1935 # This shouldn't raise even though the child is now dead
1936 getattr(p, method)(*args)
1937 p.communicate()
1938
Florent Xicluna4886d242010-03-08 13:27:26 +00001939 def test_send_signal(self):
1940 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunac049d872010-03-27 22:47:23 +00001941 _, stderr = p.communicate()
1942 self.assertIn(b'KeyboardInterrupt', stderr)
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001943 self.assertNotEqual(p.wait(), 0)
Christian Heimesa342c012008-04-20 21:01:16 +00001944
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001945 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001946 p = self._kill_process('kill')
Florent Xiclunac049d872010-03-27 22:47:23 +00001947 _, stderr = p.communicate()
1948 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001949 self.assertEqual(p.wait(), -signal.SIGKILL)
Tim Peterse718f612004-10-12 21:51:32 +00001950
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001951 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001952 p = self._kill_process('terminate')
Florent Xiclunac049d872010-03-27 22:47:23 +00001953 _, stderr = p.communicate()
1954 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001955 self.assertEqual(p.wait(), -signal.SIGTERM)
1956
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001957 def test_send_signal_dead(self):
1958 # Sending a signal to a dead process
1959 self._kill_dead_process('send_signal', signal.SIGINT)
1960
1961 def test_kill_dead(self):
1962 # Killing a dead process
1963 self._kill_dead_process('kill')
1964
1965 def test_terminate_dead(self):
1966 # Terminating a dead process
1967 self._kill_dead_process('terminate')
1968
Victor Stinnerdaf45552013-08-28 00:53:59 +02001969 def _save_fds(self, save_fds):
1970 fds = []
1971 for fd in save_fds:
1972 inheritable = os.get_inheritable(fd)
1973 saved = os.dup(fd)
1974 fds.append((fd, saved, inheritable))
1975 return fds
1976
1977 def _restore_fds(self, fds):
1978 for fd, saved, inheritable in fds:
1979 os.dup2(saved, fd, inheritable=inheritable)
1980 os.close(saved)
1981
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001982 def check_close_std_fds(self, fds):
1983 # Issue #9905: test that subprocess pipes still work properly with
1984 # some standard fds closed
1985 stdin = 0
Victor Stinnerdaf45552013-08-28 00:53:59 +02001986 saved_fds = self._save_fds(fds)
1987 for fd, saved, inheritable in saved_fds:
1988 if fd == 0:
1989 stdin = saved
1990 break
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001991 try:
1992 for fd in fds:
1993 os.close(fd)
1994 out, err = subprocess.Popen([sys.executable, "-c",
1995 'import sys;'
1996 'sys.stdout.write("apple");'
1997 'sys.stdout.flush();'
1998 'sys.stderr.write("orange")'],
1999 stdin=stdin,
2000 stdout=subprocess.PIPE,
2001 stderr=subprocess.PIPE).communicate()
2002 err = support.strip_python_stderr(err)
2003 self.assertEqual((out, err), (b'apple', b'orange'))
2004 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02002005 self._restore_fds(saved_fds)
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00002006
2007 def test_close_fd_0(self):
2008 self.check_close_std_fds([0])
2009
2010 def test_close_fd_1(self):
2011 self.check_close_std_fds([1])
2012
2013 def test_close_fd_2(self):
2014 self.check_close_std_fds([2])
2015
2016 def test_close_fds_0_1(self):
2017 self.check_close_std_fds([0, 1])
2018
2019 def test_close_fds_0_2(self):
2020 self.check_close_std_fds([0, 2])
2021
2022 def test_close_fds_1_2(self):
2023 self.check_close_std_fds([1, 2])
2024
2025 def test_close_fds_0_1_2(self):
2026 # Issue #10806: test that subprocess pipes still work properly with
2027 # all standard fds closed.
2028 self.check_close_std_fds([0, 1, 2])
2029
Gregory P. Smith53dd8162013-12-01 16:03:24 -08002030 def test_small_errpipe_write_fd(self):
2031 """Issue #15798: Popen should work when stdio fds are available."""
2032 new_stdin = os.dup(0)
2033 new_stdout = os.dup(1)
2034 try:
2035 os.close(0)
2036 os.close(1)
2037
2038 # Side test: if errpipe_write fails to have its CLOEXEC
2039 # flag set this should cause the parent to think the exec
2040 # failed. Extremely unlikely: everyone supports CLOEXEC.
2041 subprocess.Popen([
2042 sys.executable, "-c",
2043 "print('AssertionError:0:CLOEXEC failure.')"]).wait()
2044 finally:
2045 # Restore original stdin and stdout
2046 os.dup2(new_stdin, 0)
2047 os.dup2(new_stdout, 1)
2048 os.close(new_stdin)
2049 os.close(new_stdout)
2050
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002051 def test_remapping_std_fds(self):
2052 # open up some temporary files
Berker Peksag16a1f282015-09-28 13:33:14 +03002053 temps = [tempfile.mkstemp() for i in range(3)]
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002054 try:
2055 temp_fds = [fd for fd, fname in temps]
2056
2057 # unlink the files -- we won't need to reopen them
2058 for fd, fname in temps:
2059 os.unlink(fname)
2060
2061 # write some data to what will become stdin, and rewind
2062 os.write(temp_fds[1], b"STDIN")
2063 os.lseek(temp_fds[1], 0, 0)
2064
2065 # move the standard file descriptors out of the way
Victor Stinnerdaf45552013-08-28 00:53:59 +02002066 saved_fds = self._save_fds(range(3))
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002067 try:
2068 # duplicate the file objects over the standard fd's
2069 for fd, temp_fd in enumerate(temp_fds):
2070 os.dup2(temp_fd, fd)
2071
2072 # now use those files in the "wrong" order, so that subprocess
2073 # has to rearrange them in the child
2074 p = subprocess.Popen([sys.executable, "-c",
2075 'import sys; got = sys.stdin.read();'
2076 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
2077 stdin=temp_fds[1],
2078 stdout=temp_fds[2],
2079 stderr=temp_fds[0])
2080 p.wait()
2081 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02002082 self._restore_fds(saved_fds)
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002083
2084 for fd in temp_fds:
2085 os.lseek(fd, 0, 0)
2086
2087 out = os.read(temp_fds[2], 1024)
2088 err = support.strip_python_stderr(os.read(temp_fds[0], 1024))
2089 self.assertEqual(out, b"got STDIN")
2090 self.assertEqual(err, b"err")
2091
2092 finally:
2093 for fd in temp_fds:
2094 os.close(fd)
2095
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002096 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
2097 # open up some temporary files
Berker Peksag16a1f282015-09-28 13:33:14 +03002098 temps = [tempfile.mkstemp() for i in range(3)]
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002099 temp_fds = [fd for fd, fname in temps]
2100 try:
2101 # unlink the files -- we won't need to reopen them
2102 for fd, fname in temps:
2103 os.unlink(fname)
2104
2105 # save a copy of the standard file descriptors
Victor Stinnerdaf45552013-08-28 00:53:59 +02002106 saved_fds = self._save_fds(range(3))
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002107 try:
2108 # duplicate the temp files over the standard fd's 0, 1, 2
2109 for fd, temp_fd in enumerate(temp_fds):
2110 os.dup2(temp_fd, fd)
2111
2112 # write some data to what will become stdin, and rewind
2113 os.write(stdin_no, b"STDIN")
2114 os.lseek(stdin_no, 0, 0)
2115
2116 # now use those files in the given order, so that subprocess
2117 # has to rearrange them in the child
2118 p = subprocess.Popen([sys.executable, "-c",
2119 'import sys; got = sys.stdin.read();'
2120 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
2121 stdin=stdin_no,
2122 stdout=stdout_no,
2123 stderr=stderr_no)
2124 p.wait()
2125
2126 for fd in temp_fds:
2127 os.lseek(fd, 0, 0)
2128
2129 out = os.read(stdout_no, 1024)
2130 err = support.strip_python_stderr(os.read(stderr_no, 1024))
2131 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02002132 self._restore_fds(saved_fds)
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002133
2134 self.assertEqual(out, b"got STDIN")
2135 self.assertEqual(err, b"err")
2136
2137 finally:
2138 for fd in temp_fds:
2139 os.close(fd)
2140
2141 # When duping fds, if there arises a situation where one of the fds is
2142 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
2143 # This tests all combinations of this.
2144 def test_swap_fds(self):
2145 self.check_swap_fds(0, 1, 2)
2146 self.check_swap_fds(0, 2, 1)
2147 self.check_swap_fds(1, 0, 2)
2148 self.check_swap_fds(1, 2, 0)
2149 self.check_swap_fds(2, 0, 1)
2150 self.check_swap_fds(2, 1, 0)
2151
Alexey Izbyshev0e7144b2018-03-26 22:49:35 +03002152 def _check_swap_std_fds_with_one_closed(self, from_fds, to_fds):
2153 saved_fds = self._save_fds(range(3))
2154 try:
2155 for from_fd in from_fds:
2156 with tempfile.TemporaryFile() as f:
2157 os.dup2(f.fileno(), from_fd)
2158
2159 fd_to_close = (set(range(3)) - set(from_fds)).pop()
2160 os.close(fd_to_close)
2161
2162 arg_names = ['stdin', 'stdout', 'stderr']
2163 kwargs = {}
2164 for from_fd, to_fd in zip(from_fds, to_fds):
2165 kwargs[arg_names[to_fd]] = from_fd
2166
2167 code = textwrap.dedent(r'''
2168 import os, sys
2169 skipped_fd = int(sys.argv[1])
2170 for fd in range(3):
2171 if fd != skipped_fd:
2172 os.write(fd, str(fd).encode('ascii'))
2173 ''')
2174
2175 skipped_fd = (set(range(3)) - set(to_fds)).pop()
2176
2177 rc = subprocess.call([sys.executable, '-c', code, str(skipped_fd)],
2178 **kwargs)
2179 self.assertEqual(rc, 0)
2180
2181 for from_fd, to_fd in zip(from_fds, to_fds):
2182 os.lseek(from_fd, 0, os.SEEK_SET)
2183 read_bytes = os.read(from_fd, 1024)
2184 read_fds = list(map(int, read_bytes.decode('ascii')))
2185 msg = textwrap.dedent(f"""
2186 When testing {from_fds} to {to_fds} redirection,
2187 parent descriptor {from_fd} got redirected
2188 to descriptor(s) {read_fds} instead of descriptor {to_fd}.
2189 """)
2190 self.assertEqual([to_fd], read_fds, msg)
2191 finally:
2192 self._restore_fds(saved_fds)
2193
2194 # Check that subprocess can remap std fds correctly even
2195 # if one of them is closed (#32844).
2196 def test_swap_std_fds_with_one_closed(self):
2197 for from_fds in itertools.combinations(range(3), 2):
2198 for to_fds in itertools.permutations(range(3), 2):
2199 self._check_swap_std_fds_with_one_closed(from_fds, to_fds)
2200
Victor Stinner13bb71c2010-04-23 21:41:56 +00002201 def test_surrogates_error_message(self):
Victor Stinner4d078042010-04-23 19:28:32 +00002202 def prepare():
2203 raise ValueError("surrogate:\uDCff")
2204
2205 try:
2206 subprocess.call(
2207 [sys.executable, "-c", "pass"],
2208 preexec_fn=prepare)
2209 except ValueError as err:
2210 # Pure Python implementations keeps the message
2211 self.assertIsNone(subprocess._posixsubprocess)
2212 self.assertEqual(str(err), "surrogate:\uDCff")
Gregory P. Smith8d07c262012-11-10 23:53:47 -08002213 except subprocess.SubprocessError as err:
Victor Stinner4d078042010-04-23 19:28:32 +00002214 # _posixsubprocess uses a default message
2215 self.assertIsNotNone(subprocess._posixsubprocess)
2216 self.assertEqual(str(err), "Exception occurred in preexec_fn.")
2217 else:
Gregory P. Smith8d07c262012-11-10 23:53:47 -08002218 self.fail("Expected ValueError or subprocess.SubprocessError")
Victor Stinner4d078042010-04-23 19:28:32 +00002219
Victor Stinner13bb71c2010-04-23 21:41:56 +00002220 def test_undecodable_env(self):
2221 for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')):
Victor Stinner5323fb02013-11-19 23:46:06 +01002222 encoded_value = value.encode("ascii", "surrogateescape")
2223
Victor Stinner13bb71c2010-04-23 21:41:56 +00002224 # test str with surrogates
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00002225 script = "import os; print(ascii(os.getenv(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002226 env = os.environ.copy()
2227 env[key] = value
Victor Stinner5323fb02013-11-19 23:46:06 +01002228 # Use C locale to get ASCII for the locale encoding to force
Victor Stinner89f3ad12010-10-14 10:43:31 +00002229 # surrogate-escaping of \xFF in the child process; otherwise it can
2230 # be decoded as-is if the default locale is latin-1.
Victor Stinnerebc78d22010-10-14 10:38:17 +00002231 env['LC_ALL'] = 'C'
Victor Stinner5323fb02013-11-19 23:46:06 +01002232 if sys.platform.startswith("aix"):
2233 # On AIX, the C locale uses the Latin1 encoding
2234 decoded_value = encoded_value.decode("latin1", "surrogateescape")
2235 else:
2236 # On other UNIXes, the C locale uses the ASCII encoding
2237 decoded_value = value
Victor Stinner13bb71c2010-04-23 21:41:56 +00002238 stdout = subprocess.check_output(
2239 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002240 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00002241 stdout = stdout.rstrip(b'\n\r')
Victor Stinner5323fb02013-11-19 23:46:06 +01002242 self.assertEqual(stdout.decode('ascii'), ascii(decoded_value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00002243
2244 # test bytes
2245 key = key.encode("ascii", "surrogateescape")
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00002246 script = "import os; print(ascii(os.getenvb(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002247 env = os.environ.copy()
Victor Stinner5323fb02013-11-19 23:46:06 +01002248 env[key] = encoded_value
Victor Stinner13bb71c2010-04-23 21:41:56 +00002249 stdout = subprocess.check_output(
2250 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002251 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00002252 stdout = stdout.rstrip(b'\n\r')
Victor Stinner5323fb02013-11-19 23:46:06 +01002253 self.assertEqual(stdout.decode('ascii'), ascii(encoded_value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00002254
Victor Stinnerb745a742010-05-18 17:17:23 +00002255 def test_bytes_program(self):
2256 abs_program = os.fsencode(sys.executable)
2257 path, program = os.path.split(sys.executable)
2258 program = os.fsencode(program)
2259
2260 # absolute bytes path
2261 exitcode = subprocess.call([abs_program, "-c", "pass"])
Ezio Melottib3aedd42010-11-20 19:04:17 +00002262 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00002263
Victor Stinner7b3b20a2011-03-03 12:54:05 +00002264 # absolute bytes path as a string
2265 cmd = b"'" + abs_program + b"' -c pass"
2266 exitcode = subprocess.call(cmd, shell=True)
2267 self.assertEqual(exitcode, 0)
2268
Victor Stinnerb745a742010-05-18 17:17:23 +00002269 # bytes program, unicode PATH
2270 env = os.environ.copy()
2271 env["PATH"] = path
2272 exitcode = subprocess.call([program, "-c", "pass"], env=env)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002273 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00002274
2275 # bytes program, bytes PATH
2276 envb = os.environb.copy()
2277 envb[b"PATH"] = os.fsencode(path)
2278 exitcode = subprocess.call([program, "-c", "pass"], env=envb)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002279 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00002280
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002281 def test_pipe_cloexec(self):
2282 sleeper = support.findfile("input_reader.py", subdir="subprocessdata")
2283 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2284
2285 p1 = subprocess.Popen([sys.executable, sleeper],
2286 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2287 stderr=subprocess.PIPE, close_fds=False)
2288
2289 self.addCleanup(p1.communicate, b'')
2290
2291 p2 = subprocess.Popen([sys.executable, fd_status],
2292 stdout=subprocess.PIPE, close_fds=False)
2293
2294 output, error = p2.communicate()
2295 result_fds = set(map(int, output.split(b',')))
2296 unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(),
2297 p1.stderr.fileno()])
2298
2299 self.assertFalse(result_fds & unwanted_fds,
2300 "Expected no fds from %r to be open in child, "
2301 "found %r" %
2302 (unwanted_fds, result_fds & unwanted_fds))
2303
2304 def test_pipe_cloexec_real_tools(self):
2305 qcat = support.findfile("qcat.py", subdir="subprocessdata")
2306 qgrep = support.findfile("qgrep.py", subdir="subprocessdata")
2307
2308 subdata = b'zxcvbn'
2309 data = subdata * 4 + b'\n'
2310
2311 p1 = subprocess.Popen([sys.executable, qcat],
2312 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2313 close_fds=False)
2314
2315 p2 = subprocess.Popen([sys.executable, qgrep, subdata],
2316 stdin=p1.stdout, stdout=subprocess.PIPE,
2317 close_fds=False)
2318
2319 self.addCleanup(p1.wait)
2320 self.addCleanup(p2.wait)
Gregory P. Smith886455c2012-01-21 22:05:10 -08002321 def kill_p1():
2322 try:
2323 p1.terminate()
2324 except ProcessLookupError:
2325 pass
2326 def kill_p2():
2327 try:
2328 p2.terminate()
2329 except ProcessLookupError:
2330 pass
2331 self.addCleanup(kill_p1)
2332 self.addCleanup(kill_p2)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002333
2334 p1.stdin.write(data)
2335 p1.stdin.close()
2336
2337 readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10)
2338
2339 self.assertTrue(readfiles, "The child hung")
2340 self.assertEqual(p2.stdout.read(), data)
2341
Victor Stinnerfaa8c132011-01-03 16:36:00 +00002342 p1.stdout.close()
2343 p2.stdout.close()
2344
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002345 def test_close_fds(self):
2346 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2347
2348 fds = os.pipe()
2349 self.addCleanup(os.close, fds[0])
2350 self.addCleanup(os.close, fds[1])
2351
2352 open_fds = set(fds)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002353 # add a bunch more fds
2354 for _ in range(9):
Serhiy Storchaka85c30332015-02-15 13:58:23 +02002355 fd = os.open(os.devnull, os.O_RDONLY)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002356 self.addCleanup(os.close, fd)
2357 open_fds.add(fd)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002358
Victor Stinnerdaf45552013-08-28 00:53:59 +02002359 for fd in open_fds:
2360 os.set_inheritable(fd, True)
2361
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002362 p = subprocess.Popen([sys.executable, fd_status],
2363 stdout=subprocess.PIPE, close_fds=False)
2364 output, ignored = p.communicate()
2365 remaining_fds = set(map(int, output.split(b',')))
2366
2367 self.assertEqual(remaining_fds & open_fds, open_fds,
2368 "Some fds were closed")
2369
2370 p = subprocess.Popen([sys.executable, fd_status],
2371 stdout=subprocess.PIPE, close_fds=True)
2372 output, ignored = p.communicate()
2373 remaining_fds = set(map(int, output.split(b',')))
2374
2375 self.assertFalse(remaining_fds & open_fds,
2376 "Some fds were left open")
2377 self.assertIn(1, remaining_fds, "Subprocess failed")
2378
Gregory P. Smith8facece2012-01-21 14:01:08 -08002379 # Keep some of the fd's we opened open in the subprocess.
2380 # This tests _posixsubprocess.c's proper handling of fds_to_keep.
2381 fds_to_keep = set(open_fds.pop() for _ in range(8))
2382 p = subprocess.Popen([sys.executable, fd_status],
2383 stdout=subprocess.PIPE, close_fds=True,
izbyshev2d8f0632017-12-19 03:26:49 +07002384 pass_fds=fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002385 output, ignored = p.communicate()
2386 remaining_fds = set(map(int, output.split(b',')))
2387
izbyshev2d8f0632017-12-19 03:26:49 +07002388 self.assertFalse((remaining_fds - fds_to_keep) & open_fds,
Gregory P. Smith8facece2012-01-21 14:01:08 -08002389 "Some fds not in pass_fds were left open")
2390 self.assertIn(1, remaining_fds, "Subprocess failed")
2391
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002392
Gregory P. Smithd04f6992014-06-01 15:27:28 -07002393 @unittest.skipIf(sys.platform.startswith("freebsd") and
2394 os.stat("/dev").st_dev == os.stat("/dev/fd").st_dev,
2395 "Requires fdescfs mounted on /dev/fd on FreeBSD.")
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002396 def test_close_fds_when_max_fd_is_lowered(self):
2397 """Confirm that issue21618 is fixed (may fail under valgrind)."""
2398 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2399
Gregory P. Smith634aa682014-06-15 17:51:04 -07002400 # This launches the meat of the test in a child process to
2401 # avoid messing with the larger unittest processes maximum
2402 # number of file descriptors.
2403 # This process launches:
2404 # +--> Process that lowers its RLIMIT_NOFILE aftr setting up
2405 # a bunch of high open fds above the new lower rlimit.
2406 # Those are reported via stdout before launching a new
2407 # process with close_fds=False to run the actual test:
2408 # +--> The TEST: This one launches a fd_status.py
2409 # subprocess with close_fds=True so we can find out if
2410 # any of the fds above the lowered rlimit are still open.
2411 p = subprocess.Popen([sys.executable, '-c', textwrap.dedent(
2412 '''
2413 import os, resource, subprocess, sys, textwrap
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002414 open_fds = set()
2415 # Add a bunch more fds to pass down.
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002416 for _ in range(40):
Serhiy Storchaka85c30332015-02-15 13:58:23 +02002417 fd = os.open(os.devnull, os.O_RDONLY)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002418 open_fds.add(fd)
2419
2420 # Leave a two pairs of low ones available for use by the
2421 # internal child error pipe and the stdout pipe.
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002422 # We also leave 10 more open as some Python buildbots run into
2423 # "too many open files" errors during the test if we do not.
2424 for fd in sorted(open_fds)[:14]:
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002425 os.close(fd)
2426 open_fds.remove(fd)
2427
2428 for fd in open_fds:
Gregory P. Smith634aa682014-06-15 17:51:04 -07002429 #self.addCleanup(os.close, fd)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002430 os.set_inheritable(fd, True)
2431
2432 max_fd_open = max(open_fds)
2433
Gregory P. Smith634aa682014-06-15 17:51:04 -07002434 # Communicate the open_fds to the parent unittest.TestCase process.
2435 print(','.join(map(str, sorted(open_fds))))
2436 sys.stdout.flush()
2437
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002438 rlim_cur, rlim_max = resource.getrlimit(resource.RLIMIT_NOFILE)
2439 try:
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002440 # 29 is lower than the highest fds we are leaving open.
2441 resource.setrlimit(resource.RLIMIT_NOFILE, (29, rlim_max))
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002442 # Launch a new Python interpreter with our low fd rlim_cur that
2443 # inherits open fds above that limit. It then uses subprocess
2444 # with close_fds=True to get a report of open fds in the child.
2445 # An explicit list of fds to check is passed to fd_status.py as
2446 # letting fd_status rely on its default logic would miss the
2447 # fds above rlim_cur as it normally only checks up to that limit.
Gregory P. Smith634aa682014-06-15 17:51:04 -07002448 subprocess.Popen(
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002449 [sys.executable, '-c',
2450 textwrap.dedent("""
2451 import subprocess, sys
Gregory P. Smith634aa682014-06-15 17:51:04 -07002452 subprocess.Popen([sys.executable, %r] +
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002453 [str(x) for x in range({max_fd})],
Gregory P. Smithffd529c2014-06-01 13:46:54 -07002454 close_fds=True).wait()
Gregory P. Smith634aa682014-06-15 17:51:04 -07002455 """.format(max_fd=max_fd_open+1))],
2456 close_fds=False).wait()
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002457 finally:
2458 resource.setrlimit(resource.RLIMIT_NOFILE, (rlim_cur, rlim_max))
Gregory P. Smith634aa682014-06-15 17:51:04 -07002459 ''' % fd_status)], stdout=subprocess.PIPE)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002460
2461 output, unused_stderr = p.communicate()
Gregory P. Smith634aa682014-06-15 17:51:04 -07002462 output_lines = output.splitlines()
2463 self.assertEqual(len(output_lines), 2,
Gregory P. Smith9204e092014-06-15 20:16:01 -07002464 msg="expected exactly two lines of output:\n%r" % output)
Gregory P. Smith634aa682014-06-15 17:51:04 -07002465 opened_fds = set(map(int, output_lines[0].strip().split(b',')))
2466 remaining_fds = set(map(int, output_lines[1].strip().split(b',')))
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002467
Gregory P. Smith634aa682014-06-15 17:51:04 -07002468 self.assertFalse(remaining_fds & opened_fds,
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002469 msg="Some fds were left open.")
2470
2471
Victor Stinner88701e22011-06-01 13:13:04 +02002472 # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
2473 # descriptor of a pipe closed in the parent process is valid in the
2474 # child process according to fstat(), but the mode of the file
2475 # descriptor is invalid, and read or write raise an error.
2476 @support.requires_mac_ver(10, 5)
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00002477 def test_pass_fds(self):
2478 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2479
2480 open_fds = set()
2481
2482 for x in range(5):
2483 fds = os.pipe()
2484 self.addCleanup(os.close, fds[0])
2485 self.addCleanup(os.close, fds[1])
Victor Stinnerdaf45552013-08-28 00:53:59 +02002486 os.set_inheritable(fds[0], True)
2487 os.set_inheritable(fds[1], True)
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00002488 open_fds.update(fds)
2489
2490 for fd in open_fds:
2491 p = subprocess.Popen([sys.executable, fd_status],
2492 stdout=subprocess.PIPE, close_fds=True,
2493 pass_fds=(fd, ))
2494 output, ignored = p.communicate()
2495
2496 remaining_fds = set(map(int, output.split(b',')))
2497 to_be_closed = open_fds - {fd}
2498
2499 self.assertIn(fd, remaining_fds, "fd to be passed not passed")
2500 self.assertFalse(remaining_fds & to_be_closed,
2501 "fd to be closed passed")
2502
2503 # pass_fds overrides close_fds with a warning.
2504 with self.assertWarns(RuntimeWarning) as context:
2505 self.assertFalse(subprocess.call(
2506 [sys.executable, "-c", "import sys; sys.exit(0)"],
2507 close_fds=False, pass_fds=(fd, )))
2508 self.assertIn('overriding close_fds', str(context.warning))
2509
Victor Stinnerdaf45552013-08-28 00:53:59 +02002510 def test_pass_fds_inheritable(self):
Victor Stinnerf6fa22e2013-09-01 10:22:41 +02002511 script = support.findfile("fd_status.py", subdir="subprocessdata")
Victor Stinnerdaf45552013-08-28 00:53:59 +02002512
2513 inheritable, non_inheritable = os.pipe()
2514 self.addCleanup(os.close, inheritable)
2515 self.addCleanup(os.close, non_inheritable)
2516 os.set_inheritable(inheritable, True)
2517 os.set_inheritable(non_inheritable, False)
2518 pass_fds = (inheritable, non_inheritable)
2519 args = [sys.executable, script]
2520 args += list(map(str, pass_fds))
2521
2522 p = subprocess.Popen(args,
2523 stdout=subprocess.PIPE, close_fds=True,
2524 pass_fds=pass_fds)
2525 output, ignored = p.communicate()
2526 fds = set(map(int, output.split(b',')))
2527
2528 # the inheritable file descriptor must be inherited, so its inheritable
2529 # flag must be set in the child process after fork() and before exec()
Victor Stinnerf6fa22e2013-09-01 10:22:41 +02002530 self.assertEqual(fds, set(pass_fds), "output=%a" % output)
Victor Stinnerdaf45552013-08-28 00:53:59 +02002531
2532 # inheritable flag must not be changed in the parent process
2533 self.assertEqual(os.get_inheritable(inheritable), True)
2534 self.assertEqual(os.get_inheritable(non_inheritable), False)
2535
Gregory P. Smith112bb3a2011-03-15 14:55:17 -04002536 def test_stdout_stdin_are_single_inout_fd(self):
2537 with io.open(os.devnull, "r+") as inout:
2538 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2539 stdout=inout, stdin=inout)
2540 p.wait()
2541
2542 def test_stdout_stderr_are_single_inout_fd(self):
2543 with io.open(os.devnull, "r+") as inout:
2544 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2545 stdout=inout, stderr=inout)
2546 p.wait()
2547
2548 def test_stderr_stdin_are_single_inout_fd(self):
2549 with io.open(os.devnull, "r+") as inout:
2550 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2551 stderr=inout, stdin=inout)
2552 p.wait()
2553
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00002554 def test_wait_when_sigchild_ignored(self):
2555 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
2556 sigchild_ignore = support.findfile("sigchild_ignore.py",
2557 subdir="subprocessdata")
2558 p = subprocess.Popen([sys.executable, sigchild_ignore],
2559 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2560 stdout, stderr = p.communicate()
2561 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
Gregory P. Smitha80f4fb2010-12-14 15:23:02 +00002562 " non-zero with this error:\n%s" %
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00002563 stderr.decode('utf-8'))
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00002564
Antoine Pitrou7b98d022011-03-19 17:04:13 +01002565 def test_select_unbuffered(self):
2566 # Issue #11459: bufsize=0 should really set the pipes as
2567 # unbuffered (and therefore let select() work properly).
2568 select = support.import_module("select")
2569 p = subprocess.Popen([sys.executable, "-c",
2570 'import sys;'
2571 'sys.stdout.write("apple")'],
2572 stdout=subprocess.PIPE,
2573 bufsize=0)
2574 f = p.stdout
Ross Lagerwall17ace7a2011-03-26 21:21:46 +02002575 self.addCleanup(f.close)
Antoine Pitrou7b98d022011-03-19 17:04:13 +01002576 try:
2577 self.assertEqual(f.read(4), b"appl")
2578 self.assertIn(f, select.select([f], [], [], 0.0)[0])
2579 finally:
2580 p.wait()
2581
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002582 def test_zombie_fast_process_del(self):
2583 # Issue #12650: on Unix, if Popen.__del__() was called before the
2584 # process exited, it wouldn't be added to subprocess._active, and would
2585 # remain a zombie.
2586 # spawn a Popen, and delete its reference before it exits
2587 p = subprocess.Popen([sys.executable, "-c",
2588 'import sys, time;'
2589 'time.sleep(0.2)'],
2590 stdout=subprocess.PIPE,
2591 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02002592 self.addCleanup(p.stdout.close)
2593 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002594 ident = id(p)
2595 pid = p.pid
Victor Stinner5a48e212016-05-20 12:11:15 +02002596 with support.check_warnings(('', ResourceWarning)):
2597 p = None
2598
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002599 # check that p is in the active processes list
2600 self.assertIn(ident, [id(o) for o in subprocess._active])
2601
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002602 def test_leak_fast_process_del_killed(self):
2603 # Issue #12650: on Unix, if Popen.__del__() was called before the
2604 # process exited, and the process got killed by a signal, it would never
2605 # be removed from subprocess._active, which triggered a FD and memory
2606 # leak.
2607 # spawn a Popen, delete its reference and kill it
2608 p = subprocess.Popen([sys.executable, "-c",
2609 'import time;'
2610 'time.sleep(3)'],
2611 stdout=subprocess.PIPE,
2612 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02002613 self.addCleanup(p.stdout.close)
2614 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002615 ident = id(p)
2616 pid = p.pid
Victor Stinner5a48e212016-05-20 12:11:15 +02002617 with support.check_warnings(('', ResourceWarning)):
2618 p = None
2619
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002620 os.kill(pid, signal.SIGKILL)
2621 # check that p is in the active processes list
2622 self.assertIn(ident, [id(o) for o in subprocess._active])
2623
2624 # let some time for the process to exit, and create a new Popen: this
2625 # should trigger the wait() of p
2626 time.sleep(0.2)
Victor Stinnerb31206a2018-01-25 19:06:05 +01002627 with self.assertRaises(OSError):
Victor Stinner9a83f652017-08-21 23:51:31 +02002628 with subprocess.Popen(NONEXISTING_CMD,
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002629 stdout=subprocess.PIPE,
2630 stderr=subprocess.PIPE) as proc:
2631 pass
2632 # p should have been wait()ed on, and removed from the _active list
2633 self.assertRaises(OSError, os.waitpid, pid, 0)
2634 self.assertNotIn(ident, [id(o) for o in subprocess._active])
2635
Charles-François Natali249cdc32013-08-25 18:24:45 +02002636 def test_close_fds_after_preexec(self):
2637 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2638
2639 # this FD is used as dup2() target by preexec_fn, and should be closed
2640 # in the child process
2641 fd = os.dup(1)
2642 self.addCleanup(os.close, fd)
2643
2644 p = subprocess.Popen([sys.executable, fd_status],
2645 stdout=subprocess.PIPE, close_fds=True,
2646 preexec_fn=lambda: os.dup2(1, fd))
2647 output, ignored = p.communicate()
2648
2649 remaining_fds = set(map(int, output.split(b',')))
2650
2651 self.assertNotIn(fd, remaining_fds)
2652
Victor Stinner8f437aa2014-10-05 17:25:19 +02002653 @support.cpython_only
2654 def test_fork_exec(self):
2655 # Issue #22290: fork_exec() must not crash on memory allocation failure
2656 # or other errors
2657 import _posixsubprocess
2658 gc_enabled = gc.isenabled()
2659 try:
2660 # Use a preexec function and enable the garbage collector
2661 # to force fork_exec() to re-enable the garbage collector
2662 # on error.
2663 func = lambda: None
2664 gc.enable()
2665
Victor Stinner8f437aa2014-10-05 17:25:19 +02002666 for args, exe_list, cwd, env_list in (
2667 (123, [b"exe"], None, [b"env"]),
2668 ([b"arg"], 123, None, [b"env"]),
2669 ([b"arg"], [b"exe"], 123, [b"env"]),
2670 ([b"arg"], [b"exe"], None, 123),
2671 ):
2672 with self.assertRaises(TypeError):
2673 _posixsubprocess.fork_exec(
2674 args, exe_list,
Serhiy Storchaka66bffd12017-04-19 21:12:46 +03002675 True, (), cwd, env_list,
Victor Stinner8f437aa2014-10-05 17:25:19 +02002676 -1, -1, -1, -1,
2677 1, 2, 3, 4,
2678 True, True, func)
2679 finally:
2680 if not gc_enabled:
2681 gc.disable()
2682
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002683 @support.cpython_only
2684 def test_fork_exec_sorted_fd_sanity_check(self):
2685 # Issue #23564: sanity check the fork_exec() fds_to_keep sanity check.
2686 import _posixsubprocess
Serhiy Storchaka66bffd12017-04-19 21:12:46 +03002687 class BadInt:
2688 first = True
2689 def __init__(self, value):
2690 self.value = value
2691 def __int__(self):
2692 if self.first:
2693 self.first = False
2694 return self.value
2695 raise ValueError
2696
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002697 gc_enabled = gc.isenabled()
2698 try:
2699 gc.enable()
2700
2701 for fds_to_keep in (
2702 (-1, 2, 3, 4, 5), # Negative number.
2703 ('str', 4), # Not an int.
2704 (18, 23, 42, 2**63), # Out of range.
2705 (5, 4), # Not sorted.
2706 (6, 7, 7, 8), # Duplicate.
Serhiy Storchaka66bffd12017-04-19 21:12:46 +03002707 (BadInt(1), BadInt(2)),
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002708 ):
2709 with self.assertRaises(
2710 ValueError,
2711 msg='fds_to_keep={}'.format(fds_to_keep)) as c:
2712 _posixsubprocess.fork_exec(
2713 [b"false"], [b"false"],
2714 True, fds_to_keep, None, [b"env"],
2715 -1, -1, -1, -1,
2716 1, 2, 3, 4,
2717 True, True, None)
2718 self.assertIn('fds_to_keep', str(c.exception))
2719 finally:
2720 if not gc_enabled:
2721 gc.disable()
Victor Stinner8f437aa2014-10-05 17:25:19 +02002722
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)2daf8e72016-06-05 02:57:47 +00002723 def test_communicate_BrokenPipeError_stdin_close(self):
2724 # By not setting stdout or stderr or a timeout we force the fast path
2725 # that just calls _stdin_write() internally due to our mock.
2726 proc = subprocess.Popen([sys.executable, '-c', 'pass'])
2727 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
2728 mock_proc_stdin.close.side_effect = BrokenPipeError
2729 proc.communicate() # Should swallow BrokenPipeError from close.
2730 mock_proc_stdin.close.assert_called_with()
2731
2732 def test_communicate_BrokenPipeError_stdin_write(self):
2733 # By not setting stdout or stderr or a timeout we force the fast path
2734 # that just calls _stdin_write() internally due to our mock.
2735 proc = subprocess.Popen([sys.executable, '-c', 'pass'])
2736 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
2737 mock_proc_stdin.write.side_effect = BrokenPipeError
2738 proc.communicate(b'stuff') # Should swallow the BrokenPipeError.
2739 mock_proc_stdin.write.assert_called_once_with(b'stuff')
2740 mock_proc_stdin.close.assert_called_once_with()
2741
2742 def test_communicate_BrokenPipeError_stdin_flush(self):
2743 # Setting stdin and stdout forces the ._communicate() code path.
2744 # python -h exits faster than python -c pass (but spams stdout).
2745 proc = subprocess.Popen([sys.executable, '-h'],
2746 stdin=subprocess.PIPE,
2747 stdout=subprocess.PIPE)
2748 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin, \
2749 open(os.devnull, 'wb') as dev_null:
2750 mock_proc_stdin.flush.side_effect = BrokenPipeError
2751 # because _communicate registers a selector using proc.stdin...
2752 mock_proc_stdin.fileno.return_value = dev_null.fileno()
2753 # _communicate() should swallow BrokenPipeError from flush.
2754 proc.communicate(b'stuff')
2755 mock_proc_stdin.flush.assert_called_once_with()
2756
2757 def test_communicate_BrokenPipeError_stdin_close_with_timeout(self):
2758 # Setting stdin and stdout forces the ._communicate() code path.
2759 # python -h exits faster than python -c pass (but spams stdout).
2760 proc = subprocess.Popen([sys.executable, '-h'],
2761 stdin=subprocess.PIPE,
2762 stdout=subprocess.PIPE)
2763 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
2764 mock_proc_stdin.close.side_effect = BrokenPipeError
2765 # _communicate() should swallow BrokenPipeError from close.
2766 proc.communicate(timeout=999)
2767 mock_proc_stdin.close.assert_called_once_with()
2768
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002769 @unittest.skipUnless(_testcapi is not None
2770 and hasattr(_testcapi, 'W_STOPCODE'),
2771 'need _testcapi.W_STOPCODE')
2772 def test_stopped(self):
Gregory P. Smith50e16e32017-01-22 17:28:38 -08002773 """Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002774 args = [sys.executable, '-c', 'pass']
2775 proc = subprocess.Popen(args)
Victor Stinnercdee3f12017-06-26 17:23:03 +02002776
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002777 # Wait until the real process completes to avoid zombie process
2778 pid = proc.pid
2779 pid, status = os.waitpid(pid, 0)
2780 self.assertEqual(status, 0)
Victor Stinnercdee3f12017-06-26 17:23:03 +02002781
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002782 status = _testcapi.W_STOPCODE(3)
2783 with mock.patch('subprocess.os.waitpid', return_value=(pid, status)):
2784 returncode = proc.wait()
Victor Stinnercdee3f12017-06-26 17:23:03 +02002785
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002786 self.assertEqual(returncode, -3)
Gregory P. Smith50e16e32017-01-22 17:28:38 -08002787
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002788
Victor Stinner8fbbdf02018-06-22 19:25:44 +02002789@unittest.skipUnless(support.MS_WINDOWS, "Windows specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +00002790class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00002791
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002792 def test_startupinfo(self):
2793 # startupinfo argument
2794 # We uses hardcoded constants, because we do not want to
2795 # depend on win32all.
2796 STARTF_USESHOWWINDOW = 1
2797 SW_MAXIMIZE = 3
2798 startupinfo = subprocess.STARTUPINFO()
2799 startupinfo.dwFlags = STARTF_USESHOWWINDOW
2800 startupinfo.wShowWindow = SW_MAXIMIZE
2801 # Since Python is a console process, it won't be affected
2802 # by wShowWindow, but the argument should be silently
2803 # ignored
2804 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002805 startupinfo=startupinfo)
2806
Subhendu Ghoshae160bb2017-02-25 20:29:05 +05302807 def test_startupinfo_keywords(self):
2808 # startupinfo argument
2809 # We use hardcoded constants, because we do not want to
2810 # depend on win32all.
2811 STARTF_USERSHOWWINDOW = 1
2812 SW_MAXIMIZE = 3
2813 startupinfo = subprocess.STARTUPINFO(
2814 dwFlags=STARTF_USERSHOWWINDOW,
2815 wShowWindow=SW_MAXIMIZE
2816 )
2817 # Since Python is a console process, it won't be affected
2818 # by wShowWindow, but the argument should be silently
2819 # ignored
2820 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
2821 startupinfo=startupinfo)
2822
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002823 def test_creationflags(self):
2824 # creationflags argument
2825 CREATE_NEW_CONSOLE = 16
2826 sys.stderr.write(" a DOS box should flash briefly ...\n")
2827 subprocess.call(sys.executable +
2828 ' -c "import time; time.sleep(0.25)"',
2829 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002830
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002831 def test_invalid_args(self):
2832 # invalid arguments should raise ValueError
2833 self.assertRaises(ValueError, subprocess.call,
2834 [sys.executable, "-c",
2835 "import sys; sys.exit(47)"],
2836 preexec_fn=lambda: 1)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002837
Oren Milman0b3a87e2017-09-14 22:30:28 +03002838 @support.cpython_only
2839 def test_issue31471(self):
2840 # There shouldn't be an assertion failure in Popen() in case the env
2841 # argument has a bad keys() method.
2842 class BadEnv(dict):
2843 keys = None
2844 with self.assertRaises(TypeError):
2845 subprocess.Popen([sys.executable, "-c", "pass"], env=BadEnv())
2846
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002847 def test_close_fds(self):
2848 # close file descriptors
2849 rc = subprocess.call([sys.executable, "-c",
2850 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002851 close_fds=True)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002852 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002853
Segev Finerb2a60832017-12-18 11:28:19 +02002854 def test_close_fds_with_stdio(self):
2855 import msvcrt
2856
2857 fds = os.pipe()
2858 self.addCleanup(os.close, fds[0])
2859 self.addCleanup(os.close, fds[1])
2860
2861 handles = []
2862 for fd in fds:
2863 os.set_inheritable(fd, True)
2864 handles.append(msvcrt.get_osfhandle(fd))
2865
2866 p = subprocess.Popen([sys.executable, "-c",
2867 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2868 stdout=subprocess.PIPE, close_fds=False)
2869 stdout, stderr = p.communicate()
2870 self.assertEqual(p.returncode, 0)
2871 int(stdout.strip()) # Check that stdout is an integer
2872
2873 p = subprocess.Popen([sys.executable, "-c",
2874 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2875 stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
2876 stdout, stderr = p.communicate()
2877 self.assertEqual(p.returncode, 1)
2878 self.assertIn(b"OSError", stderr)
2879
2880 # The same as the previous call, but with an empty handle_list
2881 handle_list = []
2882 startupinfo = subprocess.STARTUPINFO()
2883 startupinfo.lpAttributeList = {"handle_list": handle_list}
2884 p = subprocess.Popen([sys.executable, "-c",
2885 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2886 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
2887 startupinfo=startupinfo, close_fds=True)
2888 stdout, stderr = p.communicate()
2889 self.assertEqual(p.returncode, 1)
2890 self.assertIn(b"OSError", stderr)
2891
2892 # Check for a warning due to using handle_list and close_fds=False
2893 with support.check_warnings((".*overriding close_fds", RuntimeWarning)):
2894 startupinfo = subprocess.STARTUPINFO()
2895 startupinfo.lpAttributeList = {"handle_list": handles[:]}
2896 p = subprocess.Popen([sys.executable, "-c",
2897 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2898 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
2899 startupinfo=startupinfo, close_fds=False)
2900 stdout, stderr = p.communicate()
2901 self.assertEqual(p.returncode, 0)
2902
2903 def test_empty_attribute_list(self):
2904 startupinfo = subprocess.STARTUPINFO()
2905 startupinfo.lpAttributeList = {}
2906 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
2907 startupinfo=startupinfo)
2908
2909 def test_empty_handle_list(self):
2910 startupinfo = subprocess.STARTUPINFO()
2911 startupinfo.lpAttributeList = {"handle_list": []}
2912 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
2913 startupinfo=startupinfo)
2914
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002915 def test_shell_sequence(self):
2916 # Run command through the shell (sequence)
2917 newenv = os.environ.copy()
2918 newenv["FRUIT"] = "physalis"
2919 p = subprocess.Popen(["set"], shell=1,
2920 stdout=subprocess.PIPE,
2921 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02002922 with p:
2923 self.assertIn(b"physalis", p.stdout.read())
Guido van Rossume7ba4952007-06-06 23:52:48 +00002924
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002925 def test_shell_string(self):
2926 # Run command through the shell (string)
2927 newenv = os.environ.copy()
2928 newenv["FRUIT"] = "physalis"
2929 p = subprocess.Popen("set", shell=1,
2930 stdout=subprocess.PIPE,
2931 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02002932 with p:
2933 self.assertIn(b"physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002934
Steve Dower050acae2016-09-06 20:16:17 -07002935 def test_shell_encodings(self):
2936 # Run command through the shell (string)
2937 for enc in ['ansi', 'oem']:
2938 newenv = os.environ.copy()
2939 newenv["FRUIT"] = "physalis"
2940 p = subprocess.Popen("set", shell=1,
2941 stdout=subprocess.PIPE,
2942 env=newenv,
2943 encoding=enc)
2944 with p:
2945 self.assertIn("physalis", p.stdout.read(), enc)
2946
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002947 def test_call_string(self):
2948 # call() function with string argument on Windows
2949 rc = subprocess.call(sys.executable +
2950 ' -c "import sys; sys.exit(47)"')
2951 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002952
Florent Xicluna4886d242010-03-08 13:27:26 +00002953 def _kill_process(self, method, *args):
2954 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroua4024e22010-09-24 18:57:01 +00002955 p = subprocess.Popen([sys.executable, "-c", """if 1:
2956 import sys, time
2957 sys.stdout.write('x\\n')
2958 sys.stdout.flush()
2959 time.sleep(30)
2960 """],
2961 stdin=subprocess.PIPE,
2962 stdout=subprocess.PIPE,
2963 stderr=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +02002964 with p:
2965 # Wait for the interpreter to be completely initialized before
2966 # sending any signal.
2967 p.stdout.read(1)
2968 getattr(p, method)(*args)
2969 _, stderr = p.communicate()
2970 self.assertStderrEqual(stderr, b'')
2971 returncode = p.wait()
Florent Xicluna4886d242010-03-08 13:27:26 +00002972 self.assertNotEqual(returncode, 0)
2973
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01002974 def _kill_dead_process(self, method, *args):
2975 p = subprocess.Popen([sys.executable, "-c", """if 1:
2976 import sys, time
2977 sys.stdout.write('x\\n')
2978 sys.stdout.flush()
2979 sys.exit(42)
2980 """],
2981 stdin=subprocess.PIPE,
2982 stdout=subprocess.PIPE,
2983 stderr=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +02002984 with p:
2985 # Wait for the interpreter to be completely initialized before
2986 # sending any signal.
2987 p.stdout.read(1)
2988 # The process should end after this
2989 time.sleep(1)
2990 # This shouldn't raise even though the child is now dead
2991 getattr(p, method)(*args)
2992 _, stderr = p.communicate()
2993 self.assertStderrEqual(stderr, b'')
2994 rc = p.wait()
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01002995 self.assertEqual(rc, 42)
2996
Florent Xicluna4886d242010-03-08 13:27:26 +00002997 def test_send_signal(self):
2998 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimesa342c012008-04-20 21:01:16 +00002999
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003000 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00003001 self._kill_process('kill')
Christian Heimesa342c012008-04-20 21:01:16 +00003002
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003003 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00003004 self._kill_process('terminate')
Christian Heimesa342c012008-04-20 21:01:16 +00003005
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01003006 def test_send_signal_dead(self):
3007 self._kill_dead_process('send_signal', signal.SIGTERM)
3008
3009 def test_kill_dead(self):
3010 self._kill_dead_process('kill')
3011
3012 def test_terminate_dead(self):
3013 self._kill_dead_process('terminate')
3014
Martin Panter23172bd2016-04-16 11:28:10 +00003015class MiscTests(unittest.TestCase):
Gregory P. Smithf4d644f2018-01-29 21:27:39 -08003016
3017 class RecordingPopen(subprocess.Popen):
3018 """A Popen that saves a reference to each instance for testing."""
3019 instances_created = []
3020
3021 def __init__(self, *args, **kwargs):
3022 super().__init__(*args, **kwargs)
3023 self.instances_created.append(self)
3024
3025 @mock.patch.object(subprocess.Popen, "_communicate")
3026 def _test_keyboardinterrupt_no_kill(self, popener, mock__communicate,
3027 **kwargs):
3028 """Fake a SIGINT happening during Popen._communicate() and ._wait().
3029
3030 This avoids the need to actually try and get test environments to send
3031 and receive signals reliably across platforms. The net effect of a ^C
3032 happening during a blocking subprocess execution which we want to clean
3033 up from is a KeyboardInterrupt coming out of communicate() or wait().
3034 """
3035
3036 mock__communicate.side_effect = KeyboardInterrupt
3037 try:
3038 with mock.patch.object(subprocess.Popen, "_wait") as mock__wait:
3039 # We patch out _wait() as no signal was involved so the
3040 # child process isn't actually going to exit rapidly.
3041 mock__wait.side_effect = KeyboardInterrupt
3042 with mock.patch.object(subprocess, "Popen",
3043 self.RecordingPopen):
3044 with self.assertRaises(KeyboardInterrupt):
3045 popener([sys.executable, "-c",
3046 "import time\ntime.sleep(9)\nimport sys\n"
3047 "sys.stderr.write('\\n!runaway child!\\n')"],
3048 stdout=subprocess.DEVNULL, **kwargs)
3049 for call in mock__wait.call_args_list[1:]:
3050 self.assertNotEqual(
3051 call, mock.call(timeout=None),
3052 "no open-ended wait() after the first allowed: "
3053 f"{mock__wait.call_args_list}")
3054 sigint_calls = []
3055 for call in mock__wait.call_args_list:
3056 if call == mock.call(timeout=0.25): # from Popen.__init__
3057 sigint_calls.append(call)
3058 self.assertLessEqual(mock__wait.call_count, 2,
3059 msg=mock__wait.call_args_list)
3060 self.assertEqual(len(sigint_calls), 1,
3061 msg=mock__wait.call_args_list)
3062 finally:
3063 # cleanup the forgotten (due to our mocks) child process
3064 process = self.RecordingPopen.instances_created.pop()
3065 process.kill()
3066 process.wait()
3067 self.assertEqual([], self.RecordingPopen.instances_created)
3068
3069 def test_call_keyboardinterrupt_no_kill(self):
3070 self._test_keyboardinterrupt_no_kill(subprocess.call, timeout=6.282)
3071
3072 def test_run_keyboardinterrupt_no_kill(self):
3073 self._test_keyboardinterrupt_no_kill(subprocess.run, timeout=6.282)
3074
3075 def test_context_manager_keyboardinterrupt_no_kill(self):
3076 def popen_via_context_manager(*args, **kwargs):
3077 with subprocess.Popen(*args, **kwargs) as unused_process:
3078 raise KeyboardInterrupt # Test how __exit__ handles ^C.
3079 self._test_keyboardinterrupt_no_kill(popen_via_context_manager)
3080
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003081 def test_getoutput(self):
3082 self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
3083 self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
3084 (0, 'xyzzy'))
Brett Cannona23810f2008-05-26 19:04:21 +00003085
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003086 # we use mkdtemp in the next line to create an empty directory
3087 # under our exclusive control; from that, we can invent a pathname
3088 # that we _know_ won't exist. This is guaranteed to fail.
3089 dir = None
3090 try:
3091 dir = tempfile.mkdtemp()
3092 name = os.path.join(dir, "foo")
Tim Goldene0041752013-11-03 12:53:17 +00003093 status, output = subprocess.getstatusoutput(
Victor Stinner8fbbdf02018-06-22 19:25:44 +02003094 ("type " if support.MS_WINDOWS else "cat ") + name)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003095 self.assertNotEqual(status, 0)
3096 finally:
3097 if dir is not None:
3098 os.rmdir(dir)
Brett Cannona23810f2008-05-26 19:04:21 +00003099
Gregory P. Smithace55862015-04-07 15:57:54 -07003100 def test__all__(self):
3101 """Ensure that __all__ is populated properly."""
Martin Panter528619b2016-04-16 23:42:37 +00003102 intentionally_excluded = {"list2cmdline", "Handle"}
Gregory P. Smithace55862015-04-07 15:57:54 -07003103 exported = set(subprocess.__all__)
3104 possible_exports = set()
3105 import types
3106 for name, value in subprocess.__dict__.items():
3107 if name.startswith('_'):
3108 continue
3109 if isinstance(value, (types.ModuleType,)):
3110 continue
3111 possible_exports.add(name)
3112 self.assertEqual(exported, possible_exports - intentionally_excluded)
3113
3114
Martin Panter23172bd2016-04-16 11:28:10 +00003115@unittest.skipUnless(hasattr(selectors, 'PollSelector'),
3116 "Test needs selectors.PollSelector")
3117class ProcessTestCaseNoPoll(ProcessTestCase):
3118 def setUp(self):
3119 self.orig_selector = subprocess._PopenSelector
3120 subprocess._PopenSelector = selectors.SelectSelector
3121 ProcessTestCase.setUp(self)
3122
3123 def tearDown(self):
3124 subprocess._PopenSelector = self.orig_selector
3125 ProcessTestCase.tearDown(self)
3126
Gregory P. Smithd06fa472009-07-04 02:46:54 +00003127
Victor Stinner8fbbdf02018-06-22 19:25:44 +02003128@unittest.skipUnless(support.MS_WINDOWS, "Windows-specific tests")
Tim Golden126c2962010-08-11 14:20:40 +00003129class CommandsWithSpaces (BaseTestCase):
3130
3131 def setUp(self):
3132 super().setUp()
Berker Peksag16a1f282015-09-28 13:33:14 +03003133 f, fname = tempfile.mkstemp(".py", "te st")
Tim Golden126c2962010-08-11 14:20:40 +00003134 self.fname = fname.lower ()
3135 os.write(f, b"import sys;"
3136 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
3137 )
3138 os.close(f)
3139
3140 def tearDown(self):
3141 os.remove(self.fname)
3142 super().tearDown()
3143
3144 def with_spaces(self, *args, **kwargs):
3145 kwargs['stdout'] = subprocess.PIPE
3146 p = subprocess.Popen(*args, **kwargs)
Victor Stinner7438c612016-05-20 12:43:15 +02003147 with p:
3148 self.assertEqual(
3149 p.stdout.read ().decode("mbcs"),
3150 "2 [%r, 'ab cd']" % self.fname
3151 )
Tim Golden126c2962010-08-11 14:20:40 +00003152
3153 def test_shell_string_with_spaces(self):
3154 # call() function with string argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00003155 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
3156 "ab cd"), shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00003157
3158 def test_shell_sequence_with_spaces(self):
3159 # call() function with sequence argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00003160 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00003161
3162 def test_noshell_string_with_spaces(self):
3163 # call() function with string argument with spaces on Windows
3164 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
3165 "ab cd"))
3166
3167 def test_noshell_sequence_with_spaces(self):
3168 # call() function with sequence argument with spaces on Windows
3169 self.with_spaces([sys.executable, self.fname, "ab cd"])
3170
Brian Curtin79cdb662010-12-03 02:46:02 +00003171
Georg Brandla86b2622012-02-20 21:34:57 +01003172class ContextManagerTests(BaseTestCase):
Brian Curtin79cdb662010-12-03 02:46:02 +00003173
3174 def test_pipe(self):
3175 with subprocess.Popen([sys.executable, "-c",
3176 "import sys;"
3177 "sys.stdout.write('stdout');"
3178 "sys.stderr.write('stderr');"],
3179 stdout=subprocess.PIPE,
3180 stderr=subprocess.PIPE) as proc:
3181 self.assertEqual(proc.stdout.read(), b"stdout")
3182 self.assertStderrEqual(proc.stderr.read(), b"stderr")
3183
3184 self.assertTrue(proc.stdout.closed)
3185 self.assertTrue(proc.stderr.closed)
3186
3187 def test_returncode(self):
3188 with subprocess.Popen([sys.executable, "-c",
3189 "import sys; sys.exit(100)"]) as proc:
Gregory P. Smith6b657452011-05-11 21:42:08 -07003190 pass
3191 # __exit__ calls wait(), so the returncode should be set
Brian Curtin79cdb662010-12-03 02:46:02 +00003192 self.assertEqual(proc.returncode, 100)
3193
3194 def test_communicate_stdin(self):
3195 with subprocess.Popen([sys.executable, "-c",
3196 "import sys;"
3197 "sys.exit(sys.stdin.read() == 'context')"],
3198 stdin=subprocess.PIPE) as proc:
3199 proc.communicate(b"context")
3200 self.assertEqual(proc.returncode, 1)
3201
3202 def test_invalid_args(self):
Victor Stinnerb31206a2018-01-25 19:06:05 +01003203 with self.assertRaises(NONEXISTING_ERRORS):
Victor Stinner9a83f652017-08-21 23:51:31 +02003204 with subprocess.Popen(NONEXISTING_CMD,
Brian Curtin79cdb662010-12-03 02:46:02 +00003205 stdout=subprocess.PIPE,
3206 stderr=subprocess.PIPE) as proc:
3207 pass
3208
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003209 def test_broken_pipe_cleanup(self):
3210 """Broken pipe error should not prevent wait() (Issue 21619)"""
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003211 proc = subprocess.Popen([sys.executable, '-c', 'pass'],
Victor Stinner20f4bd42015-03-05 02:38:41 +01003212 stdin=subprocess.PIPE,
Victor Stinner20f4bd42015-03-05 02:38:41 +01003213 bufsize=support.PIPE_MAX_SIZE*2)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003214 proc = proc.__enter__()
3215 # Prepare to send enough data to overflow any OS pipe buffering and
3216 # guarantee a broken pipe error. Data is held in BufferedWriter
3217 # buffer until closed.
3218 proc.stdin.write(b'x' * support.PIPE_MAX_SIZE)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003219 self.assertIsNone(proc.returncode)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003220 # EPIPE expected under POSIX; EINVAL under Windows
Serhiy Storchakacf265fd2015-02-28 13:27:54 +02003221 self.assertRaises(OSError, proc.__exit__, None, None, None)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003222 self.assertEqual(proc.returncode, 0)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003223 self.assertTrue(proc.stdin.closed)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003224
Brian Curtin79cdb662010-12-03 02:46:02 +00003225
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00003226if __name__ == "__main__":
Gregory P. Smith112bb3a2011-03-15 14:55:17 -04003227 unittest.main()