blob: 1a9fb695168e0c029b7454c16f400ebb741c23c0 [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
Gregory P. Smitha59c59f2010-03-01 00:17:40 +00007import errno
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00008import tempfile
9import time
Tim Peters3761e8d2004-10-13 04:07:12 +000010import re
Ezio Melotti184bdfb2010-02-18 09:37:05 +000011import sysconfig
Gregory P. Smith32ec9da2010-03-19 16:53:08 +000012try:
13 import gc
14except ImportError:
15 gc = None
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000016
17mswindows = (sys.platform == "win32")
18
19#
20# Depends on the following external programs: Python
21#
22
23if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000024 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
25 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000026else:
27 SETBINARY = ''
28
Florent Xiclunab1e94e82010-02-27 22:12:37 +000029
30try:
31 mkstemp = tempfile.mkstemp
32except AttributeError:
33 # tempfile.mkstemp is not available
34 def mkstemp():
35 """Replacement for mkstemp, calling mktemp."""
36 fname = tempfile.mktemp()
37 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
38
Tim Peters3761e8d2004-10-13 04:07:12 +000039
Florent Xiclunac049d872010-03-27 22:47:23 +000040class BaseTestCase(unittest.TestCase):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000041 def setUp(self):
42 # Try to minimize the number of children we have so this test
43 # doesn't crash on some buildbots (Alphas in particular).
Florent Xiclunab1e94e82010-02-27 22:12:37 +000044 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000045
Florent Xiclunaf0cbd822010-03-04 21:50:56 +000046 def tearDown(self):
47 for inst in subprocess._active:
48 inst.wait()
49 subprocess._cleanup()
50 self.assertFalse(subprocess._active, "subprocess._active not empty")
51
Florent Xiclunab1e94e82010-02-27 22:12:37 +000052 def assertStderrEqual(self, stderr, expected, msg=None):
53 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
54 # shutdown time. That frustrates tests trying to check stderr produced
55 # from a spawned Python process.
Antoine Pitrou62f68ed2010-08-04 11:48:56 +000056 actual = support.strip_python_stderr(stderr)
Florent Xiclunab1e94e82010-02-27 22:12:37 +000057 self.assertEqual(actual, expected, msg)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058
Florent Xiclunac049d872010-03-27 22:47:23 +000059
60class ProcessTestCase(BaseTestCase):
61
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000062 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000063 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000064 rc = subprocess.call([sys.executable, "-c",
65 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000066 self.assertEqual(rc, 47)
67
Peter Astrand454f7672005-01-01 09:36:35 +000068 def test_check_call_zero(self):
69 # check_call() function with zero return code
70 rc = subprocess.check_call([sys.executable, "-c",
71 "import sys; sys.exit(0)"])
72 self.assertEqual(rc, 0)
73
74 def test_check_call_nonzero(self):
75 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +000076 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +000077 subprocess.check_call([sys.executable, "-c",
78 "import sys; sys.exit(47)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +000079 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000080
Georg Brandlf9734072008-12-07 15:30:06 +000081 def test_check_output(self):
82 # check_output() function with zero return code
83 output = subprocess.check_output(
84 [sys.executable, "-c", "print('BDFL')"])
Benjamin Peterson577473f2010-01-19 00:09:57 +000085 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +000086
87 def test_check_output_nonzero(self):
88 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +000089 with self.assertRaises(subprocess.CalledProcessError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +000090 subprocess.check_output(
91 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +000092 self.assertEqual(c.exception.returncode, 5)
Georg Brandlf9734072008-12-07 15:30:06 +000093
94 def test_check_output_stderr(self):
95 # check_output() function stderr redirected to stdout
96 output = subprocess.check_output(
97 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
98 stderr=subprocess.STDOUT)
Benjamin Peterson577473f2010-01-19 00:09:57 +000099 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +0000100
101 def test_check_output_stdout_arg(self):
102 # check_output() function stderr redirected to stdout
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000103 with self.assertRaises(ValueError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +0000104 output = subprocess.check_output(
105 [sys.executable, "-c", "print('will not be run')"],
106 stdout=sys.stdout)
Georg Brandlf9734072008-12-07 15:30:06 +0000107 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000108 self.assertIn('stdout', c.exception.args[0])
Georg Brandlf9734072008-12-07 15:30:06 +0000109
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)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000124 self.addCleanup(p.stdout.close)
125 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000126 p.wait()
127 self.assertEqual(p.stdin, None)
128
129 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000130 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +0000131 p = subprocess.Popen([sys.executable, "-c",
Georg Brandl88fc6642007-02-09 21:28:07 +0000132 'print(" this bit of output is from a '
Tim Peters4052fe52004-10-13 03:29:54 +0000133 'test of stdout in a different '
Georg Brandl88fc6642007-02-09 21:28:07 +0000134 'process ...")'],
Tim Peters4052fe52004-10-13 03:29:54 +0000135 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000136 self.addCleanup(p.stdin.close)
137 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000138 p.wait()
139 self.assertEqual(p.stdout, None)
140
141 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000142 # .stderr is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000143 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000144 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000145 self.addCleanup(p.stdout.close)
146 self.addCleanup(p.stdin.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000147 p.wait()
148 self.assertEqual(p.stderr, None)
149
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000150 def test_executable_with_cwd(self):
Florent Xicluna1d1ab972010-03-11 01:53:10 +0000151 python_dir = os.path.dirname(os.path.realpath(sys.executable))
Ezio Melotti184bdfb2010-02-18 09:37:05 +0000152 p = subprocess.Popen(["somethingyoudonthave", "-c",
153 "import sys; sys.exit(47)"],
154 executable=sys.executable, cwd=python_dir)
155 p.wait()
156 self.assertEqual(p.returncode, 47)
157
158 @unittest.skipIf(sysconfig.is_python_build(),
159 "need an installed Python. See #7774")
160 def test_executable_without_cwd(self):
161 # For a normal installation, it should work without 'cwd'
162 # argument. For test runs in the build directory, see #7774.
163 p = subprocess.Popen(["somethingyoudonthave", "-c",
164 "import sys; sys.exit(47)"],
Tim Peters3b01a702004-10-12 22:19:32 +0000165 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000166 p.wait()
167 self.assertEqual(p.returncode, 47)
168
169 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000170 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000171 p = subprocess.Popen([sys.executable, "-c",
172 'import sys; sys.exit(sys.stdin.read() == "pear")'],
173 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000174 p.stdin.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000175 p.stdin.close()
176 p.wait()
177 self.assertEqual(p.returncode, 1)
178
179 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000180 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000181 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000182 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000183 d = tf.fileno()
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000184 os.write(d, b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000185 os.lseek(d, 0, 0)
186 p = subprocess.Popen([sys.executable, "-c",
187 'import sys; sys.exit(sys.stdin.read() == "pear")'],
188 stdin=d)
189 p.wait()
190 self.assertEqual(p.returncode, 1)
191
192 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000193 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000194 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000195 self.addCleanup(tf.close)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000196 tf.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000197 tf.seek(0)
198 p = subprocess.Popen([sys.executable, "-c",
199 'import sys; sys.exit(sys.stdin.read() == "pear")'],
200 stdin=tf)
201 p.wait()
202 self.assertEqual(p.returncode, 1)
203
204 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000205 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000206 p = subprocess.Popen([sys.executable, "-c",
207 'import sys; sys.stdout.write("orange")'],
208 stdout=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000209 self.addCleanup(p.stdout.close)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000210 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000211
212 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000213 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000214 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000215 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000216 d = tf.fileno()
217 p = subprocess.Popen([sys.executable, "-c",
218 'import sys; sys.stdout.write("orange")'],
219 stdout=d)
220 p.wait()
221 os.lseek(d, 0, 0)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000222 self.assertEqual(os.read(d, 1024), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000223
224 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000225 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000226 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000227 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000228 p = subprocess.Popen([sys.executable, "-c",
229 'import sys; sys.stdout.write("orange")'],
230 stdout=tf)
231 p.wait()
232 tf.seek(0)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000233 self.assertEqual(tf.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000234
235 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000236 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000237 p = subprocess.Popen([sys.executable, "-c",
238 'import sys; sys.stderr.write("strawberry")'],
239 stderr=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000240 self.addCleanup(p.stderr.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000241 self.assertStderrEqual(p.stderr.read(), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000242
243 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000244 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000245 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000246 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000247 d = tf.fileno()
248 p = subprocess.Popen([sys.executable, "-c",
249 'import sys; sys.stderr.write("strawberry")'],
250 stderr=d)
251 p.wait()
252 os.lseek(d, 0, 0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000253 self.assertStderrEqual(os.read(d, 1024), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000254
255 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000256 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000257 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000258 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000259 p = subprocess.Popen([sys.executable, "-c",
260 'import sys; sys.stderr.write("strawberry")'],
261 stderr=tf)
262 p.wait()
263 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000264 self.assertStderrEqual(tf.read(), b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000265
266 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000267 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000268 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000269 'import sys;'
270 'sys.stdout.write("apple");'
271 'sys.stdout.flush();'
272 'sys.stderr.write("orange")'],
273 stdout=subprocess.PIPE,
274 stderr=subprocess.STDOUT)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000275 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000276 self.assertStderrEqual(p.stdout.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000277
278 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000279 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000280 tf = tempfile.TemporaryFile()
Benjamin Petersoncc221b22010-10-31 02:06:21 +0000281 self.addCleanup(tf.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000282 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000283 'import sys;'
284 'sys.stdout.write("apple");'
285 'sys.stdout.flush();'
286 'sys.stderr.write("orange")'],
287 stdout=tf,
288 stderr=tf)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000289 p.wait()
290 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000291 self.assertStderrEqual(tf.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000292
Thomas Wouters89f507f2006-12-13 04:49:30 +0000293 def test_stdout_filedes_of_stdout(self):
294 # stdout is set to 1 (#1531862).
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000295 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), b'.\n'))"
Thomas Wouters89f507f2006-12-13 04:49:30 +0000296 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000297 self.assertEqual(rc, 2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000298
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000299 def test_cwd(self):
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000300 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000301 # We cannot use os.path.realpath to canonicalize the path,
302 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
303 cwd = os.getcwd()
304 os.chdir(tmpdir)
305 tmpdir = os.getcwd()
306 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000307 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000308 'import sys,os;'
309 'sys.stdout.write(os.getcwd())'],
310 stdout=subprocess.PIPE,
311 cwd=tmpdir)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000312 self.addCleanup(p.stdout.close)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000313 normcase = os.path.normcase
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000314 self.assertEqual(normcase(p.stdout.read().decode("utf-8")),
315 normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000316
317 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000318 newenv = os.environ.copy()
319 newenv["FRUIT"] = "orange"
320 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000321 'import sys,os;'
322 'sys.stdout.write(os.getenv("FRUIT"))'],
323 stdout=subprocess.PIPE,
324 env=newenv)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000325 self.addCleanup(p.stdout.close)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000326 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000327
Peter Astrandcbac93c2005-03-03 20:24:28 +0000328 def test_communicate_stdin(self):
329 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000330 'import sys;'
331 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000332 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000333 p.communicate(b"pear")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000334 self.assertEqual(p.returncode, 1)
335
336 def test_communicate_stdout(self):
337 p = subprocess.Popen([sys.executable, "-c",
338 'import sys; sys.stdout.write("pineapple")'],
339 stdout=subprocess.PIPE)
340 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000341 self.assertEqual(stdout, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000342 self.assertEqual(stderr, None)
343
344 def test_communicate_stderr(self):
345 p = subprocess.Popen([sys.executable, "-c",
346 'import sys; sys.stderr.write("pineapple")'],
347 stderr=subprocess.PIPE)
348 (stdout, stderr) = p.communicate()
349 self.assertEqual(stdout, None)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000350 self.assertStderrEqual(stderr, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000351
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000352 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000353 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000354 'import sys,os;'
355 'sys.stderr.write("pineapple");'
356 'sys.stdout.write(sys.stdin.read())'],
357 stdin=subprocess.PIPE,
358 stdout=subprocess.PIPE,
359 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000360 self.addCleanup(p.stdout.close)
361 self.addCleanup(p.stderr.close)
362 self.addCleanup(p.stdin.close)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000363 (stdout, stderr) = p.communicate(b"banana")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000364 self.assertEqual(stdout, b"banana")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000365 self.assertStderrEqual(stderr, b"pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000366
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000367 # This test is Linux specific for simplicity to at least have
368 # some coverage. It is not a platform specific bug.
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000369 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
370 "Linux specific")
371 # Test for the fd leak reported in http://bugs.python.org/issue2791.
372 def test_communicate_pipe_fd_leak(self):
373 fd_directory = '/proc/%d/fd' % os.getpid()
374 num_fds_before_popen = len(os.listdir(fd_directory))
375 p = subprocess.Popen([sys.executable, "-c", "print()"],
376 stdout=subprocess.PIPE)
377 p.communicate()
378 num_fds_after_communicate = len(os.listdir(fd_directory))
379 del p
380 num_fds_after_destruction = len(os.listdir(fd_directory))
381 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
382 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000383
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000384 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000385 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000386 p = subprocess.Popen([sys.executable, "-c",
387 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000388 (stdout, stderr) = p.communicate()
389 self.assertEqual(stdout, None)
390 self.assertEqual(stderr, None)
391
392 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000393 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000394 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000395 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000396 x, y = os.pipe()
397 if mswindows:
398 pipe_buf = 512
399 else:
400 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
401 os.close(x)
402 os.close(y)
403 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000404 'import sys,os;'
405 'sys.stdout.write(sys.stdin.read(47));'
406 'sys.stderr.write("xyz"*%d);'
407 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
408 stdin=subprocess.PIPE,
409 stdout=subprocess.PIPE,
410 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000411 self.addCleanup(p.stdout.close)
412 self.addCleanup(p.stderr.close)
413 self.addCleanup(p.stdin.close)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000414 string_to_write = b"abc"*pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000415 (stdout, stderr) = p.communicate(string_to_write)
416 self.assertEqual(stdout, string_to_write)
417
418 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000419 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000420 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000421 'import sys,os;'
422 'sys.stdout.write(sys.stdin.read())'],
423 stdin=subprocess.PIPE,
424 stdout=subprocess.PIPE,
425 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +0000426 self.addCleanup(p.stdout.close)
427 self.addCleanup(p.stderr.close)
428 self.addCleanup(p.stdin.close)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000429 p.stdin.write(b"banana")
430 (stdout, stderr) = p.communicate(b"split")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000431 self.assertEqual(stdout, b"bananasplit")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000432 self.assertStderrEqual(stderr, b"")
Tim Peterse718f612004-10-12 21:51:32 +0000433
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000434 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000435 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000436 'import sys,os;' + SETBINARY +
437 'sys.stdout.write("line1\\n");'
438 'sys.stdout.flush();'
439 'sys.stdout.write("line2\\n");'
440 'sys.stdout.flush();'
441 'sys.stdout.write("line3\\r\\n");'
442 'sys.stdout.flush();'
443 'sys.stdout.write("line4\\r");'
444 'sys.stdout.flush();'
445 'sys.stdout.write("\\nline5");'
446 'sys.stdout.flush();'
447 'sys.stdout.write("\\nline6");'],
448 stdout=subprocess.PIPE,
449 universal_newlines=1)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000450 self.addCleanup(p.stdout.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000451 stdout = p.stdout.read()
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000452 self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000453
454 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000455 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000456 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000457 'import sys,os;' + SETBINARY +
458 'sys.stdout.write("line1\\n");'
459 'sys.stdout.flush();'
460 'sys.stdout.write("line2\\n");'
461 'sys.stdout.flush();'
462 'sys.stdout.write("line3\\r\\n");'
463 'sys.stdout.flush();'
464 'sys.stdout.write("line4\\r");'
465 'sys.stdout.flush();'
466 'sys.stdout.write("\\nline5");'
467 'sys.stdout.flush();'
468 'sys.stdout.write("\\nline6");'],
469 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
470 universal_newlines=1)
Brian Curtin19a53792010-11-05 17:09:05 +0000471 self.addCleanup(p.stdout.close)
472 self.addCleanup(p.stderr.close)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000473 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000474 self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000475
476 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000477 # Make sure we leak no resources
Antoine Pitrou8db30272010-09-18 22:38:48 +0000478 if not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000479 max_handles = 1026 # too much for most UNIX systems
480 else:
Antoine Pitrou8db30272010-09-18 22:38:48 +0000481 max_handles = 2050 # too much for (at least some) Windows setups
482 handles = []
483 try:
484 for i in range(max_handles):
485 try:
486 handles.append(os.open(support.TESTFN,
487 os.O_WRONLY | os.O_CREAT))
488 except OSError as e:
489 if e.errno != errno.EMFILE:
490 raise
491 break
492 else:
493 self.skipTest("failed to reach the file descriptor limit "
494 "(tried %d)" % max_handles)
495 # Close a couple of them (should be enough for a subprocess)
496 for i in range(10):
497 os.close(handles.pop())
498 # Loop creating some subprocesses. If one of them leaks some fds,
499 # the next loop iteration will fail by reaching the max fd limit.
500 for i in range(15):
501 p = subprocess.Popen([sys.executable, "-c",
502 "import sys;"
503 "sys.stdout.write(sys.stdin.read())"],
504 stdin=subprocess.PIPE,
505 stdout=subprocess.PIPE,
506 stderr=subprocess.PIPE)
507 data = p.communicate(b"lime")[0]
508 self.assertEqual(data, b"lime")
509 finally:
510 for h in handles:
511 os.close(h)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000512
513 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000514 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
515 '"a b c" d e')
516 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
517 'ab\\"c \\ d')
Christian Heimesfdab48e2008-01-20 09:06:41 +0000518 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
519 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000520 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
521 'a\\\\\\b "de fg" h')
522 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
523 'a\\\\\\"b c d')
524 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
525 '"a\\\\b c" d e')
526 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
527 '"a\\\\b\\ c" d e')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000528 self.assertEqual(subprocess.list2cmdline(['ab', '']),
529 'ab ""')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000530
531
532 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000533 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000534 "-c", "import time; time.sleep(1)"])
535 count = 0
536 while p.poll() is None:
537 time.sleep(0.1)
538 count += 1
539 # We expect that the poll loop probably went around about 10 times,
540 # but, based on system scheduling we can't control, it's possible
541 # poll() never returned None. It "should be" very rare that it
542 # didn't go around at least twice.
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000543 self.assertGreaterEqual(count, 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000544 # Subsequent invocations should just return the returncode
545 self.assertEqual(p.poll(), 0)
546
547
548 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000549 p = subprocess.Popen([sys.executable,
550 "-c", "import time; time.sleep(2)"])
551 self.assertEqual(p.wait(), 0)
552 # Subsequent invocations should just return the returncode
553 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000554
Peter Astrand738131d2004-11-30 21:04:45 +0000555
556 def test_invalid_bufsize(self):
557 # an invalid type of the bufsize argument should raise
558 # TypeError.
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000559 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +0000560 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +0000561
Guido van Rossum46a05a72007-06-07 21:56:45 +0000562 def test_bufsize_is_none(self):
563 # bufsize=None should be the same as bufsize=0.
564 p = subprocess.Popen([sys.executable, "-c", "pass"], None)
565 self.assertEqual(p.wait(), 0)
566 # Again with keyword arg
567 p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None)
568 self.assertEqual(p.wait(), 0)
569
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000570 def test_leaking_fds_on_error(self):
571 # see bug #5179: Popen leaks file descriptors to PIPEs if
572 # the child fails to execute; this will eventually exhaust
573 # the maximum number of open fds. 1024 seems a very common
574 # value for that limit, but Windows has 2048, so we loop
575 # 1024 times (each call leaked two fds).
576 for i in range(1024):
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000577 # Windows raises IOError. Others raise OSError.
578 with self.assertRaises(EnvironmentError) as c:
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000579 subprocess.Popen(['nonexisting_i_hope'],
580 stdout=subprocess.PIPE,
581 stderr=subprocess.PIPE)
Antoine Pitrou679e0f22010-09-18 17:56:02 +0000582 if c.exception.errno != errno.ENOENT: # ignore "no such file"
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000583 raise c.exception
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000584
Victor Stinnerb3693582010-05-21 20:13:12 +0000585 def test_issue8780(self):
586 # Ensure that stdout is inherited from the parent
587 # if stdout=PIPE is not used
588 code = ';'.join((
589 'import subprocess, sys',
590 'retcode = subprocess.call('
591 "[sys.executable, '-c', 'print(\"Hello World!\")'])",
592 'assert retcode == 0'))
593 output = subprocess.check_output([sys.executable, '-c', code])
594 self.assert_(output.startswith(b'Hello World!'), ascii(output))
595
Tim Goldenaf5ac392010-08-06 13:03:56 +0000596 def test_handles_closed_on_exception(self):
597 # If CreateProcess exits with an error, ensure the
598 # duplicate output handles are released
599 ifhandle, ifname = mkstemp()
600 ofhandle, ofname = mkstemp()
601 efhandle, efname = mkstemp()
602 try:
603 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
604 stderr=efhandle)
605 except OSError:
606 os.close(ifhandle)
607 os.remove(ifname)
608 os.close(ofhandle)
609 os.remove(ofname)
610 os.close(efhandle)
611 os.remove(efname)
612 self.assertFalse(os.path.exists(ifname))
613 self.assertFalse(os.path.exists(ofname))
614 self.assertFalse(os.path.exists(efname))
615
Tim Peterse718f612004-10-12 21:51:32 +0000616
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000617# context manager
618class _SuppressCoreFiles(object):
619 """Try to prevent core files from being created."""
620 old_limit = None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000621
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000622 def __enter__(self):
623 """Try to save previous ulimit, then set it to (0, 0)."""
624 try:
625 import resource
626 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
627 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
628 except (ImportError, ValueError, resource.error):
629 pass
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000630
Ronald Oussoren102d11a2010-07-23 09:50:05 +0000631 if sys.platform == 'darwin':
632 # Check if the 'Crash Reporter' on OSX was configured
633 # in 'Developer' mode and warn that it will get triggered
634 # when it is.
635 #
636 # This assumes that this context manager is used in tests
637 # that might trigger the next manager.
638 value = subprocess.Popen(['/usr/bin/defaults', 'read',
639 'com.apple.CrashReporter', 'DialogType'],
640 stdout=subprocess.PIPE).communicate()[0]
641 if value.strip() == b'developer':
642 print("this tests triggers the Crash Reporter, "
643 "that is intentional", end='')
644 sys.stdout.flush()
645
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000646 def __exit__(self, *args):
647 """Return core file behavior to default."""
648 if self.old_limit is None:
649 return
650 try:
651 import resource
652 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
653 except (ImportError, ValueError, resource.error):
654 pass
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000655
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000656
Florent Xiclunaf0cbd822010-03-04 21:50:56 +0000657@unittest.skipIf(mswindows, "POSIX specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +0000658class POSIXProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +0000659
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000660 def test_exceptions(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000661 nonexistent_dir = "/_this/pa.th/does/not/exist"
662 try:
663 os.chdir(nonexistent_dir)
664 except OSError as e:
665 # This avoids hard coding the errno value or the OS perror()
666 # string and instead capture the exception that we want to see
667 # below for comparison.
668 desired_exception = e
669 else:
670 self.fail("chdir to nonexistant directory %s succeeded." %
671 nonexistent_dir)
672
673 # Error in the child re-raised in the parent.
674 try:
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000675 p = subprocess.Popen([sys.executable, "-c", ""],
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000676 cwd=nonexistent_dir)
677 except OSError as e:
678 # Test that the child process chdir failure actually makes
679 # it up to the parent process as the correct exception.
680 self.assertEqual(desired_exception.errno, e.errno)
681 self.assertEqual(desired_exception.strerror, e.strerror)
682 else:
683 self.fail("Expected OSError: %s" % desired_exception)
684
685 def test_restore_signals(self):
686 # Code coverage for both values of restore_signals to make sure it
687 # at least does not blow up.
688 # A test for behavior would be complex. Contributions welcome.
689 subprocess.call([sys.executable, "-c", ""], restore_signals=True)
690 subprocess.call([sys.executable, "-c", ""], restore_signals=False)
691
692 def test_start_new_session(self):
693 # For code coverage of calling setsid(). We don't care if we get an
694 # EPERM error from it depending on the test execution environment, that
695 # still indicates that it was called.
696 try:
697 output = subprocess.check_output(
698 [sys.executable, "-c",
699 "import os; print(os.getpgid(os.getpid()))"],
700 start_new_session=True)
701 except OSError as e:
702 if e.errno != errno.EPERM:
703 raise
704 else:
705 parent_pgid = os.getpgid(os.getpid())
706 child_pgid = int(output)
707 self.assertNotEqual(parent_pgid, child_pgid)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000708
709 def test_run_abort(self):
710 # returncode handles signal termination
711 with _SuppressCoreFiles():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000712 p = subprocess.Popen([sys.executable, "-c",
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000713 'import os; os.abort()'])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000714 p.wait()
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000715 self.assertEqual(-p.returncode, signal.SIGABRT)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000716
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000717 def test_preexec(self):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000718 # DISCLAIMER: Setting environment variables is *not* a good use
719 # of a preexec_fn. This is merely a test.
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000720 p = subprocess.Popen([sys.executable, "-c",
721 'import sys,os;'
722 'sys.stdout.write(os.getenv("FRUIT"))'],
723 stdout=subprocess.PIPE,
724 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Brian Curtin3c6a9512010-11-05 03:58:52 +0000725 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000726 self.assertEqual(p.stdout.read(), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000727
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000728 def test_preexec_exception(self):
729 def raise_it():
730 raise ValueError("What if two swallows carried a coconut?")
731 try:
732 p = subprocess.Popen([sys.executable, "-c", ""],
733 preexec_fn=raise_it)
734 except RuntimeError as e:
735 self.assertTrue(
736 subprocess._posixsubprocess,
737 "Expected a ValueError from the preexec_fn")
738 except ValueError as e:
739 self.assertIn("coconut", e.args[0])
740 else:
741 self.fail("Exception raised by preexec_fn did not make it "
742 "to the parent process.")
743
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000744 @unittest.skipUnless(gc, "Requires a gc module.")
745 def test_preexec_gc_module_failure(self):
746 # This tests the code that disables garbage collection if the child
747 # process will execute any Python.
748 def raise_runtime_error():
749 raise RuntimeError("this shouldn't escape")
750 enabled = gc.isenabled()
751 orig_gc_disable = gc.disable
752 orig_gc_isenabled = gc.isenabled
753 try:
754 gc.disable()
755 self.assertFalse(gc.isenabled())
756 subprocess.call([sys.executable, '-c', ''],
757 preexec_fn=lambda: None)
758 self.assertFalse(gc.isenabled(),
759 "Popen enabled gc when it shouldn't.")
760
761 gc.enable()
762 self.assertTrue(gc.isenabled())
763 subprocess.call([sys.executable, '-c', ''],
764 preexec_fn=lambda: None)
765 self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
766
767 gc.disable = raise_runtime_error
768 self.assertRaises(RuntimeError, subprocess.Popen,
769 [sys.executable, '-c', ''],
770 preexec_fn=lambda: None)
771
772 del gc.isenabled # force an AttributeError
773 self.assertRaises(AttributeError, subprocess.Popen,
774 [sys.executable, '-c', ''],
775 preexec_fn=lambda: None)
776 finally:
777 gc.disable = orig_gc_disable
778 gc.isenabled = orig_gc_isenabled
779 if not enabled:
780 gc.disable()
781
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000782 def test_args_string(self):
783 # args is a string
784 fd, fname = mkstemp()
785 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +0000786 with open(fd, "w", errors="surrogateescape") as fobj:
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000787 fobj.write("#!/bin/sh\n")
788 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
789 sys.executable)
790 os.chmod(fname, 0o700)
791 p = subprocess.Popen(fname)
792 p.wait()
793 os.remove(fname)
794 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000795
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000796 def test_invalid_args(self):
797 # invalid arguments should raise ValueError
798 self.assertRaises(ValueError, subprocess.call,
799 [sys.executable, "-c",
800 "import sys; sys.exit(47)"],
801 startupinfo=47)
802 self.assertRaises(ValueError, subprocess.call,
803 [sys.executable, "-c",
804 "import sys; sys.exit(47)"],
805 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000806
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000807 def test_shell_sequence(self):
808 # Run command through the shell (sequence)
809 newenv = os.environ.copy()
810 newenv["FRUIT"] = "apple"
811 p = subprocess.Popen(["echo $FRUIT"], shell=1,
812 stdout=subprocess.PIPE,
813 env=newenv)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000814 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000815 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000816
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000817 def test_shell_string(self):
818 # Run command through the shell (string)
819 newenv = os.environ.copy()
820 newenv["FRUIT"] = "apple"
821 p = subprocess.Popen("echo $FRUIT", shell=1,
822 stdout=subprocess.PIPE,
823 env=newenv)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000824 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000825 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Christian Heimesa342c012008-04-20 21:01:16 +0000826
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000827 def test_call_string(self):
828 # call() function with string argument on UNIX
829 fd, fname = mkstemp()
830 # reopen in text mode
Victor Stinnerf6782ac2010-10-16 23:46:43 +0000831 with open(fd, "w", errors="surrogateescape") as fobj:
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000832 fobj.write("#!/bin/sh\n")
833 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
834 sys.executable)
835 os.chmod(fname, 0o700)
836 rc = subprocess.call(fname)
837 os.remove(fname)
838 self.assertEqual(rc, 47)
Christian Heimesa342c012008-04-20 21:01:16 +0000839
Stefan Krah9542cc62010-07-19 14:20:53 +0000840 def test_specific_shell(self):
841 # Issue #9265: Incorrect name passed as arg[0].
842 shells = []
843 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
844 for name in ['bash', 'ksh']:
845 sh = os.path.join(prefix, name)
846 if os.path.isfile(sh):
847 shells.append(sh)
848 if not shells: # Will probably work for any shell but csh.
849 self.skipTest("bash or ksh required for this test")
850 sh = '/bin/sh'
851 if os.path.isfile(sh) and not os.path.islink(sh):
852 # Test will fail if /bin/sh is a symlink to csh.
853 shells.append(sh)
854 for sh in shells:
855 p = subprocess.Popen("echo $0", executable=sh, shell=True,
856 stdout=subprocess.PIPE)
Brian Curtin3c6a9512010-11-05 03:58:52 +0000857 self.addCleanup(p.stdout.close)
Stefan Krah9542cc62010-07-19 14:20:53 +0000858 self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
859
Florent Xicluna4886d242010-03-08 13:27:26 +0000860 def _kill_process(self, method, *args):
Florent Xicluna1d8ee3a2010-03-05 20:26:54 +0000861 # Do not inherit file handles from the parent.
862 # It should fix failures on some platforms.
Antoine Pitrou3d8580f2010-09-20 01:33:21 +0000863 p = subprocess.Popen([sys.executable, "-c", """if 1:
864 import sys, time
865 sys.stdout.write('x\\n')
866 sys.stdout.flush()
867 time.sleep(30)
868 """],
869 close_fds=True,
870 stdin=subprocess.PIPE,
871 stdout=subprocess.PIPE,
872 stderr=subprocess.PIPE)
873 # Wait for the interpreter to be completely initialized before
874 # sending any signal.
875 p.stdout.read(1)
876 getattr(p, method)(*args)
Florent Xicluna4886d242010-03-08 13:27:26 +0000877 return p
878
879 def test_send_signal(self):
880 p = self._kill_process('send_signal', signal.SIGINT)
Florent Xiclunac049d872010-03-27 22:47:23 +0000881 _, stderr = p.communicate()
882 self.assertIn(b'KeyboardInterrupt', stderr)
Florent Xiclunaf0cbd822010-03-04 21:50:56 +0000883 self.assertNotEqual(p.wait(), 0)
Christian Heimesa342c012008-04-20 21:01:16 +0000884
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000885 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +0000886 p = self._kill_process('kill')
Florent Xiclunac049d872010-03-27 22:47:23 +0000887 _, stderr = p.communicate()
888 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000889 self.assertEqual(p.wait(), -signal.SIGKILL)
Tim Peterse718f612004-10-12 21:51:32 +0000890
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000891 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +0000892 p = self._kill_process('terminate')
Florent Xiclunac049d872010-03-27 22:47:23 +0000893 _, stderr = p.communicate()
894 self.assertStderrEqual(stderr, b'')
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000895 self.assertEqual(p.wait(), -signal.SIGTERM)
896
Victor Stinner13bb71c2010-04-23 21:41:56 +0000897 def test_surrogates_error_message(self):
Victor Stinner4d078042010-04-23 19:28:32 +0000898 def prepare():
899 raise ValueError("surrogate:\uDCff")
900
901 try:
902 subprocess.call(
903 [sys.executable, "-c", "pass"],
904 preexec_fn=prepare)
905 except ValueError as err:
906 # Pure Python implementations keeps the message
907 self.assertIsNone(subprocess._posixsubprocess)
908 self.assertEqual(str(err), "surrogate:\uDCff")
909 except RuntimeError as err:
910 # _posixsubprocess uses a default message
911 self.assertIsNotNone(subprocess._posixsubprocess)
912 self.assertEqual(str(err), "Exception occurred in preexec_fn.")
913 else:
914 self.fail("Expected ValueError or RuntimeError")
915
Victor Stinner13bb71c2010-04-23 21:41:56 +0000916 def test_undecodable_env(self):
917 for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')):
Victor Stinner13bb71c2010-04-23 21:41:56 +0000918 # test str with surrogates
Antoine Pitroufb8db8f2010-09-19 22:46:05 +0000919 script = "import os; print(ascii(os.getenv(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +0000920 env = os.environ.copy()
921 env[key] = value
Victor Stinner89f3ad12010-10-14 10:43:31 +0000922 # Use C locale to get ascii for the locale encoding to force
923 # surrogate-escaping of \xFF in the child process; otherwise it can
924 # be decoded as-is if the default locale is latin-1.
Victor Stinnerebc78d22010-10-14 10:38:17 +0000925 env['LC_ALL'] = 'C'
Victor Stinner13bb71c2010-04-23 21:41:56 +0000926 stdout = subprocess.check_output(
927 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +0000928 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +0000929 stdout = stdout.rstrip(b'\n\r')
Antoine Pitroufb8db8f2010-09-19 22:46:05 +0000930 self.assertEquals(stdout.decode('ascii'), ascii(value))
Victor Stinner13bb71c2010-04-23 21:41:56 +0000931
932 # test bytes
933 key = key.encode("ascii", "surrogateescape")
934 value = value.encode("ascii", "surrogateescape")
Antoine Pitroufb8db8f2010-09-19 22:46:05 +0000935 script = "import os; print(ascii(os.getenvb(%s)))" % repr(key)
Victor Stinnerce2d24d2010-04-23 22:55:39 +0000936 env = os.environ.copy()
937 env[key] = value
Victor Stinner13bb71c2010-04-23 21:41:56 +0000938 stdout = subprocess.check_output(
939 [sys.executable, "-c", script],
Victor Stinnerce2d24d2010-04-23 22:55:39 +0000940 env=env)
Victor Stinner13bb71c2010-04-23 21:41:56 +0000941 stdout = stdout.rstrip(b'\n\r')
Antoine Pitroufb8db8f2010-09-19 22:46:05 +0000942 self.assertEquals(stdout.decode('ascii'), ascii(value))
Victor Stinner13bb71c2010-04-23 21:41:56 +0000943
Victor Stinnerb745a742010-05-18 17:17:23 +0000944 def test_bytes_program(self):
945 abs_program = os.fsencode(sys.executable)
946 path, program = os.path.split(sys.executable)
947 program = os.fsencode(program)
948
949 # absolute bytes path
950 exitcode = subprocess.call([abs_program, "-c", "pass"])
951 self.assertEquals(exitcode, 0)
952
953 # bytes program, unicode PATH
954 env = os.environ.copy()
955 env["PATH"] = path
956 exitcode = subprocess.call([program, "-c", "pass"], env=env)
957 self.assertEquals(exitcode, 0)
958
959 # bytes program, bytes PATH
960 envb = os.environb.copy()
961 envb[b"PATH"] = os.fsencode(path)
962 exitcode = subprocess.call([program, "-c", "pass"], env=envb)
963 self.assertEquals(exitcode, 0)
964
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000965
Florent Xiclunaf0cbd822010-03-04 21:50:56 +0000966@unittest.skipUnless(mswindows, "Windows specific tests")
Florent Xiclunac049d872010-03-27 22:47:23 +0000967class Win32ProcessTestCase(BaseTestCase):
Florent Xiclunaf0cbd822010-03-04 21:50:56 +0000968
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000969 def test_startupinfo(self):
970 # startupinfo argument
971 # We uses hardcoded constants, because we do not want to
972 # depend on win32all.
973 STARTF_USESHOWWINDOW = 1
974 SW_MAXIMIZE = 3
975 startupinfo = subprocess.STARTUPINFO()
976 startupinfo.dwFlags = STARTF_USESHOWWINDOW
977 startupinfo.wShowWindow = SW_MAXIMIZE
978 # Since Python is a console process, it won't be affected
979 # by wShowWindow, but the argument should be silently
980 # ignored
981 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000982 startupinfo=startupinfo)
983
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000984 def test_creationflags(self):
985 # creationflags argument
986 CREATE_NEW_CONSOLE = 16
987 sys.stderr.write(" a DOS box should flash briefly ...\n")
988 subprocess.call(sys.executable +
989 ' -c "import time; time.sleep(0.25)"',
990 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000991
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000992 def test_invalid_args(self):
993 # invalid arguments should raise ValueError
994 self.assertRaises(ValueError, subprocess.call,
995 [sys.executable, "-c",
996 "import sys; sys.exit(47)"],
997 preexec_fn=lambda: 1)
998 self.assertRaises(ValueError, subprocess.call,
999 [sys.executable, "-c",
1000 "import sys; sys.exit(47)"],
1001 stdout=subprocess.PIPE,
1002 close_fds=True)
1003
1004 def test_close_fds(self):
1005 # close file descriptors
1006 rc = subprocess.call([sys.executable, "-c",
1007 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001008 close_fds=True)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001009 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001010
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001011 def test_shell_sequence(self):
1012 # Run command through the shell (sequence)
1013 newenv = os.environ.copy()
1014 newenv["FRUIT"] = "physalis"
1015 p = subprocess.Popen(["set"], shell=1,
1016 stdout=subprocess.PIPE,
1017 env=newenv)
Brian Curtin19a53792010-11-05 17:09:05 +00001018 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001019 self.assertIn(b"physalis", p.stdout.read())
Guido van Rossume7ba4952007-06-06 23:52:48 +00001020
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001021 def test_shell_string(self):
1022 # Run command through the shell (string)
1023 newenv = os.environ.copy()
1024 newenv["FRUIT"] = "physalis"
1025 p = subprocess.Popen("set", shell=1,
1026 stdout=subprocess.PIPE,
1027 env=newenv)
Brian Curtin19a53792010-11-05 17:09:05 +00001028 self.addCleanup(p.stdout.close)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001029 self.assertIn(b"physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001030
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001031 def test_call_string(self):
1032 # call() function with string argument on Windows
1033 rc = subprocess.call(sys.executable +
1034 ' -c "import sys; sys.exit(47)"')
1035 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001036
Florent Xicluna4886d242010-03-08 13:27:26 +00001037 def _kill_process(self, method, *args):
1038 # Some win32 buildbot raises EOFError if stdin is inherited
Antoine Pitroua4024e22010-09-24 18:57:01 +00001039 p = subprocess.Popen([sys.executable, "-c", """if 1:
1040 import sys, time
1041 sys.stdout.write('x\\n')
1042 sys.stdout.flush()
1043 time.sleep(30)
1044 """],
1045 stdin=subprocess.PIPE,
1046 stdout=subprocess.PIPE,
1047 stderr=subprocess.PIPE)
Brian Curtin19a53792010-11-05 17:09:05 +00001048 self.addCleanup(p.stdout.close)
1049 self.addCleanup(p.stderr.close)
1050 self.addCleanup(p.stdin.close)
Antoine Pitroua4024e22010-09-24 18:57:01 +00001051 # Wait for the interpreter to be completely initialized before
1052 # sending any signal.
1053 p.stdout.read(1)
1054 getattr(p, method)(*args)
Florent Xiclunac049d872010-03-27 22:47:23 +00001055 _, stderr = p.communicate()
1056 self.assertStderrEqual(stderr, b'')
Antoine Pitroua4024e22010-09-24 18:57:01 +00001057 returncode = p.wait()
Florent Xicluna4886d242010-03-08 13:27:26 +00001058 self.assertNotEqual(returncode, 0)
1059
1060 def test_send_signal(self):
1061 self._kill_process('send_signal', signal.SIGTERM)
Christian Heimesa342c012008-04-20 21:01:16 +00001062
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001063 def test_kill(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001064 self._kill_process('kill')
Christian Heimesa342c012008-04-20 21:01:16 +00001065
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001066 def test_terminate(self):
Florent Xicluna4886d242010-03-08 13:27:26 +00001067 self._kill_process('terminate')
Christian Heimesa342c012008-04-20 21:01:16 +00001068
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001069
Brett Cannona23810f2008-05-26 19:04:21 +00001070# The module says:
1071# "NB This only works (and is only relevant) for UNIX."
1072#
1073# Actually, getoutput should work on any platform with an os.popen, but
1074# I'll take the comment as given, and skip this suite.
Florent Xiclunaf0cbd822010-03-04 21:50:56 +00001075@unittest.skipUnless(os.name == 'posix', "only relevant for UNIX")
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001076class CommandTests(unittest.TestCase):
1077 def test_getoutput(self):
1078 self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
1079 self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
1080 (0, 'xyzzy'))
Brett Cannona23810f2008-05-26 19:04:21 +00001081
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001082 # we use mkdtemp in the next line to create an empty directory
1083 # under our exclusive control; from that, we can invent a pathname
1084 # that we _know_ won't exist. This is guaranteed to fail.
1085 dir = None
1086 try:
1087 dir = tempfile.mkdtemp()
1088 name = os.path.join(dir, "foo")
Brett Cannona23810f2008-05-26 19:04:21 +00001089
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001090 status, output = subprocess.getstatusoutput('cat ' + name)
1091 self.assertNotEqual(status, 0)
1092 finally:
1093 if dir is not None:
1094 os.rmdir(dir)
Brett Cannona23810f2008-05-26 19:04:21 +00001095
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001096
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001097@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
1098 "poll system call not supported")
1099class ProcessTestCaseNoPoll(ProcessTestCase):
1100 def setUp(self):
1101 subprocess._has_poll = False
1102 ProcessTestCase.setUp(self)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001103
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001104 def tearDown(self):
1105 subprocess._has_poll = True
1106 ProcessTestCase.tearDown(self)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001107
1108
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001109@unittest.skipUnless(getattr(subprocess, '_posixsubprocess', False),
1110 "_posixsubprocess extension module not found.")
1111class ProcessTestCasePOSIXPurePython(ProcessTestCase, POSIXProcessTestCase):
1112 def setUp(self):
1113 subprocess._posixsubprocess = None
1114 ProcessTestCase.setUp(self)
1115 POSIXProcessTestCase.setUp(self)
1116
1117 def tearDown(self):
1118 subprocess._posixsubprocess = sys.modules['_posixsubprocess']
1119 POSIXProcessTestCase.tearDown(self)
1120 ProcessTestCase.tearDown(self)
1121
1122
Gregory P. Smitha59c59f2010-03-01 00:17:40 +00001123class HelperFunctionTests(unittest.TestCase):
Gregory P. Smithaf6d3b82010-03-01 02:56:44 +00001124 @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
Gregory P. Smitha59c59f2010-03-01 00:17:40 +00001125 def test_eintr_retry_call(self):
1126 record_calls = []
1127 def fake_os_func(*args):
1128 record_calls.append(args)
1129 if len(record_calls) == 2:
1130 raise OSError(errno.EINTR, "fake interrupted system call")
1131 return tuple(reversed(args))
1132
1133 self.assertEqual((999, 256),
1134 subprocess._eintr_retry_call(fake_os_func, 256, 999))
1135 self.assertEqual([(256, 999)], record_calls)
1136 # This time there will be an EINTR so it will loop once.
1137 self.assertEqual((666,),
1138 subprocess._eintr_retry_call(fake_os_func, 666))
1139 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
1140
1141
Tim Golden126c2962010-08-11 14:20:40 +00001142@unittest.skipUnless(mswindows, "Windows-specific tests")
1143class CommandsWithSpaces (BaseTestCase):
1144
1145 def setUp(self):
1146 super().setUp()
1147 f, fname = mkstemp(".py", "te st")
1148 self.fname = fname.lower ()
1149 os.write(f, b"import sys;"
1150 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
1151 )
1152 os.close(f)
1153
1154 def tearDown(self):
1155 os.remove(self.fname)
1156 super().tearDown()
1157
1158 def with_spaces(self, *args, **kwargs):
1159 kwargs['stdout'] = subprocess.PIPE
1160 p = subprocess.Popen(*args, **kwargs)
Brian Curtin19a53792010-11-05 17:09:05 +00001161 self.addCleanup(p.stdout.close)
Tim Golden126c2962010-08-11 14:20:40 +00001162 self.assertEqual(
1163 p.stdout.read ().decode("mbcs"),
1164 "2 [%r, 'ab cd']" % self.fname
1165 )
1166
1167 def test_shell_string_with_spaces(self):
1168 # call() function with string argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00001169 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1170 "ab cd"), shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00001171
1172 def test_shell_sequence_with_spaces(self):
1173 # call() function with sequence argument with spaces on Windows
Brian Curtind835cf12010-08-13 20:42:57 +00001174 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
Tim Golden126c2962010-08-11 14:20:40 +00001175
1176 def test_noshell_string_with_spaces(self):
1177 # call() function with string argument with spaces on Windows
1178 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1179 "ab cd"))
1180
1181 def test_noshell_sequence_with_spaces(self):
1182 # call() function with sequence argument with spaces on Windows
1183 self.with_spaces([sys.executable, self.fname, "ab cd"])
1184
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001185def test_main():
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001186 unit_tests = (ProcessTestCase,
1187 POSIXProcessTestCase,
1188 Win32ProcessTestCase,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001189 ProcessTestCasePOSIXPurePython,
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001190 CommandTests,
Gregory P. Smitha59c59f2010-03-01 00:17:40 +00001191 ProcessTestCaseNoPoll,
Tim Golden126c2962010-08-11 14:20:40 +00001192 HelperFunctionTests,
1193 CommandsWithSpaces)
Florent Xiclunab1e94e82010-02-27 22:12:37 +00001194
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001195 support.run_unittest(*unit_tests)
Brett Cannona23810f2008-05-26 19:04:21 +00001196 support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001197
1198if __name__ == "__main__":
Brett Cannona23810f2008-05-26 19:04:21 +00001199 test_main()