blob: e7ba26fae1c3c2ba5257f1ce806fb4a6ab517730 [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
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000075 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +000076 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000077 newenv = os.environ.copy()
78 newenv["FRUIT"] = "banana"
79 rc = subprocess.call([sys.executable, "-c",
80 'import sys, os;' \
81 'sys.exit(os.getenv("FRUIT")=="banana")'],
82 env=newenv)
83 self.assertEqual(rc, 1)
84
85 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000086 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000087 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
88 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
89 p.wait()
90 self.assertEqual(p.stdin, None)
91
92 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000093 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +000094 p = subprocess.Popen([sys.executable, "-c",
Tim Peters4052fe52004-10-13 03:29:54 +000095 'print " this bit of output is from a '
96 'test of stdout in a different '
97 'process ..."'],
98 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000099 p.wait()
100 self.assertEqual(p.stdout, None)
101
102 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000103 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000104 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
105 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
106 p.wait()
107 self.assertEqual(p.stderr, None)
108
109 def test_executable(self):
Tim Peters3b01a702004-10-12 22:19:32 +0000110 p = subprocess.Popen(["somethingyoudonthave",
111 "-c", "import sys; sys.exit(47)"],
112 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000113 p.wait()
114 self.assertEqual(p.returncode, 47)
115
116 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000117 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000118 p = subprocess.Popen([sys.executable, "-c",
119 'import sys; sys.exit(sys.stdin.read() == "pear")'],
120 stdin=subprocess.PIPE)
121 p.stdin.write("pear")
122 p.stdin.close()
123 p.wait()
124 self.assertEqual(p.returncode, 1)
125
126 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000127 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000128 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000129 d = tf.fileno()
130 os.write(d, "pear")
131 os.lseek(d, 0, 0)
132 p = subprocess.Popen([sys.executable, "-c",
133 'import sys; sys.exit(sys.stdin.read() == "pear")'],
134 stdin=d)
135 p.wait()
136 self.assertEqual(p.returncode, 1)
137
138 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000139 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000140 tf = tempfile.TemporaryFile()
141 tf.write("pear")
142 tf.seek(0)
143 p = subprocess.Popen([sys.executable, "-c",
144 'import sys; sys.exit(sys.stdin.read() == "pear")'],
145 stdin=tf)
146 p.wait()
147 self.assertEqual(p.returncode, 1)
148
149 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000150 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000151 p = subprocess.Popen([sys.executable, "-c",
152 'import sys; sys.stdout.write("orange")'],
153 stdout=subprocess.PIPE)
154 self.assertEqual(p.stdout.read(), "orange")
155
156 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000157 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000158 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000159 d = tf.fileno()
160 p = subprocess.Popen([sys.executable, "-c",
161 'import sys; sys.stdout.write("orange")'],
162 stdout=d)
163 p.wait()
164 os.lseek(d, 0, 0)
165 self.assertEqual(os.read(d, 1024), "orange")
166
167 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000168 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000169 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000170 p = subprocess.Popen([sys.executable, "-c",
171 'import sys; sys.stdout.write("orange")'],
172 stdout=tf)
173 p.wait()
174 tf.seek(0)
175 self.assertEqual(tf.read(), "orange")
176
177 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000178 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000179 p = subprocess.Popen([sys.executable, "-c",
180 'import sys; sys.stderr.write("strawberry")'],
181 stderr=subprocess.PIPE)
Tim Peters3761e8d2004-10-13 04:07:12 +0000182 self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()),
183 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000184
185 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000186 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000187 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000188 d = tf.fileno()
189 p = subprocess.Popen([sys.executable, "-c",
190 'import sys; sys.stderr.write("strawberry")'],
191 stderr=d)
192 p.wait()
193 os.lseek(d, 0, 0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000194 self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)),
195 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000196
197 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000198 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000199 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000200 p = subprocess.Popen([sys.executable, "-c",
201 'import sys; sys.stderr.write("strawberry")'],
202 stderr=tf)
203 p.wait()
204 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000205 self.assertEqual(remove_stderr_debug_decorations(tf.read()),
206 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000207
208 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000209 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000210 p = subprocess.Popen([sys.executable, "-c",
211 'import sys;' \
212 'sys.stdout.write("apple");' \
213 'sys.stdout.flush();' \
214 'sys.stderr.write("orange")'],
215 stdout=subprocess.PIPE,
216 stderr=subprocess.STDOUT)
Tim Peters3761e8d2004-10-13 04:07:12 +0000217 output = p.stdout.read()
218 stripped = remove_stderr_debug_decorations(output)
219 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000220
221 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000222 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000223 tf = tempfile.TemporaryFile()
224 p = subprocess.Popen([sys.executable, "-c",
225 'import sys;' \
226 'sys.stdout.write("apple");' \
227 'sys.stdout.flush();' \
228 'sys.stderr.write("orange")'],
229 stdout=tf,
230 stderr=tf)
231 p.wait()
232 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000233 output = tf.read()
234 stripped = remove_stderr_debug_decorations(output)
235 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000236
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000237 def test_stdout_filedes_of_stdout(self):
238 # stdout is set to 1 (#1531862).
239 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))"
240 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
241 self.assertEquals(rc, 2)
242
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000243 def test_cwd(self):
Guido van Rossume9a0e882007-12-20 17:28:10 +0000244 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000245 # We cannot use os.path.realpath to canonicalize the path,
246 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
247 cwd = os.getcwd()
248 os.chdir(tmpdir)
249 tmpdir = os.getcwd()
250 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000251 p = subprocess.Popen([sys.executable, "-c",
252 'import sys,os;' \
253 'sys.stdout.write(os.getcwd())'],
254 stdout=subprocess.PIPE,
255 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000256 normcase = os.path.normcase
257 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000258
259 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000260 newenv = os.environ.copy()
261 newenv["FRUIT"] = "orange"
262 p = subprocess.Popen([sys.executable, "-c",
263 'import sys,os;' \
264 'sys.stdout.write(os.getenv("FRUIT"))'],
265 stdout=subprocess.PIPE,
266 env=newenv)
267 self.assertEqual(p.stdout.read(), "orange")
268
Peter Astrandcbac93c2005-03-03 20:24:28 +0000269 def test_communicate_stdin(self):
270 p = subprocess.Popen([sys.executable, "-c",
271 'import sys; sys.exit(sys.stdin.read() == "pear")'],
272 stdin=subprocess.PIPE)
273 p.communicate("pear")
274 self.assertEqual(p.returncode, 1)
275
276 def test_communicate_stdout(self):
277 p = subprocess.Popen([sys.executable, "-c",
278 'import sys; sys.stdout.write("pineapple")'],
279 stdout=subprocess.PIPE)
280 (stdout, stderr) = p.communicate()
281 self.assertEqual(stdout, "pineapple")
282 self.assertEqual(stderr, None)
283
284 def test_communicate_stderr(self):
285 p = subprocess.Popen([sys.executable, "-c",
286 'import sys; sys.stderr.write("pineapple")'],
287 stderr=subprocess.PIPE)
288 (stdout, stderr) = p.communicate()
289 self.assertEqual(stdout, None)
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000290 self.assertEqual(remove_stderr_debug_decorations(stderr), "pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000291
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000292 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000293 p = subprocess.Popen([sys.executable, "-c",
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000294 'import sys,os;'
295 'sys.stderr.write("pineapple");'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000296 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000297 stdin=subprocess.PIPE,
298 stdout=subprocess.PIPE,
299 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000300 (stdout, stderr) = p.communicate("banana")
301 self.assertEqual(stdout, "banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000302 self.assertEqual(remove_stderr_debug_decorations(stderr),
303 "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000304
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000305 # This test is Linux specific for simplicity to at least have
306 # some coverage. It is not a platform specific bug.
307 if os.path.isdir('/proc/%d/fd' % os.getpid()):
308 # Test for the fd leak reported in http://bugs.python.org/issue2791.
309 def test_communicate_pipe_fd_leak(self):
310 fd_directory = '/proc/%d/fd' % os.getpid()
311 num_fds_before_popen = len(os.listdir(fd_directory))
312 p = subprocess.Popen([sys.executable, '-c', 'print()'],
313 stdout=subprocess.PIPE)
314 p.communicate()
315 num_fds_after_communicate = len(os.listdir(fd_directory))
316 del p
317 num_fds_after_destruction = len(os.listdir(fd_directory))
318 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
319 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
320
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000321 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000322 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000323 p = subprocess.Popen([sys.executable, "-c",
324 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000325 (stdout, stderr) = p.communicate()
326 self.assertEqual(stdout, None)
327 self.assertEqual(stderr, None)
328
329 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000330 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000331 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000332 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000333 x, y = os.pipe()
334 if mswindows:
335 pipe_buf = 512
336 else:
337 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
338 os.close(x)
339 os.close(y)
340 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000341 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000342 'sys.stdout.write(sys.stdin.read(47));' \
343 'sys.stderr.write("xyz"*%d);' \
344 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000345 stdin=subprocess.PIPE,
346 stdout=subprocess.PIPE,
347 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000348 string_to_write = "abc"*pipe_buf
349 (stdout, stderr) = p.communicate(string_to_write)
350 self.assertEqual(stdout, string_to_write)
351
352 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000353 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000354 p = subprocess.Popen([sys.executable, "-c",
355 'import sys,os;' \
356 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000357 stdin=subprocess.PIPE,
358 stdout=subprocess.PIPE,
359 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000360 p.stdin.write("banana")
361 (stdout, stderr) = p.communicate("split")
362 self.assertEqual(stdout, "bananasplit")
Tim Peters3761e8d2004-10-13 04:07:12 +0000363 self.assertEqual(remove_stderr_debug_decorations(stderr), "")
Tim Peterse718f612004-10-12 21:51:32 +0000364
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000365 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000366 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000367 'import sys,os;' + SETBINARY +
368 'sys.stdout.write("line1\\n");'
369 'sys.stdout.flush();'
370 'sys.stdout.write("line2\\r");'
371 'sys.stdout.flush();'
372 'sys.stdout.write("line3\\r\\n");'
373 'sys.stdout.flush();'
374 'sys.stdout.write("line4\\r");'
375 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000376 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000377 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000378 'sys.stdout.write("\\nline6");'],
379 stdout=subprocess.PIPE,
380 universal_newlines=1)
381 stdout = p.stdout.read()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000382 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000383 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000384 self.assertEqual(stdout,
385 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000386 else:
387 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000388 self.assertEqual(stdout,
389 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000390
391 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000392 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000393 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000394 'import sys,os;' + SETBINARY +
395 'sys.stdout.write("line1\\n");'
396 'sys.stdout.flush();'
397 'sys.stdout.write("line2\\r");'
398 'sys.stdout.flush();'
399 'sys.stdout.write("line3\\r\\n");'
400 'sys.stdout.flush();'
401 'sys.stdout.write("line4\\r");'
402 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000403 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000404 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000405 'sys.stdout.write("\\nline6");'],
406 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
407 universal_newlines=1)
408 (stdout, stderr) = p.communicate()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000409 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000410 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000411 self.assertEqual(stdout,
412 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000413 else:
414 # Interpreter without universal newline support
415 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
416
417 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000418 # Make sure we leak no resources
Peter Astrandd6b24302006-06-22 20:06:46 +0000419 if not hasattr(test_support, "is_resource_enabled") \
420 or test_support.is_resource_enabled("subprocess") and not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000421 max_handles = 1026 # too much for most UNIX systems
422 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000423 max_handles = 65
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000424 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000425 p = subprocess.Popen([sys.executable, "-c",
426 "import sys;sys.stdout.write(sys.stdin.read())"],
427 stdin=subprocess.PIPE,
428 stdout=subprocess.PIPE,
429 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000430 data = p.communicate("lime")[0]
431 self.assertEqual(data, "lime")
432
433
434 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000435 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
436 '"a b c" d e')
437 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
438 'ab\\"c \\ d')
Gregory P. Smithe047e6d2008-01-19 20:49:02 +0000439 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
440 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000441 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
442 'a\\\\\\b "de fg" h')
443 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
444 'a\\\\\\"b c d')
445 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
446 '"a\\\\b c" d e')
447 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
448 '"a\\\\b\\ c" d e')
Peter Astrand10514a72007-01-13 22:35:35 +0000449 self.assertEqual(subprocess.list2cmdline(['ab', '']),
450 'ab ""')
Gregory P. Smith70eb2f92008-01-19 22:49:37 +0000451 self.assertEqual(subprocess.list2cmdline(['echo', 'foo|bar']),
452 'echo "foo|bar"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000453
454
455 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000456 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000457 "-c", "import time; time.sleep(1)"])
458 count = 0
459 while p.poll() is None:
460 time.sleep(0.1)
461 count += 1
462 # We expect that the poll loop probably went around about 10 times,
463 # but, based on system scheduling we can't control, it's possible
464 # poll() never returned None. It "should be" very rare that it
465 # didn't go around at least twice.
466 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000467 # Subsequent invocations should just return the returncode
468 self.assertEqual(p.poll(), 0)
469
470
471 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000472 p = subprocess.Popen([sys.executable,
473 "-c", "import time; time.sleep(2)"])
474 self.assertEqual(p.wait(), 0)
475 # Subsequent invocations should just return the returncode
476 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000477
Peter Astrand738131d2004-11-30 21:04:45 +0000478
479 def test_invalid_bufsize(self):
480 # an invalid type of the bufsize argument should raise
481 # TypeError.
482 try:
483 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
484 except TypeError:
485 pass
486 else:
487 self.fail("Expected TypeError")
488
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000489 #
490 # POSIX tests
491 #
492 if not mswindows:
493 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000494 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000495 try:
496 p = subprocess.Popen([sys.executable, "-c", ""],
497 cwd="/this/path/does/not/exist")
498 except OSError, e:
499 # The attribute child_traceback should contain "os.chdir"
500 # somewhere.
501 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
502 else:
503 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000504
Andrew M. Kuchling86e1e382006-08-01 18:16:15 +0000505 def _suppress_core_files(self):
506 """Try to prevent core files from being created.
507 Returns previous ulimit if successful, else None.
508 """
509 try:
510 import resource
511 old_limit = resource.getrlimit(resource.RLIMIT_CORE)
512 resource.setrlimit(resource.RLIMIT_CORE, (0,0))
513 return old_limit
514 except (ImportError, ValueError, resource.error):
515 return None
516
517 def _unsuppress_core_files(self, old_limit):
518 """Return core file behavior to default."""
519 if old_limit is None:
520 return
521 try:
522 import resource
523 resource.setrlimit(resource.RLIMIT_CORE, old_limit)
524 except (ImportError, ValueError, resource.error):
525 return
Tim Peters4edcba62006-08-02 03:27:46 +0000526
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000527 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000528 # returncode handles signal termination
Andrew M. Kuchling86e1e382006-08-01 18:16:15 +0000529 old_limit = self._suppress_core_files()
530 try:
531 p = subprocess.Popen([sys.executable,
532 "-c", "import os; os.abort()"])
533 finally:
534 self._unsuppress_core_files(old_limit)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000535 p.wait()
536 self.assertEqual(-p.returncode, signal.SIGABRT)
537
538 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000539 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000540 p = subprocess.Popen([sys.executable, "-c",
541 'import sys,os;' \
542 'sys.stdout.write(os.getenv("FRUIT"))'],
543 stdout=subprocess.PIPE,
544 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
545 self.assertEqual(p.stdout.read(), "apple")
546
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000547 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000548 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000549 f, fname = self.mkstemp()
550 os.write(f, "#!/bin/sh\n")
Gregory P. Smithaf8a6872008-05-17 07:17:34 +0000551 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
Tim Peters3b01a702004-10-12 22:19:32 +0000552 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000553 os.close(f)
554 os.chmod(fname, 0700)
555 p = subprocess.Popen(fname)
556 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000557 os.remove(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000558 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000559
560 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000561 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000562 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000563 [sys.executable,
564 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000565 startupinfo=47)
566 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000567 [sys.executable,
568 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000569 creationflags=47)
570
571 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000572 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000573 newenv = os.environ.copy()
574 newenv["FRUIT"] = "apple"
575 p = subprocess.Popen(["echo $FRUIT"], shell=1,
576 stdout=subprocess.PIPE,
577 env=newenv)
578 self.assertEqual(p.stdout.read().strip(), "apple")
579
580 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000581 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000582 newenv = os.environ.copy()
583 newenv["FRUIT"] = "apple"
584 p = subprocess.Popen("echo $FRUIT", shell=1,
585 stdout=subprocess.PIPE,
586 env=newenv)
587 self.assertEqual(p.stdout.read().strip(), "apple")
588
589 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000590 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000591 f, fname = self.mkstemp()
592 os.write(f, "#!/bin/sh\n")
Gregory P. Smithaf8a6872008-05-17 07:17:34 +0000593 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
Tim Peters3b01a702004-10-12 22:19:32 +0000594 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000595 os.close(f)
596 os.chmod(fname, 0700)
597 rc = subprocess.call(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000598 os.remove(fname)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000599 self.assertEqual(rc, 47)
600
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000601 def DISABLED_test_send_signal(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000602 p = subprocess.Popen([sys.executable,
603 "-c", "input()"])
604
605 self.assert_(p.poll() is None, p.poll())
606 p.send_signal(signal.SIGINT)
607 self.assertNotEqual(p.wait(), 0)
608
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000609 def DISABLED_test_kill(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000610 p = subprocess.Popen([sys.executable,
611 "-c", "input()"])
612
613 self.assert_(p.poll() is None, p.poll())
614 p.kill()
615 self.assertEqual(p.wait(), -signal.SIGKILL)
616
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000617 def DISABLED_test_terminate(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000618 p = subprocess.Popen([sys.executable,
619 "-c", "input()"])
620
621 self.assert_(p.poll() is None, p.poll())
622 p.terminate()
623 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000624
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000625 #
626 # Windows tests
627 #
628 if mswindows:
629 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000630 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000631 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000632 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000633 STARTF_USESHOWWINDOW = 1
634 SW_MAXIMIZE = 3
635 startupinfo = subprocess.STARTUPINFO()
636 startupinfo.dwFlags = STARTF_USESHOWWINDOW
637 startupinfo.wShowWindow = SW_MAXIMIZE
638 # Since Python is a console process, it won't be affected
639 # by wShowWindow, but the argument should be silently
640 # ignored
641 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
642 startupinfo=startupinfo)
643
644 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000645 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000646 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000647 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000648 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000649 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000650 creationflags=CREATE_NEW_CONSOLE)
651
652 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000653 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000654 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000655 [sys.executable,
656 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000657 preexec_fn=lambda: 1)
658 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000659 [sys.executable,
660 "-c", "import sys; sys.exit(47)"],
Peter Astrand81a191b2007-05-26 22:18:20 +0000661 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000662 close_fds=True)
663
Peter Astrand81a191b2007-05-26 22:18:20 +0000664 def test_close_fds(self):
665 # close file descriptors
666 rc = subprocess.call([sys.executable, "-c",
667 "import sys; sys.exit(47)"],
668 close_fds=True)
669 self.assertEqual(rc, 47)
670
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000671 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000672 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000673 newenv = os.environ.copy()
674 newenv["FRUIT"] = "physalis"
675 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000676 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000677 env=newenv)
678 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
679
680 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000681 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000682 newenv = os.environ.copy()
683 newenv["FRUIT"] = "physalis"
684 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000685 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000686 env=newenv)
687 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
688
689 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000690 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000691 rc = subprocess.call(sys.executable +
692 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000693 self.assertEqual(rc, 47)
694
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000695 def DISABLED_test_send_signal(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000696 p = subprocess.Popen([sys.executable,
697 "-c", "input()"])
698
699 self.assert_(p.poll() is None, p.poll())
700 p.send_signal(signal.SIGTERM)
701 self.assertNotEqual(p.wait(), 0)
702
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000703 def DISABLED_test_kill(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000704 p = subprocess.Popen([sys.executable,
705 "-c", "input()"])
706
707 self.assert_(p.poll() is None, p.poll())
708 p.kill()
709 self.assertNotEqual(p.wait(), 0)
710
Christian Heimesc2ca6db2008-05-06 23:42:58 +0000711 def DISABLED_test_terminate(self):
Christian Heimese74c8f22008-04-19 02:23:57 +0000712 p = subprocess.Popen([sys.executable,
713 "-c", "input()"])
714
715 self.assert_(p.poll() is None, p.poll())
716 p.terminate()
717 self.assertNotEqual(p.wait(), 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000718
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000719def test_main():
720 test_support.run_unittest(ProcessTestCase)
Peter Astrand2b221ed2006-07-10 20:39:49 +0000721 if hasattr(test_support, "reap_children"):
722 test_support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000723
724if __name__ == "__main__":
725 test_main()