blob: 5d3e040a557e6b43a13e5cb6ab223b7f5592339f [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00003import 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):
Guido van Rossum98297ee2007-11-06 21:34:58 +000027 return re.sub("\[\d+ refs\]\r?\n?$", "", stderr.decode()).encode()
28 #return re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
Tim Peters3761e8d2004-10-13 04:07:12 +000029
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000030class ProcessTestCase(unittest.TestCase):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000031 def setUp(self):
32 # Try to minimize the number of children we have so this test
33 # doesn't crash on some buildbots (Alphas in particular).
Benjamin Petersonee8712c2008-05-20 21:35:26 +000034 if hasattr(support, "reap_children"):
35 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000036
37 def tearDown(self):
38 # Try to minimize the number of children we have so this test
39 # doesn't crash on some buildbots (Alphas in particular).
Benjamin Petersonee8712c2008-05-20 21:35:26 +000040 if hasattr(support, "reap_children"):
41 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000043 def mkstemp(self):
44 """wrapper for mkstemp, calling mktemp if mkstemp is not available"""
45 if hasattr(tempfile, "mkstemp"):
46 return tempfile.mkstemp()
47 else:
48 fname = tempfile.mktemp()
49 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
Tim Peterse718f612004-10-12 21:51:32 +000050
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000051 #
52 # Generic tests
53 #
54 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000055 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000056 rc = subprocess.call([sys.executable, "-c",
57 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000058 self.assertEqual(rc, 47)
59
Peter Astrand454f7672005-01-01 09:36:35 +000060 def test_check_call_zero(self):
61 # check_call() function with zero return code
62 rc = subprocess.check_call([sys.executable, "-c",
63 "import sys; sys.exit(0)"])
64 self.assertEqual(rc, 0)
65
66 def test_check_call_nonzero(self):
67 # check_call() function with non-zero return code
68 try:
69 subprocess.check_call([sys.executable, "-c",
70 "import sys; sys.exit(47)"])
Guido van Rossumb940e112007-01-10 16:19:56 +000071 except subprocess.CalledProcessError as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072 self.assertEqual(e.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000073 else:
74 self.fail("Expected CalledProcessError")
75
Georg Brandlf9734072008-12-07 15:30:06 +000076 def test_check_output(self):
77 # check_output() function with zero return code
78 output = subprocess.check_output(
79 [sys.executable, "-c", "print('BDFL')"])
80 self.assertTrue(b'BDFL' in output)
81
82 def test_check_output_nonzero(self):
83 # check_call() function with non-zero return code
84 try:
85 subprocess.check_output(
86 [sys.executable, "-c", "import sys; sys.exit(5)"])
87 except subprocess.CalledProcessError as e:
88 self.assertEqual(e.returncode, 5)
89 else:
90 self.fail("Expected CalledProcessError")
91
92 def test_check_output_stderr(self):
93 # check_output() function stderr redirected to stdout
94 output = subprocess.check_output(
95 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
96 stderr=subprocess.STDOUT)
97 self.assertTrue(b'BDFL' in output)
98
99 def test_check_output_stdout_arg(self):
100 # check_output() function stderr redirected to stdout
101 try:
102 output = subprocess.check_output(
103 [sys.executable, "-c", "print('will not be run')"],
104 stdout=sys.stdout)
105 except ValueError as e:
106 self.assertTrue('stdout' in e.args[0])
107 else:
108 self.fail("Expected ValueError when stdout arg supplied.")
109
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000110 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000111 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000112 newenv = os.environ.copy()
113 newenv["FRUIT"] = "banana"
114 rc = subprocess.call([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000115 'import sys, os;'
116 'sys.exit(os.getenv("FRUIT")=="banana")'],
117 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000118 self.assertEqual(rc, 1)
119
120 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000121 # .stdin is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000122 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000123 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
124 p.wait()
125 self.assertEqual(p.stdin, None)
126
127 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000128 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +0000129 p = subprocess.Popen([sys.executable, "-c",
Georg Brandl88fc6642007-02-09 21:28:07 +0000130 'print(" this bit of output is from a '
Tim Peters4052fe52004-10-13 03:29:54 +0000131 'test of stdout in a different '
Georg Brandl88fc6642007-02-09 21:28:07 +0000132 'process ...")'],
Tim Peters4052fe52004-10-13 03:29:54 +0000133 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000134 p.wait()
135 self.assertEqual(p.stdout, None)
136
137 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000138 # .stderr is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000139 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000140 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
141 p.wait()
142 self.assertEqual(p.stderr, None)
143
144 def test_executable(self):
Tim Peters3b01a702004-10-12 22:19:32 +0000145 p = subprocess.Popen(["somethingyoudonthave",
146 "-c", "import sys; sys.exit(47)"],
147 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000148 p.wait()
149 self.assertEqual(p.returncode, 47)
150
151 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000152 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000153 p = subprocess.Popen([sys.executable, "-c",
154 'import sys; sys.exit(sys.stdin.read() == "pear")'],
155 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000156 p.stdin.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000157 p.stdin.close()
158 p.wait()
159 self.assertEqual(p.returncode, 1)
160
161 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000162 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000163 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000164 d = tf.fileno()
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000165 os.write(d, b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000166 os.lseek(d, 0, 0)
167 p = subprocess.Popen([sys.executable, "-c",
168 'import sys; sys.exit(sys.stdin.read() == "pear")'],
169 stdin=d)
170 p.wait()
171 self.assertEqual(p.returncode, 1)
172
173 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000174 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000175 tf = tempfile.TemporaryFile()
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000176 tf.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000177 tf.seek(0)
178 p = subprocess.Popen([sys.executable, "-c",
179 'import sys; sys.exit(sys.stdin.read() == "pear")'],
180 stdin=tf)
181 p.wait()
182 self.assertEqual(p.returncode, 1)
183
184 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000185 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000186 p = subprocess.Popen([sys.executable, "-c",
187 'import sys; sys.stdout.write("orange")'],
188 stdout=subprocess.PIPE)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000189 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000190
191 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000192 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000193 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000194 d = tf.fileno()
195 p = subprocess.Popen([sys.executable, "-c",
196 'import sys; sys.stdout.write("orange")'],
197 stdout=d)
198 p.wait()
199 os.lseek(d, 0, 0)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000200 self.assertEqual(os.read(d, 1024), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000201
202 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000203 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000204 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000205 p = subprocess.Popen([sys.executable, "-c",
206 'import sys; sys.stdout.write("orange")'],
207 stdout=tf)
208 p.wait()
209 tf.seek(0)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000210 self.assertEqual(tf.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000211
212 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000213 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000214 p = subprocess.Popen([sys.executable, "-c",
215 'import sys; sys.stderr.write("strawberry")'],
216 stderr=subprocess.PIPE)
Tim Peters3761e8d2004-10-13 04:07:12 +0000217 self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()),
Guido van Rossum98297ee2007-11-06 21:34:58 +0000218 b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000219
220 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000221 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000222 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000223 d = tf.fileno()
224 p = subprocess.Popen([sys.executable, "-c",
225 'import sys; sys.stderr.write("strawberry")'],
226 stderr=d)
227 p.wait()
228 os.lseek(d, 0, 0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000229 self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)),
Guido van Rossum98297ee2007-11-06 21:34:58 +0000230 b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000231
232 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000233 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000234 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000235 p = subprocess.Popen([sys.executable, "-c",
236 'import sys; sys.stderr.write("strawberry")'],
237 stderr=tf)
238 p.wait()
239 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000240 self.assertEqual(remove_stderr_debug_decorations(tf.read()),
Guido van Rossum98297ee2007-11-06 21:34:58 +0000241 b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000242
243 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000244 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000245 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000246 'import sys;'
247 'sys.stdout.write("apple");'
248 'sys.stdout.flush();'
249 'sys.stderr.write("orange")'],
250 stdout=subprocess.PIPE,
251 stderr=subprocess.STDOUT)
Tim Peters3761e8d2004-10-13 04:07:12 +0000252 output = p.stdout.read()
253 stripped = remove_stderr_debug_decorations(output)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000254 self.assertEqual(stripped, b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000255
256 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000257 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000258 tf = tempfile.TemporaryFile()
259 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000260 'import sys;'
261 'sys.stdout.write("apple");'
262 'sys.stdout.flush();'
263 'sys.stderr.write("orange")'],
264 stdout=tf,
265 stderr=tf)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000266 p.wait()
267 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000268 output = tf.read()
269 stripped = remove_stderr_debug_decorations(output)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000270 self.assertEqual(stripped, b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000271
Thomas Wouters89f507f2006-12-13 04:49:30 +0000272 def test_stdout_filedes_of_stdout(self):
273 # stdout is set to 1 (#1531862).
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000274 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), b'.\n'))"
Thomas Wouters89f507f2006-12-13 04:49:30 +0000275 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
276 self.assertEquals(rc, 2)
277
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000278 def test_cwd(self):
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000279 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000280 # We cannot use os.path.realpath to canonicalize the path,
281 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
282 cwd = os.getcwd()
283 os.chdir(tmpdir)
284 tmpdir = os.getcwd()
285 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000286 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000287 'import sys,os;'
288 'sys.stdout.write(os.getcwd())'],
289 stdout=subprocess.PIPE,
290 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000291 normcase = os.path.normcase
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000292 self.assertEqual(normcase(p.stdout.read().decode("utf-8")),
293 normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000294
295 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000296 newenv = os.environ.copy()
297 newenv["FRUIT"] = "orange"
298 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000299 'import sys,os;'
300 'sys.stdout.write(os.getenv("FRUIT"))'],
301 stdout=subprocess.PIPE,
302 env=newenv)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000303 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000304
Peter Astrandcbac93c2005-03-03 20:24:28 +0000305 def test_communicate_stdin(self):
306 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000307 'import sys;'
308 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000309 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000310 p.communicate(b"pear")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000311 self.assertEqual(p.returncode, 1)
312
313 def test_communicate_stdout(self):
314 p = subprocess.Popen([sys.executable, "-c",
315 'import sys; sys.stdout.write("pineapple")'],
316 stdout=subprocess.PIPE)
317 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000318 self.assertEqual(stdout, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000319 self.assertEqual(stderr, None)
320
321 def test_communicate_stderr(self):
322 p = subprocess.Popen([sys.executable, "-c",
323 'import sys; sys.stderr.write("pineapple")'],
324 stderr=subprocess.PIPE)
325 (stdout, stderr) = p.communicate()
326 self.assertEqual(stdout, None)
Brett Cannon653a5ad2005-03-05 06:40:52 +0000327 # When running with a pydebug build, the # of references is outputted
328 # to stderr, so just check if stderr at least started with "pinapple"
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000329 self.assertEqual(remove_stderr_debug_decorations(stderr), b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000330
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000331 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000332 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000333 'import sys,os;'
334 'sys.stderr.write("pineapple");'
335 'sys.stdout.write(sys.stdin.read())'],
336 stdin=subprocess.PIPE,
337 stdout=subprocess.PIPE,
338 stderr=subprocess.PIPE)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000339 (stdout, stderr) = p.communicate(b"banana")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000340 self.assertEqual(stdout, b"banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000341 self.assertEqual(remove_stderr_debug_decorations(stderr),
Guido van Rossum98297ee2007-11-06 21:34:58 +0000342 b"pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000343
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000344 # This test is Linux specific for simplicity to at least have
345 # some coverage. It is not a platform specific bug.
346 if os.path.isdir('/proc/%d/fd' % os.getpid()):
347 # Test for the fd leak reported in http://bugs.python.org/issue2791.
348 def test_communicate_pipe_fd_leak(self):
349 fd_directory = '/proc/%d/fd' % os.getpid()
350 num_fds_before_popen = len(os.listdir(fd_directory))
351 p = subprocess.Popen([sys.executable, '-c', 'print()'],
352 stdout=subprocess.PIPE)
353 p.communicate()
354 num_fds_after_communicate = len(os.listdir(fd_directory))
355 del p
356 num_fds_after_destruction = len(os.listdir(fd_directory))
357 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
358 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
359
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000360 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000361 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000362 p = subprocess.Popen([sys.executable, "-c",
363 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000364 (stdout, stderr) = p.communicate()
365 self.assertEqual(stdout, None)
366 self.assertEqual(stderr, None)
367
368 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000369 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000370 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000371 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000372 x, y = os.pipe()
373 if mswindows:
374 pipe_buf = 512
375 else:
376 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
377 os.close(x)
378 os.close(y)
379 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000380 'import sys,os;'
381 'sys.stdout.write(sys.stdin.read(47));'
382 'sys.stderr.write("xyz"*%d);'
383 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
384 stdin=subprocess.PIPE,
385 stdout=subprocess.PIPE,
386 stderr=subprocess.PIPE)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000387 string_to_write = b"abc"*pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000388 (stdout, stderr) = p.communicate(string_to_write)
389 self.assertEqual(stdout, string_to_write)
390
391 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000392 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000393 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000394 'import sys,os;'
395 'sys.stdout.write(sys.stdin.read())'],
396 stdin=subprocess.PIPE,
397 stdout=subprocess.PIPE,
398 stderr=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000399 p.stdin.write(b"banana")
400 (stdout, stderr) = p.communicate(b"split")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000401 self.assertEqual(stdout, b"bananasplit")
Guido van Rossum98297ee2007-11-06 21:34:58 +0000402 self.assertEqual(remove_stderr_debug_decorations(stderr), b"")
Tim Peterse718f612004-10-12 21:51:32 +0000403
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000404 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000405 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000406 'import sys,os;' + SETBINARY +
407 'sys.stdout.write("line1\\n");'
408 'sys.stdout.flush();'
409 'sys.stdout.write("line2\\n");'
410 'sys.stdout.flush();'
411 'sys.stdout.write("line3\\r\\n");'
412 'sys.stdout.flush();'
413 'sys.stdout.write("line4\\r");'
414 'sys.stdout.flush();'
415 'sys.stdout.write("\\nline5");'
416 'sys.stdout.flush();'
417 'sys.stdout.write("\\nline6");'],
418 stdout=subprocess.PIPE,
419 universal_newlines=1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000420 stdout = p.stdout.read()
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000421 self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000422
423 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000424 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000425 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000426 'import sys,os;' + SETBINARY +
427 'sys.stdout.write("line1\\n");'
428 'sys.stdout.flush();'
429 'sys.stdout.write("line2\\n");'
430 'sys.stdout.flush();'
431 'sys.stdout.write("line3\\r\\n");'
432 'sys.stdout.flush();'
433 'sys.stdout.write("line4\\r");'
434 'sys.stdout.flush();'
435 'sys.stdout.write("\\nline5");'
436 'sys.stdout.flush();'
437 'sys.stdout.write("\\nline6");'],
438 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
439 universal_newlines=1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000440 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000441 self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000442
443 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000444 # Make sure we leak no resources
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000445 if (not hasattr(support, "is_resource_enabled") or
446 support.is_resource_enabled("subprocess") and not mswindows):
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000447 max_handles = 1026 # too much for most UNIX systems
448 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000449 max_handles = 65
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000450 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000451 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000452 "import sys;"
453 "sys.stdout.write(sys.stdin.read())"],
454 stdin=subprocess.PIPE,
455 stdout=subprocess.PIPE,
456 stderr=subprocess.PIPE)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000457 data = p.communicate(b"lime")[0]
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000458 self.assertEqual(data, b"lime")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000459
460
461 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000462 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
463 '"a b c" d e')
464 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
465 'ab\\"c \\ d')
Christian Heimesfdab48e2008-01-20 09:06:41 +0000466 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
467 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000468 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
469 'a\\\\\\b "de fg" h')
470 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
471 'a\\\\\\"b c d')
472 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
473 '"a\\\\b c" d e')
474 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
475 '"a\\\\b\\ c" d e')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000476 self.assertEqual(subprocess.list2cmdline(['ab', '']),
477 'ab ""')
Christian Heimesfdab48e2008-01-20 09:06:41 +0000478 self.assertEqual(subprocess.list2cmdline(['echo', 'foo|bar']),
479 'echo "foo|bar"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000480
481
482 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000483 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000484 "-c", "import time; time.sleep(1)"])
485 count = 0
486 while p.poll() is None:
487 time.sleep(0.1)
488 count += 1
489 # We expect that the poll loop probably went around about 10 times,
490 # but, based on system scheduling we can't control, it's possible
491 # poll() never returned None. It "should be" very rare that it
492 # didn't go around at least twice.
493 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000494 # Subsequent invocations should just return the returncode
495 self.assertEqual(p.poll(), 0)
496
497
498 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000499 p = subprocess.Popen([sys.executable,
500 "-c", "import time; time.sleep(2)"])
501 self.assertEqual(p.wait(), 0)
502 # Subsequent invocations should just return the returncode
503 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000504
Peter Astrand738131d2004-11-30 21:04:45 +0000505
506 def test_invalid_bufsize(self):
507 # an invalid type of the bufsize argument should raise
508 # TypeError.
509 try:
510 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
511 except TypeError:
512 pass
513 else:
514 self.fail("Expected TypeError")
515
Guido van Rossum46a05a72007-06-07 21:56:45 +0000516 def test_bufsize_is_none(self):
517 # bufsize=None should be the same as bufsize=0.
518 p = subprocess.Popen([sys.executable, "-c", "pass"], None)
519 self.assertEqual(p.wait(), 0)
520 # Again with keyword arg
521 p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None)
522 self.assertEqual(p.wait(), 0)
523
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000524 def test_leaking_fds_on_error(self):
525 # see bug #5179: Popen leaks file descriptors to PIPEs if
526 # the child fails to execute; this will eventually exhaust
527 # the maximum number of open fds. 1024 seems a very common
528 # value for that limit, but Windows has 2048, so we loop
529 # 1024 times (each call leaked two fds).
530 for i in range(1024):
531 try:
532 subprocess.Popen(['nonexisting_i_hope'],
533 stdout=subprocess.PIPE,
534 stderr=subprocess.PIPE)
535 # Windows raises IOError
536 except (IOError, OSError) as err:
537 if err.errno != 2: # ignore "no such file"
538 pass # XXX see #5312
539
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000540 #
541 # POSIX tests
542 #
543 if not mswindows:
544 def test_exceptions(self):
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000545 # caught & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000546 try:
547 p = subprocess.Popen([sys.executable, "-c", ""],
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000548 cwd="/this/path/does/not/exist")
Guido van Rossumb940e112007-01-10 16:19:56 +0000549 except OSError as e:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000550 # The attribute child_traceback should contain "os.chdir"
551 # somewhere.
552 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
553 else:
554 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000555
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000556 def _suppress_core_files(self):
557 """Try to prevent core files from being created.
558 Returns previous ulimit if successful, else None.
559 """
560 try:
561 import resource
562 old_limit = resource.getrlimit(resource.RLIMIT_CORE)
563 resource.setrlimit(resource.RLIMIT_CORE, (0,0))
564 return old_limit
565 except (ImportError, ValueError, resource.error):
566 return None
567
568 def _unsuppress_core_files(self, old_limit):
569 """Return core file behavior to default."""
570 if old_limit is None:
571 return
572 try:
573 import resource
574 resource.setrlimit(resource.RLIMIT_CORE, old_limit)
575 except (ImportError, ValueError, resource.error):
576 return
577
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000578 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000579 # returncode handles signal termination
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000580 old_limit = self._suppress_core_files()
581 try:
582 p = subprocess.Popen([sys.executable,
583 "-c", "import os; os.abort()"])
584 finally:
585 self._unsuppress_core_files(old_limit)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000586 p.wait()
587 self.assertEqual(-p.returncode, signal.SIGABRT)
588
589 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000590 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000591 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000592 'import sys,os;'
593 'sys.stdout.write(os.getenv("FRUIT"))'],
594 stdout=subprocess.PIPE,
595 preexec_fn=lambda: os.putenv("FRUIT",
596 "apple"))
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000597 self.assertEqual(p.stdout.read(), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000598
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000599 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000600 # args is a string
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000601 fd, fname = self.mkstemp()
602 # reopen in text mode
603 with open(fd, "w") as fobj:
604 fobj.write("#!/bin/sh\n")
605 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
606 sys.executable)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000607 os.chmod(fname, 0o700)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000608 p = subprocess.Popen(fname)
609 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000610 os.remove(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000611 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000612
613 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000614 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000615 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000616 [sys.executable,
617 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000618 startupinfo=47)
619 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000620 [sys.executable,
621 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000622 creationflags=47)
623
624 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000625 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000626 newenv = os.environ.copy()
627 newenv["FRUIT"] = "apple"
628 p = subprocess.Popen(["echo $FRUIT"], shell=1,
629 stdout=subprocess.PIPE,
630 env=newenv)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000631 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000632
633 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000634 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000635 newenv = os.environ.copy()
636 newenv["FRUIT"] = "apple"
637 p = subprocess.Popen("echo $FRUIT", shell=1,
638 stdout=subprocess.PIPE,
639 env=newenv)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000640 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000641
642 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000643 # call() function with string argument on UNIX
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000644 fd, fname = self.mkstemp()
645 # reopen in text mode
646 with open(fd, "w") as fobj:
647 fobj.write("#!/bin/sh\n")
648 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
649 sys.executable)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000650 os.chmod(fname, 0o700)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000651 rc = subprocess.call(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000652 os.remove(fname)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000653 self.assertEqual(rc, 47)
654
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000655 def DISABLED_test_send_signal(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000656 p = subprocess.Popen([sys.executable,
657 "-c", "input()"])
658
659 self.assert_(p.poll() is None, p.poll())
660 p.send_signal(signal.SIGINT)
661 self.assertNotEqual(p.wait(), 0)
662
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000663 def DISABLED_test_kill(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000664 p = subprocess.Popen([sys.executable,
665 "-c", "input()"])
666
667 self.assert_(p.poll() is None, p.poll())
668 p.kill()
669 self.assertEqual(p.wait(), -signal.SIGKILL)
670
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000671 def DISABLED_test_terminate(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000672 p = subprocess.Popen([sys.executable,
673 "-c", "input()"])
674
675 self.assert_(p.poll() is None, p.poll())
676 p.terminate()
677 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000678
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000679 #
680 # Windows tests
681 #
682 if mswindows:
683 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000684 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000685 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000686 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000687 STARTF_USESHOWWINDOW = 1
688 SW_MAXIMIZE = 3
689 startupinfo = subprocess.STARTUPINFO()
690 startupinfo.dwFlags = STARTF_USESHOWWINDOW
691 startupinfo.wShowWindow = SW_MAXIMIZE
692 # Since Python is a console process, it won't be affected
693 # by wShowWindow, but the argument should be silently
694 # ignored
695 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
696 startupinfo=startupinfo)
697
698 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000699 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000700 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000701 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000702 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000703 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000704 creationflags=CREATE_NEW_CONSOLE)
705
706 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000707 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000708 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000709 [sys.executable,
710 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000711 preexec_fn=lambda: 1)
712 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000713 [sys.executable,
714 "-c", "import sys; sys.exit(47)"],
Guido van Rossume7ba4952007-06-06 23:52:48 +0000715 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000716 close_fds=True)
717
Guido van Rossume7ba4952007-06-06 23:52:48 +0000718 def test_close_fds(self):
719 # close file descriptors
720 rc = subprocess.call([sys.executable, "-c",
721 "import sys; sys.exit(47)"],
722 close_fds=True)
723 self.assertEqual(rc, 47)
724
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000725 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000726 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000727 newenv = os.environ.copy()
728 newenv["FRUIT"] = "physalis"
729 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000730 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000731 env=newenv)
Guido van Rossumc12a8132007-10-26 04:29:23 +0000732 self.assertNotEqual(p.stdout.read().find(b"physalis"), -1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000733
734 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000735 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000736 newenv = os.environ.copy()
737 newenv["FRUIT"] = "physalis"
738 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000739 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000740 env=newenv)
Guido van Rossumc12a8132007-10-26 04:29:23 +0000741 self.assertNotEqual(p.stdout.read().find(b"physalis"), -1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000742
743 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000744 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000745 rc = subprocess.call(sys.executable +
746 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000747 self.assertEqual(rc, 47)
748
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000749 def DISABLED_test_send_signal(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000750 p = subprocess.Popen([sys.executable,
751 "-c", "input()"])
752
753 self.assert_(p.poll() is None, p.poll())
754 p.send_signal(signal.SIGTERM)
755 self.assertNotEqual(p.wait(), 0)
756
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000757 def DISABLED_test_kill(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000758 p = subprocess.Popen([sys.executable,
759 "-c", "input()"])
760
761 self.assert_(p.poll() is None, p.poll())
762 p.kill()
763 self.assertNotEqual(p.wait(), 0)
764
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000765 def DISABLED_test_terminate(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000766 p = subprocess.Popen([sys.executable,
767 "-c", "input()"])
768
769 self.assert_(p.poll() is None, p.poll())
770 p.terminate()
771 self.assertNotEqual(p.wait(), 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000772
Brett Cannona23810f2008-05-26 19:04:21 +0000773class CommandTests(unittest.TestCase):
774# The module says:
775# "NB This only works (and is only relevant) for UNIX."
776#
777# Actually, getoutput should work on any platform with an os.popen, but
778# I'll take the comment as given, and skip this suite.
779 if os.name == 'posix':
780
781 def test_getoutput(self):
782 self.assertEquals(subprocess.getoutput('echo xyzzy'), 'xyzzy')
783 self.assertEquals(subprocess.getstatusoutput('echo xyzzy'),
784 (0, 'xyzzy'))
785
786 # we use mkdtemp in the next line to create an empty directory
787 # under our exclusive control; from that, we can invent a pathname
788 # that we _know_ won't exist. This is guaranteed to fail.
789 dir = None
790 try:
791 dir = tempfile.mkdtemp()
792 name = os.path.join(dir, "foo")
793
794 status, output = subprocess.getstatusoutput('cat ' + name)
795 self.assertNotEquals(status, 0)
796 finally:
797 if dir is not None:
798 os.rmdir(dir)
799
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000800def test_main():
Brett Cannona23810f2008-05-26 19:04:21 +0000801 support.run_unittest(ProcessTestCase, CommandTests)
802 support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000803
804if __name__ == "__main__":
Brett Cannona23810f2008-05-26 19:04:21 +0000805 test_main()