blob: c351ee92b13e48c9579dd49d89b62854b47a4d9f [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001import unittest
2from test import test_support
3import subprocess
4import sys
5import signal
6import os
7import tempfile
8import time
Tim Peters3761e8d2004-10-13 04:07:12 +00009import re
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000010
11mswindows = (sys.platform == "win32")
12
13#
14# Depends on the following external programs: Python
15#
16
17if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000018 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
19 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000020else:
21 SETBINARY = ''
22
Tim Peters3761e8d2004-10-13 04:07:12 +000023# In a debug build, stuff like "[6580 refs]" is printed to stderr at
24# shutdown time. That frustrates tests trying to check stderr produced
25# from a spawned Python process.
26def remove_stderr_debug_decorations(stderr):
Tim Peters1dbf2432004-10-14 04:16:54 +000027 return re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
Tim Peters3761e8d2004-10-13 04:07:12 +000028
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000029class ProcessTestCase(unittest.TestCase):
30 def mkstemp(self):
31 """wrapper for mkstemp, calling mktemp if mkstemp is not available"""
32 if hasattr(tempfile, "mkstemp"):
33 return tempfile.mkstemp()
34 else:
35 fname = tempfile.mktemp()
36 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
Tim Peterse718f612004-10-12 21:51:32 +000037
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000038 #
39 # Generic tests
40 #
41 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000042 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000043 rc = subprocess.call([sys.executable, "-c",
44 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000045 self.assertEqual(rc, 47)
46
Peter Astrand454f7672005-01-01 09:36:35 +000047 def test_check_call_zero(self):
48 # check_call() function with zero return code
49 rc = subprocess.check_call([sys.executable, "-c",
50 "import sys; sys.exit(0)"])
51 self.assertEqual(rc, 0)
52
53 def test_check_call_nonzero(self):
54 # check_call() function with non-zero return code
55 try:
56 subprocess.check_call([sys.executable, "-c",
57 "import sys; sys.exit(47)"])
58 except subprocess.CalledProcessError, e:
59 self.assertEqual(e.errno, 47)
60 else:
61 self.fail("Expected CalledProcessError")
62
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000063 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +000064 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000065 newenv = os.environ.copy()
66 newenv["FRUIT"] = "banana"
67 rc = subprocess.call([sys.executable, "-c",
68 'import sys, os;' \
69 'sys.exit(os.getenv("FRUIT")=="banana")'],
70 env=newenv)
71 self.assertEqual(rc, 1)
72
73 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000074 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000075 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
76 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
77 p.wait()
78 self.assertEqual(p.stdin, None)
79
80 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000081 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +000082 p = subprocess.Popen([sys.executable, "-c",
Tim Peters4052fe52004-10-13 03:29:54 +000083 'print " this bit of output is from a '
84 'test of stdout in a different '
85 'process ..."'],
86 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000087 p.wait()
88 self.assertEqual(p.stdout, None)
89
90 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000091 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000092 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
93 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
94 p.wait()
95 self.assertEqual(p.stderr, None)
96
97 def test_executable(self):
Tim Peters3b01a702004-10-12 22:19:32 +000098 p = subprocess.Popen(["somethingyoudonthave",
99 "-c", "import sys; sys.exit(47)"],
100 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000101 p.wait()
102 self.assertEqual(p.returncode, 47)
103
104 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000105 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000106 p = subprocess.Popen([sys.executable, "-c",
107 'import sys; sys.exit(sys.stdin.read() == "pear")'],
108 stdin=subprocess.PIPE)
109 p.stdin.write("pear")
110 p.stdin.close()
111 p.wait()
112 self.assertEqual(p.returncode, 1)
113
114 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000115 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000116 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000117 d = tf.fileno()
118 os.write(d, "pear")
119 os.lseek(d, 0, 0)
120 p = subprocess.Popen([sys.executable, "-c",
121 'import sys; sys.exit(sys.stdin.read() == "pear")'],
122 stdin=d)
123 p.wait()
124 self.assertEqual(p.returncode, 1)
125
126 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000127 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000128 tf = tempfile.TemporaryFile()
129 tf.write("pear")
130 tf.seek(0)
131 p = subprocess.Popen([sys.executable, "-c",
132 'import sys; sys.exit(sys.stdin.read() == "pear")'],
133 stdin=tf)
134 p.wait()
135 self.assertEqual(p.returncode, 1)
136
137 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000138 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000139 p = subprocess.Popen([sys.executable, "-c",
140 'import sys; sys.stdout.write("orange")'],
141 stdout=subprocess.PIPE)
142 self.assertEqual(p.stdout.read(), "orange")
143
144 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000145 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000146 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000147 d = tf.fileno()
148 p = subprocess.Popen([sys.executable, "-c",
149 'import sys; sys.stdout.write("orange")'],
150 stdout=d)
151 p.wait()
152 os.lseek(d, 0, 0)
153 self.assertEqual(os.read(d, 1024), "orange")
154
155 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000156 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000157 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000158 p = subprocess.Popen([sys.executable, "-c",
159 'import sys; sys.stdout.write("orange")'],
160 stdout=tf)
161 p.wait()
162 tf.seek(0)
163 self.assertEqual(tf.read(), "orange")
164
165 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000166 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000167 p = subprocess.Popen([sys.executable, "-c",
168 'import sys; sys.stderr.write("strawberry")'],
169 stderr=subprocess.PIPE)
Tim Peters3761e8d2004-10-13 04:07:12 +0000170 self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()),
171 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000172
173 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000174 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000175 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000176 d = tf.fileno()
177 p = subprocess.Popen([sys.executable, "-c",
178 'import sys; sys.stderr.write("strawberry")'],
179 stderr=d)
180 p.wait()
181 os.lseek(d, 0, 0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000182 self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)),
183 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000184
185 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000186 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000187 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000188 p = subprocess.Popen([sys.executable, "-c",
189 'import sys; sys.stderr.write("strawberry")'],
190 stderr=tf)
191 p.wait()
192 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000193 self.assertEqual(remove_stderr_debug_decorations(tf.read()),
194 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000195
196 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000197 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000198 p = subprocess.Popen([sys.executable, "-c",
199 'import sys;' \
200 'sys.stdout.write("apple");' \
201 'sys.stdout.flush();' \
202 'sys.stderr.write("orange")'],
203 stdout=subprocess.PIPE,
204 stderr=subprocess.STDOUT)
Tim Peters3761e8d2004-10-13 04:07:12 +0000205 output = p.stdout.read()
206 stripped = remove_stderr_debug_decorations(output)
207 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000208
209 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000210 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000211 tf = tempfile.TemporaryFile()
212 p = subprocess.Popen([sys.executable, "-c",
213 'import sys;' \
214 'sys.stdout.write("apple");' \
215 'sys.stdout.flush();' \
216 'sys.stderr.write("orange")'],
217 stdout=tf,
218 stderr=tf)
219 p.wait()
220 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000221 output = tf.read()
222 stripped = remove_stderr_debug_decorations(output)
223 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000224
225 def test_cwd(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000226 tmpdir = os.getenv("TEMP", "/tmp")
Peter Astrand195404f2004-11-12 15:51:48 +0000227 # We cannot use os.path.realpath to canonicalize the path,
228 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
229 cwd = os.getcwd()
230 os.chdir(tmpdir)
231 tmpdir = os.getcwd()
232 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000233 p = subprocess.Popen([sys.executable, "-c",
234 'import sys,os;' \
235 'sys.stdout.write(os.getcwd())'],
236 stdout=subprocess.PIPE,
237 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000238 normcase = os.path.normcase
239 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000240
241 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000242 newenv = os.environ.copy()
243 newenv["FRUIT"] = "orange"
244 p = subprocess.Popen([sys.executable, "-c",
245 'import sys,os;' \
246 'sys.stdout.write(os.getenv("FRUIT"))'],
247 stdout=subprocess.PIPE,
248 env=newenv)
249 self.assertEqual(p.stdout.read(), "orange")
250
Peter Astrandcbac93c2005-03-03 20:24:28 +0000251 def test_communicate_stdin(self):
252 p = subprocess.Popen([sys.executable, "-c",
253 'import sys; sys.exit(sys.stdin.read() == "pear")'],
254 stdin=subprocess.PIPE)
255 p.communicate("pear")
256 self.assertEqual(p.returncode, 1)
257
258 def test_communicate_stdout(self):
259 p = subprocess.Popen([sys.executable, "-c",
260 'import sys; sys.stdout.write("pineapple")'],
261 stdout=subprocess.PIPE)
262 (stdout, stderr) = p.communicate()
263 self.assertEqual(stdout, "pineapple")
264 self.assertEqual(stderr, None)
265
266 def test_communicate_stderr(self):
267 p = subprocess.Popen([sys.executable, "-c",
268 'import sys; sys.stderr.write("pineapple")'],
269 stderr=subprocess.PIPE)
270 (stdout, stderr) = p.communicate()
271 self.assertEqual(stdout, None)
Brett Cannon653a5ad2005-03-05 06:40:52 +0000272 # When running with a pydebug build, the # of references is outputted
273 # to stderr, so just check if stderr at least started with "pinapple"
274 self.assert_(stderr.startswith("pineapple"))
Peter Astrandcbac93c2005-03-03 20:24:28 +0000275
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000276 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000277 p = subprocess.Popen([sys.executable, "-c",
278 'import sys,os;' \
279 'sys.stderr.write("pineapple");' \
280 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000281 stdin=subprocess.PIPE,
282 stdout=subprocess.PIPE,
283 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000284 (stdout, stderr) = p.communicate("banana")
285 self.assertEqual(stdout, "banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000286 self.assertEqual(remove_stderr_debug_decorations(stderr),
287 "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000288
289 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000290 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000291 p = subprocess.Popen([sys.executable, "-c",
292 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000293 (stdout, stderr) = p.communicate()
294 self.assertEqual(stdout, None)
295 self.assertEqual(stderr, None)
296
297 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000298 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000299 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000300 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000301 x, y = os.pipe()
302 if mswindows:
303 pipe_buf = 512
304 else:
305 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
306 os.close(x)
307 os.close(y)
308 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000309 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000310 'sys.stdout.write(sys.stdin.read(47));' \
311 'sys.stderr.write("xyz"*%d);' \
312 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000313 stdin=subprocess.PIPE,
314 stdout=subprocess.PIPE,
315 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000316 string_to_write = "abc"*pipe_buf
317 (stdout, stderr) = p.communicate(string_to_write)
318 self.assertEqual(stdout, string_to_write)
319
320 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000321 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000322 p = subprocess.Popen([sys.executable, "-c",
323 'import sys,os;' \
324 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000325 stdin=subprocess.PIPE,
326 stdout=subprocess.PIPE,
327 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000328 p.stdin.write("banana")
329 (stdout, stderr) = p.communicate("split")
330 self.assertEqual(stdout, "bananasplit")
Tim Peters3761e8d2004-10-13 04:07:12 +0000331 self.assertEqual(remove_stderr_debug_decorations(stderr), "")
Tim Peterse718f612004-10-12 21:51:32 +0000332
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000333 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000334 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000335 'import sys,os;' + SETBINARY +
336 'sys.stdout.write("line1\\n");'
337 'sys.stdout.flush();'
338 'sys.stdout.write("line2\\r");'
339 'sys.stdout.flush();'
340 'sys.stdout.write("line3\\r\\n");'
341 'sys.stdout.flush();'
342 'sys.stdout.write("line4\\r");'
343 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000344 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000345 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000346 'sys.stdout.write("\\nline6");'],
347 stdout=subprocess.PIPE,
348 universal_newlines=1)
349 stdout = p.stdout.read()
350 if hasattr(open, 'newlines'):
351 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000352 self.assertEqual(stdout,
353 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000354 else:
355 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000356 self.assertEqual(stdout,
357 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000358
359 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000360 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000361 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000362 'import sys,os;' + SETBINARY +
363 'sys.stdout.write("line1\\n");'
364 'sys.stdout.flush();'
365 'sys.stdout.write("line2\\r");'
366 'sys.stdout.flush();'
367 'sys.stdout.write("line3\\r\\n");'
368 'sys.stdout.flush();'
369 'sys.stdout.write("line4\\r");'
370 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000371 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000372 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000373 'sys.stdout.write("\\nline6");'],
374 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
375 universal_newlines=1)
376 (stdout, stderr) = p.communicate()
377 if hasattr(open, 'newlines'):
378 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000379 self.assertEqual(stdout,
380 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000381 else:
382 # Interpreter without universal newline support
383 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
384
385 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000386 # Make sure we leak no resources
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000387 if test_support.is_resource_enabled("subprocess") and not mswindows:
388 max_handles = 1026 # too much for most UNIX systems
389 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000390 max_handles = 65
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000391 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000392 p = subprocess.Popen([sys.executable, "-c",
393 "import sys;sys.stdout.write(sys.stdin.read())"],
394 stdin=subprocess.PIPE,
395 stdout=subprocess.PIPE,
396 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000397 data = p.communicate("lime")[0]
398 self.assertEqual(data, "lime")
399
400
401 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000402 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
403 '"a b c" d e')
404 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
405 'ab\\"c \\ d')
406 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
407 'a\\\\\\b "de fg" h')
408 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
409 'a\\\\\\"b c d')
410 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
411 '"a\\\\b c" d e')
412 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
413 '"a\\\\b\\ c" d e')
414
415
416 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000417 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000418 "-c", "import time; time.sleep(1)"])
419 count = 0
420 while p.poll() is None:
421 time.sleep(0.1)
422 count += 1
423 # We expect that the poll loop probably went around about 10 times,
424 # but, based on system scheduling we can't control, it's possible
425 # poll() never returned None. It "should be" very rare that it
426 # didn't go around at least twice.
427 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000428 # Subsequent invocations should just return the returncode
429 self.assertEqual(p.poll(), 0)
430
431
432 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000433 p = subprocess.Popen([sys.executable,
434 "-c", "import time; time.sleep(2)"])
435 self.assertEqual(p.wait(), 0)
436 # Subsequent invocations should just return the returncode
437 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000438
Peter Astrand738131d2004-11-30 21:04:45 +0000439
440 def test_invalid_bufsize(self):
441 # an invalid type of the bufsize argument should raise
442 # TypeError.
443 try:
444 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
445 except TypeError:
446 pass
447 else:
448 self.fail("Expected TypeError")
449
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000450 #
451 # POSIX tests
452 #
453 if not mswindows:
454 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000455 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000456 try:
457 p = subprocess.Popen([sys.executable, "-c", ""],
458 cwd="/this/path/does/not/exist")
459 except OSError, e:
460 # The attribute child_traceback should contain "os.chdir"
461 # somewhere.
462 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
463 else:
464 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000465
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000466 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000467 # returncode handles signal termination
Tim Peters3b01a702004-10-12 22:19:32 +0000468 p = subprocess.Popen([sys.executable,
469 "-c", "import os; os.abort()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000470 p.wait()
471 self.assertEqual(-p.returncode, signal.SIGABRT)
472
473 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000474 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000475 p = subprocess.Popen([sys.executable, "-c",
476 'import sys,os;' \
477 'sys.stdout.write(os.getenv("FRUIT"))'],
478 stdout=subprocess.PIPE,
479 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
480 self.assertEqual(p.stdout.read(), "apple")
481
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000482 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000483 # args is a string
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 p = subprocess.Popen(fname)
491 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000492 os.remove(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000493 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000494
495 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000496 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000497 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000498 [sys.executable,
499 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000500 startupinfo=47)
501 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000502 [sys.executable,
503 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000504 creationflags=47)
505
506 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000507 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000508 newenv = os.environ.copy()
509 newenv["FRUIT"] = "apple"
510 p = subprocess.Popen(["echo $FRUIT"], shell=1,
511 stdout=subprocess.PIPE,
512 env=newenv)
513 self.assertEqual(p.stdout.read().strip(), "apple")
514
515 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000516 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000517 newenv = os.environ.copy()
518 newenv["FRUIT"] = "apple"
519 p = subprocess.Popen("echo $FRUIT", shell=1,
520 stdout=subprocess.PIPE,
521 env=newenv)
522 self.assertEqual(p.stdout.read().strip(), "apple")
523
524 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000525 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000526 f, fname = self.mkstemp()
527 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000528 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
529 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000530 os.close(f)
531 os.chmod(fname, 0700)
532 rc = subprocess.call(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000533 os.remove(fname)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000534 self.assertEqual(rc, 47)
535
Tim Peterse718f612004-10-12 21:51:32 +0000536
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000537 #
538 # Windows tests
539 #
540 if mswindows:
541 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000542 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000543 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000544 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000545 STARTF_USESHOWWINDOW = 1
546 SW_MAXIMIZE = 3
547 startupinfo = subprocess.STARTUPINFO()
548 startupinfo.dwFlags = STARTF_USESHOWWINDOW
549 startupinfo.wShowWindow = SW_MAXIMIZE
550 # Since Python is a console process, it won't be affected
551 # by wShowWindow, but the argument should be silently
552 # ignored
553 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
554 startupinfo=startupinfo)
555
556 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000557 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000558 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000559 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000560 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000561 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000562 creationflags=CREATE_NEW_CONSOLE)
563
564 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000565 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000566 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000567 [sys.executable,
568 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000569 preexec_fn=lambda: 1)
570 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000571 [sys.executable,
572 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000573 close_fds=True)
574
575 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000576 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000577 newenv = os.environ.copy()
578 newenv["FRUIT"] = "physalis"
579 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000580 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000581 env=newenv)
582 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
583
584 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000585 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000586 newenv = os.environ.copy()
587 newenv["FRUIT"] = "physalis"
588 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000589 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000590 env=newenv)
591 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
592
593 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000594 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000595 rc = subprocess.call(sys.executable +
596 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000597 self.assertEqual(rc, 47)
598
599
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000600def test_main():
601 test_support.run_unittest(ProcessTestCase)
602
603if __name__ == "__main__":
604 test_main()