blob: 7dde01313edf5b602b134da83fabc3c85fc34a8a [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. Smithca574992010-03-01 00:35:34 +00007import errno
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00008import tempfile
9import time
Tim Peters3761e8d2004-10-13 04:07:12 +000010import re
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000011
12mswindows = (sys.platform == "win32")
13
14#
15# Depends on the following external programs: Python
16#
17
18if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000019 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
20 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000021else:
22 SETBINARY = ''
23
Tim Peters3761e8d2004-10-13 04:07:12 +000024# In a debug build, stuff like "[6580 refs]" is printed to stderr at
25# shutdown time. That frustrates tests trying to check stderr produced
26# from a spawned Python process.
27def remove_stderr_debug_decorations(stderr):
Tim Peters1dbf2432004-10-14 04:16:54 +000028 return re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
Tim Peters3761e8d2004-10-13 04:07:12 +000029
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000030class ProcessTestCase(unittest.TestCase):
Neal Norwitzb15ac312006-06-29 04:10:08 +000031 def setUp(self):
Tim Peters38ff36c2006-06-30 06:18:39 +000032 # Try to minimize the number of children we have so this test
33 # doesn't crash on some buildbots (Alphas in particular).
Peter Astrand2b221ed2006-07-10 20:39:49 +000034 if hasattr(test_support, "reap_children"):
35 test_support.reap_children()
Neal Norwitzb15ac312006-06-29 04:10:08 +000036
37 def tearDown(self):
Tim Peters38ff36c2006-06-30 06:18:39 +000038 # Try to minimize the number of children we have so this test
39 # doesn't crash on some buildbots (Alphas in particular).
Peter Astrand2b221ed2006-07-10 20:39:49 +000040 if hasattr(test_support, "reap_children"):
41 test_support.reap_children()
Neal Norwitzb15ac312006-06-29 04:10:08 +000042
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000043 def mkstemp(self):
44 """wrapper for mkstemp, calling mktemp if mkstemp is not available"""
45 if hasattr(tempfile, "mkstemp"):
46 return tempfile.mkstemp()
47 else:
48 fname = tempfile.mktemp()
49 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
Tim Peterse718f612004-10-12 21:51:32 +000050
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000051 #
52 # Generic tests
53 #
54 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000055 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000056 rc = subprocess.call([sys.executable, "-c",
57 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000058 self.assertEqual(rc, 47)
59
Peter Astrand454f7672005-01-01 09:36:35 +000060 def test_check_call_zero(self):
61 # check_call() function with zero return code
62 rc = subprocess.check_call([sys.executable, "-c",
63 "import sys; sys.exit(0)"])
64 self.assertEqual(rc, 0)
65
66 def test_check_call_nonzero(self):
67 # check_call() function with non-zero return code
68 try:
69 subprocess.check_call([sys.executable, "-c",
70 "import sys; sys.exit(47)"])
71 except subprocess.CalledProcessError, e:
Peter Astrand7d1d4362006-07-14 14:04:45 +000072 self.assertEqual(e.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000073 else:
74 self.fail("Expected CalledProcessError")
75
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000076 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +000077 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000078 newenv = os.environ.copy()
79 newenv["FRUIT"] = "banana"
80 rc = subprocess.call([sys.executable, "-c",
81 'import sys, os;' \
82 'sys.exit(os.getenv("FRUIT")=="banana")'],
83 env=newenv)
84 self.assertEqual(rc, 1)
85
86 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000087 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000088 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
89 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
90 p.wait()
91 self.assertEqual(p.stdin, None)
92
93 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000094 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +000095 p = subprocess.Popen([sys.executable, "-c",
Tim Peters4052fe52004-10-13 03:29:54 +000096 'print " this bit of output is from a '
97 'test of stdout in a different '
98 'process ..."'],
99 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000100 p.wait()
101 self.assertEqual(p.stdout, None)
102
103 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000104 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000105 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
106 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
107 p.wait()
108 self.assertEqual(p.stderr, None)
109
110 def test_executable(self):
Tim Peters3b01a702004-10-12 22:19:32 +0000111 p = subprocess.Popen(["somethingyoudonthave",
112 "-c", "import sys; sys.exit(47)"],
113 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000114 p.wait()
115 self.assertEqual(p.returncode, 47)
116
117 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000118 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000119 p = subprocess.Popen([sys.executable, "-c",
120 'import sys; sys.exit(sys.stdin.read() == "pear")'],
121 stdin=subprocess.PIPE)
122 p.stdin.write("pear")
123 p.stdin.close()
124 p.wait()
125 self.assertEqual(p.returncode, 1)
126
127 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000128 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000129 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000130 d = tf.fileno()
131 os.write(d, "pear")
132 os.lseek(d, 0, 0)
133 p = subprocess.Popen([sys.executable, "-c",
134 'import sys; sys.exit(sys.stdin.read() == "pear")'],
135 stdin=d)
136 p.wait()
137 self.assertEqual(p.returncode, 1)
138
139 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000140 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000141 tf = tempfile.TemporaryFile()
142 tf.write("pear")
143 tf.seek(0)
144 p = subprocess.Popen([sys.executable, "-c",
145 'import sys; sys.exit(sys.stdin.read() == "pear")'],
146 stdin=tf)
147 p.wait()
148 self.assertEqual(p.returncode, 1)
149
150 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000151 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000152 p = subprocess.Popen([sys.executable, "-c",
153 'import sys; sys.stdout.write("orange")'],
154 stdout=subprocess.PIPE)
155 self.assertEqual(p.stdout.read(), "orange")
156
157 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000158 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000159 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000160 d = tf.fileno()
161 p = subprocess.Popen([sys.executable, "-c",
162 'import sys; sys.stdout.write("orange")'],
163 stdout=d)
164 p.wait()
165 os.lseek(d, 0, 0)
166 self.assertEqual(os.read(d, 1024), "orange")
167
168 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000169 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000170 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000171 p = subprocess.Popen([sys.executable, "-c",
172 'import sys; sys.stdout.write("orange")'],
173 stdout=tf)
174 p.wait()
175 tf.seek(0)
176 self.assertEqual(tf.read(), "orange")
177
178 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000179 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000180 p = subprocess.Popen([sys.executable, "-c",
181 'import sys; sys.stderr.write("strawberry")'],
182 stderr=subprocess.PIPE)
Tim Peters3761e8d2004-10-13 04:07:12 +0000183 self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()),
184 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000185
186 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000187 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000188 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000189 d = tf.fileno()
190 p = subprocess.Popen([sys.executable, "-c",
191 'import sys; sys.stderr.write("strawberry")'],
192 stderr=d)
193 p.wait()
194 os.lseek(d, 0, 0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000195 self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)),
196 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000197
198 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000199 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000200 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000201 p = subprocess.Popen([sys.executable, "-c",
202 'import sys; sys.stderr.write("strawberry")'],
203 stderr=tf)
204 p.wait()
205 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000206 self.assertEqual(remove_stderr_debug_decorations(tf.read()),
207 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000208
209 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000210 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000211 p = subprocess.Popen([sys.executable, "-c",
212 'import sys;' \
213 'sys.stdout.write("apple");' \
214 'sys.stdout.flush();' \
215 'sys.stderr.write("orange")'],
216 stdout=subprocess.PIPE,
217 stderr=subprocess.STDOUT)
Tim Peters3761e8d2004-10-13 04:07:12 +0000218 output = p.stdout.read()
219 stripped = remove_stderr_debug_decorations(output)
220 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000221
222 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000223 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000224 tf = tempfile.TemporaryFile()
225 p = subprocess.Popen([sys.executable, "-c",
226 'import sys;' \
227 'sys.stdout.write("apple");' \
228 'sys.stdout.flush();' \
229 'sys.stderr.write("orange")'],
230 stdout=tf,
231 stderr=tf)
232 p.wait()
233 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000234 output = tf.read()
235 stripped = remove_stderr_debug_decorations(output)
236 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000237
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000238 def test_stdout_filedes_of_stdout(self):
239 # stdout is set to 1 (#1531862).
240 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))"
241 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
242 self.assertEquals(rc, 2)
243
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000244 def test_cwd(self):
Guido van Rossume9a0e882007-12-20 17:28:10 +0000245 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000246 # We cannot use os.path.realpath to canonicalize the path,
247 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
248 cwd = os.getcwd()
249 os.chdir(tmpdir)
250 tmpdir = os.getcwd()
251 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000252 p = subprocess.Popen([sys.executable, "-c",
253 'import sys,os;' \
254 'sys.stdout.write(os.getcwd())'],
255 stdout=subprocess.PIPE,
256 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000257 normcase = os.path.normcase
258 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000259
260 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000261 newenv = os.environ.copy()
262 newenv["FRUIT"] = "orange"
263 p = subprocess.Popen([sys.executable, "-c",
264 'import sys,os;' \
265 'sys.stdout.write(os.getenv("FRUIT"))'],
266 stdout=subprocess.PIPE,
267 env=newenv)
268 self.assertEqual(p.stdout.read(), "orange")
269
Peter Astrandcbac93c2005-03-03 20:24:28 +0000270 def test_communicate_stdin(self):
271 p = subprocess.Popen([sys.executable, "-c",
272 'import sys; sys.exit(sys.stdin.read() == "pear")'],
273 stdin=subprocess.PIPE)
274 p.communicate("pear")
275 self.assertEqual(p.returncode, 1)
276
277 def test_communicate_stdout(self):
278 p = subprocess.Popen([sys.executable, "-c",
279 'import sys; sys.stdout.write("pineapple")'],
280 stdout=subprocess.PIPE)
281 (stdout, stderr) = p.communicate()
282 self.assertEqual(stdout, "pineapple")
283 self.assertEqual(stderr, None)
284
285 def test_communicate_stderr(self):
286 p = subprocess.Popen([sys.executable, "-c",
287 'import sys; sys.stderr.write("pineapple")'],
288 stderr=subprocess.PIPE)
289 (stdout, stderr) = p.communicate()
290 self.assertEqual(stdout, None)
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000291 self.assertEqual(remove_stderr_debug_decorations(stderr), "pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000292
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000293 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000294 p = subprocess.Popen([sys.executable, "-c",
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000295 'import sys,os;'
296 'sys.stderr.write("pineapple");'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000297 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000298 stdin=subprocess.PIPE,
299 stdout=subprocess.PIPE,
300 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000301 (stdout, stderr) = p.communicate("banana")
302 self.assertEqual(stdout, "banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000303 self.assertEqual(remove_stderr_debug_decorations(stderr),
304 "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000305
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000306 # This test is Linux specific for simplicity to at least have
307 # some coverage. It is not a platform specific bug.
308 if os.path.isdir('/proc/%d/fd' % os.getpid()):
309 # Test for the fd leak reported in http://bugs.python.org/issue2791.
310 def test_communicate_pipe_fd_leak(self):
311 fd_directory = '/proc/%d/fd' % os.getpid()
312 num_fds_before_popen = len(os.listdir(fd_directory))
313 p = subprocess.Popen([sys.executable, '-c', 'print()'],
314 stdout=subprocess.PIPE)
315 p.communicate()
316 num_fds_after_communicate = len(os.listdir(fd_directory))
317 del p
318 num_fds_after_destruction = len(os.listdir(fd_directory))
319 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
320 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
321
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000322 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000323 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000324 p = subprocess.Popen([sys.executable, "-c",
325 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000326 (stdout, stderr) = p.communicate()
327 self.assertEqual(stdout, None)
328 self.assertEqual(stderr, None)
329
330 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000331 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000332 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000333 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000334 x, y = os.pipe()
335 if mswindows:
336 pipe_buf = 512
337 else:
338 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
339 os.close(x)
340 os.close(y)
341 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000342 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000343 'sys.stdout.write(sys.stdin.read(47));' \
344 'sys.stderr.write("xyz"*%d);' \
345 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000346 stdin=subprocess.PIPE,
347 stdout=subprocess.PIPE,
348 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000349 string_to_write = "abc"*pipe_buf
350 (stdout, stderr) = p.communicate(string_to_write)
351 self.assertEqual(stdout, string_to_write)
352
353 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000354 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000355 p = subprocess.Popen([sys.executable, "-c",
356 'import sys,os;' \
357 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000358 stdin=subprocess.PIPE,
359 stdout=subprocess.PIPE,
360 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000361 p.stdin.write("banana")
362 (stdout, stderr) = p.communicate("split")
363 self.assertEqual(stdout, "bananasplit")
Tim Peters3761e8d2004-10-13 04:07:12 +0000364 self.assertEqual(remove_stderr_debug_decorations(stderr), "")
Tim Peterse718f612004-10-12 21:51:32 +0000365
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000366 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000367 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000368 'import sys,os;' + SETBINARY +
369 'sys.stdout.write("line1\\n");'
370 'sys.stdout.flush();'
371 'sys.stdout.write("line2\\r");'
372 'sys.stdout.flush();'
373 'sys.stdout.write("line3\\r\\n");'
374 'sys.stdout.flush();'
375 'sys.stdout.write("line4\\r");'
376 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000377 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000378 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000379 'sys.stdout.write("\\nline6");'],
380 stdout=subprocess.PIPE,
381 universal_newlines=1)
382 stdout = p.stdout.read()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000383 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000384 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000385 self.assertEqual(stdout,
386 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000387 else:
388 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000389 self.assertEqual(stdout,
390 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000391
392 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000393 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000394 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000395 'import sys,os;' + SETBINARY +
396 'sys.stdout.write("line1\\n");'
397 'sys.stdout.flush();'
398 'sys.stdout.write("line2\\r");'
399 'sys.stdout.flush();'
400 'sys.stdout.write("line3\\r\\n");'
401 'sys.stdout.flush();'
402 'sys.stdout.write("line4\\r");'
403 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000404 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000405 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000406 'sys.stdout.write("\\nline6");'],
407 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
408 universal_newlines=1)
409 (stdout, stderr) = p.communicate()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000410 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000411 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000412 self.assertEqual(stdout,
413 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000414 else:
415 # Interpreter without universal newline support
416 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
417
418 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000419 # Make sure we leak no resources
Peter Astrandd6b24302006-06-22 20:06:46 +0000420 if not hasattr(test_support, "is_resource_enabled") \
421 or test_support.is_resource_enabled("subprocess") and not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000422 max_handles = 1026 # too much for most UNIX systems
423 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000424 max_handles = 65
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000425 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000426 p = subprocess.Popen([sys.executable, "-c",
427 "import sys;sys.stdout.write(sys.stdin.read())"],
428 stdin=subprocess.PIPE,
429 stdout=subprocess.PIPE,
430 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000431 data = p.communicate("lime")[0]
432 self.assertEqual(data, "lime")
433
434
435 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000436 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
437 '"a b c" d e')
438 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
439 'ab\\"c \\ d')
Gregory P. Smithe047e6d2008-01-19 20:49:02 +0000440 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
441 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000442 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
443 'a\\\\\\b "de fg" h')
444 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
445 'a\\\\\\"b c d')
446 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
447 '"a\\\\b c" d e')
448 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
449 '"a\\\\b\\ c" d e')
Peter Astrand10514a72007-01-13 22:35:35 +0000450 self.assertEqual(subprocess.list2cmdline(['ab', '']),
451 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000452
453
454 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000455 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000456 "-c", "import time; time.sleep(1)"])
457 count = 0
458 while p.poll() is None:
459 time.sleep(0.1)
460 count += 1
461 # We expect that the poll loop probably went around about 10 times,
462 # but, based on system scheduling we can't control, it's possible
463 # poll() never returned None. It "should be" very rare that it
464 # didn't go around at least twice.
465 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000466 # Subsequent invocations should just return the returncode
467 self.assertEqual(p.poll(), 0)
468
469
470 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000471 p = subprocess.Popen([sys.executable,
472 "-c", "import time; time.sleep(2)"])
473 self.assertEqual(p.wait(), 0)
474 # Subsequent invocations should just return the returncode
475 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000476
Peter Astrand738131d2004-11-30 21:04:45 +0000477
478 def test_invalid_bufsize(self):
479 # an invalid type of the bufsize argument should raise
480 # TypeError.
481 try:
482 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
483 except TypeError:
484 pass
485 else:
486 self.fail("Expected TypeError")
487
Georg Brandl78162da2009-02-14 17:04:26 +0000488 def test_leaking_fds_on_error(self):
489 # see bug #5179: Popen leaks file descriptors to PIPEs if
490 # the child fails to execute; this will eventually exhaust
491 # the maximum number of open fds. 1024 seems a very common
492 # value for that limit, but Windows has 2048, so we loop
493 # 1024 times (each call leaked two fds).
494 for i in range(1024):
495 try:
496 subprocess.Popen(['nonexisting_i_hope'],
497 stdout=subprocess.PIPE,
498 stderr=subprocess.PIPE)
499 # Windows raises IOError
500 except (IOError, OSError), err:
501 if err.errno != 2: # ignore "no such file"
502 raise
503
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000504 #
505 # POSIX tests
506 #
507 if not mswindows:
508 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000509 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000510 try:
511 p = subprocess.Popen([sys.executable, "-c", ""],
512 cwd="/this/path/does/not/exist")
513 except OSError, e:
514 # The attribute child_traceback should contain "os.chdir"
515 # somewhere.
516 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
517 else:
518 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000519
Andrew M. Kuchling86e1e382006-08-01 18:16:15 +0000520 def _suppress_core_files(self):
521 """Try to prevent core files from being created.
522 Returns previous ulimit if successful, else None.
523 """
Ronald Oussorenef8204e2010-07-23 12:32:07 +0000524 if sys.platform == 'darwin':
525 # Check if the 'Crash Reporter' on OSX was configured
526 # in 'Developer' mode and warn that it will get triggered
527 # when it is.
528 #
529 # This assumes that this context manager is used in tests
530 # that might trigger the next manager.
531 value = subprocess.Popen(['/usr/bin/defaults', 'read',
532 'com.apple.CrashReporter', 'DialogType'],
533 stdout=subprocess.PIPE).communicate()[0]
534 if value.strip() == b'developer':
535 print "this tests triggers the Crash Reporter, that is intentional"
536 sys.stdout.flush()
537
Andrew M. Kuchling86e1e382006-08-01 18:16:15 +0000538 try:
539 import resource
540 old_limit = resource.getrlimit(resource.RLIMIT_CORE)
541 resource.setrlimit(resource.RLIMIT_CORE, (0,0))
542 return old_limit
543 except (ImportError, ValueError, resource.error):
544 return None
545
546 def _unsuppress_core_files(self, old_limit):
547 """Return core file behavior to default."""
548 if old_limit is None:
549 return
550 try:
551 import resource
552 resource.setrlimit(resource.RLIMIT_CORE, old_limit)
553 except (ImportError, ValueError, resource.error):
554 return
Tim Peters4edcba62006-08-02 03:27:46 +0000555
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000556 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000557 # returncode handles signal termination
Andrew M. Kuchling86e1e382006-08-01 18:16:15 +0000558 old_limit = self._suppress_core_files()
559 try:
560 p = subprocess.Popen([sys.executable,
561 "-c", "import os; os.abort()"])
562 finally:
563 self._unsuppress_core_files(old_limit)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000564 p.wait()
565 self.assertEqual(-p.returncode, signal.SIGABRT)
566
567 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000568 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000569 p = subprocess.Popen([sys.executable, "-c",
570 'import sys,os;' \
571 'sys.stdout.write(os.getenv("FRUIT"))'],
572 stdout=subprocess.PIPE,
573 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
574 self.assertEqual(p.stdout.read(), "apple")
575
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000576 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000577 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000578 f, fname = self.mkstemp()
579 os.write(f, "#!/bin/sh\n")
Gregory P. Smithaf8a6872008-05-17 07:17:34 +0000580 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
Tim Peters3b01a702004-10-12 22:19:32 +0000581 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000582 os.close(f)
583 os.chmod(fname, 0700)
584 p = subprocess.Popen(fname)
585 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000586 os.remove(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000587 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000588
589 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000590 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000591 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000592 [sys.executable,
593 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000594 startupinfo=47)
595 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000596 [sys.executable,
597 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000598 creationflags=47)
599
600 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000601 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000602 newenv = os.environ.copy()
603 newenv["FRUIT"] = "apple"
604 p = subprocess.Popen(["echo $FRUIT"], shell=1,
605 stdout=subprocess.PIPE,
606 env=newenv)
607 self.assertEqual(p.stdout.read().strip(), "apple")
608
609 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000610 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000611 newenv = os.environ.copy()
612 newenv["FRUIT"] = "apple"
613 p = subprocess.Popen("echo $FRUIT", shell=1,
614 stdout=subprocess.PIPE,
615 env=newenv)
616 self.assertEqual(p.stdout.read().strip(), "apple")
617
618 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000619 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000620 f, fname = self.mkstemp()
621 os.write(f, "#!/bin/sh\n")
Gregory P. Smithaf8a6872008-05-17 07:17:34 +0000622 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
Tim Peters3b01a702004-10-12 22:19:32 +0000623 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000624 os.close(f)
625 os.chmod(fname, 0700)
626 rc = subprocess.call(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000627 os.remove(fname)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000628 self.assertEqual(rc, 47)
629
Stefan Krah9ea629c2010-07-19 14:49:38 +0000630 def test_specific_shell(self):
631 # Issue #9265: Incorrect name passed as arg[0].
632 shells = []
633 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
634 for name in ['bash', 'ksh']:
635 sh = os.path.join(prefix, name)
636 if os.path.isfile(sh):
637 shells.append(sh)
Florent Xicluna13bc24b2010-08-11 00:19:53 +0000638 if not shells: # Will probably work for any shell but csh.
639 return # skip test
Stefan Krah9ea629c2010-07-19 14:49:38 +0000640 sh = '/bin/sh'
641 if os.path.isfile(sh) and not os.path.islink(sh):
642 # Test will fail if /bin/sh is a symlink to csh.
643 shells.append(sh)
644 for sh in shells:
645 p = subprocess.Popen("echo $0", executable=sh, shell=True,
646 stdout=subprocess.PIPE)
647 self.assertEqual(p.stdout.read().strip(), sh)
648
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000649 def DISABLED_test_send_signal(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000650 p = subprocess.Popen([sys.executable,
651 "-c", "input()"])
652
653 self.assert_(p.poll() is None, p.poll())
654 p.send_signal(signal.SIGINT)
655 self.assertNotEqual(p.wait(), 0)
656
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000657 def DISABLED_test_kill(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000658 p = subprocess.Popen([sys.executable,
659 "-c", "input()"])
660
661 self.assert_(p.poll() is None, p.poll())
662 p.kill()
663 self.assertEqual(p.wait(), -signal.SIGKILL)
664
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000665 def DISABLED_test_terminate(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000666 p = subprocess.Popen([sys.executable,
667 "-c", "input()"])
668
669 self.assert_(p.poll() is None, p.poll())
670 p.terminate()
671 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000672
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000673 #
674 # Windows tests
675 #
676 if mswindows:
677 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000678 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000679 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000680 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000681 STARTF_USESHOWWINDOW = 1
682 SW_MAXIMIZE = 3
683 startupinfo = subprocess.STARTUPINFO()
684 startupinfo.dwFlags = STARTF_USESHOWWINDOW
685 startupinfo.wShowWindow = SW_MAXIMIZE
686 # Since Python is a console process, it won't be affected
687 # by wShowWindow, but the argument should be silently
688 # ignored
689 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
690 startupinfo=startupinfo)
691
692 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000693 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000694 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000695 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000696 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000697 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000698 creationflags=CREATE_NEW_CONSOLE)
699
700 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000701 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000702 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000703 [sys.executable,
704 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000705 preexec_fn=lambda: 1)
706 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000707 [sys.executable,
708 "-c", "import sys; sys.exit(47)"],
Peter Astrand81a191b2007-05-26 22:18:20 +0000709 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000710 close_fds=True)
711
Peter Astrand81a191b2007-05-26 22:18:20 +0000712 def test_close_fds(self):
713 # close file descriptors
714 rc = subprocess.call([sys.executable, "-c",
715 "import sys; sys.exit(47)"],
716 close_fds=True)
717 self.assertEqual(rc, 47)
718
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000719 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000720 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000721 newenv = os.environ.copy()
722 newenv["FRUIT"] = "physalis"
723 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000724 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000725 env=newenv)
726 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
727
728 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000729 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000730 newenv = os.environ.copy()
731 newenv["FRUIT"] = "physalis"
732 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000733 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000734 env=newenv)
735 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
736
737 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000738 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000739 rc = subprocess.call(sys.executable +
740 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000741 self.assertEqual(rc, 47)
742
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000743 def DISABLED_test_send_signal(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000744 p = subprocess.Popen([sys.executable,
745 "-c", "input()"])
746
747 self.assert_(p.poll() is None, p.poll())
748 p.send_signal(signal.SIGTERM)
749 self.assertNotEqual(p.wait(), 0)
750
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000751 def DISABLED_test_kill(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000752 p = subprocess.Popen([sys.executable,
753 "-c", "input()"])
754
755 self.assert_(p.poll() is None, p.poll())
756 p.kill()
757 self.assertNotEqual(p.wait(), 0)
758
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000759 def DISABLED_test_terminate(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000760 p = subprocess.Popen([sys.executable,
761 "-c", "input()"])
762
763 self.assert_(p.poll() is None, p.poll())
764 p.terminate()
765 self.assertNotEqual(p.wait(), 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000766
Gregory P. Smithca574992010-03-01 00:35:34 +0000767class HelperFunctionTests(unittest.TestCase):
Gregory P. Smithc234a3c2010-03-01 03:04:05 +0000768 def _test_eintr_retry_call(self):
Gregory P. Smithca574992010-03-01 00:35:34 +0000769 record_calls = []
770 def fake_os_func(*args):
771 record_calls.append(args)
772 if len(record_calls) == 2:
773 raise OSError(errno.EINTR, "fake interrupted system call")
774 return tuple(reversed(args))
775
776 self.assertEqual((999, 256),
777 subprocess._eintr_retry_call(fake_os_func, 256, 999))
778 self.assertEqual([(256, 999)], record_calls)
779 # This time there will be an EINTR so it will loop once.
780 self.assertEqual((666,),
781 subprocess._eintr_retry_call(fake_os_func, 666))
782 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
783
Gregory P. Smithc234a3c2010-03-01 03:04:05 +0000784 if not mswindows:
785 test_eintr_retry_call = _test_eintr_retry_call
786
Gregory P. Smithca574992010-03-01 00:35:34 +0000787
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000788def test_main():
Gregory P. Smithca574992010-03-01 00:35:34 +0000789 test_support.run_unittest(ProcessTestCase,
790 HelperFunctionTests)
Peter Astrand2b221ed2006-07-10 20:39:49 +0000791 if hasattr(test_support, "reap_children"):
792 test_support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000793
794if __name__ == "__main__":
795 test_main()