blob: f7a7b115cdfe669853572c1929b8f9aa04da5314 [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
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001209 def _kill_dead_process(self, method, *args):
1210 # Do not inherit file handles from the parent.
1211 # It should fix failures on some platforms.
1212 p = subprocess.Popen([sys.executable, "-c", """if 1:
1213 import sys, time
1214 sys.stdout.write('x\\n')
1215 sys.stdout.flush()
1216 """],
1217 close_fds=True,
1218 stdin=subprocess.PIPE,
1219 stdout=subprocess.PIPE,
1220 stderr=subprocess.PIPE)
1221 # Wait for the interpreter to be completely initialized before
1222 # sending any signal.
1223 p.stdout.read(1)
1224 # The process should end after this
1225 time.sleep(1)
1226 # This shouldn't raise even though the child is now dead
1227 getattr(p, method)(*args)
1228 p.communicate()
1229
Florent Xicluna4886d242010-03-08 13:27:26 +00001230 def test_send_signal(self):
1231 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunac049d872010-03-27 22:47:23 +00001232 _, stderr = p.communicate()
1233 self.assertIn(b'KeyboardInterrupt', stderr)
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001234 self.assertNotEqual(p.wait(), 0)
Christian Heimesa342c012008-04-20 21:01:16 +00001235
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001236 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001237 p = self._kill_process('kill')
Florent Xiclunac049d872010-03-27 22:47:23 +00001238 _, stderr = p.communicate()
1239 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001240 self.assertEqual(p.wait(), -signal.SIGKILL)
Tim Peterse718f612004-10-12 21:51:32 +00001241
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001242 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001243 p = self._kill_process('terminate')
Florent Xiclunac049d872010-03-27 22:47:23 +00001244 _, stderr = p.communicate()
1245 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001246 self.assertEqual(p.wait(), -signal.SIGTERM)
1247
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001248 def test_send_signal_dead(self):
1249 # Sending a signal to a dead process
1250 self._kill_dead_process('send_signal', signal.SIGINT)
1251
1252 def test_kill_dead(self):
1253 # Killing a dead process
1254 self._kill_dead_process('kill')
1255
1256 def test_terminate_dead(self):
1257 # Terminating a dead process
1258 self._kill_dead_process('terminate')
1259
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +00001260 def check_close_std_fds(self, fds):
1261 # Issue #9905: test that subprocess pipes still work properly with
1262 # some standard fds closed
1263 stdin = 0
1264 newfds = []
1265 for a in fds:
1266 b = os.dup(a)
1267 newfds.append(b)
1268 if a == 0:
1269 stdin = b
1270 try:
1271 for fd in fds:
1272 os.close(fd)
1273 out, err = subprocess.Popen([sys.executable, "-c",
1274 'import sys;'
1275 'sys.stdout.write("apple");'
1276 'sys.stdout.flush();'
1277 'sys.stderr.write("orange")'],
1278 stdin=stdin,
1279 stdout=subprocess.PIPE,
1280 stderr=subprocess.PIPE).communicate()
1281 err = support.strip_python_stderr(err)
1282 self.assertEqual((out, err), (b'apple', b'orange'))
1283 finally:
1284 for b, a in zip(newfds, fds):
1285 os.dup2(b, a)
1286 for b in newfds:
1287 os.close(b)
1288
1289 def test_close_fd_0(self):
1290 self.check_close_std_fds([0])
1291
1292 def test_close_fd_1(self):
1293 self.check_close_std_fds([1])
1294
1295 def test_close_fd_2(self):
1296 self.check_close_std_fds([2])
1297
1298 def test_close_fds_0_1(self):
1299 self.check_close_std_fds([0, 1])
1300
1301 def test_close_fds_0_2(self):
1302 self.check_close_std_fds([0, 2])
1303
1304 def test_close_fds_1_2(self):
1305 self.check_close_std_fds([1, 2])
1306
1307 def test_close_fds_0_1_2(self):
1308 # Issue #10806: test that subprocess pipes still work properly with
1309 # all standard fds closed.
1310 self.check_close_std_fds([0, 1, 2])
1311
Antoine Pitrou95aaeee2011-01-03 21:15:48 +00001312 def test_remapping_std_fds(self):
1313 # open up some temporary files
1314 temps = [mkstemp() for i in range(3)]
1315 try:
1316 temp_fds = [fd for fd, fname in temps]
1317
1318 # unlink the files -- we won't need to reopen them
1319 for fd, fname in temps:
1320 os.unlink(fname)
1321
1322 # write some data to what will become stdin, and rewind
1323 os.write(temp_fds[1], b"STDIN")
1324 os.lseek(temp_fds[1], 0, 0)
1325
1326 # move the standard file descriptors out of the way
1327 saved_fds = [os.dup(fd) for fd in range(3)]
1328 try:
1329 # duplicate the file objects over the standard fd's
1330 for fd, temp_fd in enumerate(temp_fds):
1331 os.dup2(temp_fd, fd)
1332
1333 # now use those files in the "wrong" order, so that subprocess
1334 # has to rearrange them in the child
1335 p = subprocess.Popen([sys.executable, "-c",
1336 'import sys; got = sys.stdin.read();'
1337 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
1338 stdin=temp_fds[1],
1339 stdout=temp_fds[2],
1340 stderr=temp_fds[0])
1341 p.wait()
1342 finally:
1343 # restore the original fd's underneath sys.stdin, etc.
1344 for std, saved in enumerate(saved_fds):
1345 os.dup2(saved, std)
1346 os.close(saved)
1347
1348 for fd in temp_fds:
1349 os.lseek(fd, 0, 0)
1350
1351 out = os.read(temp_fds[2], 1024)
1352 err = support.strip_python_stderr(os.read(temp_fds[0], 1024))
1353 self.assertEqual(out, b"got STDIN")
1354 self.assertEqual(err, b"err")
1355
1356 finally:
1357 for fd in temp_fds:
1358 os.close(fd)
1359
Ross Lagerwalld98646e2011-07-27 07:16:31 +02001360 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
1361 # open up some temporary files
1362 temps = [mkstemp() for i in range(3)]
1363 temp_fds = [fd for fd, fname in temps]
1364 try:
1365 # unlink the files -- we won't need to reopen them
1366 for fd, fname in temps:
1367 os.unlink(fname)
1368
1369 # save a copy of the standard file descriptors
1370 saved_fds = [os.dup(fd) for fd in range(3)]
1371 try:
1372 # duplicate the temp files over the standard fd's 0, 1, 2
1373 for fd, temp_fd in enumerate(temp_fds):
1374 os.dup2(temp_fd, fd)
1375
1376 # write some data to what will become stdin, and rewind
1377 os.write(stdin_no, b"STDIN")
1378 os.lseek(stdin_no, 0, 0)
1379
1380 # now use those files in the given order, so that subprocess
1381 # has to rearrange them in the child
1382 p = subprocess.Popen([sys.executable, "-c",
1383 'import sys; got = sys.stdin.read();'
1384 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
1385 stdin=stdin_no,
1386 stdout=stdout_no,
1387 stderr=stderr_no)
1388 p.wait()
1389
1390 for fd in temp_fds:
1391 os.lseek(fd, 0, 0)
1392
1393 out = os.read(stdout_no, 1024)
1394 err = support.strip_python_stderr(os.read(stderr_no, 1024))
1395 finally:
1396 for std, saved in enumerate(saved_fds):
1397 os.dup2(saved, std)
1398 os.close(saved)
1399
1400 self.assertEqual(out, b"got STDIN")
1401 self.assertEqual(err, b"err")
1402
1403 finally:
1404 for fd in temp_fds:
1405 os.close(fd)
1406
1407 # When duping fds, if there arises a situation where one of the fds is
1408 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
1409 # This tests all combinations of this.
1410 def test_swap_fds(self):
1411 self.check_swap_fds(0, 1, 2)
1412 self.check_swap_fds(0, 2, 1)
1413 self.check_swap_fds(1, 0, 2)
1414 self.check_swap_fds(1, 2, 0)
1415 self.check_swap_fds(2, 0, 1)
1416 self.check_swap_fds(2, 1, 0)
1417
Victor Stinner13bb71c2010-04-23 21:41:56 +00001418 def test_surrogates_error_message(self):
Victor Stinner4d078042010-04-23 19:28:32 +00001419 def prepare():
1420 raise ValueError("surrogate:\uDCff")
1421
1422 try:
1423 subprocess.call(
1424 [sys.executable, "-c", "pass"],
1425 preexec_fn=prepare)
1426 except ValueError as err:
1427 # Pure Python implementations keeps the message
1428 self.assertIsNone(subprocess._posixsubprocess)
1429 self.assertEqual(str(err), "surrogate:\uDCff")
1430 except RuntimeError as err:
1431 # _posixsubprocess uses a default message
1432 self.assertIsNotNone(subprocess._posixsubprocess)
1433 self.assertEqual(str(err), "Exception occurred in preexec_fn.")
1434 else:
1435 self.fail("Expected ValueError or RuntimeError")
1436
Victor Stinner13bb71c2010-04-23 21:41:56 +00001437 def test_undecodable_env(self):
1438 for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')):
Victor Stinner13bb71c2010-04-23 21:41:56 +00001439 # test str with surrogates
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00001440 script = "import os; print(ascii(os.getenv(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00001441 env = os.environ.copy()
1442 env[key] = value
Victor Stinner89f3ad12010-10-14 10:43:31 +00001443 # Use C locale to get ascii for the locale encoding to force
1444 # surrogate-escaping of \xFF in the child process; otherwise it can
1445 # be decoded as-is if the default locale is latin-1.
Victor Stinnerebc78d22010-10-14 10:38:17 +00001446 env['LC_ALL'] = 'C'
Victor Stinner13bb71c2010-04-23 21:41:56 +00001447 stdout = subprocess.check_output(
1448 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00001449 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00001450 stdout = stdout.rstrip(b'\n\r')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001451 self.assertEqual(stdout.decode('ascii'), ascii(value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00001452
1453 # test bytes
1454 key = key.encode("ascii", "surrogateescape")
1455 value = value.encode("ascii", "surrogateescape")
Antoine Pitroufb8db8f2010-09-19 22:46:05 +00001456 script = "import os; print(ascii(os.getenvb(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +00001457 env = os.environ.copy()
1458 env[key] = value
Victor Stinner13bb71c2010-04-23 21:41:56 +00001459 stdout = subprocess.check_output(
1460 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +00001461 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +00001462 stdout = stdout.rstrip(b'\n\r')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001463 self.assertEqual(stdout.decode('ascii'), ascii(value))
Victor Stinner13bb71c2010-04-23 21:41:56 +00001464
Victor Stinnerb745a742010-05-18 17:17:23 +00001465 def test_bytes_program(self):
1466 abs_program = os.fsencode(sys.executable)
1467 path, program = os.path.split(sys.executable)
1468 program = os.fsencode(program)
1469
1470 # absolute bytes path
1471 exitcode = subprocess.call([abs_program, "-c", "pass"])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001472 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00001473
1474 # bytes program, unicode PATH
1475 env = os.environ.copy()
1476 env["PATH"] = path
1477 exitcode = subprocess.call([program, "-c", "pass"], env=env)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001478 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00001479
1480 # bytes program, bytes PATH
1481 envb = os.environb.copy()
1482 envb[b"PATH"] = os.fsencode(path)
1483 exitcode = subprocess.call([program, "-c", "pass"], env=envb)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001484 self.assertEqual(exitcode, 0)
Victor Stinnerb745a742010-05-18 17:17:23 +00001485
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001486 def test_pipe_cloexec(self):
1487 sleeper = support.findfile("input_reader.py", subdir="subprocessdata")
1488 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
1489
1490 p1 = subprocess.Popen([sys.executable, sleeper],
1491 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1492 stderr=subprocess.PIPE, close_fds=False)
1493
1494 self.addCleanup(p1.communicate, b'')
1495
1496 p2 = subprocess.Popen([sys.executable, fd_status],
1497 stdout=subprocess.PIPE, close_fds=False)
1498
1499 output, error = p2.communicate()
1500 result_fds = set(map(int, output.split(b',')))
1501 unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(),
1502 p1.stderr.fileno()])
1503
1504 self.assertFalse(result_fds & unwanted_fds,
1505 "Expected no fds from %r to be open in child, "
1506 "found %r" %
1507 (unwanted_fds, result_fds & unwanted_fds))
1508
1509 def test_pipe_cloexec_real_tools(self):
1510 qcat = support.findfile("qcat.py", subdir="subprocessdata")
1511 qgrep = support.findfile("qgrep.py", subdir="subprocessdata")
1512
1513 subdata = b'zxcvbn'
1514 data = subdata * 4 + b'\n'
1515
1516 p1 = subprocess.Popen([sys.executable, qcat],
1517 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1518 close_fds=False)
1519
1520 p2 = subprocess.Popen([sys.executable, qgrep, subdata],
1521 stdin=p1.stdout, stdout=subprocess.PIPE,
1522 close_fds=False)
1523
1524 self.addCleanup(p1.wait)
1525 self.addCleanup(p2.wait)
Gregory P. Smith886455c2012-01-21 22:05:10 -08001526 def kill_p1():
1527 try:
1528 p1.terminate()
1529 except ProcessLookupError:
1530 pass
1531 def kill_p2():
1532 try:
1533 p2.terminate()
1534 except ProcessLookupError:
1535 pass
1536 self.addCleanup(kill_p1)
1537 self.addCleanup(kill_p2)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001538
1539 p1.stdin.write(data)
1540 p1.stdin.close()
1541
1542 readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10)
1543
1544 self.assertTrue(readfiles, "The child hung")
1545 self.assertEqual(p2.stdout.read(), data)
1546
Victor Stinnerfaa8c132011-01-03 16:36:00 +00001547 p1.stdout.close()
1548 p2.stdout.close()
1549
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001550 def test_close_fds(self):
1551 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
1552
1553 fds = os.pipe()
1554 self.addCleanup(os.close, fds[0])
1555 self.addCleanup(os.close, fds[1])
1556
1557 open_fds = set(fds)
Gregory P. Smith8facece2012-01-21 14:01:08 -08001558 # add a bunch more fds
1559 for _ in range(9):
1560 fd = os.open("/dev/null", os.O_RDONLY)
1561 self.addCleanup(os.close, fd)
1562 open_fds.add(fd)
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001563
1564 p = subprocess.Popen([sys.executable, fd_status],
1565 stdout=subprocess.PIPE, close_fds=False)
1566 output, ignored = p.communicate()
1567 remaining_fds = set(map(int, output.split(b',')))
1568
1569 self.assertEqual(remaining_fds & open_fds, open_fds,
1570 "Some fds were closed")
1571
1572 p = subprocess.Popen([sys.executable, fd_status],
1573 stdout=subprocess.PIPE, close_fds=True)
1574 output, ignored = p.communicate()
1575 remaining_fds = set(map(int, output.split(b',')))
1576
1577 self.assertFalse(remaining_fds & open_fds,
1578 "Some fds were left open")
1579 self.assertIn(1, remaining_fds, "Subprocess failed")
1580
Gregory P. Smith8facece2012-01-21 14:01:08 -08001581 # Keep some of the fd's we opened open in the subprocess.
1582 # This tests _posixsubprocess.c's proper handling of fds_to_keep.
1583 fds_to_keep = set(open_fds.pop() for _ in range(8))
1584 p = subprocess.Popen([sys.executable, fd_status],
1585 stdout=subprocess.PIPE, close_fds=True,
1586 pass_fds=())
1587 output, ignored = p.communicate()
1588 remaining_fds = set(map(int, output.split(b',')))
1589
1590 self.assertFalse(remaining_fds & fds_to_keep & open_fds,
1591 "Some fds not in pass_fds were left open")
1592 self.assertIn(1, remaining_fds, "Subprocess failed")
1593
Victor Stinner88701e22011-06-01 13:13:04 +02001594 # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
1595 # descriptor of a pipe closed in the parent process is valid in the
1596 # child process according to fstat(), but the mode of the file
1597 # descriptor is invalid, and read or write raise an error.
1598 @support.requires_mac_ver(10, 5)
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00001599 def test_pass_fds(self):
1600 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
1601
1602 open_fds = set()
1603
1604 for x in range(5):
1605 fds = os.pipe()
1606 self.addCleanup(os.close, fds[0])
1607 self.addCleanup(os.close, fds[1])
1608 open_fds.update(fds)
1609
1610 for fd in open_fds:
1611 p = subprocess.Popen([sys.executable, fd_status],
1612 stdout=subprocess.PIPE, close_fds=True,
1613 pass_fds=(fd, ))
1614 output, ignored = p.communicate()
1615
1616 remaining_fds = set(map(int, output.split(b',')))
1617 to_be_closed = open_fds - {fd}
1618
1619 self.assertIn(fd, remaining_fds, "fd to be passed not passed")
1620 self.assertFalse(remaining_fds & to_be_closed,
1621 "fd to be closed passed")
1622
1623 # pass_fds overrides close_fds with a warning.
1624 with self.assertWarns(RuntimeWarning) as context:
1625 self.assertFalse(subprocess.call(
1626 [sys.executable, "-c", "import sys; sys.exit(0)"],
1627 close_fds=False, pass_fds=(fd, )))
1628 self.assertIn('overriding close_fds', str(context.warning))
1629
Gregory P. Smithe14e9c22011-03-15 14:55:17 -04001630 def test_stdout_stdin_are_single_inout_fd(self):
1631 with io.open(os.devnull, "r+") as inout:
1632 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
1633 stdout=inout, stdin=inout)
1634 p.wait()
1635
1636 def test_stdout_stderr_are_single_inout_fd(self):
1637 with io.open(os.devnull, "r+") as inout:
1638 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
1639 stdout=inout, stderr=inout)
1640 p.wait()
1641
1642 def test_stderr_stdin_are_single_inout_fd(self):
1643 with io.open(os.devnull, "r+") as inout:
1644 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
1645 stderr=inout, stdin=inout)
1646 p.wait()
1647
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00001648 def test_wait_when_sigchild_ignored(self):
1649 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
1650 sigchild_ignore = support.findfile("sigchild_ignore.py",
1651 subdir="subprocessdata")
1652 p = subprocess.Popen([sys.executable, sigchild_ignore],
1653 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1654 stdout, stderr = p.communicate()
1655 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
Gregory P. Smitha80f4fb2010-12-14 15:23:02 +00001656 " non-zero with this error:\n%s" %
1657 stderr.decode('utf8'))
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00001658
Antoine Pitrou7b98d022011-03-19 17:04:13 +01001659 def test_select_unbuffered(self):
1660 # Issue #11459: bufsize=0 should really set the pipes as
1661 # unbuffered (and therefore let select() work properly).
1662 select = support.import_module("select")
1663 p = subprocess.Popen([sys.executable, "-c",
1664 'import sys;'
1665 'sys.stdout.write("apple")'],
1666 stdout=subprocess.PIPE,
1667 bufsize=0)
1668 f = p.stdout
Ross Lagerwall17ace7a2011-03-26 21:21:46 +02001669 self.addCleanup(f.close)
Antoine Pitrou7b98d022011-03-19 17:04:13 +01001670 try:
1671 self.assertEqual(f.read(4), b"appl")
1672 self.assertIn(f, select.select([f], [], [], 0.0)[0])
1673 finally:
1674 p.wait()
1675
Charles-François Natali134a8ba2011-08-18 18:49:39 +02001676 def test_zombie_fast_process_del(self):
1677 # Issue #12650: on Unix, if Popen.__del__() was called before the
1678 # process exited, it wouldn't be added to subprocess._active, and would
1679 # remain a zombie.
1680 # spawn a Popen, and delete its reference before it exits
1681 p = subprocess.Popen([sys.executable, "-c",
1682 'import sys, time;'
1683 'time.sleep(0.2)'],
1684 stdout=subprocess.PIPE,
1685 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02001686 self.addCleanup(p.stdout.close)
1687 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02001688 ident = id(p)
1689 pid = p.pid
1690 del p
1691 # check that p is in the active processes list
1692 self.assertIn(ident, [id(o) for o in subprocess._active])
1693
Charles-François Natali134a8ba2011-08-18 18:49:39 +02001694 def test_leak_fast_process_del_killed(self):
1695 # Issue #12650: on Unix, if Popen.__del__() was called before the
1696 # process exited, and the process got killed by a signal, it would never
1697 # be removed from subprocess._active, which triggered a FD and memory
1698 # leak.
1699 # spawn a Popen, delete its reference and kill it
1700 p = subprocess.Popen([sys.executable, "-c",
1701 'import time;'
1702 'time.sleep(3)'],
1703 stdout=subprocess.PIPE,
1704 stderr=subprocess.PIPE)
Nadeem Vawda0d7cda32011-08-19 05:12:01 +02001705 self.addCleanup(p.stdout.close)
1706 self.addCleanup(p.stderr.close)
Charles-François Natali134a8ba2011-08-18 18:49:39 +02001707 ident = id(p)
1708 pid = p.pid
1709 del p
1710 os.kill(pid, signal.SIGKILL)
1711 # check that p is in the active processes list
1712 self.assertIn(ident, [id(o) for o in subprocess._active])
1713
1714 # let some time for the process to exit, and create a new Popen: this
1715 # should trigger the wait() of p
1716 time.sleep(0.2)
1717 with self.assertRaises(EnvironmentError) as c:
1718 with subprocess.Popen(['nonexisting_i_hope'],
1719 stdout=subprocess.PIPE,
1720 stderr=subprocess.PIPE) as proc:
1721 pass
1722 # p should have been wait()ed on, and removed from the _active list
1723 self.assertRaises(OSError, os.waitpid, pid, 0)
1724 self.assertNotIn(ident, [id(o) for o in subprocess._active])
1725
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001726
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001727@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +00001728class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001729
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001730 def test_startupinfo(self):
1731 # startupinfo argument
1732 # We uses hardcoded constants, because we do not want to
1733 # depend on win32all.
1734 STARTF_USESHOWWINDOW = 1
1735 SW_MAXIMIZE = 3
1736 startupinfo = subprocess.STARTUPINFO()
1737 startupinfo.dwFlags = STARTF_USESHOWWINDOW
1738 startupinfo.wShowWindow = SW_MAXIMIZE
1739 # Since Python is a console process, it won't be affected
1740 # by wShowWindow, but the argument should be silently
1741 # ignored
1742 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001743 startupinfo=startupinfo)
1744
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001745 def test_creationflags(self):
1746 # creationflags argument
1747 CREATE_NEW_CONSOLE = 16
1748 sys.stderr.write(" a DOS box should flash briefly ...\n")
1749 subprocess.call(sys.executable +
1750 ' -c "import time; time.sleep(0.25)"',
1751 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001752
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001753 def test_invalid_args(self):
1754 # invalid arguments should raise ValueError
1755 self.assertRaises(ValueError, subprocess.call,
1756 [sys.executable, "-c",
1757 "import sys; sys.exit(47)"],
1758 preexec_fn=lambda: 1)
1759 self.assertRaises(ValueError, subprocess.call,
1760 [sys.executable, "-c",
1761 "import sys; sys.exit(47)"],
1762 stdout=subprocess.PIPE,
1763 close_fds=True)
1764
1765 def test_close_fds(self):
1766 # close file descriptors
1767 rc = subprocess.call([sys.executable, "-c",
1768 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001769 close_fds=True)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001770 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001771
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001772 def test_shell_sequence(self):
1773 # Run command through the shell (sequence)
1774 newenv = os.environ.copy()
1775 newenv["FRUIT"] = "physalis"
1776 p = subprocess.Popen(["set"], shell=1,
1777 stdout=subprocess.PIPE,
1778 env=newenv)
Brian Curtin19a53792010-11-05 17:09:05 +00001779 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001780 self.assertIn(b"physalis", p.stdout.read())
Guido van Rossume7ba4952007-06-06 23:52:48 +00001781
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001782 def test_shell_string(self):
1783 # Run command through the shell (string)
1784 newenv = os.environ.copy()
1785 newenv["FRUIT"] = "physalis"
1786 p = subprocess.Popen("set", shell=1,
1787 stdout=subprocess.PIPE,
1788 env=newenv)
Brian Curtin19a53792010-11-05 17:09:05 +00001789 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001790 self.assertIn(b"physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001791
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001792 def test_call_string(self):
1793 # call() function with string argument on Windows
1794 rc = subprocess.call(sys.executable +
1795 ' -c "import sys; sys.exit(47)"')
1796 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001797
Florent Xicluna4886d242010-03-08 13:27:26 +00001798 def _kill_process(self, method, *args):
1799 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroua4024e22010-09-24 18:57:01 +00001800 p = subprocess.Popen([sys.executable, "-c", """if 1:
1801 import sys, time
1802 sys.stdout.write('x\\n')
1803 sys.stdout.flush()
1804 time.sleep(30)
1805 """],
1806 stdin=subprocess.PIPE,
1807 stdout=subprocess.PIPE,
1808 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +00001809 self.addCleanup(p.stdout.close)
1810 self.addCleanup(p.stderr.close)
1811 self.addCleanup(p.stdin.close)
Antoine Pitroua4024e22010-09-24 18:57:01 +00001812 # Wait for the interpreter to be completely initialized before
1813 # sending any signal.
1814 p.stdout.read(1)
1815 getattr(p, method)(*args)
Florent Xiclunac049d872010-03-27 22:47:23 +00001816 _, stderr = p.communicate()
1817 self.assertStderrEqual(stderr, b'')
Antoine Pitroua4024e22010-09-24 18:57:01 +00001818 returncode = p.wait()
Florent Xicluna4886d242010-03-08 13:27:26 +00001819 self.assertNotEqual(returncode, 0)
1820
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001821 def _kill_dead_process(self, method, *args):
1822 p = subprocess.Popen([sys.executable, "-c", """if 1:
1823 import sys, time
1824 sys.stdout.write('x\\n')
1825 sys.stdout.flush()
1826 sys.exit(42)
1827 """],
1828 stdin=subprocess.PIPE,
1829 stdout=subprocess.PIPE,
1830 stderr=subprocess.PIPE)
1831 self.addCleanup(p.stdout.close)
1832 self.addCleanup(p.stderr.close)
1833 self.addCleanup(p.stdin.close)
1834 # Wait for the interpreter to be completely initialized before
1835 # sending any signal.
1836 p.stdout.read(1)
1837 # The process should end after this
1838 time.sleep(1)
1839 # This shouldn't raise even though the child is now dead
1840 getattr(p, method)(*args)
1841 _, stderr = p.communicate()
1842 self.assertStderrEqual(stderr, b'')
1843 rc = p.wait()
1844 self.assertEqual(rc, 42)
1845
Florent Xicluna4886d242010-03-08 13:27:26 +00001846 def test_send_signal(self):
1847 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimesa342c012008-04-20 21:01:16 +00001848
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001849 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001850 self._kill_process('kill')
Christian Heimesa342c012008-04-20 21:01:16 +00001851
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001852 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001853 self._kill_process('terminate')
Christian Heimesa342c012008-04-20 21:01:16 +00001854
Antoine Pitrou1f9a8352012-03-11 19:29:12 +01001855 def test_send_signal_dead(self):
1856 self._kill_dead_process('send_signal', signal.SIGTERM)
1857
1858 def test_kill_dead(self):
1859 self._kill_dead_process('kill')
1860
1861 def test_terminate_dead(self):
1862 self._kill_dead_process('terminate')
1863
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001864
Brett Cannona23810f2008-05-26 19:04:21 +00001865# The module says:
1866# "NB This only works (and is only relevant) for UNIX."
1867#
1868# Actually, getoutput should work on any platform with an os.popen, but
1869# I'll take the comment as given, and skip this suite.
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001870@unittest.skipUnless(os.name == 'posix', "only relevant for UNIX")
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001871class CommandTests(unittest.TestCase):
1872 def test_getoutput(self):
1873 self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
1874 self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
1875 (0, 'xyzzy'))
Brett Cannona23810f2008-05-26 19:04:21 +00001876
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001877 # we use mkdtemp in the next line to create an empty directory
1878 # under our exclusive control; from that, we can invent a pathname
1879 # that we _know_ won't exist. This is guaranteed to fail.
1880 dir = None
1881 try:
1882 dir = tempfile.mkdtemp()
1883 name = os.path.join(dir, "foo")
Brett Cannona23810f2008-05-26 19:04:21 +00001884
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001885 status, output = subprocess.getstatusoutput('cat ' + name)
1886 self.assertNotEqual(status, 0)
1887 finally:
1888 if dir is not None:
1889 os.rmdir(dir)
Brett Cannona23810f2008-05-26 19:04:21 +00001890
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001891
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001892@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
1893 "poll system call not supported")
1894class ProcessTestCaseNoPoll(ProcessTestCase):
1895 def setUp(self):
1896 subprocess._has_poll = False
1897 ProcessTestCase.setUp(self)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001898
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001899 def tearDown(self):
1900 subprocess._has_poll = True
1901 ProcessTestCase.tearDown(self)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001902
1903
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001904@unittest.skipUnless(getattr(subprocess, '_posixsubprocess', False),
1905 "_posixsubprocess extension module not found.")
1906class ProcessTestCasePOSIXPurePython(ProcessTestCase, POSIXProcessTestCase):
Gregory P. Smith7439e7b2011-05-28 09:06:02 -07001907 @classmethod
1908 def setUpClass(cls):
1909 global subprocess
1910 assert subprocess._posixsubprocess
1911 # Reimport subprocess while forcing _posixsubprocess to not exist.
1912 with support.check_warnings(('.*_posixsubprocess .* not being used.*',
1913 RuntimeWarning)):
1914 subprocess = support.import_fresh_module(
1915 'subprocess', blocked=['_posixsubprocess'])
1916 assert not subprocess._posixsubprocess
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001917
Gregory P. Smith7439e7b2011-05-28 09:06:02 -07001918 @classmethod
1919 def tearDownClass(cls):
1920 global subprocess
1921 # Reimport subprocess as it should be, restoring order to the universe.
1922 subprocess = support.import_fresh_module('subprocess')
1923 assert subprocess._posixsubprocess
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001924
1925
Gregory P. Smitha59c59f2010-03-01 00:17:40 +00001926class HelperFunctionTests(unittest.TestCase):
Gregory P. Smithaf6d3b82010-03-01 02:56:44 +00001927 @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
Gregory P. Smitha59c59f2010-03-01 00:17:40 +00001928 def test_eintr_retry_call(self):
1929 record_calls = []
1930 def fake_os_func(*args):
1931 record_calls.append(args)
1932 if len(record_calls) == 2:
1933 raise OSError(errno.EINTR, "fake interrupted system call")
1934 return tuple(reversed(args))
1935
1936 self.assertEqual((999, 256),
1937 subprocess._eintr_retry_call(fake_os_func, 256, 999))
1938 self.assertEqual([(256, 999)], record_calls)
1939 # This time there will be an EINTR so it will loop once.
1940 self.assertEqual((666,),
1941 subprocess._eintr_retry_call(fake_os_func, 666))
1942 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
1943
1944
Tim Golden126c2962010-08-11 14:20:40 +00001945@unittest.skipUnless(mswindows, "Windows-specific tests")
1946class CommandsWithSpaces (BaseTestCase):
1947
1948 def setUp(self):
1949 super().setUp()
1950 f, fname = mkstemp(".py", "te st")
1951 self.fname = fname.lower ()
1952 os.write(f, b"import sys;"
1953 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
1954 )
1955 os.close(f)
1956
1957 def tearDown(self):
1958 os.remove(self.fname)
1959 super().tearDown()
1960
1961 def with_spaces(self, *args, **kwargs):
1962 kwargs['stdout'] = subprocess.PIPE
1963 p = subprocess.Popen(*args, **kwargs)
Brian Curtin19a53792010-11-05 17:09:05 +00001964 self.addCleanup(p.stdout.close)
Tim Golden126c2962010-08-11 14:20:40 +00001965 self.assertEqual(
1966 p.stdout.read ().decode("mbcs"),
1967 "2 [%r, 'ab cd']" % self.fname
1968 )
1969
1970 def test_shell_string_with_spaces(self):
1971 # call() function with string argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00001972 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1973 "ab cd"), shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00001974
1975 def test_shell_sequence_with_spaces(self):
1976 # call() function with sequence argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00001977 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00001978
1979 def test_noshell_string_with_spaces(self):
1980 # call() function with string argument with spaces on Windows
1981 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1982 "ab cd"))
1983
1984 def test_noshell_sequence_with_spaces(self):
1985 # call() function with sequence argument with spaces on Windows
1986 self.with_spaces([sys.executable, self.fname, "ab cd"])
1987
Brian Curtin79cdb662010-12-03 02:46:02 +00001988
Georg Brandla86b2622012-02-20 21:34:57 +01001989class ContextManagerTests(BaseTestCase):
Brian Curtin79cdb662010-12-03 02:46:02 +00001990
1991 def test_pipe(self):
1992 with subprocess.Popen([sys.executable, "-c",
1993 "import sys;"
1994 "sys.stdout.write('stdout');"
1995 "sys.stderr.write('stderr');"],
1996 stdout=subprocess.PIPE,
1997 stderr=subprocess.PIPE) as proc:
1998 self.assertEqual(proc.stdout.read(), b"stdout")
1999 self.assertStderrEqual(proc.stderr.read(), b"stderr")
2000
2001 self.assertTrue(proc.stdout.closed)
2002 self.assertTrue(proc.stderr.closed)
2003
2004 def test_returncode(self):
2005 with subprocess.Popen([sys.executable, "-c",
2006 "import sys; sys.exit(100)"]) as proc:
Gregory P. Smithc9557af2011-05-11 22:18:23 -07002007 pass
2008 # __exit__ calls wait(), so the returncode should be set
Brian Curtin79cdb662010-12-03 02:46:02 +00002009 self.assertEqual(proc.returncode, 100)
2010
2011 def test_communicate_stdin(self):
2012 with subprocess.Popen([sys.executable, "-c",
2013 "import sys;"
2014 "sys.exit(sys.stdin.read() == 'context')"],
2015 stdin=subprocess.PIPE) as proc:
2016 proc.communicate(b"context")
2017 self.assertEqual(proc.returncode, 1)
2018
2019 def test_invalid_args(self):
2020 with self.assertRaises(EnvironmentError) as c:
2021 with subprocess.Popen(['nonexisting_i_hope'],
2022 stdout=subprocess.PIPE,
2023 stderr=subprocess.PIPE) as proc:
2024 pass
2025
2026 if c.exception.errno != errno.ENOENT: # ignore "no such file"
2027 raise c.exception
2028
2029
Gregory P. Smith961e0e82011-03-15 15:43:39 -04002030def test_main():
2031 unit_tests = (ProcessTestCase,
2032 POSIXProcessTestCase,
2033 Win32ProcessTestCase,
2034 ProcessTestCasePOSIXPurePython,
2035 CommandTests,
2036 ProcessTestCaseNoPoll,
2037 HelperFunctionTests,
2038 CommandsWithSpaces,
Antoine Pitrouab85ff32011-07-23 22:03:45 +02002039 ContextManagerTests,
2040 )
Gregory P. Smith961e0e82011-03-15 15:43:39 -04002041
2042 support.run_unittest(*unit_tests)
2043 support.reap_children()
2044
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00002045if __name__ == "__main__":
Gregory P. Smithe14e9c22011-03-15 14:55:17 -04002046 unittest.main()