blob: 7b5d51926342cb43cf4093ae0634ea6fe7cc662b [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 ""')
Gregory P. Smith70eb2f92008-01-19 22:49:37 +0000452 self.assertEqual(subprocess.list2cmdline(['echo', 'foo|bar']),
453 'echo "foo|bar"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000454
455
456 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000457 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000458 "-c", "import time; time.sleep(1)"])
459 count = 0
460 while p.poll() is None:
461 time.sleep(0.1)
462 count += 1
463 # We expect that the poll loop probably went around about 10 times,
464 # but, based on system scheduling we can't control, it's possible
465 # poll() never returned None. It "should be" very rare that it
466 # didn't go around at least twice.
467 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000468 # Subsequent invocations should just return the returncode
469 self.assertEqual(p.poll(), 0)
470
471
472 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000473 p = subprocess.Popen([sys.executable,
474 "-c", "import time; time.sleep(2)"])
475 self.assertEqual(p.wait(), 0)
476 # Subsequent invocations should just return the returncode
477 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000478
Peter Astrand738131d2004-11-30 21:04:45 +0000479
480 def test_invalid_bufsize(self):
481 # an invalid type of the bufsize argument should raise
482 # TypeError.
483 try:
484 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
485 except TypeError:
486 pass
487 else:
488 self.fail("Expected TypeError")
489
Georg Brandl78162da2009-02-14 17:04:26 +0000490 def test_leaking_fds_on_error(self):
491 # see bug #5179: Popen leaks file descriptors to PIPEs if
492 # the child fails to execute; this will eventually exhaust
493 # the maximum number of open fds. 1024 seems a very common
494 # value for that limit, but Windows has 2048, so we loop
495 # 1024 times (each call leaked two fds).
496 for i in range(1024):
497 try:
498 subprocess.Popen(['nonexisting_i_hope'],
499 stdout=subprocess.PIPE,
500 stderr=subprocess.PIPE)
501 # Windows raises IOError
502 except (IOError, OSError), err:
503 if err.errno != 2: # ignore "no such file"
504 raise
505
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000506 #
507 # POSIX tests
508 #
509 if not mswindows:
510 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000511 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000512 try:
513 p = subprocess.Popen([sys.executable, "-c", ""],
514 cwd="/this/path/does/not/exist")
515 except OSError, e:
516 # The attribute child_traceback should contain "os.chdir"
517 # somewhere.
518 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
519 else:
520 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000521
Andrew M. Kuchling86e1e382006-08-01 18:16:15 +0000522 def _suppress_core_files(self):
523 """Try to prevent core files from being created.
524 Returns previous ulimit if successful, else None.
525 """
526 try:
527 import resource
528 old_limit = resource.getrlimit(resource.RLIMIT_CORE)
529 resource.setrlimit(resource.RLIMIT_CORE, (0,0))
530 return old_limit
531 except (ImportError, ValueError, resource.error):
532 return None
533
534 def _unsuppress_core_files(self, old_limit):
535 """Return core file behavior to default."""
536 if old_limit is None:
537 return
538 try:
539 import resource
540 resource.setrlimit(resource.RLIMIT_CORE, old_limit)
541 except (ImportError, ValueError, resource.error):
542 return
Tim Peters4edcba62006-08-02 03:27:46 +0000543
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000544 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000545 # returncode handles signal termination
Andrew M. Kuchling86e1e382006-08-01 18:16:15 +0000546 old_limit = self._suppress_core_files()
547 try:
548 p = subprocess.Popen([sys.executable,
549 "-c", "import os; os.abort()"])
550 finally:
551 self._unsuppress_core_files(old_limit)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000552 p.wait()
553 self.assertEqual(-p.returncode, signal.SIGABRT)
554
555 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000556 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000557 p = subprocess.Popen([sys.executable, "-c",
558 'import sys,os;' \
559 'sys.stdout.write(os.getenv("FRUIT"))'],
560 stdout=subprocess.PIPE,
561 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
562 self.assertEqual(p.stdout.read(), "apple")
563
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000564 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000565 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000566 f, fname = self.mkstemp()
567 os.write(f, "#!/bin/sh\n")
Gregory P. Smithaf8a6872008-05-17 07:17:34 +0000568 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
Tim Peters3b01a702004-10-12 22:19:32 +0000569 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000570 os.close(f)
571 os.chmod(fname, 0700)
572 p = subprocess.Popen(fname)
573 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000574 os.remove(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000575 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000576
577 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000578 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000579 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000580 [sys.executable,
581 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000582 startupinfo=47)
583 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000584 [sys.executable,
585 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000586 creationflags=47)
587
588 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000589 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000590 newenv = os.environ.copy()
591 newenv["FRUIT"] = "apple"
592 p = subprocess.Popen(["echo $FRUIT"], shell=1,
593 stdout=subprocess.PIPE,
594 env=newenv)
595 self.assertEqual(p.stdout.read().strip(), "apple")
596
597 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000598 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000599 newenv = os.environ.copy()
600 newenv["FRUIT"] = "apple"
601 p = subprocess.Popen("echo $FRUIT", shell=1,
602 stdout=subprocess.PIPE,
603 env=newenv)
604 self.assertEqual(p.stdout.read().strip(), "apple")
605
606 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000607 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000608 f, fname = self.mkstemp()
609 os.write(f, "#!/bin/sh\n")
Gregory P. Smithaf8a6872008-05-17 07:17:34 +0000610 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
Tim Peters3b01a702004-10-12 22:19:32 +0000611 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000612 os.close(f)
613 os.chmod(fname, 0700)
614 rc = subprocess.call(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000615 os.remove(fname)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000616 self.assertEqual(rc, 47)
617
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000618 def DISABLED_test_send_signal(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000619 p = subprocess.Popen([sys.executable,
620 "-c", "input()"])
621
622 self.assert_(p.poll() is None, p.poll())
623 p.send_signal(signal.SIGINT)
624 self.assertNotEqual(p.wait(), 0)
625
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000626 def DISABLED_test_kill(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000627 p = subprocess.Popen([sys.executable,
628 "-c", "input()"])
629
630 self.assert_(p.poll() is None, p.poll())
631 p.kill()
632 self.assertEqual(p.wait(), -signal.SIGKILL)
633
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000634 def DISABLED_test_terminate(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000635 p = subprocess.Popen([sys.executable,
636 "-c", "input()"])
637
638 self.assert_(p.poll() is None, p.poll())
639 p.terminate()
640 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000641
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000642 #
643 # Windows tests
644 #
645 if mswindows:
646 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000647 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000648 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000649 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000650 STARTF_USESHOWWINDOW = 1
651 SW_MAXIMIZE = 3
652 startupinfo = subprocess.STARTUPINFO()
653 startupinfo.dwFlags = STARTF_USESHOWWINDOW
654 startupinfo.wShowWindow = SW_MAXIMIZE
655 # Since Python is a console process, it won't be affected
656 # by wShowWindow, but the argument should be silently
657 # ignored
658 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
659 startupinfo=startupinfo)
660
661 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000662 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000663 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000664 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000665 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000666 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000667 creationflags=CREATE_NEW_CONSOLE)
668
669 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000670 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000671 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000672 [sys.executable,
673 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000674 preexec_fn=lambda: 1)
675 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000676 [sys.executable,
677 "-c", "import sys; sys.exit(47)"],
Peter Astrand81a191b2007-05-26 22:18:20 +0000678 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000679 close_fds=True)
680
Peter Astrand81a191b2007-05-26 22:18:20 +0000681 def test_close_fds(self):
682 # close file descriptors
683 rc = subprocess.call([sys.executable, "-c",
684 "import sys; sys.exit(47)"],
685 close_fds=True)
686 self.assertEqual(rc, 47)
687
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000688 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000689 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000690 newenv = os.environ.copy()
691 newenv["FRUIT"] = "physalis"
692 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000693 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000694 env=newenv)
695 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
696
697 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000698 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000699 newenv = os.environ.copy()
700 newenv["FRUIT"] = "physalis"
701 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000702 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000703 env=newenv)
704 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
705
706 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000707 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000708 rc = subprocess.call(sys.executable +
709 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000710 self.assertEqual(rc, 47)
711
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000712 def DISABLED_test_send_signal(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000713 p = subprocess.Popen([sys.executable,
714 "-c", "input()"])
715
716 self.assert_(p.poll() is None, p.poll())
717 p.send_signal(signal.SIGTERM)
718 self.assertNotEqual(p.wait(), 0)
719
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000720 def DISABLED_test_kill(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000721 p = subprocess.Popen([sys.executable,
722 "-c", "input()"])
723
724 self.assert_(p.poll() is None, p.poll())
725 p.kill()
726 self.assertNotEqual(p.wait(), 0)
727
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000728 def DISABLED_test_terminate(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000729 p = subprocess.Popen([sys.executable,
730 "-c", "input()"])
731
732 self.assert_(p.poll() is None, p.poll())
733 p.terminate()
734 self.assertNotEqual(p.wait(), 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000735
Gregory P. Smithca574992010-03-01 00:35:34 +0000736class HelperFunctionTests(unittest.TestCase):
Gregory P. Smithc234a3c2010-03-01 03:04:05 +0000737 def _test_eintr_retry_call(self):
Gregory P. Smithca574992010-03-01 00:35:34 +0000738 record_calls = []
739 def fake_os_func(*args):
740 record_calls.append(args)
741 if len(record_calls) == 2:
742 raise OSError(errno.EINTR, "fake interrupted system call")
743 return tuple(reversed(args))
744
745 self.assertEqual((999, 256),
746 subprocess._eintr_retry_call(fake_os_func, 256, 999))
747 self.assertEqual([(256, 999)], record_calls)
748 # This time there will be an EINTR so it will loop once.
749 self.assertEqual((666,),
750 subprocess._eintr_retry_call(fake_os_func, 666))
751 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
752
Gregory P. Smithc234a3c2010-03-01 03:04:05 +0000753 if not mswindows:
754 test_eintr_retry_call = _test_eintr_retry_call
755
Gregory P. Smithca574992010-03-01 00:35:34 +0000756
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000757def test_main():
Gregory P. Smithca574992010-03-01 00:35:34 +0000758 test_support.run_unittest(ProcessTestCase,
759 HelperFunctionTests)
Peter Astrand2b221ed2006-07-10 20:39:49 +0000760 if hasattr(test_support, "reap_children"):
761 test_support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000762
763if __name__ == "__main__":
764 test_main()