blob: 5e74f4aa187c5d2e867a1b5f25d86ba938278945 [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001import unittest
2from test import test_support
3import subprocess
4import sys
5import signal
6import os
Gregory P. Smithcce211f2010-03-01 00:05:08 +00007import errno
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00008import tempfile
9import time
Tim Peters3761e8d2004-10-13 04:07:12 +000010import re
Ezio Melotti8f6a2872010-02-10 21:40:33 +000011import sysconfig
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000012
Benjamin Peterson8b59c232011-12-10 12:31:42 -050013try:
14 import resource
15except ImportError:
16 resource = None
17
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000018mswindows = (sys.platform == "win32")
19
20#
21# Depends on the following external programs: Python
22#
23
24if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000025 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
26 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000027else:
28 SETBINARY = ''
29
Florent Xicluna98e3fc32010-02-27 19:20:50 +000030
31try:
32 mkstemp = tempfile.mkstemp
33except AttributeError:
34 # tempfile.mkstemp is not available
35 def mkstemp():
36 """Replacement for mkstemp, calling mktemp."""
37 fname = tempfile.mktemp()
38 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
39
Tim Peters3761e8d2004-10-13 04:07:12 +000040
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000041class BaseTestCase(unittest.TestCase):
Neal Norwitzb15ac312006-06-29 04:10:08 +000042 def setUp(self):
Tim Peters38ff36c2006-06-30 06:18:39 +000043 # Try to minimize the number of children we have so this test
44 # doesn't crash on some buildbots (Alphas in particular).
Florent Xicluna98e3fc32010-02-27 19:20:50 +000045 test_support.reap_children()
Neal Norwitzb15ac312006-06-29 04:10:08 +000046
Florent Xiclunaab5e17f2010-03-04 21:31:58 +000047 def tearDown(self):
48 for inst in subprocess._active:
49 inst.wait()
50 subprocess._cleanup()
51 self.assertFalse(subprocess._active, "subprocess._active not empty")
52
Florent Xicluna98e3fc32010-02-27 19:20:50 +000053 def assertStderrEqual(self, stderr, expected, msg=None):
54 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
55 # shutdown time. That frustrates tests trying to check stderr produced
56 # from a spawned Python process.
57 actual = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
58 self.assertEqual(actual, expected, msg)
Neal Norwitzb15ac312006-06-29 04:10:08 +000059
Florent Xiclunafc4d6d72010-03-23 14:36:45 +000060
61class ProcessTestCase(BaseTestCase):
62
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000063 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000064 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000065 rc = subprocess.call([sys.executable, "-c",
66 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000067 self.assertEqual(rc, 47)
68
Peter Astrand454f7672005-01-01 09:36:35 +000069 def test_check_call_zero(self):
70 # check_call() function with zero return code
71 rc = subprocess.check_call([sys.executable, "-c",
72 "import sys; sys.exit(0)"])
73 self.assertEqual(rc, 0)
74
75 def test_check_call_nonzero(self):
76 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +000077 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +000078 subprocess.check_call([sys.executable, "-c",
79 "import sys; sys.exit(47)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +000080 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000081
Gregory P. Smith26576802008-12-05 02:27:01 +000082 def test_check_output(self):
83 # check_output() function with zero return code
84 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000085 [sys.executable, "-c", "print 'BDFL'"])
Ezio Melottiaa980582010-01-23 23:04:36 +000086 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000087
Gregory P. Smith26576802008-12-05 02:27:01 +000088 def test_check_output_nonzero(self):
Gregory P. Smith97f49f42008-12-04 20:21:09 +000089 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +000090 with self.assertRaises(subprocess.CalledProcessError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +000091 subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000092 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +000093 self.assertEqual(c.exception.returncode, 5)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000094
Gregory P. Smith26576802008-12-05 02:27:01 +000095 def test_check_output_stderr(self):
96 # check_output() function stderr redirected to stdout
97 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000098 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
99 stderr=subprocess.STDOUT)
Ezio Melottiaa980582010-01-23 23:04:36 +0000100 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000101
Gregory P. Smith26576802008-12-05 02:27:01 +0000102 def test_check_output_stdout_arg(self):
103 # check_output() function stderr redirected to stdout
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000104 with self.assertRaises(ValueError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +0000105 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000106 [sys.executable, "-c", "print 'will not be run'"],
107 stdout=sys.stdout)
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000108 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000109 self.assertIn('stdout', c.exception.args[0])
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000110
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000111 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000112 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000113 newenv = os.environ.copy()
114 newenv["FRUIT"] = "banana"
115 rc = subprocess.call([sys.executable, "-c",
Florent Xiclunabab22a72010-03-04 19:40:48 +0000116 'import sys, os;'
117 'sys.exit(os.getenv("FRUIT")=="banana")'],
118 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000119 self.assertEqual(rc, 1)
120
Victor Stinner776e69b2011-06-01 01:03:00 +0200121 def test_invalid_args(self):
122 # Popen() called with invalid arguments should raise TypeError
123 # but Popen.__del__ should not complain (issue #12085)
Victor Stinnere9b185f2011-06-01 01:57:48 +0200124 with test_support.captured_stderr() as s:
Victor Stinner776e69b2011-06-01 01:03:00 +0200125 self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
126 argcount = subprocess.Popen.__init__.__code__.co_argcount
127 too_many_args = [0] * (argcount + 1)
128 self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
129 self.assertEqual(s.getvalue(), '')
130
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000131 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000132 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000133 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
134 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000135 self.addCleanup(p.stdout.close)
136 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000137 p.wait()
138 self.assertEqual(p.stdin, None)
139
140 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000141 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +0000142 p = subprocess.Popen([sys.executable, "-c",
Tim Peters4052fe52004-10-13 03:29:54 +0000143 'print " this bit of output is from a '
144 'test of stdout in a different '
145 'process ..."'],
146 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000147 self.addCleanup(p.stdin.close)
148 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000149 p.wait()
150 self.assertEqual(p.stdout, None)
151
152 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000153 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000154 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
155 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000156 self.addCleanup(p.stdout.close)
157 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000158 p.wait()
159 self.assertEqual(p.stderr, None)
160
Ezio Melotti8f6a2872010-02-10 21:40:33 +0000161 def test_executable_with_cwd(self):
Florent Xicluna63763702010-03-11 01:50:48 +0000162 python_dir = os.path.dirname(os.path.realpath(sys.executable))
Ezio Melotti8f6a2872010-02-10 21:40:33 +0000163 p = subprocess.Popen(["somethingyoudonthave", "-c",
164 "import sys; sys.exit(47)"],
165 executable=sys.executable, cwd=python_dir)
166 p.wait()
167 self.assertEqual(p.returncode, 47)
168
169 @unittest.skipIf(sysconfig.is_python_build(),
170 "need an installed Python. See #7774")
171 def test_executable_without_cwd(self):
172 # For a normal installation, it should work without 'cwd'
173 # argument. For test runs in the build directory, see #7774.
174 p = subprocess.Popen(["somethingyoudonthave", "-c",
175 "import sys; sys.exit(47)"],
Tim Peters3b01a702004-10-12 22:19:32 +0000176 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000177 p.wait()
178 self.assertEqual(p.returncode, 47)
179
180 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000181 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000182 p = subprocess.Popen([sys.executable, "-c",
183 'import sys; sys.exit(sys.stdin.read() == "pear")'],
184 stdin=subprocess.PIPE)
185 p.stdin.write("pear")
186 p.stdin.close()
187 p.wait()
188 self.assertEqual(p.returncode, 1)
189
190 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000191 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000192 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000193 d = tf.fileno()
194 os.write(d, "pear")
195 os.lseek(d, 0, 0)
196 p = subprocess.Popen([sys.executable, "-c",
197 'import sys; sys.exit(sys.stdin.read() == "pear")'],
198 stdin=d)
199 p.wait()
200 self.assertEqual(p.returncode, 1)
201
202 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000203 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000204 tf = tempfile.TemporaryFile()
205 tf.write("pear")
206 tf.seek(0)
207 p = subprocess.Popen([sys.executable, "-c",
208 'import sys; sys.exit(sys.stdin.read() == "pear")'],
209 stdin=tf)
210 p.wait()
211 self.assertEqual(p.returncode, 1)
212
213 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000214 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000215 p = subprocess.Popen([sys.executable, "-c",
216 'import sys; sys.stdout.write("orange")'],
217 stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000218 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000219 self.assertEqual(p.stdout.read(), "orange")
220
221 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000222 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000223 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000224 d = tf.fileno()
225 p = subprocess.Popen([sys.executable, "-c",
226 'import sys; sys.stdout.write("orange")'],
227 stdout=d)
228 p.wait()
229 os.lseek(d, 0, 0)
230 self.assertEqual(os.read(d, 1024), "orange")
231
232 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000233 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000234 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000235 p = subprocess.Popen([sys.executable, "-c",
236 'import sys; sys.stdout.write("orange")'],
237 stdout=tf)
238 p.wait()
239 tf.seek(0)
240 self.assertEqual(tf.read(), "orange")
241
242 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000243 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000244 p = subprocess.Popen([sys.executable, "-c",
245 'import sys; sys.stderr.write("strawberry")'],
246 stderr=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000247 self.addCleanup(p.stderr.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000248 self.assertStderrEqual(p.stderr.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000249
250 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000251 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000252 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000253 d = tf.fileno()
254 p = subprocess.Popen([sys.executable, "-c",
255 'import sys; sys.stderr.write("strawberry")'],
256 stderr=d)
257 p.wait()
258 os.lseek(d, 0, 0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000259 self.assertStderrEqual(os.read(d, 1024), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000260
261 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000262 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000263 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000264 p = subprocess.Popen([sys.executable, "-c",
265 'import sys; sys.stderr.write("strawberry")'],
266 stderr=tf)
267 p.wait()
268 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000269 self.assertStderrEqual(tf.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000270
271 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000272 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000273 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000274 'import sys;'
275 'sys.stdout.write("apple");'
276 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000277 'sys.stderr.write("orange")'],
278 stdout=subprocess.PIPE,
279 stderr=subprocess.STDOUT)
Brian Curtind117b562010-11-05 04:09:09 +0000280 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000281 self.assertStderrEqual(p.stdout.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000282
283 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000284 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000285 tf = tempfile.TemporaryFile()
286 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000287 'import sys;'
288 'sys.stdout.write("apple");'
289 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000290 'sys.stderr.write("orange")'],
291 stdout=tf,
292 stderr=tf)
293 p.wait()
294 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000295 self.assertStderrEqual(tf.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000296
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000297 def test_stdout_filedes_of_stdout(self):
298 # stdout is set to 1 (#1531862).
299 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))"
300 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000301 self.assertEqual(rc, 2)
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000302
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000303 def test_cwd(self):
Guido van Rossume9a0e882007-12-20 17:28:10 +0000304 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000305 # We cannot use os.path.realpath to canonicalize the path,
306 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
307 cwd = os.getcwd()
308 os.chdir(tmpdir)
309 tmpdir = os.getcwd()
310 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000311 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000312 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000313 'sys.stdout.write(os.getcwd())'],
314 stdout=subprocess.PIPE,
315 cwd=tmpdir)
Brian Curtind117b562010-11-05 04:09:09 +0000316 self.addCleanup(p.stdout.close)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000317 normcase = os.path.normcase
318 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000319
320 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000321 newenv = os.environ.copy()
322 newenv["FRUIT"] = "orange"
323 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000324 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000325 'sys.stdout.write(os.getenv("FRUIT"))'],
326 stdout=subprocess.PIPE,
327 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000328 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000329 self.assertEqual(p.stdout.read(), "orange")
330
Peter Astrandcbac93c2005-03-03 20:24:28 +0000331 def test_communicate_stdin(self):
332 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000333 'import sys;'
334 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000335 stdin=subprocess.PIPE)
336 p.communicate("pear")
337 self.assertEqual(p.returncode, 1)
338
339 def test_communicate_stdout(self):
340 p = subprocess.Popen([sys.executable, "-c",
341 'import sys; sys.stdout.write("pineapple")'],
342 stdout=subprocess.PIPE)
343 (stdout, stderr) = p.communicate()
344 self.assertEqual(stdout, "pineapple")
345 self.assertEqual(stderr, None)
346
347 def test_communicate_stderr(self):
348 p = subprocess.Popen([sys.executable, "-c",
349 'import sys; sys.stderr.write("pineapple")'],
350 stderr=subprocess.PIPE)
351 (stdout, stderr) = p.communicate()
352 self.assertEqual(stdout, None)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000353 self.assertStderrEqual(stderr, "pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000354
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000355 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000356 p = subprocess.Popen([sys.executable, "-c",
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000357 'import sys,os;'
358 'sys.stderr.write("pineapple");'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000359 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000360 stdin=subprocess.PIPE,
361 stdout=subprocess.PIPE,
362 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000363 self.addCleanup(p.stdout.close)
364 self.addCleanup(p.stderr.close)
365 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000366 (stdout, stderr) = p.communicate("banana")
367 self.assertEqual(stdout, "banana")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000368 self.assertStderrEqual(stderr, "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000369
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000370 # This test is Linux specific for simplicity to at least have
371 # some coverage. It is not a platform specific bug.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000372 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
373 "Linux specific")
374 # Test for the fd leak reported in http://bugs.python.org/issue2791.
375 def test_communicate_pipe_fd_leak(self):
376 fd_directory = '/proc/%d/fd' % os.getpid()
377 num_fds_before_popen = len(os.listdir(fd_directory))
378 p = subprocess.Popen([sys.executable, "-c", "print()"],
379 stdout=subprocess.PIPE)
380 p.communicate()
381 num_fds_after_communicate = len(os.listdir(fd_directory))
382 del p
383 num_fds_after_destruction = len(os.listdir(fd_directory))
384 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
385 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000386
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000387 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000388 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000389 p = subprocess.Popen([sys.executable, "-c",
390 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000391 (stdout, stderr) = p.communicate()
392 self.assertEqual(stdout, None)
393 self.assertEqual(stderr, None)
394
395 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000396 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000397 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000398 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000399 x, y = os.pipe()
400 if mswindows:
401 pipe_buf = 512
402 else:
403 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
404 os.close(x)
405 os.close(y)
406 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000407 'import sys,os;'
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000408 'sys.stdout.write(sys.stdin.read(47));'
409 'sys.stderr.write("xyz"*%d);'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000410 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000411 stdin=subprocess.PIPE,
412 stdout=subprocess.PIPE,
413 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000414 self.addCleanup(p.stdout.close)
415 self.addCleanup(p.stderr.close)
416 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000417 string_to_write = "abc"*pipe_buf
418 (stdout, stderr) = p.communicate(string_to_write)
419 self.assertEqual(stdout, string_to_write)
420
421 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000422 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000423 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000424 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000425 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000426 stdin=subprocess.PIPE,
427 stdout=subprocess.PIPE,
428 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000429 self.addCleanup(p.stdout.close)
430 self.addCleanup(p.stderr.close)
431 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000432 p.stdin.write("banana")
433 (stdout, stderr) = p.communicate("split")
434 self.assertEqual(stdout, "bananasplit")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000435 self.assertStderrEqual(stderr, "")
Tim Peterse718f612004-10-12 21:51:32 +0000436
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000437 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000438 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000439 'import sys,os;' + SETBINARY +
440 'sys.stdout.write("line1\\n");'
441 'sys.stdout.flush();'
442 'sys.stdout.write("line2\\r");'
443 'sys.stdout.flush();'
444 'sys.stdout.write("line3\\r\\n");'
445 'sys.stdout.flush();'
446 'sys.stdout.write("line4\\r");'
447 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000448 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000449 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000450 'sys.stdout.write("\\nline6");'],
451 stdout=subprocess.PIPE,
452 universal_newlines=1)
Brian Curtind117b562010-11-05 04:09:09 +0000453 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000454 stdout = p.stdout.read()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000455 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000456 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000457 self.assertEqual(stdout,
458 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000459 else:
460 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000461 self.assertEqual(stdout,
462 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000463
464 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000465 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000466 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000467 'import sys,os;' + SETBINARY +
468 'sys.stdout.write("line1\\n");'
469 'sys.stdout.flush();'
470 'sys.stdout.write("line2\\r");'
471 'sys.stdout.flush();'
472 'sys.stdout.write("line3\\r\\n");'
473 'sys.stdout.flush();'
474 'sys.stdout.write("line4\\r");'
475 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000476 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000477 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000478 'sys.stdout.write("\\nline6");'],
479 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
480 universal_newlines=1)
Brian Curtin7fe045e2010-11-05 17:19:38 +0000481 self.addCleanup(p.stdout.close)
482 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000483 (stdout, stderr) = p.communicate()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000484 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000485 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000486 self.assertEqual(stdout,
487 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000488 else:
489 # Interpreter without universal newline support
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000490 self.assertEqual(stdout,
491 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000492
493 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000494 # Make sure we leak no resources
Antoine Pitroub0b3bff2010-09-18 22:42:30 +0000495 if not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000496 max_handles = 1026 # too much for most UNIX systems
497 else:
Antoine Pitroub0b3bff2010-09-18 22:42:30 +0000498 max_handles = 2050 # too much for (at least some) Windows setups
499 handles = []
500 try:
501 for i in range(max_handles):
502 try:
503 handles.append(os.open(test_support.TESTFN,
504 os.O_WRONLY | os.O_CREAT))
505 except OSError as e:
506 if e.errno != errno.EMFILE:
507 raise
508 break
509 else:
510 self.skipTest("failed to reach the file descriptor limit "
511 "(tried %d)" % max_handles)
512 # Close a couple of them (should be enough for a subprocess)
513 for i in range(10):
514 os.close(handles.pop())
515 # Loop creating some subprocesses. If one of them leaks some fds,
516 # the next loop iteration will fail by reaching the max fd limit.
517 for i in range(15):
518 p = subprocess.Popen([sys.executable, "-c",
519 "import sys;"
520 "sys.stdout.write(sys.stdin.read())"],
521 stdin=subprocess.PIPE,
522 stdout=subprocess.PIPE,
523 stderr=subprocess.PIPE)
524 data = p.communicate(b"lime")[0]
525 self.assertEqual(data, b"lime")
526 finally:
527 for h in handles:
528 os.close(h)
Mark Dickinson313dc9b2012-10-07 15:41:38 +0100529 test_support.unlink(test_support.TESTFN)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000530
531 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000532 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
533 '"a b c" d e')
534 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
535 'ab\\"c \\ d')
Gregory P. Smithe047e6d2008-01-19 20:49:02 +0000536 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
537 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000538 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
539 'a\\\\\\b "de fg" h')
540 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
541 'a\\\\\\"b c d')
542 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
543 '"a\\\\b c" d e')
544 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
545 '"a\\\\b\\ c" d e')
Peter Astrand10514a72007-01-13 22:35:35 +0000546 self.assertEqual(subprocess.list2cmdline(['ab', '']),
547 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000548
549
550 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000551 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000552 "-c", "import time; time.sleep(1)"])
553 count = 0
554 while p.poll() is None:
555 time.sleep(0.1)
556 count += 1
557 # We expect that the poll loop probably went around about 10 times,
558 # but, based on system scheduling we can't control, it's possible
559 # poll() never returned None. It "should be" very rare that it
560 # didn't go around at least twice.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000561 self.assertGreaterEqual(count, 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000562 # Subsequent invocations should just return the returncode
563 self.assertEqual(p.poll(), 0)
564
565
566 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000567 p = subprocess.Popen([sys.executable,
568 "-c", "import time; time.sleep(2)"])
569 self.assertEqual(p.wait(), 0)
570 # Subsequent invocations should just return the returncode
571 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000572
Peter Astrand738131d2004-11-30 21:04:45 +0000573
574 def test_invalid_bufsize(self):
575 # an invalid type of the bufsize argument should raise
576 # TypeError.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000577 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +0000578 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +0000579
Georg Brandlf3715d22009-02-14 17:01:36 +0000580 def test_leaking_fds_on_error(self):
581 # see bug #5179: Popen leaks file descriptors to PIPEs if
582 # the child fails to execute; this will eventually exhaust
583 # the maximum number of open fds. 1024 seems a very common
584 # value for that limit, but Windows has 2048, so we loop
585 # 1024 times (each call leaked two fds).
586 for i in range(1024):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000587 # Windows raises IOError. Others raise OSError.
588 with self.assertRaises(EnvironmentError) as c:
Georg Brandlf3715d22009-02-14 17:01:36 +0000589 subprocess.Popen(['nonexisting_i_hope'],
590 stdout=subprocess.PIPE,
591 stderr=subprocess.PIPE)
R David Murraycdd5fc92011-03-13 22:37:18 -0400592 # ignore errors that indicate the command was not found
593 if c.exception.errno not in (errno.ENOENT, errno.EACCES):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000594 raise c.exception
Georg Brandlf3715d22009-02-14 17:01:36 +0000595
Tim Golden90374f52010-08-06 13:14:33 +0000596 def test_handles_closed_on_exception(self):
597 # If CreateProcess exits with an error, ensure the
598 # duplicate output handles are released
599 ifhandle, ifname = mkstemp()
600 ofhandle, ofname = mkstemp()
601 efhandle, efname = mkstemp()
602 try:
603 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
604 stderr=efhandle)
605 except OSError:
606 os.close(ifhandle)
607 os.remove(ifname)
608 os.close(ofhandle)
609 os.remove(ofname)
610 os.close(efhandle)
611 os.remove(efname)
612 self.assertFalse(os.path.exists(ifname))
613 self.assertFalse(os.path.exists(ofname))
614 self.assertFalse(os.path.exists(efname))
615
Ross Lagerwall104c3f12011-04-05 15:24:34 +0200616 def test_communicate_epipe(self):
617 # Issue 10963: communicate() should hide EPIPE
618 p = subprocess.Popen([sys.executable, "-c", 'pass'],
619 stdin=subprocess.PIPE,
620 stdout=subprocess.PIPE,
621 stderr=subprocess.PIPE)
622 self.addCleanup(p.stdout.close)
623 self.addCleanup(p.stderr.close)
624 self.addCleanup(p.stdin.close)
625 p.communicate("x" * 2**20)
626
627 def test_communicate_epipe_only_stdin(self):
628 # Issue 10963: communicate() should hide EPIPE
629 p = subprocess.Popen([sys.executable, "-c", 'pass'],
630 stdin=subprocess.PIPE)
631 self.addCleanup(p.stdin.close)
632 time.sleep(2)
633 p.communicate("x" * 2**20)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000634
635# context manager
636class _SuppressCoreFiles(object):
637 """Try to prevent core files from being created."""
638 old_limit = None
639
640 def __enter__(self):
641 """Try to save previous ulimit, then set it to (0, 0)."""
Benjamin Peterson8b59c232011-12-10 12:31:42 -0500642 if resource is not None:
643 try:
644 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
645 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
646 except (ValueError, resource.error):
647 pass
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000648
Ronald Oussoren21b44e02010-07-23 12:26:30 +0000649 if sys.platform == 'darwin':
650 # Check if the 'Crash Reporter' on OSX was configured
651 # in 'Developer' mode and warn that it will get triggered
652 # when it is.
653 #
654 # This assumes that this context manager is used in tests
655 # that might trigger the next manager.
656 value = subprocess.Popen(['/usr/bin/defaults', 'read',
657 'com.apple.CrashReporter', 'DialogType'],
658 stdout=subprocess.PIPE).communicate()[0]
659 if value.strip() == b'developer':
660 print "this tests triggers the Crash Reporter, that is intentional"
661 sys.stdout.flush()
662
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000663 def __exit__(self, *args):
664 """Return core file behavior to default."""
665 if self.old_limit is None:
666 return
Benjamin Peterson8b59c232011-12-10 12:31:42 -0500667 if resource is not None:
668 try:
669 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
670 except (ValueError, resource.error):
671 pass
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000672
Victor Stinnerb78fed92011-07-05 14:50:35 +0200673 @unittest.skipUnless(hasattr(signal, 'SIGALRM'),
674 "Requires signal.SIGALRM")
Victor Stinnere7901312011-07-05 14:08:01 +0200675 def test_communicate_eintr(self):
676 # Issue #12493: communicate() should handle EINTR
677 def handler(signum, frame):
678 pass
679 old_handler = signal.signal(signal.SIGALRM, handler)
680 self.addCleanup(signal.signal, signal.SIGALRM, old_handler)
681
682 # the process is running for 2 seconds
683 args = [sys.executable, "-c", 'import time; time.sleep(2)']
684 for stream in ('stdout', 'stderr'):
685 kw = {stream: subprocess.PIPE}
686 with subprocess.Popen(args, **kw) as process:
687 signal.alarm(1)
688 # communicate() will be interrupted by SIGALRM
689 process.communicate()
690
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000691
Florent Xiclunabab22a72010-03-04 19:40:48 +0000692@unittest.skipIf(mswindows, "POSIX specific tests")
Florent Xiclunafc4d6d72010-03-23 14:36:45 +0000693class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaab5e17f2010-03-04 21:31:58 +0000694
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000695 def test_exceptions(self):
696 # caught & re-raised exceptions
697 with self.assertRaises(OSError) as c:
698 p = subprocess.Popen([sys.executable, "-c", ""],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000699 cwd="/this/path/does/not/exist")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000700 # The attribute child_traceback should contain "os.chdir" somewhere.
701 self.assertIn("os.chdir", c.exception.child_traceback)
Tim Peterse718f612004-10-12 21:51:32 +0000702
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000703 def test_run_abort(self):
704 # returncode handles signal termination
705 with _SuppressCoreFiles():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000706 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000707 "import os; os.abort()"])
708 p.wait()
709 self.assertEqual(-p.returncode, signal.SIGABRT)
710
711 def test_preexec(self):
712 # preexec function
713 p = subprocess.Popen([sys.executable, "-c",
714 "import sys, os;"
715 "sys.stdout.write(os.getenv('FRUIT'))"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000716 stdout=subprocess.PIPE,
717 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Brian Curtind117b562010-11-05 04:09:09 +0000718 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000719 self.assertEqual(p.stdout.read(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000720
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000721 def test_args_string(self):
722 # args is a string
723 f, fname = mkstemp()
724 os.write(f, "#!/bin/sh\n")
725 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
726 sys.executable)
727 os.close(f)
728 os.chmod(fname, 0o700)
729 p = subprocess.Popen(fname)
730 p.wait()
731 os.remove(fname)
732 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000733
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000734 def test_invalid_args(self):
735 # invalid arguments should raise ValueError
736 self.assertRaises(ValueError, subprocess.call,
737 [sys.executable, "-c",
738 "import sys; sys.exit(47)"],
739 startupinfo=47)
740 self.assertRaises(ValueError, subprocess.call,
741 [sys.executable, "-c",
742 "import sys; sys.exit(47)"],
743 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000744
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000745 def test_shell_sequence(self):
746 # Run command through the shell (sequence)
747 newenv = os.environ.copy()
748 newenv["FRUIT"] = "apple"
749 p = subprocess.Popen(["echo $FRUIT"], shell=1,
750 stdout=subprocess.PIPE,
751 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000752 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000753 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000754
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000755 def test_shell_string(self):
756 # Run command through the shell (string)
757 newenv = os.environ.copy()
758 newenv["FRUIT"] = "apple"
759 p = subprocess.Popen("echo $FRUIT", shell=1,
760 stdout=subprocess.PIPE,
761 env=newenv)
Brian Curtind117b562010-11-05 04:09:09 +0000762 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000763 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000764
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000765 def test_call_string(self):
766 # call() function with string argument on UNIX
767 f, fname = mkstemp()
768 os.write(f, "#!/bin/sh\n")
769 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
770 sys.executable)
771 os.close(f)
772 os.chmod(fname, 0700)
773 rc = subprocess.call(fname)
774 os.remove(fname)
775 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000776
Stefan Krahe9a6a7d2010-07-19 14:41:08 +0000777 def test_specific_shell(self):
778 # Issue #9265: Incorrect name passed as arg[0].
779 shells = []
780 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
781 for name in ['bash', 'ksh']:
782 sh = os.path.join(prefix, name)
783 if os.path.isfile(sh):
784 shells.append(sh)
785 if not shells: # Will probably work for any shell but csh.
786 self.skipTest("bash or ksh required for this test")
787 sh = '/bin/sh'
788 if os.path.isfile(sh) and not os.path.islink(sh):
789 # Test will fail if /bin/sh is a symlink to csh.
790 shells.append(sh)
791 for sh in shells:
792 p = subprocess.Popen("echo $0", executable=sh, shell=True,
793 stdout=subprocess.PIPE)
Brian Curtind117b562010-11-05 04:09:09 +0000794 self.addCleanup(p.stdout.close)
Stefan Krahe9a6a7d2010-07-19 14:41:08 +0000795 self.assertEqual(p.stdout.read().strip(), sh)
796
Florent Xiclunac0838642010-03-07 15:27:39 +0000797 def _kill_process(self, method, *args):
Florent Xiclunacecef392010-03-05 19:31:21 +0000798 # Do not inherit file handles from the parent.
799 # It should fix failures on some platforms.
Antoine Pitroua6166da2010-09-20 11:20:44 +0000800 p = subprocess.Popen([sys.executable, "-c", """if 1:
801 import sys, time
802 sys.stdout.write('x\\n')
803 sys.stdout.flush()
804 time.sleep(30)
805 """],
806 close_fds=True,
807 stdin=subprocess.PIPE,
808 stdout=subprocess.PIPE,
809 stderr=subprocess.PIPE)
810 # Wait for the interpreter to be completely initialized before
811 # sending any signal.
812 p.stdout.read(1)
813 getattr(p, method)(*args)
Florent Xiclunac0838642010-03-07 15:27:39 +0000814 return p
815
Antoine Pitrouf60845b2012-03-11 19:29:12 +0100816 def _kill_dead_process(self, method, *args):
817 # Do not inherit file handles from the parent.
818 # It should fix failures on some platforms.
819 p = subprocess.Popen([sys.executable, "-c", """if 1:
820 import sys, time
821 sys.stdout.write('x\\n')
822 sys.stdout.flush()
823 """],
824 close_fds=True,
825 stdin=subprocess.PIPE,
826 stdout=subprocess.PIPE,
827 stderr=subprocess.PIPE)
828 # Wait for the interpreter to be completely initialized before
829 # sending any signal.
830 p.stdout.read(1)
831 # The process should end after this
832 time.sleep(1)
833 # This shouldn't raise even though the child is now dead
834 getattr(p, method)(*args)
835 p.communicate()
836
Florent Xiclunac0838642010-03-07 15:27:39 +0000837 def test_send_signal(self):
838 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunafc4d6d72010-03-23 14:36:45 +0000839 _, stderr = p.communicate()
Florent Xicluna3c919cf2010-03-23 19:19:16 +0000840 self.assertIn('KeyboardInterrupt', stderr)
Florent Xicluna446ff142010-03-23 15:05:30 +0000841 self.assertNotEqual(p.wait(), 0)
Christian Heimese74c8f22008-04-19 02:23:57 +0000842
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000843 def test_kill(self):
Florent Xiclunac0838642010-03-07 15:27:39 +0000844 p = self._kill_process('kill')
Florent Xicluna446ff142010-03-23 15:05:30 +0000845 _, stderr = p.communicate()
846 self.assertStderrEqual(stderr, '')
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000847 self.assertEqual(p.wait(), -signal.SIGKILL)
Christian Heimese74c8f22008-04-19 02:23:57 +0000848
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000849 def test_terminate(self):
Florent Xiclunac0838642010-03-07 15:27:39 +0000850 p = self._kill_process('terminate')
Florent Xicluna446ff142010-03-23 15:05:30 +0000851 _, stderr = p.communicate()
852 self.assertStderrEqual(stderr, '')
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000853 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000854
Antoine Pitrouf60845b2012-03-11 19:29:12 +0100855 def test_send_signal_dead(self):
856 # Sending a signal to a dead process
857 self._kill_dead_process('send_signal', signal.SIGINT)
858
859 def test_kill_dead(self):
860 # Killing a dead process
861 self._kill_dead_process('kill')
862
863 def test_terminate_dead(self):
864 # Terminating a dead process
865 self._kill_dead_process('terminate')
866
Antoine Pitrou91ce0d92011-01-03 18:45:09 +0000867 def check_close_std_fds(self, fds):
868 # Issue #9905: test that subprocess pipes still work properly with
869 # some standard fds closed
870 stdin = 0
871 newfds = []
872 for a in fds:
873 b = os.dup(a)
874 newfds.append(b)
875 if a == 0:
876 stdin = b
877 try:
878 for fd in fds:
879 os.close(fd)
880 out, err = subprocess.Popen([sys.executable, "-c",
881 'import sys;'
882 'sys.stdout.write("apple");'
883 'sys.stdout.flush();'
884 'sys.stderr.write("orange")'],
885 stdin=stdin,
886 stdout=subprocess.PIPE,
887 stderr=subprocess.PIPE).communicate()
888 err = test_support.strip_python_stderr(err)
889 self.assertEqual((out, err), (b'apple', b'orange'))
890 finally:
891 for b, a in zip(newfds, fds):
892 os.dup2(b, a)
893 for b in newfds:
894 os.close(b)
895
896 def test_close_fd_0(self):
897 self.check_close_std_fds([0])
898
899 def test_close_fd_1(self):
900 self.check_close_std_fds([1])
901
902 def test_close_fd_2(self):
903 self.check_close_std_fds([2])
904
905 def test_close_fds_0_1(self):
906 self.check_close_std_fds([0, 1])
907
908 def test_close_fds_0_2(self):
909 self.check_close_std_fds([0, 2])
910
911 def test_close_fds_1_2(self):
912 self.check_close_std_fds([1, 2])
913
914 def test_close_fds_0_1_2(self):
915 # Issue #10806: test that subprocess pipes still work properly with
916 # all standard fds closed.
917 self.check_close_std_fds([0, 1, 2])
918
Ross Lagerwalld8e39012011-07-27 18:54:53 +0200919 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
920 # open up some temporary files
921 temps = [mkstemp() for i in range(3)]
922 temp_fds = [fd for fd, fname in temps]
923 try:
924 # unlink the files -- we won't need to reopen them
925 for fd, fname in temps:
926 os.unlink(fname)
927
928 # save a copy of the standard file descriptors
929 saved_fds = [os.dup(fd) for fd in range(3)]
930 try:
931 # duplicate the temp files over the standard fd's 0, 1, 2
932 for fd, temp_fd in enumerate(temp_fds):
933 os.dup2(temp_fd, fd)
934
935 # write some data to what will become stdin, and rewind
936 os.write(stdin_no, b"STDIN")
937 os.lseek(stdin_no, 0, 0)
938
939 # now use those files in the given order, so that subprocess
940 # has to rearrange them in the child
941 p = subprocess.Popen([sys.executable, "-c",
942 'import sys; got = sys.stdin.read();'
943 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
944 stdin=stdin_no,
945 stdout=stdout_no,
946 stderr=stderr_no)
947 p.wait()
948
949 for fd in temp_fds:
950 os.lseek(fd, 0, 0)
951
952 out = os.read(stdout_no, 1024)
953 err = test_support.strip_python_stderr(os.read(stderr_no, 1024))
954 finally:
955 for std, saved in enumerate(saved_fds):
956 os.dup2(saved, std)
957 os.close(saved)
958
959 self.assertEqual(out, b"got STDIN")
960 self.assertEqual(err, b"err")
961
962 finally:
963 for fd in temp_fds:
964 os.close(fd)
965
966 # When duping fds, if there arises a situation where one of the fds is
967 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
968 # This tests all combinations of this.
969 def test_swap_fds(self):
970 self.check_swap_fds(0, 1, 2)
971 self.check_swap_fds(0, 2, 1)
972 self.check_swap_fds(1, 0, 2)
973 self.check_swap_fds(1, 2, 0)
974 self.check_swap_fds(2, 0, 1)
975 self.check_swap_fds(2, 1, 0)
976
Gregory P. Smith312efbc2010-12-14 15:02:53 +0000977 def test_wait_when_sigchild_ignored(self):
978 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
979 sigchild_ignore = test_support.findfile("sigchild_ignore.py",
980 subdir="subprocessdata")
981 p = subprocess.Popen([sys.executable, sigchild_ignore],
982 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
983 stdout, stderr = p.communicate()
984 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
985 " non-zero with this error:\n%s" % stderr)
986
Charles-François Natali100df0f2011-08-18 17:56:02 +0200987 def test_zombie_fast_process_del(self):
988 # Issue #12650: on Unix, if Popen.__del__() was called before the
989 # process exited, it wouldn't be added to subprocess._active, and would
990 # remain a zombie.
991 # spawn a Popen, and delete its reference before it exits
992 p = subprocess.Popen([sys.executable, "-c",
993 'import sys, time;'
994 'time.sleep(0.2)'],
995 stdout=subprocess.PIPE,
996 stderr=subprocess.PIPE)
Nadeem Vawda86059362011-08-19 05:22:24 +0200997 self.addCleanup(p.stdout.close)
998 self.addCleanup(p.stderr.close)
Charles-François Natali100df0f2011-08-18 17:56:02 +0200999 ident = id(p)
1000 pid = p.pid
1001 del p
1002 # check that p is in the active processes list
1003 self.assertIn(ident, [id(o) for o in subprocess._active])
1004
Charles-François Natali100df0f2011-08-18 17:56:02 +02001005 def test_leak_fast_process_del_killed(self):
1006 # Issue #12650: on Unix, if Popen.__del__() was called before the
1007 # process exited, and the process got killed by a signal, it would never
1008 # be removed from subprocess._active, which triggered a FD and memory
1009 # leak.
1010 # spawn a Popen, delete its reference and kill it
1011 p = subprocess.Popen([sys.executable, "-c",
1012 'import time;'
1013 'time.sleep(3)'],
1014 stdout=subprocess.PIPE,
1015 stderr=subprocess.PIPE)
Nadeem Vawda86059362011-08-19 05:22:24 +02001016 self.addCleanup(p.stdout.close)
1017 self.addCleanup(p.stderr.close)
Charles-François Natali100df0f2011-08-18 17:56:02 +02001018 ident = id(p)
1019 pid = p.pid
1020 del p
1021 os.kill(pid, signal.SIGKILL)
1022 # check that p is in the active processes list
1023 self.assertIn(ident, [id(o) for o in subprocess._active])
1024
1025 # let some time for the process to exit, and create a new Popen: this
1026 # should trigger the wait() of p
1027 time.sleep(0.2)
1028 with self.assertRaises(EnvironmentError) as c:
1029 with subprocess.Popen(['nonexisting_i_hope'],
1030 stdout=subprocess.PIPE,
1031 stderr=subprocess.PIPE) as proc:
1032 pass
1033 # p should have been wait()ed on, and removed from the _active list
1034 self.assertRaises(OSError, os.waitpid, pid, 0)
1035 self.assertNotIn(ident, [id(o) for o in subprocess._active])
1036
Charles-François Natali2a34eb32011-08-25 21:20:54 +02001037 def test_pipe_cloexec(self):
1038 # Issue 12786: check that the communication pipes' FDs are set CLOEXEC,
1039 # and are not inherited by another child process.
1040 p1 = subprocess.Popen([sys.executable, "-c",
1041 'import os;'
1042 'os.read(0, 1)'
1043 ],
1044 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1045 stderr=subprocess.PIPE)
1046
1047 p2 = subprocess.Popen([sys.executable, "-c", """if True:
1048 import os, errno, sys
1049 for fd in %r:
1050 try:
1051 os.close(fd)
1052 except OSError as e:
1053 if e.errno != errno.EBADF:
1054 raise
1055 else:
1056 sys.exit(1)
1057 sys.exit(0)
1058 """ % [f.fileno() for f in (p1.stdin, p1.stdout,
1059 p1.stderr)]
1060 ],
1061 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1062 stderr=subprocess.PIPE, close_fds=False)
1063 p1.communicate('foo')
1064 _, stderr = p2.communicate()
1065
1066 self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr))
1067
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001068
Florent Xiclunabab22a72010-03-04 19:40:48 +00001069@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunafc4d6d72010-03-23 14:36:45 +00001070class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaab5e17f2010-03-04 21:31:58 +00001071
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001072 def test_startupinfo(self):
1073 # startupinfo argument
1074 # We uses hardcoded constants, because we do not want to
1075 # depend on win32all.
1076 STARTF_USESHOWWINDOW = 1
1077 SW_MAXIMIZE = 3
1078 startupinfo = subprocess.STARTUPINFO()
1079 startupinfo.dwFlags = STARTF_USESHOWWINDOW
1080 startupinfo.wShowWindow = SW_MAXIMIZE
1081 # Since Python is a console process, it won't be affected
1082 # by wShowWindow, but the argument should be silently
1083 # ignored
1084 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001085 startupinfo=startupinfo)
1086
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001087 def test_creationflags(self):
1088 # creationflags argument
1089 CREATE_NEW_CONSOLE = 16
1090 sys.stderr.write(" a DOS box should flash briefly ...\n")
1091 subprocess.call(sys.executable +
1092 ' -c "import time; time.sleep(0.25)"',
1093 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001094
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001095 def test_invalid_args(self):
1096 # invalid arguments should raise ValueError
1097 self.assertRaises(ValueError, subprocess.call,
1098 [sys.executable, "-c",
1099 "import sys; sys.exit(47)"],
1100 preexec_fn=lambda: 1)
1101 self.assertRaises(ValueError, subprocess.call,
1102 [sys.executable, "-c",
1103 "import sys; sys.exit(47)"],
1104 stdout=subprocess.PIPE,
1105 close_fds=True)
1106
1107 def test_close_fds(self):
1108 # close file descriptors
1109 rc = subprocess.call([sys.executable, "-c",
1110 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001111 close_fds=True)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001112 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001113
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001114 def test_shell_sequence(self):
1115 # Run command through the shell (sequence)
1116 newenv = os.environ.copy()
1117 newenv["FRUIT"] = "physalis"
1118 p = subprocess.Popen(["set"], shell=1,
1119 stdout=subprocess.PIPE,
1120 env=newenv)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001121 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001122 self.assertIn("physalis", p.stdout.read())
Peter Astrand81a191b2007-05-26 22:18:20 +00001123
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001124 def test_shell_string(self):
1125 # Run command through the shell (string)
1126 newenv = os.environ.copy()
1127 newenv["FRUIT"] = "physalis"
1128 p = subprocess.Popen("set", shell=1,
1129 stdout=subprocess.PIPE,
1130 env=newenv)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001131 self.addCleanup(p.stdout.close)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001132 self.assertIn("physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001133
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001134 def test_call_string(self):
1135 # call() function with string argument on Windows
1136 rc = subprocess.call(sys.executable +
1137 ' -c "import sys; sys.exit(47)"')
1138 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001139
Florent Xiclunac0838642010-03-07 15:27:39 +00001140 def _kill_process(self, method, *args):
Florent Xicluna400efc22010-03-07 17:12:23 +00001141 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroudee00972010-09-24 19:00:29 +00001142 p = subprocess.Popen([sys.executable, "-c", """if 1:
1143 import sys, time
1144 sys.stdout.write('x\\n')
1145 sys.stdout.flush()
1146 time.sleep(30)
1147 """],
1148 stdin=subprocess.PIPE,
1149 stdout=subprocess.PIPE,
1150 stderr=subprocess.PIPE)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001151 self.addCleanup(p.stdout.close)
1152 self.addCleanup(p.stderr.close)
1153 self.addCleanup(p.stdin.close)
Antoine Pitroudee00972010-09-24 19:00:29 +00001154 # Wait for the interpreter to be completely initialized before
1155 # sending any signal.
1156 p.stdout.read(1)
1157 getattr(p, method)(*args)
Florent Xicluna446ff142010-03-23 15:05:30 +00001158 _, stderr = p.communicate()
1159 self.assertStderrEqual(stderr, '')
Antoine Pitroudee00972010-09-24 19:00:29 +00001160 returncode = p.wait()
Florent Xiclunafaf17532010-03-08 10:59:33 +00001161 self.assertNotEqual(returncode, 0)
Florent Xiclunac0838642010-03-07 15:27:39 +00001162
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001163 def _kill_dead_process(self, method, *args):
1164 p = subprocess.Popen([sys.executable, "-c", """if 1:
1165 import sys, time
1166 sys.stdout.write('x\\n')
1167 sys.stdout.flush()
1168 sys.exit(42)
1169 """],
1170 stdin=subprocess.PIPE,
1171 stdout=subprocess.PIPE,
1172 stderr=subprocess.PIPE)
1173 self.addCleanup(p.stdout.close)
1174 self.addCleanup(p.stderr.close)
1175 self.addCleanup(p.stdin.close)
1176 # Wait for the interpreter to be completely initialized before
1177 # sending any signal.
1178 p.stdout.read(1)
1179 # The process should end after this
1180 time.sleep(1)
1181 # This shouldn't raise even though the child is now dead
1182 getattr(p, method)(*args)
1183 _, stderr = p.communicate()
1184 self.assertStderrEqual(stderr, b'')
1185 rc = p.wait()
1186 self.assertEqual(rc, 42)
1187
Florent Xiclunac0838642010-03-07 15:27:39 +00001188 def test_send_signal(self):
1189 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimese74c8f22008-04-19 02:23:57 +00001190
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001191 def test_kill(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001192 self._kill_process('kill')
Christian Heimese74c8f22008-04-19 02:23:57 +00001193
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001194 def test_terminate(self):
Florent Xiclunac0838642010-03-07 15:27:39 +00001195 self._kill_process('terminate')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001196
Antoine Pitrouf60845b2012-03-11 19:29:12 +01001197 def test_send_signal_dead(self):
1198 self._kill_dead_process('send_signal', signal.SIGTERM)
1199
1200 def test_kill_dead(self):
1201 self._kill_dead_process('kill')
1202
1203 def test_terminate_dead(self):
1204 self._kill_dead_process('terminate')
1205
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001206
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001207@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
1208 "poll system call not supported")
1209class ProcessTestCaseNoPoll(ProcessTestCase):
1210 def setUp(self):
1211 subprocess._has_poll = False
1212 ProcessTestCase.setUp(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001213
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001214 def tearDown(self):
1215 subprocess._has_poll = True
1216 ProcessTestCase.tearDown(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001217
1218
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001219class HelperFunctionTests(unittest.TestCase):
Gregory P. Smithc1baf4a2010-03-01 02:53:24 +00001220 @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001221 def test_eintr_retry_call(self):
1222 record_calls = []
1223 def fake_os_func(*args):
1224 record_calls.append(args)
1225 if len(record_calls) == 2:
1226 raise OSError(errno.EINTR, "fake interrupted system call")
1227 return tuple(reversed(args))
1228
1229 self.assertEqual((999, 256),
1230 subprocess._eintr_retry_call(fake_os_func, 256, 999))
1231 self.assertEqual([(256, 999)], record_calls)
1232 # This time there will be an EINTR so it will loop once.
1233 self.assertEqual((666,),
1234 subprocess._eintr_retry_call(fake_os_func, 666))
1235 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
1236
Tim Golden8e4756c2010-08-12 11:00:35 +00001237@unittest.skipUnless(mswindows, "mswindows only")
1238class CommandsWithSpaces (BaseTestCase):
1239
1240 def setUp(self):
1241 super(CommandsWithSpaces, self).setUp()
1242 f, fname = mkstemp(".py", "te st")
1243 self.fname = fname.lower ()
1244 os.write(f, b"import sys;"
1245 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
1246 )
1247 os.close(f)
1248
1249 def tearDown(self):
1250 os.remove(self.fname)
1251 super(CommandsWithSpaces, self).tearDown()
1252
1253 def with_spaces(self, *args, **kwargs):
1254 kwargs['stdout'] = subprocess.PIPE
1255 p = subprocess.Popen(*args, **kwargs)
Brian Curtin7fe045e2010-11-05 17:19:38 +00001256 self.addCleanup(p.stdout.close)
Tim Golden8e4756c2010-08-12 11:00:35 +00001257 self.assertEqual(
1258 p.stdout.read ().decode("mbcs"),
1259 "2 [%r, 'ab cd']" % self.fname
1260 )
1261
1262 def test_shell_string_with_spaces(self):
1263 # call() function with string argument with spaces on Windows
Brian Curtine8c49202010-08-13 21:01:52 +00001264 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1265 "ab cd"), shell=1)
Tim Golden8e4756c2010-08-12 11:00:35 +00001266
1267 def test_shell_sequence_with_spaces(self):
1268 # call() function with sequence argument with spaces on Windows
Brian Curtine8c49202010-08-13 21:01:52 +00001269 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden8e4756c2010-08-12 11:00:35 +00001270
1271 def test_noshell_string_with_spaces(self):
1272 # call() function with string argument with spaces on Windows
1273 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1274 "ab cd"))
1275
1276 def test_noshell_sequence_with_spaces(self):
1277 # call() function with sequence argument with spaces on Windows
1278 self.with_spaces([sys.executable, self.fname, "ab cd"])
1279
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001280def test_main():
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001281 unit_tests = (ProcessTestCase,
1282 POSIXProcessTestCase,
1283 Win32ProcessTestCase,
Gregory P. Smithcce211f2010-03-01 00:05:08 +00001284 ProcessTestCaseNoPoll,
Tim Golden8e4756c2010-08-12 11:00:35 +00001285 HelperFunctionTests,
1286 CommandsWithSpaces)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001287
Gregory P. Smithdd7ca242009-07-04 01:49:29 +00001288 test_support.run_unittest(*unit_tests)
Florent Xicluna98e3fc32010-02-27 19:20:50 +00001289 test_support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001290
1291if __name__ == "__main__":
1292 test_main()