blob: 0b1fe2525d6e0e41d01120e187ce15e72b55a656 [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
Ezio Melotti184bdfb2010-02-18 09:37:05 +000010import sysconfig
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000011
12mswindows = (sys.platform == "win32")
13
14#
15# Depends on the following external programs: Python
16#
17
18if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000019 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
20 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000021else:
22 SETBINARY = ''
23
Tim Peters3761e8d2004-10-13 04:07:12 +000024# In a debug build, stuff like "[6580 refs]" is printed to stderr at
25# shutdown time. That frustrates tests trying to check stderr produced
26# from a spawned Python process.
27def remove_stderr_debug_decorations(stderr):
Guido van Rossum98297ee2007-11-06 21:34:58 +000028 return re.sub("\[\d+ refs\]\r?\n?$", "", stderr.decode()).encode()
29 #return re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
Tim Peters3761e8d2004-10-13 04:07:12 +000030
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000031class ProcessTestCase(unittest.TestCase):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000032 def setUp(self):
33 # Try to minimize the number of children we have so this test
34 # doesn't crash on some buildbots (Alphas in particular).
Benjamin Petersonee8712c2008-05-20 21:35:26 +000035 if hasattr(support, "reap_children"):
36 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000037
38 def tearDown(self):
39 # Try to minimize the number of children we have so this test
40 # doesn't crash on some buildbots (Alphas in particular).
Benjamin Petersonee8712c2008-05-20 21:35:26 +000041 if hasattr(support, "reap_children"):
42 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000043
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000044 def mkstemp(self):
45 """wrapper for mkstemp, calling mktemp if mkstemp is not available"""
46 if hasattr(tempfile, "mkstemp"):
47 return tempfile.mkstemp()
48 else:
49 fname = tempfile.mktemp()
50 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
Tim Peterse718f612004-10-12 21:51:32 +000051
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000052 #
53 # Generic tests
54 #
55 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000056 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000057 rc = subprocess.call([sys.executable, "-c",
58 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000059 self.assertEqual(rc, 47)
60
Peter Astrand454f7672005-01-01 09:36:35 +000061 def test_check_call_zero(self):
62 # check_call() function with zero return code
63 rc = subprocess.check_call([sys.executable, "-c",
64 "import sys; sys.exit(0)"])
65 self.assertEqual(rc, 0)
66
67 def test_check_call_nonzero(self):
68 # check_call() function with non-zero return code
69 try:
70 subprocess.check_call([sys.executable, "-c",
71 "import sys; sys.exit(47)"])
Guido van Rossumb940e112007-01-10 16:19:56 +000072 except subprocess.CalledProcessError as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000073 self.assertEqual(e.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000074 else:
75 self.fail("Expected CalledProcessError")
76
Georg Brandlf9734072008-12-07 15:30:06 +000077 def test_check_output(self):
78 # check_output() function with zero return code
79 output = subprocess.check_output(
80 [sys.executable, "-c", "print('BDFL')"])
Benjamin Peterson577473f2010-01-19 00:09:57 +000081 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +000082
83 def test_check_output_nonzero(self):
84 # check_call() function with non-zero return code
85 try:
86 subprocess.check_output(
87 [sys.executable, "-c", "import sys; sys.exit(5)"])
88 except subprocess.CalledProcessError as e:
89 self.assertEqual(e.returncode, 5)
90 else:
91 self.fail("Expected CalledProcessError")
92
93 def test_check_output_stderr(self):
94 # check_output() function stderr redirected to stdout
95 output = subprocess.check_output(
96 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
97 stderr=subprocess.STDOUT)
Benjamin Peterson577473f2010-01-19 00:09:57 +000098 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +000099
100 def test_check_output_stdout_arg(self):
101 # check_output() function stderr redirected to stdout
102 try:
103 output = subprocess.check_output(
104 [sys.executable, "-c", "print('will not be run')"],
105 stdout=sys.stdout)
106 except ValueError as e:
Benjamin Peterson577473f2010-01-19 00:09:57 +0000107 self.assertIn('stdout', e.args[0])
Georg Brandlf9734072008-12-07 15:30:06 +0000108 else:
109 self.fail("Expected ValueError when stdout arg supplied.")
110
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000111 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000112 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000113 newenv = os.environ.copy()
114 newenv["FRUIT"] = "banana"
115 rc = subprocess.call([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000116 'import sys, os;'
117 'sys.exit(os.getenv("FRUIT")=="banana")'],
118 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000119 self.assertEqual(rc, 1)
120
121 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000122 # .stdin is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000123 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000124 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
125 p.wait()
126 self.assertEqual(p.stdin, None)
127
128 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000129 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +0000130 p = subprocess.Popen([sys.executable, "-c",
Georg Brandl88fc6642007-02-09 21:28:07 +0000131 'print(" this bit of output is from a '
Tim Peters4052fe52004-10-13 03:29:54 +0000132 'test of stdout in a different '
Georg Brandl88fc6642007-02-09 21:28:07 +0000133 'process ...")'],
Tim Peters4052fe52004-10-13 03:29:54 +0000134 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000135 p.wait()
136 self.assertEqual(p.stdout, None)
137
138 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000139 # .stderr is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000140 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000141 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
142 p.wait()
143 self.assertEqual(p.stderr, None)
144
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000145 def test_executable_with_cwd(self):
146 python_dir = os.path.dirname(os.path.realpath(sys.executable))
147 p = subprocess.Popen(["somethingyoudonthave", "-c",
148 "import sys; sys.exit(47)"],
149 executable=sys.executable, cwd=python_dir)
150 p.wait()
151 self.assertEqual(p.returncode, 47)
152
153 @unittest.skipIf(sysconfig.is_python_build(),
154 "need an installed Python. See #7774")
155 def test_executable_without_cwd(self):
156 # For a normal installation, it should work without 'cwd'
157 # argument. For test runs in the build directory, see #7774.
158 p = subprocess.Popen(["somethingyoudonthave", "-c",
159 "import sys; sys.exit(47)"],
Tim Peters3b01a702004-10-12 22:19:32 +0000160 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000161 p.wait()
162 self.assertEqual(p.returncode, 47)
163
164 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000165 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000166 p = subprocess.Popen([sys.executable, "-c",
167 'import sys; sys.exit(sys.stdin.read() == "pear")'],
168 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000169 p.stdin.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000170 p.stdin.close()
171 p.wait()
172 self.assertEqual(p.returncode, 1)
173
174 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000175 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000176 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000177 d = tf.fileno()
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000178 os.write(d, b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000179 os.lseek(d, 0, 0)
180 p = subprocess.Popen([sys.executable, "-c",
181 'import sys; sys.exit(sys.stdin.read() == "pear")'],
182 stdin=d)
183 p.wait()
184 self.assertEqual(p.returncode, 1)
185
186 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000187 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000188 tf = tempfile.TemporaryFile()
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000189 tf.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000190 tf.seek(0)
191 p = subprocess.Popen([sys.executable, "-c",
192 'import sys; sys.exit(sys.stdin.read() == "pear")'],
193 stdin=tf)
194 p.wait()
195 self.assertEqual(p.returncode, 1)
196
197 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000198 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000199 p = subprocess.Popen([sys.executable, "-c",
200 'import sys; sys.stdout.write("orange")'],
201 stdout=subprocess.PIPE)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000202 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000203
204 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000205 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000206 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000207 d = tf.fileno()
208 p = subprocess.Popen([sys.executable, "-c",
209 'import sys; sys.stdout.write("orange")'],
210 stdout=d)
211 p.wait()
212 os.lseek(d, 0, 0)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000213 self.assertEqual(os.read(d, 1024), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000214
215 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000216 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000217 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000218 p = subprocess.Popen([sys.executable, "-c",
219 'import sys; sys.stdout.write("orange")'],
220 stdout=tf)
221 p.wait()
222 tf.seek(0)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000223 self.assertEqual(tf.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000224
225 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000226 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000227 p = subprocess.Popen([sys.executable, "-c",
228 'import sys; sys.stderr.write("strawberry")'],
229 stderr=subprocess.PIPE)
Tim Peters3761e8d2004-10-13 04:07:12 +0000230 self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()),
Guido van Rossum98297ee2007-11-06 21:34:58 +0000231 b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000232
233 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000234 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000235 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000236 d = tf.fileno()
237 p = subprocess.Popen([sys.executable, "-c",
238 'import sys; sys.stderr.write("strawberry")'],
239 stderr=d)
240 p.wait()
241 os.lseek(d, 0, 0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000242 self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)),
Guido van Rossum98297ee2007-11-06 21:34:58 +0000243 b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000244
245 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000246 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000247 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000248 p = subprocess.Popen([sys.executable, "-c",
249 'import sys; sys.stderr.write("strawberry")'],
250 stderr=tf)
251 p.wait()
252 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000253 self.assertEqual(remove_stderr_debug_decorations(tf.read()),
Guido van Rossum98297ee2007-11-06 21:34:58 +0000254 b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000255
256 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000257 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000258 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000259 'import sys;'
260 'sys.stdout.write("apple");'
261 'sys.stdout.flush();'
262 'sys.stderr.write("orange")'],
263 stdout=subprocess.PIPE,
264 stderr=subprocess.STDOUT)
Tim Peters3761e8d2004-10-13 04:07:12 +0000265 output = p.stdout.read()
266 stripped = remove_stderr_debug_decorations(output)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000267 self.assertEqual(stripped, b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000268
269 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000270 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000271 tf = tempfile.TemporaryFile()
272 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000273 'import sys;'
274 'sys.stdout.write("apple");'
275 'sys.stdout.flush();'
276 'sys.stderr.write("orange")'],
277 stdout=tf,
278 stderr=tf)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000279 p.wait()
280 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000281 output = tf.read()
282 stripped = remove_stderr_debug_decorations(output)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000283 self.assertEqual(stripped, b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000284
Thomas Wouters89f507f2006-12-13 04:49:30 +0000285 def test_stdout_filedes_of_stdout(self):
286 # stdout is set to 1 (#1531862).
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000287 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), b'.\n'))"
Thomas Wouters89f507f2006-12-13 04:49:30 +0000288 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
289 self.assertEquals(rc, 2)
290
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000291 def test_cwd(self):
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000292 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000293 # We cannot use os.path.realpath to canonicalize the path,
294 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
295 cwd = os.getcwd()
296 os.chdir(tmpdir)
297 tmpdir = os.getcwd()
298 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000299 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000300 'import sys,os;'
301 'sys.stdout.write(os.getcwd())'],
302 stdout=subprocess.PIPE,
303 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000304 normcase = os.path.normcase
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000305 self.assertEqual(normcase(p.stdout.read().decode("utf-8")),
306 normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000307
308 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000309 newenv = os.environ.copy()
310 newenv["FRUIT"] = "orange"
311 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000312 'import sys,os;'
313 'sys.stdout.write(os.getenv("FRUIT"))'],
314 stdout=subprocess.PIPE,
315 env=newenv)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000316 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000317
Peter Astrandcbac93c2005-03-03 20:24:28 +0000318 def test_communicate_stdin(self):
319 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000320 'import sys;'
321 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000322 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000323 p.communicate(b"pear")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000324 self.assertEqual(p.returncode, 1)
325
326 def test_communicate_stdout(self):
327 p = subprocess.Popen([sys.executable, "-c",
328 'import sys; sys.stdout.write("pineapple")'],
329 stdout=subprocess.PIPE)
330 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000331 self.assertEqual(stdout, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000332 self.assertEqual(stderr, None)
333
334 def test_communicate_stderr(self):
335 p = subprocess.Popen([sys.executable, "-c",
336 'import sys; sys.stderr.write("pineapple")'],
337 stderr=subprocess.PIPE)
338 (stdout, stderr) = p.communicate()
339 self.assertEqual(stdout, None)
Brett Cannon653a5ad2005-03-05 06:40:52 +0000340 # When running with a pydebug build, the # of references is outputted
341 # to stderr, so just check if stderr at least started with "pinapple"
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000342 self.assertEqual(remove_stderr_debug_decorations(stderr), b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000343
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000344 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000345 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000346 'import sys,os;'
347 'sys.stderr.write("pineapple");'
348 'sys.stdout.write(sys.stdin.read())'],
349 stdin=subprocess.PIPE,
350 stdout=subprocess.PIPE,
351 stderr=subprocess.PIPE)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000352 (stdout, stderr) = p.communicate(b"banana")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000353 self.assertEqual(stdout, b"banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000354 self.assertEqual(remove_stderr_debug_decorations(stderr),
Guido van Rossum98297ee2007-11-06 21:34:58 +0000355 b"pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000356
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000357 # This test is Linux specific for simplicity to at least have
358 # some coverage. It is not a platform specific bug.
359 if os.path.isdir('/proc/%d/fd' % os.getpid()):
360 # Test for the fd leak reported in http://bugs.python.org/issue2791.
361 def test_communicate_pipe_fd_leak(self):
362 fd_directory = '/proc/%d/fd' % os.getpid()
363 num_fds_before_popen = len(os.listdir(fd_directory))
364 p = subprocess.Popen([sys.executable, '-c', 'print()'],
365 stdout=subprocess.PIPE)
366 p.communicate()
367 num_fds_after_communicate = len(os.listdir(fd_directory))
368 del p
369 num_fds_after_destruction = len(os.listdir(fd_directory))
370 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
371 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
372
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000373 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000374 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000375 p = subprocess.Popen([sys.executable, "-c",
376 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000377 (stdout, stderr) = p.communicate()
378 self.assertEqual(stdout, None)
379 self.assertEqual(stderr, None)
380
381 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000382 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000383 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000384 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000385 x, y = os.pipe()
386 if mswindows:
387 pipe_buf = 512
388 else:
389 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
390 os.close(x)
391 os.close(y)
392 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000393 'import sys,os;'
394 'sys.stdout.write(sys.stdin.read(47));'
395 'sys.stderr.write("xyz"*%d);'
396 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
397 stdin=subprocess.PIPE,
398 stdout=subprocess.PIPE,
399 stderr=subprocess.PIPE)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000400 string_to_write = b"abc"*pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000401 (stdout, stderr) = p.communicate(string_to_write)
402 self.assertEqual(stdout, string_to_write)
403
404 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000405 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000406 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000407 'import sys,os;'
408 'sys.stdout.write(sys.stdin.read())'],
409 stdin=subprocess.PIPE,
410 stdout=subprocess.PIPE,
411 stderr=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000412 p.stdin.write(b"banana")
413 (stdout, stderr) = p.communicate(b"split")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000414 self.assertEqual(stdout, b"bananasplit")
Guido van Rossum98297ee2007-11-06 21:34:58 +0000415 self.assertEqual(remove_stderr_debug_decorations(stderr), b"")
Tim Peterse718f612004-10-12 21:51:32 +0000416
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000417 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000418 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000419 'import sys,os;' + SETBINARY +
420 'sys.stdout.write("line1\\n");'
421 'sys.stdout.flush();'
422 'sys.stdout.write("line2\\n");'
423 'sys.stdout.flush();'
424 'sys.stdout.write("line3\\r\\n");'
425 'sys.stdout.flush();'
426 'sys.stdout.write("line4\\r");'
427 'sys.stdout.flush();'
428 'sys.stdout.write("\\nline5");'
429 'sys.stdout.flush();'
430 'sys.stdout.write("\\nline6");'],
431 stdout=subprocess.PIPE,
432 universal_newlines=1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000433 stdout = p.stdout.read()
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000434 self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000435
436 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000437 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000438 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000439 'import sys,os;' + SETBINARY +
440 'sys.stdout.write("line1\\n");'
441 'sys.stdout.flush();'
442 'sys.stdout.write("line2\\n");'
443 'sys.stdout.flush();'
444 'sys.stdout.write("line3\\r\\n");'
445 'sys.stdout.flush();'
446 'sys.stdout.write("line4\\r");'
447 'sys.stdout.flush();'
448 'sys.stdout.write("\\nline5");'
449 'sys.stdout.flush();'
450 'sys.stdout.write("\\nline6");'],
451 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
452 universal_newlines=1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000453 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000454 self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000455
456 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000457 # Make sure we leak no resources
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000458 if (not hasattr(support, "is_resource_enabled") or
459 support.is_resource_enabled("subprocess") and not mswindows):
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000460 max_handles = 1026 # too much for most UNIX systems
461 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000462 max_handles = 65
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000463 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000464 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000465 "import sys;"
466 "sys.stdout.write(sys.stdin.read())"],
467 stdin=subprocess.PIPE,
468 stdout=subprocess.PIPE,
469 stderr=subprocess.PIPE)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000470 data = p.communicate(b"lime")[0]
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000471 self.assertEqual(data, b"lime")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000472
473
474 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000475 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
476 '"a b c" d e')
477 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
478 'ab\\"c \\ d')
Christian Heimesfdab48e2008-01-20 09:06:41 +0000479 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
480 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000481 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
482 'a\\\\\\b "de fg" h')
483 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
484 'a\\\\\\"b c d')
485 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
486 '"a\\\\b c" d e')
487 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
488 '"a\\\\b\\ c" d e')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000489 self.assertEqual(subprocess.list2cmdline(['ab', '']),
490 'ab ""')
Christian Heimesfdab48e2008-01-20 09:06:41 +0000491 self.assertEqual(subprocess.list2cmdline(['echo', 'foo|bar']),
492 'echo "foo|bar"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000493
494
495 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000496 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000497 "-c", "import time; time.sleep(1)"])
498 count = 0
499 while p.poll() is None:
500 time.sleep(0.1)
501 count += 1
502 # We expect that the poll loop probably went around about 10 times,
503 # but, based on system scheduling we can't control, it's possible
504 # poll() never returned None. It "should be" very rare that it
505 # didn't go around at least twice.
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000506 self.assertTrue(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000507 # Subsequent invocations should just return the returncode
508 self.assertEqual(p.poll(), 0)
509
510
511 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000512 p = subprocess.Popen([sys.executable,
513 "-c", "import time; time.sleep(2)"])
514 self.assertEqual(p.wait(), 0)
515 # Subsequent invocations should just return the returncode
516 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000517
Peter Astrand738131d2004-11-30 21:04:45 +0000518
519 def test_invalid_bufsize(self):
520 # an invalid type of the bufsize argument should raise
521 # TypeError.
522 try:
523 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
524 except TypeError:
525 pass
526 else:
527 self.fail("Expected TypeError")
528
Guido van Rossum46a05a72007-06-07 21:56:45 +0000529 def test_bufsize_is_none(self):
530 # bufsize=None should be the same as bufsize=0.
531 p = subprocess.Popen([sys.executable, "-c", "pass"], None)
532 self.assertEqual(p.wait(), 0)
533 # Again with keyword arg
534 p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None)
535 self.assertEqual(p.wait(), 0)
536
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000537 def test_leaking_fds_on_error(self):
538 # see bug #5179: Popen leaks file descriptors to PIPEs if
539 # the child fails to execute; this will eventually exhaust
540 # the maximum number of open fds. 1024 seems a very common
541 # value for that limit, but Windows has 2048, so we loop
542 # 1024 times (each call leaked two fds).
543 for i in range(1024):
544 try:
545 subprocess.Popen(['nonexisting_i_hope'],
546 stdout=subprocess.PIPE,
547 stderr=subprocess.PIPE)
548 # Windows raises IOError
549 except (IOError, OSError) as err:
550 if err.errno != 2: # ignore "no such file"
Benjamin Peterson4b068192009-02-20 03:19:25 +0000551 raise
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000552
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000553 #
554 # POSIX tests
555 #
556 if not mswindows:
557 def test_exceptions(self):
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000558 # caught & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000559 try:
560 p = subprocess.Popen([sys.executable, "-c", ""],
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000561 cwd="/this/path/does/not/exist")
Guido van Rossumb940e112007-01-10 16:19:56 +0000562 except OSError as e:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000563 # The attribute child_traceback should contain "os.chdir"
564 # somewhere.
565 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
566 else:
567 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000568
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000569 def _suppress_core_files(self):
570 """Try to prevent core files from being created.
571 Returns previous ulimit if successful, else None.
572 """
573 try:
574 import resource
575 old_limit = resource.getrlimit(resource.RLIMIT_CORE)
576 resource.setrlimit(resource.RLIMIT_CORE, (0,0))
577 return old_limit
578 except (ImportError, ValueError, resource.error):
579 return None
580
581 def _unsuppress_core_files(self, old_limit):
582 """Return core file behavior to default."""
583 if old_limit is None:
584 return
585 try:
586 import resource
587 resource.setrlimit(resource.RLIMIT_CORE, old_limit)
588 except (ImportError, ValueError, resource.error):
589 return
590
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000591 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000592 # returncode handles signal termination
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000593 old_limit = self._suppress_core_files()
594 try:
595 p = subprocess.Popen([sys.executable,
596 "-c", "import os; os.abort()"])
597 finally:
598 self._unsuppress_core_files(old_limit)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000599 p.wait()
600 self.assertEqual(-p.returncode, signal.SIGABRT)
601
602 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000603 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000604 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000605 'import sys,os;'
606 'sys.stdout.write(os.getenv("FRUIT"))'],
607 stdout=subprocess.PIPE,
608 preexec_fn=lambda: os.putenv("FRUIT",
609 "apple"))
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000610 self.assertEqual(p.stdout.read(), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000611
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000612 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000613 # args is a string
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000614 fd, fname = self.mkstemp()
615 # reopen in text mode
616 with open(fd, "w") as fobj:
617 fobj.write("#!/bin/sh\n")
618 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
619 sys.executable)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000620 os.chmod(fname, 0o700)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000621 p = subprocess.Popen(fname)
622 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000623 os.remove(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000624 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000625
626 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000627 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000628 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000629 [sys.executable,
630 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000631 startupinfo=47)
632 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000633 [sys.executable,
634 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000635 creationflags=47)
636
637 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000638 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000639 newenv = os.environ.copy()
640 newenv["FRUIT"] = "apple"
641 p = subprocess.Popen(["echo $FRUIT"], shell=1,
642 stdout=subprocess.PIPE,
643 env=newenv)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000644 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000645
646 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000647 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000648 newenv = os.environ.copy()
649 newenv["FRUIT"] = "apple"
650 p = subprocess.Popen("echo $FRUIT", shell=1,
651 stdout=subprocess.PIPE,
652 env=newenv)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000653 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000654
655 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000656 # call() function with string argument on UNIX
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000657 fd, fname = self.mkstemp()
658 # reopen in text mode
659 with open(fd, "w") as fobj:
660 fobj.write("#!/bin/sh\n")
661 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
662 sys.executable)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000663 os.chmod(fname, 0o700)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000664 rc = subprocess.call(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000665 os.remove(fname)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000666 self.assertEqual(rc, 47)
667
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000668 def DISABLED_test_send_signal(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000669 p = subprocess.Popen([sys.executable,
670 "-c", "input()"])
671
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000672 self.assertTrue(p.poll() is None, p.poll())
Christian Heimesa342c012008-04-20 21:01:16 +0000673 p.send_signal(signal.SIGINT)
674 self.assertNotEqual(p.wait(), 0)
675
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000676 def DISABLED_test_kill(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000677 p = subprocess.Popen([sys.executable,
678 "-c", "input()"])
679
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000680 self.assertTrue(p.poll() is None, p.poll())
Christian Heimesa342c012008-04-20 21:01:16 +0000681 p.kill()
682 self.assertEqual(p.wait(), -signal.SIGKILL)
683
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000684 def DISABLED_test_terminate(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000685 p = subprocess.Popen([sys.executable,
686 "-c", "input()"])
687
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000688 self.assertTrue(p.poll() is None, p.poll())
Christian Heimesa342c012008-04-20 21:01:16 +0000689 p.terminate()
690 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000691
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000692 #
693 # Windows tests
694 #
695 if mswindows:
696 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000697 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000698 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000699 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000700 STARTF_USESHOWWINDOW = 1
701 SW_MAXIMIZE = 3
702 startupinfo = subprocess.STARTUPINFO()
703 startupinfo.dwFlags = STARTF_USESHOWWINDOW
704 startupinfo.wShowWindow = SW_MAXIMIZE
705 # Since Python is a console process, it won't be affected
706 # by wShowWindow, but the argument should be silently
707 # ignored
708 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
709 startupinfo=startupinfo)
710
711 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000712 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000713 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000714 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000715 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000716 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000717 creationflags=CREATE_NEW_CONSOLE)
718
719 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000720 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000721 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000722 [sys.executable,
723 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000724 preexec_fn=lambda: 1)
725 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000726 [sys.executable,
727 "-c", "import sys; sys.exit(47)"],
Guido van Rossume7ba4952007-06-06 23:52:48 +0000728 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000729 close_fds=True)
730
Guido van Rossume7ba4952007-06-06 23:52:48 +0000731 def test_close_fds(self):
732 # close file descriptors
733 rc = subprocess.call([sys.executable, "-c",
734 "import sys; sys.exit(47)"],
735 close_fds=True)
736 self.assertEqual(rc, 47)
737
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000738 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000739 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000740 newenv = os.environ.copy()
741 newenv["FRUIT"] = "physalis"
742 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000743 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000744 env=newenv)
Guido van Rossumc12a8132007-10-26 04:29:23 +0000745 self.assertNotEqual(p.stdout.read().find(b"physalis"), -1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000746
747 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000748 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000749 newenv = os.environ.copy()
750 newenv["FRUIT"] = "physalis"
751 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000752 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000753 env=newenv)
Guido van Rossumc12a8132007-10-26 04:29:23 +0000754 self.assertNotEqual(p.stdout.read().find(b"physalis"), -1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000755
756 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000757 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000758 rc = subprocess.call(sys.executable +
759 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000760 self.assertEqual(rc, 47)
761
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000762 def DISABLED_test_send_signal(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000763 p = subprocess.Popen([sys.executable,
764 "-c", "input()"])
765
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000766 self.assertTrue(p.poll() is None, p.poll())
Christian Heimesa342c012008-04-20 21:01:16 +0000767 p.send_signal(signal.SIGTERM)
768 self.assertNotEqual(p.wait(), 0)
769
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000770 def DISABLED_test_kill(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000771 p = subprocess.Popen([sys.executable,
772 "-c", "input()"])
773
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000774 self.assertTrue(p.poll() is None, p.poll())
Christian Heimesa342c012008-04-20 21:01:16 +0000775 p.kill()
776 self.assertNotEqual(p.wait(), 0)
777
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000778 def DISABLED_test_terminate(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000779 p = subprocess.Popen([sys.executable,
780 "-c", "input()"])
781
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000782 self.assertTrue(p.poll() is None, p.poll())
Christian Heimesa342c012008-04-20 21:01:16 +0000783 p.terminate()
784 self.assertNotEqual(p.wait(), 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000785
Brett Cannona23810f2008-05-26 19:04:21 +0000786class CommandTests(unittest.TestCase):
787# The module says:
788# "NB This only works (and is only relevant) for UNIX."
789#
790# Actually, getoutput should work on any platform with an os.popen, but
791# I'll take the comment as given, and skip this suite.
792 if os.name == 'posix':
793
794 def test_getoutput(self):
795 self.assertEquals(subprocess.getoutput('echo xyzzy'), 'xyzzy')
796 self.assertEquals(subprocess.getstatusoutput('echo xyzzy'),
797 (0, 'xyzzy'))
798
799 # we use mkdtemp in the next line to create an empty directory
800 # under our exclusive control; from that, we can invent a pathname
801 # that we _know_ won't exist. This is guaranteed to fail.
802 dir = None
803 try:
804 dir = tempfile.mkdtemp()
805 name = os.path.join(dir, "foo")
806
807 status, output = subprocess.getstatusoutput('cat ' + name)
808 self.assertNotEquals(status, 0)
809 finally:
810 if dir is not None:
811 os.rmdir(dir)
812
Gregory P. Smithd06fa472009-07-04 02:46:54 +0000813
814unit_tests = [ProcessTestCase, CommandTests]
815
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +0000816if getattr(subprocess, '_has_poll', False):
Gregory P. Smithd06fa472009-07-04 02:46:54 +0000817 class ProcessTestCaseNoPoll(ProcessTestCase):
818 def setUp(self):
819 subprocess._has_poll = False
820 ProcessTestCase.setUp(self)
821
822 def tearDown(self):
823 subprocess._has_poll = True
824 ProcessTestCase.tearDown(self)
825
826 unit_tests.append(ProcessTestCaseNoPoll)
827
828
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000829def test_main():
Gregory P. Smithd06fa472009-07-04 02:46:54 +0000830 support.run_unittest(*unit_tests)
Brett Cannona23810f2008-05-26 19:04:21 +0000831 support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000832
833if __name__ == "__main__":
Brett Cannona23810f2008-05-26 19:04:21 +0000834 test_main()