blob: 921328f001afe21ea6f123a2a9c6a762aa2afc19 [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001import unittest
Chris Jerdonekec3ea942012-09-30 00:10:28 -07002from test 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. Smithe14e9c22011-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
Ezio Melotti184bdfb2010-02-18 09:37:05 +000014import sysconfig
Gregory P. Smithd23047b2010-12-04 09:10:44 +000015import warnings
Gregory P. Smith51ee2702010-12-13 07:59:39 +000016import select
Gregory P. Smith81ce6852011-03-15 02:04:11 -040017import shutil
Benjamin Petersonb870aa12011-12-10 12:44:25 -050018import gc
Benjamin Peterson964561b2011-12-10 12:31:42 -050019
20try:
21 import resource
22except ImportError:
23 resource = None
24
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000025mswindows = (sys.platform == "win32")
26
27#
28# Depends on the following external programs: Python
29#
30
31if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000032 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
33 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000034else:
35 SETBINARY = ''
36
Florent Xiclunab1e94e82010-02-27 22:12:37 +000037
38try:
39 mkstemp = tempfile.mkstemp
40except AttributeError:
41 # tempfile.mkstemp is not available
42 def mkstemp():
43 """Replacement for mkstemp, calling mktemp."""
44 fname = tempfile.mktemp()
45 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
46
Tim Peters3761e8d2004-10-13 04:07:12 +000047
Florent Xiclunac049d872010-03-27 22:47:23 +000048class BaseTestCase(unittest.TestCase):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000049 def setUp(self):
50 # Try to minimize the number of children we have so this test
51 # doesn't crash on some buildbots (Alphas in particular).
Florent Xiclunab1e94e82010-02-27 22:12:37 +000052 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000053
Florent Xiclunaf0cbd822010-03-04 21:50:56 +000054 def tearDown(self):
55 for inst in subprocess._active:
56 inst.wait()
57 subprocess._cleanup()
58 self.assertFalse(subprocess._active, "subprocess._active not empty")
59
Florent Xiclunab1e94e82010-02-27 22:12:37 +000060 def assertStderrEqual(self, stderr, expected, msg=None):
61 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
62 # shutdown time. That frustrates tests trying to check stderr produced
63 # from a spawned Python process.
Antoine Pitrou62f68ed2010-08-04 11:48:56 +000064 actual = support.strip_python_stderr(stderr)
Florent Xiclunab1e94e82010-02-27 22:12:37 +000065 self.assertEqual(actual, expected, msg)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066
Florent Xiclunac049d872010-03-27 22:47:23 +000067
Gregory P. Smith3d8e7762012-11-10 22:32:22 -080068class PopenTestException(Exception):
69 pass
70
71
72class PopenExecuteChildRaises(subprocess.Popen):
73 """Popen subclass for testing cleanup of subprocess.PIPE filehandles when
74 _execute_child fails.
75 """
76 def _execute_child(self, *args, **kwargs):
77 raise PopenTestException("Forced Exception for Test")
78
79
Florent Xiclunac049d872010-03-27 22:47:23 +000080class ProcessTestCase(BaseTestCase):
81
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000082 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000083 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000084 rc = subprocess.call([sys.executable, "-c",
85 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000086 self.assertEqual(rc, 47)
87
Peter Astrand454f7672005-01-01 09:36:35 +000088 def test_check_call_zero(self):
89 # check_call() function with zero return code
90 rc = subprocess.check_call([sys.executable, "-c",
91 "import sys; sys.exit(0)"])
92 self.assertEqual(rc, 0)
93
94 def test_check_call_nonzero(self):
95 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +000096 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +000097 subprocess.check_call([sys.executable, "-c",
98 "import sys; sys.exit(47)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +000099 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +0000100
Georg Brandlf9734072008-12-07 15:30:06 +0000101 def test_check_output(self):
102 # check_output() function with zero return code
103 output = subprocess.check_output(
104 [sys.executable, "-c", "print('BDFL')"])
Benjamin Peterson577473f2010-01-19 00:09:57 +0000105 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +0000106
107 def test_check_output_nonzero(self):
108 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000109 with self.assertRaises(subprocess.CalledProcessError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +0000110 subprocess.check_output(
111 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000112 self.assertEqual(c.exception.returncode, 5)
Georg Brandlf9734072008-12-07 15:30:06 +0000113
114 def test_check_output_stderr(self):
115 # check_output() function stderr redirected to stdout
116 output = subprocess.check_output(
117 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
118 stderr=subprocess.STDOUT)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000119 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +0000120
121 def test_check_output_stdout_arg(self):
122 # check_output() function stderr redirected to stdout
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000123 with self.assertRaises(ValueError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +0000124 output = subprocess.check_output(
125 [sys.executable, "-c", "print('will not be run')"],
126 stdout=sys.stdout)
Georg Brandlf9734072008-12-07 15:30:06 +0000127 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000128 self.assertIn('stdout', c.exception.args[0])
Georg Brandlf9734072008-12-07 15:30:06 +0000129
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000130 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000131 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000132 newenv = os.environ.copy()
133 newenv["FRUIT"] = "banana"
134 rc = subprocess.call([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000135 'import sys, os;'
136 'sys.exit(os.getenv("FRUIT")=="banana")'],
137 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000138 self.assertEqual(rc, 1)
139
Victor Stinner87b9bc32011-06-01 00:57:47 +0200140 def test_invalid_args(self):
141 # Popen() called with invalid arguments should raise TypeError
142 # but Popen.__del__ should not complain (issue #12085)
143 with support.captured_stderr() as s:
144 self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
145 argcount = subprocess.Popen.__init__.__code__.co_argcount
146 too_many_args = [0] * (argcount + 1)
147 self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
148 self.assertEqual(s.getvalue(), '')
149
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000150 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000151 # .stdin is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000152 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000153 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000154 self.addCleanup(p.stdout.close)
155 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000156 p.wait()
157 self.assertEqual(p.stdin, None)
158
159 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000160 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +0000161 p = subprocess.Popen([sys.executable, "-c",
Georg Brandl88fc6642007-02-09 21:28:07 +0000162 'print(" this bit of output is from a '
Tim Peters4052fe52004-10-13 03:29:54 +0000163 'test of stdout in a different '
Georg Brandl88fc6642007-02-09 21:28:07 +0000164 'process ...")'],
Tim Peters4052fe52004-10-13 03:29:54 +0000165 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000166 self.addCleanup(p.stdin.close)
167 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000168 p.wait()
169 self.assertEqual(p.stdout, None)
170
171 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000172 # .stderr is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000173 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000174 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000175 self.addCleanup(p.stdout.close)
176 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000177 p.wait()
178 self.assertEqual(p.stderr, None)
179
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700180 # For use in the test_cwd* tests below.
181 def _normalize_cwd(self, cwd):
182 # Normalize an expected cwd (for Tru64 support).
183 # We can't use os.path.realpath since it doesn't expand Tru64 {memb}
184 # strings. See bug #1063571.
185 original_cwd = os.getcwd()
186 os.chdir(cwd)
187 cwd = os.getcwd()
188 os.chdir(original_cwd)
189 return cwd
190
191 # For use in the test_cwd* tests below.
192 def _split_python_path(self):
193 # Return normalized (python_dir, python_base).
194 python_path = os.path.realpath(sys.executable)
195 return os.path.split(python_path)
196
197 # For use in the test_cwd* tests below.
198 def _assert_cwd(self, expected_cwd, python_arg, **kwargs):
199 # Invoke Python via Popen, and assert that (1) the call succeeds,
200 # and that (2) the current working directory of the child process
201 # matches *expected_cwd*.
202 p = subprocess.Popen([python_arg, "-c",
203 "import os, sys; "
204 "sys.stdout.write(os.getcwd()); "
205 "sys.exit(47)"],
206 stdout=subprocess.PIPE,
207 **kwargs)
208 self.addCleanup(p.stdout.close)
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000209 p.wait()
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700210 self.assertEqual(47, p.returncode)
211 normcase = os.path.normcase
212 self.assertEqual(normcase(expected_cwd),
213 normcase(p.stdout.read().decode("utf-8")))
214
215 def test_cwd(self):
216 # Check that cwd changes the cwd for the child process.
217 temp_dir = tempfile.gettempdir()
218 temp_dir = self._normalize_cwd(temp_dir)
219 self._assert_cwd(temp_dir, sys.executable, cwd=temp_dir)
220
Chris Jerdonekc2cd6262012-09-30 09:45:00 -0700221 @unittest.skipIf(mswindows, "pending resolution of issue #15533")
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700222 def test_cwd_with_relative_arg(self):
223 # Check that Popen looks for args[0] relative to cwd if args[0]
224 # is relative.
225 python_dir, python_base = self._split_python_path()
226 rel_python = os.path.join(os.curdir, python_base)
227 with support.temp_cwd() as wrong_dir:
228 # Before calling with the correct cwd, confirm that the call fails
229 # without cwd and with the wrong cwd.
230 self.assertRaises(OSError, subprocess.Popen,
231 [rel_python])
232 self.assertRaises(OSError, subprocess.Popen,
233 [rel_python], cwd=wrong_dir)
234 python_dir = self._normalize_cwd(python_dir)
235 self._assert_cwd(python_dir, rel_python, cwd=python_dir)
236
Chris Jerdonekc2cd6262012-09-30 09:45:00 -0700237 @unittest.skipIf(mswindows, "pending resolution of issue #15533")
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700238 def test_cwd_with_relative_executable(self):
239 # Check that Popen looks for executable relative to cwd if executable
240 # is relative (and that executable takes precedence over args[0]).
241 python_dir, python_base = self._split_python_path()
242 rel_python = os.path.join(os.curdir, python_base)
243 doesntexist = "somethingyoudonthave"
244 with support.temp_cwd() as wrong_dir:
245 # Before calling with the correct cwd, confirm that the call fails
246 # without cwd and with the wrong cwd.
247 self.assertRaises(OSError, subprocess.Popen,
248 [doesntexist], executable=rel_python)
249 self.assertRaises(OSError, subprocess.Popen,
250 [doesntexist], executable=rel_python,
251 cwd=wrong_dir)
252 python_dir = self._normalize_cwd(python_dir)
253 self._assert_cwd(python_dir, doesntexist, executable=rel_python,
254 cwd=python_dir)
255
256 def test_cwd_with_absolute_arg(self):
257 # Check that Popen can find the executable when the cwd is wrong
258 # if args[0] is an absolute path.
259 python_dir, python_base = self._split_python_path()
260 abs_python = os.path.join(python_dir, python_base)
261 rel_python = os.path.join(os.curdir, python_base)
262 with script_helper.temp_dir() as wrong_dir:
263 # Before calling with an absolute path, confirm that using a
264 # relative path fails.
265 self.assertRaises(OSError, subprocess.Popen,
266 [rel_python], cwd=wrong_dir)
267 wrong_dir = self._normalize_cwd(wrong_dir)
268 self._assert_cwd(wrong_dir, abs_python, cwd=wrong_dir)
269
270 def test_executable_with_cwd(self):
271 python_dir, python_base = self._split_python_path()
272 python_dir = self._normalize_cwd(python_dir)
273 self._assert_cwd(python_dir, "somethingyoudonthave",
274 executable=sys.executable, cwd=python_dir)
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000275
276 @unittest.skipIf(sysconfig.is_python_build(),
277 "need an installed Python. See #7774")
278 def test_executable_without_cwd(self):
279 # For a normal installation, it should work without 'cwd'
280 # argument. For test runs in the build directory, see #7774.
Chris Jerdonekec3ea942012-09-30 00:10:28 -0700281 self._assert_cwd('', "somethingyoudonthave", executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000282
283 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000284 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000285 p = subprocess.Popen([sys.executable, "-c",
286 'import sys; sys.exit(sys.stdin.read() == "pear")'],
287 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000288 p.stdin.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000289 p.stdin.close()
290 p.wait()
291 self.assertEqual(p.returncode, 1)
292
293 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000294 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000295 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000296 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000297 d = tf.fileno()
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000298 os.write(d, b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000299 os.lseek(d, 0, 0)
300 p = subprocess.Popen([sys.executable, "-c",
301 'import sys; sys.exit(sys.stdin.read() == "pear")'],
302 stdin=d)
303 p.wait()
304 self.assertEqual(p.returncode, 1)
305
306 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000307 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000308 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000309 self.addCleanup(tf.close)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000310 tf.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000311 tf.seek(0)
312 p = subprocess.Popen([sys.executable, "-c",
313 'import sys; sys.exit(sys.stdin.read() == "pear")'],
314 stdin=tf)
315 p.wait()
316 self.assertEqual(p.returncode, 1)
317
318 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000319 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000320 p = subprocess.Popen([sys.executable, "-c",
321 'import sys; sys.stdout.write("orange")'],
322 stdout=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000323 self.addCleanup(p.stdout.close)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000324 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000325
326 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000327 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000328 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000329 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000330 d = tf.fileno()
331 p = subprocess.Popen([sys.executable, "-c",
332 'import sys; sys.stdout.write("orange")'],
333 stdout=d)
334 p.wait()
335 os.lseek(d, 0, 0)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000336 self.assertEqual(os.read(d, 1024), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000337
338 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000339 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000340 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000341 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000342 p = subprocess.Popen([sys.executable, "-c",
343 'import sys; sys.stdout.write("orange")'],
344 stdout=tf)
345 p.wait()
346 tf.seek(0)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000347 self.assertEqual(tf.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000348
349 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000350 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000351 p = subprocess.Popen([sys.executable, "-c",
352 'import sys; sys.stderr.write("strawberry")'],
353 stderr=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000354 self.addCleanup(p.stderr.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000355 self.assertStderrEqual(p.stderr.read(), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000356
357 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000358 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000359 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000360 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000361 d = tf.fileno()
362 p = subprocess.Popen([sys.executable, "-c",
363 'import sys; sys.stderr.write("strawberry")'],
364 stderr=d)
365 p.wait()
366 os.lseek(d, 0, 0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000367 self.assertStderrEqual(os.read(d, 1024), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000368
369 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000370 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000371 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000372 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000373 p = subprocess.Popen([sys.executable, "-c",
374 'import sys; sys.stderr.write("strawberry")'],
375 stderr=tf)
376 p.wait()
377 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000378 self.assertStderrEqual(tf.read(), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000379
380 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000381 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000382 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000383 'import sys;'
384 'sys.stdout.write("apple");'
385 'sys.stdout.flush();'
386 'sys.stderr.write("orange")'],
387 stdout=subprocess.PIPE,
388 stderr=subprocess.STDOUT)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000389 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000390 self.assertStderrEqual(p.stdout.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000391
392 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000393 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000394 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000395 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000396 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000397 'import sys;'
398 'sys.stdout.write("apple");'
399 'sys.stdout.flush();'
400 'sys.stderr.write("orange")'],
401 stdout=tf,
402 stderr=tf)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000403 p.wait()
404 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000405 self.assertStderrEqual(tf.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000406
Thomas Wouters89f507f2006-12-13 04:49:30 +0000407 def test_stdout_filedes_of_stdout(self):
408 # stdout is set to 1 (#1531862).
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000409 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), b'.\n'))"
Thomas Wouters89f507f2006-12-13 04:49:30 +0000410 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000411 self.assertEqual(rc, 2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000412
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000413 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000414 newenv = os.environ.copy()
415 newenv["FRUIT"] = "orange"
Victor Stinnerf1512a22011-06-21 17:18:38 +0200416 with subprocess.Popen([sys.executable, "-c",
417 'import sys,os;'
418 'sys.stdout.write(os.getenv("FRUIT"))'],
419 stdout=subprocess.PIPE,
420 env=newenv) as p:
421 stdout, stderr = p.communicate()
422 self.assertEqual(stdout, b"orange")
423
Victor Stinner62d51182011-06-23 01:02:25 +0200424 # Windows requires at least the SYSTEMROOT environment variable to start
425 # Python
426 @unittest.skipIf(sys.platform == 'win32',
427 'cannot test an empty env on Windows')
Victor Stinner237e5cb2011-06-22 21:28:43 +0200428 @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') is not None,
Victor Stinner372309a2011-06-21 21:59:06 +0200429 'the python library cannot be loaded '
430 'with an empty environment')
Victor Stinnerf1512a22011-06-21 17:18:38 +0200431 def test_empty_env(self):
432 with subprocess.Popen([sys.executable, "-c",
433 'import os; '
Victor Stinner372309a2011-06-21 21:59:06 +0200434 'print(list(os.environ.keys()))'],
Victor Stinnerf1512a22011-06-21 17:18:38 +0200435 stdout=subprocess.PIPE,
436 env={}) as p:
437 stdout, stderr = p.communicate()
Victor Stinner237e5cb2011-06-22 21:28:43 +0200438 self.assertIn(stdout.strip(),
439 (b"[]",
440 # Mac OS X adds __CF_USER_TEXT_ENCODING variable to an empty
441 # environment
442 b"['__CF_USER_TEXT_ENCODING']"))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000443
Peter Astrandcbac93c2005-03-03 20:24:28 +0000444 def test_communicate_stdin(self):
445 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000446 'import sys;'
447 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000448 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000449 p.communicate(b"pear")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000450 self.assertEqual(p.returncode, 1)
451
452 def test_communicate_stdout(self):
453 p = subprocess.Popen([sys.executable, "-c",
454 'import sys; sys.stdout.write("pineapple")'],
455 stdout=subprocess.PIPE)
456 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000457 self.assertEqual(stdout, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000458 self.assertEqual(stderr, None)
459
460 def test_communicate_stderr(self):
461 p = subprocess.Popen([sys.executable, "-c",
462 'import sys; sys.stderr.write("pineapple")'],
463 stderr=subprocess.PIPE)
464 (stdout, stderr) = p.communicate()
465 self.assertEqual(stdout, None)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000466 self.assertStderrEqual(stderr, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000467
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000468 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000469 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000470 'import sys,os;'
471 'sys.stderr.write("pineapple");'
472 'sys.stdout.write(sys.stdin.read())'],
473 stdin=subprocess.PIPE,
474 stdout=subprocess.PIPE,
475 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000476 self.addCleanup(p.stdout.close)
477 self.addCleanup(p.stderr.close)
478 self.addCleanup(p.stdin.close)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000479 (stdout, stderr) = p.communicate(b"banana")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000480 self.assertEqual(stdout, b"banana")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000481 self.assertStderrEqual(stderr, b"pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000482
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000483 # Test for the fd leak reported in http://bugs.python.org/issue2791.
484 def test_communicate_pipe_fd_leak(self):
Victor Stinner667d4b52010-12-25 22:40:32 +0000485 for stdin_pipe in (False, True):
486 for stdout_pipe in (False, True):
487 for stderr_pipe in (False, True):
488 options = {}
489 if stdin_pipe:
490 options['stdin'] = subprocess.PIPE
491 if stdout_pipe:
492 options['stdout'] = subprocess.PIPE
493 if stderr_pipe:
494 options['stderr'] = subprocess.PIPE
495 if not options:
496 continue
497 p = subprocess.Popen((sys.executable, "-c", "pass"), **options)
498 p.communicate()
499 if p.stdin is not None:
500 self.assertTrue(p.stdin.closed)
501 if p.stdout is not None:
502 self.assertTrue(p.stdout.closed)
503 if p.stderr is not None:
504 self.assertTrue(p.stderr.closed)
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000505
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000506 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000507 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000508 p = subprocess.Popen([sys.executable, "-c",
509 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000510 (stdout, stderr) = p.communicate()
511 self.assertEqual(stdout, None)
512 self.assertEqual(stderr, None)
513
514 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000515 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000516 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000517 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000518 x, y = os.pipe()
519 if mswindows:
520 pipe_buf = 512
521 else:
522 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
523 os.close(x)
524 os.close(y)
525 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000526 'import sys,os;'
527 'sys.stdout.write(sys.stdin.read(47));'
528 'sys.stderr.write("xyz"*%d);'
529 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
530 stdin=subprocess.PIPE,
531 stdout=subprocess.PIPE,
532 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000533 self.addCleanup(p.stdout.close)
534 self.addCleanup(p.stderr.close)
535 self.addCleanup(p.stdin.close)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000536 string_to_write = b"abc"*pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000537 (stdout, stderr) = p.communicate(string_to_write)
538 self.assertEqual(stdout, string_to_write)
539
540 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000541 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000542 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000543 'import sys,os;'
544 'sys.stdout.write(sys.stdin.read())'],
545 stdin=subprocess.PIPE,
546 stdout=subprocess.PIPE,
547 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000548 self.addCleanup(p.stdout.close)
549 self.addCleanup(p.stderr.close)
550 self.addCleanup(p.stdin.close)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000551 p.stdin.write(b"banana")
552 (stdout, stderr) = p.communicate(b"split")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000553 self.assertEqual(stdout, b"bananasplit")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000554 self.assertStderrEqual(stderr, b"")
Tim Peterse718f612004-10-12 21:51:32 +0000555
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000556 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000557 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000558 'import sys,os;' + SETBINARY +
Antoine Pitrouec2d2692012-08-05 00:23:40 +0200559 'buf = sys.stdout.buffer;'
560 'buf.write(sys.stdin.readline().encode());'
561 'buf.flush();'
562 'buf.write(b"line2\\n");'
563 'buf.flush();'
564 'buf.write(sys.stdin.read().encode());'
565 'buf.flush();'
566 'buf.write(b"line4\\n");'
567 'buf.flush();'
568 'buf.write(b"line5\\r\\n");'
569 'buf.flush();'
570 'buf.write(b"line6\\r");'
571 'buf.flush();'
572 'buf.write(b"\\nline7");'
573 'buf.flush();'
574 'buf.write(b"\\nline8");'],
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200575 stdin=subprocess.PIPE,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000576 stdout=subprocess.PIPE,
577 universal_newlines=1)
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200578 p.stdin.write("line1\n")
579 self.assertEqual(p.stdout.readline(), "line1\n")
580 p.stdin.write("line3\n")
581 p.stdin.close()
Brian Curtin3c6a9512010-11-05 03:58:52 +0000582 self.addCleanup(p.stdout.close)
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200583 self.assertEqual(p.stdout.readline(),
584 "line2\n")
585 self.assertEqual(p.stdout.read(6),
586 "line3\n")
587 self.assertEqual(p.stdout.read(),
588 "line4\nline5\nline6\nline7\nline8")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000589
590 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000591 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000592 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000593 'import sys,os;' + SETBINARY +
Antoine Pitrouec2d2692012-08-05 00:23:40 +0200594 'buf = sys.stdout.buffer;'
595 'buf.write(b"line2\\n");'
596 'buf.flush();'
597 'buf.write(b"line4\\n");'
598 'buf.flush();'
599 'buf.write(b"line5\\r\\n");'
600 'buf.flush();'
601 'buf.write(b"line6\\r");'
602 'buf.flush();'
603 'buf.write(b"\\nline7");'
604 'buf.flush();'
605 'buf.write(b"\\nline8");'],
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200606 stderr=subprocess.PIPE,
607 stdout=subprocess.PIPE,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000608 universal_newlines=1)
Brian Curtin19a53792010-11-05 17:09:05 +0000609 self.addCleanup(p.stdout.close)
610 self.addCleanup(p.stderr.close)
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200611 # BUG: can't give a non-empty stdin because it breaks both the
612 # select- and poll-based communicate() implementations.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000613 (stdout, stderr) = p.communicate()
Antoine Pitrouab85ff32011-07-23 22:03:45 +0200614 self.assertEqual(stdout,
615 "line2\nline4\nline5\nline6\nline7\nline8")
616
617 def test_universal_newlines_communicate_stdin(self):
618 # universal newlines through communicate(), with only stdin
619 p = subprocess.Popen([sys.executable, "-c",
620 'import sys,os;' + SETBINARY + '''\nif True:
621 s = sys.stdin.readline()
622 assert s == "line1\\n", repr(s)
623 s = sys.stdin.read()
624 assert s == "line3\\n", repr(s)
625 '''],
626 stdin=subprocess.PIPE,
627 universal_newlines=1)
628 (stdout, stderr) = p.communicate("line1\nline3\n")
629 self.assertEqual(p.returncode, 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000630
Andrew Svetlovf3765072012-08-14 18:35:17 +0300631 def test_universal_newlines_communicate_input_none(self):
632 # Test communicate(input=None) with universal newlines.
633 #
634 # We set stdout to PIPE because, as of this writing, a different
635 # code path is tested when the number of pipes is zero or one.
636 p = subprocess.Popen([sys.executable, "-c", "pass"],
637 stdin=subprocess.PIPE,
638 stdout=subprocess.PIPE,
639 universal_newlines=True)
640 p.communicate()
641 self.assertEqual(p.returncode, 0)
642
Andrew Svetlov82860712012-08-19 22:13:41 +0300643 def test_universal_newlines_communicate_encodings(self):
644 # Check that universal newlines mode works for various encodings,
645 # in particular for encodings in the UTF-16 and UTF-32 families.
646 # See issue #15595.
647 #
648 # UTF-16 and UTF-32-BE are sufficient to check both with BOM and
649 # without, and UTF-16 and UTF-32.
650 for encoding in ['utf-16', 'utf-32-be']:
651 old_getpreferredencoding = locale.getpreferredencoding
652 # Indirectly via io.TextIOWrapper, Popen() defaults to
653 # locale.getpreferredencoding(False) and earlier in Python 3.2 to
654 # locale.getpreferredencoding().
655 def getpreferredencoding(do_setlocale=True):
656 return encoding
657 code = ("import sys; "
658 r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" %
659 encoding)
660 args = [sys.executable, '-c', code]
661 try:
662 locale.getpreferredencoding = getpreferredencoding
663 # We set stdin to be non-None because, as of this writing,
664 # a different code path is used when the number of pipes is
665 # zero or one.
666 popen = subprocess.Popen(args, universal_newlines=True,
667 stdin=subprocess.PIPE,
668 stdout=subprocess.PIPE)
669 stdout, stderr = popen.communicate(input='')
670 finally:
671 locale.getpreferredencoding = old_getpreferredencoding
672
673 self.assertEqual(stdout, '1\n2\n3\n4')
674
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000675 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000676 # Make sure we leak no resources
Antoine Pitrou8db30272010-09-18 22:38:48 +0000677 if not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000678 max_handles = 1026 # too much for most UNIX systems
679 else:
Antoine Pitrou8db30272010-09-18 22:38:48 +0000680 max_handles = 2050 # too much for (at least some) Windows setups
681 handles = []
Gregory P. Smith81ce6852011-03-15 02:04:11 -0400682 tmpdir = tempfile.mkdtemp()
Antoine Pitrou8db30272010-09-18 22:38:48 +0000683 try:
684 for i in range(max_handles):
685 try:
Gregory P. Smith81ce6852011-03-15 02:04:11 -0400686 tmpfile = os.path.join(tmpdir, support.TESTFN)
687 handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT))
Antoine Pitrou8db30272010-09-18 22:38:48 +0000688 except OSError as e:
689 if e.errno != errno.EMFILE:
690 raise
691 break
692 else:
693 self.skipTest("failed to reach the file descriptor limit "
694 "(tried %d)" % max_handles)
695 # Close a couple of them (should be enough for a subprocess)
696 for i in range(10):
697 os.close(handles.pop())
698 # Loop creating some subprocesses. If one of them leaks some fds,
699 # the next loop iteration will fail by reaching the max fd limit.
700 for i in range(15):
701 p = subprocess.Popen([sys.executable, "-c",
702 "import sys;"
703 "sys.stdout.write(sys.stdin.read())"],
704 stdin=subprocess.PIPE,
705 stdout=subprocess.PIPE,
706 stderr=subprocess.PIPE)
707 data = p.communicate(b"lime")[0]
708 self.assertEqual(data, b"lime")
709 finally:
710 for h in handles:
711 os.close(h)
Gregory P. Smith81ce6852011-03-15 02:04:11 -0400712 shutil.rmtree(tmpdir)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000713
714 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000715 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
716 '"a b c" d e')
717 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
718 'ab\\"c \\ d')
Christian Heimesfdab48e2008-01-20 09:06:41 +0000719 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
720 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000721 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
722 'a\\\\\\b "de fg" h')
723 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
724 'a\\\\\\"b c d')
725 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
726 '"a\\\\b c" d e')
727 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
728 '"a\\\\b\\ c" d e')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000729 self.assertEqual(subprocess.list2cmdline(['ab', '']),
730 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000731
732
733 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000734 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000735 "-c", "import time; time.sleep(1)"])
736 count = 0
737 while p.poll() is None:
738 time.sleep(0.1)
739 count += 1
740 # We expect that the poll loop probably went around about 10 times,
741 # but, based on system scheduling we can't control, it's possible
742 # poll() never returned None. It "should be" very rare that it
743 # didn't go around at least twice.
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000744 self.assertGreaterEqual(count, 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000745 # Subsequent invocations should just return the returncode
746 self.assertEqual(p.poll(), 0)
747
748
749 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000750 p = subprocess.Popen([sys.executable,
751 "-c", "import time; time.sleep(2)"])
752 self.assertEqual(p.wait(), 0)
753 # Subsequent invocations should just return the returncode
754 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000755
Peter Astrand738131d2004-11-30 21:04:45 +0000756
757 def test_invalid_bufsize(self):
758 # an invalid type of the bufsize argument should raise
759 # TypeError.
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000760 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +0000761 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +0000762
Guido van Rossum46a05a72007-06-07 21:56:45 +0000763 def test_bufsize_is_none(self):
764 # bufsize=None should be the same as bufsize=0.
765 p = subprocess.Popen([sys.executable, "-c", "pass"], None)
766 self.assertEqual(p.wait(), 0)
767 # Again with keyword arg
768 p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None)
769 self.assertEqual(p.wait(), 0)
770
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000771 def test_leaking_fds_on_error(self):
772 # see bug #5179: Popen leaks file descriptors to PIPEs if
773 # the child fails to execute; this will eventually exhaust
774 # the maximum number of open fds. 1024 seems a very common
775 # value for that limit, but Windows has 2048, so we loop
776 # 1024 times (each call leaked two fds).
777 for i in range(1024):
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000778 # Windows raises IOError. Others raise OSError.
779 with self.assertRaises(EnvironmentError) as c:
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000780 subprocess.Popen(['nonexisting_i_hope'],
781 stdout=subprocess.PIPE,
782 stderr=subprocess.PIPE)
R David Murray384069c2011-03-13 22:26:53 -0400783 # ignore errors that indicate the command was not found
R David Murray6924bd72011-03-13 22:48:55 -0400784 if c.exception.errno not in (errno.ENOENT, errno.EACCES):
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000785 raise c.exception
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000786
Victor Stinnerb3693582010-05-21 20:13:12 +0000787 def test_issue8780(self):
788 # Ensure that stdout is inherited from the parent
789 # if stdout=PIPE is not used
790 code = ';'.join((
791 'import subprocess, sys',
792 'retcode = subprocess.call('
793 "[sys.executable, '-c', 'print(\"Hello World!\")'])",
794 'assert retcode == 0'))
795 output = subprocess.check_output([sys.executable, '-c', code])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000796 self.assertTrue(output.startswith(b'Hello World!'), ascii(output))
Victor Stinnerb3693582010-05-21 20:13:12 +0000797
Tim Goldenaf5ac392010-08-06 13:03:56 +0000798 def test_handles_closed_on_exception(self):
799 # If CreateProcess exits with an error, ensure the
800 # duplicate output handles are released
801 ifhandle, ifname = mkstemp()
802 ofhandle, ofname = mkstemp()
803 efhandle, efname = mkstemp()
804 try:
805 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
806 stderr=efhandle)
807 except OSError:
808 os.close(ifhandle)
809 os.remove(ifname)
810 os.close(ofhandle)
811 os.remove(ofname)
812 os.close(efhandle)
813 os.remove(efname)
814 self.assertFalse(os.path.exists(ifname))
815 self.assertFalse(os.path.exists(ofname))
816 self.assertFalse(os.path.exists(efname))
817
Ross Lagerwall4f61b022011-04-05 15:34:00 +0200818 def test_communicate_epipe(self):
819 # Issue 10963: communicate() should hide EPIPE
820 p = subprocess.Popen([sys.executable, "-c", 'pass'],
821 stdin=subprocess.PIPE,
822 stdout=subprocess.PIPE,
823 stderr=subprocess.PIPE)
824 self.addCleanup(p.stdout.close)
825 self.addCleanup(p.stderr.close)
826 self.addCleanup(p.stdin.close)
827 p.communicate(b"x" * 2**20)
828
829 def test_communicate_epipe_only_stdin(self):
830 # Issue 10963: communicate() should hide EPIPE
831 p = subprocess.Popen([sys.executable, "-c", 'pass'],
832 stdin=subprocess.PIPE)
833 self.addCleanup(p.stdin.close)
834 time.sleep(2)
835 p.communicate(b"x" * 2**20)
836
Victor Stinner1848db82011-07-05 14:49:46 +0200837 @unittest.skipUnless(hasattr(signal, 'SIGALRM'),
838 "Requires signal.SIGALRM")
Victor Stinner2cfb6f32011-07-05 14:00:56 +0200839 def test_communicate_eintr(self):
840 # Issue #12493: communicate() should handle EINTR
841 def handler(signum, frame):
842 pass
843 old_handler = signal.signal(signal.SIGALRM, handler)
844 self.addCleanup(signal.signal, signal.SIGALRM, old_handler)
845
846 # the process is running for 2 seconds
847 args = [sys.executable, "-c", 'import time; time.sleep(2)']
848 for stream in ('stdout', 'stderr'):
849 kw = {stream: subprocess.PIPE}
850 with subprocess.Popen(args, **kw) as process:
851 signal.alarm(1)
852 # communicate() will be interrupted by SIGALRM
853 process.communicate()
854
Tim Peterse718f612004-10-12 21:51:32 +0000855
Gregory P. Smith3d8e7762012-11-10 22:32:22 -0800856 # This test is Linux-ish specific for simplicity to at least have
857 # some coverage. It is not a platform specific bug.
858 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
859 "Linux specific")
860 def test_failed_child_execute_fd_leak(self):
861 """Test for the fork() failure fd leak reported in issue16327."""
862 fd_directory = '/proc/%d/fd' % os.getpid()
863 fds_before_popen = os.listdir(fd_directory)
864 with self.assertRaises(PopenTestException):
865 PopenExecuteChildRaises(
866 [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE,
867 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
868
869 # NOTE: This test doesn't verify that the real _execute_child
870 # does not close the file descriptors itself on the way out
871 # during an exception. Code inspection has confirmed that.
872
873 fds_after_exception = os.listdir(fd_directory)
874 self.assertEqual(fds_before_popen, fds_after_exception)
875
876
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000877# context manager
878class _SuppressCoreFiles(object):
879 """Try to prevent core files from being created."""
880 old_limit = None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000881
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000882 def __enter__(self):
883 """Try to save previous ulimit, then set it to (0, 0)."""
Benjamin Peterson964561b2011-12-10 12:31:42 -0500884 if resource is not None:
885 try:
886 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
887 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
888 except (ValueError, resource.error):
889 pass
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000890
Ronald Oussoren102d11a2010-07-23 09:50:05 +0000891 if sys.platform == 'darwin':
892 # Check if the 'Crash Reporter' on OSX was configured
893 # in 'Developer' mode and warn that it will get triggered
894 # when it is.
895 #
896 # This assumes that this context manager is used in tests
897 # that might trigger the next manager.
898 value = subprocess.Popen(['/usr/bin/defaults', 'read',
899 'com.apple.CrashReporter', 'DialogType'],
900 stdout=subprocess.PIPE).communicate()[0]
901 if value.strip() == b'developer':
902 print("this tests triggers the Crash Reporter, "
903 "that is intentional", end='')
904 sys.stdout.flush()
905
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000906 def __exit__(self, *args):
907 """Return core file behavior to default."""
908 if self.old_limit is None:
909 return
Benjamin Peterson964561b2011-12-10 12:31:42 -0500910 if resource is not None:
911 try:
912 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
913 except (ValueError, resource.error):
914 pass
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000915
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000916
Florent Xiclunaf0cbd822010-03-04 21:50:56 +0000917@unittest.skipIf(mswindows, "POSIX specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +0000918class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +0000919
Gregory P. Smith5591b022012-10-10 03:34:47 -0700920 def setUp(self):
921 super().setUp()
922 self._nonexistent_dir = "/_this/pa.th/does/not/exist"
923
924 def _get_chdir_exception(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000925 try:
Gregory P. Smith5591b022012-10-10 03:34:47 -0700926 os.chdir(self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000927 except OSError as e:
928 # This avoids hard coding the errno value or the OS perror()
929 # string and instead capture the exception that we want to see
930 # below for comparison.
931 desired_exception = e
Gregory P. Smith5591b022012-10-10 03:34:47 -0700932 desired_exception.strerror += ': ' + repr(self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000933 else:
934 self.fail("chdir to nonexistant directory %s succeeded." %
Gregory P. Smith5591b022012-10-10 03:34:47 -0700935 self._nonexistent_dir)
936 return desired_exception
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000937
Gregory P. Smith5591b022012-10-10 03:34:47 -0700938 def test_exception_cwd(self):
939 """Test error in the child raised in the parent for a bad cwd."""
940 desired_exception = self._get_chdir_exception()
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000941 try:
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000942 p = subprocess.Popen([sys.executable, "-c", ""],
Gregory P. Smith5591b022012-10-10 03:34:47 -0700943 cwd=self._nonexistent_dir)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000944 except OSError as e:
945 # Test that the child process chdir failure actually makes
946 # it up to the parent process as the correct exception.
947 self.assertEqual(desired_exception.errno, e.errno)
948 self.assertEqual(desired_exception.strerror, e.strerror)
949 else:
950 self.fail("Expected OSError: %s" % desired_exception)
951
Gregory P. Smith5591b022012-10-10 03:34:47 -0700952 def test_exception_bad_executable(self):
953 """Test error in the child raised in the parent for a bad executable."""
954 desired_exception = self._get_chdir_exception()
955 try:
956 p = subprocess.Popen([sys.executable, "-c", ""],
957 executable=self._nonexistent_dir)
958 except OSError as e:
959 # Test that the child process exec failure actually makes
960 # it up to the parent process as the correct exception.
961 self.assertEqual(desired_exception.errno, e.errno)
962 self.assertEqual(desired_exception.strerror, e.strerror)
963 else:
964 self.fail("Expected OSError: %s" % desired_exception)
965
966 def test_exception_bad_args_0(self):
967 """Test error in the child raised in the parent for a bad args[0]."""
968 desired_exception = self._get_chdir_exception()
969 try:
970 p = subprocess.Popen([self._nonexistent_dir, "-c", ""])
971 except OSError as e:
972 # Test that the child process exec failure actually makes
973 # it up to the parent process as the correct exception.
974 self.assertEqual(desired_exception.errno, e.errno)
975 self.assertEqual(desired_exception.strerror, e.strerror)
976 else:
977 self.fail("Expected OSError: %s" % desired_exception)
978
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000979 def test_restore_signals(self):
980 # Code coverage for both values of restore_signals to make sure it
981 # at least does not blow up.
982 # A test for behavior would be complex. Contributions welcome.
983 subprocess.call([sys.executable, "-c", ""], restore_signals=True)
984 subprocess.call([sys.executable, "-c", ""], restore_signals=False)
985
986 def test_start_new_session(self):
987 # For code coverage of calling setsid(). We don't care if we get an
988 # EPERM error from it depending on the test execution environment, that
989 # still indicates that it was called.
990 try:
991 output = subprocess.check_output(
992 [sys.executable, "-c",
993 "import os; print(os.getpgid(os.getpid()))"],
994 start_new_session=True)
995 except OSError as e:
996 if e.errno != errno.EPERM:
997 raise
998 else:
999 parent_pgid = os.getpgid(os.getpid())
1000 child_pgid = int(output)
1001 self.assertNotEqual(parent_pgid, child_pgid)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001002
1003 def test_run_abort(self):
1004 # returncode handles signal termination
1005 with _SuppressCoreFiles():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001006 p = subprocess.Popen([sys.executable, "-c",
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001007 'import os; os.abort()'])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001008 p.wait()
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001009 self.assertEqual(-p.returncode, signal.SIGABRT)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001010
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001011 def test_preexec(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001012 # DISCLAIMER: Setting environment variables is *not* a good use
1013 # of a preexec_fn. This is merely a test.
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001014 p = subprocess.Popen([sys.executable, "-c",
1015 'import sys,os;'
1016 'sys.stdout.write(os.getenv("FRUIT"))'],
1017 stdout=subprocess.PIPE,
1018 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Brian Curtin3c6a9512010-11-05 03:58:52 +00001019 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001020 self.assertEqual(p.stdout.read(), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001021
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001022 def test_preexec_exception(self):
1023 def raise_it():
1024 raise ValueError("What if two swallows carried a coconut?")
1025 try:
1026 p = subprocess.Popen([sys.executable, "-c", ""],
1027 preexec_fn=raise_it)
1028 except RuntimeError as e:
1029 self.assertTrue(
1030 subprocess._posixsubprocess,
1031 "Expected a ValueError from the preexec_fn")
1032 except ValueError as e:
1033 self.assertIn("coconut", e.args[0])
1034 else:
1035 self.fail("Exception raised by preexec_fn did not make it "
1036 "to the parent process.")
1037
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001038 class _TestExecuteChildPopen(subprocess.Popen):
1039 """Used to test behavior at the end of _execute_child."""
1040 def __init__(self, testcase, *args, **kwargs):
1041 self._testcase = testcase
1042 subprocess.Popen.__init__(self, *args, **kwargs)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001043
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001044 def _execute_child(self, *args, **kwargs):
Gregory P. Smith12489d92012-11-11 01:37:02 -08001045 try:
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001046 subprocess.Popen._execute_child(self, *args, **kwargs)
Gregory P. Smith12489d92012-11-11 01:37:02 -08001047 finally:
1048 # Open a bunch of file descriptors and verify that
1049 # none of them are the same as the ones the Popen
1050 # instance is using for stdin/stdout/stderr.
1051 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
1052 for _ in range(8)]
1053 try:
1054 for fd in devzero_fds:
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001055 self._testcase.assertNotIn(
1056 fd, (self.stdin.fileno(), self.stdout.fileno(),
1057 self.stderr.fileno()),
Gregory P. Smith12489d92012-11-11 01:37:02 -08001058 msg="At least one fd was closed early.")
1059 finally:
1060 map(os.close, devzero_fds)
1061
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001062 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
1063 def test_preexec_errpipe_does_not_double_close_pipes(self):
1064 """Issue16140: Don't double close pipes on preexec error."""
1065
1066 def raise_it():
1067 raise RuntimeError("force the _execute_child() errpipe_data path.")
Gregory P. Smith12489d92012-11-11 01:37:02 -08001068
1069 with self.assertRaises(RuntimeError):
Gregory P. Smithe27faac2012-11-11 09:59:27 -08001070 self._TestExecuteChildPopen(
1071 self, [sys.executable, "-c", "pass"],
Gregory P. Smith12489d92012-11-11 01:37:02 -08001072 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1073 stderr=subprocess.PIPE, preexec_fn=raise_it)
1074
Gregory P. Smith32ec9da2010-03-19 16:53:08 +00001075 def test_preexec_gc_module_failure(self):
1076 # This tests the code that disables garbage collection if the child
1077 # process will execute any Python.
1078 def raise_runtime_error():
1079 raise RuntimeError("this shouldn't escape")
1080 enabled = gc.isenabled()
1081 orig_gc_disable = gc.disable
1082 orig_gc_isenabled = gc.isenabled
1083 try:
1084 gc.disable()
1085 self.assertFalse(gc.isenabled())
1086 subprocess.call([sys.executable, '-c', ''],
1087 preexec_fn=lambda: None)
1088 self.assertFalse(gc.isenabled(),
1089 "Popen enabled gc when it shouldn't.")
1090
1091 gc.enable()
1092 self.assertTrue(gc.isenabled())
1093 subprocess.call([sys.executable, '-c', ''],
1094 preexec_fn=lambda: None)
1095 self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
1096
1097 gc.disable = raise_runtime_error
1098 self.assertRaises(RuntimeError, subprocess.Popen,
1099 [sys.executable, '-c', ''],
1100 preexec_fn=lambda: None)
1101
1102 del gc.isenabled # force an AttributeError
1103 self.assertRaises(AttributeError, subprocess.Popen,
1104 [sys.executable, '-c', ''],
1105 preexec_fn=lambda: None)
1106 finally:
1107 gc.disable = orig_gc_disable
1108 gc.isenabled = orig_gc_isenabled
1109 if not enabled:
1110 gc.disable()
1111
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001112 def test_args_string(self):
1113 # args is a string
1114 fd, fname = mkstemp()
1115 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +00001116 with open(fd, "w", errors="surrogateescape") as fobj:
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001117 fobj.write("#!/bin/sh\n")
1118 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
1119 sys.executable)
1120 os.chmod(fname, 0o700)
1121 p = subprocess.Popen(fname)
1122 p.wait()
1123 os.remove(fname)
1124 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001125
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001126 def test_invalid_args(self):
1127 # invalid arguments should raise ValueError
1128 self.assertRaises(ValueError, subprocess.call,
1129 [sys.executable, "-c",
1130 "import sys; sys.exit(47)"],
1131 startupinfo=47)
1132 self.assertRaises(ValueError, subprocess.call,
1133 [sys.executable, "-c",
1134 "import sys; sys.exit(47)"],
1135 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001136
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001137 def test_shell_sequence(self):
1138 # Run command through the shell (sequence)
1139 newenv = os.environ.copy()
1140 newenv["FRUIT"] = "apple"
1141 p = subprocess.Popen(["echo $FRUIT"], shell=1,
1142 stdout=subprocess.PIPE,
1143 env=newenv)
Brian Curtin3c6a9512010-11-05 03:58:52 +00001144 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001145 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001146
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001147 def test_shell_string(self):
1148 # Run command through the shell (string)
1149 newenv = os.environ.copy()
1150 newenv["FRUIT"] = "apple"
1151 p = subprocess.Popen("echo $FRUIT", shell=1,
1152 stdout=subprocess.PIPE,
1153 env=newenv)
Brian Curtin3c6a9512010-11-05 03:58:52 +00001154 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001155 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Christian Heimesa342c012008-04-20 21:01:16 +00001156
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001157 def test_call_string(self):
1158 # call() function with string argument on UNIX
1159 fd, fname = mkstemp()
1160 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +00001161 with open(fd, "w", errors="surrogateescape") as fobj:
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001162 fobj.write("#!/bin/sh\n")
1163 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
1164 sys.executable)
1165 os.chmod(fname, 0o700)
1166 rc = subprocess.call(fname)
1167 os.remove(fname)
1168 self.assertEqual(rc, 47)
Christian Heimesa342c012008-04-20 21:01:16 +00001169
Stefan Krah9542cc62010-07-19 14:20:53 +00001170 def test_specific_shell(self):
1171 # Issue #9265: Incorrect name passed as arg[0].
1172 shells = []
1173 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
1174 for name in ['bash', 'ksh']:
1175 sh = os.path.join(prefix, name)
1176 if os.path.isfile(sh):
1177 shells.append(sh)
1178 if not shells: # Will probably work for any shell but csh.
1179 self.skipTest("bash or ksh required for this test")
1180 sh = '/bin/sh'
1181 if os.path.isfile(sh) and not os.path.islink(sh):
1182 # Test will fail if /bin/sh is a symlink to csh.
1183 shells.append(sh)
1184 for sh in shells:
1185 p = subprocess.Popen("echo $0", executable=sh, shell=True,
1186 stdout=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +00001187 self.addCleanup(p.stdout.close)
Stefan Krah9542cc62010-07-19 14:20:53 +00001188 self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
1189
Florent Xicluna4886d242010-03-08 13:27:26 +00001190 def _kill_process(self, method, *args):
Florent Xicluna1d8ee3a2010-03-05 20:26:54 +00001191 # Do not inherit file handles from the parent.
1192 # It should fix failures on some platforms.
Antoine Pitrou3d8580f2010-09-20 01:33:21 +00001193 p = subprocess.Popen([sys.executable, "-c", """if 1:
1194 import sys, time
1195 sys.stdout.write('x\\n')
1196 sys.stdout.flush()
1197 time.sleep(30)
1198 """],
1199 close_fds=True,
1200 stdin=subprocess.PIPE,
1201 stdout=subprocess.PIPE,
1202 stderr=subprocess.PIPE)
1203 # Wait for the interpreter to be completely initialized before
1204 # sending any signal.
1205 p.stdout.read(1)
1206 getattr(p, method)(*args)
Florent Xicluna4886d242010-03-08 13:27:26 +00001207 return p
1208
Charles-François Natali53221e32013-01-12 16:52:20 +01001209 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
1210 "Due to known OS bug (issue #16762)")
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001211 def _kill_dead_process(self, method, *args):
1212 # Do not inherit file handles from the parent.
1213 # It should fix failures on some platforms.
1214 p = subprocess.Popen([sys.executable, "-c", """if 1:
1215 import sys, time
1216 sys.stdout.write('x\\n')
1217 sys.stdout.flush()
1218 """],
1219 close_fds=True,
1220 stdin=subprocess.PIPE,
1221 stdout=subprocess.PIPE,
1222 stderr=subprocess.PIPE)
1223 # Wait for the interpreter to be completely initialized before
1224 # sending any signal.
1225 p.stdout.read(1)
1226 # The process should end after this
1227 time.sleep(1)
1228 # This shouldn't raise even though the child is now dead
1229 getattr(p, method)(*args)
1230 p.communicate()
1231
Florent Xicluna4886d242010-03-08 13:27:26 +00001232 def test_send_signal(self):
1233 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunac049d872010-03-27 22:47:23 +00001234 _, stderr = p.communicate()
1235 self.assertIn(b'KeyboardInterrupt', stderr)
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001236 self.assertNotEqual(p.wait(), 0)
Christian Heimesa342c012008-04-20 21:01:16 +00001237
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001238 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001239 p = self._kill_process('kill')
Florent Xiclunac049d872010-03-27 22:47:23 +00001240 _, stderr = p.communicate()
1241 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001242 self.assertEqual(p.wait(), -signal.SIGKILL)
Tim Peterse718f612004-10-12 21:51:32 +00001243
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001244 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001245 p = self._kill_process('terminate')
Florent Xiclunac049d872010-03-27 22:47:23 +00001246 _, stderr = p.communicate()
1247 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001248 self.assertEqual(p.wait(), -signal.SIGTERM)
1249
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001250 def test_send_signal_dead(self):
1251 # Sending a signal to a dead process
1252 self._kill_dead_process('send_signal', signal.SIGINT)
1253
1254 def test_kill_dead(self):
1255 # Killing a dead process
1256 self._kill_dead_process('kill')
1257
1258 def test_terminate_dead(self):
1259 # Terminating a dead process
1260 self._kill_dead_process('terminate')
1261
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001262 def check_close_std_fds(self, fds):
1263 # Issue #9905: test that subprocess pipes still work properly with
1264 # some standard fds closed
1265 stdin = 0
1266 newfds = []
1267 for a in fds:
1268 b = os.dup(a)
1269 newfds.append(b)
1270 if a == 0:
1271 stdin = b
1272 try:
1273 for fd in fds:
1274 os.close(fd)
1275 out, err = subprocess.Popen([sys.executable, "-c",
1276 'import sys;'
1277 'sys.stdout.write("apple");'
1278 'sys.stdout.flush();'
1279 'sys.stderr.write("orange")'],
1280 stdin=stdin,
1281 stdout=subprocess.PIPE,
1282 stderr=subprocess.PIPE).communicate()
1283 err = support.strip_python_stderr(err)
1284 self.assertEqual((out, err), (b'apple', b'orange'))
1285 finally:
1286 for b, a in zip(newfds, fds):
1287 os.dup2(b, a)
1288 for b in newfds:
1289 os.close(b)
1290
1291 def test_close_fd_0(self):
1292 self.check_close_std_fds([0])
1293
1294 def test_close_fd_1(self):
1295 self.check_close_std_fds([1])
1296
1297 def test_close_fd_2(self):
1298 self.check_close_std_fds([2])
1299
1300 def test_close_fds_0_1(self):
1301 self.check_close_std_fds([0, 1])
1302
1303 def test_close_fds_0_2(self):
1304 self.check_close_std_fds([0, 2])
1305
1306 def test_close_fds_1_2(self):
1307 self.check_close_std_fds([1, 2])
1308
1309 def test_close_fds_0_1_2(self):
1310 # Issue #10806: test that subprocess pipes still work properly with
1311 # all standard fds closed.
1312 self.check_close_std_fds([0, 1, 2])
1313
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00001314 def test_remapping_std_fds(self):
1315 # open up some temporary files
1316 temps = [mkstemp() for i in range(3)]
1317 try:
1318 temp_fds = [fd for fd, fname in temps]
1319
1320 # unlink the files -- we won't need to reopen them
1321 for fd, fname in temps:
1322 os.unlink(fname)
1323
1324 # write some data to what will become stdin, and rewind
1325 os.write(temp_fds[1], b"STDIN")
1326 os.lseek(temp_fds[1], 0, 0)
1327
1328 # move the standard file descriptors out of the way
1329 saved_fds = [os.dup(fd) for fd in range(3)]
1330 try:
1331 # duplicate the file objects over the standard fd's
1332 for fd, temp_fd in enumerate(temp_fds):
1333 os.dup2(temp_fd, fd)
1334
1335 # now use those files in the "wrong" order, so that subprocess
1336 # has to rearrange them in the child
1337 p = subprocess.Popen([sys.executable, "-c",
1338 'import sys; got = sys.stdin.read();'
1339 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
1340 stdin=temp_fds[1],
1341 stdout=temp_fds[2],
1342 stderr=temp_fds[0])
1343 p.wait()
1344 finally:
1345 # restore the original fd's underneath sys.stdin, etc.
1346 for std, saved in enumerate(saved_fds):
1347 os.dup2(saved, std)
1348 os.close(saved)
1349
1350 for fd in temp_fds:
1351 os.lseek(fd, 0, 0)
1352
1353 out = os.read(temp_fds[2], 1024)
1354 err = support.strip_python_stderr(os.read(temp_fds[0], 1024))
1355 self.assertEqual(out, b"got STDIN")
1356 self.assertEqual(err, b"err")
1357
1358 finally:
1359 for fd in temp_fds:
1360 os.close(fd)
1361
Ross Lagerwalld98646e2011-07-27 07:16:31 +02001362 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
1363 # open up some temporary files
1364 temps = [mkstemp() for i in range(3)]
1365 temp_fds = [fd for fd, fname in temps]
1366 try:
1367 # unlink the files -- we won't need to reopen them
1368 for fd, fname in temps:
1369 os.unlink(fname)
1370
1371 # save a copy of the standard file descriptors
1372 saved_fds = [os.dup(fd) for fd in range(3)]
1373 try:
1374 # duplicate the temp files over the standard fd's 0, 1, 2
1375 for fd, temp_fd in enumerate(temp_fds):
1376 os.dup2(temp_fd, fd)
1377
1378 # write some data to what will become stdin, and rewind
1379 os.write(stdin_no, b"STDIN")
1380 os.lseek(stdin_no, 0, 0)
1381
1382 # now use those files in the given order, so that subprocess
1383 # has to rearrange them in the child
1384 p = subprocess.Popen([sys.executable, "-c",
1385 'import sys; got = sys.stdin.read();'
1386 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
1387 stdin=stdin_no,
1388 stdout=stdout_no,
1389 stderr=stderr_no)
1390 p.wait()
1391
1392 for fd in temp_fds:
1393 os.lseek(fd, 0, 0)
1394
1395 out = os.read(stdout_no, 1024)
1396 err = support.strip_python_stderr(os.read(stderr_no, 1024))
1397 finally:
1398 for std, saved in enumerate(saved_fds):
1399 os.dup2(saved, std)
1400 os.close(saved)
1401
1402 self.assertEqual(out, b"got STDIN")
1403 self.assertEqual(err, b"err")
1404
1405 finally:
1406 for fd in temp_fds:
1407 os.close(fd)
1408
1409 # When duping fds, if there arises a situation where one of the fds is
1410 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
1411 # This tests all combinations of this.
1412 def test_swap_fds(self):
1413 self.check_swap_fds(0, 1, 2)
1414 self.check_swap_fds(0, 2, 1)
1415 self.check_swap_fds(1, 0, 2)
1416 self.check_swap_fds(1, 2, 0)
1417 self.check_swap_fds(2, 0, 1)
1418 self.check_swap_fds(2, 1, 0)
1419
Victor Stinner13bb71c2010-04-23 21:41:56 +00001420 def test_surrogates_error_message(self):
Victor Stinner4d078042010-04-23 19:28:32 +00001421 def prepare():
1422 raise ValueError("surrogate:\uDCff")
1423
1424 try:
1425 subprocess.call(
1426 [sys.executable, "-c", "pass"],
1427 preexec_fn=prepare)
1428 except ValueError as err:
1429 # Pure Python implementations keeps the message
1430 self.assertIsNone(subprocess._posixsubprocess)
1431 self.assertEqual(str(err), "surrogate:\uDCff")
1432 except RuntimeError as err:
1433 # _posixsubprocess uses a default message
1434 self.assertIsNotNone(subprocess._posixsubprocess)
1435 self.assertEqual(str(err), "Exception occurred in preexec_fn.")
1436 else:
1437 self.fail("Expected ValueError or RuntimeError")
1438
Victor Stinner13bb71c2010-04-23 21:41:56 +00001439 def test_undecodable_env(self):
1440 for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')):
Victor Stinner13bb71c2010-04-23 21:41:56 +00001441 # test str with surrogates
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00001442 script = "import os; print(ascii(os.getenv(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00001443 env = os.environ.copy()
1444 env[key] = value
Victor Stinner89f3ad12010-10-14 10:43:31 +00001445 # Use C locale to get ascii for the locale encoding to force
1446 # surrogate-escaping of \xFF in the child process; otherwise it can
1447 # be decoded as-is if the default locale is latin-1.
Victor Stinnerebc78d22010-10-14 10:38:17 +00001448 env['LC_ALL'] = 'C'
Victor Stinner13bb71c2010-04-23 21:41:56 +00001449 stdout = subprocess.check_output(
1450 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00001451 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00001452 stdout = stdout.rstrip(b'\n\r')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001453 self.assertEqual(stdout.decode('ascii'), ascii(value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00001454
1455 # test bytes
1456 key = key.encode("ascii", "surrogateescape")
1457 value = value.encode("ascii", "surrogateescape")
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00001458 script = "import os; print(ascii(os.getenvb(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00001459 env = os.environ.copy()
1460 env[key] = value
Victor Stinner13bb71c2010-04-23 21:41:56 +00001461 stdout = subprocess.check_output(
1462 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00001463 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00001464 stdout = stdout.rstrip(b'\n\r')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001465 self.assertEqual(stdout.decode('ascii'), ascii(value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00001466
Victor Stinnerb745a742010-05-18 17:17:23 +00001467 def test_bytes_program(self):
1468 abs_program = os.fsencode(sys.executable)
1469 path, program = os.path.split(sys.executable)
1470 program = os.fsencode(program)
1471
1472 # absolute bytes path
1473 exitcode = subprocess.call([abs_program, "-c", "pass"])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001474 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00001475
1476 # bytes program, unicode PATH
1477 env = os.environ.copy()
1478 env["PATH"] = path
1479 exitcode = subprocess.call([program, "-c", "pass"], env=env)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001480 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00001481
1482 # bytes program, bytes PATH
1483 envb = os.environb.copy()
1484 envb[b"PATH"] = os.fsencode(path)
1485 exitcode = subprocess.call([program, "-c", "pass"], env=envb)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001486 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00001487
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001488 def test_pipe_cloexec(self):
1489 sleeper = support.findfile("input_reader.py", subdir="subprocessdata")
1490 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
1491
1492 p1 = subprocess.Popen([sys.executable, sleeper],
1493 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1494 stderr=subprocess.PIPE, close_fds=False)
1495
1496 self.addCleanup(p1.communicate, b'')
1497
1498 p2 = subprocess.Popen([sys.executable, fd_status],
1499 stdout=subprocess.PIPE, close_fds=False)
1500
1501 output, error = p2.communicate()
1502 result_fds = set(map(int, output.split(b',')))
1503 unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(),
1504 p1.stderr.fileno()])
1505
1506 self.assertFalse(result_fds & unwanted_fds,
1507 "Expected no fds from %r to be open in child, "
1508 "found %r" %
1509 (unwanted_fds, result_fds & unwanted_fds))
1510
1511 def test_pipe_cloexec_real_tools(self):
1512 qcat = support.findfile("qcat.py", subdir="subprocessdata")
1513 qgrep = support.findfile("qgrep.py", subdir="subprocessdata")
1514
1515 subdata = b'zxcvbn'
1516 data = subdata * 4 + b'\n'
1517
1518 p1 = subprocess.Popen([sys.executable, qcat],
1519 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1520 close_fds=False)
1521
1522 p2 = subprocess.Popen([sys.executable, qgrep, subdata],
1523 stdin=p1.stdout, stdout=subprocess.PIPE,
1524 close_fds=False)
1525
1526 self.addCleanup(p1.wait)
1527 self.addCleanup(p2.wait)
Gregory P. Smith886455c2012-01-21 22:05:10 -08001528 def kill_p1():
1529 try:
1530 p1.terminate()
1531 except ProcessLookupError:
1532 pass
1533 def kill_p2():
1534 try:
1535 p2.terminate()
1536 except ProcessLookupError:
1537 pass
1538 self.addCleanup(kill_p1)
1539 self.addCleanup(kill_p2)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001540
1541 p1.stdin.write(data)
1542 p1.stdin.close()
1543
1544 readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10)
1545
1546 self.assertTrue(readfiles, "The child hung")
1547 self.assertEqual(p2.stdout.read(), data)
1548
Victor Stinnerfaa8c132011-01-03 16:36:00 +00001549 p1.stdout.close()
1550 p2.stdout.close()
1551
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001552 def test_close_fds(self):
1553 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
1554
1555 fds = os.pipe()
1556 self.addCleanup(os.close, fds[0])
1557 self.addCleanup(os.close, fds[1])
1558
1559 open_fds = set(fds)
Gregory P. Smith8facece2012-01-21 14:01:08 -08001560 # add a bunch more fds
1561 for _ in range(9):
1562 fd = os.open("/dev/null", os.O_RDONLY)
1563 self.addCleanup(os.close, fd)
1564 open_fds.add(fd)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001565
1566 p = subprocess.Popen([sys.executable, fd_status],
1567 stdout=subprocess.PIPE, close_fds=False)
1568 output, ignored = p.communicate()
1569 remaining_fds = set(map(int, output.split(b',')))
1570
1571 self.assertEqual(remaining_fds & open_fds, open_fds,
1572 "Some fds were closed")
1573
1574 p = subprocess.Popen([sys.executable, fd_status],
1575 stdout=subprocess.PIPE, close_fds=True)
1576 output, ignored = p.communicate()
1577 remaining_fds = set(map(int, output.split(b',')))
1578
1579 self.assertFalse(remaining_fds & open_fds,
1580 "Some fds were left open")
1581 self.assertIn(1, remaining_fds, "Subprocess failed")
1582
Gregory P. Smith8facece2012-01-21 14:01:08 -08001583 # Keep some of the fd's we opened open in the subprocess.
1584 # This tests _posixsubprocess.c's proper handling of fds_to_keep.
1585 fds_to_keep = set(open_fds.pop() for _ in range(8))
1586 p = subprocess.Popen([sys.executable, fd_status],
1587 stdout=subprocess.PIPE, close_fds=True,
1588 pass_fds=())
1589 output, ignored = p.communicate()
1590 remaining_fds = set(map(int, output.split(b',')))
1591
1592 self.assertFalse(remaining_fds & fds_to_keep & open_fds,
1593 "Some fds not in pass_fds were left open")
1594 self.assertIn(1, remaining_fds, "Subprocess failed")
1595
Victor Stinner88701e22011-06-01 13:13:04 +02001596 # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
1597 # descriptor of a pipe closed in the parent process is valid in the
1598 # child process according to fstat(), but the mode of the file
1599 # descriptor is invalid, and read or write raise an error.
1600 @support.requires_mac_ver(10, 5)
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00001601 def test_pass_fds(self):
1602 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
1603
1604 open_fds = set()
1605
1606 for x in range(5):
1607 fds = os.pipe()
1608 self.addCleanup(os.close, fds[0])
1609 self.addCleanup(os.close, fds[1])
1610 open_fds.update(fds)
1611
1612 for fd in open_fds:
1613 p = subprocess.Popen([sys.executable, fd_status],
1614 stdout=subprocess.PIPE, close_fds=True,
1615 pass_fds=(fd, ))
1616 output, ignored = p.communicate()
1617
1618 remaining_fds = set(map(int, output.split(b',')))
1619 to_be_closed = open_fds - {fd}
1620
1621 self.assertIn(fd, remaining_fds, "fd to be passed not passed")
1622 self.assertFalse(remaining_fds & to_be_closed,
1623 "fd to be closed passed")
1624
1625 # pass_fds overrides close_fds with a warning.
1626 with self.assertWarns(RuntimeWarning) as context:
1627 self.assertFalse(subprocess.call(
1628 [sys.executable, "-c", "import sys; sys.exit(0)"],
1629 close_fds=False, pass_fds=(fd, )))
1630 self.assertIn('overriding close_fds', str(context.warning))
1631
Gregory P. Smithe14e9c22011-03-15 14:55:17 -04001632 def test_stdout_stdin_are_single_inout_fd(self):
1633 with io.open(os.devnull, "r+") as inout:
1634 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
1635 stdout=inout, stdin=inout)
1636 p.wait()
1637
1638 def test_stdout_stderr_are_single_inout_fd(self):
1639 with io.open(os.devnull, "r+") as inout:
1640 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
1641 stdout=inout, stderr=inout)
1642 p.wait()
1643
1644 def test_stderr_stdin_are_single_inout_fd(self):
1645 with io.open(os.devnull, "r+") as inout:
1646 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
1647 stderr=inout, stdin=inout)
1648 p.wait()
1649
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00001650 def test_wait_when_sigchild_ignored(self):
1651 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
1652 sigchild_ignore = support.findfile("sigchild_ignore.py",
1653 subdir="subprocessdata")
1654 p = subprocess.Popen([sys.executable, sigchild_ignore],
1655 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1656 stdout, stderr = p.communicate()
1657 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
Gregory P. Smitha80f4fb2010-12-14 15:23:02 +00001658 " non-zero with this error:\n%s" %
1659 stderr.decode('utf8'))
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00001660
Antoine Pitrou7b98d022011-03-19 17:04:13 +01001661 def test_select_unbuffered(self):
1662 # Issue #11459: bufsize=0 should really set the pipes as
1663 # unbuffered (and therefore let select() work properly).
1664 select = support.import_module("select")
1665 p = subprocess.Popen([sys.executable, "-c",
1666 'import sys;'
1667 'sys.stdout.write("apple")'],
1668 stdout=subprocess.PIPE,
1669 bufsize=0)
1670 f = p.stdout
Ross Lagerwall17ace7a2011-03-26 21:21:46 +02001671 self.addCleanup(f.close)
Antoine Pitrou7b98d022011-03-19 17:04:13 +01001672 try:
1673 self.assertEqual(f.read(4), b"appl")
1674 self.assertIn(f, select.select([f], [], [], 0.0)[0])
1675 finally:
1676 p.wait()
1677
Charles-François Natali134a8ba2011-08-18 18:49:39 +02001678 def test_zombie_fast_process_del(self):
1679 # Issue #12650: on Unix, if Popen.__del__() was called before the
1680 # process exited, it wouldn't be added to subprocess._active, and would
1681 # remain a zombie.
1682 # spawn a Popen, and delete its reference before it exits
1683 p = subprocess.Popen([sys.executable, "-c",
1684 'import sys, time;'
1685 'time.sleep(0.2)'],
1686 stdout=subprocess.PIPE,
1687 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02001688 self.addCleanup(p.stdout.close)
1689 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02001690 ident = id(p)
1691 pid = p.pid
1692 del p
1693 # check that p is in the active processes list
1694 self.assertIn(ident, [id(o) for o in subprocess._active])
1695
Charles-François Natali134a8ba2011-08-18 18:49:39 +02001696 def test_leak_fast_process_del_killed(self):
1697 # Issue #12650: on Unix, if Popen.__del__() was called before the
1698 # process exited, and the process got killed by a signal, it would never
1699 # be removed from subprocess._active, which triggered a FD and memory
1700 # leak.
1701 # spawn a Popen, delete its reference and kill it
1702 p = subprocess.Popen([sys.executable, "-c",
1703 'import time;'
1704 'time.sleep(3)'],
1705 stdout=subprocess.PIPE,
1706 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02001707 self.addCleanup(p.stdout.close)
1708 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02001709 ident = id(p)
1710 pid = p.pid
1711 del p
1712 os.kill(pid, signal.SIGKILL)
1713 # check that p is in the active processes list
1714 self.assertIn(ident, [id(o) for o in subprocess._active])
1715
1716 # let some time for the process to exit, and create a new Popen: this
1717 # should trigger the wait() of p
1718 time.sleep(0.2)
1719 with self.assertRaises(EnvironmentError) as c:
1720 with subprocess.Popen(['nonexisting_i_hope'],
1721 stdout=subprocess.PIPE,
1722 stderr=subprocess.PIPE) as proc:
1723 pass
1724 # p should have been wait()ed on, and removed from the _active list
1725 self.assertRaises(OSError, os.waitpid, pid, 0)
1726 self.assertNotIn(ident, [id(o) for o in subprocess._active])
1727
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001728
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001729@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +00001730class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001731
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001732 def test_startupinfo(self):
1733 # startupinfo argument
1734 # We uses hardcoded constants, because we do not want to
1735 # depend on win32all.
1736 STARTF_USESHOWWINDOW = 1
1737 SW_MAXIMIZE = 3
1738 startupinfo = subprocess.STARTUPINFO()
1739 startupinfo.dwFlags = STARTF_USESHOWWINDOW
1740 startupinfo.wShowWindow = SW_MAXIMIZE
1741 # Since Python is a console process, it won't be affected
1742 # by wShowWindow, but the argument should be silently
1743 # ignored
1744 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001745 startupinfo=startupinfo)
1746
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001747 def test_creationflags(self):
1748 # creationflags argument
1749 CREATE_NEW_CONSOLE = 16
1750 sys.stderr.write(" a DOS box should flash briefly ...\n")
1751 subprocess.call(sys.executable +
1752 ' -c "import time; time.sleep(0.25)"',
1753 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001754
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001755 def test_invalid_args(self):
1756 # invalid arguments should raise ValueError
1757 self.assertRaises(ValueError, subprocess.call,
1758 [sys.executable, "-c",
1759 "import sys; sys.exit(47)"],
1760 preexec_fn=lambda: 1)
1761 self.assertRaises(ValueError, subprocess.call,
1762 [sys.executable, "-c",
1763 "import sys; sys.exit(47)"],
1764 stdout=subprocess.PIPE,
1765 close_fds=True)
1766
1767 def test_close_fds(self):
1768 # close file descriptors
1769 rc = subprocess.call([sys.executable, "-c",
1770 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001771 close_fds=True)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001772 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001773
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001774 def test_shell_sequence(self):
1775 # Run command through the shell (sequence)
1776 newenv = os.environ.copy()
1777 newenv["FRUIT"] = "physalis"
1778 p = subprocess.Popen(["set"], shell=1,
1779 stdout=subprocess.PIPE,
1780 env=newenv)
Brian Curtin19a53792010-11-05 17:09:05 +00001781 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001782 self.assertIn(b"physalis", p.stdout.read())
Guido van Rossume7ba4952007-06-06 23:52:48 +00001783
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001784 def test_shell_string(self):
1785 # Run command through the shell (string)
1786 newenv = os.environ.copy()
1787 newenv["FRUIT"] = "physalis"
1788 p = subprocess.Popen("set", shell=1,
1789 stdout=subprocess.PIPE,
1790 env=newenv)
Brian Curtin19a53792010-11-05 17:09:05 +00001791 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001792 self.assertIn(b"physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001793
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001794 def test_call_string(self):
1795 # call() function with string argument on Windows
1796 rc = subprocess.call(sys.executable +
1797 ' -c "import sys; sys.exit(47)"')
1798 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001799
Florent Xicluna4886d242010-03-08 13:27:26 +00001800 def _kill_process(self, method, *args):
1801 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroua4024e22010-09-24 18:57:01 +00001802 p = subprocess.Popen([sys.executable, "-c", """if 1:
1803 import sys, time
1804 sys.stdout.write('x\\n')
1805 sys.stdout.flush()
1806 time.sleep(30)
1807 """],
1808 stdin=subprocess.PIPE,
1809 stdout=subprocess.PIPE,
1810 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +00001811 self.addCleanup(p.stdout.close)
1812 self.addCleanup(p.stderr.close)
1813 self.addCleanup(p.stdin.close)
Antoine Pitroua4024e22010-09-24 18:57:01 +00001814 # Wait for the interpreter to be completely initialized before
1815 # sending any signal.
1816 p.stdout.read(1)
1817 getattr(p, method)(*args)
Florent Xiclunac049d872010-03-27 22:47:23 +00001818 _, stderr = p.communicate()
1819 self.assertStderrEqual(stderr, b'')
Antoine Pitroua4024e22010-09-24 18:57:01 +00001820 returncode = p.wait()
Florent Xicluna4886d242010-03-08 13:27:26 +00001821 self.assertNotEqual(returncode, 0)
1822
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001823 def _kill_dead_process(self, method, *args):
1824 p = subprocess.Popen([sys.executable, "-c", """if 1:
1825 import sys, time
1826 sys.stdout.write('x\\n')
1827 sys.stdout.flush()
1828 sys.exit(42)
1829 """],
1830 stdin=subprocess.PIPE,
1831 stdout=subprocess.PIPE,
1832 stderr=subprocess.PIPE)
1833 self.addCleanup(p.stdout.close)
1834 self.addCleanup(p.stderr.close)
1835 self.addCleanup(p.stdin.close)
1836 # Wait for the interpreter to be completely initialized before
1837 # sending any signal.
1838 p.stdout.read(1)
1839 # The process should end after this
1840 time.sleep(1)
1841 # This shouldn't raise even though the child is now dead
1842 getattr(p, method)(*args)
1843 _, stderr = p.communicate()
1844 self.assertStderrEqual(stderr, b'')
1845 rc = p.wait()
1846 self.assertEqual(rc, 42)
1847
Florent Xicluna4886d242010-03-08 13:27:26 +00001848 def test_send_signal(self):
1849 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimesa342c012008-04-20 21:01:16 +00001850
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001851 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001852 self._kill_process('kill')
Christian Heimesa342c012008-04-20 21:01:16 +00001853
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001854 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001855 self._kill_process('terminate')
Christian Heimesa342c012008-04-20 21:01:16 +00001856
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001857 def test_send_signal_dead(self):
1858 self._kill_dead_process('send_signal', signal.SIGTERM)
1859
1860 def test_kill_dead(self):
1861 self._kill_dead_process('kill')
1862
1863 def test_terminate_dead(self):
1864 self._kill_dead_process('terminate')
1865
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001866
Brett Cannona23810f2008-05-26 19:04:21 +00001867# The module says:
1868# "NB This only works (and is only relevant) for UNIX."
1869#
1870# Actually, getoutput should work on any platform with an os.popen, but
1871# I'll take the comment as given, and skip this suite.
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001872@unittest.skipUnless(os.name == 'posix', "only relevant for UNIX")
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001873class CommandTests(unittest.TestCase):
1874 def test_getoutput(self):
1875 self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
1876 self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
1877 (0, 'xyzzy'))
Brett Cannona23810f2008-05-26 19:04:21 +00001878
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001879 # we use mkdtemp in the next line to create an empty directory
1880 # under our exclusive control; from that, we can invent a pathname
1881 # that we _know_ won't exist. This is guaranteed to fail.
1882 dir = None
1883 try:
1884 dir = tempfile.mkdtemp()
1885 name = os.path.join(dir, "foo")
Brett Cannona23810f2008-05-26 19:04:21 +00001886
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001887 status, output = subprocess.getstatusoutput('cat ' + name)
1888 self.assertNotEqual(status, 0)
1889 finally:
1890 if dir is not None:
1891 os.rmdir(dir)
Brett Cannona23810f2008-05-26 19:04:21 +00001892
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001893
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001894@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
1895 "poll system call not supported")
1896class ProcessTestCaseNoPoll(ProcessTestCase):
1897 def setUp(self):
1898 subprocess._has_poll = False
1899 ProcessTestCase.setUp(self)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001900
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001901 def tearDown(self):
1902 subprocess._has_poll = True
1903 ProcessTestCase.tearDown(self)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001904
1905
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001906@unittest.skipUnless(getattr(subprocess, '_posixsubprocess', False),
1907 "_posixsubprocess extension module not found.")
1908class ProcessTestCasePOSIXPurePython(ProcessTestCase, POSIXProcessTestCase):
Gregory P. Smith7439e7b2011-05-28 09:06:02 -07001909 @classmethod
1910 def setUpClass(cls):
1911 global subprocess
1912 assert subprocess._posixsubprocess
1913 # Reimport subprocess while forcing _posixsubprocess to not exist.
1914 with support.check_warnings(('.*_posixsubprocess .* not being used.*',
1915 RuntimeWarning)):
1916 subprocess = support.import_fresh_module(
1917 'subprocess', blocked=['_posixsubprocess'])
1918 assert not subprocess._posixsubprocess
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001919
Gregory P. Smith7439e7b2011-05-28 09:06:02 -07001920 @classmethod
1921 def tearDownClass(cls):
1922 global subprocess
1923 # Reimport subprocess as it should be, restoring order to the universe.
1924 subprocess = support.import_fresh_module('subprocess')
1925 assert subprocess._posixsubprocess
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001926
1927
Gregory P. Smitha59c59f2010-03-01 00:17:40 +00001928class HelperFunctionTests(unittest.TestCase):
Gregory P. Smithaf6d3b82010-03-01 02:56:44 +00001929 @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
Gregory P. Smitha59c59f2010-03-01 00:17:40 +00001930 def test_eintr_retry_call(self):
1931 record_calls = []
1932 def fake_os_func(*args):
1933 record_calls.append(args)
1934 if len(record_calls) == 2:
1935 raise OSError(errno.EINTR, "fake interrupted system call")
1936 return tuple(reversed(args))
1937
1938 self.assertEqual((999, 256),
1939 subprocess._eintr_retry_call(fake_os_func, 256, 999))
1940 self.assertEqual([(256, 999)], record_calls)
1941 # This time there will be an EINTR so it will loop once.
1942 self.assertEqual((666,),
1943 subprocess._eintr_retry_call(fake_os_func, 666))
1944 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
1945
1946
Tim Golden126c2962010-08-11 14:20:40 +00001947@unittest.skipUnless(mswindows, "Windows-specific tests")
1948class CommandsWithSpaces (BaseTestCase):
1949
1950 def setUp(self):
1951 super().setUp()
1952 f, fname = mkstemp(".py", "te st")
1953 self.fname = fname.lower ()
1954 os.write(f, b"import sys;"
1955 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
1956 )
1957 os.close(f)
1958
1959 def tearDown(self):
1960 os.remove(self.fname)
1961 super().tearDown()
1962
1963 def with_spaces(self, *args, **kwargs):
1964 kwargs['stdout'] = subprocess.PIPE
1965 p = subprocess.Popen(*args, **kwargs)
Brian Curtin19a53792010-11-05 17:09:05 +00001966 self.addCleanup(p.stdout.close)
Tim Golden126c2962010-08-11 14:20:40 +00001967 self.assertEqual(
1968 p.stdout.read ().decode("mbcs"),
1969 "2 [%r, 'ab cd']" % self.fname
1970 )
1971
1972 def test_shell_string_with_spaces(self):
1973 # call() function with string argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00001974 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1975 "ab cd"), shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00001976
1977 def test_shell_sequence_with_spaces(self):
1978 # call() function with sequence argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00001979 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00001980
1981 def test_noshell_string_with_spaces(self):
1982 # call() function with string argument with spaces on Windows
1983 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1984 "ab cd"))
1985
1986 def test_noshell_sequence_with_spaces(self):
1987 # call() function with sequence argument with spaces on Windows
1988 self.with_spaces([sys.executable, self.fname, "ab cd"])
1989
Brian Curtin79cdb662010-12-03 02:46:02 +00001990
Georg Brandla86b2622012-02-20 21:34:57 +01001991class ContextManagerTests(BaseTestCase):
Brian Curtin79cdb662010-12-03 02:46:02 +00001992
1993 def test_pipe(self):
1994 with subprocess.Popen([sys.executable, "-c",
1995 "import sys;"
1996 "sys.stdout.write('stdout');"
1997 "sys.stderr.write('stderr');"],
1998 stdout=subprocess.PIPE,
1999 stderr=subprocess.PIPE) as proc:
2000 self.assertEqual(proc.stdout.read(), b"stdout")
2001 self.assertStderrEqual(proc.stderr.read(), b"stderr")
2002
2003 self.assertTrue(proc.stdout.closed)
2004 self.assertTrue(proc.stderr.closed)
2005
2006 def test_returncode(self):
2007 with subprocess.Popen([sys.executable, "-c",
2008 "import sys; sys.exit(100)"]) as proc:
Gregory P. Smithc9557af2011-05-11 22:18:23 -07002009 pass
2010 # __exit__ calls wait(), so the returncode should be set
Brian Curtin79cdb662010-12-03 02:46:02 +00002011 self.assertEqual(proc.returncode, 100)
2012
2013 def test_communicate_stdin(self):
2014 with subprocess.Popen([sys.executable, "-c",
2015 "import sys;"
2016 "sys.exit(sys.stdin.read() == 'context')"],
2017 stdin=subprocess.PIPE) as proc:
2018 proc.communicate(b"context")
2019 self.assertEqual(proc.returncode, 1)
2020
2021 def test_invalid_args(self):
2022 with self.assertRaises(EnvironmentError) as c:
2023 with subprocess.Popen(['nonexisting_i_hope'],
2024 stdout=subprocess.PIPE,
2025 stderr=subprocess.PIPE) as proc:
2026 pass
2027
Andrew Svetlov57a12332012-12-26 23:31:45 +02002028 self.assertEqual(c.exception.errno, errno.ENOENT)
Brian Curtin79cdb662010-12-03 02:46:02 +00002029
2030
Gregory P. Smith961e0e82011-03-15 15:43:39 -04002031def test_main():
2032 unit_tests = (ProcessTestCase,
2033 POSIXProcessTestCase,
2034 Win32ProcessTestCase,
2035 ProcessTestCasePOSIXPurePython,
2036 CommandTests,
2037 ProcessTestCaseNoPoll,
2038 HelperFunctionTests,
2039 CommandsWithSpaces,
Antoine Pitrouab85ff32011-07-23 22:03:45 +02002040 ContextManagerTests,
2041 )
Gregory P. Smith961e0e82011-03-15 15:43:39 -04002042
2043 support.run_unittest(*unit_tests)
2044 support.reap_children()
2045
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002046if __name__ == "__main__":
Gregory P. Smithe14e9c22011-03-15 14:55:17 -04002047 unittest.main()