blob: 639a54d9b510c683bb3e415ada5eb30e374db068 [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")
211 tmpdir = os.path.realpath(tmpdir)
212 p = subprocess.Popen([sys.executable, "-c",
213 'import sys,os;' \
214 'sys.stdout.write(os.getcwd())'],
215 stdout=subprocess.PIPE,
216 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000217 normcase = os.path.normcase
218 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000219
220 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000221 newenv = os.environ.copy()
222 newenv["FRUIT"] = "orange"
223 p = subprocess.Popen([sys.executable, "-c",
224 'import sys,os;' \
225 'sys.stdout.write(os.getenv("FRUIT"))'],
226 stdout=subprocess.PIPE,
227 env=newenv)
228 self.assertEqual(p.stdout.read(), "orange")
229
230 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000231 p = subprocess.Popen([sys.executable, "-c",
232 'import sys,os;' \
233 'sys.stderr.write("pineapple");' \
234 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000235 stdin=subprocess.PIPE,
236 stdout=subprocess.PIPE,
237 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000238 (stdout, stderr) = p.communicate("banana")
239 self.assertEqual(stdout, "banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000240 self.assertEqual(remove_stderr_debug_decorations(stderr),
241 "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000242
243 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000244 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000245 p = subprocess.Popen([sys.executable, "-c",
246 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000247 (stdout, stderr) = p.communicate()
248 self.assertEqual(stdout, None)
249 self.assertEqual(stderr, None)
250
251 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000252 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000253 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000254 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000255 x, y = os.pipe()
256 if mswindows:
257 pipe_buf = 512
258 else:
259 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
260 os.close(x)
261 os.close(y)
262 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000263 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000264 'sys.stdout.write(sys.stdin.read(47));' \
265 'sys.stderr.write("xyz"*%d);' \
266 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000267 stdin=subprocess.PIPE,
268 stdout=subprocess.PIPE,
269 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000270 string_to_write = "abc"*pipe_buf
271 (stdout, stderr) = p.communicate(string_to_write)
272 self.assertEqual(stdout, string_to_write)
273
274 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000275 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000276 p = subprocess.Popen([sys.executable, "-c",
277 'import sys,os;' \
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 p.stdin.write("banana")
283 (stdout, stderr) = p.communicate("split")
284 self.assertEqual(stdout, "bananasplit")
Tim Peters3761e8d2004-10-13 04:07:12 +0000285 self.assertEqual(remove_stderr_debug_decorations(stderr), "")
Tim Peterse718f612004-10-12 21:51:32 +0000286
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000287 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000288 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000289 'import sys,os;' + SETBINARY +
290 'sys.stdout.write("line1\\n");'
291 'sys.stdout.flush();'
292 'sys.stdout.write("line2\\r");'
293 'sys.stdout.flush();'
294 'sys.stdout.write("line3\\r\\n");'
295 'sys.stdout.flush();'
296 'sys.stdout.write("line4\\r");'
297 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000298 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000299 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000300 'sys.stdout.write("\\nline6");'],
301 stdout=subprocess.PIPE,
302 universal_newlines=1)
303 stdout = p.stdout.read()
304 if hasattr(open, 'newlines'):
305 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000306 self.assertEqual(stdout,
307 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000308 else:
309 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000310 self.assertEqual(stdout,
311 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000312
313 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000314 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000315 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000316 'import sys,os;' + SETBINARY +
317 'sys.stdout.write("line1\\n");'
318 'sys.stdout.flush();'
319 'sys.stdout.write("line2\\r");'
320 'sys.stdout.flush();'
321 'sys.stdout.write("line3\\r\\n");'
322 'sys.stdout.flush();'
323 'sys.stdout.write("line4\\r");'
324 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000325 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000326 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000327 'sys.stdout.write("\\nline6");'],
328 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
329 universal_newlines=1)
330 (stdout, stderr) = p.communicate()
331 if hasattr(open, 'newlines'):
332 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000333 self.assertEqual(stdout,
334 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000335 else:
336 # Interpreter without universal newline support
337 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
338
339 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000340 # Make sure we leak no resources
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000341 max_handles = 1026 # too much for most UNIX systems
342 if mswindows:
343 max_handles = 65 # a full test is too slow on Windows
344 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000345 p = subprocess.Popen([sys.executable, "-c",
346 "import sys;sys.stdout.write(sys.stdin.read())"],
347 stdin=subprocess.PIPE,
348 stdout=subprocess.PIPE,
349 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000350 data = p.communicate("lime")[0]
351 self.assertEqual(data, "lime")
352
353
354 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000355 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
356 '"a b c" d e')
357 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
358 'ab\\"c \\ d')
359 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
360 'a\\\\\\b "de fg" h')
361 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
362 'a\\\\\\"b c d')
363 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
364 '"a\\\\b c" d e')
365 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
366 '"a\\\\b\\ c" d e')
367
368
369 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000370 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000371 "-c", "import time; time.sleep(1)"])
372 count = 0
373 while p.poll() is None:
374 time.sleep(0.1)
375 count += 1
376 # We expect that the poll loop probably went around about 10 times,
377 # but, based on system scheduling we can't control, it's possible
378 # poll() never returned None. It "should be" very rare that it
379 # didn't go around at least twice.
380 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000381 # Subsequent invocations should just return the returncode
382 self.assertEqual(p.poll(), 0)
383
384
385 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000386 p = subprocess.Popen([sys.executable,
387 "-c", "import time; time.sleep(2)"])
388 self.assertEqual(p.wait(), 0)
389 # Subsequent invocations should just return the returncode
390 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000391
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000392 #
393 # POSIX tests
394 #
395 if not mswindows:
396 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000397 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000398 try:
399 p = subprocess.Popen([sys.executable, "-c", ""],
400 cwd="/this/path/does/not/exist")
401 except OSError, e:
402 # The attribute child_traceback should contain "os.chdir"
403 # somewhere.
404 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
405 else:
406 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000407
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000408 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000409 # returncode handles signal termination
Tim Peters3b01a702004-10-12 22:19:32 +0000410 p = subprocess.Popen([sys.executable,
411 "-c", "import os; os.abort()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000412 p.wait()
413 self.assertEqual(-p.returncode, signal.SIGABRT)
414
415 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000416 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000417 p = subprocess.Popen([sys.executable, "-c",
418 'import sys,os;' \
419 'sys.stdout.write(os.getenv("FRUIT"))'],
420 stdout=subprocess.PIPE,
421 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
422 self.assertEqual(p.stdout.read(), "apple")
423
424 def test_close_fds(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000425 # Make sure we have some fds open
426 os.pipe()
427 p = subprocess.Popen([sys.executable, "-c",
428 'import sys,os;' \
429 'sys.stdout.write(str(os.dup(0)))'],
430 stdout=subprocess.PIPE, close_fds=1)
Tim Peterse718f612004-10-12 21:51:32 +0000431 # When all fds are closed, the next free fd should be 3.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000432 self.assertEqual(p.stdout.read(), "3")
433
434 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000435 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000436 f, fname = self.mkstemp()
437 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000438 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
439 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000440 os.close(f)
441 os.chmod(fname, 0700)
442 p = subprocess.Popen(fname)
443 p.wait()
444 self.assertEqual(p.returncode, 47)
445 os.remove(fname)
446
447 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000448 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000449 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000450 [sys.executable,
451 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000452 startupinfo=47)
453 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000454 [sys.executable,
455 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000456 creationflags=47)
457
458 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000459 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000460 newenv = os.environ.copy()
461 newenv["FRUIT"] = "apple"
462 p = subprocess.Popen(["echo $FRUIT"], shell=1,
463 stdout=subprocess.PIPE,
464 env=newenv)
465 self.assertEqual(p.stdout.read().strip(), "apple")
466
467 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000468 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000469 newenv = os.environ.copy()
470 newenv["FRUIT"] = "apple"
471 p = subprocess.Popen("echo $FRUIT", shell=1,
472 stdout=subprocess.PIPE,
473 env=newenv)
474 self.assertEqual(p.stdout.read().strip(), "apple")
475
476 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000477 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000478 f, fname = self.mkstemp()
479 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000480 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
481 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000482 os.close(f)
483 os.chmod(fname, 0700)
484 rc = subprocess.call(fname)
485 self.assertEqual(rc, 47)
486
Tim Peterse718f612004-10-12 21:51:32 +0000487
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000488 #
489 # Windows tests
490 #
491 if mswindows:
492 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000493 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000494 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000495 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000496 STARTF_USESHOWWINDOW = 1
497 SW_MAXIMIZE = 3
498 startupinfo = subprocess.STARTUPINFO()
499 startupinfo.dwFlags = STARTF_USESHOWWINDOW
500 startupinfo.wShowWindow = SW_MAXIMIZE
501 # Since Python is a console process, it won't be affected
502 # by wShowWindow, but the argument should be silently
503 # ignored
504 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
505 startupinfo=startupinfo)
506
507 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000508 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000509 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000510 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000511 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000512 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000513 creationflags=CREATE_NEW_CONSOLE)
514
515 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000516 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000517 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000518 [sys.executable,
519 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000520 preexec_fn=lambda: 1)
521 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000522 [sys.executable,
523 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000524 close_fds=True)
525
526 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000527 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000528 newenv = os.environ.copy()
529 newenv["FRUIT"] = "physalis"
530 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000531 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000532 env=newenv)
533 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
534
535 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000536 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000537 newenv = os.environ.copy()
538 newenv["FRUIT"] = "physalis"
539 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000540 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000541 env=newenv)
542 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
543
544 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000545 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000546 rc = subprocess.call(sys.executable +
547 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000548 self.assertEqual(rc, 47)
549
550
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000551def test_main():
552 test_support.run_unittest(ProcessTestCase)
553
554if __name__ == "__main__":
555 test_main()