blob: db933937777d7e74c15df85be576852ab2fd75e8 [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
7import tempfile
8import time
Tim Peters3761e8d2004-10-13 04:07:12 +00009import re
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000010
11mswindows = (sys.platform == "win32")
12
13#
14# Depends on the following external programs: Python
15#
16
17if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000018 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
19 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000020else:
21 SETBINARY = ''
22
Tim Peters3761e8d2004-10-13 04:07:12 +000023# In a debug build, stuff like "[6580 refs]" is printed to stderr at
24# shutdown time. That frustrates tests trying to check stderr produced
25# from a spawned Python process.
26def remove_stderr_debug_decorations(stderr):
Tim Peters1dbf2432004-10-14 04:16:54 +000027 return re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
Tim Peters3761e8d2004-10-13 04:07:12 +000028
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000029class ProcessTestCase(unittest.TestCase):
Neal Norwitzb15ac312006-06-29 04:10:08 +000030 def setUp(self):
Tim Peters38ff36c2006-06-30 06:18:39 +000031 # Try to minimize the number of children we have so this test
32 # doesn't crash on some buildbots (Alphas in particular).
Peter Astrand2b221ed2006-07-10 20:39:49 +000033 if hasattr(test_support, "reap_children"):
34 test_support.reap_children()
Neal Norwitzb15ac312006-06-29 04:10:08 +000035
36 def tearDown(self):
Tim Peters38ff36c2006-06-30 06:18:39 +000037 # Try to minimize the number of children we have so this test
38 # doesn't crash on some buildbots (Alphas in particular).
Peter Astrand2b221ed2006-07-10 20:39:49 +000039 if hasattr(test_support, "reap_children"):
40 test_support.reap_children()
Neal Norwitzb15ac312006-06-29 04:10:08 +000041
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000042 def mkstemp(self):
43 """wrapper for mkstemp, calling mktemp if mkstemp is not available"""
44 if hasattr(tempfile, "mkstemp"):
45 return tempfile.mkstemp()
46 else:
47 fname = tempfile.mktemp()
48 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
Tim Peterse718f612004-10-12 21:51:32 +000049
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000050 #
51 # Generic tests
52 #
53 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000054 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000055 rc = subprocess.call([sys.executable, "-c",
56 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000057 self.assertEqual(rc, 47)
58
Peter Astrand454f7672005-01-01 09:36:35 +000059 def test_check_call_zero(self):
60 # check_call() function with zero return code
61 rc = subprocess.check_call([sys.executable, "-c",
62 "import sys; sys.exit(0)"])
63 self.assertEqual(rc, 0)
64
65 def test_check_call_nonzero(self):
66 # check_call() function with non-zero return code
67 try:
68 subprocess.check_call([sys.executable, "-c",
69 "import sys; sys.exit(47)"])
70 except subprocess.CalledProcessError, e:
Peter Astrand7d1d4362006-07-14 14:04:45 +000071 self.assertEqual(e.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000072 else:
73 self.fail("Expected CalledProcessError")
74
Gregory P. Smith26576802008-12-05 02:27:01 +000075 def test_check_output(self):
76 # check_output() function with zero return code
77 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000078 [sys.executable, "-c", "print 'BDFL'"])
Ezio Melottiaa980582010-01-23 23:04:36 +000079 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000080
Gregory P. Smith26576802008-12-05 02:27:01 +000081 def test_check_output_nonzero(self):
Gregory P. Smith97f49f42008-12-04 20:21:09 +000082 # check_call() function with non-zero return code
83 try:
Gregory P. Smith26576802008-12-05 02:27:01 +000084 subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000085 [sys.executable, "-c", "import sys; sys.exit(5)"])
86 except subprocess.CalledProcessError, e:
87 self.assertEqual(e.returncode, 5)
88 else:
89 self.fail("Expected CalledProcessError")
90
Gregory P. Smith26576802008-12-05 02:27:01 +000091 def test_check_output_stderr(self):
92 # check_output() function stderr redirected to stdout
93 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000094 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
95 stderr=subprocess.STDOUT)
Ezio Melottiaa980582010-01-23 23:04:36 +000096 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000097
Gregory P. Smith26576802008-12-05 02:27:01 +000098 def test_check_output_stdout_arg(self):
99 # check_output() function stderr redirected to stdout
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000100 try:
Gregory P. Smith26576802008-12-05 02:27:01 +0000101 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000102 [sys.executable, "-c", "print 'will not be run'"],
103 stdout=sys.stdout)
104 except ValueError, e:
Ezio Melottiaa980582010-01-23 23:04:36 +0000105 self.assertIn('stdout', e.args[0])
Gregory P. Smith97f49f42008-12-04 20:21:09 +0000106 else:
107 self.fail("Expected ValueError when stdout arg supplied.")
108
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000109 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000110 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000111 newenv = os.environ.copy()
112 newenv["FRUIT"] = "banana"
113 rc = subprocess.call([sys.executable, "-c",
114 'import sys, os;' \
115 'sys.exit(os.getenv("FRUIT")=="banana")'],
116 env=newenv)
117 self.assertEqual(rc, 1)
118
119 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000120 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000121 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
122 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
123 p.wait()
124 self.assertEqual(p.stdin, None)
125
126 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000127 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +0000128 p = subprocess.Popen([sys.executable, "-c",
Tim Peters4052fe52004-10-13 03:29:54 +0000129 'print " this bit of output is from a '
130 'test of stdout in a different '
131 'process ..."'],
132 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000133 p.wait()
134 self.assertEqual(p.stdout, None)
135
136 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000137 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000138 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
139 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
140 p.wait()
141 self.assertEqual(p.stderr, None)
142
143 def test_executable(self):
Tim Peters3b01a702004-10-12 22:19:32 +0000144 p = subprocess.Popen(["somethingyoudonthave",
145 "-c", "import sys; sys.exit(47)"],
146 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000147 p.wait()
148 self.assertEqual(p.returncode, 47)
149
150 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000151 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000152 p = subprocess.Popen([sys.executable, "-c",
153 'import sys; sys.exit(sys.stdin.read() == "pear")'],
154 stdin=subprocess.PIPE)
155 p.stdin.write("pear")
156 p.stdin.close()
157 p.wait()
158 self.assertEqual(p.returncode, 1)
159
160 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000161 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000162 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000163 d = tf.fileno()
164 os.write(d, "pear")
165 os.lseek(d, 0, 0)
166 p = subprocess.Popen([sys.executable, "-c",
167 'import sys; sys.exit(sys.stdin.read() == "pear")'],
168 stdin=d)
169 p.wait()
170 self.assertEqual(p.returncode, 1)
171
172 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000173 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000174 tf = tempfile.TemporaryFile()
175 tf.write("pear")
176 tf.seek(0)
177 p = subprocess.Popen([sys.executable, "-c",
178 'import sys; sys.exit(sys.stdin.read() == "pear")'],
179 stdin=tf)
180 p.wait()
181 self.assertEqual(p.returncode, 1)
182
183 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000184 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000185 p = subprocess.Popen([sys.executable, "-c",
186 'import sys; sys.stdout.write("orange")'],
187 stdout=subprocess.PIPE)
188 self.assertEqual(p.stdout.read(), "orange")
189
190 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000191 # stdout 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 p = subprocess.Popen([sys.executable, "-c",
195 'import sys; sys.stdout.write("orange")'],
196 stdout=d)
197 p.wait()
198 os.lseek(d, 0, 0)
199 self.assertEqual(os.read(d, 1024), "orange")
200
201 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000202 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000203 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000204 p = subprocess.Popen([sys.executable, "-c",
205 'import sys; sys.stdout.write("orange")'],
206 stdout=tf)
207 p.wait()
208 tf.seek(0)
209 self.assertEqual(tf.read(), "orange")
210
211 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000212 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000213 p = subprocess.Popen([sys.executable, "-c",
214 'import sys; sys.stderr.write("strawberry")'],
215 stderr=subprocess.PIPE)
Tim Peters3761e8d2004-10-13 04:07:12 +0000216 self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()),
217 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000218
219 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000220 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000221 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000222 d = tf.fileno()
223 p = subprocess.Popen([sys.executable, "-c",
224 'import sys; sys.stderr.write("strawberry")'],
225 stderr=d)
226 p.wait()
227 os.lseek(d, 0, 0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000228 self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)),
229 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000230
231 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000232 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000233 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000234 p = subprocess.Popen([sys.executable, "-c",
235 'import sys; sys.stderr.write("strawberry")'],
236 stderr=tf)
237 p.wait()
238 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000239 self.assertEqual(remove_stderr_debug_decorations(tf.read()),
240 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000241
242 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000243 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000244 p = subprocess.Popen([sys.executable, "-c",
245 'import sys;' \
246 'sys.stdout.write("apple");' \
247 'sys.stdout.flush();' \
248 'sys.stderr.write("orange")'],
249 stdout=subprocess.PIPE,
250 stderr=subprocess.STDOUT)
Tim Peters3761e8d2004-10-13 04:07:12 +0000251 output = p.stdout.read()
252 stripped = remove_stderr_debug_decorations(output)
253 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000254
255 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000256 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000257 tf = tempfile.TemporaryFile()
258 p = subprocess.Popen([sys.executable, "-c",
259 'import sys;' \
260 'sys.stdout.write("apple");' \
261 'sys.stdout.flush();' \
262 'sys.stderr.write("orange")'],
263 stdout=tf,
264 stderr=tf)
265 p.wait()
266 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000267 output = tf.read()
268 stripped = remove_stderr_debug_decorations(output)
269 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000270
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000271 def test_stdout_filedes_of_stdout(self):
272 # stdout is set to 1 (#1531862).
273 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))"
274 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
275 self.assertEquals(rc, 2)
276
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000277 def test_cwd(self):
Guido van Rossume9a0e882007-12-20 17:28:10 +0000278 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000279 # We cannot use os.path.realpath to canonicalize the path,
280 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
281 cwd = os.getcwd()
282 os.chdir(tmpdir)
283 tmpdir = os.getcwd()
284 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000285 p = subprocess.Popen([sys.executable, "-c",
286 'import sys,os;' \
287 'sys.stdout.write(os.getcwd())'],
288 stdout=subprocess.PIPE,
289 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000290 normcase = os.path.normcase
291 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000292
293 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000294 newenv = os.environ.copy()
295 newenv["FRUIT"] = "orange"
296 p = subprocess.Popen([sys.executable, "-c",
297 'import sys,os;' \
298 'sys.stdout.write(os.getenv("FRUIT"))'],
299 stdout=subprocess.PIPE,
300 env=newenv)
301 self.assertEqual(p.stdout.read(), "orange")
302
Peter Astrandcbac93c2005-03-03 20:24:28 +0000303 def test_communicate_stdin(self):
304 p = subprocess.Popen([sys.executable, "-c",
305 'import sys; sys.exit(sys.stdin.read() == "pear")'],
306 stdin=subprocess.PIPE)
307 p.communicate("pear")
308 self.assertEqual(p.returncode, 1)
309
310 def test_communicate_stdout(self):
311 p = subprocess.Popen([sys.executable, "-c",
312 'import sys; sys.stdout.write("pineapple")'],
313 stdout=subprocess.PIPE)
314 (stdout, stderr) = p.communicate()
315 self.assertEqual(stdout, "pineapple")
316 self.assertEqual(stderr, None)
317
318 def test_communicate_stderr(self):
319 p = subprocess.Popen([sys.executable, "-c",
320 'import sys; sys.stderr.write("pineapple")'],
321 stderr=subprocess.PIPE)
322 (stdout, stderr) = p.communicate()
323 self.assertEqual(stdout, None)
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000324 self.assertEqual(remove_stderr_debug_decorations(stderr), "pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000325
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000326 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000327 p = subprocess.Popen([sys.executable, "-c",
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000328 'import sys,os;'
329 'sys.stderr.write("pineapple");'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000330 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000331 stdin=subprocess.PIPE,
332 stdout=subprocess.PIPE,
333 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000334 (stdout, stderr) = p.communicate("banana")
335 self.assertEqual(stdout, "banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000336 self.assertEqual(remove_stderr_debug_decorations(stderr),
337 "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000338
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000339 # This test is Linux specific for simplicity to at least have
340 # some coverage. It is not a platform specific bug.
341 if os.path.isdir('/proc/%d/fd' % os.getpid()):
342 # Test for the fd leak reported in http://bugs.python.org/issue2791.
343 def test_communicate_pipe_fd_leak(self):
344 fd_directory = '/proc/%d/fd' % os.getpid()
345 num_fds_before_popen = len(os.listdir(fd_directory))
346 p = subprocess.Popen([sys.executable, '-c', 'print()'],
347 stdout=subprocess.PIPE)
348 p.communicate()
349 num_fds_after_communicate = len(os.listdir(fd_directory))
350 del p
351 num_fds_after_destruction = len(os.listdir(fd_directory))
352 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
353 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
354
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000355 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000356 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000357 p = subprocess.Popen([sys.executable, "-c",
358 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000359 (stdout, stderr) = p.communicate()
360 self.assertEqual(stdout, None)
361 self.assertEqual(stderr, None)
362
363 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000364 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000365 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000366 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000367 x, y = os.pipe()
368 if mswindows:
369 pipe_buf = 512
370 else:
371 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
372 os.close(x)
373 os.close(y)
374 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000375 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000376 'sys.stdout.write(sys.stdin.read(47));' \
377 'sys.stderr.write("xyz"*%d);' \
378 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000379 stdin=subprocess.PIPE,
380 stdout=subprocess.PIPE,
381 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000382 string_to_write = "abc"*pipe_buf
383 (stdout, stderr) = p.communicate(string_to_write)
384 self.assertEqual(stdout, string_to_write)
385
386 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000387 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000388 p = subprocess.Popen([sys.executable, "-c",
389 'import sys,os;' \
390 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000391 stdin=subprocess.PIPE,
392 stdout=subprocess.PIPE,
393 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000394 p.stdin.write("banana")
395 (stdout, stderr) = p.communicate("split")
396 self.assertEqual(stdout, "bananasplit")
Tim Peters3761e8d2004-10-13 04:07:12 +0000397 self.assertEqual(remove_stderr_debug_decorations(stderr), "")
Tim Peterse718f612004-10-12 21:51:32 +0000398
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000399 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000400 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000401 'import sys,os;' + SETBINARY +
402 'sys.stdout.write("line1\\n");'
403 'sys.stdout.flush();'
404 'sys.stdout.write("line2\\r");'
405 'sys.stdout.flush();'
406 'sys.stdout.write("line3\\r\\n");'
407 'sys.stdout.flush();'
408 'sys.stdout.write("line4\\r");'
409 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000410 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000411 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000412 'sys.stdout.write("\\nline6");'],
413 stdout=subprocess.PIPE,
414 universal_newlines=1)
415 stdout = p.stdout.read()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000416 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000417 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000418 self.assertEqual(stdout,
419 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000420 else:
421 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000422 self.assertEqual(stdout,
423 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000424
425 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000426 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000427 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000428 'import sys,os;' + SETBINARY +
429 'sys.stdout.write("line1\\n");'
430 'sys.stdout.flush();'
431 'sys.stdout.write("line2\\r");'
432 'sys.stdout.flush();'
433 'sys.stdout.write("line3\\r\\n");'
434 'sys.stdout.flush();'
435 'sys.stdout.write("line4\\r");'
436 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000437 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000438 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000439 'sys.stdout.write("\\nline6");'],
440 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
441 universal_newlines=1)
442 (stdout, stderr) = p.communicate()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000443 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000444 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000445 self.assertEqual(stdout,
446 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000447 else:
448 # Interpreter without universal newline support
449 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
450
451 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000452 # Make sure we leak no resources
Peter Astrandd6b24302006-06-22 20:06:46 +0000453 if not hasattr(test_support, "is_resource_enabled") \
454 or test_support.is_resource_enabled("subprocess") and not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000455 max_handles = 1026 # too much for most UNIX systems
456 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000457 max_handles = 65
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000458 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000459 p = subprocess.Popen([sys.executable, "-c",
460 "import sys;sys.stdout.write(sys.stdin.read())"],
461 stdin=subprocess.PIPE,
462 stdout=subprocess.PIPE,
463 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000464 data = p.communicate("lime")[0]
465 self.assertEqual(data, "lime")
466
467
468 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000469 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
470 '"a b c" d e')
471 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
472 'ab\\"c \\ d')
Gregory P. Smithe047e6d2008-01-19 20:49:02 +0000473 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
474 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000475 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
476 'a\\\\\\b "de fg" h')
477 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
478 'a\\\\\\"b c d')
479 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
480 '"a\\\\b c" d e')
481 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
482 '"a\\\\b\\ c" d e')
Peter Astrand10514a72007-01-13 22:35:35 +0000483 self.assertEqual(subprocess.list2cmdline(['ab', '']),
484 'ab ""')
Gregory P. Smith70eb2f92008-01-19 22:49:37 +0000485 self.assertEqual(subprocess.list2cmdline(['echo', 'foo|bar']),
486 'echo "foo|bar"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000487
488
489 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000490 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000491 "-c", "import time; time.sleep(1)"])
492 count = 0
493 while p.poll() is None:
494 time.sleep(0.1)
495 count += 1
496 # We expect that the poll loop probably went around about 10 times,
497 # but, based on system scheduling we can't control, it's possible
498 # poll() never returned None. It "should be" very rare that it
499 # didn't go around at least twice.
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000500 self.assertTrue(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000501 # Subsequent invocations should just return the returncode
502 self.assertEqual(p.poll(), 0)
503
504
505 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000506 p = subprocess.Popen([sys.executable,
507 "-c", "import time; time.sleep(2)"])
508 self.assertEqual(p.wait(), 0)
509 # Subsequent invocations should just return the returncode
510 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000511
Peter Astrand738131d2004-11-30 21:04:45 +0000512
513 def test_invalid_bufsize(self):
514 # an invalid type of the bufsize argument should raise
515 # TypeError.
516 try:
517 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
518 except TypeError:
519 pass
520 else:
521 self.fail("Expected TypeError")
522
Georg Brandlf3715d22009-02-14 17:01:36 +0000523 def test_leaking_fds_on_error(self):
524 # see bug #5179: Popen leaks file descriptors to PIPEs if
525 # the child fails to execute; this will eventually exhaust
526 # the maximum number of open fds. 1024 seems a very common
527 # value for that limit, but Windows has 2048, so we loop
528 # 1024 times (each call leaked two fds).
529 for i in range(1024):
530 try:
531 subprocess.Popen(['nonexisting_i_hope'],
532 stdout=subprocess.PIPE,
533 stderr=subprocess.PIPE)
534 # Windows raises IOError
535 except (IOError, OSError), err:
536 if err.errno != 2: # ignore "no such file"
537 raise
538
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000539 #
540 # POSIX tests
541 #
542 if not mswindows:
543 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000544 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000545 try:
546 p = subprocess.Popen([sys.executable, "-c", ""],
547 cwd="/this/path/does/not/exist")
548 except OSError, e:
549 # The attribute child_traceback should contain "os.chdir"
550 # somewhere.
551 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
552 else:
553 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000554
Andrew M. Kuchling86e1e382006-08-01 18:16:15 +0000555 def _suppress_core_files(self):
556 """Try to prevent core files from being created.
557 Returns previous ulimit if successful, else None.
558 """
559 try:
560 import resource
561 old_limit = resource.getrlimit(resource.RLIMIT_CORE)
562 resource.setrlimit(resource.RLIMIT_CORE, (0,0))
563 return old_limit
564 except (ImportError, ValueError, resource.error):
565 return None
566
567 def _unsuppress_core_files(self, old_limit):
568 """Return core file behavior to default."""
569 if old_limit is None:
570 return
571 try:
572 import resource
573 resource.setrlimit(resource.RLIMIT_CORE, old_limit)
574 except (ImportError, ValueError, resource.error):
575 return
Tim Peters4edcba62006-08-02 03:27:46 +0000576
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000577 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000578 # returncode handles signal termination
Andrew M. Kuchling86e1e382006-08-01 18:16:15 +0000579 old_limit = self._suppress_core_files()
580 try:
581 p = subprocess.Popen([sys.executable,
582 "-c", "import os; os.abort()"])
583 finally:
584 self._unsuppress_core_files(old_limit)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000585 p.wait()
586 self.assertEqual(-p.returncode, signal.SIGABRT)
587
588 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000589 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000590 p = subprocess.Popen([sys.executable, "-c",
591 'import sys,os;' \
592 'sys.stdout.write(os.getenv("FRUIT"))'],
593 stdout=subprocess.PIPE,
594 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
595 self.assertEqual(p.stdout.read(), "apple")
596
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000597 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000598 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000599 f, fname = self.mkstemp()
600 os.write(f, "#!/bin/sh\n")
Gregory P. Smithaf8a6872008-05-17 07:17:34 +0000601 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
Tim Peters3b01a702004-10-12 22:19:32 +0000602 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000603 os.close(f)
604 os.chmod(fname, 0700)
605 p = subprocess.Popen(fname)
606 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000607 os.remove(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000608 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000609
610 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000611 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000612 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000613 [sys.executable,
614 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000615 startupinfo=47)
616 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000617 [sys.executable,
618 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000619 creationflags=47)
620
621 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000622 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000623 newenv = os.environ.copy()
624 newenv["FRUIT"] = "apple"
625 p = subprocess.Popen(["echo $FRUIT"], shell=1,
626 stdout=subprocess.PIPE,
627 env=newenv)
628 self.assertEqual(p.stdout.read().strip(), "apple")
629
630 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000631 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000632 newenv = os.environ.copy()
633 newenv["FRUIT"] = "apple"
634 p = subprocess.Popen("echo $FRUIT", shell=1,
635 stdout=subprocess.PIPE,
636 env=newenv)
637 self.assertEqual(p.stdout.read().strip(), "apple")
638
639 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000640 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000641 f, fname = self.mkstemp()
642 os.write(f, "#!/bin/sh\n")
Gregory P. Smithaf8a6872008-05-17 07:17:34 +0000643 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
Tim Peters3b01a702004-10-12 22:19:32 +0000644 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000645 os.close(f)
646 os.chmod(fname, 0700)
647 rc = subprocess.call(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000648 os.remove(fname)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000649 self.assertEqual(rc, 47)
650
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000651 def DISABLED_test_send_signal(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000652 p = subprocess.Popen([sys.executable,
653 "-c", "input()"])
654
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000655 self.assertTrue(p.poll() is None, p.poll())
Christian Heimese74c8f22008-04-19 02:23:57 +0000656 p.send_signal(signal.SIGINT)
657 self.assertNotEqual(p.wait(), 0)
658
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000659 def DISABLED_test_kill(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000660 p = subprocess.Popen([sys.executable,
661 "-c", "input()"])
662
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000663 self.assertTrue(p.poll() is None, p.poll())
Christian Heimese74c8f22008-04-19 02:23:57 +0000664 p.kill()
665 self.assertEqual(p.wait(), -signal.SIGKILL)
666
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000667 def DISABLED_test_terminate(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000668 p = subprocess.Popen([sys.executable,
669 "-c", "input()"])
670
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000671 self.assertTrue(p.poll() is None, p.poll())
Christian Heimese74c8f22008-04-19 02:23:57 +0000672 p.terminate()
673 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000674
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000675 #
676 # Windows tests
677 #
678 if mswindows:
679 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000680 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000681 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000682 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000683 STARTF_USESHOWWINDOW = 1
684 SW_MAXIMIZE = 3
685 startupinfo = subprocess.STARTUPINFO()
686 startupinfo.dwFlags = STARTF_USESHOWWINDOW
687 startupinfo.wShowWindow = SW_MAXIMIZE
688 # Since Python is a console process, it won't be affected
689 # by wShowWindow, but the argument should be silently
690 # ignored
691 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
692 startupinfo=startupinfo)
693
694 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000695 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000696 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000697 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000698 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000699 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000700 creationflags=CREATE_NEW_CONSOLE)
701
702 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000703 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000704 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000705 [sys.executable,
706 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000707 preexec_fn=lambda: 1)
708 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000709 [sys.executable,
710 "-c", "import sys; sys.exit(47)"],
Peter Astrand81a191b2007-05-26 22:18:20 +0000711 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000712 close_fds=True)
713
Peter Astrand81a191b2007-05-26 22:18:20 +0000714 def test_close_fds(self):
715 # close file descriptors
716 rc = subprocess.call([sys.executable, "-c",
717 "import sys; sys.exit(47)"],
718 close_fds=True)
719 self.assertEqual(rc, 47)
720
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000721 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000722 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000723 newenv = os.environ.copy()
724 newenv["FRUIT"] = "physalis"
725 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000726 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000727 env=newenv)
728 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
729
730 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000731 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000732 newenv = os.environ.copy()
733 newenv["FRUIT"] = "physalis"
734 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000735 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000736 env=newenv)
737 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
738
739 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000740 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000741 rc = subprocess.call(sys.executable +
742 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000743 self.assertEqual(rc, 47)
744
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000745 def DISABLED_test_send_signal(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000746 p = subprocess.Popen([sys.executable,
747 "-c", "input()"])
748
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000749 self.assertTrue(p.poll() is None, p.poll())
Christian Heimese74c8f22008-04-19 02:23:57 +0000750 p.send_signal(signal.SIGTERM)
751 self.assertNotEqual(p.wait(), 0)
752
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000753 def DISABLED_test_kill(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000754 p = subprocess.Popen([sys.executable,
755 "-c", "input()"])
756
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000757 self.assertTrue(p.poll() is None, p.poll())
Christian Heimese74c8f22008-04-19 02:23:57 +0000758 p.kill()
759 self.assertNotEqual(p.wait(), 0)
760
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000761 def DISABLED_test_terminate(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000762 p = subprocess.Popen([sys.executable,
763 "-c", "input()"])
764
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000765 self.assertTrue(p.poll() is None, p.poll())
Christian Heimese74c8f22008-04-19 02:23:57 +0000766 p.terminate()
767 self.assertNotEqual(p.wait(), 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000768
Gregory P. Smithdd7ca242009-07-04 01:49:29 +0000769
770unit_tests = [ProcessTestCase]
771
Amaury Forgeot d'Arcce32eb72009-07-09 22:37:22 +0000772if getattr(subprocess, '_has_poll', False):
Gregory P. Smithdd7ca242009-07-04 01:49:29 +0000773 class ProcessTestCaseNoPoll(ProcessTestCase):
774 def setUp(self):
775 subprocess._has_poll = False
776 ProcessTestCase.setUp(self)
777
778 def tearDown(self):
779 subprocess._has_poll = True
780 ProcessTestCase.tearDown(self)
781
782 unit_tests.append(ProcessTestCaseNoPoll)
783
784
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000785def test_main():
Gregory P. Smithdd7ca242009-07-04 01:49:29 +0000786 test_support.run_unittest(*unit_tests)
Peter Astrand2b221ed2006-07-10 20:39:49 +0000787 if hasattr(test_support, "reap_children"):
788 test_support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000789
790if __name__ == "__main__":
791 test_main()