blob: 9c7aa93f049a5e3ac914717002734db750a1fe50 [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001import unittest
Berker Peksagce643912015-05-06 06:33:17 +03002from test.support import script_helper
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00004import subprocess
5import sys
6import signal
Gregory P. Smith112bb3a2011-03-15 14:55:17 -04007import io
Andrew Svetlov82860712012-08-19 22:13:41 +03008import locale
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00009import os
Gregory P. Smitha59c59f2010-03-01 00:17:40 +000010import errno
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000011import tempfile
12import time
Tim Peters3761e8d2004-10-13 04:07:12 +000013import re
Charles-François Natali3a4586a2013-11-08 19:56:59 +010014import selectors
Ezio Melotti184bdfb2010-02-18 09:37:05 +000015import sysconfig
Gregory P. Smithd23047b2010-12-04 09:10:44 +000016import warnings
Gregory P. Smith51ee2702010-12-13 07:59:39 +000017import select
Gregory P. Smith81ce6852011-03-15 02:04:11 -040018import shutil
Benjamin Petersonb870aa12011-12-10 12:44:25 -050019import gc
Andrew Svetlov47ec25d2012-08-19 16:25:37 +030020import textwrap
Benjamin Peterson964561b2011-12-10 12:31:42 -050021
22try:
Antoine Pitroua8392712013-08-30 23:38:13 +020023 import threading
24except ImportError:
25 threading = None
Benjamin Peterson964561b2011-12-10 12:31:42 -050026
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000027mswindows = (sys.platform == "win32")
28
29#
30# Depends on the following external programs: Python
31#
32
33if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000034 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
35 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000036else:
37 SETBINARY = ''
38
Florent Xiclunab1e94e82010-02-27 22:12:37 +000039
Florent Xiclunac049d872010-03-27 22:47:23 +000040class BaseTestCase(unittest.TestCase):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000041 def setUp(self):
42 # Try to minimize the number of children we have so this test
43 # doesn't crash on some buildbots (Alphas in particular).
Florent Xiclunab1e94e82010-02-27 22:12:37 +000044 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000045
Florent Xiclunaf0cbd822010-03-04 21:50:56 +000046 def tearDown(self):
47 for inst in subprocess._active:
48 inst.wait()
49 subprocess._cleanup()
50 self.assertFalse(subprocess._active, "subprocess._active not empty")
51
Florent Xiclunab1e94e82010-02-27 22:12:37 +000052 def assertStderrEqual(self, stderr, expected, msg=None):
53 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
54 # shutdown time. That frustrates tests trying to check stderr produced
55 # from a spawned Python process.
Antoine Pitrou62f68ed2010-08-04 11:48:56 +000056 actual = support.strip_python_stderr(stderr)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -040057 # strip_python_stderr also strips whitespace, so we do too.
58 expected = expected.strip()
Florent Xiclunab1e94e82010-02-27 22:12:37 +000059 self.assertEqual(actual, expected, msg)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000060
Florent Xiclunac049d872010-03-27 22:47:23 +000061
Gregory P. Smith3d8e7762012-11-10 22:32:22 -080062class PopenTestException(Exception):
63 pass
64
65
66class PopenExecuteChildRaises(subprocess.Popen):
67 """Popen subclass for testing cleanup of subprocess.PIPE filehandles when
68 _execute_child fails.
69 """
70 def _execute_child(self, *args, **kwargs):
71 raise PopenTestException("Forced Exception for Test")
72
73
Florent Xiclunac049d872010-03-27 22:47:23 +000074class ProcessTestCase(BaseTestCase):
75
Gregory P. Smitha1ed5392013-03-23 11:44:25 -070076 def test_io_buffered_by_default(self):
77 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
78 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
79 stderr=subprocess.PIPE)
80 try:
81 self.assertIsInstance(p.stdin, io.BufferedIOBase)
82 self.assertIsInstance(p.stdout, io.BufferedIOBase)
83 self.assertIsInstance(p.stderr, io.BufferedIOBase)
84 finally:
Gregory P. Smitha1b9ed32013-03-23 11:54:22 -070085 p.stdin.close()
86 p.stdout.close()
87 p.stderr.close()
Gregory P. Smitha1ed5392013-03-23 11:44:25 -070088 p.wait()
89
90 def test_io_unbuffered_works(self):
91 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
92 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
93 stderr=subprocess.PIPE, bufsize=0)
94 try:
95 self.assertIsInstance(p.stdin, io.RawIOBase)
96 self.assertIsInstance(p.stdout, io.RawIOBase)
97 self.assertIsInstance(p.stderr, io.RawIOBase)
98 finally:
Gregory P. Smitha1b9ed32013-03-23 11:54:22 -070099 p.stdin.close()
100 p.stdout.close()
101 p.stderr.close()
Gregory P. Smitha1ed5392013-03-23 11:44:25 -0700102 p.wait()
103
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000104 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000105 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +0000106 rc = subprocess.call([sys.executable, "-c",
107 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000108 self.assertEqual(rc, 47)
109
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400110 def test_call_timeout(self):
111 # call() function with timeout argument; we want to test that the child
112 # process gets killed when the timeout expires. If the child isn't
113 # killed, this call will deadlock since subprocess.call waits for the
114 # child.
115 self.assertRaises(subprocess.TimeoutExpired, subprocess.call,
116 [sys.executable, "-c", "while True: pass"],
117 timeout=0.1)
118
Peter Astrand454f7672005-01-01 09:36:35 +0000119 def test_check_call_zero(self):
120 # check_call() function with zero return code
121 rc = subprocess.check_call([sys.executable, "-c",
122 "import sys; sys.exit(0)"])
123 self.assertEqual(rc, 0)
124
125 def test_check_call_nonzero(self):
126 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000127 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +0000128 subprocess.check_call([sys.executable, "-c",
129 "import sys; sys.exit(47)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000130 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +0000131
Georg Brandlf9734072008-12-07 15:30:06 +0000132 def test_check_output(self):
133 # check_output() function with zero return code
134 output = subprocess.check_output(
135 [sys.executable, "-c", "print('BDFL')"])
Benjamin Peterson577473f2010-01-19 00:09:57 +0000136 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +0000137
138 def test_check_output_nonzero(self):
139 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000140 with self.assertRaises(subprocess.CalledProcessError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +0000141 subprocess.check_output(
142 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000143 self.assertEqual(c.exception.returncode, 5)
Georg Brandlf9734072008-12-07 15:30:06 +0000144
145 def test_check_output_stderr(self):
146 # check_output() function stderr redirected to stdout
147 output = subprocess.check_output(
148 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
149 stderr=subprocess.STDOUT)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000150 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +0000151
Serhiy Storchakafcd9f222013-04-22 20:20:54 +0300152 def test_check_output_stdin_arg(self):
153 # check_output() can be called with stdin set to a file
154 tf = tempfile.TemporaryFile()
155 self.addCleanup(tf.close)
156 tf.write(b'pear')
157 tf.seek(0)
158 output = subprocess.check_output(
159 [sys.executable, "-c",
160 "import sys; sys.stdout.write(sys.stdin.read().upper())"],
161 stdin=tf)
162 self.assertIn(b'PEAR', output)
163
164 def test_check_output_input_arg(self):
165 # check_output() can be called with input set to a string
166 output = subprocess.check_output(
167 [sys.executable, "-c",
168 "import sys; sys.stdout.write(sys.stdin.read().upper())"],
169 input=b'pear')
170 self.assertIn(b'PEAR', output)
171
Georg Brandlf9734072008-12-07 15:30:06 +0000172 def test_check_output_stdout_arg(self):
Serhiy Storchakafcd9f222013-04-22 20:20:54 +0300173 # check_output() refuses to accept 'stdout' argument
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000174 with self.assertRaises(ValueError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +0000175 output = subprocess.check_output(
176 [sys.executable, "-c", "print('will not be run')"],
177 stdout=sys.stdout)
Georg Brandlf9734072008-12-07 15:30:06 +0000178 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000179 self.assertIn('stdout', c.exception.args[0])
Georg Brandlf9734072008-12-07 15:30:06 +0000180
Serhiy Storchakafcd9f222013-04-22 20:20:54 +0300181 def test_check_output_stdin_with_input_arg(self):
182 # check_output() refuses to accept 'stdin' with 'input'
183 tf = tempfile.TemporaryFile()
184 self.addCleanup(tf.close)
185 tf.write(b'pear')
186 tf.seek(0)
187 with self.assertRaises(ValueError) as c:
188 output = subprocess.check_output(
189 [sys.executable, "-c", "print('will not be run')"],
190 stdin=tf, input=b'hare')
191 self.fail("Expected ValueError when stdin and input args supplied.")
192 self.assertIn('stdin', c.exception.args[0])
193 self.assertIn('input', c.exception.args[0])
194
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400195 def test_check_output_timeout(self):
196 # check_output() function with timeout arg
197 with self.assertRaises(subprocess.TimeoutExpired) as c:
198 output = subprocess.check_output(
199 [sys.executable, "-c",
Victor Stinner149b1c72011-06-06 23:43:02 +0200200 "import sys, time\n"
201 "sys.stdout.write('BDFL')\n"
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400202 "sys.stdout.flush()\n"
Victor Stinner149b1c72011-06-06 23:43:02 +0200203 "time.sleep(3600)"],
Reid Klecknerda9ac722011-03-16 17:08:21 -0400204 # Some heavily loaded buildbots (sparc Debian 3.x) require
205 # this much time to start and print.
206 timeout=3)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400207 self.fail("Expected TimeoutExpired.")
208 self.assertEqual(c.exception.output, b'BDFL')
209
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000210 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000211 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000212 newenv = os.environ.copy()
213 newenv["FRUIT"] = "banana"
214 rc = subprocess.call([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000215 'import sys, os;'
216 'sys.exit(os.getenv("FRUIT")=="banana")'],
217 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000218 self.assertEqual(rc, 1)
219
Victor Stinner87b9bc32011-06-01 00:57:47 +0200220 def test_invalid_args(self):
221 # Popen() called with invalid arguments should raise TypeError
222 # but Popen.__del__ should not complain (issue #12085)
223 with support.captured_stderr() as s:
224 self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
225 argcount = subprocess.Popen.__init__.__code__.co_argcount
226 too_many_args = [0] * (argcount + 1)
227 self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
228 self.assertEqual(s.getvalue(), '')
229
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000230 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000231 # .stdin is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000232 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000233 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000234 self.addCleanup(p.stdout.close)
235 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000236 p.wait()
237 self.assertEqual(p.stdin, None)
238
239 def test_stdout_none(self):
Ezio Melotti42a541b2013-03-11 05:53:34 +0200240 # .stdout is None when not redirected, and the child's stdout will
241 # be inherited from the parent. In order to test this we run a
242 # subprocess in a subprocess:
243 # this_test
244 # \-- subprocess created by this test (parent)
245 # \-- subprocess created by the parent subprocess (child)
246 # The parent doesn't specify stdout, so the child will use the
247 # parent's stdout. This test checks that the message printed by the
248 # child goes to the parent stdout. The parent also checks that the
249 # child's stdout is None. See #11963.
250 code = ('import sys; from subprocess import Popen, PIPE;'
251 'p = Popen([sys.executable, "-c", "print(\'test_stdout_none\')"],'
252 ' stdin=PIPE, stderr=PIPE);'
253 'p.wait(); assert p.stdout is None;')
254 p = subprocess.Popen([sys.executable, "-c", code],
255 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
256 self.addCleanup(p.stdout.close)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000257 self.addCleanup(p.stderr.close)
Ezio Melotti42a541b2013-03-11 05:53:34 +0200258 out, err = p.communicate()
259 self.assertEqual(p.returncode, 0, err)
260 self.assertEqual(out.rstrip(), b'test_stdout_none')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000261
262 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000263 # .stderr is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000264 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000265 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000266 self.addCleanup(p.stdout.close)
267 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000268 p.wait()
269 self.assertEqual(p.stderr, None)
270
Chris Jerdonek776cb192012-10-08 15:56:43 -0700271 def _assert_python(self, pre_args, **kwargs):
272 # We include sys.exit() to prevent the test runner from hanging
273 # whenever python is found.
274 args = pre_args + ["import sys; sys.exit(47)"]
275 p = subprocess.Popen(args, **kwargs)
276 p.wait()
277 self.assertEqual(47, p.returncode)
278
279 def test_executable(self):
280 # Check that the executable argument works.
Chris Jerdonek86b0fb22012-10-09 13:17:49 -0700281 #
282 # On Unix (non-Mac and non-Windows), Python looks at args[0] to
283 # determine where its standard library is, so we need the directory
284 # of args[0] to be valid for the Popen() call to Python to succeed.
285 # See also issue #16170 and issue #7774.
286 doesnotexist = os.path.join(os.path.dirname(sys.executable),
287 "doesnotexist")
288 self._assert_python([doesnotexist, "-c"], executable=sys.executable)
Chris Jerdonek776cb192012-10-08 15:56:43 -0700289
290 def test_executable_takes_precedence(self):
291 # Check that the executable argument takes precedence over args[0].
292 #
293 # Verify first that the call succeeds without the executable arg.
294 pre_args = [sys.executable, "-c"]
295 self._assert_python(pre_args)
296 self.assertRaises(FileNotFoundError, self._assert_python, pre_args,
297 executable="doesnotexist")
298
299 @unittest.skipIf(mswindows, "executable argument replaces shell")
300 def test_executable_replaces_shell(self):
301 # Check that the executable argument replaces the default shell
302 # when shell=True.
303 self._assert_python([], executable=sys.executable, shell=True)
304
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700305 # For use in the test_cwd* tests below.
306 def _normalize_cwd(self, cwd):
307 # Normalize an expected cwd (for Tru64 support).
308 # We can't use os.path.realpath since it doesn't expand Tru64 {memb}
309 # strings. See bug #1063571.
Serhiy Storchaka2a23adf2015-09-06 14:13:25 +0300310 with support.change_cwd(cwd):
311 return os.getcwd()
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700312
313 # For use in the test_cwd* tests below.
314 def _split_python_path(self):
315 # Return normalized (python_dir, python_base).
316 python_path = os.path.realpath(sys.executable)
317 return os.path.split(python_path)
318
319 # For use in the test_cwd* tests below.
320 def _assert_cwd(self, expected_cwd, python_arg, **kwargs):
321 # Invoke Python via Popen, and assert that (1) the call succeeds,
322 # and that (2) the current working directory of the child process
323 # matches *expected_cwd*.
324 p = subprocess.Popen([python_arg, "-c",
325 "import os, sys; "
326 "sys.stdout.write(os.getcwd()); "
327 "sys.exit(47)"],
328 stdout=subprocess.PIPE,
329 **kwargs)
330 self.addCleanup(p.stdout.close)
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000331 p.wait()
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700332 self.assertEqual(47, p.returncode)
333 normcase = os.path.normcase
334 self.assertEqual(normcase(expected_cwd),
335 normcase(p.stdout.read().decode("utf-8")))
336
337 def test_cwd(self):
338 # Check that cwd changes the cwd for the child process.
339 temp_dir = tempfile.gettempdir()
340 temp_dir = self._normalize_cwd(temp_dir)
341 self._assert_cwd(temp_dir, sys.executable, cwd=temp_dir)
342
Chris Jerdonekc2cd6262012-09-30 09:45:00 -0700343 @unittest.skipIf(mswindows, "pending resolution of issue #15533")
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700344 def test_cwd_with_relative_arg(self):
345 # Check that Popen looks for args[0] relative to cwd if args[0]
346 # is relative.
347 python_dir, python_base = self._split_python_path()
348 rel_python = os.path.join(os.curdir, python_base)
349 with support.temp_cwd() as wrong_dir:
350 # Before calling with the correct cwd, confirm that the call fails
351 # without cwd and with the wrong cwd.
Chris Jerdonek28714c82012-09-30 02:15:37 -0700352 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700353 [rel_python])
Chris Jerdonek28714c82012-09-30 02:15:37 -0700354 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700355 [rel_python], cwd=wrong_dir)
356 python_dir = self._normalize_cwd(python_dir)
357 self._assert_cwd(python_dir, rel_python, cwd=python_dir)
358
Chris Jerdonekc2cd6262012-09-30 09:45:00 -0700359 @unittest.skipIf(mswindows, "pending resolution of issue #15533")
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700360 def test_cwd_with_relative_executable(self):
361 # Check that Popen looks for executable relative to cwd if executable
362 # is relative (and that executable takes precedence over args[0]).
363 python_dir, python_base = self._split_python_path()
364 rel_python = os.path.join(os.curdir, python_base)
365 doesntexist = "somethingyoudonthave"
366 with support.temp_cwd() as wrong_dir:
367 # Before calling with the correct cwd, confirm that the call fails
368 # without cwd and with the wrong cwd.
Chris Jerdonek28714c82012-09-30 02:15:37 -0700369 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700370 [doesntexist], executable=rel_python)
Chris Jerdonek28714c82012-09-30 02:15:37 -0700371 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700372 [doesntexist], executable=rel_python,
373 cwd=wrong_dir)
374 python_dir = self._normalize_cwd(python_dir)
375 self._assert_cwd(python_dir, doesntexist, executable=rel_python,
376 cwd=python_dir)
377
378 def test_cwd_with_absolute_arg(self):
379 # Check that Popen can find the executable when the cwd is wrong
380 # if args[0] is an absolute path.
381 python_dir, python_base = self._split_python_path()
382 abs_python = os.path.join(python_dir, python_base)
383 rel_python = os.path.join(os.curdir, python_base)
Berker Peksagce643912015-05-06 06:33:17 +0300384 with support.temp_dir() as wrong_dir:
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700385 # Before calling with an absolute path, confirm that using a
386 # relative path fails.
Chris Jerdonek28714c82012-09-30 02:15:37 -0700387 self.assertRaises(FileNotFoundError, subprocess.Popen,
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700388 [rel_python], cwd=wrong_dir)
389 wrong_dir = self._normalize_cwd(wrong_dir)
390 self._assert_cwd(wrong_dir, abs_python, cwd=wrong_dir)
391
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100392 @unittest.skipIf(sys.base_prefix != sys.prefix,
393 'Test is not venv-compatible')
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000394 def test_executable_with_cwd(self):
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700395 python_dir, python_base = self._split_python_path()
396 python_dir = self._normalize_cwd(python_dir)
397 self._assert_cwd(python_dir, "somethingyoudonthave",
398 executable=sys.executable, cwd=python_dir)
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000399
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100400 @unittest.skipIf(sys.base_prefix != sys.prefix,
401 'Test is not venv-compatible')
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000402 @unittest.skipIf(sysconfig.is_python_build(),
403 "need an installed Python. See #7774")
404 def test_executable_without_cwd(self):
405 # For a normal installation, it should work without 'cwd'
406 # argument. For test runs in the build directory, see #7774.
Ned Deilye92dfbf2013-08-02 18:02:21 -0700407 self._assert_cwd(os.getcwd(), "somethingyoudonthave",
408 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000409
410 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000411 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000412 p = subprocess.Popen([sys.executable, "-c",
413 'import sys; sys.exit(sys.stdin.read() == "pear")'],
414 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000415 p.stdin.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000416 p.stdin.close()
417 p.wait()
418 self.assertEqual(p.returncode, 1)
419
420 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000421 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000422 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000423 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000424 d = tf.fileno()
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000425 os.write(d, b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000426 os.lseek(d, 0, 0)
427 p = subprocess.Popen([sys.executable, "-c",
428 'import sys; sys.exit(sys.stdin.read() == "pear")'],
429 stdin=d)
430 p.wait()
431 self.assertEqual(p.returncode, 1)
432
433 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000434 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000435 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000436 self.addCleanup(tf.close)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000437 tf.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000438 tf.seek(0)
439 p = subprocess.Popen([sys.executable, "-c",
440 'import sys; sys.exit(sys.stdin.read() == "pear")'],
441 stdin=tf)
442 p.wait()
443 self.assertEqual(p.returncode, 1)
444
445 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000446 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000447 p = subprocess.Popen([sys.executable, "-c",
448 'import sys; sys.stdout.write("orange")'],
449 stdout=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000450 self.addCleanup(p.stdout.close)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000451 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000452
453 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000454 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000455 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000456 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000457 d = tf.fileno()
458 p = subprocess.Popen([sys.executable, "-c",
459 'import sys; sys.stdout.write("orange")'],
460 stdout=d)
461 p.wait()
462 os.lseek(d, 0, 0)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000463 self.assertEqual(os.read(d, 1024), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000464
465 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000466 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000467 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000468 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000469 p = subprocess.Popen([sys.executable, "-c",
470 'import sys; sys.stdout.write("orange")'],
471 stdout=tf)
472 p.wait()
473 tf.seek(0)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000474 self.assertEqual(tf.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000475
476 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000477 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000478 p = subprocess.Popen([sys.executable, "-c",
479 'import sys; sys.stderr.write("strawberry")'],
480 stderr=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000481 self.addCleanup(p.stderr.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000482 self.assertStderrEqual(p.stderr.read(), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000483
484 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000485 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000486 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000487 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000488 d = tf.fileno()
489 p = subprocess.Popen([sys.executable, "-c",
490 'import sys; sys.stderr.write("strawberry")'],
491 stderr=d)
492 p.wait()
493 os.lseek(d, 0, 0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000494 self.assertStderrEqual(os.read(d, 1024), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000495
496 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000497 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000498 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000499 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000500 p = subprocess.Popen([sys.executable, "-c",
501 'import sys; sys.stderr.write("strawberry")'],
502 stderr=tf)
503 p.wait()
504 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000505 self.assertStderrEqual(tf.read(), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000506
Martin Panterc7635892016-05-13 01:54:44 +0000507 def test_stderr_redirect_with_no_stdout_redirect(self):
508 # test stderr=STDOUT while stdout=None (not set)
509
510 # - grandchild prints to stderr
511 # - child redirects grandchild's stderr to its stdout
512 # - the parent should get grandchild's stderr in child's stdout
513 p = subprocess.Popen([sys.executable, "-c",
514 'import sys, subprocess;'
515 'rc = subprocess.call([sys.executable, "-c",'
516 ' "import sys;"'
517 ' "sys.stderr.write(\'42\')"],'
518 ' stderr=subprocess.STDOUT);'
519 'sys.exit(rc)'],
520 stdout=subprocess.PIPE,
521 stderr=subprocess.PIPE)
522 stdout, stderr = p.communicate()
523 #NOTE: stdout should get stderr from grandchild
524 self.assertStderrEqual(stdout, b'42')
525 self.assertStderrEqual(stderr, b'') # should be empty
526 self.assertEqual(p.returncode, 0)
527
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000528 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000529 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000530 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000531 'import sys;'
532 'sys.stdout.write("apple");'
533 'sys.stdout.flush();'
534 'sys.stderr.write("orange")'],
535 stdout=subprocess.PIPE,
536 stderr=subprocess.STDOUT)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000537 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000538 self.assertStderrEqual(p.stdout.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000539
540 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000541 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000542 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000543 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000544 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000545 'import sys;'
546 'sys.stdout.write("apple");'
547 'sys.stdout.flush();'
548 'sys.stderr.write("orange")'],
549 stdout=tf,
550 stderr=tf)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000551 p.wait()
552 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000553 self.assertStderrEqual(tf.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000554
Thomas Wouters89f507f2006-12-13 04:49:30 +0000555 def test_stdout_filedes_of_stdout(self):
556 # stdout is set to 1 (#1531862).
Ezio Melotti42a541b2013-03-11 05:53:34 +0200557 # To avoid printing the text on stdout, we do something similar to
558 # test_stdout_none (see above). The parent subprocess calls the child
559 # subprocess passing stdout=1, and this test uses stdout=PIPE in
560 # order to capture and check the output of the parent. See #11963.
561 code = ('import sys, subprocess; '
562 'rc = subprocess.call([sys.executable, "-c", '
563 ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), '
564 'b\'test with stdout=1\'))"], stdout=1); '
565 'assert rc == 18')
566 p = subprocess.Popen([sys.executable, "-c", code],
567 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
568 self.addCleanup(p.stdout.close)
569 self.addCleanup(p.stderr.close)
570 out, err = p.communicate()
571 self.assertEqual(p.returncode, 0, err)
572 self.assertEqual(out.rstrip(), b'test with stdout=1')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000573
Ross Lagerwallba102ec2011-03-16 18:40:25 +0200574 def test_stdout_devnull(self):
575 p = subprocess.Popen([sys.executable, "-c",
576 'for i in range(10240):'
577 'print("x" * 1024)'],
578 stdout=subprocess.DEVNULL)
579 p.wait()
580 self.assertEqual(p.stdout, None)
581
582 def test_stderr_devnull(self):
583 p = subprocess.Popen([sys.executable, "-c",
584 'import sys\n'
585 'for i in range(10240):'
586 'sys.stderr.write("x" * 1024)'],
587 stderr=subprocess.DEVNULL)
588 p.wait()
589 self.assertEqual(p.stderr, None)
590
591 def test_stdin_devnull(self):
592 p = subprocess.Popen([sys.executable, "-c",
593 'import sys;'
594 'sys.stdin.read(1)'],
595 stdin=subprocess.DEVNULL)
596 p.wait()
597 self.assertEqual(p.stdin, None)
598
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000599 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000600 newenv = os.environ.copy()
601 newenv["FRUIT"] = "orange"
Victor Stinnerf1512a22011-06-21 17:18:38 +0200602 with subprocess.Popen([sys.executable, "-c",
603 'import sys,os;'
604 'sys.stdout.write(os.getenv("FRUIT"))'],
605 stdout=subprocess.PIPE,
606 env=newenv) as p:
607 stdout, stderr = p.communicate()
608 self.assertEqual(stdout, b"orange")
609
Victor Stinner62d51182011-06-23 01:02:25 +0200610 # Windows requires at least the SYSTEMROOT environment variable to start
611 # Python
612 @unittest.skipIf(sys.platform == 'win32',
613 'cannot test an empty env on Windows')
Victor Stinner237e5cb2011-06-22 21:28:43 +0200614 @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') is not None,
Victor Stinner372309a2011-06-21 21:59:06 +0200615 'the python library cannot be loaded '
616 'with an empty environment')
Victor Stinnerf1512a22011-06-21 17:18:38 +0200617 def test_empty_env(self):
618 with subprocess.Popen([sys.executable, "-c",
619 'import os; '
Victor Stinner372309a2011-06-21 21:59:06 +0200620 'print(list(os.environ.keys()))'],
Victor Stinnerf1512a22011-06-21 17:18:38 +0200621 stdout=subprocess.PIPE,
622 env={}) as p:
623 stdout, stderr = p.communicate()
Victor Stinner237e5cb2011-06-22 21:28:43 +0200624 self.assertIn(stdout.strip(),
625 (b"[]",
626 # Mac OS X adds __CF_USER_TEXT_ENCODING variable to an empty
627 # environment
628 b"['__CF_USER_TEXT_ENCODING']"))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000629
Peter Astrandcbac93c2005-03-03 20:24:28 +0000630 def test_communicate_stdin(self):
631 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000632 'import sys;'
633 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000634 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000635 p.communicate(b"pear")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000636 self.assertEqual(p.returncode, 1)
637
638 def test_communicate_stdout(self):
639 p = subprocess.Popen([sys.executable, "-c",
640 'import sys; sys.stdout.write("pineapple")'],
641 stdout=subprocess.PIPE)
642 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000643 self.assertEqual(stdout, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000644 self.assertEqual(stderr, None)
645
646 def test_communicate_stderr(self):
647 p = subprocess.Popen([sys.executable, "-c",
648 'import sys; sys.stderr.write("pineapple")'],
649 stderr=subprocess.PIPE)
650 (stdout, stderr) = p.communicate()
651 self.assertEqual(stdout, None)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000652 self.assertStderrEqual(stderr, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000653
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000654 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000655 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000656 'import sys,os;'
657 'sys.stderr.write("pineapple");'
658 'sys.stdout.write(sys.stdin.read())'],
659 stdin=subprocess.PIPE,
660 stdout=subprocess.PIPE,
661 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000662 self.addCleanup(p.stdout.close)
663 self.addCleanup(p.stderr.close)
664 self.addCleanup(p.stdin.close)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000665 (stdout, stderr) = p.communicate(b"banana")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000666 self.assertEqual(stdout, b"banana")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000667 self.assertStderrEqual(stderr, b"pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000668
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400669 def test_communicate_timeout(self):
670 p = subprocess.Popen([sys.executable, "-c",
671 'import sys,os,time;'
672 'sys.stderr.write("pineapple\\n");'
673 'time.sleep(1);'
674 'sys.stderr.write("pear\\n");'
675 'sys.stdout.write(sys.stdin.read())'],
676 universal_newlines=True,
677 stdin=subprocess.PIPE,
678 stdout=subprocess.PIPE,
679 stderr=subprocess.PIPE)
680 self.assertRaises(subprocess.TimeoutExpired, p.communicate, "banana",
681 timeout=0.3)
682 # Make sure we can keep waiting for it, and that we get the whole output
683 # after it completes.
684 (stdout, stderr) = p.communicate()
685 self.assertEqual(stdout, "banana")
686 self.assertStderrEqual(stderr.encode(), b"pineapple\npear\n")
687
688 def test_communicate_timeout_large_ouput(self):
Ross Lagerwall003c7a32012-02-12 09:02:01 +0200689 # Test an expiring timeout while the child is outputting lots of data.
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400690 p = subprocess.Popen([sys.executable, "-c",
691 'import sys,os,time;'
692 'sys.stdout.write("a" * (64 * 1024));'
693 'time.sleep(0.2);'
694 'sys.stdout.write("a" * (64 * 1024));'
695 'time.sleep(0.2);'
696 'sys.stdout.write("a" * (64 * 1024));'
697 'time.sleep(0.2);'
698 'sys.stdout.write("a" * (64 * 1024));'],
699 stdout=subprocess.PIPE)
700 self.assertRaises(subprocess.TimeoutExpired, p.communicate, timeout=0.4)
701 (stdout, _) = p.communicate()
702 self.assertEqual(len(stdout), 4 * 64 * 1024)
703
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000704 # Test for the fd leak reported in http://bugs.python.org/issue2791.
705 def test_communicate_pipe_fd_leak(self):
Victor Stinner667d4b52010-12-25 22:40:32 +0000706 for stdin_pipe in (False, True):
707 for stdout_pipe in (False, True):
708 for stderr_pipe in (False, True):
709 options = {}
710 if stdin_pipe:
711 options['stdin'] = subprocess.PIPE
712 if stdout_pipe:
713 options['stdout'] = subprocess.PIPE
714 if stderr_pipe:
715 options['stderr'] = subprocess.PIPE
716 if not options:
717 continue
718 p = subprocess.Popen((sys.executable, "-c", "pass"), **options)
719 p.communicate()
720 if p.stdin is not None:
721 self.assertTrue(p.stdin.closed)
722 if p.stdout is not None:
723 self.assertTrue(p.stdout.closed)
724 if p.stderr is not None:
725 self.assertTrue(p.stderr.closed)
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000726
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000727 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000728 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000729 p = subprocess.Popen([sys.executable, "-c",
730 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000731 (stdout, stderr) = p.communicate()
732 self.assertEqual(stdout, None)
733 self.assertEqual(stderr, None)
734
735 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000736 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000737 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000738 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000739 x, y = os.pipe()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000740 os.close(x)
741 os.close(y)
742 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000743 'import sys,os;'
744 'sys.stdout.write(sys.stdin.read(47));'
Charles-François Natali2d517212011-05-29 16:36:44 +0200745 'sys.stderr.write("x" * %d);'
746 'sys.stdout.write(sys.stdin.read())' %
747 support.PIPE_MAX_SIZE],
Guido van Rossum98297ee2007-11-06 21:34:58 +0000748 stdin=subprocess.PIPE,
749 stdout=subprocess.PIPE,
750 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000751 self.addCleanup(p.stdout.close)
752 self.addCleanup(p.stderr.close)
753 self.addCleanup(p.stdin.close)
Charles-François Natali2d517212011-05-29 16:36:44 +0200754 string_to_write = b"a" * support.PIPE_MAX_SIZE
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000755 (stdout, stderr) = p.communicate(string_to_write)
756 self.assertEqual(stdout, string_to_write)
757
758 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000759 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000760 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000761 'import sys,os;'
762 'sys.stdout.write(sys.stdin.read())'],
763 stdin=subprocess.PIPE,
764 stdout=subprocess.PIPE,
765 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000766 self.addCleanup(p.stdout.close)
767 self.addCleanup(p.stderr.close)
768 self.addCleanup(p.stdin.close)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000769 p.stdin.write(b"banana")
770 (stdout, stderr) = p.communicate(b"split")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000771 self.assertEqual(stdout, b"bananasplit")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000772 self.assertStderrEqual(stderr, b"")
Tim Peterse718f612004-10-12 21:51:32 +0000773
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000774 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000775 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000776 'import sys,os;' + SETBINARY +
Antoine Pitrouec2d2692012-08-05 00:23:40 +0200777 'buf = sys.stdout.buffer;'
778 'buf.write(sys.stdin.readline().encode());'
779 'buf.flush();'
780 'buf.write(b"line2\\n");'
781 'buf.flush();'
782 'buf.write(sys.stdin.read().encode());'
783 'buf.flush();'
784 'buf.write(b"line4\\n");'
785 'buf.flush();'
786 'buf.write(b"line5\\r\\n");'
787 'buf.flush();'
788 'buf.write(b"line6\\r");'
789 'buf.flush();'
790 'buf.write(b"\\nline7");'
791 'buf.flush();'
792 'buf.write(b"\\nline8");'],
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200793 stdin=subprocess.PIPE,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000794 stdout=subprocess.PIPE,
795 universal_newlines=1)
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200796 p.stdin.write("line1\n")
Antoine Pitrouc644e7c2014-05-09 00:24:50 +0200797 p.stdin.flush()
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200798 self.assertEqual(p.stdout.readline(), "line1\n")
799 p.stdin.write("line3\n")
800 p.stdin.close()
Brian Curtin3c6a9512010-11-05 03:58:52 +0000801 self.addCleanup(p.stdout.close)
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200802 self.assertEqual(p.stdout.readline(),
803 "line2\n")
804 self.assertEqual(p.stdout.read(6),
805 "line3\n")
806 self.assertEqual(p.stdout.read(),
807 "line4\nline5\nline6\nline7\nline8")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000808
809 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000810 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000811 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000812 'import sys,os;' + SETBINARY +
Antoine Pitrouec2d2692012-08-05 00:23:40 +0200813 'buf = sys.stdout.buffer;'
814 'buf.write(b"line2\\n");'
815 'buf.flush();'
816 'buf.write(b"line4\\n");'
817 'buf.flush();'
818 'buf.write(b"line5\\r\\n");'
819 'buf.flush();'
820 'buf.write(b"line6\\r");'
821 'buf.flush();'
822 'buf.write(b"\\nline7");'
823 'buf.flush();'
824 'buf.write(b"\\nline8");'],
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200825 stderr=subprocess.PIPE,
826 stdout=subprocess.PIPE,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000827 universal_newlines=1)
Brian Curtin19a53792010-11-05 17:09:05 +0000828 self.addCleanup(p.stdout.close)
829 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000830 (stdout, stderr) = p.communicate()
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200831 self.assertEqual(stdout,
832 "line2\nline4\nline5\nline6\nline7\nline8")
833
834 def test_universal_newlines_communicate_stdin(self):
835 # universal newlines through communicate(), with only stdin
836 p = subprocess.Popen([sys.executable, "-c",
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300837 'import sys,os;' + SETBINARY + textwrap.dedent('''
838 s = sys.stdin.readline()
839 assert s == "line1\\n", repr(s)
840 s = sys.stdin.read()
841 assert s == "line3\\n", repr(s)
842 ''')],
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200843 stdin=subprocess.PIPE,
844 universal_newlines=1)
845 (stdout, stderr) = p.communicate("line1\nline3\n")
846 self.assertEqual(p.returncode, 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000847
Andrew Svetlovf3765072012-08-14 18:35:17 +0300848 def test_universal_newlines_communicate_input_none(self):
849 # Test communicate(input=None) with universal newlines.
850 #
851 # We set stdout to PIPE because, as of this writing, a different
852 # code path is tested when the number of pipes is zero or one.
853 p = subprocess.Popen([sys.executable, "-c", "pass"],
854 stdin=subprocess.PIPE,
855 stdout=subprocess.PIPE,
856 universal_newlines=True)
857 p.communicate()
858 self.assertEqual(p.returncode, 0)
859
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300860 def test_universal_newlines_communicate_stdin_stdout_stderr(self):
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300861 # universal newlines through communicate(), with stdin, stdout, stderr
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300862 p = subprocess.Popen([sys.executable, "-c",
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300863 'import sys,os;' + SETBINARY + textwrap.dedent('''
864 s = sys.stdin.buffer.readline()
865 sys.stdout.buffer.write(s)
866 sys.stdout.buffer.write(b"line2\\r")
867 sys.stderr.buffer.write(b"eline2\\n")
868 s = sys.stdin.buffer.read()
869 sys.stdout.buffer.write(s)
870 sys.stdout.buffer.write(b"line4\\n")
871 sys.stdout.buffer.write(b"line5\\r\\n")
872 sys.stderr.buffer.write(b"eline6\\r")
873 sys.stderr.buffer.write(b"eline7\\r\\nz")
874 ''')],
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300875 stdin=subprocess.PIPE,
876 stderr=subprocess.PIPE,
877 stdout=subprocess.PIPE,
Andrew Svetlov47ec25d2012-08-19 16:25:37 +0300878 universal_newlines=True)
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300879 self.addCleanup(p.stdout.close)
880 self.addCleanup(p.stderr.close)
881 (stdout, stderr) = p.communicate("line1\nline3\n")
882 self.assertEqual(p.returncode, 0)
Andrew Svetlov943c5b32012-08-16 20:17:47 +0300883 self.assertEqual("line1\nline2\nline3\nline4\nline5\n", stdout)
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300884 # Python debug build push something like "[42442 refs]\n"
885 # to stderr at exit of subprocess.
Andrew Svetlov943c5b32012-08-16 20:17:47 +0300886 # Don't use assertStderrEqual because it strips CR and LF from output.
887 self.assertTrue(stderr.startswith("eline2\neline6\neline7\n"))
Andrew Svetlov5395d2f2012-08-15 22:46:43 +0300888
Andrew Svetlov82860712012-08-19 22:13:41 +0300889 def test_universal_newlines_communicate_encodings(self):
890 # Check that universal newlines mode works for various encodings,
891 # in particular for encodings in the UTF-16 and UTF-32 families.
892 # See issue #15595.
893 #
894 # UTF-16 and UTF-32-BE are sufficient to check both with BOM and
895 # without, and UTF-16 and UTF-32.
Antoine Pitroufd4722c2013-10-12 00:13:50 +0200896 import _bootlocale
Andrew Svetlov82860712012-08-19 22:13:41 +0300897 for encoding in ['utf-16', 'utf-32-be']:
Antoine Pitroufd4722c2013-10-12 00:13:50 +0200898 old_getpreferredencoding = _bootlocale.getpreferredencoding
Andrew Svetlov82860712012-08-19 22:13:41 +0300899 # Indirectly via io.TextIOWrapper, Popen() defaults to
900 # locale.getpreferredencoding(False) and earlier in Python 3.2 to
901 # locale.getpreferredencoding().
902 def getpreferredencoding(do_setlocale=True):
903 return encoding
904 code = ("import sys; "
905 r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" %
906 encoding)
907 args = [sys.executable, '-c', code]
908 try:
Antoine Pitroufd4722c2013-10-12 00:13:50 +0200909 _bootlocale.getpreferredencoding = getpreferredencoding
Andrew Svetlov82860712012-08-19 22:13:41 +0300910 # We set stdin to be non-None because, as of this writing,
911 # a different code path is used when the number of pipes is
912 # zero or one.
913 popen = subprocess.Popen(args, universal_newlines=True,
914 stdin=subprocess.PIPE,
915 stdout=subprocess.PIPE)
916 stdout, stderr = popen.communicate(input='')
917 finally:
Antoine Pitroufd4722c2013-10-12 00:13:50 +0200918 _bootlocale.getpreferredencoding = old_getpreferredencoding
Andrew Svetlov82860712012-08-19 22:13:41 +0300919 self.assertEqual(stdout, '1\n2\n3\n4')
920
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000921 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000922 # Make sure we leak no resources
Antoine Pitrou8db30272010-09-18 22:38:48 +0000923 if not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000924 max_handles = 1026 # too much for most UNIX systems
925 else:
Antoine Pitrou8db30272010-09-18 22:38:48 +0000926 max_handles = 2050 # too much for (at least some) Windows setups
927 handles = []
Gregory P. Smith81ce6852011-03-15 02:04:11 -0400928 tmpdir = tempfile.mkdtemp()
Antoine Pitrou8db30272010-09-18 22:38:48 +0000929 try:
930 for i in range(max_handles):
931 try:
Gregory P. Smith81ce6852011-03-15 02:04:11 -0400932 tmpfile = os.path.join(tmpdir, support.TESTFN)
933 handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT))
Antoine Pitrou8db30272010-09-18 22:38:48 +0000934 except OSError as e:
935 if e.errno != errno.EMFILE:
936 raise
937 break
938 else:
939 self.skipTest("failed to reach the file descriptor limit "
940 "(tried %d)" % max_handles)
941 # Close a couple of them (should be enough for a subprocess)
942 for i in range(10):
943 os.close(handles.pop())
944 # Loop creating some subprocesses. If one of them leaks some fds,
945 # the next loop iteration will fail by reaching the max fd limit.
946 for i in range(15):
947 p = subprocess.Popen([sys.executable, "-c",
948 "import sys;"
949 "sys.stdout.write(sys.stdin.read())"],
950 stdin=subprocess.PIPE,
951 stdout=subprocess.PIPE,
952 stderr=subprocess.PIPE)
953 data = p.communicate(b"lime")[0]
954 self.assertEqual(data, b"lime")
955 finally:
956 for h in handles:
957 os.close(h)
Gregory P. Smith81ce6852011-03-15 02:04:11 -0400958 shutil.rmtree(tmpdir)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000959
960 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000961 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
962 '"a b c" d e')
963 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
964 'ab\\"c \\ d')
Christian Heimesfdab48e2008-01-20 09:06:41 +0000965 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
966 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000967 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
968 'a\\\\\\b "de fg" h')
969 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
970 'a\\\\\\"b c d')
971 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
972 '"a\\\\b c" d e')
973 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
974 '"a\\\\b\\ c" d e')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000975 self.assertEqual(subprocess.list2cmdline(['ab', '']),
976 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000977
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000978 def test_poll(self):
Ross Lagerwallab66d2a2012-02-12 09:01:30 +0200979 p = subprocess.Popen([sys.executable, "-c",
Ross Lagerwalle7ad4192012-02-22 06:02:07 +0200980 "import os; os.read(0, 1)"],
981 stdin=subprocess.PIPE)
Ross Lagerwallab66d2a2012-02-12 09:01:30 +0200982 self.addCleanup(p.stdin.close)
983 self.assertIsNone(p.poll())
984 os.write(p.stdin.fileno(), b'A')
985 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000986 # Subsequent invocations should just return the returncode
987 self.assertEqual(p.poll(), 0)
988
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000989 def test_wait(self):
Ross Lagerwallab66d2a2012-02-12 09:01:30 +0200990 p = subprocess.Popen([sys.executable, "-c", "pass"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000991 self.assertEqual(p.wait(), 0)
992 # Subsequent invocations should just return the returncode
993 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000994
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400995 def test_wait_timeout(self):
996 p = subprocess.Popen([sys.executable,
Antoine Pitroudc49b2b2013-05-19 15:55:40 +0200997 "-c", "import time; time.sleep(0.3)"])
Reid Kleckner2b228f02011-03-16 16:57:54 -0400998 with self.assertRaises(subprocess.TimeoutExpired) as c:
Antoine Pitroudc49b2b2013-05-19 15:55:40 +0200999 p.wait(timeout=0.0001)
1000 self.assertIn("0.0001", str(c.exception)) # For coverage of __str__.
Reid Klecknerda9ac722011-03-16 17:08:21 -04001001 # Some heavily loaded buildbots (sparc Debian 3.x) require this much
1002 # time to start.
1003 self.assertEqual(p.wait(timeout=3), 0)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001004
Peter Astrand738131d2004-11-30 21:04:45 +00001005 def test_invalid_bufsize(self):
1006 # an invalid type of the bufsize argument should raise
1007 # TypeError.
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001008 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +00001009 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +00001010
Guido van Rossum46a05a72007-06-07 21:56:45 +00001011 def test_bufsize_is_none(self):
1012 # bufsize=None should be the same as bufsize=0.
1013 p = subprocess.Popen([sys.executable, "-c", "pass"], None)
1014 self.assertEqual(p.wait(), 0)
1015 # Again with keyword arg
1016 p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None)
1017 self.assertEqual(p.wait(), 0)
1018
Antoine Pitrouafe8d062014-09-21 21:10:56 +02001019 def _test_bufsize_equal_one(self, line, expected, universal_newlines):
1020 # subprocess may deadlock with bufsize=1, see issue #21332
1021 with subprocess.Popen([sys.executable, "-c", "import sys;"
1022 "sys.stdout.write(sys.stdin.readline());"
1023 "sys.stdout.flush()"],
1024 stdin=subprocess.PIPE,
1025 stdout=subprocess.PIPE,
1026 stderr=subprocess.DEVNULL,
1027 bufsize=1,
1028 universal_newlines=universal_newlines) as p:
1029 p.stdin.write(line) # expect that it flushes the line in text mode
1030 os.close(p.stdin.fileno()) # close it without flushing the buffer
1031 read_line = p.stdout.readline()
1032 try:
1033 p.stdin.close()
1034 except OSError:
1035 pass
1036 p.stdin = None
1037 self.assertEqual(p.returncode, 0)
1038 self.assertEqual(read_line, expected)
1039
1040 def test_bufsize_equal_one_text_mode(self):
1041 # line is flushed in text mode with bufsize=1.
1042 # we should get the full line in return
1043 line = "line\n"
1044 self._test_bufsize_equal_one(line, line, universal_newlines=True)
1045
1046 def test_bufsize_equal_one_binary_mode(self):
1047 # line is not flushed in binary mode with bufsize=1.
1048 # we should get empty response
1049 line = b'line' + os.linesep.encode() # assume ascii-based locale
1050 self._test_bufsize_equal_one(line, b'', universal_newlines=False)
1051
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001052 def test_leaking_fds_on_error(self):
1053 # see bug #5179: Popen leaks file descriptors to PIPEs if
1054 # the child fails to execute; this will eventually exhaust
1055 # the maximum number of open fds. 1024 seems a very common
1056 # value for that limit, but Windows has 2048, so we loop
1057 # 1024 times (each call leaked two fds).
1058 for i in range(1024):
Andrew Svetlov3438fa42012-12-17 23:35:18 +02001059 with self.assertRaises(OSError) as c:
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001060 subprocess.Popen(['nonexisting_i_hope'],
1061 stdout=subprocess.PIPE,
1062 stderr=subprocess.PIPE)
R David Murray384069c2011-03-13 22:26:53 -04001063 # ignore errors that indicate the command was not found
R David Murray6924bd72011-03-13 22:48:55 -04001064 if c.exception.errno not in (errno.ENOENT, errno.EACCES):
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001065 raise c.exception
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001066
Antoine Pitroua8392712013-08-30 23:38:13 +02001067 @unittest.skipIf(threading is None, "threading required")
1068 def test_double_close_on_error(self):
1069 # Issue #18851
1070 fds = []
1071 def open_fds():
1072 for i in range(20):
1073 fds.extend(os.pipe())
1074 time.sleep(0.001)
1075 t = threading.Thread(target=open_fds)
1076 t.start()
1077 try:
1078 with self.assertRaises(EnvironmentError):
1079 subprocess.Popen(['nonexisting_i_hope'],
1080 stdin=subprocess.PIPE,
1081 stdout=subprocess.PIPE,
1082 stderr=subprocess.PIPE)
1083 finally:
1084 t.join()
1085 exc = None
1086 for fd in fds:
1087 # If a double close occurred, some of those fds will
1088 # already have been closed by mistake, and os.close()
1089 # here will raise.
1090 try:
1091 os.close(fd)
1092 except OSError as e:
1093 exc = e
1094 if exc is not None:
1095 raise exc
1096
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001097 @unittest.skipIf(threading is None, "threading required")
1098 def test_threadsafe_wait(self):
1099 """Issue21291: Popen.wait() needs to be threadsafe for returncode."""
1100 proc = subprocess.Popen([sys.executable, '-c',
1101 'import time; time.sleep(12)'])
1102 self.assertEqual(proc.returncode, None)
1103 results = []
1104
1105 def kill_proc_timer_thread():
1106 results.append(('thread-start-poll-result', proc.poll()))
1107 # terminate it from the thread and wait for the result.
1108 proc.kill()
1109 proc.wait()
1110 results.append(('thread-after-kill-and-wait', proc.returncode))
1111 # this wait should be a no-op given the above.
1112 proc.wait()
1113 results.append(('thread-after-second-wait', proc.returncode))
1114
1115 # This is a timing sensitive test, the failure mode is
1116 # triggered when both the main thread and this thread are in
1117 # the wait() call at once. The delay here is to allow the
1118 # main thread to most likely be blocked in its wait() call.
1119 t = threading.Timer(0.2, kill_proc_timer_thread)
1120 t.start()
1121
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001122 if mswindows:
1123 expected_errorcode = 1
1124 else:
1125 # Should be -9 because of the proc.kill() from the thread.
1126 expected_errorcode = -9
1127
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001128 # Wait for the process to finish; the thread should kill it
1129 # long before it finishes on its own. Supplying a timeout
1130 # triggers a different code path for better coverage.
1131 proc.wait(timeout=20)
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001132 self.assertEqual(proc.returncode, expected_errorcode,
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001133 msg="unexpected result in wait from main thread")
1134
1135 # This should be a no-op with no change in returncode.
1136 proc.wait()
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001137 self.assertEqual(proc.returncode, expected_errorcode,
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001138 msg="unexpected result in second main wait.")
1139
1140 t.join()
1141 # Ensure that all of the thread results are as expected.
1142 # When a race condition occurs in wait(), the returncode could
1143 # be set by the wrong thread that doesn't actually have it
1144 # leading to an incorrect value.
1145 self.assertEqual([('thread-start-poll-result', None),
Gregory P. Smithab2719f2014-04-23 08:38:36 -07001146 ('thread-after-kill-and-wait', expected_errorcode),
1147 ('thread-after-second-wait', expected_errorcode)],
Gregory P. Smithd65ba512014-04-23 00:27:17 -07001148 results)
1149
Victor Stinnerb3693582010-05-21 20:13:12 +00001150 def test_issue8780(self):
1151 # Ensure that stdout is inherited from the parent
1152 # if stdout=PIPE is not used
1153 code = ';'.join((
1154 'import subprocess, sys',
1155 'retcode = subprocess.call('
1156 "[sys.executable, '-c', 'print(\"Hello World!\")'])",
1157 'assert retcode == 0'))
1158 output = subprocess.check_output([sys.executable, '-c', code])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001159 self.assertTrue(output.startswith(b'Hello World!'), ascii(output))
Victor Stinnerb3693582010-05-21 20:13:12 +00001160
Tim Goldenaf5ac392010-08-06 13:03:56 +00001161 def test_handles_closed_on_exception(self):
1162 # If CreateProcess exits with an error, ensure the
1163 # duplicate output handles are released
Berker Peksag16a1f282015-09-28 13:33:14 +03001164 ifhandle, ifname = tempfile.mkstemp()
1165 ofhandle, ofname = tempfile.mkstemp()
1166 efhandle, efname = tempfile.mkstemp()
Tim Goldenaf5ac392010-08-06 13:03:56 +00001167 try:
1168 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
1169 stderr=efhandle)
1170 except OSError:
1171 os.close(ifhandle)
1172 os.remove(ifname)
1173 os.close(ofhandle)
1174 os.remove(ofname)
1175 os.close(efhandle)
1176 os.remove(efname)
1177 self.assertFalse(os.path.exists(ifname))
1178 self.assertFalse(os.path.exists(ofname))
1179 self.assertFalse(os.path.exists(efname))
1180
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001181 def test_communicate_epipe(self):
1182 # Issue 10963: communicate() should hide EPIPE
1183 p = subprocess.Popen([sys.executable, "-c", 'pass'],
1184 stdin=subprocess.PIPE,
1185 stdout=subprocess.PIPE,
1186 stderr=subprocess.PIPE)
1187 self.addCleanup(p.stdout.close)
1188 self.addCleanup(p.stderr.close)
1189 self.addCleanup(p.stdin.close)
1190 p.communicate(b"x" * 2**20)
1191
1192 def test_communicate_epipe_only_stdin(self):
1193 # Issue 10963: communicate() should hide EPIPE
1194 p = subprocess.Popen([sys.executable, "-c", 'pass'],
1195 stdin=subprocess.PIPE)
1196 self.addCleanup(p.stdin.close)
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001197 p.wait()
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001198 p.communicate(b"x" * 2**20)
1199
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001200 @unittest.skipUnless(hasattr(signal, 'SIGUSR1'),
1201 "Requires signal.SIGUSR1")
1202 @unittest.skipUnless(hasattr(os, 'kill'),
1203 "Requires os.kill")
1204 @unittest.skipUnless(hasattr(os, 'getppid'),
1205 "Requires os.getppid")
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001206 def test_communicate_eintr(self):
1207 # Issue #12493: communicate() should handle EINTR
1208 def handler(signum, frame):
1209 pass
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001210 old_handler = signal.signal(signal.SIGUSR1, handler)
1211 self.addCleanup(signal.signal, signal.SIGUSR1, old_handler)
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001212
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001213 args = [sys.executable, "-c",
1214 'import os, signal;'
1215 'os.kill(os.getppid(), signal.SIGUSR1)']
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001216 for stream in ('stdout', 'stderr'):
1217 kw = {stream: subprocess.PIPE}
1218 with subprocess.Popen(args, **kw) as process:
Ross Lagerwallab66d2a2012-02-12 09:01:30 +02001219 # communicate() will be interrupted by SIGUSR1
Victor Stinner2cfb6f32011-07-05 14:00:56 +02001220 process.communicate()
1221
Tim Peterse718f612004-10-12 21:51:32 +00001222
Gregory P. Smith3d8e7762012-11-10 22:32:22 -08001223 # This test is Linux-ish specific for simplicity to at least have
1224 # some coverage. It is not a platform specific bug.
1225 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
1226 "Linux specific")
1227 def test_failed_child_execute_fd_leak(self):
1228 """Test for the fork() failure fd leak reported in issue16327."""
1229 fd_directory = '/proc/%d/fd' % os.getpid()
1230 fds_before_popen = os.listdir(fd_directory)
1231 with self.assertRaises(PopenTestException):
1232 PopenExecuteChildRaises(
1233 [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE,
1234 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1235
1236 # NOTE: This test doesn't verify that the real _execute_child
1237 # does not close the file descriptors itself on the way out
1238 # during an exception. Code inspection has confirmed that.
1239
1240 fds_after_exception = os.listdir(fd_directory)
1241 self.assertEqual(fds_before_popen, fds_after_exception)
1242
Gregory P. Smith6e730002015-04-14 16:14:25 -07001243
1244class RunFuncTestCase(BaseTestCase):
1245 def run_python(self, code, **kwargs):
1246 """Run Python code in a subprocess using subprocess.run"""
1247 argv = [sys.executable, "-c", code]
1248 return subprocess.run(argv, **kwargs)
1249
1250 def test_returncode(self):
1251 # call() function with sequence argument
1252 cp = self.run_python("import sys; sys.exit(47)")
1253 self.assertEqual(cp.returncode, 47)
1254 with self.assertRaises(subprocess.CalledProcessError):
1255 cp.check_returncode()
1256
1257 def test_check(self):
1258 with self.assertRaises(subprocess.CalledProcessError) as c:
1259 self.run_python("import sys; sys.exit(47)", check=True)
1260 self.assertEqual(c.exception.returncode, 47)
1261
1262 def test_check_zero(self):
1263 # check_returncode shouldn't raise when returncode is zero
1264 cp = self.run_python("import sys; sys.exit(0)", check=True)
1265 self.assertEqual(cp.returncode, 0)
1266
1267 def test_timeout(self):
1268 # run() function with timeout argument; we want to test that the child
1269 # process gets killed when the timeout expires. If the child isn't
1270 # killed, this call will deadlock since subprocess.run waits for the
1271 # child.
1272 with self.assertRaises(subprocess.TimeoutExpired):
1273 self.run_python("while True: pass", timeout=0.0001)
1274
1275 def test_capture_stdout(self):
1276 # capture stdout with zero return code
1277 cp = self.run_python("print('BDFL')", stdout=subprocess.PIPE)
1278 self.assertIn(b'BDFL', cp.stdout)
1279
1280 def test_capture_stderr(self):
1281 cp = self.run_python("import sys; sys.stderr.write('BDFL')",
1282 stderr=subprocess.PIPE)
1283 self.assertIn(b'BDFL', cp.stderr)
1284
1285 def test_check_output_stdin_arg(self):
1286 # run() can be called with stdin set to a file
1287 tf = tempfile.TemporaryFile()
1288 self.addCleanup(tf.close)
1289 tf.write(b'pear')
1290 tf.seek(0)
1291 cp = self.run_python(
1292 "import sys; sys.stdout.write(sys.stdin.read().upper())",
1293 stdin=tf, stdout=subprocess.PIPE)
1294 self.assertIn(b'PEAR', cp.stdout)
1295
1296 def test_check_output_input_arg(self):
1297 # check_output() can be called with input set to a string
1298 cp = self.run_python(
1299 "import sys; sys.stdout.write(sys.stdin.read().upper())",
1300 input=b'pear', stdout=subprocess.PIPE)
1301 self.assertIn(b'PEAR', cp.stdout)
1302
1303 def test_check_output_stdin_with_input_arg(self):
1304 # run() refuses to accept 'stdin' with 'input'
1305 tf = tempfile.TemporaryFile()
1306 self.addCleanup(tf.close)
1307 tf.write(b'pear')
1308 tf.seek(0)
1309 with self.assertRaises(ValueError,
1310 msg="Expected ValueError when stdin and input args supplied.") as c:
1311 output = self.run_python("print('will not be run')",
1312 stdin=tf, input=b'hare')
1313 self.assertIn('stdin', c.exception.args[0])
1314 self.assertIn('input', c.exception.args[0])
1315
1316 def test_check_output_timeout(self):
1317 with self.assertRaises(subprocess.TimeoutExpired) as c:
1318 cp = self.run_python((
1319 "import sys, time\n"
1320 "sys.stdout.write('BDFL')\n"
1321 "sys.stdout.flush()\n"
1322 "time.sleep(3600)"),
1323 # Some heavily loaded buildbots (sparc Debian 3.x) require
1324 # this much time to start and print.
1325 timeout=3, stdout=subprocess.PIPE)
1326 self.assertEqual(c.exception.output, b'BDFL')
1327 # output is aliased to stdout
1328 self.assertEqual(c.exception.stdout, b'BDFL')
1329
1330 def test_run_kwargs(self):
1331 newenv = os.environ.copy()
1332 newenv["FRUIT"] = "banana"
1333 cp = self.run_python(('import sys, os;'
1334 'sys.exit(33 if os.getenv("FRUIT")=="banana" else 31)'),
1335 env=newenv)
1336 self.assertEqual(cp.returncode, 33)
1337
1338
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001339@unittest.skipIf(mswindows, "POSIX specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +00001340class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001341
Gregory P. Smith5591b022012-10-10 03:34:47 -07001342 def setUp(self):
1343 super().setUp()
1344 self._nonexistent_dir = "/_this/pa.th/does/not/exist"
1345
1346 def _get_chdir_exception(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001347 try:
Gregory P. Smith5591b022012-10-10 03:34:47 -07001348 os.chdir(self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001349 except OSError as e:
1350 # This avoids hard coding the errno value or the OS perror()
1351 # string and instead capture the exception that we want to see
1352 # below for comparison.
1353 desired_exception = e
Gregory P. Smith5591b022012-10-10 03:34:47 -07001354 desired_exception.strerror += ': ' + repr(self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001355 else:
1356 self.fail("chdir to nonexistant directory %s succeeded." %
Gregory P. Smith5591b022012-10-10 03:34:47 -07001357 self._nonexistent_dir)
1358 return desired_exception
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001359
Gregory P. Smith5591b022012-10-10 03:34:47 -07001360 def test_exception_cwd(self):
1361 """Test error in the child raised in the parent for a bad cwd."""
1362 desired_exception = self._get_chdir_exception()
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001363 try:
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001364 p = subprocess.Popen([sys.executable, "-c", ""],
Gregory P. Smith5591b022012-10-10 03:34:47 -07001365 cwd=self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001366 except OSError as e:
1367 # Test that the child process chdir failure actually makes
1368 # it up to the parent process as the correct exception.
1369 self.assertEqual(desired_exception.errno, e.errno)
1370 self.assertEqual(desired_exception.strerror, e.strerror)
1371 else:
1372 self.fail("Expected OSError: %s" % desired_exception)
1373
Gregory P. Smith5591b022012-10-10 03:34:47 -07001374 def test_exception_bad_executable(self):
1375 """Test error in the child raised in the parent for a bad executable."""
1376 desired_exception = self._get_chdir_exception()
1377 try:
1378 p = subprocess.Popen([sys.executable, "-c", ""],
1379 executable=self._nonexistent_dir)
1380 except OSError as e:
1381 # Test that the child process exec failure actually makes
1382 # it up to the parent process as the correct exception.
1383 self.assertEqual(desired_exception.errno, e.errno)
1384 self.assertEqual(desired_exception.strerror, e.strerror)
1385 else:
1386 self.fail("Expected OSError: %s" % desired_exception)
1387
1388 def test_exception_bad_args_0(self):
1389 """Test error in the child raised in the parent for a bad args[0]."""
1390 desired_exception = self._get_chdir_exception()
1391 try:
1392 p = subprocess.Popen([self._nonexistent_dir, "-c", ""])
1393 except OSError as e:
1394 # Test that the child process exec failure actually makes
1395 # it up to the parent process as the correct exception.
1396 self.assertEqual(desired_exception.errno, e.errno)
1397 self.assertEqual(desired_exception.strerror, e.strerror)
1398 else:
1399 self.fail("Expected OSError: %s" % desired_exception)
1400
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001401 def test_restore_signals(self):
1402 # Code coverage for both values of restore_signals to make sure it
1403 # at least does not blow up.
1404 # A test for behavior would be complex. Contributions welcome.
1405 subprocess.call([sys.executable, "-c", ""], restore_signals=True)
1406 subprocess.call([sys.executable, "-c", ""], restore_signals=False)
1407
1408 def test_start_new_session(self):
1409 # For code coverage of calling setsid(). We don't care if we get an
1410 # EPERM error from it depending on the test execution environment, that
1411 # still indicates that it was called.
1412 try:
1413 output = subprocess.check_output(
1414 [sys.executable, "-c",
1415 "import os; print(os.getpgid(os.getpid()))"],
1416 start_new_session=True)
1417 except OSError as e:
1418 if e.errno != errno.EPERM:
1419 raise
1420 else:
1421 parent_pgid = os.getpgid(os.getpid())
1422 child_pgid = int(output)
1423 self.assertNotEqual(parent_pgid, child_pgid)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001424
1425 def test_run_abort(self):
1426 # returncode handles signal termination
Antoine Pitrou77e904e2013-10-08 23:04:32 +02001427 with support.SuppressCrashReport():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001428 p = subprocess.Popen([sys.executable, "-c",
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001429 'import os; os.abort()'])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001430 p.wait()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001431 self.assertEqual(-p.returncode, signal.SIGABRT)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001432
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001433 def test_preexec(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001434 # DISCLAIMER: Setting environment variables is *not* a good use
1435 # of a preexec_fn. This is merely a test.
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001436 p = subprocess.Popen([sys.executable, "-c",
1437 'import sys,os;'
1438 'sys.stdout.write(os.getenv("FRUIT"))'],
1439 stdout=subprocess.PIPE,
1440 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Brian Curtin3c6a9512010-11-05 03:58:52 +00001441 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001442 self.assertEqual(p.stdout.read(), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001443
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001444 def test_preexec_exception(self):
1445 def raise_it():
1446 raise ValueError("What if two swallows carried a coconut?")
1447 try:
1448 p = subprocess.Popen([sys.executable, "-c", ""],
1449 preexec_fn=raise_it)
Gregory P. Smith8d07c262012-11-10 23:53:47 -08001450 except subprocess.SubprocessError as e:
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001451 self.assertTrue(
1452 subprocess._posixsubprocess,
1453 "Expected a ValueError from the preexec_fn")
1454 except ValueError as e:
1455 self.assertIn("coconut", e.args[0])
1456 else:
1457 self.fail("Exception raised by preexec_fn did not make it "
1458 "to the parent process.")
1459
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001460 class _TestExecuteChildPopen(subprocess.Popen):
1461 """Used to test behavior at the end of _execute_child."""
1462 def __init__(self, testcase, *args, **kwargs):
1463 self._testcase = testcase
1464 subprocess.Popen.__init__(self, *args, **kwargs)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001465
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001466 def _execute_child(self, *args, **kwargs):
Gregory P. Smith12489d92012-11-11 01:37:02 -08001467 try:
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001468 subprocess.Popen._execute_child(self, *args, **kwargs)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001469 finally:
1470 # Open a bunch of file descriptors and verify that
1471 # none of them are the same as the ones the Popen
1472 # instance is using for stdin/stdout/stderr.
1473 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
1474 for _ in range(8)]
1475 try:
1476 for fd in devzero_fds:
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001477 self._testcase.assertNotIn(
1478 fd, (self.stdin.fileno(), self.stdout.fileno(),
1479 self.stderr.fileno()),
Gregory P. Smith12489d92012-11-11 01:37:02 -08001480 msg="At least one fd was closed early.")
1481 finally:
Richard Oudkerk0e547b62013-06-10 16:29:19 +01001482 for fd in devzero_fds:
1483 os.close(fd)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001484
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001485 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
1486 def test_preexec_errpipe_does_not_double_close_pipes(self):
1487 """Issue16140: Don't double close pipes on preexec error."""
1488
1489 def raise_it():
Gregory P. Smith65ee6ec2012-11-11 10:12:40 -08001490 raise subprocess.SubprocessError(
1491 "force the _execute_child() errpipe_data path.")
Gregory P. Smith12489d92012-11-11 01:37:02 -08001492
Gregory P. Smithc2c4cb62012-11-11 01:41:49 -08001493 with self.assertRaises(subprocess.SubprocessError):
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001494 self._TestExecuteChildPopen(
1495 self, [sys.executable, "-c", "pass"],
Gregory P. Smith12489d92012-11-11 01:37:02 -08001496 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1497 stderr=subprocess.PIPE, preexec_fn=raise_it)
1498
Gregory P. Smith32ec9da2010-03-19 16:53:08 +00001499 def test_preexec_gc_module_failure(self):
1500 # This tests the code that disables garbage collection if the child
1501 # process will execute any Python.
1502 def raise_runtime_error():
1503 raise RuntimeError("this shouldn't escape")
1504 enabled = gc.isenabled()
1505 orig_gc_disable = gc.disable
1506 orig_gc_isenabled = gc.isenabled
1507 try:
1508 gc.disable()
1509 self.assertFalse(gc.isenabled())
1510 subprocess.call([sys.executable, '-c', ''],
1511 preexec_fn=lambda: None)
1512 self.assertFalse(gc.isenabled(),
1513 "Popen enabled gc when it shouldn't.")
1514
1515 gc.enable()
1516 self.assertTrue(gc.isenabled())
1517 subprocess.call([sys.executable, '-c', ''],
1518 preexec_fn=lambda: None)
1519 self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
1520
1521 gc.disable = raise_runtime_error
1522 self.assertRaises(RuntimeError, subprocess.Popen,
1523 [sys.executable, '-c', ''],
1524 preexec_fn=lambda: None)
1525
1526 del gc.isenabled # force an AttributeError
1527 self.assertRaises(AttributeError, subprocess.Popen,
1528 [sys.executable, '-c', ''],
1529 preexec_fn=lambda: None)
1530 finally:
1531 gc.disable = orig_gc_disable
1532 gc.isenabled = orig_gc_isenabled
1533 if not enabled:
1534 gc.disable()
1535
Martin Panterf7fdbda2015-12-05 09:51:52 +00001536 @unittest.skipIf(
1537 sys.platform == 'darwin', 'setrlimit() seems to fail on OS X')
Martin Panterafdd5132015-11-30 02:21:41 +00001538 def test_preexec_fork_failure(self):
1539 # The internal code did not preserve the previous exception when
1540 # re-enabling garbage collection
1541 try:
1542 from resource import getrlimit, setrlimit, RLIMIT_NPROC
1543 except ImportError as err:
1544 self.skipTest(err) # RLIMIT_NPROC is specific to Linux and BSD
1545 limits = getrlimit(RLIMIT_NPROC)
1546 [_, hard] = limits
1547 setrlimit(RLIMIT_NPROC, (0, hard))
1548 self.addCleanup(setrlimit, RLIMIT_NPROC, limits)
Martin Panter5cf791b2015-12-11 05:40:14 +00001549 try:
Martin Panterafdd5132015-11-30 02:21:41 +00001550 subprocess.call([sys.executable, '-c', ''],
1551 preexec_fn=lambda: None)
Martin Panter5cf791b2015-12-11 05:40:14 +00001552 except BlockingIOError:
1553 # Forking should raise EAGAIN, translated to BlockingIOError
1554 pass
1555 else:
1556 self.skipTest('RLIMIT_NPROC had no effect; probably superuser')
Martin Panterafdd5132015-11-30 02:21:41 +00001557
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001558 def test_args_string(self):
1559 # args is a string
Berker Peksag16a1f282015-09-28 13:33:14 +03001560 fd, fname = tempfile.mkstemp()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001561 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +00001562 with open(fd, "w", errors="surrogateescape") as fobj:
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001563 fobj.write("#!/bin/sh\n")
1564 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
1565 sys.executable)
1566 os.chmod(fname, 0o700)
1567 p = subprocess.Popen(fname)
1568 p.wait()
1569 os.remove(fname)
1570 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001571
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001572 def test_invalid_args(self):
1573 # invalid arguments should raise ValueError
1574 self.assertRaises(ValueError, subprocess.call,
1575 [sys.executable, "-c",
1576 "import sys; sys.exit(47)"],
1577 startupinfo=47)
1578 self.assertRaises(ValueError, subprocess.call,
1579 [sys.executable, "-c",
1580 "import sys; sys.exit(47)"],
1581 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001582
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001583 def test_shell_sequence(self):
1584 # Run command through the shell (sequence)
1585 newenv = os.environ.copy()
1586 newenv["FRUIT"] = "apple"
1587 p = subprocess.Popen(["echo $FRUIT"], shell=1,
1588 stdout=subprocess.PIPE,
1589 env=newenv)
Brian Curtin3c6a9512010-11-05 03:58:52 +00001590 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001591 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001592
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001593 def test_shell_string(self):
1594 # Run command through the shell (string)
1595 newenv = os.environ.copy()
1596 newenv["FRUIT"] = "apple"
1597 p = subprocess.Popen("echo $FRUIT", shell=1,
1598 stdout=subprocess.PIPE,
1599 env=newenv)
Brian Curtin3c6a9512010-11-05 03:58:52 +00001600 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001601 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Christian Heimesa342c012008-04-20 21:01:16 +00001602
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001603 def test_call_string(self):
1604 # call() function with string argument on UNIX
Berker Peksag16a1f282015-09-28 13:33:14 +03001605 fd, fname = tempfile.mkstemp()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001606 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +00001607 with open(fd, "w", errors="surrogateescape") as fobj:
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001608 fobj.write("#!/bin/sh\n")
1609 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
1610 sys.executable)
1611 os.chmod(fname, 0o700)
1612 rc = subprocess.call(fname)
1613 os.remove(fname)
1614 self.assertEqual(rc, 47)
Christian Heimesa342c012008-04-20 21:01:16 +00001615
Stefan Krah9542cc62010-07-19 14:20:53 +00001616 def test_specific_shell(self):
1617 # Issue #9265: Incorrect name passed as arg[0].
1618 shells = []
1619 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
1620 for name in ['bash', 'ksh']:
1621 sh = os.path.join(prefix, name)
1622 if os.path.isfile(sh):
1623 shells.append(sh)
1624 if not shells: # Will probably work for any shell but csh.
1625 self.skipTest("bash or ksh required for this test")
1626 sh = '/bin/sh'
1627 if os.path.isfile(sh) and not os.path.islink(sh):
1628 # Test will fail if /bin/sh is a symlink to csh.
1629 shells.append(sh)
1630 for sh in shells:
1631 p = subprocess.Popen("echo $0", executable=sh, shell=True,
1632 stdout=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +00001633 self.addCleanup(p.stdout.close)
Stefan Krah9542cc62010-07-19 14:20:53 +00001634 self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
1635
Florent Xicluna4886d242010-03-08 13:27:26 +00001636 def _kill_process(self, method, *args):
Florent Xicluna1d8ee3a2010-03-05 20:26:54 +00001637 # Do not inherit file handles from the parent.
1638 # It should fix failures on some platforms.
Gregory P. Smithdee04342013-08-29 13:35:27 -07001639 # Also set the SIGINT handler to the default to make sure it's not
1640 # being ignored (some tests rely on that.)
1641 old_handler = signal.signal(signal.SIGINT, signal.default_int_handler)
1642 try:
1643 p = subprocess.Popen([sys.executable, "-c", """if 1:
1644 import sys, time
1645 sys.stdout.write('x\\n')
1646 sys.stdout.flush()
1647 time.sleep(30)
1648 """],
1649 close_fds=True,
1650 stdin=subprocess.PIPE,
1651 stdout=subprocess.PIPE,
1652 stderr=subprocess.PIPE)
1653 finally:
1654 signal.signal(signal.SIGINT, old_handler)
Antoine Pitrou3d8580f2010-09-20 01:33:21 +00001655 # Wait for the interpreter to be completely initialized before
1656 # sending any signal.
1657 p.stdout.read(1)
1658 getattr(p, method)(*args)
Florent Xicluna4886d242010-03-08 13:27:26 +00001659 return p
1660
Charles-François Natali53221e32013-01-12 16:52:20 +01001661 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
1662 "Due to known OS bug (issue #16762)")
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001663 def _kill_dead_process(self, method, *args):
1664 # Do not inherit file handles from the parent.
1665 # It should fix failures on some platforms.
1666 p = subprocess.Popen([sys.executable, "-c", """if 1:
1667 import sys, time
1668 sys.stdout.write('x\\n')
1669 sys.stdout.flush()
1670 """],
1671 close_fds=True,
1672 stdin=subprocess.PIPE,
1673 stdout=subprocess.PIPE,
1674 stderr=subprocess.PIPE)
1675 # Wait for the interpreter to be completely initialized before
1676 # sending any signal.
1677 p.stdout.read(1)
1678 # The process should end after this
1679 time.sleep(1)
1680 # This shouldn't raise even though the child is now dead
1681 getattr(p, method)(*args)
1682 p.communicate()
1683
Florent Xicluna4886d242010-03-08 13:27:26 +00001684 def test_send_signal(self):
1685 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunac049d872010-03-27 22:47:23 +00001686 _, stderr = p.communicate()
1687 self.assertIn(b'KeyboardInterrupt', stderr)
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001688 self.assertNotEqual(p.wait(), 0)
Christian Heimesa342c012008-04-20 21:01:16 +00001689
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001690 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001691 p = self._kill_process('kill')
Florent Xiclunac049d872010-03-27 22:47:23 +00001692 _, stderr = p.communicate()
1693 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001694 self.assertEqual(p.wait(), -signal.SIGKILL)
Tim Peterse718f612004-10-12 21:51:32 +00001695
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001696 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001697 p = self._kill_process('terminate')
Florent Xiclunac049d872010-03-27 22:47:23 +00001698 _, stderr = p.communicate()
1699 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001700 self.assertEqual(p.wait(), -signal.SIGTERM)
1701
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001702 def test_send_signal_dead(self):
1703 # Sending a signal to a dead process
1704 self._kill_dead_process('send_signal', signal.SIGINT)
1705
1706 def test_kill_dead(self):
1707 # Killing a dead process
1708 self._kill_dead_process('kill')
1709
1710 def test_terminate_dead(self):
1711 # Terminating a dead process
1712 self._kill_dead_process('terminate')
1713
Victor Stinnerdaf45552013-08-28 00:53:59 +02001714 def _save_fds(self, save_fds):
1715 fds = []
1716 for fd in save_fds:
1717 inheritable = os.get_inheritable(fd)
1718 saved = os.dup(fd)
1719 fds.append((fd, saved, inheritable))
1720 return fds
1721
1722 def _restore_fds(self, fds):
1723 for fd, saved, inheritable in fds:
1724 os.dup2(saved, fd, inheritable=inheritable)
1725 os.close(saved)
1726
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001727 def check_close_std_fds(self, fds):
1728 # Issue #9905: test that subprocess pipes still work properly with
1729 # some standard fds closed
1730 stdin = 0
Victor Stinnerdaf45552013-08-28 00:53:59 +02001731 saved_fds = self._save_fds(fds)
1732 for fd, saved, inheritable in saved_fds:
1733 if fd == 0:
1734 stdin = saved
1735 break
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001736 try:
1737 for fd in fds:
1738 os.close(fd)
1739 out, err = subprocess.Popen([sys.executable, "-c",
1740 'import sys;'
1741 'sys.stdout.write("apple");'
1742 'sys.stdout.flush();'
1743 'sys.stderr.write("orange")'],
1744 stdin=stdin,
1745 stdout=subprocess.PIPE,
1746 stderr=subprocess.PIPE).communicate()
1747 err = support.strip_python_stderr(err)
1748 self.assertEqual((out, err), (b'apple', b'orange'))
1749 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02001750 self._restore_fds(saved_fds)
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001751
1752 def test_close_fd_0(self):
1753 self.check_close_std_fds([0])
1754
1755 def test_close_fd_1(self):
1756 self.check_close_std_fds([1])
1757
1758 def test_close_fd_2(self):
1759 self.check_close_std_fds([2])
1760
1761 def test_close_fds_0_1(self):
1762 self.check_close_std_fds([0, 1])
1763
1764 def test_close_fds_0_2(self):
1765 self.check_close_std_fds([0, 2])
1766
1767 def test_close_fds_1_2(self):
1768 self.check_close_std_fds([1, 2])
1769
1770 def test_close_fds_0_1_2(self):
1771 # Issue #10806: test that subprocess pipes still work properly with
1772 # all standard fds closed.
1773 self.check_close_std_fds([0, 1, 2])
1774
Gregory P. Smith53dd8162013-12-01 16:03:24 -08001775 def test_small_errpipe_write_fd(self):
1776 """Issue #15798: Popen should work when stdio fds are available."""
1777 new_stdin = os.dup(0)
1778 new_stdout = os.dup(1)
1779 try:
1780 os.close(0)
1781 os.close(1)
1782
1783 # Side test: if errpipe_write fails to have its CLOEXEC
1784 # flag set this should cause the parent to think the exec
1785 # failed. Extremely unlikely: everyone supports CLOEXEC.
1786 subprocess.Popen([
1787 sys.executable, "-c",
1788 "print('AssertionError:0:CLOEXEC failure.')"]).wait()
1789 finally:
1790 # Restore original stdin and stdout
1791 os.dup2(new_stdin, 0)
1792 os.dup2(new_stdout, 1)
1793 os.close(new_stdin)
1794 os.close(new_stdout)
1795
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00001796 def test_remapping_std_fds(self):
1797 # open up some temporary files
Berker Peksag16a1f282015-09-28 13:33:14 +03001798 temps = [tempfile.mkstemp() for i in range(3)]
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00001799 try:
1800 temp_fds = [fd for fd, fname in temps]
1801
1802 # unlink the files -- we won't need to reopen them
1803 for fd, fname in temps:
1804 os.unlink(fname)
1805
1806 # write some data to what will become stdin, and rewind
1807 os.write(temp_fds[1], b"STDIN")
1808 os.lseek(temp_fds[1], 0, 0)
1809
1810 # move the standard file descriptors out of the way
Victor Stinnerdaf45552013-08-28 00:53:59 +02001811 saved_fds = self._save_fds(range(3))
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00001812 try:
1813 # duplicate the file objects over the standard fd's
1814 for fd, temp_fd in enumerate(temp_fds):
1815 os.dup2(temp_fd, fd)
1816
1817 # now use those files in the "wrong" order, so that subprocess
1818 # has to rearrange them in the child
1819 p = subprocess.Popen([sys.executable, "-c",
1820 'import sys; got = sys.stdin.read();'
1821 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
1822 stdin=temp_fds[1],
1823 stdout=temp_fds[2],
1824 stderr=temp_fds[0])
1825 p.wait()
1826 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02001827 self._restore_fds(saved_fds)
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00001828
1829 for fd in temp_fds:
1830 os.lseek(fd, 0, 0)
1831
1832 out = os.read(temp_fds[2], 1024)
1833 err = support.strip_python_stderr(os.read(temp_fds[0], 1024))
1834 self.assertEqual(out, b"got STDIN")
1835 self.assertEqual(err, b"err")
1836
1837 finally:
1838 for fd in temp_fds:
1839 os.close(fd)
1840
Ross Lagerwalld98646e2011-07-27 07:16:31 +02001841 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
1842 # open up some temporary files
Berker Peksag16a1f282015-09-28 13:33:14 +03001843 temps = [tempfile.mkstemp() for i in range(3)]
Ross Lagerwalld98646e2011-07-27 07:16:31 +02001844 temp_fds = [fd for fd, fname in temps]
1845 try:
1846 # unlink the files -- we won't need to reopen them
1847 for fd, fname in temps:
1848 os.unlink(fname)
1849
1850 # save a copy of the standard file descriptors
Victor Stinnerdaf45552013-08-28 00:53:59 +02001851 saved_fds = self._save_fds(range(3))
Ross Lagerwalld98646e2011-07-27 07:16:31 +02001852 try:
1853 # duplicate the temp files over the standard fd's 0, 1, 2
1854 for fd, temp_fd in enumerate(temp_fds):
1855 os.dup2(temp_fd, fd)
1856
1857 # write some data to what will become stdin, and rewind
1858 os.write(stdin_no, b"STDIN")
1859 os.lseek(stdin_no, 0, 0)
1860
1861 # now use those files in the given order, so that subprocess
1862 # has to rearrange them in the child
1863 p = subprocess.Popen([sys.executable, "-c",
1864 'import sys; got = sys.stdin.read();'
1865 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
1866 stdin=stdin_no,
1867 stdout=stdout_no,
1868 stderr=stderr_no)
1869 p.wait()
1870
1871 for fd in temp_fds:
1872 os.lseek(fd, 0, 0)
1873
1874 out = os.read(stdout_no, 1024)
1875 err = support.strip_python_stderr(os.read(stderr_no, 1024))
1876 finally:
Victor Stinnerdaf45552013-08-28 00:53:59 +02001877 self._restore_fds(saved_fds)
Ross Lagerwalld98646e2011-07-27 07:16:31 +02001878
1879 self.assertEqual(out, b"got STDIN")
1880 self.assertEqual(err, b"err")
1881
1882 finally:
1883 for fd in temp_fds:
1884 os.close(fd)
1885
1886 # When duping fds, if there arises a situation where one of the fds is
1887 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
1888 # This tests all combinations of this.
1889 def test_swap_fds(self):
1890 self.check_swap_fds(0, 1, 2)
1891 self.check_swap_fds(0, 2, 1)
1892 self.check_swap_fds(1, 0, 2)
1893 self.check_swap_fds(1, 2, 0)
1894 self.check_swap_fds(2, 0, 1)
1895 self.check_swap_fds(2, 1, 0)
1896
Victor Stinner13bb71c2010-04-23 21:41:56 +00001897 def test_surrogates_error_message(self):
Victor Stinner4d078042010-04-23 19:28:32 +00001898 def prepare():
1899 raise ValueError("surrogate:\uDCff")
1900
1901 try:
1902 subprocess.call(
1903 [sys.executable, "-c", "pass"],
1904 preexec_fn=prepare)
1905 except ValueError as err:
1906 # Pure Python implementations keeps the message
1907 self.assertIsNone(subprocess._posixsubprocess)
1908 self.assertEqual(str(err), "surrogate:\uDCff")
Gregory P. Smith8d07c262012-11-10 23:53:47 -08001909 except subprocess.SubprocessError as err:
Victor Stinner4d078042010-04-23 19:28:32 +00001910 # _posixsubprocess uses a default message
1911 self.assertIsNotNone(subprocess._posixsubprocess)
1912 self.assertEqual(str(err), "Exception occurred in preexec_fn.")
1913 else:
Gregory P. Smith8d07c262012-11-10 23:53:47 -08001914 self.fail("Expected ValueError or subprocess.SubprocessError")
Victor Stinner4d078042010-04-23 19:28:32 +00001915
Victor Stinner13bb71c2010-04-23 21:41:56 +00001916 def test_undecodable_env(self):
1917 for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')):
Victor Stinner5323fb02013-11-19 23:46:06 +01001918 encoded_value = value.encode("ascii", "surrogateescape")
1919
Victor Stinner13bb71c2010-04-23 21:41:56 +00001920 # test str with surrogates
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00001921 script = "import os; print(ascii(os.getenv(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00001922 env = os.environ.copy()
1923 env[key] = value
Victor Stinner5323fb02013-11-19 23:46:06 +01001924 # Use C locale to get ASCII for the locale encoding to force
Victor Stinner89f3ad12010-10-14 10:43:31 +00001925 # surrogate-escaping of \xFF in the child process; otherwise it can
1926 # be decoded as-is if the default locale is latin-1.
Victor Stinnerebc78d22010-10-14 10:38:17 +00001927 env['LC_ALL'] = 'C'
Victor Stinner5323fb02013-11-19 23:46:06 +01001928 if sys.platform.startswith("aix"):
1929 # On AIX, the C locale uses the Latin1 encoding
1930 decoded_value = encoded_value.decode("latin1", "surrogateescape")
1931 else:
1932 # On other UNIXes, the C locale uses the ASCII encoding
1933 decoded_value = value
Victor Stinner13bb71c2010-04-23 21:41:56 +00001934 stdout = subprocess.check_output(
1935 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00001936 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00001937 stdout = stdout.rstrip(b'\n\r')
Victor Stinner5323fb02013-11-19 23:46:06 +01001938 self.assertEqual(stdout.decode('ascii'), ascii(decoded_value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00001939
1940 # test bytes
1941 key = key.encode("ascii", "surrogateescape")
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00001942 script = "import os; print(ascii(os.getenvb(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00001943 env = os.environ.copy()
Victor Stinner5323fb02013-11-19 23:46:06 +01001944 env[key] = encoded_value
Victor Stinner13bb71c2010-04-23 21:41:56 +00001945 stdout = subprocess.check_output(
1946 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00001947 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00001948 stdout = stdout.rstrip(b'\n\r')
Victor Stinner5323fb02013-11-19 23:46:06 +01001949 self.assertEqual(stdout.decode('ascii'), ascii(encoded_value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00001950
Victor Stinnerb745a742010-05-18 17:17:23 +00001951 def test_bytes_program(self):
1952 abs_program = os.fsencode(sys.executable)
1953 path, program = os.path.split(sys.executable)
1954 program = os.fsencode(program)
1955
1956 # absolute bytes path
1957 exitcode = subprocess.call([abs_program, "-c", "pass"])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001958 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00001959
Victor Stinner7b3b20a2011-03-03 12:54:05 +00001960 # absolute bytes path as a string
1961 cmd = b"'" + abs_program + b"' -c pass"
1962 exitcode = subprocess.call(cmd, shell=True)
1963 self.assertEqual(exitcode, 0)
1964
Victor Stinnerb745a742010-05-18 17:17:23 +00001965 # bytes program, unicode PATH
1966 env = os.environ.copy()
1967 env["PATH"] = path
1968 exitcode = subprocess.call([program, "-c", "pass"], env=env)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001969 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00001970
1971 # bytes program, bytes PATH
1972 envb = os.environb.copy()
1973 envb[b"PATH"] = os.fsencode(path)
1974 exitcode = subprocess.call([program, "-c", "pass"], env=envb)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001975 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00001976
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001977 def test_pipe_cloexec(self):
1978 sleeper = support.findfile("input_reader.py", subdir="subprocessdata")
1979 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
1980
1981 p1 = subprocess.Popen([sys.executable, sleeper],
1982 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1983 stderr=subprocess.PIPE, close_fds=False)
1984
1985 self.addCleanup(p1.communicate, b'')
1986
1987 p2 = subprocess.Popen([sys.executable, fd_status],
1988 stdout=subprocess.PIPE, close_fds=False)
1989
1990 output, error = p2.communicate()
1991 result_fds = set(map(int, output.split(b',')))
1992 unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(),
1993 p1.stderr.fileno()])
1994
1995 self.assertFalse(result_fds & unwanted_fds,
1996 "Expected no fds from %r to be open in child, "
1997 "found %r" %
1998 (unwanted_fds, result_fds & unwanted_fds))
1999
2000 def test_pipe_cloexec_real_tools(self):
2001 qcat = support.findfile("qcat.py", subdir="subprocessdata")
2002 qgrep = support.findfile("qgrep.py", subdir="subprocessdata")
2003
2004 subdata = b'zxcvbn'
2005 data = subdata * 4 + b'\n'
2006
2007 p1 = subprocess.Popen([sys.executable, qcat],
2008 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2009 close_fds=False)
2010
2011 p2 = subprocess.Popen([sys.executable, qgrep, subdata],
2012 stdin=p1.stdout, stdout=subprocess.PIPE,
2013 close_fds=False)
2014
2015 self.addCleanup(p1.wait)
2016 self.addCleanup(p2.wait)
Gregory P. Smith886455c2012-01-21 22:05:10 -08002017 def kill_p1():
2018 try:
2019 p1.terminate()
2020 except ProcessLookupError:
2021 pass
2022 def kill_p2():
2023 try:
2024 p2.terminate()
2025 except ProcessLookupError:
2026 pass
2027 self.addCleanup(kill_p1)
2028 self.addCleanup(kill_p2)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002029
2030 p1.stdin.write(data)
2031 p1.stdin.close()
2032
2033 readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10)
2034
2035 self.assertTrue(readfiles, "The child hung")
2036 self.assertEqual(p2.stdout.read(), data)
2037
Victor Stinnerfaa8c132011-01-03 16:36:00 +00002038 p1.stdout.close()
2039 p2.stdout.close()
2040
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002041 def test_close_fds(self):
2042 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2043
2044 fds = os.pipe()
2045 self.addCleanup(os.close, fds[0])
2046 self.addCleanup(os.close, fds[1])
2047
2048 open_fds = set(fds)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002049 # add a bunch more fds
2050 for _ in range(9):
Serhiy Storchaka85c30332015-02-15 13:58:23 +02002051 fd = os.open(os.devnull, os.O_RDONLY)
Gregory P. Smith8facece2012-01-21 14:01:08 -08002052 self.addCleanup(os.close, fd)
2053 open_fds.add(fd)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002054
Victor Stinnerdaf45552013-08-28 00:53:59 +02002055 for fd in open_fds:
2056 os.set_inheritable(fd, True)
2057
Gregory P. Smith51ee2702010-12-13 07:59:39 +00002058 p = subprocess.Popen([sys.executable, fd_status],
2059 stdout=subprocess.PIPE, close_fds=False)
2060 output, ignored = p.communicate()
2061 remaining_fds = set(map(int, output.split(b',')))
2062
2063 self.assertEqual(remaining_fds & open_fds, open_fds,
2064 "Some fds were closed")
2065
2066 p = subprocess.Popen([sys.executable, fd_status],
2067 stdout=subprocess.PIPE, close_fds=True)
2068 output, ignored = p.communicate()
2069 remaining_fds = set(map(int, output.split(b',')))
2070
2071 self.assertFalse(remaining_fds & open_fds,
2072 "Some fds were left open")
2073 self.assertIn(1, remaining_fds, "Subprocess failed")
2074
Gregory P. Smith8facece2012-01-21 14:01:08 -08002075 # Keep some of the fd's we opened open in the subprocess.
2076 # This tests _posixsubprocess.c's proper handling of fds_to_keep.
2077 fds_to_keep = set(open_fds.pop() for _ in range(8))
2078 p = subprocess.Popen([sys.executable, fd_status],
2079 stdout=subprocess.PIPE, close_fds=True,
2080 pass_fds=())
2081 output, ignored = p.communicate()
2082 remaining_fds = set(map(int, output.split(b',')))
2083
2084 self.assertFalse(remaining_fds & fds_to_keep & open_fds,
2085 "Some fds not in pass_fds were left open")
2086 self.assertIn(1, remaining_fds, "Subprocess failed")
2087
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002088
Gregory P. Smithd04f6992014-06-01 15:27:28 -07002089 @unittest.skipIf(sys.platform.startswith("freebsd") and
2090 os.stat("/dev").st_dev == os.stat("/dev/fd").st_dev,
2091 "Requires fdescfs mounted on /dev/fd on FreeBSD.")
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002092 def test_close_fds_when_max_fd_is_lowered(self):
2093 """Confirm that issue21618 is fixed (may fail under valgrind)."""
2094 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2095
Gregory P. Smith634aa682014-06-15 17:51:04 -07002096 # This launches the meat of the test in a child process to
2097 # avoid messing with the larger unittest processes maximum
2098 # number of file descriptors.
2099 # This process launches:
2100 # +--> Process that lowers its RLIMIT_NOFILE aftr setting up
2101 # a bunch of high open fds above the new lower rlimit.
2102 # Those are reported via stdout before launching a new
2103 # process with close_fds=False to run the actual test:
2104 # +--> The TEST: This one launches a fd_status.py
2105 # subprocess with close_fds=True so we can find out if
2106 # any of the fds above the lowered rlimit are still open.
2107 p = subprocess.Popen([sys.executable, '-c', textwrap.dedent(
2108 '''
2109 import os, resource, subprocess, sys, textwrap
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002110 open_fds = set()
2111 # Add a bunch more fds to pass down.
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002112 for _ in range(40):
Serhiy Storchaka85c30332015-02-15 13:58:23 +02002113 fd = os.open(os.devnull, os.O_RDONLY)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002114 open_fds.add(fd)
2115
2116 # Leave a two pairs of low ones available for use by the
2117 # internal child error pipe and the stdout pipe.
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002118 # We also leave 10 more open as some Python buildbots run into
2119 # "too many open files" errors during the test if we do not.
2120 for fd in sorted(open_fds)[:14]:
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002121 os.close(fd)
2122 open_fds.remove(fd)
2123
2124 for fd in open_fds:
Gregory P. Smith634aa682014-06-15 17:51:04 -07002125 #self.addCleanup(os.close, fd)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002126 os.set_inheritable(fd, True)
2127
2128 max_fd_open = max(open_fds)
2129
Gregory P. Smith634aa682014-06-15 17:51:04 -07002130 # Communicate the open_fds to the parent unittest.TestCase process.
2131 print(','.join(map(str, sorted(open_fds))))
2132 sys.stdout.flush()
2133
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002134 rlim_cur, rlim_max = resource.getrlimit(resource.RLIMIT_NOFILE)
2135 try:
Gregory P. Smith8fed4de2014-06-01 15:15:44 -07002136 # 29 is lower than the highest fds we are leaving open.
2137 resource.setrlimit(resource.RLIMIT_NOFILE, (29, rlim_max))
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002138 # Launch a new Python interpreter with our low fd rlim_cur that
2139 # inherits open fds above that limit. It then uses subprocess
2140 # with close_fds=True to get a report of open fds in the child.
2141 # An explicit list of fds to check is passed to fd_status.py as
2142 # letting fd_status rely on its default logic would miss the
2143 # fds above rlim_cur as it normally only checks up to that limit.
Gregory P. Smith634aa682014-06-15 17:51:04 -07002144 subprocess.Popen(
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002145 [sys.executable, '-c',
2146 textwrap.dedent("""
2147 import subprocess, sys
Gregory P. Smith634aa682014-06-15 17:51:04 -07002148 subprocess.Popen([sys.executable, %r] +
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002149 [str(x) for x in range({max_fd})],
Gregory P. Smithffd529c2014-06-01 13:46:54 -07002150 close_fds=True).wait()
Gregory P. Smith634aa682014-06-15 17:51:04 -07002151 """.format(max_fd=max_fd_open+1))],
2152 close_fds=False).wait()
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002153 finally:
2154 resource.setrlimit(resource.RLIMIT_NOFILE, (rlim_cur, rlim_max))
Gregory P. Smith634aa682014-06-15 17:51:04 -07002155 ''' % fd_status)], stdout=subprocess.PIPE)
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002156
2157 output, unused_stderr = p.communicate()
Gregory P. Smith634aa682014-06-15 17:51:04 -07002158 output_lines = output.splitlines()
2159 self.assertEqual(len(output_lines), 2,
Gregory P. Smith9204e092014-06-15 20:16:01 -07002160 msg="expected exactly two lines of output:\n%r" % output)
Gregory P. Smith634aa682014-06-15 17:51:04 -07002161 opened_fds = set(map(int, output_lines[0].strip().split(b',')))
2162 remaining_fds = set(map(int, output_lines[1].strip().split(b',')))
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002163
Gregory P. Smith634aa682014-06-15 17:51:04 -07002164 self.assertFalse(remaining_fds & opened_fds,
Gregory P. Smithd4dcb702014-06-01 13:18:28 -07002165 msg="Some fds were left open.")
2166
2167
Victor Stinner88701e22011-06-01 13:13:04 +02002168 # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
2169 # descriptor of a pipe closed in the parent process is valid in the
2170 # child process according to fstat(), but the mode of the file
2171 # descriptor is invalid, and read or write raise an error.
2172 @support.requires_mac_ver(10, 5)
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00002173 def test_pass_fds(self):
2174 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2175
2176 open_fds = set()
2177
2178 for x in range(5):
2179 fds = os.pipe()
2180 self.addCleanup(os.close, fds[0])
2181 self.addCleanup(os.close, fds[1])
Victor Stinnerdaf45552013-08-28 00:53:59 +02002182 os.set_inheritable(fds[0], True)
2183 os.set_inheritable(fds[1], True)
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00002184 open_fds.update(fds)
2185
2186 for fd in open_fds:
2187 p = subprocess.Popen([sys.executable, fd_status],
2188 stdout=subprocess.PIPE, close_fds=True,
2189 pass_fds=(fd, ))
2190 output, ignored = p.communicate()
2191
2192 remaining_fds = set(map(int, output.split(b',')))
2193 to_be_closed = open_fds - {fd}
2194
2195 self.assertIn(fd, remaining_fds, "fd to be passed not passed")
2196 self.assertFalse(remaining_fds & to_be_closed,
2197 "fd to be closed passed")
2198
2199 # pass_fds overrides close_fds with a warning.
2200 with self.assertWarns(RuntimeWarning) as context:
2201 self.assertFalse(subprocess.call(
2202 [sys.executable, "-c", "import sys; sys.exit(0)"],
2203 close_fds=False, pass_fds=(fd, )))
2204 self.assertIn('overriding close_fds', str(context.warning))
2205
Victor Stinnerdaf45552013-08-28 00:53:59 +02002206 def test_pass_fds_inheritable(self):
Victor Stinnerf6fa22e2013-09-01 10:22:41 +02002207 script = support.findfile("fd_status.py", subdir="subprocessdata")
Victor Stinnerdaf45552013-08-28 00:53:59 +02002208
2209 inheritable, non_inheritable = os.pipe()
2210 self.addCleanup(os.close, inheritable)
2211 self.addCleanup(os.close, non_inheritable)
2212 os.set_inheritable(inheritable, True)
2213 os.set_inheritable(non_inheritable, False)
2214 pass_fds = (inheritable, non_inheritable)
2215 args = [sys.executable, script]
2216 args += list(map(str, pass_fds))
2217
2218 p = subprocess.Popen(args,
2219 stdout=subprocess.PIPE, close_fds=True,
2220 pass_fds=pass_fds)
2221 output, ignored = p.communicate()
2222 fds = set(map(int, output.split(b',')))
2223
2224 # the inheritable file descriptor must be inherited, so its inheritable
2225 # flag must be set in the child process after fork() and before exec()
Victor Stinnerf6fa22e2013-09-01 10:22:41 +02002226 self.assertEqual(fds, set(pass_fds), "output=%a" % output)
Victor Stinnerdaf45552013-08-28 00:53:59 +02002227
2228 # inheritable flag must not be changed in the parent process
2229 self.assertEqual(os.get_inheritable(inheritable), True)
2230 self.assertEqual(os.get_inheritable(non_inheritable), False)
2231
Gregory P. Smith112bb3a2011-03-15 14:55:17 -04002232 def test_stdout_stdin_are_single_inout_fd(self):
2233 with io.open(os.devnull, "r+") as inout:
2234 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2235 stdout=inout, stdin=inout)
2236 p.wait()
2237
2238 def test_stdout_stderr_are_single_inout_fd(self):
2239 with io.open(os.devnull, "r+") as inout:
2240 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2241 stdout=inout, stderr=inout)
2242 p.wait()
2243
2244 def test_stderr_stdin_are_single_inout_fd(self):
2245 with io.open(os.devnull, "r+") as inout:
2246 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
2247 stderr=inout, stdin=inout)
2248 p.wait()
2249
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00002250 def test_wait_when_sigchild_ignored(self):
2251 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
2252 sigchild_ignore = support.findfile("sigchild_ignore.py",
2253 subdir="subprocessdata")
2254 p = subprocess.Popen([sys.executable, sigchild_ignore],
2255 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2256 stdout, stderr = p.communicate()
2257 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
Gregory P. Smitha80f4fb2010-12-14 15:23:02 +00002258 " non-zero with this error:\n%s" %
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00002259 stderr.decode('utf-8'))
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00002260
Antoine Pitrou7b98d022011-03-19 17:04:13 +01002261 def test_select_unbuffered(self):
2262 # Issue #11459: bufsize=0 should really set the pipes as
2263 # unbuffered (and therefore let select() work properly).
2264 select = support.import_module("select")
2265 p = subprocess.Popen([sys.executable, "-c",
2266 'import sys;'
2267 'sys.stdout.write("apple")'],
2268 stdout=subprocess.PIPE,
2269 bufsize=0)
2270 f = p.stdout
Ross Lagerwall17ace7a2011-03-26 21:21:46 +02002271 self.addCleanup(f.close)
Antoine Pitrou7b98d022011-03-19 17:04:13 +01002272 try:
2273 self.assertEqual(f.read(4), b"appl")
2274 self.assertIn(f, select.select([f], [], [], 0.0)[0])
2275 finally:
2276 p.wait()
2277
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002278 def test_zombie_fast_process_del(self):
2279 # Issue #12650: on Unix, if Popen.__del__() was called before the
2280 # process exited, it wouldn't be added to subprocess._active, and would
2281 # remain a zombie.
2282 # spawn a Popen, and delete its reference before it exits
2283 p = subprocess.Popen([sys.executable, "-c",
2284 'import sys, time;'
2285 'time.sleep(0.2)'],
2286 stdout=subprocess.PIPE,
2287 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02002288 self.addCleanup(p.stdout.close)
2289 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002290 ident = id(p)
2291 pid = p.pid
2292 del p
2293 # check that p is in the active processes list
2294 self.assertIn(ident, [id(o) for o in subprocess._active])
2295
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002296 def test_leak_fast_process_del_killed(self):
2297 # Issue #12650: on Unix, if Popen.__del__() was called before the
2298 # process exited, and the process got killed by a signal, it would never
2299 # be removed from subprocess._active, which triggered a FD and memory
2300 # leak.
2301 # spawn a Popen, delete its reference and kill it
2302 p = subprocess.Popen([sys.executable, "-c",
2303 'import time;'
2304 'time.sleep(3)'],
2305 stdout=subprocess.PIPE,
2306 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02002307 self.addCleanup(p.stdout.close)
2308 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002309 ident = id(p)
2310 pid = p.pid
2311 del p
2312 os.kill(pid, signal.SIGKILL)
2313 # check that p is in the active processes list
2314 self.assertIn(ident, [id(o) for o in subprocess._active])
2315
2316 # let some time for the process to exit, and create a new Popen: this
2317 # should trigger the wait() of p
2318 time.sleep(0.2)
Andrew Svetlov3438fa42012-12-17 23:35:18 +02002319 with self.assertRaises(OSError) as c:
Charles-François Natali134a8ba2011-08-18 18:49:39 +02002320 with subprocess.Popen(['nonexisting_i_hope'],
2321 stdout=subprocess.PIPE,
2322 stderr=subprocess.PIPE) as proc:
2323 pass
2324 # p should have been wait()ed on, and removed from the _active list
2325 self.assertRaises(OSError, os.waitpid, pid, 0)
2326 self.assertNotIn(ident, [id(o) for o in subprocess._active])
2327
Charles-François Natali249cdc32013-08-25 18:24:45 +02002328 def test_close_fds_after_preexec(self):
2329 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2330
2331 # this FD is used as dup2() target by preexec_fn, and should be closed
2332 # in the child process
2333 fd = os.dup(1)
2334 self.addCleanup(os.close, fd)
2335
2336 p = subprocess.Popen([sys.executable, fd_status],
2337 stdout=subprocess.PIPE, close_fds=True,
2338 preexec_fn=lambda: os.dup2(1, fd))
2339 output, ignored = p.communicate()
2340
2341 remaining_fds = set(map(int, output.split(b',')))
2342
2343 self.assertNotIn(fd, remaining_fds)
2344
Victor Stinner8f437aa2014-10-05 17:25:19 +02002345 @support.cpython_only
2346 def test_fork_exec(self):
2347 # Issue #22290: fork_exec() must not crash on memory allocation failure
2348 # or other errors
2349 import _posixsubprocess
2350 gc_enabled = gc.isenabled()
2351 try:
2352 # Use a preexec function and enable the garbage collector
2353 # to force fork_exec() to re-enable the garbage collector
2354 # on error.
2355 func = lambda: None
2356 gc.enable()
2357
Victor Stinner8f437aa2014-10-05 17:25:19 +02002358 for args, exe_list, cwd, env_list in (
2359 (123, [b"exe"], None, [b"env"]),
2360 ([b"arg"], 123, None, [b"env"]),
2361 ([b"arg"], [b"exe"], 123, [b"env"]),
2362 ([b"arg"], [b"exe"], None, 123),
2363 ):
2364 with self.assertRaises(TypeError):
2365 _posixsubprocess.fork_exec(
2366 args, exe_list,
2367 True, [], cwd, env_list,
2368 -1, -1, -1, -1,
2369 1, 2, 3, 4,
2370 True, True, func)
2371 finally:
2372 if not gc_enabled:
2373 gc.disable()
2374
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -08002375 @support.cpython_only
2376 def test_fork_exec_sorted_fd_sanity_check(self):
2377 # Issue #23564: sanity check the fork_exec() fds_to_keep sanity check.
2378 import _posixsubprocess
2379 gc_enabled = gc.isenabled()
2380 try:
2381 gc.enable()
2382
2383 for fds_to_keep in (
2384 (-1, 2, 3, 4, 5), # Negative number.
2385 ('str', 4), # Not an int.
2386 (18, 23, 42, 2**63), # Out of range.
2387 (5, 4), # Not sorted.
2388 (6, 7, 7, 8), # Duplicate.
2389 ):
2390 with self.assertRaises(
2391 ValueError,
2392 msg='fds_to_keep={}'.format(fds_to_keep)) as c:
2393 _posixsubprocess.fork_exec(
2394 [b"false"], [b"false"],
2395 True, fds_to_keep, None, [b"env"],
2396 -1, -1, -1, -1,
2397 1, 2, 3, 4,
2398 True, True, None)
2399 self.assertIn('fds_to_keep', str(c.exception))
2400 finally:
2401 if not gc_enabled:
2402 gc.disable()
Victor Stinner8f437aa2014-10-05 17:25:19 +02002403
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002404
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00002405@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +00002406class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00002407
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002408 def test_startupinfo(self):
2409 # startupinfo argument
2410 # We uses hardcoded constants, because we do not want to
2411 # depend on win32all.
2412 STARTF_USESHOWWINDOW = 1
2413 SW_MAXIMIZE = 3
2414 startupinfo = subprocess.STARTUPINFO()
2415 startupinfo.dwFlags = STARTF_USESHOWWINDOW
2416 startupinfo.wShowWindow = SW_MAXIMIZE
2417 # Since Python is a console process, it won't be affected
2418 # by wShowWindow, but the argument should be silently
2419 # ignored
2420 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002421 startupinfo=startupinfo)
2422
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002423 def test_creationflags(self):
2424 # creationflags argument
2425 CREATE_NEW_CONSOLE = 16
2426 sys.stderr.write(" a DOS box should flash briefly ...\n")
2427 subprocess.call(sys.executable +
2428 ' -c "import time; time.sleep(0.25)"',
2429 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002430
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002431 def test_invalid_args(self):
2432 # invalid arguments should raise ValueError
2433 self.assertRaises(ValueError, subprocess.call,
2434 [sys.executable, "-c",
2435 "import sys; sys.exit(47)"],
2436 preexec_fn=lambda: 1)
2437 self.assertRaises(ValueError, subprocess.call,
2438 [sys.executable, "-c",
2439 "import sys; sys.exit(47)"],
2440 stdout=subprocess.PIPE,
2441 close_fds=True)
2442
2443 def test_close_fds(self):
2444 # close file descriptors
2445 rc = subprocess.call([sys.executable, "-c",
2446 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002447 close_fds=True)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002448 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002449
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002450 def test_shell_sequence(self):
2451 # Run command through the shell (sequence)
2452 newenv = os.environ.copy()
2453 newenv["FRUIT"] = "physalis"
2454 p = subprocess.Popen(["set"], shell=1,
2455 stdout=subprocess.PIPE,
2456 env=newenv)
Brian Curtin19a53792010-11-05 17:09:05 +00002457 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002458 self.assertIn(b"physalis", p.stdout.read())
Guido van Rossume7ba4952007-06-06 23:52:48 +00002459
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002460 def test_shell_string(self):
2461 # Run command through the shell (string)
2462 newenv = os.environ.copy()
2463 newenv["FRUIT"] = "physalis"
2464 p = subprocess.Popen("set", shell=1,
2465 stdout=subprocess.PIPE,
2466 env=newenv)
Brian Curtin19a53792010-11-05 17:09:05 +00002467 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002468 self.assertIn(b"physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002469
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002470 def test_call_string(self):
2471 # call() function with string argument on Windows
2472 rc = subprocess.call(sys.executable +
2473 ' -c "import sys; sys.exit(47)"')
2474 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002475
Florent Xicluna4886d242010-03-08 13:27:26 +00002476 def _kill_process(self, method, *args):
2477 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroua4024e22010-09-24 18:57:01 +00002478 p = subprocess.Popen([sys.executable, "-c", """if 1:
2479 import sys, time
2480 sys.stdout.write('x\\n')
2481 sys.stdout.flush()
2482 time.sleep(30)
2483 """],
2484 stdin=subprocess.PIPE,
2485 stdout=subprocess.PIPE,
2486 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +00002487 self.addCleanup(p.stdout.close)
2488 self.addCleanup(p.stderr.close)
2489 self.addCleanup(p.stdin.close)
Antoine Pitroua4024e22010-09-24 18:57:01 +00002490 # Wait for the interpreter to be completely initialized before
2491 # sending any signal.
2492 p.stdout.read(1)
2493 getattr(p, method)(*args)
Florent Xiclunac049d872010-03-27 22:47:23 +00002494 _, stderr = p.communicate()
2495 self.assertStderrEqual(stderr, b'')
Antoine Pitroua4024e22010-09-24 18:57:01 +00002496 returncode = p.wait()
Florent Xicluna4886d242010-03-08 13:27:26 +00002497 self.assertNotEqual(returncode, 0)
2498
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01002499 def _kill_dead_process(self, method, *args):
2500 p = subprocess.Popen([sys.executable, "-c", """if 1:
2501 import sys, time
2502 sys.stdout.write('x\\n')
2503 sys.stdout.flush()
2504 sys.exit(42)
2505 """],
2506 stdin=subprocess.PIPE,
2507 stdout=subprocess.PIPE,
2508 stderr=subprocess.PIPE)
2509 self.addCleanup(p.stdout.close)
2510 self.addCleanup(p.stderr.close)
2511 self.addCleanup(p.stdin.close)
2512 # Wait for the interpreter to be completely initialized before
2513 # sending any signal.
2514 p.stdout.read(1)
2515 # The process should end after this
2516 time.sleep(1)
2517 # This shouldn't raise even though the child is now dead
2518 getattr(p, method)(*args)
2519 _, stderr = p.communicate()
2520 self.assertStderrEqual(stderr, b'')
2521 rc = p.wait()
2522 self.assertEqual(rc, 42)
2523
Florent Xicluna4886d242010-03-08 13:27:26 +00002524 def test_send_signal(self):
2525 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimesa342c012008-04-20 21:01:16 +00002526
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002527 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00002528 self._kill_process('kill')
Christian Heimesa342c012008-04-20 21:01:16 +00002529
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002530 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00002531 self._kill_process('terminate')
Christian Heimesa342c012008-04-20 21:01:16 +00002532
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01002533 def test_send_signal_dead(self):
2534 self._kill_dead_process('send_signal', signal.SIGTERM)
2535
2536 def test_kill_dead(self):
2537 self._kill_dead_process('kill')
2538
2539 def test_terminate_dead(self):
2540 self._kill_dead_process('terminate')
2541
Martin Panter23172bd2016-04-16 11:28:10 +00002542class MiscTests(unittest.TestCase):
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002543 def test_getoutput(self):
2544 self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
2545 self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
2546 (0, 'xyzzy'))
Brett Cannona23810f2008-05-26 19:04:21 +00002547
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002548 # we use mkdtemp in the next line to create an empty directory
2549 # under our exclusive control; from that, we can invent a pathname
2550 # that we _know_ won't exist. This is guaranteed to fail.
2551 dir = None
2552 try:
2553 dir = tempfile.mkdtemp()
2554 name = os.path.join(dir, "foo")
Tim Goldene0041752013-11-03 12:53:17 +00002555 status, output = subprocess.getstatusoutput(
2556 ("type " if mswindows else "cat ") + name)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00002557 self.assertNotEqual(status, 0)
2558 finally:
2559 if dir is not None:
2560 os.rmdir(dir)
Brett Cannona23810f2008-05-26 19:04:21 +00002561
Gregory P. Smithace55862015-04-07 15:57:54 -07002562 def test__all__(self):
2563 """Ensure that __all__ is populated properly."""
Martin Panter06172e72016-04-16 23:38:25 +00002564 # STARTUPINFO added to __all__ in 3.6
2565 intentionally_excluded = {"list2cmdline", "STARTUPINFO", "Handle"}
Gregory P. Smithace55862015-04-07 15:57:54 -07002566 exported = set(subprocess.__all__)
2567 possible_exports = set()
2568 import types
2569 for name, value in subprocess.__dict__.items():
2570 if name.startswith('_'):
2571 continue
2572 if isinstance(value, (types.ModuleType,)):
2573 continue
2574 possible_exports.add(name)
2575 self.assertEqual(exported, possible_exports - intentionally_excluded)
2576
2577
Martin Panter23172bd2016-04-16 11:28:10 +00002578@unittest.skipUnless(hasattr(selectors, 'PollSelector'),
2579 "Test needs selectors.PollSelector")
2580class ProcessTestCaseNoPoll(ProcessTestCase):
2581 def setUp(self):
2582 self.orig_selector = subprocess._PopenSelector
2583 subprocess._PopenSelector = selectors.SelectSelector
2584 ProcessTestCase.setUp(self)
2585
2586 def tearDown(self):
2587 subprocess._PopenSelector = self.orig_selector
2588 ProcessTestCase.tearDown(self)
2589
Gregory P. Smithd06fa472009-07-04 02:46:54 +00002590
Tim Golden126c2962010-08-11 14:20:40 +00002591@unittest.skipUnless(mswindows, "Windows-specific tests")
2592class CommandsWithSpaces (BaseTestCase):
2593
2594 def setUp(self):
2595 super().setUp()
Berker Peksag16a1f282015-09-28 13:33:14 +03002596 f, fname = tempfile.mkstemp(".py", "te st")
Tim Golden126c2962010-08-11 14:20:40 +00002597 self.fname = fname.lower ()
2598 os.write(f, b"import sys;"
2599 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
2600 )
2601 os.close(f)
2602
2603 def tearDown(self):
2604 os.remove(self.fname)
2605 super().tearDown()
2606
2607 def with_spaces(self, *args, **kwargs):
2608 kwargs['stdout'] = subprocess.PIPE
2609 p = subprocess.Popen(*args, **kwargs)
Brian Curtin19a53792010-11-05 17:09:05 +00002610 self.addCleanup(p.stdout.close)
Tim Golden126c2962010-08-11 14:20:40 +00002611 self.assertEqual(
2612 p.stdout.read ().decode("mbcs"),
2613 "2 [%r, 'ab cd']" % self.fname
2614 )
2615
2616 def test_shell_string_with_spaces(self):
2617 # call() function with string argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00002618 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
2619 "ab cd"), shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00002620
2621 def test_shell_sequence_with_spaces(self):
2622 # call() function with sequence argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00002623 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00002624
2625 def test_noshell_string_with_spaces(self):
2626 # call() function with string argument with spaces on Windows
2627 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
2628 "ab cd"))
2629
2630 def test_noshell_sequence_with_spaces(self):
2631 # call() function with sequence argument with spaces on Windows
2632 self.with_spaces([sys.executable, self.fname, "ab cd"])
2633
Brian Curtin79cdb662010-12-03 02:46:02 +00002634
Georg Brandla86b2622012-02-20 21:34:57 +01002635class ContextManagerTests(BaseTestCase):
Brian Curtin79cdb662010-12-03 02:46:02 +00002636
2637 def test_pipe(self):
2638 with subprocess.Popen([sys.executable, "-c",
2639 "import sys;"
2640 "sys.stdout.write('stdout');"
2641 "sys.stderr.write('stderr');"],
2642 stdout=subprocess.PIPE,
2643 stderr=subprocess.PIPE) as proc:
2644 self.assertEqual(proc.stdout.read(), b"stdout")
2645 self.assertStderrEqual(proc.stderr.read(), b"stderr")
2646
2647 self.assertTrue(proc.stdout.closed)
2648 self.assertTrue(proc.stderr.closed)
2649
2650 def test_returncode(self):
2651 with subprocess.Popen([sys.executable, "-c",
2652 "import sys; sys.exit(100)"]) as proc:
Gregory P. Smith6b657452011-05-11 21:42:08 -07002653 pass
2654 # __exit__ calls wait(), so the returncode should be set
Brian Curtin79cdb662010-12-03 02:46:02 +00002655 self.assertEqual(proc.returncode, 100)
2656
2657 def test_communicate_stdin(self):
2658 with subprocess.Popen([sys.executable, "-c",
2659 "import sys;"
2660 "sys.exit(sys.stdin.read() == 'context')"],
2661 stdin=subprocess.PIPE) as proc:
2662 proc.communicate(b"context")
2663 self.assertEqual(proc.returncode, 1)
2664
2665 def test_invalid_args(self):
Andrew Svetlovb1726972012-12-26 23:34:54 +02002666 with self.assertRaises(FileNotFoundError) as c:
Brian Curtin79cdb662010-12-03 02:46:02 +00002667 with subprocess.Popen(['nonexisting_i_hope'],
2668 stdout=subprocess.PIPE,
2669 stderr=subprocess.PIPE) as proc:
2670 pass
2671
Serhiy Storchakaab900c22015-02-28 12:43:08 +02002672 def test_broken_pipe_cleanup(self):
2673 """Broken pipe error should not prevent wait() (Issue 21619)"""
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02002674 proc = subprocess.Popen([sys.executable, '-c', 'pass'],
Victor Stinner20f4bd42015-03-05 02:38:41 +01002675 stdin=subprocess.PIPE,
Victor Stinner20f4bd42015-03-05 02:38:41 +01002676 bufsize=support.PIPE_MAX_SIZE*2)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02002677 proc = proc.__enter__()
2678 # Prepare to send enough data to overflow any OS pipe buffering and
2679 # guarantee a broken pipe error. Data is held in BufferedWriter
2680 # buffer until closed.
2681 proc.stdin.write(b'x' * support.PIPE_MAX_SIZE)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02002682 self.assertIsNone(proc.returncode)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02002683 # EPIPE expected under POSIX; EINVAL under Windows
Serhiy Storchakacf265fd2015-02-28 13:27:54 +02002684 self.assertRaises(OSError, proc.__exit__, None, None, None)
Serhiy Storchakaf87afb02015-03-08 09:16:40 +02002685 self.assertEqual(proc.returncode, 0)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02002686 self.assertTrue(proc.stdin.closed)
Serhiy Storchakaab900c22015-02-28 12:43:08 +02002687
Brian Curtin79cdb662010-12-03 02:46:02 +00002688
Gregory P. Smith3b4652e2011-03-15 15:43:39 -04002689def test_main():
2690 unit_tests = (ProcessTestCase,
2691 POSIXProcessTestCase,
2692 Win32ProcessTestCase,
Martin Panter23172bd2016-04-16 11:28:10 +00002693 MiscTests,
Gregory P. Smith3b4652e2011-03-15 15:43:39 -04002694 ProcessTestCaseNoPoll,
Gregory P. Smith3b4652e2011-03-15 15:43:39 -04002695 CommandsWithSpaces,
Antoine Pitrouab85ff32011-07-23 22:03:45 +02002696 ContextManagerTests,
Gregory P. Smith6e730002015-04-14 16:14:25 -07002697 RunFuncTestCase,
Antoine Pitrouab85ff32011-07-23 22:03:45 +02002698 )
Gregory P. Smith3b4652e2011-03-15 15:43:39 -04002699
2700 support.run_unittest(*unit_tests)
2701 support.reap_children()
2702
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002703if __name__ == "__main__":
Gregory P. Smith112bb3a2011-03-15 14:55:17 -04002704 unittest.main()