blob: 52f4d4711d25ac063d539197b663f37f0d3cf75d [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
251 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000252 p = subprocess.Popen([sys.executable, "-c",
253 'import sys,os;' \
254 'sys.stderr.write("pineapple");' \
255 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000256 stdin=subprocess.PIPE,
257 stdout=subprocess.PIPE,
258 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000259 (stdout, stderr) = p.communicate("banana")
260 self.assertEqual(stdout, "banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000261 self.assertEqual(remove_stderr_debug_decorations(stderr),
262 "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000263
264 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000265 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000266 p = subprocess.Popen([sys.executable, "-c",
267 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000268 (stdout, stderr) = p.communicate()
269 self.assertEqual(stdout, None)
270 self.assertEqual(stderr, None)
271
272 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000273 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000274 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000275 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000276 x, y = os.pipe()
277 if mswindows:
278 pipe_buf = 512
279 else:
280 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
281 os.close(x)
282 os.close(y)
283 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000284 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000285 'sys.stdout.write(sys.stdin.read(47));' \
286 'sys.stderr.write("xyz"*%d);' \
287 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000288 stdin=subprocess.PIPE,
289 stdout=subprocess.PIPE,
290 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000291 string_to_write = "abc"*pipe_buf
292 (stdout, stderr) = p.communicate(string_to_write)
293 self.assertEqual(stdout, string_to_write)
294
295 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000296 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000297 p = subprocess.Popen([sys.executable, "-c",
298 'import sys,os;' \
299 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000300 stdin=subprocess.PIPE,
301 stdout=subprocess.PIPE,
302 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000303 p.stdin.write("banana")
304 (stdout, stderr) = p.communicate("split")
305 self.assertEqual(stdout, "bananasplit")
Tim Peters3761e8d2004-10-13 04:07:12 +0000306 self.assertEqual(remove_stderr_debug_decorations(stderr), "")
Tim Peterse718f612004-10-12 21:51:32 +0000307
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000308 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000309 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000310 'import sys,os;' + SETBINARY +
311 'sys.stdout.write("line1\\n");'
312 'sys.stdout.flush();'
313 'sys.stdout.write("line2\\r");'
314 'sys.stdout.flush();'
315 'sys.stdout.write("line3\\r\\n");'
316 'sys.stdout.flush();'
317 'sys.stdout.write("line4\\r");'
318 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000319 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000320 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000321 'sys.stdout.write("\\nline6");'],
322 stdout=subprocess.PIPE,
323 universal_newlines=1)
324 stdout = p.stdout.read()
325 if hasattr(open, 'newlines'):
326 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000327 self.assertEqual(stdout,
328 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000329 else:
330 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000331 self.assertEqual(stdout,
332 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000333
334 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000335 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000336 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000337 'import sys,os;' + SETBINARY +
338 'sys.stdout.write("line1\\n");'
339 'sys.stdout.flush();'
340 'sys.stdout.write("line2\\r");'
341 'sys.stdout.flush();'
342 'sys.stdout.write("line3\\r\\n");'
343 'sys.stdout.flush();'
344 'sys.stdout.write("line4\\r");'
345 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000346 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000347 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000348 'sys.stdout.write("\\nline6");'],
349 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
350 universal_newlines=1)
351 (stdout, stderr) = p.communicate()
352 if hasattr(open, 'newlines'):
353 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000354 self.assertEqual(stdout,
355 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000356 else:
357 # Interpreter without universal newline support
358 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
359
360 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000361 # Make sure we leak no resources
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000362 max_handles = 1026 # too much for most UNIX systems
363 if mswindows:
364 max_handles = 65 # a full test is too slow on Windows
365 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000366 p = subprocess.Popen([sys.executable, "-c",
367 "import sys;sys.stdout.write(sys.stdin.read())"],
368 stdin=subprocess.PIPE,
369 stdout=subprocess.PIPE,
370 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000371 data = p.communicate("lime")[0]
372 self.assertEqual(data, "lime")
373
374
375 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000376 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
377 '"a b c" d e')
378 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
379 'ab\\"c \\ d')
380 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
381 'a\\\\\\b "de fg" h')
382 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
383 'a\\\\\\"b c d')
384 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
385 '"a\\\\b c" d e')
386 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
387 '"a\\\\b\\ c" d e')
388
389
390 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000391 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000392 "-c", "import time; time.sleep(1)"])
393 count = 0
394 while p.poll() is None:
395 time.sleep(0.1)
396 count += 1
397 # We expect that the poll loop probably went around about 10 times,
398 # but, based on system scheduling we can't control, it's possible
399 # poll() never returned None. It "should be" very rare that it
400 # didn't go around at least twice.
401 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000402 # Subsequent invocations should just return the returncode
403 self.assertEqual(p.poll(), 0)
404
405
406 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000407 p = subprocess.Popen([sys.executable,
408 "-c", "import time; time.sleep(2)"])
409 self.assertEqual(p.wait(), 0)
410 # Subsequent invocations should just return the returncode
411 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000412
Peter Astrand738131d2004-11-30 21:04:45 +0000413
414 def test_invalid_bufsize(self):
415 # an invalid type of the bufsize argument should raise
416 # TypeError.
417 try:
418 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
419 except TypeError:
420 pass
421 else:
422 self.fail("Expected TypeError")
423
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000424 #
425 # POSIX tests
426 #
427 if not mswindows:
428 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000429 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000430 try:
431 p = subprocess.Popen([sys.executable, "-c", ""],
432 cwd="/this/path/does/not/exist")
433 except OSError, e:
434 # The attribute child_traceback should contain "os.chdir"
435 # somewhere.
436 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
437 else:
438 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000439
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000440 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000441 # returncode handles signal termination
Tim Peters3b01a702004-10-12 22:19:32 +0000442 p = subprocess.Popen([sys.executable,
443 "-c", "import os; os.abort()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000444 p.wait()
445 self.assertEqual(-p.returncode, signal.SIGABRT)
446
447 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000448 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000449 p = subprocess.Popen([sys.executable, "-c",
450 'import sys,os;' \
451 'sys.stdout.write(os.getenv("FRUIT"))'],
452 stdout=subprocess.PIPE,
453 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
454 self.assertEqual(p.stdout.read(), "apple")
455
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000456 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000457 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000458 f, fname = self.mkstemp()
459 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000460 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
461 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000462 os.close(f)
463 os.chmod(fname, 0700)
464 p = subprocess.Popen(fname)
465 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000466 os.remove(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000467 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000468
469 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000470 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000471 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000472 [sys.executable,
473 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000474 startupinfo=47)
475 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000476 [sys.executable,
477 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000478 creationflags=47)
479
480 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000481 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000482 newenv = os.environ.copy()
483 newenv["FRUIT"] = "apple"
484 p = subprocess.Popen(["echo $FRUIT"], shell=1,
485 stdout=subprocess.PIPE,
486 env=newenv)
487 self.assertEqual(p.stdout.read().strip(), "apple")
488
489 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000490 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000491 newenv = os.environ.copy()
492 newenv["FRUIT"] = "apple"
493 p = subprocess.Popen("echo $FRUIT", shell=1,
494 stdout=subprocess.PIPE,
495 env=newenv)
496 self.assertEqual(p.stdout.read().strip(), "apple")
497
498 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000499 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000500 f, fname = self.mkstemp()
501 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000502 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
503 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000504 os.close(f)
505 os.chmod(fname, 0700)
506 rc = subprocess.call(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000507 os.remove(fname)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000508 self.assertEqual(rc, 47)
509
Tim Peterse718f612004-10-12 21:51:32 +0000510
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000511 #
512 # Windows tests
513 #
514 if mswindows:
515 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000516 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000517 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000518 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000519 STARTF_USESHOWWINDOW = 1
520 SW_MAXIMIZE = 3
521 startupinfo = subprocess.STARTUPINFO()
522 startupinfo.dwFlags = STARTF_USESHOWWINDOW
523 startupinfo.wShowWindow = SW_MAXIMIZE
524 # Since Python is a console process, it won't be affected
525 # by wShowWindow, but the argument should be silently
526 # ignored
527 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
528 startupinfo=startupinfo)
529
530 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000531 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000532 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000533 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000534 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000535 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000536 creationflags=CREATE_NEW_CONSOLE)
537
538 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000539 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000540 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000541 [sys.executable,
542 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000543 preexec_fn=lambda: 1)
544 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000545 [sys.executable,
546 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000547 close_fds=True)
548
549 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000550 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000551 newenv = os.environ.copy()
552 newenv["FRUIT"] = "physalis"
553 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000554 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000555 env=newenv)
556 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
557
558 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000559 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000560 newenv = os.environ.copy()
561 newenv["FRUIT"] = "physalis"
562 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000563 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000564 env=newenv)
565 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
566
567 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000568 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000569 rc = subprocess.call(sys.executable +
570 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000571 self.assertEqual(rc, 47)
572
573
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000574def test_main():
575 test_support.run_unittest(ProcessTestCase)
576
577if __name__ == "__main__":
578 test_main()