blob: b0d82356bca5372640ab755f30f00d0a12de4e35 [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
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000397 #
398 # POSIX tests
399 #
400 if not mswindows:
401 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000402 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000403 try:
404 p = subprocess.Popen([sys.executable, "-c", ""],
405 cwd="/this/path/does/not/exist")
406 except OSError, e:
407 # The attribute child_traceback should contain "os.chdir"
408 # somewhere.
409 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
410 else:
411 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000412
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000413 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000414 # returncode handles signal termination
Tim Peters3b01a702004-10-12 22:19:32 +0000415 p = subprocess.Popen([sys.executable,
416 "-c", "import os; os.abort()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000417 p.wait()
418 self.assertEqual(-p.returncode, signal.SIGABRT)
419
420 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000421 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000422 p = subprocess.Popen([sys.executable, "-c",
423 'import sys,os;' \
424 'sys.stdout.write(os.getenv("FRUIT"))'],
425 stdout=subprocess.PIPE,
426 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
427 self.assertEqual(p.stdout.read(), "apple")
428
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000429 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000430 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000431 f, fname = self.mkstemp()
432 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000433 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
434 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000435 os.close(f)
436 os.chmod(fname, 0700)
437 p = subprocess.Popen(fname)
438 p.wait()
439 self.assertEqual(p.returncode, 47)
440 os.remove(fname)
441
442 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000443 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000444 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000445 [sys.executable,
446 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000447 startupinfo=47)
448 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000449 [sys.executable,
450 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000451 creationflags=47)
452
453 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000454 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000455 newenv = os.environ.copy()
456 newenv["FRUIT"] = "apple"
457 p = subprocess.Popen(["echo $FRUIT"], shell=1,
458 stdout=subprocess.PIPE,
459 env=newenv)
460 self.assertEqual(p.stdout.read().strip(), "apple")
461
462 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000463 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000464 newenv = os.environ.copy()
465 newenv["FRUIT"] = "apple"
466 p = subprocess.Popen("echo $FRUIT", shell=1,
467 stdout=subprocess.PIPE,
468 env=newenv)
469 self.assertEqual(p.stdout.read().strip(), "apple")
470
471 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000472 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000473 f, fname = self.mkstemp()
474 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000475 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
476 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000477 os.close(f)
478 os.chmod(fname, 0700)
479 rc = subprocess.call(fname)
480 self.assertEqual(rc, 47)
481
Tim Peterse718f612004-10-12 21:51:32 +0000482
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000483 #
484 # Windows tests
485 #
486 if mswindows:
487 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000488 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000489 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000490 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000491 STARTF_USESHOWWINDOW = 1
492 SW_MAXIMIZE = 3
493 startupinfo = subprocess.STARTUPINFO()
494 startupinfo.dwFlags = STARTF_USESHOWWINDOW
495 startupinfo.wShowWindow = SW_MAXIMIZE
496 # Since Python is a console process, it won't be affected
497 # by wShowWindow, but the argument should be silently
498 # ignored
499 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
500 startupinfo=startupinfo)
501
502 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000503 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000504 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000505 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000506 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000507 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000508 creationflags=CREATE_NEW_CONSOLE)
509
510 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000511 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000512 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000513 [sys.executable,
514 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000515 preexec_fn=lambda: 1)
516 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000517 [sys.executable,
518 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000519 close_fds=True)
520
521 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000522 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000523 newenv = os.environ.copy()
524 newenv["FRUIT"] = "physalis"
525 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000526 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000527 env=newenv)
528 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
529
530 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000531 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000532 newenv = os.environ.copy()
533 newenv["FRUIT"] = "physalis"
534 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000535 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000536 env=newenv)
537 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
538
539 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000540 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000541 rc = subprocess.call(sys.executable +
542 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000543 self.assertEqual(rc, 47)
544
545
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000546def test_main():
547 test_support.run_unittest(ProcessTestCase)
548
549if __name__ == "__main__":
550 test_main()