blob: b26d40cb29d4f65da34b7d046c7d9daeeb33eaa0 [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
47 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +000048 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000049 newenv = os.environ.copy()
50 newenv["FRUIT"] = "banana"
51 rc = subprocess.call([sys.executable, "-c",
52 'import sys, os;' \
53 'sys.exit(os.getenv("FRUIT")=="banana")'],
54 env=newenv)
55 self.assertEqual(rc, 1)
56
57 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000058 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000059 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
60 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
61 p.wait()
62 self.assertEqual(p.stdin, None)
63
64 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000065 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +000066 p = subprocess.Popen([sys.executable, "-c",
Tim Peters4052fe52004-10-13 03:29:54 +000067 'print " this bit of output is from a '
68 'test of stdout in a different '
69 'process ..."'],
70 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000071 p.wait()
72 self.assertEqual(p.stdout, None)
73
74 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000075 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000076 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
77 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
78 p.wait()
79 self.assertEqual(p.stderr, None)
80
81 def test_executable(self):
Tim Peters3b01a702004-10-12 22:19:32 +000082 p = subprocess.Popen(["somethingyoudonthave",
83 "-c", "import sys; sys.exit(47)"],
84 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000085 p.wait()
86 self.assertEqual(p.returncode, 47)
87
88 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +000089 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000090 p = subprocess.Popen([sys.executable, "-c",
91 'import sys; sys.exit(sys.stdin.read() == "pear")'],
92 stdin=subprocess.PIPE)
93 p.stdin.write("pear")
94 p.stdin.close()
95 p.wait()
96 self.assertEqual(p.returncode, 1)
97
98 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +000099 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000100 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000101 d = tf.fileno()
102 os.write(d, "pear")
103 os.lseek(d, 0, 0)
104 p = subprocess.Popen([sys.executable, "-c",
105 'import sys; sys.exit(sys.stdin.read() == "pear")'],
106 stdin=d)
107 p.wait()
108 self.assertEqual(p.returncode, 1)
109
110 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000111 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000112 tf = tempfile.TemporaryFile()
113 tf.write("pear")
114 tf.seek(0)
115 p = subprocess.Popen([sys.executable, "-c",
116 'import sys; sys.exit(sys.stdin.read() == "pear")'],
117 stdin=tf)
118 p.wait()
119 self.assertEqual(p.returncode, 1)
120
121 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000122 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000123 p = subprocess.Popen([sys.executable, "-c",
124 'import sys; sys.stdout.write("orange")'],
125 stdout=subprocess.PIPE)
126 self.assertEqual(p.stdout.read(), "orange")
127
128 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000129 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000130 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000131 d = tf.fileno()
132 p = subprocess.Popen([sys.executable, "-c",
133 'import sys; sys.stdout.write("orange")'],
134 stdout=d)
135 p.wait()
136 os.lseek(d, 0, 0)
137 self.assertEqual(os.read(d, 1024), "orange")
138
139 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000140 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000141 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000142 p = subprocess.Popen([sys.executable, "-c",
143 'import sys; sys.stdout.write("orange")'],
144 stdout=tf)
145 p.wait()
146 tf.seek(0)
147 self.assertEqual(tf.read(), "orange")
148
149 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000150 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000151 p = subprocess.Popen([sys.executable, "-c",
152 'import sys; sys.stderr.write("strawberry")'],
153 stderr=subprocess.PIPE)
Tim Peters3761e8d2004-10-13 04:07:12 +0000154 self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()),
155 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000156
157 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000158 # stderr 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.stderr.write("strawberry")'],
163 stderr=d)
164 p.wait()
165 os.lseek(d, 0, 0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000166 self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)),
167 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000168
169 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000170 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000171 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000172 p = subprocess.Popen([sys.executable, "-c",
173 'import sys; sys.stderr.write("strawberry")'],
174 stderr=tf)
175 p.wait()
176 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000177 self.assertEqual(remove_stderr_debug_decorations(tf.read()),
178 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000179
180 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000181 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000182 p = subprocess.Popen([sys.executable, "-c",
183 'import sys;' \
184 'sys.stdout.write("apple");' \
185 'sys.stdout.flush();' \
186 'sys.stderr.write("orange")'],
187 stdout=subprocess.PIPE,
188 stderr=subprocess.STDOUT)
Tim Peters3761e8d2004-10-13 04:07:12 +0000189 output = p.stdout.read()
190 stripped = remove_stderr_debug_decorations(output)
191 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000192
193 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000194 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000195 tf = tempfile.TemporaryFile()
196 p = subprocess.Popen([sys.executable, "-c",
197 'import sys;' \
198 'sys.stdout.write("apple");' \
199 'sys.stdout.flush();' \
200 'sys.stderr.write("orange")'],
201 stdout=tf,
202 stderr=tf)
203 p.wait()
204 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000205 output = tf.read()
206 stripped = remove_stderr_debug_decorations(output)
207 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000208
209 def test_cwd(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000210 tmpdir = os.getenv("TEMP", "/tmp")
Peter Astrand195404f2004-11-12 15:51:48 +0000211 # We cannot use os.path.realpath to canonicalize the path,
212 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
213 cwd = os.getcwd()
214 os.chdir(tmpdir)
215 tmpdir = os.getcwd()
216 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000217 p = subprocess.Popen([sys.executable, "-c",
218 'import sys,os;' \
219 'sys.stdout.write(os.getcwd())'],
220 stdout=subprocess.PIPE,
221 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000222 normcase = os.path.normcase
223 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000224
225 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000226 newenv = os.environ.copy()
227 newenv["FRUIT"] = "orange"
228 p = subprocess.Popen([sys.executable, "-c",
229 'import sys,os;' \
230 'sys.stdout.write(os.getenv("FRUIT"))'],
231 stdout=subprocess.PIPE,
232 env=newenv)
233 self.assertEqual(p.stdout.read(), "orange")
234
235 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000236 p = subprocess.Popen([sys.executable, "-c",
237 'import sys,os;' \
238 'sys.stderr.write("pineapple");' \
239 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000240 stdin=subprocess.PIPE,
241 stdout=subprocess.PIPE,
242 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000243 (stdout, stderr) = p.communicate("banana")
244 self.assertEqual(stdout, "banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000245 self.assertEqual(remove_stderr_debug_decorations(stderr),
246 "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000247
248 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000249 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000250 p = subprocess.Popen([sys.executable, "-c",
251 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000252 (stdout, stderr) = p.communicate()
253 self.assertEqual(stdout, None)
254 self.assertEqual(stderr, None)
255
256 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000257 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000258 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000259 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000260 x, y = os.pipe()
261 if mswindows:
262 pipe_buf = 512
263 else:
264 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
265 os.close(x)
266 os.close(y)
267 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000268 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000269 'sys.stdout.write(sys.stdin.read(47));' \
270 'sys.stderr.write("xyz"*%d);' \
271 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000272 stdin=subprocess.PIPE,
273 stdout=subprocess.PIPE,
274 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000275 string_to_write = "abc"*pipe_buf
276 (stdout, stderr) = p.communicate(string_to_write)
277 self.assertEqual(stdout, string_to_write)
278
279 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000280 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000281 p = subprocess.Popen([sys.executable, "-c",
282 'import sys,os;' \
283 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000284 stdin=subprocess.PIPE,
285 stdout=subprocess.PIPE,
286 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000287 p.stdin.write("banana")
288 (stdout, stderr) = p.communicate("split")
289 self.assertEqual(stdout, "bananasplit")
Tim Peters3761e8d2004-10-13 04:07:12 +0000290 self.assertEqual(remove_stderr_debug_decorations(stderr), "")
Tim Peterse718f612004-10-12 21:51:32 +0000291
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000292 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000293 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000294 'import sys,os;' + SETBINARY +
295 'sys.stdout.write("line1\\n");'
296 'sys.stdout.flush();'
297 'sys.stdout.write("line2\\r");'
298 'sys.stdout.flush();'
299 'sys.stdout.write("line3\\r\\n");'
300 'sys.stdout.flush();'
301 'sys.stdout.write("line4\\r");'
302 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000303 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000304 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000305 'sys.stdout.write("\\nline6");'],
306 stdout=subprocess.PIPE,
307 universal_newlines=1)
308 stdout = p.stdout.read()
309 if hasattr(open, 'newlines'):
310 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000311 self.assertEqual(stdout,
312 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000313 else:
314 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000315 self.assertEqual(stdout,
316 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000317
318 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000319 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000320 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000321 'import sys,os;' + SETBINARY +
322 'sys.stdout.write("line1\\n");'
323 'sys.stdout.flush();'
324 'sys.stdout.write("line2\\r");'
325 'sys.stdout.flush();'
326 'sys.stdout.write("line3\\r\\n");'
327 'sys.stdout.flush();'
328 'sys.stdout.write("line4\\r");'
329 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000330 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000331 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000332 'sys.stdout.write("\\nline6");'],
333 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
334 universal_newlines=1)
335 (stdout, stderr) = p.communicate()
336 if hasattr(open, 'newlines'):
337 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000338 self.assertEqual(stdout,
339 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000340 else:
341 # Interpreter without universal newline support
342 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
343
344 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000345 # Make sure we leak no resources
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000346 max_handles = 1026 # too much for most UNIX systems
347 if mswindows:
348 max_handles = 65 # a full test is too slow on Windows
349 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000350 p = subprocess.Popen([sys.executable, "-c",
351 "import sys;sys.stdout.write(sys.stdin.read())"],
352 stdin=subprocess.PIPE,
353 stdout=subprocess.PIPE,
354 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000355 data = p.communicate("lime")[0]
356 self.assertEqual(data, "lime")
357
358
359 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000360 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
361 '"a b c" d e')
362 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
363 'ab\\"c \\ d')
364 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
365 'a\\\\\\b "de fg" h')
366 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
367 'a\\\\\\"b c d')
368 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
369 '"a\\\\b c" d e')
370 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
371 '"a\\\\b\\ c" d e')
372
373
374 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000375 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000376 "-c", "import time; time.sleep(1)"])
377 count = 0
378 while p.poll() is None:
379 time.sleep(0.1)
380 count += 1
381 # We expect that the poll loop probably went around about 10 times,
382 # but, based on system scheduling we can't control, it's possible
383 # poll() never returned None. It "should be" very rare that it
384 # didn't go around at least twice.
385 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000386 # Subsequent invocations should just return the returncode
387 self.assertEqual(p.poll(), 0)
388
389
390 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000391 p = subprocess.Popen([sys.executable,
392 "-c", "import time; time.sleep(2)"])
393 self.assertEqual(p.wait(), 0)
394 # Subsequent invocations should just return the returncode
395 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000396
Peter Astrand738131d2004-11-30 21:04:45 +0000397
398 def test_invalid_bufsize(self):
399 # an invalid type of the bufsize argument should raise
400 # TypeError.
401 try:
402 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
403 except TypeError:
404 pass
405 else:
406 self.fail("Expected TypeError")
407
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000408 #
409 # POSIX tests
410 #
411 if not mswindows:
412 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000413 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000414 try:
415 p = subprocess.Popen([sys.executable, "-c", ""],
416 cwd="/this/path/does/not/exist")
417 except OSError, e:
418 # The attribute child_traceback should contain "os.chdir"
419 # somewhere.
420 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
421 else:
422 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000423
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000424 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000425 # returncode handles signal termination
Tim Peters3b01a702004-10-12 22:19:32 +0000426 p = subprocess.Popen([sys.executable,
427 "-c", "import os; os.abort()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000428 p.wait()
429 self.assertEqual(-p.returncode, signal.SIGABRT)
430
431 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000432 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000433 p = subprocess.Popen([sys.executable, "-c",
434 'import sys,os;' \
435 'sys.stdout.write(os.getenv("FRUIT"))'],
436 stdout=subprocess.PIPE,
437 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
438 self.assertEqual(p.stdout.read(), "apple")
439
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000440 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000441 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000442 f, fname = self.mkstemp()
443 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000444 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
445 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000446 os.close(f)
447 os.chmod(fname, 0700)
448 p = subprocess.Popen(fname)
449 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000450 os.remove(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000451 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000452
453 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000454 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000455 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000456 [sys.executable,
457 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000458 startupinfo=47)
459 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000460 [sys.executable,
461 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000462 creationflags=47)
463
464 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000465 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000466 newenv = os.environ.copy()
467 newenv["FRUIT"] = "apple"
468 p = subprocess.Popen(["echo $FRUIT"], shell=1,
469 stdout=subprocess.PIPE,
470 env=newenv)
471 self.assertEqual(p.stdout.read().strip(), "apple")
472
473 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000474 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000475 newenv = os.environ.copy()
476 newenv["FRUIT"] = "apple"
477 p = subprocess.Popen("echo $FRUIT", shell=1,
478 stdout=subprocess.PIPE,
479 env=newenv)
480 self.assertEqual(p.stdout.read().strip(), "apple")
481
482 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000483 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000484 f, fname = self.mkstemp()
485 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000486 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
487 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000488 os.close(f)
489 os.chmod(fname, 0700)
490 rc = subprocess.call(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000491 os.remove(fname)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000492 self.assertEqual(rc, 47)
493
Tim Peterse718f612004-10-12 21:51:32 +0000494
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000495 #
496 # Windows tests
497 #
498 if mswindows:
499 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000500 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000501 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000502 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000503 STARTF_USESHOWWINDOW = 1
504 SW_MAXIMIZE = 3
505 startupinfo = subprocess.STARTUPINFO()
506 startupinfo.dwFlags = STARTF_USESHOWWINDOW
507 startupinfo.wShowWindow = SW_MAXIMIZE
508 # Since Python is a console process, it won't be affected
509 # by wShowWindow, but the argument should be silently
510 # ignored
511 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
512 startupinfo=startupinfo)
513
514 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000515 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000516 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000517 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000518 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000519 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000520 creationflags=CREATE_NEW_CONSOLE)
521
522 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000523 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000524 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000525 [sys.executable,
526 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000527 preexec_fn=lambda: 1)
528 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000529 [sys.executable,
530 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000531 close_fds=True)
532
533 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000534 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000535 newenv = os.environ.copy()
536 newenv["FRUIT"] = "physalis"
537 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000538 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000539 env=newenv)
540 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
541
542 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000543 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000544 newenv = os.environ.copy()
545 newenv["FRUIT"] = "physalis"
546 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000547 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000548 env=newenv)
549 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
550
551 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000552 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000553 rc = subprocess.call(sys.executable +
554 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000555 self.assertEqual(rc, 47)
556
557
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000558def test_main():
559 test_support.run_unittest(ProcessTestCase)
560
561if __name__ == "__main__":
562 test_main()