blob: e8415cdf80ac321ccb40b684da0f5b25c3306872 [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):
30 def mkstemp(self):
31 """wrapper for mkstemp, calling mktemp if mkstemp is not available"""
32 if hasattr(tempfile, "mkstemp"):
33 return tempfile.mkstemp()
34 else:
35 fname = tempfile.mktemp()
36 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
Tim Peterse718f612004-10-12 21:51:32 +000037
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000038 #
39 # Generic tests
40 #
41 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000042 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000043 rc = subprocess.call([sys.executable, "-c",
44 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000045 self.assertEqual(rc, 47)
46
Peter Astrand454f7672005-01-01 09:36:35 +000047 def test_check_call_zero(self):
48 # check_call() function with zero return code
49 rc = subprocess.check_call([sys.executable, "-c",
50 "import sys; sys.exit(0)"])
51 self.assertEqual(rc, 0)
52
53 def test_check_call_nonzero(self):
54 # check_call() function with non-zero return code
55 try:
56 subprocess.check_call([sys.executable, "-c",
57 "import sys; sys.exit(47)"])
58 except subprocess.CalledProcessError, e:
59 self.assertEqual(e.errno, 47)
60 else:
61 self.fail("Expected CalledProcessError")
62
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000063 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +000064 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000065 newenv = os.environ.copy()
66 newenv["FRUIT"] = "banana"
67 rc = subprocess.call([sys.executable, "-c",
68 'import sys, os;' \
69 'sys.exit(os.getenv("FRUIT")=="banana")'],
70 env=newenv)
71 self.assertEqual(rc, 1)
72
73 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000074 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000075 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
76 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
77 p.wait()
78 self.assertEqual(p.stdin, None)
79
80 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000081 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +000082 p = subprocess.Popen([sys.executable, "-c",
Tim Peters4052fe52004-10-13 03:29:54 +000083 'print " this bit of output is from a '
84 'test of stdout in a different '
85 'process ..."'],
86 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000087 p.wait()
88 self.assertEqual(p.stdout, None)
89
90 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000091 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000092 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
93 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
94 p.wait()
95 self.assertEqual(p.stderr, None)
96
97 def test_executable(self):
Tim Peters3b01a702004-10-12 22:19:32 +000098 p = subprocess.Popen(["somethingyoudonthave",
99 "-c", "import sys; sys.exit(47)"],
100 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000101 p.wait()
102 self.assertEqual(p.returncode, 47)
103
104 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000105 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000106 p = subprocess.Popen([sys.executable, "-c",
107 'import sys; sys.exit(sys.stdin.read() == "pear")'],
108 stdin=subprocess.PIPE)
109 p.stdin.write("pear")
110 p.stdin.close()
111 p.wait()
112 self.assertEqual(p.returncode, 1)
113
114 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000115 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000116 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000117 d = tf.fileno()
118 os.write(d, "pear")
119 os.lseek(d, 0, 0)
120 p = subprocess.Popen([sys.executable, "-c",
121 'import sys; sys.exit(sys.stdin.read() == "pear")'],
122 stdin=d)
123 p.wait()
124 self.assertEqual(p.returncode, 1)
125
126 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000127 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000128 tf = tempfile.TemporaryFile()
129 tf.write("pear")
130 tf.seek(0)
131 p = subprocess.Popen([sys.executable, "-c",
132 'import sys; sys.exit(sys.stdin.read() == "pear")'],
133 stdin=tf)
134 p.wait()
135 self.assertEqual(p.returncode, 1)
136
137 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000138 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000139 p = subprocess.Popen([sys.executable, "-c",
140 'import sys; sys.stdout.write("orange")'],
141 stdout=subprocess.PIPE)
142 self.assertEqual(p.stdout.read(), "orange")
143
144 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000145 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000146 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000147 d = tf.fileno()
148 p = subprocess.Popen([sys.executable, "-c",
149 'import sys; sys.stdout.write("orange")'],
150 stdout=d)
151 p.wait()
152 os.lseek(d, 0, 0)
153 self.assertEqual(os.read(d, 1024), "orange")
154
155 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000156 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000157 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000158 p = subprocess.Popen([sys.executable, "-c",
159 'import sys; sys.stdout.write("orange")'],
160 stdout=tf)
161 p.wait()
162 tf.seek(0)
163 self.assertEqual(tf.read(), "orange")
164
165 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000166 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000167 p = subprocess.Popen([sys.executable, "-c",
168 'import sys; sys.stderr.write("strawberry")'],
169 stderr=subprocess.PIPE)
Tim Peters3761e8d2004-10-13 04:07:12 +0000170 self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()),
171 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000172
173 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000174 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000175 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000176 d = tf.fileno()
177 p = subprocess.Popen([sys.executable, "-c",
178 'import sys; sys.stderr.write("strawberry")'],
179 stderr=d)
180 p.wait()
181 os.lseek(d, 0, 0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000182 self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)),
183 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000184
185 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000186 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000187 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000188 p = subprocess.Popen([sys.executable, "-c",
189 'import sys; sys.stderr.write("strawberry")'],
190 stderr=tf)
191 p.wait()
192 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000193 self.assertEqual(remove_stderr_debug_decorations(tf.read()),
194 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000195
196 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000197 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000198 p = subprocess.Popen([sys.executable, "-c",
199 'import sys;' \
200 'sys.stdout.write("apple");' \
201 'sys.stdout.flush();' \
202 'sys.stderr.write("orange")'],
203 stdout=subprocess.PIPE,
204 stderr=subprocess.STDOUT)
Tim Peters3761e8d2004-10-13 04:07:12 +0000205 output = p.stdout.read()
206 stripped = remove_stderr_debug_decorations(output)
207 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000208
209 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000210 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000211 tf = tempfile.TemporaryFile()
212 p = subprocess.Popen([sys.executable, "-c",
213 'import sys;' \
214 'sys.stdout.write("apple");' \
215 'sys.stdout.flush();' \
216 'sys.stderr.write("orange")'],
217 stdout=tf,
218 stderr=tf)
219 p.wait()
220 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000221 output = tf.read()
222 stripped = remove_stderr_debug_decorations(output)
223 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000224
225 def test_cwd(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000226 tmpdir = os.getenv("TEMP", "/tmp")
Peter Astrand195404f2004-11-12 15:51:48 +0000227 # We cannot use os.path.realpath to canonicalize the path,
228 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
229 cwd = os.getcwd()
230 os.chdir(tmpdir)
231 tmpdir = os.getcwd()
232 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000233 p = subprocess.Popen([sys.executable, "-c",
234 'import sys,os;' \
235 'sys.stdout.write(os.getcwd())'],
236 stdout=subprocess.PIPE,
237 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000238 normcase = os.path.normcase
239 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000240
241 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000242 newenv = os.environ.copy()
243 newenv["FRUIT"] = "orange"
244 p = subprocess.Popen([sys.executable, "-c",
245 'import sys,os;' \
246 'sys.stdout.write(os.getenv("FRUIT"))'],
247 stdout=subprocess.PIPE,
248 env=newenv)
249 self.assertEqual(p.stdout.read(), "orange")
250
Peter Astrandcbac93c2005-03-03 20:24:28 +0000251 def test_communicate_stdin(self):
252 p = subprocess.Popen([sys.executable, "-c",
253 'import sys; sys.exit(sys.stdin.read() == "pear")'],
254 stdin=subprocess.PIPE)
255 p.communicate("pear")
256 self.assertEqual(p.returncode, 1)
257
258 def test_communicate_stdout(self):
259 p = subprocess.Popen([sys.executable, "-c",
260 'import sys; sys.stdout.write("pineapple")'],
261 stdout=subprocess.PIPE)
262 (stdout, stderr) = p.communicate()
263 self.assertEqual(stdout, "pineapple")
264 self.assertEqual(stderr, None)
265
266 def test_communicate_stderr(self):
267 p = subprocess.Popen([sys.executable, "-c",
268 'import sys; sys.stderr.write("pineapple")'],
269 stderr=subprocess.PIPE)
270 (stdout, stderr) = p.communicate()
271 self.assertEqual(stdout, None)
272 self.assertEqual(stderr, "pineapple")
273
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000274 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000275 p = subprocess.Popen([sys.executable, "-c",
276 'import sys,os;' \
277 'sys.stderr.write("pineapple");' \
278 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000279 stdin=subprocess.PIPE,
280 stdout=subprocess.PIPE,
281 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000282 (stdout, stderr) = p.communicate("banana")
283 self.assertEqual(stdout, "banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000284 self.assertEqual(remove_stderr_debug_decorations(stderr),
285 "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000286
287 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000288 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000289 p = subprocess.Popen([sys.executable, "-c",
290 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000291 (stdout, stderr) = p.communicate()
292 self.assertEqual(stdout, None)
293 self.assertEqual(stderr, None)
294
295 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000296 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000297 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000298 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000299 x, y = os.pipe()
300 if mswindows:
301 pipe_buf = 512
302 else:
303 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
304 os.close(x)
305 os.close(y)
306 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000307 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000308 'sys.stdout.write(sys.stdin.read(47));' \
309 'sys.stderr.write("xyz"*%d);' \
310 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000311 stdin=subprocess.PIPE,
312 stdout=subprocess.PIPE,
313 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000314 string_to_write = "abc"*pipe_buf
315 (stdout, stderr) = p.communicate(string_to_write)
316 self.assertEqual(stdout, string_to_write)
317
318 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000319 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000320 p = subprocess.Popen([sys.executable, "-c",
321 'import sys,os;' \
322 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000323 stdin=subprocess.PIPE,
324 stdout=subprocess.PIPE,
325 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000326 p.stdin.write("banana")
327 (stdout, stderr) = p.communicate("split")
328 self.assertEqual(stdout, "bananasplit")
Tim Peters3761e8d2004-10-13 04:07:12 +0000329 self.assertEqual(remove_stderr_debug_decorations(stderr), "")
Tim Peterse718f612004-10-12 21:51:32 +0000330
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000331 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000332 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000333 'import sys,os;' + SETBINARY +
334 'sys.stdout.write("line1\\n");'
335 'sys.stdout.flush();'
336 'sys.stdout.write("line2\\r");'
337 'sys.stdout.flush();'
338 'sys.stdout.write("line3\\r\\n");'
339 'sys.stdout.flush();'
340 'sys.stdout.write("line4\\r");'
341 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000342 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000343 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000344 'sys.stdout.write("\\nline6");'],
345 stdout=subprocess.PIPE,
346 universal_newlines=1)
347 stdout = p.stdout.read()
348 if hasattr(open, 'newlines'):
349 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000350 self.assertEqual(stdout,
351 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000352 else:
353 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000354 self.assertEqual(stdout,
355 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000356
357 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000358 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000359 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000360 'import sys,os;' + SETBINARY +
361 'sys.stdout.write("line1\\n");'
362 'sys.stdout.flush();'
363 'sys.stdout.write("line2\\r");'
364 'sys.stdout.flush();'
365 'sys.stdout.write("line3\\r\\n");'
366 'sys.stdout.flush();'
367 'sys.stdout.write("line4\\r");'
368 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000369 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000370 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000371 'sys.stdout.write("\\nline6");'],
372 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
373 universal_newlines=1)
374 (stdout, stderr) = p.communicate()
375 if hasattr(open, 'newlines'):
376 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000377 self.assertEqual(stdout,
378 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000379 else:
380 # Interpreter without universal newline support
381 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
382
383 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000384 # Make sure we leak no resources
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000385 max_handles = 1026 # too much for most UNIX systems
386 if mswindows:
387 max_handles = 65 # a full test is too slow on Windows
388 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000389 p = subprocess.Popen([sys.executable, "-c",
390 "import sys;sys.stdout.write(sys.stdin.read())"],
391 stdin=subprocess.PIPE,
392 stdout=subprocess.PIPE,
393 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000394 data = p.communicate("lime")[0]
395 self.assertEqual(data, "lime")
396
397
398 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000399 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
400 '"a b c" d e')
401 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
402 'ab\\"c \\ d')
403 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
404 'a\\\\\\b "de fg" h')
405 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
406 'a\\\\\\"b c d')
407 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
408 '"a\\\\b c" d e')
409 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
410 '"a\\\\b\\ c" d e')
411
412
413 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000414 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000415 "-c", "import time; time.sleep(1)"])
416 count = 0
417 while p.poll() is None:
418 time.sleep(0.1)
419 count += 1
420 # We expect that the poll loop probably went around about 10 times,
421 # but, based on system scheduling we can't control, it's possible
422 # poll() never returned None. It "should be" very rare that it
423 # didn't go around at least twice.
424 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000425 # Subsequent invocations should just return the returncode
426 self.assertEqual(p.poll(), 0)
427
428
429 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000430 p = subprocess.Popen([sys.executable,
431 "-c", "import time; time.sleep(2)"])
432 self.assertEqual(p.wait(), 0)
433 # Subsequent invocations should just return the returncode
434 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000435
Peter Astrand738131d2004-11-30 21:04:45 +0000436
437 def test_invalid_bufsize(self):
438 # an invalid type of the bufsize argument should raise
439 # TypeError.
440 try:
441 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
442 except TypeError:
443 pass
444 else:
445 self.fail("Expected TypeError")
446
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000447 #
448 # POSIX tests
449 #
450 if not mswindows:
451 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000452 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000453 try:
454 p = subprocess.Popen([sys.executable, "-c", ""],
455 cwd="/this/path/does/not/exist")
456 except OSError, e:
457 # The attribute child_traceback should contain "os.chdir"
458 # somewhere.
459 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
460 else:
461 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000462
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000463 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000464 # returncode handles signal termination
Tim Peters3b01a702004-10-12 22:19:32 +0000465 p = subprocess.Popen([sys.executable,
466 "-c", "import os; os.abort()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000467 p.wait()
468 self.assertEqual(-p.returncode, signal.SIGABRT)
469
470 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000471 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000472 p = subprocess.Popen([sys.executable, "-c",
473 'import sys,os;' \
474 'sys.stdout.write(os.getenv("FRUIT"))'],
475 stdout=subprocess.PIPE,
476 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
477 self.assertEqual(p.stdout.read(), "apple")
478
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000479 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000480 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000481 f, fname = self.mkstemp()
482 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000483 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
484 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000485 os.close(f)
486 os.chmod(fname, 0700)
487 p = subprocess.Popen(fname)
488 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000489 os.remove(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000490 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000491
492 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000493 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000494 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000495 [sys.executable,
496 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000497 startupinfo=47)
498 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000499 [sys.executable,
500 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000501 creationflags=47)
502
503 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000504 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000505 newenv = os.environ.copy()
506 newenv["FRUIT"] = "apple"
507 p = subprocess.Popen(["echo $FRUIT"], shell=1,
508 stdout=subprocess.PIPE,
509 env=newenv)
510 self.assertEqual(p.stdout.read().strip(), "apple")
511
512 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000513 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000514 newenv = os.environ.copy()
515 newenv["FRUIT"] = "apple"
516 p = subprocess.Popen("echo $FRUIT", shell=1,
517 stdout=subprocess.PIPE,
518 env=newenv)
519 self.assertEqual(p.stdout.read().strip(), "apple")
520
521 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000522 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000523 f, fname = self.mkstemp()
524 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000525 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
526 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000527 os.close(f)
528 os.chmod(fname, 0700)
529 rc = subprocess.call(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000530 os.remove(fname)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000531 self.assertEqual(rc, 47)
532
Tim Peterse718f612004-10-12 21:51:32 +0000533
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000534 #
535 # Windows tests
536 #
537 if mswindows:
538 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000539 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000540 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000541 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000542 STARTF_USESHOWWINDOW = 1
543 SW_MAXIMIZE = 3
544 startupinfo = subprocess.STARTUPINFO()
545 startupinfo.dwFlags = STARTF_USESHOWWINDOW
546 startupinfo.wShowWindow = SW_MAXIMIZE
547 # Since Python is a console process, it won't be affected
548 # by wShowWindow, but the argument should be silently
549 # ignored
550 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
551 startupinfo=startupinfo)
552
553 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000554 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000555 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000556 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000557 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000558 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000559 creationflags=CREATE_NEW_CONSOLE)
560
561 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000562 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000563 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000564 [sys.executable,
565 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000566 preexec_fn=lambda: 1)
567 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000568 [sys.executable,
569 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000570 close_fds=True)
571
572 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000573 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000574 newenv = os.environ.copy()
575 newenv["FRUIT"] = "physalis"
576 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000577 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000578 env=newenv)
579 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
580
581 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000582 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000583 newenv = os.environ.copy()
584 newenv["FRUIT"] = "physalis"
585 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000586 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000587 env=newenv)
588 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
589
590 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000591 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000592 rc = subprocess.call(sys.executable +
593 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000594 self.assertEqual(rc, 47)
595
596
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000597def test_main():
598 test_support.run_unittest(ProcessTestCase)
599
600if __name__ == "__main__":
601 test_main()