blob: 2a766d7c92ad822bd612de21f905be4991df0411 [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
Miss Islington (bot)05455632018-03-26 13:14:09 -07009import 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
Miss Islington (bot)a13b6542018-03-02 02:17:51 -080021from 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 +000038mswindows = (sys.platform == "win32")
39
40#
41# Depends on the following external programs: Python
42#
43
44if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000045 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
46 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000047else:
48 SETBINARY = ''
49
Victor Stinner9a83f652017-08-21 23:51:31 +020050NONEXISTING_CMD = ('nonexisting_i_hope',)
Victor Stinnerb31206a2018-01-25 19:06:05 +010051# Ignore errors that indicate the command was not found
52NONEXISTING_ERRORS = (FileNotFoundError, NotADirectoryError, PermissionError)
Victor Stinner9a83f652017-08-21 23:51:31 +020053
Florent Xiclunab1e94e82010-02-27 22:12:37 +000054
Florent Xiclunac049d872010-03-27 22:47:23 +000055class BaseTestCase(unittest.TestCase):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056 def setUp(self):
57 # Try to minimize the number of children we have so this test
58 # doesn't crash on some buildbots (Alphas in particular).
Florent Xiclunab1e94e82010-02-27 22:12:37 +000059 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000060
Florent Xiclunaf0cbd822010-03-04 21:50:56 +000061 def tearDown(self):
62 for inst in subprocess._active:
63 inst.wait()
64 subprocess._cleanup()
65 self.assertFalse(subprocess._active, "subprocess._active not empty")
Victor Stinnercc42c122017-07-28 18:00:22 +020066 self.doCleanups()
67 support.reap_children()
Florent Xiclunaf0cbd822010-03-04 21:50:56 +000068
Florent Xiclunab1e94e82010-02-27 22:12:37 +000069 def assertStderrEqual(self, stderr, expected, msg=None):
70 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
71 # shutdown time. That frustrates tests trying to check stderr produced
72 # from a spawned Python process.
Antoine Pitrou62f68ed2010-08-04 11:48:56 +000073 actual = support.strip_python_stderr(stderr)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -040074 # strip_python_stderr also strips whitespace, so we do too.
75 expected = expected.strip()
Florent Xiclunab1e94e82010-02-27 22:12:37 +000076 self.assertEqual(actual, expected, msg)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077
Florent Xiclunac049d872010-03-27 22:47:23 +000078
Gregory P. Smith3d8e7762012-11-10 22:32:22 -080079class PopenTestException(Exception):
80 pass
81
82
83class PopenExecuteChildRaises(subprocess.Popen):
84 """Popen subclass for testing cleanup of subprocess.PIPE filehandles when
85 _execute_child fails.
86 """
87 def _execute_child(self, *args, **kwargs):
88 raise PopenTestException("Forced Exception for Test")
89
90
Florent Xiclunac049d872010-03-27 22:47:23 +000091class ProcessTestCase(BaseTestCase):
92
Gregory P. Smitha1ed5392013-03-23 11:44:25 -070093 def test_io_buffered_by_default(self):
94 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
95 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
96 stderr=subprocess.PIPE)
97 try:
98 self.assertIsInstance(p.stdin, io.BufferedIOBase)
99 self.assertIsInstance(p.stdout, io.BufferedIOBase)
100 self.assertIsInstance(p.stderr, io.BufferedIOBase)
101 finally:
Gregory P. Smitha1b9ed32013-03-23 11:54:22 -0700102 p.stdin.close()
103 p.stdout.close()
104 p.stderr.close()
Gregory P. Smitha1ed5392013-03-23 11:44:25 -0700105 p.wait()
106
107 def test_io_unbuffered_works(self):
108 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
109 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
110 stderr=subprocess.PIPE, bufsize=0)
111 try:
112 self.assertIsInstance(p.stdin, io.RawIOBase)
113 self.assertIsInstance(p.stdout, io.RawIOBase)
114 self.assertIsInstance(p.stderr, io.RawIOBase)
115 finally:
Gregory P. Smitha1b9ed32013-03-23 11:54:22 -0700116 p.stdin.close()
117 p.stdout.close()
118 p.stderr.close()
Gregory P. Smitha1ed5392013-03-23 11:44:25 -0700119 p.wait()
120
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000121 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000122 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +0000123 rc = subprocess.call([sys.executable, "-c",
124 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000125 self.assertEqual(rc, 47)
126
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400127 def test_call_timeout(self):
128 # call() function with timeout argument; we want to test that the child
129 # process gets killed when the timeout expires. If the child isn't
130 # killed, this call will deadlock since subprocess.call waits for the
131 # child.
132 self.assertRaises(subprocess.TimeoutExpired, subprocess.call,
133 [sys.executable, "-c", "while True: pass"],
134 timeout=0.1)
135
Peter Astrand454f7672005-01-01 09:36:35 +0000136 def test_check_call_zero(self):
137 # check_call() function with zero return code
138 rc = subprocess.check_call([sys.executable, "-c",
139 "import sys; sys.exit(0)"])
140 self.assertEqual(rc, 0)
141
142 def test_check_call_nonzero(self):
143 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000144 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +0000145 subprocess.check_call([sys.executable, "-c",
146 "import sys; sys.exit(47)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000147 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +0000148
Georg Brandlf9734072008-12-07 15:30:06 +0000149 def test_check_output(self):
150 # check_output() function with zero return code
151 output = subprocess.check_output(
152 [sys.executable, "-c", "print('BDFL')"])
Benjamin Peterson577473f2010-01-19 00:09:57 +0000153 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +0000154
155 def test_check_output_nonzero(self):
156 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000157 with self.assertRaises(subprocess.CalledProcessError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +0000158 subprocess.check_output(
159 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000160 self.assertEqual(c.exception.returncode, 5)
Georg Brandlf9734072008-12-07 15:30:06 +0000161
162 def test_check_output_stderr(self):
163 # check_output() function stderr redirected to stdout
164 output = subprocess.check_output(
165 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
166 stderr=subprocess.STDOUT)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000167 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +0000168
Serhiy Storchakafcd9f222013-04-22 20:20:54 +0300169 def test_check_output_stdin_arg(self):
170 # check_output() can be called with stdin set to a file
171 tf = tempfile.TemporaryFile()
172 self.addCleanup(tf.close)
173 tf.write(b'pear')
174 tf.seek(0)
175 output = subprocess.check_output(
176 [sys.executable, "-c",
177 "import sys; sys.stdout.write(sys.stdin.read().upper())"],
178 stdin=tf)
179 self.assertIn(b'PEAR', output)
180
181 def test_check_output_input_arg(self):
182 # check_output() can be called with input set to a string
183 output = subprocess.check_output(
184 [sys.executable, "-c",
185 "import sys; sys.stdout.write(sys.stdin.read().upper())"],
186 input=b'pear')
187 self.assertIn(b'PEAR', output)
188
Georg Brandlf9734072008-12-07 15:30:06 +0000189 def test_check_output_stdout_arg(self):
Serhiy Storchakafcd9f222013-04-22 20:20:54 +0300190 # check_output() refuses to accept 'stdout' argument
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000191 with self.assertRaises(ValueError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +0000192 output = subprocess.check_output(
193 [sys.executable, "-c", "print('will not be run')"],
194 stdout=sys.stdout)
Georg Brandlf9734072008-12-07 15:30:06 +0000195 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000196 self.assertIn('stdout', c.exception.args[0])
Georg Brandlf9734072008-12-07 15:30:06 +0000197
Serhiy Storchakafcd9f222013-04-22 20:20:54 +0300198 def test_check_output_stdin_with_input_arg(self):
199 # check_output() refuses to accept 'stdin' with 'input'
200 tf = tempfile.TemporaryFile()
201 self.addCleanup(tf.close)
202 tf.write(b'pear')
203 tf.seek(0)
204 with self.assertRaises(ValueError) as c:
205 output = subprocess.check_output(
206 [sys.executable, "-c", "print('will not be run')"],
207 stdin=tf, input=b'hare')
208 self.fail("Expected ValueError when stdin and input args supplied.")
209 self.assertIn('stdin', c.exception.args[0])
210 self.assertIn('input', c.exception.args[0])
211
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400212 def test_check_output_timeout(self):
213 # check_output() function with timeout arg
214 with self.assertRaises(subprocess.TimeoutExpired) as c:
215 output = subprocess.check_output(
216 [sys.executable, "-c",
Victor Stinner149b1c72011-06-06 23:43:02 +0200217 "import sys, time\n"
218 "sys.stdout.write('BDFL')\n"
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400219 "sys.stdout.flush()\n"
Victor Stinner149b1c72011-06-06 23:43:02 +0200220 "time.sleep(3600)"],
Reid Klecknerda9ac722011-03-16 17:08:21 -0400221 # Some heavily loaded buildbots (sparc Debian 3.x) require
222 # this much time to start and print.
223 timeout=3)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400224 self.fail("Expected TimeoutExpired.")
225 self.assertEqual(c.exception.output, b'BDFL')
226
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000227 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000228 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000229 newenv = os.environ.copy()
230 newenv["FRUIT"] = "banana"
231 rc = subprocess.call([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000232 'import sys, os;'
233 'sys.exit(os.getenv("FRUIT")=="banana")'],
234 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000235 self.assertEqual(rc, 1)
236
Victor Stinner87b9bc32011-06-01 00:57:47 +0200237 def test_invalid_args(self):
238 # Popen() called with invalid arguments should raise TypeError
239 # but Popen.__del__ should not complain (issue #12085)
240 with support.captured_stderr() as s:
241 self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
242 argcount = subprocess.Popen.__init__.__code__.co_argcount
243 too_many_args = [0] * (argcount + 1)
244 self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
245 self.assertEqual(s.getvalue(), '')
246
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000247 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000248 # .stdin is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000249 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000250 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000251 self.addCleanup(p.stdout.close)
252 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000253 p.wait()
254 self.assertEqual(p.stdin, None)
255
256 def test_stdout_none(self):
Ezio Melotti42a541b2013-03-11 05:53:34 +0200257 # .stdout is None when not redirected, and the child's stdout will
258 # be inherited from the parent. In order to test this we run a
259 # subprocess in a subprocess:
260 # this_test
261 # \-- subprocess created by this test (parent)
262 # \-- subprocess created by the parent subprocess (child)
263 # The parent doesn't specify stdout, so the child will use the
264 # parent's stdout. This test checks that the message printed by the
265 # child goes to the parent stdout. The parent also checks that the
266 # child's stdout is None. See #11963.
267 code = ('import sys; from subprocess import Popen, PIPE;'
268 'p = Popen([sys.executable, "-c", "print(\'test_stdout_none\')"],'
269 ' stdin=PIPE, stderr=PIPE);'
270 'p.wait(); assert p.stdout is None;')
271 p = subprocess.Popen([sys.executable, "-c", code],
272 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
273 self.addCleanup(p.stdout.close)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000274 self.addCleanup(p.stderr.close)
Ezio Melotti42a541b2013-03-11 05:53:34 +0200275 out, err = p.communicate()
276 self.assertEqual(p.returncode, 0, err)
277 self.assertEqual(out.rstrip(), b'test_stdout_none')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000278
279 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000280 # .stderr is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000281 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000282 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000283 self.addCleanup(p.stdout.close)
284 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000285 p.wait()
286 self.assertEqual(p.stderr, None)
287
Chris Jerdonek776cb192012-10-08 15:56:43 -0700288 def _assert_python(self, pre_args, **kwargs):
289 # We include sys.exit() to prevent the test runner from hanging
290 # whenever python is found.
291 args = pre_args + ["import sys; sys.exit(47)"]
292 p = subprocess.Popen(args, **kwargs)
293 p.wait()
294 self.assertEqual(47, p.returncode)
295
296 def test_executable(self):
297 # Check that the executable argument works.
Chris Jerdonek86b0fb22012-10-09 13:17:49 -0700298 #
299 # On Unix (non-Mac and non-Windows), Python looks at args[0] to
300 # determine where its standard library is, so we need the directory
301 # of args[0] to be valid for the Popen() call to Python to succeed.
302 # See also issue #16170 and issue #7774.
303 doesnotexist = os.path.join(os.path.dirname(sys.executable),
304 "doesnotexist")
305 self._assert_python([doesnotexist, "-c"], executable=sys.executable)
Chris Jerdonek776cb192012-10-08 15:56:43 -0700306
307 def test_executable_takes_precedence(self):
308 # Check that the executable argument takes precedence over args[0].
309 #
310 # Verify first that the call succeeds without the executable arg.
311 pre_args = [sys.executable, "-c"]
312 self._assert_python(pre_args)
Victor Stinnerb31206a2018-01-25 19:06:05 +0100313 self.assertRaises(NONEXISTING_ERRORS,
Xavier de Gaye38c8b7d2016-11-14 17:14:42 +0100314 self._assert_python, pre_args,
Victor Stinnerb31206a2018-01-25 19:06:05 +0100315 executable=NONEXISTING_CMD[0])
Chris Jerdonek776cb192012-10-08 15:56:43 -0700316
317 @unittest.skipIf(mswindows, "executable argument replaces shell")
318 def test_executable_replaces_shell(self):
319 # Check that the executable argument replaces the default shell
320 # when shell=True.
321 self._assert_python([], executable=sys.executable, shell=True)
322
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700323 # For use in the test_cwd* tests below.
324 def _normalize_cwd(self, cwd):
325 # Normalize an expected cwd (for Tru64 support).
326 # We can't use os.path.realpath since it doesn't expand Tru64 {memb}
327 # strings. See bug #1063571.
Serhiy Storchaka2a23adf2015-09-06 14:13:25 +0300328 with support.change_cwd(cwd):
329 return os.getcwd()
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700330
331 # For use in the test_cwd* tests below.
332 def _split_python_path(self):
333 # Return normalized (python_dir, python_base).
334 python_path = os.path.realpath(sys.executable)
335 return os.path.split(python_path)
336
337 # For use in the test_cwd* tests below.
338 def _assert_cwd(self, expected_cwd, python_arg, **kwargs):
339 # Invoke Python via Popen, and assert that (1) the call succeeds,
340 # and that (2) the current working directory of the child process
341 # matches *expected_cwd*.
342 p = subprocess.Popen([python_arg, "-c",
343 "import os, sys; "
344 "sys.stdout.write(os.getcwd()); "
345 "sys.exit(47)"],
346 stdout=subprocess.PIPE,
347 **kwargs)
348 self.addCleanup(p.stdout.close)
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000349 p.wait()
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700350 self.assertEqual(47, p.returncode)
351 normcase = os.path.normcase
352 self.assertEqual(normcase(expected_cwd),
353 normcase(p.stdout.read().decode("utf-8")))
354
355 def test_cwd(self):
356 # Check that cwd changes the cwd for the child process.
357 temp_dir = tempfile.gettempdir()
358 temp_dir = self._normalize_cwd(temp_dir)
359 self._assert_cwd(temp_dir, sys.executable, cwd=temp_dir)
360
Sayan Chowdhuryd5c11f72017-02-26 22:36:10 +0530361 def test_cwd_with_pathlike(self):
362 temp_dir = tempfile.gettempdir()
363 temp_dir = self._normalize_cwd(temp_dir)
Miss Islington (bot)a13b6542018-03-02 02:17:51 -0800364 self._assert_cwd(temp_dir, sys.executable, cwd=FakePath(temp_dir))
Sayan Chowdhuryd5c11f72017-02-26 22:36:10 +0530365
Chris Jerdonekc2cd6262012-09-30 09:45:00 -0700366 @unittest.skipIf(mswindows, "pending resolution of issue #15533")
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700367 def test_cwd_with_relative_arg(self):
368 # Check that Popen looks for args[0] relative to cwd if args[0]
369 # is relative.
370 python_dir, python_base = self._split_python_path()
371 rel_python = os.path.join(os.curdir, python_base)
372 with support.temp_cwd() as wrong_dir:
373 # Before calling with the correct cwd, confirm that the call fails
374 # without cwd and with the wrong cwd.
Chris Jerdonek28714c82012-09-30 02:15:37 -0700375 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700376 [rel_python])
Chris Jerdonek28714c82012-09-30 02:15:37 -0700377 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700378 [rel_python], cwd=wrong_dir)
379 python_dir = self._normalize_cwd(python_dir)
380 self._assert_cwd(python_dir, rel_python, cwd=python_dir)
381
Chris Jerdonekc2cd6262012-09-30 09:45:00 -0700382 @unittest.skipIf(mswindows, "pending resolution of issue #15533")
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700383 def test_cwd_with_relative_executable(self):
384 # Check that Popen looks for executable relative to cwd if executable
385 # is relative (and that executable takes precedence over args[0]).
386 python_dir, python_base = self._split_python_path()
387 rel_python = os.path.join(os.curdir, python_base)
388 doesntexist = "somethingyoudonthave"
389 with support.temp_cwd() as wrong_dir:
390 # Before calling with the correct cwd, confirm that the call fails
391 # without cwd and with the wrong cwd.
Chris Jerdonek28714c82012-09-30 02:15:37 -0700392 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700393 [doesntexist], executable=rel_python)
Chris Jerdonek28714c82012-09-30 02:15:37 -0700394 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700395 [doesntexist], executable=rel_python,
396 cwd=wrong_dir)
397 python_dir = self._normalize_cwd(python_dir)
398 self._assert_cwd(python_dir, doesntexist, executable=rel_python,
399 cwd=python_dir)
400
401 def test_cwd_with_absolute_arg(self):
402 # Check that Popen can find the executable when the cwd is wrong
403 # if args[0] is an absolute path.
404 python_dir, python_base = self._split_python_path()
405 abs_python = os.path.join(python_dir, python_base)
406 rel_python = os.path.join(os.curdir, python_base)
Berker Peksagce643912015-05-06 06:33:17 +0300407 with support.temp_dir() as wrong_dir:
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700408 # Before calling with an absolute path, confirm that using a
409 # relative path fails.
Chris Jerdonek28714c82012-09-30 02:15:37 -0700410 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700411 [rel_python], cwd=wrong_dir)
412 wrong_dir = self._normalize_cwd(wrong_dir)
413 self._assert_cwd(wrong_dir, abs_python, cwd=wrong_dir)
414
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100415 @unittest.skipIf(sys.base_prefix != sys.prefix,
416 'Test is not venv-compatible')
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000417 def test_executable_with_cwd(self):
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700418 python_dir, python_base = self._split_python_path()
419 python_dir = self._normalize_cwd(python_dir)
420 self._assert_cwd(python_dir, "somethingyoudonthave",
421 executable=sys.executable, cwd=python_dir)
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000422
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100423 @unittest.skipIf(sys.base_prefix != sys.prefix,
424 'Test is not venv-compatible')
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000425 @unittest.skipIf(sysconfig.is_python_build(),
426 "need an installed Python. See #7774")
427 def test_executable_without_cwd(self):
428 # For a normal installation, it should work without 'cwd'
429 # argument. For test runs in the build directory, see #7774.
Ned Deilye92dfbf2013-08-02 18:02:21 -0700430 self._assert_cwd(os.getcwd(), "somethingyoudonthave",
431 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000432
433 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000434 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000435 p = subprocess.Popen([sys.executable, "-c",
436 'import sys; sys.exit(sys.stdin.read() == "pear")'],
437 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000438 p.stdin.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000439 p.stdin.close()
440 p.wait()
441 self.assertEqual(p.returncode, 1)
442
443 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000444 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000445 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000446 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000447 d = tf.fileno()
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000448 os.write(d, b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000449 os.lseek(d, 0, 0)
450 p = subprocess.Popen([sys.executable, "-c",
451 'import sys; sys.exit(sys.stdin.read() == "pear")'],
452 stdin=d)
453 p.wait()
454 self.assertEqual(p.returncode, 1)
455
456 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000457 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000458 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000459 self.addCleanup(tf.close)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000460 tf.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000461 tf.seek(0)
462 p = subprocess.Popen([sys.executable, "-c",
463 'import sys; sys.exit(sys.stdin.read() == "pear")'],
464 stdin=tf)
465 p.wait()
466 self.assertEqual(p.returncode, 1)
467
468 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000469 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000470 p = subprocess.Popen([sys.executable, "-c",
471 'import sys; sys.stdout.write("orange")'],
472 stdout=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +0200473 with p:
474 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000475
476 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000477 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000478 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000479 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000480 d = tf.fileno()
481 p = subprocess.Popen([sys.executable, "-c",
482 'import sys; sys.stdout.write("orange")'],
483 stdout=d)
484 p.wait()
485 os.lseek(d, 0, 0)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000486 self.assertEqual(os.read(d, 1024), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000487
488 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000489 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000490 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000491 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000492 p = subprocess.Popen([sys.executable, "-c",
493 'import sys; sys.stdout.write("orange")'],
494 stdout=tf)
495 p.wait()
496 tf.seek(0)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000497 self.assertEqual(tf.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000498
499 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000500 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000501 p = subprocess.Popen([sys.executable, "-c",
502 'import sys; sys.stderr.write("strawberry")'],
503 stderr=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +0200504 with p:
505 self.assertStderrEqual(p.stderr.read(), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000506
507 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000508 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000509 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000510 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000511 d = tf.fileno()
512 p = subprocess.Popen([sys.executable, "-c",
513 'import sys; sys.stderr.write("strawberry")'],
514 stderr=d)
515 p.wait()
516 os.lseek(d, 0, 0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000517 self.assertStderrEqual(os.read(d, 1024), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000518
519 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000520 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000521 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000522 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000523 p = subprocess.Popen([sys.executable, "-c",
524 'import sys; sys.stderr.write("strawberry")'],
525 stderr=tf)
526 p.wait()
527 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000528 self.assertStderrEqual(tf.read(), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000529
Martin Panterc7635892016-05-13 01:54:44 +0000530 def test_stderr_redirect_with_no_stdout_redirect(self):
531 # test stderr=STDOUT while stdout=None (not set)
532
533 # - grandchild prints to stderr
534 # - child redirects grandchild's stderr to its stdout
535 # - the parent should get grandchild's stderr in child's stdout
536 p = subprocess.Popen([sys.executable, "-c",
537 'import sys, subprocess;'
538 'rc = subprocess.call([sys.executable, "-c",'
539 ' "import sys;"'
540 ' "sys.stderr.write(\'42\')"],'
541 ' stderr=subprocess.STDOUT);'
542 'sys.exit(rc)'],
543 stdout=subprocess.PIPE,
544 stderr=subprocess.PIPE)
545 stdout, stderr = p.communicate()
546 #NOTE: stdout should get stderr from grandchild
547 self.assertStderrEqual(stdout, b'42')
548 self.assertStderrEqual(stderr, b'') # should be empty
549 self.assertEqual(p.returncode, 0)
550
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000551 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000552 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000553 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000554 'import sys;'
555 'sys.stdout.write("apple");'
556 'sys.stdout.flush();'
557 'sys.stderr.write("orange")'],
558 stdout=subprocess.PIPE,
559 stderr=subprocess.STDOUT)
Victor Stinner7438c612016-05-20 12:43:15 +0200560 with p:
561 self.assertStderrEqual(p.stdout.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000562
563 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000564 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000565 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000566 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000567 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000568 'import sys;'
569 'sys.stdout.write("apple");'
570 'sys.stdout.flush();'
571 'sys.stderr.write("orange")'],
572 stdout=tf,
573 stderr=tf)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000574 p.wait()
575 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000576 self.assertStderrEqual(tf.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000577
Thomas Wouters89f507f2006-12-13 04:49:30 +0000578 def test_stdout_filedes_of_stdout(self):
579 # stdout is set to 1 (#1531862).
Ezio Melotti42a541b2013-03-11 05:53:34 +0200580 # To avoid printing the text on stdout, we do something similar to
581 # test_stdout_none (see above). The parent subprocess calls the child
582 # subprocess passing stdout=1, and this test uses stdout=PIPE in
583 # order to capture and check the output of the parent. See #11963.
584 code = ('import sys, subprocess; '
585 'rc = subprocess.call([sys.executable, "-c", '
586 ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), '
587 'b\'test with stdout=1\'))"], stdout=1); '
588 'assert rc == 18')
589 p = subprocess.Popen([sys.executable, "-c", code],
590 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
591 self.addCleanup(p.stdout.close)
592 self.addCleanup(p.stderr.close)
593 out, err = p.communicate()
594 self.assertEqual(p.returncode, 0, err)
595 self.assertEqual(out.rstrip(), b'test with stdout=1')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000596
Ross Lagerwallba102ec2011-03-16 18:40:25 +0200597 def test_stdout_devnull(self):
598 p = subprocess.Popen([sys.executable, "-c",
599 'for i in range(10240):'
600 'print("x" * 1024)'],
601 stdout=subprocess.DEVNULL)
602 p.wait()
603 self.assertEqual(p.stdout, None)
604
605 def test_stderr_devnull(self):
606 p = subprocess.Popen([sys.executable, "-c",
607 'import sys\n'
608 'for i in range(10240):'
609 'sys.stderr.write("x" * 1024)'],
610 stderr=subprocess.DEVNULL)
611 p.wait()
612 self.assertEqual(p.stderr, None)
613
614 def test_stdin_devnull(self):
615 p = subprocess.Popen([sys.executable, "-c",
616 'import sys;'
617 'sys.stdin.read(1)'],
618 stdin=subprocess.DEVNULL)
619 p.wait()
620 self.assertEqual(p.stdin, None)
621
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000622 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000623 newenv = os.environ.copy()
624 newenv["FRUIT"] = "orange"
Victor Stinnerf1512a22011-06-21 17:18:38 +0200625 with subprocess.Popen([sys.executable, "-c",
626 'import sys,os;'
627 'sys.stdout.write(os.getenv("FRUIT"))'],
628 stdout=subprocess.PIPE,
629 env=newenv) as p:
630 stdout, stderr = p.communicate()
631 self.assertEqual(stdout, b"orange")
632
Victor Stinner62d51182011-06-23 01:02:25 +0200633 # Windows requires at least the SYSTEMROOT environment variable to start
634 # Python
635 @unittest.skipIf(sys.platform == 'win32',
636 'cannot test an empty env on Windows')
Gregory P. Smithb3512482017-05-30 14:40:37 -0700637 @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') == 1,
638 'The Python shared library cannot be loaded '
639 'with an empty environment.')
Victor Stinnerf1512a22011-06-21 17:18:38 +0200640 def test_empty_env(self):
Gregory P. Smithb3512482017-05-30 14:40:37 -0700641 """Verify that env={} is as empty as possible."""
642
Gregory P. Smith85aba232017-05-30 16:21:47 -0700643 def is_env_var_to_ignore(n):
Gregory P. Smithb3512482017-05-30 14:40:37 -0700644 """Determine if an environment variable is under our control."""
645 # This excludes some __CF_* and VERSIONER_* keys MacOS insists
646 # on adding even when the environment in exec is empty.
647 # Gentoo sandboxes also force LD_PRELOAD and SANDBOX_* to exist.
Gregory P. Smith85aba232017-05-30 16:21:47 -0700648 return ('VERSIONER' in n or '__CF' in n or # MacOS
Ned Deily918edc02017-09-04 00:00:21 -0400649 '__PYVENV_LAUNCHER__' in n or # MacOS framework build
Nick Coghlan6ea41862017-06-11 13:16:15 +1000650 n == 'LD_PRELOAD' or n.startswith('SANDBOX') or # Gentoo
651 n == 'LC_CTYPE') # Locale coercion triggered
Gregory P. Smithb3512482017-05-30 14:40:37 -0700652
Victor Stinnerf1512a22011-06-21 17:18:38 +0200653 with subprocess.Popen([sys.executable, "-c",
Gregory P. Smithb3512482017-05-30 14:40:37 -0700654 'import os; print(list(os.environ.keys()))'],
655 stdout=subprocess.PIPE, env={}) as p:
Victor Stinnerf1512a22011-06-21 17:18:38 +0200656 stdout, stderr = p.communicate()
Gregory P. Smithb3512482017-05-30 14:40:37 -0700657 child_env_names = eval(stdout.strip())
658 self.assertIsInstance(child_env_names, list)
659 child_env_names = [k for k in child_env_names
660 if not is_env_var_to_ignore(k)]
661 self.assertEqual(child_env_names, [])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000662
Serhiy Storchakad174d242017-06-23 19:39:27 +0300663 def test_invalid_cmd(self):
664 # null character in the command name
665 cmd = sys.executable + '\0'
666 with self.assertRaises(ValueError):
667 subprocess.Popen([cmd, "-c", "pass"])
668
669 # null character in the command argument
670 with self.assertRaises(ValueError):
671 subprocess.Popen([sys.executable, "-c", "pass#\0"])
672
673 def test_invalid_env(self):
Ville Skyttä49b27342017-08-03 09:00:59 +0300674 # null character in the environment variable name
Serhiy Storchakad174d242017-06-23 19:39:27 +0300675 newenv = os.environ.copy()
676 newenv["FRUIT\0VEGETABLE"] = "cabbage"
677 with self.assertRaises(ValueError):
678 subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
679
Ville Skyttä49b27342017-08-03 09:00:59 +0300680 # null character in the environment variable value
Serhiy Storchakad174d242017-06-23 19:39:27 +0300681 newenv = os.environ.copy()
682 newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
683 with self.assertRaises(ValueError):
684 subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
685
Ville Skyttä49b27342017-08-03 09:00:59 +0300686 # equal character in the environment variable name
Serhiy Storchakad174d242017-06-23 19:39:27 +0300687 newenv = os.environ.copy()
688 newenv["FRUIT=ORANGE"] = "lemon"
689 with self.assertRaises(ValueError):
690 subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
691
Ville Skyttä49b27342017-08-03 09:00:59 +0300692 # equal character in the environment variable value
Serhiy Storchakad174d242017-06-23 19:39:27 +0300693 newenv = os.environ.copy()
694 newenv["FRUIT"] = "orange=lemon"
695 with subprocess.Popen([sys.executable, "-c",
696 'import sys, os;'
697 'sys.stdout.write(os.getenv("FRUIT"))'],
698 stdout=subprocess.PIPE,
699 env=newenv) as p:
700 stdout, stderr = p.communicate()
701 self.assertEqual(stdout, b"orange=lemon")
702
Peter Astrandcbac93c2005-03-03 20:24:28 +0000703 def test_communicate_stdin(self):
704 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000705 'import sys;'
706 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000707 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000708 p.communicate(b"pear")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000709 self.assertEqual(p.returncode, 1)
710
711 def test_communicate_stdout(self):
712 p = subprocess.Popen([sys.executable, "-c",
713 'import sys; sys.stdout.write("pineapple")'],
714 stdout=subprocess.PIPE)
715 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000716 self.assertEqual(stdout, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000717 self.assertEqual(stderr, None)
718
719 def test_communicate_stderr(self):
720 p = subprocess.Popen([sys.executable, "-c",
721 'import sys; sys.stderr.write("pineapple")'],
722 stderr=subprocess.PIPE)
723 (stdout, stderr) = p.communicate()
724 self.assertEqual(stdout, None)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000725 self.assertStderrEqual(stderr, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000726
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000727 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000728 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000729 'import sys,os;'
730 'sys.stderr.write("pineapple");'
731 'sys.stdout.write(sys.stdin.read())'],
732 stdin=subprocess.PIPE,
733 stdout=subprocess.PIPE,
734 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000735 self.addCleanup(p.stdout.close)
736 self.addCleanup(p.stderr.close)
737 self.addCleanup(p.stdin.close)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000738 (stdout, stderr) = p.communicate(b"banana")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000739 self.assertEqual(stdout, b"banana")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000740 self.assertStderrEqual(stderr, b"pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000741
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400742 def test_communicate_timeout(self):
743 p = subprocess.Popen([sys.executable, "-c",
744 'import sys,os,time;'
745 'sys.stderr.write("pineapple\\n");'
746 'time.sleep(1);'
747 'sys.stderr.write("pear\\n");'
748 'sys.stdout.write(sys.stdin.read())'],
749 universal_newlines=True,
750 stdin=subprocess.PIPE,
751 stdout=subprocess.PIPE,
752 stderr=subprocess.PIPE)
753 self.assertRaises(subprocess.TimeoutExpired, p.communicate, "banana",
754 timeout=0.3)
755 # Make sure we can keep waiting for it, and that we get the whole output
756 # after it completes.
757 (stdout, stderr) = p.communicate()
758 self.assertEqual(stdout, "banana")
759 self.assertStderrEqual(stderr.encode(), b"pineapple\npear\n")
760
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700761 def test_communicate_timeout_large_output(self):
Ross Lagerwall003c7a32012-02-12 09:02:01 +0200762 # Test an expiring timeout while the child is outputting lots of data.
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400763 p = subprocess.Popen([sys.executable, "-c",
764 'import sys,os,time;'
765 'sys.stdout.write("a" * (64 * 1024));'
766 'time.sleep(0.2);'
767 'sys.stdout.write("a" * (64 * 1024));'
768 'time.sleep(0.2);'
769 'sys.stdout.write("a" * (64 * 1024));'
770 'time.sleep(0.2);'
771 'sys.stdout.write("a" * (64 * 1024));'],
772 stdout=subprocess.PIPE)
773 self.assertRaises(subprocess.TimeoutExpired, p.communicate, timeout=0.4)
774 (stdout, _) = p.communicate()
775 self.assertEqual(len(stdout), 4 * 64 * 1024)
776
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000777 # Test for the fd leak reported in http://bugs.python.org/issue2791.
778 def test_communicate_pipe_fd_leak(self):
Victor Stinner667d4b52010-12-25 22:40:32 +0000779 for stdin_pipe in (False, True):
780 for stdout_pipe in (False, True):
781 for stderr_pipe in (False, True):
782 options = {}
783 if stdin_pipe:
784 options['stdin'] = subprocess.PIPE
785 if stdout_pipe:
786 options['stdout'] = subprocess.PIPE
787 if stderr_pipe:
788 options['stderr'] = subprocess.PIPE
789 if not options:
790 continue
791 p = subprocess.Popen((sys.executable, "-c", "pass"), **options)
792 p.communicate()
793 if p.stdin is not None:
794 self.assertTrue(p.stdin.closed)
795 if p.stdout is not None:
796 self.assertTrue(p.stdout.closed)
797 if p.stderr is not None:
798 self.assertTrue(p.stderr.closed)
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000799
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000800 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000801 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000802 p = subprocess.Popen([sys.executable, "-c",
803 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000804 (stdout, stderr) = p.communicate()
805 self.assertEqual(stdout, None)
806 self.assertEqual(stderr, None)
807
808 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000809 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000810 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000811 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000812 x, y = os.pipe()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000813 os.close(x)
814 os.close(y)
815 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000816 'import sys,os;'
817 'sys.stdout.write(sys.stdin.read(47));'
Charles-François Natali2d517212011-05-29 16:36:44 +0200818 'sys.stderr.write("x" * %d);'
819 'sys.stdout.write(sys.stdin.read())' %
820 support.PIPE_MAX_SIZE],
Guido van Rossum98297ee2007-11-06 21:34:58 +0000821 stdin=subprocess.PIPE,
822 stdout=subprocess.PIPE,
823 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000824 self.addCleanup(p.stdout.close)
825 self.addCleanup(p.stderr.close)
826 self.addCleanup(p.stdin.close)
Charles-François Natali2d517212011-05-29 16:36:44 +0200827 string_to_write = b"a" * support.PIPE_MAX_SIZE
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000828 (stdout, stderr) = p.communicate(string_to_write)
829 self.assertEqual(stdout, string_to_write)
830
831 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000832 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000833 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000834 'import sys,os;'
835 'sys.stdout.write(sys.stdin.read())'],
836 stdin=subprocess.PIPE,
837 stdout=subprocess.PIPE,
838 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000839 self.addCleanup(p.stdout.close)
840 self.addCleanup(p.stderr.close)
841 self.addCleanup(p.stdin.close)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000842 p.stdin.write(b"banana")
843 (stdout, stderr) = p.communicate(b"split")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000844 self.assertEqual(stdout, b"bananasplit")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000845 self.assertStderrEqual(stderr, b"")
Tim Peterse718f612004-10-12 21:51:32 +0000846
andyclegg7fed7bd2017-10-23 03:01:19 +0100847 def test_universal_newlines_and_text(self):
848 args = [
849 sys.executable, "-c",
850 'import sys,os;' + SETBINARY +
851 'buf = sys.stdout.buffer;'
852 'buf.write(sys.stdin.readline().encode());'
853 'buf.flush();'
854 'buf.write(b"line2\\n");'
855 'buf.flush();'
856 'buf.write(sys.stdin.read().encode());'
857 'buf.flush();'
858 'buf.write(b"line4\\n");'
859 'buf.flush();'
860 'buf.write(b"line5\\r\\n");'
861 'buf.flush();'
862 'buf.write(b"line6\\r");'
863 'buf.flush();'
864 'buf.write(b"\\nline7");'
865 'buf.flush();'
866 'buf.write(b"\\nline8");']
867
868 for extra_kwarg in ('universal_newlines', 'text'):
869 p = subprocess.Popen(args, **{'stdin': subprocess.PIPE,
870 'stdout': subprocess.PIPE,
871 extra_kwarg: True})
872 with p:
873 p.stdin.write("line1\n")
874 p.stdin.flush()
875 self.assertEqual(p.stdout.readline(), "line1\n")
876 p.stdin.write("line3\n")
877 p.stdin.close()
878 self.addCleanup(p.stdout.close)
879 self.assertEqual(p.stdout.readline(),
880 "line2\n")
881 self.assertEqual(p.stdout.read(6),
882 "line3\n")
883 self.assertEqual(p.stdout.read(),
884 "line4\nline5\nline6\nline7\nline8")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000885
886 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000887 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000888 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000889 'import sys,os;' + SETBINARY +
Antoine Pitrouec2d2692012-08-05 00:23:40 +0200890 'buf = sys.stdout.buffer;'
891 'buf.write(b"line2\\n");'
892 'buf.flush();'
893 'buf.write(b"line4\\n");'
894 'buf.flush();'
895 'buf.write(b"line5\\r\\n");'
896 'buf.flush();'
897 'buf.write(b"line6\\r");'
898 'buf.flush();'
899 'buf.write(b"\\nline7");'
900 'buf.flush();'
901 'buf.write(b"\\nline8");'],
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200902 stderr=subprocess.PIPE,
903 stdout=subprocess.PIPE,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000904 universal_newlines=1)
Brian Curtin19a53792010-11-05 17:09:05 +0000905 self.addCleanup(p.stdout.close)
906 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000907 (stdout, stderr) = p.communicate()
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200908 self.assertEqual(stdout,
909 "line2\nline4\nline5\nline6\nline7\nline8")
910
911 def test_universal_newlines_communicate_stdin(self):
912 # universal newlines through communicate(), with only stdin
913 p = subprocess.Popen([sys.executable, "-c",
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300914 'import sys,os;' + SETBINARY + textwrap.dedent('''
915 s = sys.stdin.readline()
916 assert s == "line1\\n", repr(s)
917 s = sys.stdin.read()
918 assert s == "line3\\n", repr(s)
919 ''')],
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200920 stdin=subprocess.PIPE,
921 universal_newlines=1)
922 (stdout, stderr) = p.communicate("line1\nline3\n")
923 self.assertEqual(p.returncode, 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000924
Andrew Svetlovf3765072012-08-14 18:35:17 +0300925 def test_universal_newlines_communicate_input_none(self):
926 # Test communicate(input=None) with universal newlines.
927 #
928 # We set stdout to PIPE because, as of this writing, a different
929 # code path is tested when the number of pipes is zero or one.
930 p = subprocess.Popen([sys.executable, "-c", "pass"],
931 stdin=subprocess.PIPE,
932 stdout=subprocess.PIPE,
933 universal_newlines=True)
934 p.communicate()
935 self.assertEqual(p.returncode, 0)
936
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300937 def test_universal_newlines_communicate_stdin_stdout_stderr(self):
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300938 # universal newlines through communicate(), with stdin, stdout, stderr
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300939 p = subprocess.Popen([sys.executable, "-c",
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300940 'import sys,os;' + SETBINARY + textwrap.dedent('''
941 s = sys.stdin.buffer.readline()
942 sys.stdout.buffer.write(s)
943 sys.stdout.buffer.write(b"line2\\r")
944 sys.stderr.buffer.write(b"eline2\\n")
945 s = sys.stdin.buffer.read()
946 sys.stdout.buffer.write(s)
947 sys.stdout.buffer.write(b"line4\\n")
948 sys.stdout.buffer.write(b"line5\\r\\n")
949 sys.stderr.buffer.write(b"eline6\\r")
950 sys.stderr.buffer.write(b"eline7\\r\\nz")
951 ''')],
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300952 stdin=subprocess.PIPE,
953 stderr=subprocess.PIPE,
954 stdout=subprocess.PIPE,
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300955 universal_newlines=True)
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300956 self.addCleanup(p.stdout.close)
957 self.addCleanup(p.stderr.close)
958 (stdout, stderr) = p.communicate("line1\nline3\n")
959 self.assertEqual(p.returncode, 0)
Andrew Svetlov943c5b32012-08-16 20:17:47 +0300960 self.assertEqual("line1\nline2\nline3\nline4\nline5\n", stdout)
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300961 # Python debug build push something like "[42442 refs]\n"
962 # to stderr at exit of subprocess.
Andrew Svetlov943c5b32012-08-16 20:17:47 +0300963 # Don't use assertStderrEqual because it strips CR and LF from output.
964 self.assertTrue(stderr.startswith("eline2\neline6\neline7\n"))
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300965
Andrew Svetlov82860712012-08-19 22:13:41 +0300966 def test_universal_newlines_communicate_encodings(self):
967 # Check that universal newlines mode works for various encodings,
968 # in particular for encodings in the UTF-16 and UTF-32 families.
969 # See issue #15595.
970 #
971 # UTF-16 and UTF-32-BE are sufficient to check both with BOM and
972 # without, and UTF-16 and UTF-32.
973 for encoding in ['utf-16', 'utf-32-be']:
Andrew Svetlov82860712012-08-19 22:13:41 +0300974 code = ("import sys; "
975 r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" %
976 encoding)
977 args = [sys.executable, '-c', code]
Steve Dower050acae2016-09-06 20:16:17 -0700978 # We set stdin to be non-None because, as of this writing,
979 # a different code path is used when the number of pipes is
980 # zero or one.
981 popen = subprocess.Popen(args,
982 stdin=subprocess.PIPE,
983 stdout=subprocess.PIPE,
984 encoding=encoding)
985 stdout, stderr = popen.communicate(input='')
Andrew Svetlov82860712012-08-19 22:13:41 +0300986 self.assertEqual(stdout, '1\n2\n3\n4')
987
Steve Dower050acae2016-09-06 20:16:17 -0700988 def test_communicate_errors(self):
989 for errors, expected in [
990 ('ignore', ''),
991 ('replace', '\ufffd\ufffd'),
992 ('surrogateescape', '\udc80\udc80'),
993 ('backslashreplace', '\\x80\\x80'),
994 ]:
995 code = ("import sys; "
996 r"sys.stdout.buffer.write(b'[\x80\x80]')")
997 args = [sys.executable, '-c', code]
998 # We set stdin to be non-None because, as of this writing,
999 # a different code path is used when the number of pipes is
1000 # zero or one.
1001 popen = subprocess.Popen(args,
1002 stdin=subprocess.PIPE,
1003 stdout=subprocess.PIPE,
1004 encoding='utf-8',
1005 errors=errors)
1006 stdout, stderr = popen.communicate(input='')
1007 self.assertEqual(stdout, '[{}]'.format(expected))
1008
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001009 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +00001010 # Make sure we leak no resources
Antoine Pitrou8db30272010-09-18 22:38:48 +00001011 if not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +00001012 max_handles = 1026 # too much for most UNIX systems
1013 else:
Antoine Pitrou8db30272010-09-18 22:38:48 +00001014 max_handles = 2050 # too much for (at least some) Windows setups
1015 handles = []
Gregory P. Smith81ce6852011-03-15 02:04:11 -04001016 tmpdir = tempfile.mkdtemp()
Antoine Pitrou8db30272010-09-18 22:38:48 +00001017 try:
1018 for i in range(max_handles):
1019 try:
Gregory P. Smith81ce6852011-03-15 02:04:11 -04001020 tmpfile = os.path.join(tmpdir, support.TESTFN)
1021 handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT))
Antoine Pitrou8db30272010-09-18 22:38:48 +00001022 except OSError as e:
1023 if e.errno != errno.EMFILE:
1024 raise
1025 break
1026 else:
1027 self.skipTest("failed to reach the file descriptor limit "
1028 "(tried %d)" % max_handles)
1029 # Close a couple of them (should be enough for a subprocess)
1030 for i in range(10):
1031 os.close(handles.pop())
1032 # Loop creating some subprocesses. If one of them leaks some fds,
1033 # the next loop iteration will fail by reaching the max fd limit.
1034 for i in range(15):
1035 p = subprocess.Popen([sys.executable, "-c",
1036 "import sys;"
1037 "sys.stdout.write(sys.stdin.read())"],
1038 stdin=subprocess.PIPE,
1039 stdout=subprocess.PIPE,
1040 stderr=subprocess.PIPE)
1041 data = p.communicate(b"lime")[0]
1042 self.assertEqual(data, b"lime")
1043 finally:
1044 for h in handles:
1045 os.close(h)
Gregory P. Smith81ce6852011-03-15 02:04:11 -04001046 shutil.rmtree(tmpdir)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001047
1048 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001049 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
1050 '"a b c" d e')
1051 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
1052 'ab\\"c \\ d')
Christian Heimesfdab48e2008-01-20 09:06:41 +00001053 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
1054 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001055 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
1056 'a\\\\\\b "de fg" h')
1057 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
1058 'a\\\\\\"b c d')
1059 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
1060 '"a\\\\b c" d e')
1061 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
1062 '"a\\\\b\\ c" d e')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001063 self.assertEqual(subprocess.list2cmdline(['ab', '']),
1064 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001065
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001066 def test_poll(self):
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001067 p = subprocess.Popen([sys.executable, "-c",
Ross Lagerwalle7ad4192012-02-22 06:02:07 +02001068 "import os; os.read(0, 1)"],
1069 stdin=subprocess.PIPE)
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001070 self.addCleanup(p.stdin.close)
1071 self.assertIsNone(p.poll())
1072 os.write(p.stdin.fileno(), b'A')
1073 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001074 # Subsequent invocations should just return the returncode
1075 self.assertEqual(p.poll(), 0)
1076
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001077 def test_wait(self):
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001078 p = subprocess.Popen([sys.executable, "-c", "pass"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001079 self.assertEqual(p.wait(), 0)
1080 # Subsequent invocations should just return the returncode
1081 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +00001082
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001083 def test_wait_timeout(self):
1084 p = subprocess.Popen([sys.executable,
Antoine Pitroudc49b2b2013-05-19 15:55:40 +02001085 "-c", "import time; time.sleep(0.3)"])
Reid Kleckner2b228f02011-03-16 16:57:54 -04001086 with self.assertRaises(subprocess.TimeoutExpired) as c:
Antoine Pitroudc49b2b2013-05-19 15:55:40 +02001087 p.wait(timeout=0.0001)
1088 self.assertIn("0.0001", str(c.exception)) # For coverage of __str__.
Reid Klecknerda9ac722011-03-16 17:08:21 -04001089 # Some heavily loaded buildbots (sparc Debian 3.x) require this much
1090 # time to start.
1091 self.assertEqual(p.wait(timeout=3), 0)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001092
Peter Astrand738131d2004-11-30 21:04:45 +00001093 def test_invalid_bufsize(self):
1094 # an invalid type of the bufsize argument should raise
1095 # TypeError.
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001096 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +00001097 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +00001098
Guido van Rossum46a05a72007-06-07 21:56:45 +00001099 def test_bufsize_is_none(self):
1100 # bufsize=None should be the same as bufsize=0.
1101 p = subprocess.Popen([sys.executable, "-c", "pass"], None)
1102 self.assertEqual(p.wait(), 0)
1103 # Again with keyword arg
1104 p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None)
1105 self.assertEqual(p.wait(), 0)
1106
Antoine Pitrouafe8d062014-09-21 21:10:56 +02001107 def _test_bufsize_equal_one(self, line, expected, universal_newlines):
1108 # subprocess may deadlock with bufsize=1, see issue #21332
1109 with subprocess.Popen([sys.executable, "-c", "import sys;"
1110 "sys.stdout.write(sys.stdin.readline());"
1111 "sys.stdout.flush()"],
1112 stdin=subprocess.PIPE,
1113 stdout=subprocess.PIPE,
1114 stderr=subprocess.DEVNULL,
1115 bufsize=1,
1116 universal_newlines=universal_newlines) as p:
1117 p.stdin.write(line) # expect that it flushes the line in text mode
1118 os.close(p.stdin.fileno()) # close it without flushing the buffer
1119 read_line = p.stdout.readline()
Segev Finer4d385172017-08-18 16:18:13 +03001120 with support.SuppressCrashReport():
1121 try:
1122 p.stdin.close()
1123 except OSError:
1124 pass
Antoine Pitrouafe8d062014-09-21 21:10:56 +02001125 p.stdin = None
1126 self.assertEqual(p.returncode, 0)
1127 self.assertEqual(read_line, expected)
1128
1129 def test_bufsize_equal_one_text_mode(self):
1130 # line is flushed in text mode with bufsize=1.
1131 # we should get the full line in return
1132 line = "line\n"
1133 self._test_bufsize_equal_one(line, line, universal_newlines=True)
1134
1135 def test_bufsize_equal_one_binary_mode(self):
1136 # line is not flushed in binary mode with bufsize=1.
1137 # we should get empty response
1138 line = b'line' + os.linesep.encode() # assume ascii-based locale
1139 self._test_bufsize_equal_one(line, b'', universal_newlines=False)
1140
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001141 def test_leaking_fds_on_error(self):
1142 # see bug #5179: Popen leaks file descriptors to PIPEs if
1143 # the child fails to execute; this will eventually exhaust
1144 # the maximum number of open fds. 1024 seems a very common
1145 # value for that limit, but Windows has 2048, so we loop
1146 # 1024 times (each call leaked two fds).
1147 for i in range(1024):
Victor Stinnerb31206a2018-01-25 19:06:05 +01001148 with self.assertRaises(NONEXISTING_ERRORS):
Victor Stinner9a83f652017-08-21 23:51:31 +02001149 subprocess.Popen(NONEXISTING_CMD,
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001150 stdout=subprocess.PIPE,
1151 stderr=subprocess.PIPE)
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001152
Victor Stinner9a83f652017-08-21 23:51:31 +02001153 def test_nonexisting_with_pipes(self):
1154 # bpo-30121: Popen with pipes must close properly pipes on error.
1155 # Previously, os.close() was called with a Windows handle which is not
1156 # a valid file descriptor.
1157 #
1158 # Run the test in a subprocess to control how the CRT reports errors
1159 # and to get stderr content.
1160 try:
1161 import msvcrt
1162 msvcrt.CrtSetReportMode
1163 except (AttributeError, ImportError):
1164 self.skipTest("need msvcrt.CrtSetReportMode")
1165
1166 code = textwrap.dedent(f"""
1167 import msvcrt
1168 import subprocess
1169
1170 cmd = {NONEXISTING_CMD!r}
1171
1172 for report_type in [msvcrt.CRT_WARN,
1173 msvcrt.CRT_ERROR,
1174 msvcrt.CRT_ASSERT]:
1175 msvcrt.CrtSetReportMode(report_type, msvcrt.CRTDBG_MODE_FILE)
1176 msvcrt.CrtSetReportFile(report_type, msvcrt.CRTDBG_FILE_STDERR)
1177
1178 try:
Miss Islington (bot)622a8242018-02-19 13:00:22 -08001179 subprocess.Popen(cmd,
Victor Stinner9a83f652017-08-21 23:51:31 +02001180 stdout=subprocess.PIPE,
1181 stderr=subprocess.PIPE)
1182 except OSError:
1183 pass
1184 """)
1185 cmd = [sys.executable, "-c", code]
1186 proc = subprocess.Popen(cmd,
1187 stderr=subprocess.PIPE,
1188 universal_newlines=True)
1189 with proc:
1190 stderr = proc.communicate()[1]
1191 self.assertEqual(stderr, "")
1192 self.assertEqual(proc.returncode, 0)
1193
Antoine Pitroua8392712013-08-30 23:38:13 +02001194 def test_double_close_on_error(self):
1195 # Issue #18851
1196 fds = []
1197 def open_fds():
1198 for i in range(20):
1199 fds.extend(os.pipe())
1200 time.sleep(0.001)
1201 t = threading.Thread(target=open_fds)
1202 t.start()
1203 try:
1204 with self.assertRaises(EnvironmentError):
Victor Stinner9a83f652017-08-21 23:51:31 +02001205 subprocess.Popen(NONEXISTING_CMD,
Antoine Pitroua8392712013-08-30 23:38:13 +02001206 stdin=subprocess.PIPE,
1207 stdout=subprocess.PIPE,
1208 stderr=subprocess.PIPE)
1209 finally:
1210 t.join()
1211 exc = None
1212 for fd in fds:
1213 # If a double close occurred, some of those fds will
1214 # already have been closed by mistake, and os.close()
1215 # here will raise.
1216 try:
1217 os.close(fd)
1218 except OSError as e:
1219 exc = e
1220 if exc is not None:
1221 raise exc
1222
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001223 def test_threadsafe_wait(self):
1224 """Issue21291: Popen.wait() needs to be threadsafe for returncode."""
1225 proc = subprocess.Popen([sys.executable, '-c',
1226 'import time; time.sleep(12)'])
1227 self.assertEqual(proc.returncode, None)
1228 results = []
1229
1230 def kill_proc_timer_thread():
1231 results.append(('thread-start-poll-result', proc.poll()))
1232 # terminate it from the thread and wait for the result.
1233 proc.kill()
1234 proc.wait()
1235 results.append(('thread-after-kill-and-wait', proc.returncode))
1236 # this wait should be a no-op given the above.
1237 proc.wait()
1238 results.append(('thread-after-second-wait', proc.returncode))
1239
1240 # This is a timing sensitive test, the failure mode is
1241 # triggered when both the main thread and this thread are in
1242 # the wait() call at once. The delay here is to allow the
1243 # main thread to most likely be blocked in its wait() call.
1244 t = threading.Timer(0.2, kill_proc_timer_thread)
1245 t.start()
1246
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001247 if mswindows:
1248 expected_errorcode = 1
1249 else:
1250 # Should be -9 because of the proc.kill() from the thread.
1251 expected_errorcode = -9
1252
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001253 # Wait for the process to finish; the thread should kill it
1254 # long before it finishes on its own. Supplying a timeout
1255 # triggers a different code path for better coverage.
1256 proc.wait(timeout=20)
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001257 self.assertEqual(proc.returncode, expected_errorcode,
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001258 msg="unexpected result in wait from main thread")
1259
1260 # This should be a no-op with no change in returncode.
1261 proc.wait()
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001262 self.assertEqual(proc.returncode, expected_errorcode,
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001263 msg="unexpected result in second main wait.")
1264
1265 t.join()
1266 # Ensure that all of the thread results are as expected.
1267 # When a race condition occurs in wait(), the returncode could
1268 # be set by the wrong thread that doesn't actually have it
1269 # leading to an incorrect value.
1270 self.assertEqual([('thread-start-poll-result', None),
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001271 ('thread-after-kill-and-wait', expected_errorcode),
1272 ('thread-after-second-wait', expected_errorcode)],
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001273 results)
1274
Victor Stinnerb3693582010-05-21 20:13:12 +00001275 def test_issue8780(self):
1276 # Ensure that stdout is inherited from the parent
1277 # if stdout=PIPE is not used
1278 code = ';'.join((
1279 'import subprocess, sys',
1280 'retcode = subprocess.call('
1281 "[sys.executable, '-c', 'print(\"Hello World!\")'])",
1282 'assert retcode == 0'))
1283 output = subprocess.check_output([sys.executable, '-c', code])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001284 self.assertTrue(output.startswith(b'Hello World!'), ascii(output))
Victor Stinnerb3693582010-05-21 20:13:12 +00001285
Tim Goldenaf5ac392010-08-06 13:03:56 +00001286 def test_handles_closed_on_exception(self):
1287 # If CreateProcess exits with an error, ensure the
1288 # duplicate output handles are released
Berker Peksag16a1f282015-09-28 13:33:14 +03001289 ifhandle, ifname = tempfile.mkstemp()
1290 ofhandle, ofname = tempfile.mkstemp()
1291 efhandle, efname = tempfile.mkstemp()
Tim Goldenaf5ac392010-08-06 13:03:56 +00001292 try:
1293 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
1294 stderr=efhandle)
1295 except OSError:
1296 os.close(ifhandle)
1297 os.remove(ifname)
1298 os.close(ofhandle)
1299 os.remove(ofname)
1300 os.close(efhandle)
1301 os.remove(efname)
1302 self.assertFalse(os.path.exists(ifname))
1303 self.assertFalse(os.path.exists(ofname))
1304 self.assertFalse(os.path.exists(efname))
1305
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001306 def test_communicate_epipe(self):
1307 # Issue 10963: communicate() should hide EPIPE
1308 p = subprocess.Popen([sys.executable, "-c", 'pass'],
1309 stdin=subprocess.PIPE,
1310 stdout=subprocess.PIPE,
1311 stderr=subprocess.PIPE)
1312 self.addCleanup(p.stdout.close)
1313 self.addCleanup(p.stderr.close)
1314 self.addCleanup(p.stdin.close)
1315 p.communicate(b"x" * 2**20)
1316
1317 def test_communicate_epipe_only_stdin(self):
1318 # Issue 10963: communicate() should hide EPIPE
1319 p = subprocess.Popen([sys.executable, "-c", 'pass'],
1320 stdin=subprocess.PIPE)
1321 self.addCleanup(p.stdin.close)
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001322 p.wait()
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001323 p.communicate(b"x" * 2**20)
1324
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001325 @unittest.skipUnless(hasattr(signal, 'SIGUSR1'),
1326 "Requires signal.SIGUSR1")
1327 @unittest.skipUnless(hasattr(os, 'kill'),
1328 "Requires os.kill")
1329 @unittest.skipUnless(hasattr(os, 'getppid'),
1330 "Requires os.getppid")
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001331 def test_communicate_eintr(self):
1332 # Issue #12493: communicate() should handle EINTR
1333 def handler(signum, frame):
1334 pass
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001335 old_handler = signal.signal(signal.SIGUSR1, handler)
1336 self.addCleanup(signal.signal, signal.SIGUSR1, old_handler)
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001337
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001338 args = [sys.executable, "-c",
1339 'import os, signal;'
1340 'os.kill(os.getppid(), signal.SIGUSR1)']
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001341 for stream in ('stdout', 'stderr'):
1342 kw = {stream: subprocess.PIPE}
1343 with subprocess.Popen(args, **kw) as process:
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001344 # communicate() will be interrupted by SIGUSR1
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001345 process.communicate()
1346
Tim Peterse718f612004-10-12 21:51:32 +00001347
Gregory P. Smith3d8e7762012-11-10 22:32:22 -08001348 # This test is Linux-ish specific for simplicity to at least have
1349 # some coverage. It is not a platform specific bug.
1350 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
1351 "Linux specific")
1352 def test_failed_child_execute_fd_leak(self):
1353 """Test for the fork() failure fd leak reported in issue16327."""
1354 fd_directory = '/proc/%d/fd' % os.getpid()
1355 fds_before_popen = os.listdir(fd_directory)
1356 with self.assertRaises(PopenTestException):
1357 PopenExecuteChildRaises(
1358 [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE,
1359 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1360
1361 # NOTE: This test doesn't verify that the real _execute_child
1362 # does not close the file descriptors itself on the way out
1363 # during an exception. Code inspection has confirmed that.
1364
1365 fds_after_exception = os.listdir(fd_directory)
1366 self.assertEqual(fds_before_popen, fds_after_exception)
1367
Gregory P. Smitha3a6df32017-08-24 18:15:02 -07001368 @unittest.skipIf(mswindows, "behavior currently not supported on Windows")
Gregory P. Smith8621bb52017-08-24 14:58:25 -07001369 def test_file_not_found_includes_filename(self):
1370 with self.assertRaises(FileNotFoundError) as c:
1371 subprocess.call(['/opt/nonexistent_binary', 'with', 'some', 'args'])
1372 self.assertEqual(c.exception.filename, '/opt/nonexistent_binary')
1373
Gregory P. Smitha3a6df32017-08-24 18:15:02 -07001374 @unittest.skipIf(mswindows, "behavior currently not supported on Windows")
Gregory P. Smith8621bb52017-08-24 14:58:25 -07001375 def test_file_not_found_with_bad_cwd(self):
1376 with self.assertRaises(FileNotFoundError) as c:
1377 subprocess.Popen(['exit', '0'], cwd='/some/nonexistent/directory')
1378 self.assertEqual(c.exception.filename, '/some/nonexistent/directory')
1379
Gregory P. Smith6e730002015-04-14 16:14:25 -07001380
1381class RunFuncTestCase(BaseTestCase):
1382 def run_python(self, code, **kwargs):
1383 """Run Python code in a subprocess using subprocess.run"""
1384 argv = [sys.executable, "-c", code]
1385 return subprocess.run(argv, **kwargs)
1386
1387 def test_returncode(self):
1388 # call() function with sequence argument
1389 cp = self.run_python("import sys; sys.exit(47)")
1390 self.assertEqual(cp.returncode, 47)
1391 with self.assertRaises(subprocess.CalledProcessError):
1392 cp.check_returncode()
1393
1394 def test_check(self):
1395 with self.assertRaises(subprocess.CalledProcessError) as c:
1396 self.run_python("import sys; sys.exit(47)", check=True)
1397 self.assertEqual(c.exception.returncode, 47)
1398
1399 def test_check_zero(self):
1400 # check_returncode shouldn't raise when returncode is zero
1401 cp = self.run_python("import sys; sys.exit(0)", check=True)
1402 self.assertEqual(cp.returncode, 0)
1403
1404 def test_timeout(self):
1405 # run() function with timeout argument; we want to test that the child
1406 # process gets killed when the timeout expires. If the child isn't
1407 # killed, this call will deadlock since subprocess.run waits for the
1408 # child.
1409 with self.assertRaises(subprocess.TimeoutExpired):
1410 self.run_python("while True: pass", timeout=0.0001)
1411
1412 def test_capture_stdout(self):
1413 # capture stdout with zero return code
1414 cp = self.run_python("print('BDFL')", stdout=subprocess.PIPE)
1415 self.assertIn(b'BDFL', cp.stdout)
1416
1417 def test_capture_stderr(self):
1418 cp = self.run_python("import sys; sys.stderr.write('BDFL')",
1419 stderr=subprocess.PIPE)
1420 self.assertIn(b'BDFL', cp.stderr)
1421
1422 def test_check_output_stdin_arg(self):
1423 # run() can be called with stdin set to a file
1424 tf = tempfile.TemporaryFile()
1425 self.addCleanup(tf.close)
1426 tf.write(b'pear')
1427 tf.seek(0)
1428 cp = self.run_python(
1429 "import sys; sys.stdout.write(sys.stdin.read().upper())",
1430 stdin=tf, stdout=subprocess.PIPE)
1431 self.assertIn(b'PEAR', cp.stdout)
1432
1433 def test_check_output_input_arg(self):
1434 # check_output() can be called with input set to a string
1435 cp = self.run_python(
1436 "import sys; sys.stdout.write(sys.stdin.read().upper())",
1437 input=b'pear', stdout=subprocess.PIPE)
1438 self.assertIn(b'PEAR', cp.stdout)
1439
1440 def test_check_output_stdin_with_input_arg(self):
1441 # run() refuses to accept 'stdin' with 'input'
1442 tf = tempfile.TemporaryFile()
1443 self.addCleanup(tf.close)
1444 tf.write(b'pear')
1445 tf.seek(0)
1446 with self.assertRaises(ValueError,
1447 msg="Expected ValueError when stdin and input args supplied.") as c:
1448 output = self.run_python("print('will not be run')",
1449 stdin=tf, input=b'hare')
1450 self.assertIn('stdin', c.exception.args[0])
1451 self.assertIn('input', c.exception.args[0])
1452
1453 def test_check_output_timeout(self):
1454 with self.assertRaises(subprocess.TimeoutExpired) as c:
1455 cp = self.run_python((
1456 "import sys, time\n"
1457 "sys.stdout.write('BDFL')\n"
1458 "sys.stdout.flush()\n"
1459 "time.sleep(3600)"),
1460 # Some heavily loaded buildbots (sparc Debian 3.x) require
1461 # this much time to start and print.
1462 timeout=3, stdout=subprocess.PIPE)
1463 self.assertEqual(c.exception.output, b'BDFL')
1464 # output is aliased to stdout
1465 self.assertEqual(c.exception.stdout, b'BDFL')
1466
1467 def test_run_kwargs(self):
1468 newenv = os.environ.copy()
1469 newenv["FRUIT"] = "banana"
1470 cp = self.run_python(('import sys, os;'
1471 'sys.exit(33 if os.getenv("FRUIT")=="banana" else 31)'),
1472 env=newenv)
1473 self.assertEqual(cp.returncode, 33)
1474
Bo Baylesce0f33d2018-01-30 00:40:39 -06001475 def test_capture_output(self):
1476 cp = self.run_python(("import sys;"
1477 "sys.stdout.write('BDFL'); "
1478 "sys.stderr.write('FLUFL')"),
1479 capture_output=True)
1480 self.assertIn(b'BDFL', cp.stdout)
1481 self.assertIn(b'FLUFL', cp.stderr)
1482
1483 def test_stdout_with_capture_output_arg(self):
1484 # run() refuses to accept 'stdout' with 'capture_output'
1485 tf = tempfile.TemporaryFile()
1486 self.addCleanup(tf.close)
1487 with self.assertRaises(ValueError,
1488 msg=("Expected ValueError when stdout and capture_output "
1489 "args supplied.")) as c:
1490 output = self.run_python("print('will not be run')",
1491 capture_output=True, stdout=tf)
1492 self.assertIn('stdout', c.exception.args[0])
1493 self.assertIn('capture_output', c.exception.args[0])
1494
1495 def test_stderr_with_capture_output_arg(self):
1496 # run() refuses to accept 'stderr' with 'capture_output'
1497 tf = tempfile.TemporaryFile()
1498 self.addCleanup(tf.close)
1499 with self.assertRaises(ValueError,
1500 msg=("Expected ValueError when stderr and capture_output "
1501 "args supplied.")) as c:
1502 output = self.run_python("print('will not be run')",
1503 capture_output=True, stderr=tf)
1504 self.assertIn('stderr', c.exception.args[0])
1505 self.assertIn('capture_output', c.exception.args[0])
1506
Gregory P. Smith6e730002015-04-14 16:14:25 -07001507
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001508@unittest.skipIf(mswindows, "POSIX specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +00001509class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001510
Gregory P. Smith5591b022012-10-10 03:34:47 -07001511 def setUp(self):
1512 super().setUp()
1513 self._nonexistent_dir = "/_this/pa.th/does/not/exist"
1514
1515 def _get_chdir_exception(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001516 try:
Gregory P. Smith5591b022012-10-10 03:34:47 -07001517 os.chdir(self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001518 except OSError as e:
1519 # This avoids hard coding the errno value or the OS perror()
1520 # string and instead capture the exception that we want to see
1521 # below for comparison.
1522 desired_exception = e
Gregory P. Smith5591b022012-10-10 03:34:47 -07001523 desired_exception.strerror += ': ' + repr(self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001524 else:
Martin Pantereb995702016-07-28 01:11:04 +00001525 self.fail("chdir to nonexistent directory %s succeeded." %
Gregory P. Smith5591b022012-10-10 03:34:47 -07001526 self._nonexistent_dir)
1527 return desired_exception
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001528
Gregory P. Smith5591b022012-10-10 03:34:47 -07001529 def test_exception_cwd(self):
1530 """Test error in the child raised in the parent for a bad cwd."""
1531 desired_exception = self._get_chdir_exception()
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001532 try:
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001533 p = subprocess.Popen([sys.executable, "-c", ""],
Gregory P. Smith5591b022012-10-10 03:34:47 -07001534 cwd=self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001535 except OSError as e:
1536 # Test that the child process chdir failure actually makes
1537 # it up to the parent process as the correct exception.
1538 self.assertEqual(desired_exception.errno, e.errno)
1539 self.assertEqual(desired_exception.strerror, e.strerror)
1540 else:
1541 self.fail("Expected OSError: %s" % desired_exception)
1542
Gregory P. Smith5591b022012-10-10 03:34:47 -07001543 def test_exception_bad_executable(self):
1544 """Test error in the child raised in the parent for a bad executable."""
1545 desired_exception = self._get_chdir_exception()
1546 try:
1547 p = subprocess.Popen([sys.executable, "-c", ""],
1548 executable=self._nonexistent_dir)
1549 except OSError as e:
1550 # Test that the child process exec failure actually makes
1551 # it up to the parent process as the correct exception.
1552 self.assertEqual(desired_exception.errno, e.errno)
1553 self.assertEqual(desired_exception.strerror, e.strerror)
1554 else:
1555 self.fail("Expected OSError: %s" % desired_exception)
1556
1557 def test_exception_bad_args_0(self):
1558 """Test error in the child raised in the parent for a bad args[0]."""
1559 desired_exception = self._get_chdir_exception()
1560 try:
1561 p = subprocess.Popen([self._nonexistent_dir, "-c", ""])
1562 except OSError as e:
1563 # Test that the child process exec failure actually makes
1564 # it up to the parent process as the correct exception.
1565 self.assertEqual(desired_exception.errno, e.errno)
1566 self.assertEqual(desired_exception.strerror, e.strerror)
1567 else:
1568 self.fail("Expected OSError: %s" % desired_exception)
1569
Ammar Askar3fc499b2017-09-06 02:41:30 -04001570 # We mock the __del__ method for Popen in the next two tests
1571 # because it does cleanup based on the pid returned by fork_exec
1572 # along with issuing a resource warning if it still exists. Since
1573 # we don't actually spawn a process in these tests we can forego
1574 # the destructor. An alternative would be to set _child_created to
1575 # False before the destructor is called but there is no easy way
1576 # to do that
1577 class PopenNoDestructor(subprocess.Popen):
1578 def __del__(self):
1579 pass
1580
1581 @mock.patch("subprocess._posixsubprocess.fork_exec")
1582 def test_exception_errpipe_normal(self, fork_exec):
1583 """Test error passing done through errpipe_write in the good case"""
1584 def proper_error(*args):
1585 errpipe_write = args[13]
1586 # Write the hex for the error code EISDIR: 'is a directory'
1587 err_code = '{:x}'.format(errno.EISDIR).encode()
1588 os.write(errpipe_write, b"OSError:" + err_code + b":")
1589 return 0
1590
1591 fork_exec.side_effect = proper_error
1592
Victor Stinner11045c92017-10-05 06:32:53 -07001593 with mock.patch("subprocess.os.waitpid",
1594 side_effect=ChildProcessError):
1595 with self.assertRaises(IsADirectoryError):
1596 self.PopenNoDestructor(["non_existent_command"])
Ammar Askar3fc499b2017-09-06 02:41:30 -04001597
1598 @mock.patch("subprocess._posixsubprocess.fork_exec")
1599 def test_exception_errpipe_bad_data(self, fork_exec):
1600 """Test error passing done through errpipe_write where its not
1601 in the expected format"""
1602 error_data = b"\xFF\x00\xDE\xAD"
1603 def bad_error(*args):
1604 errpipe_write = args[13]
1605 # Anything can be in the pipe, no assumptions should
1606 # be made about its encoding, so we'll write some
1607 # arbitrary hex bytes to test it out
1608 os.write(errpipe_write, error_data)
1609 return 0
1610
1611 fork_exec.side_effect = bad_error
1612
Victor Stinner11045c92017-10-05 06:32:53 -07001613 with mock.patch("subprocess.os.waitpid",
1614 side_effect=ChildProcessError):
1615 with self.assertRaises(subprocess.SubprocessError) as e:
1616 self.PopenNoDestructor(["non_existent_command"])
Ammar Askar3fc499b2017-09-06 02:41:30 -04001617
1618 self.assertIn(repr(error_data), str(e.exception))
1619
1620
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001621 def test_restore_signals(self):
1622 # Code coverage for both values of restore_signals to make sure it
1623 # at least does not blow up.
1624 # A test for behavior would be complex. Contributions welcome.
1625 subprocess.call([sys.executable, "-c", ""], restore_signals=True)
1626 subprocess.call([sys.executable, "-c", ""], restore_signals=False)
1627
1628 def test_start_new_session(self):
1629 # For code coverage of calling setsid(). We don't care if we get an
1630 # EPERM error from it depending on the test execution environment, that
1631 # still indicates that it was called.
1632 try:
1633 output = subprocess.check_output(
1634 [sys.executable, "-c",
1635 "import os; print(os.getpgid(os.getpid()))"],
1636 start_new_session=True)
1637 except OSError as e:
1638 if e.errno != errno.EPERM:
1639 raise
1640 else:
1641 parent_pgid = os.getpgid(os.getpid())
1642 child_pgid = int(output)
1643 self.assertNotEqual(parent_pgid, child_pgid)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001644
1645 def test_run_abort(self):
1646 # returncode handles signal termination
Antoine Pitrou77e904e2013-10-08 23:04:32 +02001647 with support.SuppressCrashReport():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001648 p = subprocess.Popen([sys.executable, "-c",
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001649 'import os; os.abort()'])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001650 p.wait()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001651 self.assertEqual(-p.returncode, signal.SIGABRT)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001652
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)d6da7602016-06-03 06:14:06 +00001653 def test_CalledProcessError_str_signal(self):
1654 err = subprocess.CalledProcessError(-int(signal.SIGABRT), "fake cmd")
1655 error_string = str(err)
1656 # We're relying on the repr() of the signal.Signals intenum to provide
1657 # the word signal, the signal name and the numeric value.
1658 self.assertIn("signal", error_string.lower())
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)b4149062016-06-03 06:19:35 +00001659 # We're not being specific about the signal name as some signals have
1660 # multiple names and which name is revealed can vary.
1661 self.assertIn("SIG", error_string)
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)d6da7602016-06-03 06:14:06 +00001662 self.assertIn(str(signal.SIGABRT), error_string)
1663
1664 def test_CalledProcessError_str_unknown_signal(self):
1665 err = subprocess.CalledProcessError(-9876543, "fake cmd")
1666 error_string = str(err)
1667 self.assertIn("unknown signal 9876543.", error_string)
1668
1669 def test_CalledProcessError_str_non_zero(self):
1670 err = subprocess.CalledProcessError(2, "fake cmd")
1671 error_string = str(err)
1672 self.assertIn("non-zero exit status 2.", error_string)
1673
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001674 def test_preexec(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001675 # DISCLAIMER: Setting environment variables is *not* a good use
1676 # of a preexec_fn. This is merely a test.
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001677 p = subprocess.Popen([sys.executable, "-c",
1678 'import sys,os;'
1679 'sys.stdout.write(os.getenv("FRUIT"))'],
1680 stdout=subprocess.PIPE,
1681 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Victor Stinner7438c612016-05-20 12:43:15 +02001682 with p:
1683 self.assertEqual(p.stdout.read(), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001684
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001685 def test_preexec_exception(self):
1686 def raise_it():
1687 raise ValueError("What if two swallows carried a coconut?")
1688 try:
1689 p = subprocess.Popen([sys.executable, "-c", ""],
1690 preexec_fn=raise_it)
Gregory P. Smith8d07c262012-11-10 23:53:47 -08001691 except subprocess.SubprocessError as e:
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001692 self.assertTrue(
1693 subprocess._posixsubprocess,
1694 "Expected a ValueError from the preexec_fn")
1695 except ValueError as e:
1696 self.assertIn("coconut", e.args[0])
1697 else:
1698 self.fail("Exception raised by preexec_fn did not make it "
1699 "to the parent process.")
1700
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001701 class _TestExecuteChildPopen(subprocess.Popen):
1702 """Used to test behavior at the end of _execute_child."""
1703 def __init__(self, testcase, *args, **kwargs):
1704 self._testcase = testcase
1705 subprocess.Popen.__init__(self, *args, **kwargs)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001706
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001707 def _execute_child(self, *args, **kwargs):
Gregory P. Smith12489d92012-11-11 01:37:02 -08001708 try:
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001709 subprocess.Popen._execute_child(self, *args, **kwargs)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001710 finally:
1711 # Open a bunch of file descriptors and verify that
1712 # none of them are the same as the ones the Popen
1713 # instance is using for stdin/stdout/stderr.
1714 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
1715 for _ in range(8)]
1716 try:
1717 for fd in devzero_fds:
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001718 self._testcase.assertNotIn(
1719 fd, (self.stdin.fileno(), self.stdout.fileno(),
1720 self.stderr.fileno()),
Gregory P. Smith12489d92012-11-11 01:37:02 -08001721 msg="At least one fd was closed early.")
1722 finally:
Richard Oudkerk0e547b62013-06-10 16:29:19 +01001723 for fd in devzero_fds:
1724 os.close(fd)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001725
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001726 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
1727 def test_preexec_errpipe_does_not_double_close_pipes(self):
1728 """Issue16140: Don't double close pipes on preexec error."""
1729
1730 def raise_it():
Gregory P. Smith65ee6ec2012-11-11 10:12:40 -08001731 raise subprocess.SubprocessError(
1732 "force the _execute_child() errpipe_data path.")
Gregory P. Smith12489d92012-11-11 01:37:02 -08001733
Gregory P. Smithc2c4cb62012-11-11 01:41:49 -08001734 with self.assertRaises(subprocess.SubprocessError):
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001735 self._TestExecuteChildPopen(
1736 self, [sys.executable, "-c", "pass"],
Gregory P. Smith12489d92012-11-11 01:37:02 -08001737 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1738 stderr=subprocess.PIPE, preexec_fn=raise_it)
1739
Gregory P. Smith32ec9da2010-03-19 16:53:08 +00001740 def test_preexec_gc_module_failure(self):
1741 # This tests the code that disables garbage collection if the child
1742 # process will execute any Python.
1743 def raise_runtime_error():
1744 raise RuntimeError("this shouldn't escape")
1745 enabled = gc.isenabled()
1746 orig_gc_disable = gc.disable
1747 orig_gc_isenabled = gc.isenabled
1748 try:
1749 gc.disable()
1750 self.assertFalse(gc.isenabled())
1751 subprocess.call([sys.executable, '-c', ''],
1752 preexec_fn=lambda: None)
1753 self.assertFalse(gc.isenabled(),
1754 "Popen enabled gc when it shouldn't.")
1755
1756 gc.enable()
1757 self.assertTrue(gc.isenabled())
1758 subprocess.call([sys.executable, '-c', ''],
1759 preexec_fn=lambda: None)
1760 self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
1761
1762 gc.disable = raise_runtime_error
1763 self.assertRaises(RuntimeError, subprocess.Popen,
1764 [sys.executable, '-c', ''],
1765 preexec_fn=lambda: None)
1766
1767 del gc.isenabled # force an AttributeError
1768 self.assertRaises(AttributeError, subprocess.Popen,
1769 [sys.executable, '-c', ''],
1770 preexec_fn=lambda: None)
1771 finally:
1772 gc.disable = orig_gc_disable
1773 gc.isenabled = orig_gc_isenabled
1774 if not enabled:
1775 gc.disable()
1776
Martin Panterf7fdbda2015-12-05 09:51:52 +00001777 @unittest.skipIf(
1778 sys.platform == 'darwin', 'setrlimit() seems to fail on OS X')
Martin Panterafdd5132015-11-30 02:21:41 +00001779 def test_preexec_fork_failure(self):
1780 # The internal code did not preserve the previous exception when
1781 # re-enabling garbage collection
1782 try:
1783 from resource import getrlimit, setrlimit, RLIMIT_NPROC
1784 except ImportError as err:
1785 self.skipTest(err) # RLIMIT_NPROC is specific to Linux and BSD
1786 limits = getrlimit(RLIMIT_NPROC)
1787 [_, hard] = limits
1788 setrlimit(RLIMIT_NPROC, (0, hard))
1789 self.addCleanup(setrlimit, RLIMIT_NPROC, limits)
Martin Panter5cf791b2015-12-11 05:40:14 +00001790 try:
Martin Panterafdd5132015-11-30 02:21:41 +00001791 subprocess.call([sys.executable, '-c', ''],
1792 preexec_fn=lambda: None)
Martin Panter5cf791b2015-12-11 05:40:14 +00001793 except BlockingIOError:
1794 # Forking should raise EAGAIN, translated to BlockingIOError
1795 pass
1796 else:
1797 self.skipTest('RLIMIT_NPROC had no effect; probably superuser')
Martin Panterafdd5132015-11-30 02:21:41 +00001798
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001799 def test_args_string(self):
1800 # args is a string
Berker Peksag16a1f282015-09-28 13:33:14 +03001801 fd, fname = tempfile.mkstemp()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001802 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +00001803 with open(fd, "w", errors="surrogateescape") as fobj:
Xavier de Gayed1415312016-07-22 12:15:29 +02001804 fobj.write("#!%s\n" % support.unix_shell)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001805 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
1806 sys.executable)
1807 os.chmod(fname, 0o700)
1808 p = subprocess.Popen(fname)
1809 p.wait()
1810 os.remove(fname)
1811 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001812
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001813 def test_invalid_args(self):
1814 # invalid arguments should raise ValueError
1815 self.assertRaises(ValueError, subprocess.call,
1816 [sys.executable, "-c",
1817 "import sys; sys.exit(47)"],
1818 startupinfo=47)
1819 self.assertRaises(ValueError, subprocess.call,
1820 [sys.executable, "-c",
1821 "import sys; sys.exit(47)"],
1822 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001823
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001824 def test_shell_sequence(self):
1825 # Run command through the shell (sequence)
1826 newenv = os.environ.copy()
1827 newenv["FRUIT"] = "apple"
1828 p = subprocess.Popen(["echo $FRUIT"], shell=1,
1829 stdout=subprocess.PIPE,
1830 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02001831 with p:
1832 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001833
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001834 def test_shell_string(self):
1835 # Run command through the shell (string)
1836 newenv = os.environ.copy()
1837 newenv["FRUIT"] = "apple"
1838 p = subprocess.Popen("echo $FRUIT", shell=1,
1839 stdout=subprocess.PIPE,
1840 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02001841 with p:
1842 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Christian Heimesa342c012008-04-20 21:01:16 +00001843
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001844 def test_call_string(self):
1845 # call() function with string argument on UNIX
Berker Peksag16a1f282015-09-28 13:33:14 +03001846 fd, fname = tempfile.mkstemp()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001847 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +00001848 with open(fd, "w", errors="surrogateescape") as fobj:
Xavier de Gayed1415312016-07-22 12:15:29 +02001849 fobj.write("#!%s\n" % support.unix_shell)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001850 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
1851 sys.executable)
1852 os.chmod(fname, 0o700)
1853 rc = subprocess.call(fname)
1854 os.remove(fname)
1855 self.assertEqual(rc, 47)
Christian Heimesa342c012008-04-20 21:01:16 +00001856
Stefan Krah9542cc62010-07-19 14:20:53 +00001857 def test_specific_shell(self):
1858 # Issue #9265: Incorrect name passed as arg[0].
1859 shells = []
1860 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
1861 for name in ['bash', 'ksh']:
1862 sh = os.path.join(prefix, name)
1863 if os.path.isfile(sh):
1864 shells.append(sh)
1865 if not shells: # Will probably work for any shell but csh.
1866 self.skipTest("bash or ksh required for this test")
1867 sh = '/bin/sh'
1868 if os.path.isfile(sh) and not os.path.islink(sh):
1869 # Test will fail if /bin/sh is a symlink to csh.
1870 shells.append(sh)
1871 for sh in shells:
1872 p = subprocess.Popen("echo $0", executable=sh, shell=True,
1873 stdout=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +02001874 with p:
1875 self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
Stefan Krah9542cc62010-07-19 14:20:53 +00001876
Florent Xicluna4886d242010-03-08 13:27:26 +00001877 def _kill_process(self, method, *args):
Florent Xicluna1d8ee3a2010-03-05 20:26:54 +00001878 # Do not inherit file handles from the parent.
1879 # It should fix failures on some platforms.
Gregory P. Smithdee04342013-08-29 13:35:27 -07001880 # Also set the SIGINT handler to the default to make sure it's not
1881 # being ignored (some tests rely on that.)
1882 old_handler = signal.signal(signal.SIGINT, signal.default_int_handler)
1883 try:
1884 p = subprocess.Popen([sys.executable, "-c", """if 1:
1885 import sys, time
1886 sys.stdout.write('x\\n')
1887 sys.stdout.flush()
1888 time.sleep(30)
1889 """],
1890 close_fds=True,
1891 stdin=subprocess.PIPE,
1892 stdout=subprocess.PIPE,
1893 stderr=subprocess.PIPE)
1894 finally:
1895 signal.signal(signal.SIGINT, old_handler)
Antoine Pitrou3d8580f2010-09-20 01:33:21 +00001896 # Wait for the interpreter to be completely initialized before
1897 # sending any signal.
1898 p.stdout.read(1)
1899 getattr(p, method)(*args)
Florent Xicluna4886d242010-03-08 13:27:26 +00001900 return p
1901
Charles-François Natali53221e32013-01-12 16:52:20 +01001902 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
1903 "Due to known OS bug (issue #16762)")
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001904 def _kill_dead_process(self, method, *args):
1905 # Do not inherit file handles from the parent.
1906 # It should fix failures on some platforms.
1907 p = subprocess.Popen([sys.executable, "-c", """if 1:
1908 import sys, time
1909 sys.stdout.write('x\\n')
1910 sys.stdout.flush()
1911 """],
1912 close_fds=True,
1913 stdin=subprocess.PIPE,
1914 stdout=subprocess.PIPE,
1915 stderr=subprocess.PIPE)
1916 # Wait for the interpreter to be completely initialized before
1917 # sending any signal.
1918 p.stdout.read(1)
1919 # The process should end after this
1920 time.sleep(1)
1921 # This shouldn't raise even though the child is now dead
1922 getattr(p, method)(*args)
1923 p.communicate()
1924
Florent Xicluna4886d242010-03-08 13:27:26 +00001925 def test_send_signal(self):
1926 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunac049d872010-03-27 22:47:23 +00001927 _, stderr = p.communicate()
1928 self.assertIn(b'KeyboardInterrupt', stderr)
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001929 self.assertNotEqual(p.wait(), 0)
Christian Heimesa342c012008-04-20 21:01:16 +00001930
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001931 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001932 p = self._kill_process('kill')
Florent Xiclunac049d872010-03-27 22:47:23 +00001933 _, stderr = p.communicate()
1934 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001935 self.assertEqual(p.wait(), -signal.SIGKILL)
Tim Peterse718f612004-10-12 21:51:32 +00001936
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001937 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001938 p = self._kill_process('terminate')
Florent Xiclunac049d872010-03-27 22:47:23 +00001939 _, stderr = p.communicate()
1940 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001941 self.assertEqual(p.wait(), -signal.SIGTERM)
1942
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001943 def test_send_signal_dead(self):
1944 # Sending a signal to a dead process
1945 self._kill_dead_process('send_signal', signal.SIGINT)
1946
1947 def test_kill_dead(self):
1948 # Killing a dead process
1949 self._kill_dead_process('kill')
1950
1951 def test_terminate_dead(self):
1952 # Terminating a dead process
1953 self._kill_dead_process('terminate')
1954
Victor Stinnerdaf45552013-08-28 00:53:59 +02001955 def _save_fds(self, save_fds):
1956 fds = []
1957 for fd in save_fds:
1958 inheritable = os.get_inheritable(fd)
1959 saved = os.dup(fd)
1960 fds.append((fd, saved, inheritable))
1961 return fds
1962
1963 def _restore_fds(self, fds):
1964 for fd, saved, inheritable in fds:
1965 os.dup2(saved, fd, inheritable=inheritable)
1966 os.close(saved)
1967
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001968 def check_close_std_fds(self, fds):
1969 # Issue #9905: test that subprocess pipes still work properly with
1970 # some standard fds closed
1971 stdin = 0
Victor Stinnerdaf45552013-08-28 00:53:59 +02001972 saved_fds = self._save_fds(fds)
1973 for fd, saved, inheritable in saved_fds:
1974 if fd == 0:
1975 stdin = saved
1976 break
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001977 try:
1978 for fd in fds:
1979 os.close(fd)
1980 out, err = subprocess.Popen([sys.executable, "-c",
1981 'import sys;'
1982 'sys.stdout.write("apple");'
1983 'sys.stdout.flush();'
1984 'sys.stderr.write("orange")'],
1985 stdin=stdin,
1986 stdout=subprocess.PIPE,
1987 stderr=subprocess.PIPE).communicate()
1988 err = support.strip_python_stderr(err)
1989 self.assertEqual((out, err), (b'apple', b'orange'))
1990 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02001991 self._restore_fds(saved_fds)
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001992
1993 def test_close_fd_0(self):
1994 self.check_close_std_fds([0])
1995
1996 def test_close_fd_1(self):
1997 self.check_close_std_fds([1])
1998
1999 def test_close_fd_2(self):
2000 self.check_close_std_fds([2])
2001
2002 def test_close_fds_0_1(self):
2003 self.check_close_std_fds([0, 1])
2004
2005 def test_close_fds_0_2(self):
2006 self.check_close_std_fds([0, 2])
2007
2008 def test_close_fds_1_2(self):
2009 self.check_close_std_fds([1, 2])
2010
2011 def test_close_fds_0_1_2(self):
2012 # Issue #10806: test that subprocess pipes still work properly with
2013 # all standard fds closed.
2014 self.check_close_std_fds([0, 1, 2])
2015
Gregory P. Smith53dd8162013-12-01 16:03:24 -08002016 def test_small_errpipe_write_fd(self):
2017 """Issue #15798: Popen should work when stdio fds are available."""
2018 new_stdin = os.dup(0)
2019 new_stdout = os.dup(1)
2020 try:
2021 os.close(0)
2022 os.close(1)
2023
2024 # Side test: if errpipe_write fails to have its CLOEXEC
2025 # flag set this should cause the parent to think the exec
2026 # failed. Extremely unlikely: everyone supports CLOEXEC.
2027 subprocess.Popen([
2028 sys.executable, "-c",
2029 "print('AssertionError:0:CLOEXEC failure.')"]).wait()
2030 finally:
2031 # Restore original stdin and stdout
2032 os.dup2(new_stdin, 0)
2033 os.dup2(new_stdout, 1)
2034 os.close(new_stdin)
2035 os.close(new_stdout)
2036
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002037 def test_remapping_std_fds(self):
2038 # open up some temporary files
Berker Peksag16a1f282015-09-28 13:33:14 +03002039 temps = [tempfile.mkstemp() for i in range(3)]
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002040 try:
2041 temp_fds = [fd for fd, fname in temps]
2042
2043 # unlink the files -- we won't need to reopen them
2044 for fd, fname in temps:
2045 os.unlink(fname)
2046
2047 # write some data to what will become stdin, and rewind
2048 os.write(temp_fds[1], b"STDIN")
2049 os.lseek(temp_fds[1], 0, 0)
2050
2051 # move the standard file descriptors out of the way
Victor Stinnerdaf45552013-08-28 00:53:59 +02002052 saved_fds = self._save_fds(range(3))
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002053 try:
2054 # duplicate the file objects over the standard fd's
2055 for fd, temp_fd in enumerate(temp_fds):
2056 os.dup2(temp_fd, fd)
2057
2058 # now use those files in the "wrong" order, so that subprocess
2059 # has to rearrange them in the child
2060 p = subprocess.Popen([sys.executable, "-c",
2061 'import sys; got = sys.stdin.read();'
2062 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
2063 stdin=temp_fds[1],
2064 stdout=temp_fds[2],
2065 stderr=temp_fds[0])
2066 p.wait()
2067 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02002068 self._restore_fds(saved_fds)
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00002069
2070 for fd in temp_fds:
2071 os.lseek(fd, 0, 0)
2072
2073 out = os.read(temp_fds[2], 1024)
2074 err = support.strip_python_stderr(os.read(temp_fds[0], 1024))
2075 self.assertEqual(out, b"got STDIN")
2076 self.assertEqual(err, b"err")
2077
2078 finally:
2079 for fd in temp_fds:
2080 os.close(fd)
2081
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002082 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
2083 # open up some temporary files
Berker Peksag16a1f282015-09-28 13:33:14 +03002084 temps = [tempfile.mkstemp() for i in range(3)]
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002085 temp_fds = [fd for fd, fname in temps]
2086 try:
2087 # unlink the files -- we won't need to reopen them
2088 for fd, fname in temps:
2089 os.unlink(fname)
2090
2091 # save a copy of the standard file descriptors
Victor Stinnerdaf45552013-08-28 00:53:59 +02002092 saved_fds = self._save_fds(range(3))
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002093 try:
2094 # duplicate the temp files over the standard fd's 0, 1, 2
2095 for fd, temp_fd in enumerate(temp_fds):
2096 os.dup2(temp_fd, fd)
2097
2098 # write some data to what will become stdin, and rewind
2099 os.write(stdin_no, b"STDIN")
2100 os.lseek(stdin_no, 0, 0)
2101
2102 # now use those files in the given order, so that subprocess
2103 # has to rearrange them in the child
2104 p = subprocess.Popen([sys.executable, "-c",
2105 'import sys; got = sys.stdin.read();'
2106 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
2107 stdin=stdin_no,
2108 stdout=stdout_no,
2109 stderr=stderr_no)
2110 p.wait()
2111
2112 for fd in temp_fds:
2113 os.lseek(fd, 0, 0)
2114
2115 out = os.read(stdout_no, 1024)
2116 err = support.strip_python_stderr(os.read(stderr_no, 1024))
2117 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02002118 self._restore_fds(saved_fds)
Ross Lagerwalld98646e2011-07-27 07:16:31 +02002119
2120 self.assertEqual(out, b"got STDIN")
2121 self.assertEqual(err, b"err")
2122
2123 finally:
2124 for fd in temp_fds:
2125 os.close(fd)
2126
2127 # When duping fds, if there arises a situation where one of the fds is
2128 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
2129 # This tests all combinations of this.
2130 def test_swap_fds(self):
2131 self.check_swap_fds(0, 1, 2)
2132 self.check_swap_fds(0, 2, 1)
2133 self.check_swap_fds(1, 0, 2)
2134 self.check_swap_fds(1, 2, 0)
2135 self.check_swap_fds(2, 0, 1)
2136 self.check_swap_fds(2, 1, 0)
2137
Miss Islington (bot)05455632018-03-26 13:14:09 -07002138 def _check_swap_std_fds_with_one_closed(self, from_fds, to_fds):
2139 saved_fds = self._save_fds(range(3))
2140 try:
2141 for from_fd in from_fds:
2142 with tempfile.TemporaryFile() as f:
2143 os.dup2(f.fileno(), from_fd)
2144
2145 fd_to_close = (set(range(3)) - set(from_fds)).pop()
2146 os.close(fd_to_close)
2147
2148 arg_names = ['stdin', 'stdout', 'stderr']
2149 kwargs = {}
2150 for from_fd, to_fd in zip(from_fds, to_fds):
2151 kwargs[arg_names[to_fd]] = from_fd
2152
2153 code = textwrap.dedent(r'''
2154 import os, sys
2155 skipped_fd = int(sys.argv[1])
2156 for fd in range(3):
2157 if fd != skipped_fd:
2158 os.write(fd, str(fd).encode('ascii'))
2159 ''')
2160
2161 skipped_fd = (set(range(3)) - set(to_fds)).pop()
2162
2163 rc = subprocess.call([sys.executable, '-c', code, str(skipped_fd)],
2164 **kwargs)
2165 self.assertEqual(rc, 0)
2166
2167 for from_fd, to_fd in zip(from_fds, to_fds):
2168 os.lseek(from_fd, 0, os.SEEK_SET)
2169 read_bytes = os.read(from_fd, 1024)
2170 read_fds = list(map(int, read_bytes.decode('ascii')))
2171 msg = textwrap.dedent(f"""
2172 When testing {from_fds} to {to_fds} redirection,
2173 parent descriptor {from_fd} got redirected
2174 to descriptor(s) {read_fds} instead of descriptor {to_fd}.
2175 """)
2176 self.assertEqual([to_fd], read_fds, msg)
2177 finally:
2178 self._restore_fds(saved_fds)
2179
2180 # Check that subprocess can remap std fds correctly even
2181 # if one of them is closed (#32844).
2182 def test_swap_std_fds_with_one_closed(self):
2183 for from_fds in itertools.combinations(range(3), 2):
2184 for to_fds in itertools.permutations(range(3), 2):
2185 self._check_swap_std_fds_with_one_closed(from_fds, to_fds)
2186
Victor Stinner13bb71c2010-04-23 21:41:56 +00002187 def test_surrogates_error_message(self):
Victor Stinner4d078042010-04-23 19:28:32 +00002188 def prepare():
2189 raise ValueError("surrogate:\uDCff")
2190
2191 try:
2192 subprocess.call(
2193 [sys.executable, "-c", "pass"],
2194 preexec_fn=prepare)
2195 except ValueError as err:
2196 # Pure Python implementations keeps the message
2197 self.assertIsNone(subprocess._posixsubprocess)
2198 self.assertEqual(str(err), "surrogate:\uDCff")
Gregory P. Smith8d07c262012-11-10 23:53:47 -08002199 except subprocess.SubprocessError as err:
Victor Stinner4d078042010-04-23 19:28:32 +00002200 # _posixsubprocess uses a default message
2201 self.assertIsNotNone(subprocess._posixsubprocess)
2202 self.assertEqual(str(err), "Exception occurred in preexec_fn.")
2203 else:
Gregory P. Smith8d07c262012-11-10 23:53:47 -08002204 self.fail("Expected ValueError or subprocess.SubprocessError")
Victor Stinner4d078042010-04-23 19:28:32 +00002205
Victor Stinner13bb71c2010-04-23 21:41:56 +00002206 def test_undecodable_env(self):
2207 for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')):
Victor Stinner5323fb02013-11-19 23:46:06 +01002208 encoded_value = value.encode("ascii", "surrogateescape")
2209
Victor Stinner13bb71c2010-04-23 21:41:56 +00002210 # test str with surrogates
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00002211 script = "import os; print(ascii(os.getenv(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002212 env = os.environ.copy()
2213 env[key] = value
Victor Stinner5323fb02013-11-19 23:46:06 +01002214 # Use C locale to get ASCII for the locale encoding to force
Victor Stinner89f3ad12010-10-14 10:43:31 +00002215 # surrogate-escaping of \xFF in the child process; otherwise it can
2216 # be decoded as-is if the default locale is latin-1.
Victor Stinnerebc78d22010-10-14 10:38:17 +00002217 env['LC_ALL'] = 'C'
Victor Stinner5323fb02013-11-19 23:46:06 +01002218 if sys.platform.startswith("aix"):
2219 # On AIX, the C locale uses the Latin1 encoding
2220 decoded_value = encoded_value.decode("latin1", "surrogateescape")
2221 else:
2222 # On other UNIXes, the C locale uses the ASCII encoding
2223 decoded_value = value
Victor Stinner13bb71c2010-04-23 21:41:56 +00002224 stdout = subprocess.check_output(
2225 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002226 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00002227 stdout = stdout.rstrip(b'\n\r')
Victor Stinner5323fb02013-11-19 23:46:06 +01002228 self.assertEqual(stdout.decode('ascii'), ascii(decoded_value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00002229
2230 # test bytes
2231 key = key.encode("ascii", "surrogateescape")
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00002232 script = "import os; print(ascii(os.getenvb(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002233 env = os.environ.copy()
Victor Stinner5323fb02013-11-19 23:46:06 +01002234 env[key] = encoded_value
Victor Stinner13bb71c2010-04-23 21:41:56 +00002235 stdout = subprocess.check_output(
2236 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00002237 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00002238 stdout = stdout.rstrip(b'\n\r')
Victor Stinner5323fb02013-11-19 23:46:06 +01002239 self.assertEqual(stdout.decode('ascii'), ascii(encoded_value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00002240
Victor Stinnerb745a742010-05-18 17:17:23 +00002241 def test_bytes_program(self):
2242 abs_program = os.fsencode(sys.executable)
2243 path, program = os.path.split(sys.executable)
2244 program = os.fsencode(program)
2245
2246 # absolute bytes path
2247 exitcode = subprocess.call([abs_program, "-c", "pass"])
Ezio Melottib3aedd42010-11-20 19:04:17 +00002248 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00002249
Victor Stinner7b3b20a2011-03-03 12:54:05 +00002250 # absolute bytes path as a string
2251 cmd = b"'" + abs_program + b"' -c pass"
2252 exitcode = subprocess.call(cmd, shell=True)
2253 self.assertEqual(exitcode, 0)
2254
Victor Stinnerb745a742010-05-18 17:17:23 +00002255 # bytes program, unicode PATH
2256 env = os.environ.copy()
2257 env["PATH"] = path
2258 exitcode = subprocess.call([program, "-c", "pass"], env=env)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002259 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00002260
2261 # bytes program, bytes PATH
2262 envb = os.environb.copy()
2263 envb[b"PATH"] = os.fsencode(path)
2264 exitcode = subprocess.call([program, "-c", "pass"], env=envb)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002265 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00002266
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002267 def test_pipe_cloexec(self):
2268 sleeper = support.findfile("input_reader.py", subdir="subprocessdata")
2269 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2270
2271 p1 = subprocess.Popen([sys.executable, sleeper],
2272 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2273 stderr=subprocess.PIPE, close_fds=False)
2274
2275 self.addCleanup(p1.communicate, b'')
2276
2277 p2 = subprocess.Popen([sys.executable, fd_status],
2278 stdout=subprocess.PIPE, close_fds=False)
2279
2280 output, error = p2.communicate()
2281 result_fds = set(map(int, output.split(b',')))
2282 unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(),
2283 p1.stderr.fileno()])
2284
2285 self.assertFalse(result_fds & unwanted_fds,
2286 "Expected no fds from %r to be open in child, "
2287 "found %r" %
2288 (unwanted_fds, result_fds & unwanted_fds))
2289
2290 def test_pipe_cloexec_real_tools(self):
2291 qcat = support.findfile("qcat.py", subdir="subprocessdata")
2292 qgrep = support.findfile("qgrep.py", subdir="subprocessdata")
2293
2294 subdata = b'zxcvbn'
2295 data = subdata * 4 + b'\n'
2296
2297 p1 = subprocess.Popen([sys.executable, qcat],
2298 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2299 close_fds=False)
2300
2301 p2 = subprocess.Popen([sys.executable, qgrep, subdata],
2302 stdin=p1.stdout, stdout=subprocess.PIPE,
2303 close_fds=False)
2304
2305 self.addCleanup(p1.wait)
2306 self.addCleanup(p2.wait)
Gregory P. Smith886455c2012-01-21 22:05:10 -08002307 def kill_p1():
2308 try:
2309 p1.terminate()
2310 except ProcessLookupError:
2311 pass
2312 def kill_p2():
2313 try:
2314 p2.terminate()
2315 except ProcessLookupError:
2316 pass
2317 self.addCleanup(kill_p1)
2318 self.addCleanup(kill_p2)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002319
2320 p1.stdin.write(data)
2321 p1.stdin.close()
2322
2323 readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10)
2324
2325 self.assertTrue(readfiles, "The child hung")
2326 self.assertEqual(p2.stdout.read(), data)
2327
Victor Stinnerfaa8c132011-01-03 16:36:00 +00002328 p1.stdout.close()
2329 p2.stdout.close()
2330
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002331 def test_close_fds(self):
2332 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2333
2334 fds = os.pipe()
2335 self.addCleanup(os.close, fds[0])
2336 self.addCleanup(os.close, fds[1])
2337
2338 open_fds = set(fds)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002339 # add a bunch more fds
2340 for _ in range(9):
Serhiy Storchaka85c30332015-02-15 13:58:23 +02002341 fd = os.open(os.devnull, os.O_RDONLY)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002342 self.addCleanup(os.close, fd)
2343 open_fds.add(fd)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002344
Victor Stinnerdaf45552013-08-28 00:53:59 +02002345 for fd in open_fds:
2346 os.set_inheritable(fd, True)
2347
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002348 p = subprocess.Popen([sys.executable, fd_status],
2349 stdout=subprocess.PIPE, close_fds=False)
2350 output, ignored = p.communicate()
2351 remaining_fds = set(map(int, output.split(b',')))
2352
2353 self.assertEqual(remaining_fds & open_fds, open_fds,
2354 "Some fds were closed")
2355
2356 p = subprocess.Popen([sys.executable, fd_status],
2357 stdout=subprocess.PIPE, close_fds=True)
2358 output, ignored = p.communicate()
2359 remaining_fds = set(map(int, output.split(b',')))
2360
2361 self.assertFalse(remaining_fds & open_fds,
2362 "Some fds were left open")
2363 self.assertIn(1, remaining_fds, "Subprocess failed")
2364
Gregory P. Smith8facece2012-01-21 14:01:08 -08002365 # Keep some of the fd's we opened open in the subprocess.
2366 # This tests _posixsubprocess.c's proper handling of fds_to_keep.
2367 fds_to_keep = set(open_fds.pop() for _ in range(8))
2368 p = subprocess.Popen([sys.executable, fd_status],
2369 stdout=subprocess.PIPE, close_fds=True,
izbyshev2d8f0632017-12-19 03:26:49 +07002370 pass_fds=fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002371 output, ignored = p.communicate()
2372 remaining_fds = set(map(int, output.split(b',')))
2373
izbyshev2d8f0632017-12-19 03:26:49 +07002374 self.assertFalse((remaining_fds - fds_to_keep) & open_fds,
Gregory P. Smith8facece2012-01-21 14:01:08 -08002375 "Some fds not in pass_fds were left open")
2376 self.assertIn(1, remaining_fds, "Subprocess failed")
2377
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002378
Gregory P. Smithd04f6992014-06-01 15:27:28 -07002379 @unittest.skipIf(sys.platform.startswith("freebsd") and
2380 os.stat("/dev").st_dev == os.stat("/dev/fd").st_dev,
2381 "Requires fdescfs mounted on /dev/fd on FreeBSD.")
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002382 def test_close_fds_when_max_fd_is_lowered(self):
2383 """Confirm that issue21618 is fixed (may fail under valgrind)."""
2384 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2385
Gregory P. Smith634aa682014-06-15 17:51:04 -07002386 # This launches the meat of the test in a child process to
2387 # avoid messing with the larger unittest processes maximum
2388 # number of file descriptors.
2389 # This process launches:
2390 # +--> Process that lowers its RLIMIT_NOFILE aftr setting up
2391 # a bunch of high open fds above the new lower rlimit.
2392 # Those are reported via stdout before launching a new
2393 # process with close_fds=False to run the actual test:
2394 # +--> The TEST: This one launches a fd_status.py
2395 # subprocess with close_fds=True so we can find out if
2396 # any of the fds above the lowered rlimit are still open.
2397 p = subprocess.Popen([sys.executable, '-c', textwrap.dedent(
2398 '''
2399 import os, resource, subprocess, sys, textwrap
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002400 open_fds = set()
2401 # Add a bunch more fds to pass down.
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002402 for _ in range(40):
Serhiy Storchaka85c30332015-02-15 13:58:23 +02002403 fd = os.open(os.devnull, os.O_RDONLY)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002404 open_fds.add(fd)
2405
2406 # Leave a two pairs of low ones available for use by the
2407 # internal child error pipe and the stdout pipe.
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002408 # We also leave 10 more open as some Python buildbots run into
2409 # "too many open files" errors during the test if we do not.
2410 for fd in sorted(open_fds)[:14]:
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002411 os.close(fd)
2412 open_fds.remove(fd)
2413
2414 for fd in open_fds:
Gregory P. Smith634aa682014-06-15 17:51:04 -07002415 #self.addCleanup(os.close, fd)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002416 os.set_inheritable(fd, True)
2417
2418 max_fd_open = max(open_fds)
2419
Gregory P. Smith634aa682014-06-15 17:51:04 -07002420 # Communicate the open_fds to the parent unittest.TestCase process.
2421 print(','.join(map(str, sorted(open_fds))))
2422 sys.stdout.flush()
2423
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002424 rlim_cur, rlim_max = resource.getrlimit(resource.RLIMIT_NOFILE)
2425 try:
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002426 # 29 is lower than the highest fds we are leaving open.
2427 resource.setrlimit(resource.RLIMIT_NOFILE, (29, rlim_max))
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002428 # Launch a new Python interpreter with our low fd rlim_cur that
2429 # inherits open fds above that limit. It then uses subprocess
2430 # with close_fds=True to get a report of open fds in the child.
2431 # An explicit list of fds to check is passed to fd_status.py as
2432 # letting fd_status rely on its default logic would miss the
2433 # fds above rlim_cur as it normally only checks up to that limit.
Gregory P. Smith634aa682014-06-15 17:51:04 -07002434 subprocess.Popen(
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002435 [sys.executable, '-c',
2436 textwrap.dedent("""
2437 import subprocess, sys
Gregory P. Smith634aa682014-06-15 17:51:04 -07002438 subprocess.Popen([sys.executable, %r] +
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002439 [str(x) for x in range({max_fd})],
Gregory P. Smithffd529c2014-06-01 13:46:54 -07002440 close_fds=True).wait()
Gregory P. Smith634aa682014-06-15 17:51:04 -07002441 """.format(max_fd=max_fd_open+1))],
2442 close_fds=False).wait()
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002443 finally:
2444 resource.setrlimit(resource.RLIMIT_NOFILE, (rlim_cur, rlim_max))
Gregory P. Smith634aa682014-06-15 17:51:04 -07002445 ''' % fd_status)], stdout=subprocess.PIPE)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002446
2447 output, unused_stderr = p.communicate()
Gregory P. Smith634aa682014-06-15 17:51:04 -07002448 output_lines = output.splitlines()
2449 self.assertEqual(len(output_lines), 2,
Gregory P. Smith9204e092014-06-15 20:16:01 -07002450 msg="expected exactly two lines of output:\n%r" % output)
Gregory P. Smith634aa682014-06-15 17:51:04 -07002451 opened_fds = set(map(int, output_lines[0].strip().split(b',')))
2452 remaining_fds = set(map(int, output_lines[1].strip().split(b',')))
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002453
Gregory P. Smith634aa682014-06-15 17:51:04 -07002454 self.assertFalse(remaining_fds & opened_fds,
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002455 msg="Some fds were left open.")
2456
2457
Victor Stinner88701e22011-06-01 13:13:04 +02002458 # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
2459 # descriptor of a pipe closed in the parent process is valid in the
2460 # child process according to fstat(), but the mode of the file
2461 # descriptor is invalid, and read or write raise an error.
2462 @support.requires_mac_ver(10, 5)
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00002463 def test_pass_fds(self):
2464 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2465
2466 open_fds = set()
2467
2468 for x in range(5):
2469 fds = os.pipe()
2470 self.addCleanup(os.close, fds[0])
2471 self.addCleanup(os.close, fds[1])
Victor Stinnerdaf45552013-08-28 00:53:59 +02002472 os.set_inheritable(fds[0], True)
2473 os.set_inheritable(fds[1], True)
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00002474 open_fds.update(fds)
2475
2476 for fd in open_fds:
2477 p = subprocess.Popen([sys.executable, fd_status],
2478 stdout=subprocess.PIPE, close_fds=True,
2479 pass_fds=(fd, ))
2480 output, ignored = p.communicate()
2481
2482 remaining_fds = set(map(int, output.split(b',')))
2483 to_be_closed = open_fds - {fd}
2484
2485 self.assertIn(fd, remaining_fds, "fd to be passed not passed")
2486 self.assertFalse(remaining_fds & to_be_closed,
2487 "fd to be closed passed")
2488
2489 # pass_fds overrides close_fds with a warning.
2490 with self.assertWarns(RuntimeWarning) as context:
2491 self.assertFalse(subprocess.call(
2492 [sys.executable, "-c", "import sys; sys.exit(0)"],
2493 close_fds=False, pass_fds=(fd, )))
2494 self.assertIn('overriding close_fds', str(context.warning))
2495
Victor Stinnerdaf45552013-08-28 00:53:59 +02002496 def test_pass_fds_inheritable(self):
Victor Stinnerf6fa22e2013-09-01 10:22:41 +02002497 script = support.findfile("fd_status.py", subdir="subprocessdata")
Victor Stinnerdaf45552013-08-28 00:53:59 +02002498
2499 inheritable, non_inheritable = os.pipe()
2500 self.addCleanup(os.close, inheritable)
2501 self.addCleanup(os.close, non_inheritable)
2502 os.set_inheritable(inheritable, True)
2503 os.set_inheritable(non_inheritable, False)
2504 pass_fds = (inheritable, non_inheritable)
2505 args = [sys.executable, script]
2506 args += list(map(str, pass_fds))
2507
2508 p = subprocess.Popen(args,
2509 stdout=subprocess.PIPE, close_fds=True,
2510 pass_fds=pass_fds)
2511 output, ignored = p.communicate()
2512 fds = set(map(int, output.split(b',')))
2513
2514 # the inheritable file descriptor must be inherited, so its inheritable
2515 # flag must be set in the child process after fork() and before exec()
Victor Stinnerf6fa22e2013-09-01 10:22:41 +02002516 self.assertEqual(fds, set(pass_fds), "output=%a" % output)
Victor Stinnerdaf45552013-08-28 00:53:59 +02002517
2518 # inheritable flag must not be changed in the parent process
2519 self.assertEqual(os.get_inheritable(inheritable), True)
2520 self.assertEqual(os.get_inheritable(non_inheritable), False)
2521
Gregory P. Smith112bb3a2011-03-15 14:55:17 -04002522 def test_stdout_stdin_are_single_inout_fd(self):
2523 with io.open(os.devnull, "r+") as inout:
2524 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2525 stdout=inout, stdin=inout)
2526 p.wait()
2527
2528 def test_stdout_stderr_are_single_inout_fd(self):
2529 with io.open(os.devnull, "r+") as inout:
2530 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2531 stdout=inout, stderr=inout)
2532 p.wait()
2533
2534 def test_stderr_stdin_are_single_inout_fd(self):
2535 with io.open(os.devnull, "r+") as inout:
2536 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2537 stderr=inout, stdin=inout)
2538 p.wait()
2539
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00002540 def test_wait_when_sigchild_ignored(self):
2541 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
2542 sigchild_ignore = support.findfile("sigchild_ignore.py",
2543 subdir="subprocessdata")
2544 p = subprocess.Popen([sys.executable, sigchild_ignore],
2545 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2546 stdout, stderr = p.communicate()
2547 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
Gregory P. Smitha80f4fb2010-12-14 15:23:02 +00002548 " non-zero with this error:\n%s" %
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00002549 stderr.decode('utf-8'))
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00002550
Antoine Pitrou7b98d022011-03-19 17:04:13 +01002551 def test_select_unbuffered(self):
2552 # Issue #11459: bufsize=0 should really set the pipes as
2553 # unbuffered (and therefore let select() work properly).
2554 select = support.import_module("select")
2555 p = subprocess.Popen([sys.executable, "-c",
2556 'import sys;'
2557 'sys.stdout.write("apple")'],
2558 stdout=subprocess.PIPE,
2559 bufsize=0)
2560 f = p.stdout
Ross Lagerwall17ace7a2011-03-26 21:21:46 +02002561 self.addCleanup(f.close)
Antoine Pitrou7b98d022011-03-19 17:04:13 +01002562 try:
2563 self.assertEqual(f.read(4), b"appl")
2564 self.assertIn(f, select.select([f], [], [], 0.0)[0])
2565 finally:
2566 p.wait()
2567
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002568 def test_zombie_fast_process_del(self):
2569 # Issue #12650: on Unix, if Popen.__del__() was called before the
2570 # process exited, it wouldn't be added to subprocess._active, and would
2571 # remain a zombie.
2572 # spawn a Popen, and delete its reference before it exits
2573 p = subprocess.Popen([sys.executable, "-c",
2574 'import sys, time;'
2575 'time.sleep(0.2)'],
2576 stdout=subprocess.PIPE,
2577 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02002578 self.addCleanup(p.stdout.close)
2579 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002580 ident = id(p)
2581 pid = p.pid
Victor Stinner5a48e212016-05-20 12:11:15 +02002582 with support.check_warnings(('', ResourceWarning)):
2583 p = None
2584
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002585 # check that p is in the active processes list
2586 self.assertIn(ident, [id(o) for o in subprocess._active])
2587
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002588 def test_leak_fast_process_del_killed(self):
2589 # Issue #12650: on Unix, if Popen.__del__() was called before the
2590 # process exited, and the process got killed by a signal, it would never
2591 # be removed from subprocess._active, which triggered a FD and memory
2592 # leak.
2593 # spawn a Popen, delete its reference and kill it
2594 p = subprocess.Popen([sys.executable, "-c",
2595 'import time;'
2596 'time.sleep(3)'],
2597 stdout=subprocess.PIPE,
2598 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02002599 self.addCleanup(p.stdout.close)
2600 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002601 ident = id(p)
2602 pid = p.pid
Victor Stinner5a48e212016-05-20 12:11:15 +02002603 with support.check_warnings(('', ResourceWarning)):
2604 p = None
2605
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002606 os.kill(pid, signal.SIGKILL)
2607 # check that p is in the active processes list
2608 self.assertIn(ident, [id(o) for o in subprocess._active])
2609
2610 # let some time for the process to exit, and create a new Popen: this
2611 # should trigger the wait() of p
2612 time.sleep(0.2)
Victor Stinnerb31206a2018-01-25 19:06:05 +01002613 with self.assertRaises(OSError):
Victor Stinner9a83f652017-08-21 23:51:31 +02002614 with subprocess.Popen(NONEXISTING_CMD,
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002615 stdout=subprocess.PIPE,
2616 stderr=subprocess.PIPE) as proc:
2617 pass
2618 # p should have been wait()ed on, and removed from the _active list
2619 self.assertRaises(OSError, os.waitpid, pid, 0)
2620 self.assertNotIn(ident, [id(o) for o in subprocess._active])
2621
Charles-François Natali249cdc32013-08-25 18:24:45 +02002622 def test_close_fds_after_preexec(self):
2623 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2624
2625 # this FD is used as dup2() target by preexec_fn, and should be closed
2626 # in the child process
2627 fd = os.dup(1)
2628 self.addCleanup(os.close, fd)
2629
2630 p = subprocess.Popen([sys.executable, fd_status],
2631 stdout=subprocess.PIPE, close_fds=True,
2632 preexec_fn=lambda: os.dup2(1, fd))
2633 output, ignored = p.communicate()
2634
2635 remaining_fds = set(map(int, output.split(b',')))
2636
2637 self.assertNotIn(fd, remaining_fds)
2638
Victor Stinner8f437aa2014-10-05 17:25:19 +02002639 @support.cpython_only
2640 def test_fork_exec(self):
2641 # Issue #22290: fork_exec() must not crash on memory allocation failure
2642 # or other errors
2643 import _posixsubprocess
2644 gc_enabled = gc.isenabled()
2645 try:
2646 # Use a preexec function and enable the garbage collector
2647 # to force fork_exec() to re-enable the garbage collector
2648 # on error.
2649 func = lambda: None
2650 gc.enable()
2651
Victor Stinner8f437aa2014-10-05 17:25:19 +02002652 for args, exe_list, cwd, env_list in (
2653 (123, [b"exe"], None, [b"env"]),
2654 ([b"arg"], 123, None, [b"env"]),
2655 ([b"arg"], [b"exe"], 123, [b"env"]),
2656 ([b"arg"], [b"exe"], None, 123),
2657 ):
2658 with self.assertRaises(TypeError):
2659 _posixsubprocess.fork_exec(
2660 args, exe_list,
Serhiy Storchaka66bffd12017-04-19 21:12:46 +03002661 True, (), cwd, env_list,
Victor Stinner8f437aa2014-10-05 17:25:19 +02002662 -1, -1, -1, -1,
2663 1, 2, 3, 4,
2664 True, True, func)
2665 finally:
2666 if not gc_enabled:
2667 gc.disable()
2668
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002669 @support.cpython_only
2670 def test_fork_exec_sorted_fd_sanity_check(self):
2671 # Issue #23564: sanity check the fork_exec() fds_to_keep sanity check.
2672 import _posixsubprocess
Serhiy Storchaka66bffd12017-04-19 21:12:46 +03002673 class BadInt:
2674 first = True
2675 def __init__(self, value):
2676 self.value = value
2677 def __int__(self):
2678 if self.first:
2679 self.first = False
2680 return self.value
2681 raise ValueError
2682
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002683 gc_enabled = gc.isenabled()
2684 try:
2685 gc.enable()
2686
2687 for fds_to_keep in (
2688 (-1, 2, 3, 4, 5), # Negative number.
2689 ('str', 4), # Not an int.
2690 (18, 23, 42, 2**63), # Out of range.
2691 (5, 4), # Not sorted.
2692 (6, 7, 7, 8), # Duplicate.
Serhiy Storchaka66bffd12017-04-19 21:12:46 +03002693 (BadInt(1), BadInt(2)),
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002694 ):
2695 with self.assertRaises(
2696 ValueError,
2697 msg='fds_to_keep={}'.format(fds_to_keep)) as c:
2698 _posixsubprocess.fork_exec(
2699 [b"false"], [b"false"],
2700 True, fds_to_keep, None, [b"env"],
2701 -1, -1, -1, -1,
2702 1, 2, 3, 4,
2703 True, True, None)
2704 self.assertIn('fds_to_keep', str(c.exception))
2705 finally:
2706 if not gc_enabled:
2707 gc.disable()
Victor Stinner8f437aa2014-10-05 17:25:19 +02002708
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)2daf8e72016-06-05 02:57:47 +00002709 def test_communicate_BrokenPipeError_stdin_close(self):
2710 # By not setting stdout or stderr or a timeout we force the fast path
2711 # that just calls _stdin_write() internally due to our mock.
2712 proc = subprocess.Popen([sys.executable, '-c', 'pass'])
2713 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
2714 mock_proc_stdin.close.side_effect = BrokenPipeError
2715 proc.communicate() # Should swallow BrokenPipeError from close.
2716 mock_proc_stdin.close.assert_called_with()
2717
2718 def test_communicate_BrokenPipeError_stdin_write(self):
2719 # By not setting stdout or stderr or a timeout we force the fast path
2720 # that just calls _stdin_write() internally due to our mock.
2721 proc = subprocess.Popen([sys.executable, '-c', 'pass'])
2722 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
2723 mock_proc_stdin.write.side_effect = BrokenPipeError
2724 proc.communicate(b'stuff') # Should swallow the BrokenPipeError.
2725 mock_proc_stdin.write.assert_called_once_with(b'stuff')
2726 mock_proc_stdin.close.assert_called_once_with()
2727
2728 def test_communicate_BrokenPipeError_stdin_flush(self):
2729 # Setting stdin and stdout forces the ._communicate() code path.
2730 # python -h exits faster than python -c pass (but spams stdout).
2731 proc = subprocess.Popen([sys.executable, '-h'],
2732 stdin=subprocess.PIPE,
2733 stdout=subprocess.PIPE)
2734 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin, \
2735 open(os.devnull, 'wb') as dev_null:
2736 mock_proc_stdin.flush.side_effect = BrokenPipeError
2737 # because _communicate registers a selector using proc.stdin...
2738 mock_proc_stdin.fileno.return_value = dev_null.fileno()
2739 # _communicate() should swallow BrokenPipeError from flush.
2740 proc.communicate(b'stuff')
2741 mock_proc_stdin.flush.assert_called_once_with()
2742
2743 def test_communicate_BrokenPipeError_stdin_close_with_timeout(self):
2744 # Setting stdin and stdout forces the ._communicate() code path.
2745 # python -h exits faster than python -c pass (but spams stdout).
2746 proc = subprocess.Popen([sys.executable, '-h'],
2747 stdin=subprocess.PIPE,
2748 stdout=subprocess.PIPE)
2749 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
2750 mock_proc_stdin.close.side_effect = BrokenPipeError
2751 # _communicate() should swallow BrokenPipeError from close.
2752 proc.communicate(timeout=999)
2753 mock_proc_stdin.close.assert_called_once_with()
2754
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002755 @unittest.skipUnless(_testcapi is not None
2756 and hasattr(_testcapi, 'W_STOPCODE'),
2757 'need _testcapi.W_STOPCODE')
2758 def test_stopped(self):
Gregory P. Smith50e16e32017-01-22 17:28:38 -08002759 """Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002760 args = [sys.executable, '-c', 'pass']
2761 proc = subprocess.Popen(args)
Victor Stinnercdee3f12017-06-26 17:23:03 +02002762
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002763 # Wait until the real process completes to avoid zombie process
2764 pid = proc.pid
2765 pid, status = os.waitpid(pid, 0)
2766 self.assertEqual(status, 0)
Victor Stinnercdee3f12017-06-26 17:23:03 +02002767
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002768 status = _testcapi.W_STOPCODE(3)
2769 with mock.patch('subprocess.os.waitpid', return_value=(pid, status)):
2770 returncode = proc.wait()
Victor Stinnercdee3f12017-06-26 17:23:03 +02002771
Victor Stinner7b7c6dc2017-08-10 12:37:39 +02002772 self.assertEqual(returncode, -3)
Gregory P. Smith50e16e32017-01-22 17:28:38 -08002773
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002774
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00002775@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +00002776class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00002777
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002778 def test_startupinfo(self):
2779 # startupinfo argument
2780 # We uses hardcoded constants, because we do not want to
2781 # depend on win32all.
2782 STARTF_USESHOWWINDOW = 1
2783 SW_MAXIMIZE = 3
2784 startupinfo = subprocess.STARTUPINFO()
2785 startupinfo.dwFlags = STARTF_USESHOWWINDOW
2786 startupinfo.wShowWindow = SW_MAXIMIZE
2787 # Since Python is a console process, it won't be affected
2788 # by wShowWindow, but the argument should be silently
2789 # ignored
2790 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002791 startupinfo=startupinfo)
2792
Subhendu Ghoshae160bb2017-02-25 20:29:05 +05302793 def test_startupinfo_keywords(self):
2794 # startupinfo argument
2795 # We use hardcoded constants, because we do not want to
2796 # depend on win32all.
2797 STARTF_USERSHOWWINDOW = 1
2798 SW_MAXIMIZE = 3
2799 startupinfo = subprocess.STARTUPINFO(
2800 dwFlags=STARTF_USERSHOWWINDOW,
2801 wShowWindow=SW_MAXIMIZE
2802 )
2803 # Since Python is a console process, it won't be affected
2804 # by wShowWindow, but the argument should be silently
2805 # ignored
2806 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
2807 startupinfo=startupinfo)
2808
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002809 def test_creationflags(self):
2810 # creationflags argument
2811 CREATE_NEW_CONSOLE = 16
2812 sys.stderr.write(" a DOS box should flash briefly ...\n")
2813 subprocess.call(sys.executable +
2814 ' -c "import time; time.sleep(0.25)"',
2815 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002816
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002817 def test_invalid_args(self):
2818 # invalid arguments should raise ValueError
2819 self.assertRaises(ValueError, subprocess.call,
2820 [sys.executable, "-c",
2821 "import sys; sys.exit(47)"],
2822 preexec_fn=lambda: 1)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002823
Oren Milman0b3a87e2017-09-14 22:30:28 +03002824 @support.cpython_only
2825 def test_issue31471(self):
2826 # There shouldn't be an assertion failure in Popen() in case the env
2827 # argument has a bad keys() method.
2828 class BadEnv(dict):
2829 keys = None
2830 with self.assertRaises(TypeError):
2831 subprocess.Popen([sys.executable, "-c", "pass"], env=BadEnv())
2832
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002833 def test_close_fds(self):
2834 # close file descriptors
2835 rc = subprocess.call([sys.executable, "-c",
2836 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002837 close_fds=True)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002838 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002839
Segev Finerb2a60832017-12-18 11:28:19 +02002840 def test_close_fds_with_stdio(self):
2841 import msvcrt
2842
2843 fds = os.pipe()
2844 self.addCleanup(os.close, fds[0])
2845 self.addCleanup(os.close, fds[1])
2846
2847 handles = []
2848 for fd in fds:
2849 os.set_inheritable(fd, True)
2850 handles.append(msvcrt.get_osfhandle(fd))
2851
2852 p = subprocess.Popen([sys.executable, "-c",
2853 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2854 stdout=subprocess.PIPE, close_fds=False)
2855 stdout, stderr = p.communicate()
2856 self.assertEqual(p.returncode, 0)
2857 int(stdout.strip()) # Check that stdout is an integer
2858
2859 p = subprocess.Popen([sys.executable, "-c",
2860 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2861 stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
2862 stdout, stderr = p.communicate()
2863 self.assertEqual(p.returncode, 1)
2864 self.assertIn(b"OSError", stderr)
2865
2866 # The same as the previous call, but with an empty handle_list
2867 handle_list = []
2868 startupinfo = subprocess.STARTUPINFO()
2869 startupinfo.lpAttributeList = {"handle_list": handle_list}
2870 p = subprocess.Popen([sys.executable, "-c",
2871 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2872 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
2873 startupinfo=startupinfo, close_fds=True)
2874 stdout, stderr = p.communicate()
2875 self.assertEqual(p.returncode, 1)
2876 self.assertIn(b"OSError", stderr)
2877
2878 # Check for a warning due to using handle_list and close_fds=False
2879 with support.check_warnings((".*overriding close_fds", RuntimeWarning)):
2880 startupinfo = subprocess.STARTUPINFO()
2881 startupinfo.lpAttributeList = {"handle_list": handles[:]}
2882 p = subprocess.Popen([sys.executable, "-c",
2883 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
2884 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
2885 startupinfo=startupinfo, close_fds=False)
2886 stdout, stderr = p.communicate()
2887 self.assertEqual(p.returncode, 0)
2888
2889 def test_empty_attribute_list(self):
2890 startupinfo = subprocess.STARTUPINFO()
2891 startupinfo.lpAttributeList = {}
2892 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
2893 startupinfo=startupinfo)
2894
2895 def test_empty_handle_list(self):
2896 startupinfo = subprocess.STARTUPINFO()
2897 startupinfo.lpAttributeList = {"handle_list": []}
2898 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
2899 startupinfo=startupinfo)
2900
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002901 def test_shell_sequence(self):
2902 # Run command through the shell (sequence)
2903 newenv = os.environ.copy()
2904 newenv["FRUIT"] = "physalis"
2905 p = subprocess.Popen(["set"], shell=1,
2906 stdout=subprocess.PIPE,
2907 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02002908 with p:
2909 self.assertIn(b"physalis", p.stdout.read())
Guido van Rossume7ba4952007-06-06 23:52:48 +00002910
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002911 def test_shell_string(self):
2912 # Run command through the shell (string)
2913 newenv = os.environ.copy()
2914 newenv["FRUIT"] = "physalis"
2915 p = subprocess.Popen("set", shell=1,
2916 stdout=subprocess.PIPE,
2917 env=newenv)
Victor Stinner7438c612016-05-20 12:43:15 +02002918 with p:
2919 self.assertIn(b"physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002920
Steve Dower050acae2016-09-06 20:16:17 -07002921 def test_shell_encodings(self):
2922 # Run command through the shell (string)
2923 for enc in ['ansi', 'oem']:
2924 newenv = os.environ.copy()
2925 newenv["FRUIT"] = "physalis"
2926 p = subprocess.Popen("set", shell=1,
2927 stdout=subprocess.PIPE,
2928 env=newenv,
2929 encoding=enc)
2930 with p:
2931 self.assertIn("physalis", p.stdout.read(), enc)
2932
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002933 def test_call_string(self):
2934 # call() function with string argument on Windows
2935 rc = subprocess.call(sys.executable +
2936 ' -c "import sys; sys.exit(47)"')
2937 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002938
Florent Xicluna4886d242010-03-08 13:27:26 +00002939 def _kill_process(self, method, *args):
2940 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroua4024e22010-09-24 18:57:01 +00002941 p = subprocess.Popen([sys.executable, "-c", """if 1:
2942 import sys, time
2943 sys.stdout.write('x\\n')
2944 sys.stdout.flush()
2945 time.sleep(30)
2946 """],
2947 stdin=subprocess.PIPE,
2948 stdout=subprocess.PIPE,
2949 stderr=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +02002950 with p:
2951 # Wait for the interpreter to be completely initialized before
2952 # sending any signal.
2953 p.stdout.read(1)
2954 getattr(p, method)(*args)
2955 _, stderr = p.communicate()
2956 self.assertStderrEqual(stderr, b'')
2957 returncode = p.wait()
Florent Xicluna4886d242010-03-08 13:27:26 +00002958 self.assertNotEqual(returncode, 0)
2959
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01002960 def _kill_dead_process(self, method, *args):
2961 p = subprocess.Popen([sys.executable, "-c", """if 1:
2962 import sys, time
2963 sys.stdout.write('x\\n')
2964 sys.stdout.flush()
2965 sys.exit(42)
2966 """],
2967 stdin=subprocess.PIPE,
2968 stdout=subprocess.PIPE,
2969 stderr=subprocess.PIPE)
Victor Stinner7438c612016-05-20 12:43:15 +02002970 with p:
2971 # Wait for the interpreter to be completely initialized before
2972 # sending any signal.
2973 p.stdout.read(1)
2974 # The process should end after this
2975 time.sleep(1)
2976 # This shouldn't raise even though the child is now dead
2977 getattr(p, method)(*args)
2978 _, stderr = p.communicate()
2979 self.assertStderrEqual(stderr, b'')
2980 rc = p.wait()
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01002981 self.assertEqual(rc, 42)
2982
Florent Xicluna4886d242010-03-08 13:27:26 +00002983 def test_send_signal(self):
2984 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimesa342c012008-04-20 21:01:16 +00002985
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002986 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00002987 self._kill_process('kill')
Christian Heimesa342c012008-04-20 21:01:16 +00002988
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002989 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00002990 self._kill_process('terminate')
Christian Heimesa342c012008-04-20 21:01:16 +00002991
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01002992 def test_send_signal_dead(self):
2993 self._kill_dead_process('send_signal', signal.SIGTERM)
2994
2995 def test_kill_dead(self):
2996 self._kill_dead_process('kill')
2997
2998 def test_terminate_dead(self):
2999 self._kill_dead_process('terminate')
3000
Martin Panter23172bd2016-04-16 11:28:10 +00003001class MiscTests(unittest.TestCase):
Gregory P. Smithf4d644f2018-01-29 21:27:39 -08003002
3003 class RecordingPopen(subprocess.Popen):
3004 """A Popen that saves a reference to each instance for testing."""
3005 instances_created = []
3006
3007 def __init__(self, *args, **kwargs):
3008 super().__init__(*args, **kwargs)
3009 self.instances_created.append(self)
3010
3011 @mock.patch.object(subprocess.Popen, "_communicate")
3012 def _test_keyboardinterrupt_no_kill(self, popener, mock__communicate,
3013 **kwargs):
3014 """Fake a SIGINT happening during Popen._communicate() and ._wait().
3015
3016 This avoids the need to actually try and get test environments to send
3017 and receive signals reliably across platforms. The net effect of a ^C
3018 happening during a blocking subprocess execution which we want to clean
3019 up from is a KeyboardInterrupt coming out of communicate() or wait().
3020 """
3021
3022 mock__communicate.side_effect = KeyboardInterrupt
3023 try:
3024 with mock.patch.object(subprocess.Popen, "_wait") as mock__wait:
3025 # We patch out _wait() as no signal was involved so the
3026 # child process isn't actually going to exit rapidly.
3027 mock__wait.side_effect = KeyboardInterrupt
3028 with mock.patch.object(subprocess, "Popen",
3029 self.RecordingPopen):
3030 with self.assertRaises(KeyboardInterrupt):
3031 popener([sys.executable, "-c",
3032 "import time\ntime.sleep(9)\nimport sys\n"
3033 "sys.stderr.write('\\n!runaway child!\\n')"],
3034 stdout=subprocess.DEVNULL, **kwargs)
3035 for call in mock__wait.call_args_list[1:]:
3036 self.assertNotEqual(
3037 call, mock.call(timeout=None),
3038 "no open-ended wait() after the first allowed: "
3039 f"{mock__wait.call_args_list}")
3040 sigint_calls = []
3041 for call in mock__wait.call_args_list:
3042 if call == mock.call(timeout=0.25): # from Popen.__init__
3043 sigint_calls.append(call)
3044 self.assertLessEqual(mock__wait.call_count, 2,
3045 msg=mock__wait.call_args_list)
3046 self.assertEqual(len(sigint_calls), 1,
3047 msg=mock__wait.call_args_list)
3048 finally:
3049 # cleanup the forgotten (due to our mocks) child process
3050 process = self.RecordingPopen.instances_created.pop()
3051 process.kill()
3052 process.wait()
3053 self.assertEqual([], self.RecordingPopen.instances_created)
3054
3055 def test_call_keyboardinterrupt_no_kill(self):
3056 self._test_keyboardinterrupt_no_kill(subprocess.call, timeout=6.282)
3057
3058 def test_run_keyboardinterrupt_no_kill(self):
3059 self._test_keyboardinterrupt_no_kill(subprocess.run, timeout=6.282)
3060
3061 def test_context_manager_keyboardinterrupt_no_kill(self):
3062 def popen_via_context_manager(*args, **kwargs):
3063 with subprocess.Popen(*args, **kwargs) as unused_process:
3064 raise KeyboardInterrupt # Test how __exit__ handles ^C.
3065 self._test_keyboardinterrupt_no_kill(popen_via_context_manager)
3066
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003067 def test_getoutput(self):
3068 self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
3069 self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
3070 (0, 'xyzzy'))
Brett Cannona23810f2008-05-26 19:04:21 +00003071
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003072 # we use mkdtemp in the next line to create an empty directory
3073 # under our exclusive control; from that, we can invent a pathname
3074 # that we _know_ won't exist. This is guaranteed to fail.
3075 dir = None
3076 try:
3077 dir = tempfile.mkdtemp()
3078 name = os.path.join(dir, "foo")
Tim Goldene0041752013-11-03 12:53:17 +00003079 status, output = subprocess.getstatusoutput(
3080 ("type " if mswindows else "cat ") + name)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00003081 self.assertNotEqual(status, 0)
3082 finally:
3083 if dir is not None:
3084 os.rmdir(dir)
Brett Cannona23810f2008-05-26 19:04:21 +00003085
Gregory P. Smithace55862015-04-07 15:57:54 -07003086 def test__all__(self):
3087 """Ensure that __all__ is populated properly."""
Martin Panter528619b2016-04-16 23:42:37 +00003088 intentionally_excluded = {"list2cmdline", "Handle"}
Gregory P. Smithace55862015-04-07 15:57:54 -07003089 exported = set(subprocess.__all__)
3090 possible_exports = set()
3091 import types
3092 for name, value in subprocess.__dict__.items():
3093 if name.startswith('_'):
3094 continue
3095 if isinstance(value, (types.ModuleType,)):
3096 continue
3097 possible_exports.add(name)
3098 self.assertEqual(exported, possible_exports - intentionally_excluded)
3099
3100
Martin Panter23172bd2016-04-16 11:28:10 +00003101@unittest.skipUnless(hasattr(selectors, 'PollSelector'),
3102 "Test needs selectors.PollSelector")
3103class ProcessTestCaseNoPoll(ProcessTestCase):
3104 def setUp(self):
3105 self.orig_selector = subprocess._PopenSelector
3106 subprocess._PopenSelector = selectors.SelectSelector
3107 ProcessTestCase.setUp(self)
3108
3109 def tearDown(self):
3110 subprocess._PopenSelector = self.orig_selector
3111 ProcessTestCase.tearDown(self)
3112
Gregory P. Smithd06fa472009-07-04 02:46:54 +00003113
Tim Golden126c2962010-08-11 14:20:40 +00003114@unittest.skipUnless(mswindows, "Windows-specific tests")
3115class CommandsWithSpaces (BaseTestCase):
3116
3117 def setUp(self):
3118 super().setUp()
Berker Peksag16a1f282015-09-28 13:33:14 +03003119 f, fname = tempfile.mkstemp(".py", "te st")
Tim Golden126c2962010-08-11 14:20:40 +00003120 self.fname = fname.lower ()
3121 os.write(f, b"import sys;"
3122 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
3123 )
3124 os.close(f)
3125
3126 def tearDown(self):
3127 os.remove(self.fname)
3128 super().tearDown()
3129
3130 def with_spaces(self, *args, **kwargs):
3131 kwargs['stdout'] = subprocess.PIPE
3132 p = subprocess.Popen(*args, **kwargs)
Victor Stinner7438c612016-05-20 12:43:15 +02003133 with p:
3134 self.assertEqual(
3135 p.stdout.read ().decode("mbcs"),
3136 "2 [%r, 'ab cd']" % self.fname
3137 )
Tim Golden126c2962010-08-11 14:20:40 +00003138
3139 def test_shell_string_with_spaces(self):
3140 # call() function with string argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00003141 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
3142 "ab cd"), shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00003143
3144 def test_shell_sequence_with_spaces(self):
3145 # call() function with sequence argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00003146 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00003147
3148 def test_noshell_string_with_spaces(self):
3149 # call() function with string argument with spaces on Windows
3150 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
3151 "ab cd"))
3152
3153 def test_noshell_sequence_with_spaces(self):
3154 # call() function with sequence argument with spaces on Windows
3155 self.with_spaces([sys.executable, self.fname, "ab cd"])
3156
Brian Curtin79cdb662010-12-03 02:46:02 +00003157
Georg Brandla86b2622012-02-20 21:34:57 +01003158class ContextManagerTests(BaseTestCase):
Brian Curtin79cdb662010-12-03 02:46:02 +00003159
3160 def test_pipe(self):
3161 with subprocess.Popen([sys.executable, "-c",
3162 "import sys;"
3163 "sys.stdout.write('stdout');"
3164 "sys.stderr.write('stderr');"],
3165 stdout=subprocess.PIPE,
3166 stderr=subprocess.PIPE) as proc:
3167 self.assertEqual(proc.stdout.read(), b"stdout")
3168 self.assertStderrEqual(proc.stderr.read(), b"stderr")
3169
3170 self.assertTrue(proc.stdout.closed)
3171 self.assertTrue(proc.stderr.closed)
3172
3173 def test_returncode(self):
3174 with subprocess.Popen([sys.executable, "-c",
3175 "import sys; sys.exit(100)"]) as proc:
Gregory P. Smith6b657452011-05-11 21:42:08 -07003176 pass
3177 # __exit__ calls wait(), so the returncode should be set
Brian Curtin79cdb662010-12-03 02:46:02 +00003178 self.assertEqual(proc.returncode, 100)
3179
3180 def test_communicate_stdin(self):
3181 with subprocess.Popen([sys.executable, "-c",
3182 "import sys;"
3183 "sys.exit(sys.stdin.read() == 'context')"],
3184 stdin=subprocess.PIPE) as proc:
3185 proc.communicate(b"context")
3186 self.assertEqual(proc.returncode, 1)
3187
3188 def test_invalid_args(self):
Victor Stinnerb31206a2018-01-25 19:06:05 +01003189 with self.assertRaises(NONEXISTING_ERRORS):
Victor Stinner9a83f652017-08-21 23:51:31 +02003190 with subprocess.Popen(NONEXISTING_CMD,
Brian Curtin79cdb662010-12-03 02:46:02 +00003191 stdout=subprocess.PIPE,
3192 stderr=subprocess.PIPE) as proc:
3193 pass
3194
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003195 def test_broken_pipe_cleanup(self):
3196 """Broken pipe error should not prevent wait() (Issue 21619)"""
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003197 proc = subprocess.Popen([sys.executable, '-c', 'pass'],
Victor Stinner20f4bd42015-03-05 02:38:41 +01003198 stdin=subprocess.PIPE,
Victor Stinner20f4bd42015-03-05 02:38:41 +01003199 bufsize=support.PIPE_MAX_SIZE*2)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003200 proc = proc.__enter__()
3201 # Prepare to send enough data to overflow any OS pipe buffering and
3202 # guarantee a broken pipe error. Data is held in BufferedWriter
3203 # buffer until closed.
3204 proc.stdin.write(b'x' * support.PIPE_MAX_SIZE)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003205 self.assertIsNone(proc.returncode)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003206 # EPIPE expected under POSIX; EINVAL under Windows
Serhiy Storchakacf265fd2015-02-28 13:27:54 +02003207 self.assertRaises(OSError, proc.__exit__, None, None, None)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02003208 self.assertEqual(proc.returncode, 0)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003209 self.assertTrue(proc.stdin.closed)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02003210
Brian Curtin79cdb662010-12-03 02:46:02 +00003211
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00003212if __name__ == "__main__":
Gregory P. Smith112bb3a2011-03-15 14:55:17 -04003213 unittest.main()