blob: c72b6e0b77dd7cc2f449ebbc87fca773264f029b [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001import unittest
2from test import test_support
3import subprocess
4import sys
5import signal
6import os
7import tempfile
8import time
Tim Peters3761e8d2004-10-13 04:07:12 +00009import re
Ezio Melotti8f6a2872010-02-10 21:40:33 +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
Florent Xicluna98e3fc32010-02-27 19:20:50 +000024
25try:
26 mkstemp = tempfile.mkstemp
27except AttributeError:
28 # tempfile.mkstemp is not available
29 def mkstemp():
30 """Replacement for mkstemp, calling mktemp."""
31 fname = tempfile.mktemp()
32 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
33
Tim Peters3761e8d2004-10-13 04:07:12 +000034
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000035class ProcessTestCase(unittest.TestCase):
Neal Norwitzb15ac312006-06-29 04:10:08 +000036 def setUp(self):
Tim Peters38ff36c2006-06-30 06:18:39 +000037 # Try to minimize the number of children we have so this test
38 # doesn't crash on some buildbots (Alphas in particular).
Florent Xicluna98e3fc32010-02-27 19:20:50 +000039 test_support.reap_children()
Neal Norwitzb15ac312006-06-29 04:10:08 +000040
Florent Xicluna98e3fc32010-02-27 19:20:50 +000041 def assertStderrEqual(self, stderr, expected, msg=None):
42 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
43 # shutdown time. That frustrates tests trying to check stderr produced
44 # from a spawned Python process.
45 actual = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
46 self.assertEqual(actual, expected, msg)
Neal Norwitzb15ac312006-06-29 04:10:08 +000047
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000048 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000049 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000050 rc = subprocess.call([sys.executable, "-c",
51 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000052 self.assertEqual(rc, 47)
53
Peter Astrand454f7672005-01-01 09:36:35 +000054 def test_check_call_zero(self):
55 # check_call() function with zero return code
56 rc = subprocess.check_call([sys.executable, "-c",
57 "import sys; sys.exit(0)"])
58 self.assertEqual(rc, 0)
59
60 def test_check_call_nonzero(self):
61 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +000062 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +000063 subprocess.check_call([sys.executable, "-c",
64 "import sys; sys.exit(47)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +000065 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000066
Gregory P. Smith26576802008-12-05 02:27:01 +000067 def test_check_output(self):
68 # check_output() function with zero return code
69 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000070 [sys.executable, "-c", "print 'BDFL'"])
Ezio Melottiaa980582010-01-23 23:04:36 +000071 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000072
Gregory P. Smith26576802008-12-05 02:27:01 +000073 def test_check_output_nonzero(self):
Gregory P. Smith97f49f42008-12-04 20:21:09 +000074 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +000075 with self.assertRaises(subprocess.CalledProcessError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +000076 subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000077 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +000078 self.assertEqual(c.exception.returncode, 5)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000079
Gregory P. Smith26576802008-12-05 02:27:01 +000080 def test_check_output_stderr(self):
81 # check_output() function stderr redirected to stdout
82 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000083 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
84 stderr=subprocess.STDOUT)
Ezio Melottiaa980582010-01-23 23:04:36 +000085 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000086
Gregory P. Smith26576802008-12-05 02:27:01 +000087 def test_check_output_stdout_arg(self):
88 # check_output() function stderr redirected to stdout
Florent Xicluna98e3fc32010-02-27 19:20:50 +000089 with self.assertRaises(ValueError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +000090 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000091 [sys.executable, "-c", "print 'will not be run'"],
92 stdout=sys.stdout)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000093 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xicluna98e3fc32010-02-27 19:20:50 +000094 self.assertIn('stdout', c.exception.args[0])
Gregory P. Smith97f49f42008-12-04 20:21:09 +000095
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000096 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +000097 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000098 newenv = os.environ.copy()
99 newenv["FRUIT"] = "banana"
100 rc = subprocess.call([sys.executable, "-c",
101 'import sys, os;' \
102 'sys.exit(os.getenv("FRUIT")=="banana")'],
103 env=newenv)
104 self.assertEqual(rc, 1)
105
106 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000107 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000108 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
109 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
110 p.wait()
111 self.assertEqual(p.stdin, None)
112
113 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000114 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +0000115 p = subprocess.Popen([sys.executable, "-c",
Tim Peters4052fe52004-10-13 03:29:54 +0000116 'print " this bit of output is from a '
117 'test of stdout in a different '
118 'process ..."'],
119 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000120 p.wait()
121 self.assertEqual(p.stdout, None)
122
123 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000124 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000125 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
126 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
127 p.wait()
128 self.assertEqual(p.stderr, None)
129
Ezio Melotti8f6a2872010-02-10 21:40:33 +0000130 def test_executable_with_cwd(self):
131 python_dir = os.path.dirname(os.path.realpath(sys.executable))
132 p = subprocess.Popen(["somethingyoudonthave", "-c",
133 "import sys; sys.exit(47)"],
134 executable=sys.executable, cwd=python_dir)
135 p.wait()
136 self.assertEqual(p.returncode, 47)
137
138 @unittest.skipIf(sysconfig.is_python_build(),
139 "need an installed Python. See #7774")
140 def test_executable_without_cwd(self):
141 # For a normal installation, it should work without 'cwd'
142 # argument. For test runs in the build directory, see #7774.
143 p = subprocess.Popen(["somethingyoudonthave", "-c",
144 "import sys; sys.exit(47)"],
Tim Peters3b01a702004-10-12 22:19:32 +0000145 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000146 p.wait()
147 self.assertEqual(p.returncode, 47)
148
149 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000150 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000151 p = subprocess.Popen([sys.executable, "-c",
152 'import sys; sys.exit(sys.stdin.read() == "pear")'],
153 stdin=subprocess.PIPE)
154 p.stdin.write("pear")
155 p.stdin.close()
156 p.wait()
157 self.assertEqual(p.returncode, 1)
158
159 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000160 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000161 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000162 d = tf.fileno()
163 os.write(d, "pear")
164 os.lseek(d, 0, 0)
165 p = subprocess.Popen([sys.executable, "-c",
166 'import sys; sys.exit(sys.stdin.read() == "pear")'],
167 stdin=d)
168 p.wait()
169 self.assertEqual(p.returncode, 1)
170
171 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000172 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000173 tf = tempfile.TemporaryFile()
174 tf.write("pear")
175 tf.seek(0)
176 p = subprocess.Popen([sys.executable, "-c",
177 'import sys; sys.exit(sys.stdin.read() == "pear")'],
178 stdin=tf)
179 p.wait()
180 self.assertEqual(p.returncode, 1)
181
182 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000183 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000184 p = subprocess.Popen([sys.executable, "-c",
185 'import sys; sys.stdout.write("orange")'],
186 stdout=subprocess.PIPE)
187 self.assertEqual(p.stdout.read(), "orange")
188
189 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000190 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000191 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000192 d = tf.fileno()
193 p = subprocess.Popen([sys.executable, "-c",
194 'import sys; sys.stdout.write("orange")'],
195 stdout=d)
196 p.wait()
197 os.lseek(d, 0, 0)
198 self.assertEqual(os.read(d, 1024), "orange")
199
200 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000201 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000202 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000203 p = subprocess.Popen([sys.executable, "-c",
204 'import sys; sys.stdout.write("orange")'],
205 stdout=tf)
206 p.wait()
207 tf.seek(0)
208 self.assertEqual(tf.read(), "orange")
209
210 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000211 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000212 p = subprocess.Popen([sys.executable, "-c",
213 'import sys; sys.stderr.write("strawberry")'],
214 stderr=subprocess.PIPE)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000215 self.assertStderrEqual(p.stderr.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000216
217 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000218 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000219 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000220 d = tf.fileno()
221 p = subprocess.Popen([sys.executable, "-c",
222 'import sys; sys.stderr.write("strawberry")'],
223 stderr=d)
224 p.wait()
225 os.lseek(d, 0, 0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000226 self.assertStderrEqual(os.read(d, 1024), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000227
228 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000229 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000230 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000231 p = subprocess.Popen([sys.executable, "-c",
232 'import sys; sys.stderr.write("strawberry")'],
233 stderr=tf)
234 p.wait()
235 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000236 self.assertStderrEqual(tf.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000237
238 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000239 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000240 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000241 'import sys;'
242 'sys.stdout.write("apple");'
243 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000244 'sys.stderr.write("orange")'],
245 stdout=subprocess.PIPE,
246 stderr=subprocess.STDOUT)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000247 self.assertStderrEqual(p.stdout.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000248
249 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000250 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000251 tf = tempfile.TemporaryFile()
252 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000253 'import sys;'
254 'sys.stdout.write("apple");'
255 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000256 'sys.stderr.write("orange")'],
257 stdout=tf,
258 stderr=tf)
259 p.wait()
260 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000261 self.assertStderrEqual(tf.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000262
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000263 def test_stdout_filedes_of_stdout(self):
264 # stdout is set to 1 (#1531862).
265 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))"
266 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000267 self.assertEqual(rc, 2)
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000268
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000269 def test_cwd(self):
Guido van Rossume9a0e882007-12-20 17:28:10 +0000270 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000271 # We cannot use os.path.realpath to canonicalize the path,
272 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
273 cwd = os.getcwd()
274 os.chdir(tmpdir)
275 tmpdir = os.getcwd()
276 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000277 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000278 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000279 'sys.stdout.write(os.getcwd())'],
280 stdout=subprocess.PIPE,
281 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000282 normcase = os.path.normcase
283 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000284
285 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000286 newenv = os.environ.copy()
287 newenv["FRUIT"] = "orange"
288 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000289 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000290 'sys.stdout.write(os.getenv("FRUIT"))'],
291 stdout=subprocess.PIPE,
292 env=newenv)
293 self.assertEqual(p.stdout.read(), "orange")
294
Peter Astrandcbac93c2005-03-03 20:24:28 +0000295 def test_communicate_stdin(self):
296 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000297 'import sys;'
298 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000299 stdin=subprocess.PIPE)
300 p.communicate("pear")
301 self.assertEqual(p.returncode, 1)
302
303 def test_communicate_stdout(self):
304 p = subprocess.Popen([sys.executable, "-c",
305 'import sys; sys.stdout.write("pineapple")'],
306 stdout=subprocess.PIPE)
307 (stdout, stderr) = p.communicate()
308 self.assertEqual(stdout, "pineapple")
309 self.assertEqual(stderr, None)
310
311 def test_communicate_stderr(self):
312 p = subprocess.Popen([sys.executable, "-c",
313 'import sys; sys.stderr.write("pineapple")'],
314 stderr=subprocess.PIPE)
315 (stdout, stderr) = p.communicate()
316 self.assertEqual(stdout, None)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000317 self.assertStderrEqual(stderr, "pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000318
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000319 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000320 p = subprocess.Popen([sys.executable, "-c",
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000321 'import sys,os;'
322 'sys.stderr.write("pineapple");'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000323 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000324 stdin=subprocess.PIPE,
325 stdout=subprocess.PIPE,
326 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000327 (stdout, stderr) = p.communicate("banana")
328 self.assertEqual(stdout, "banana")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000329 self.assertStderrEqual(stderr, "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000330
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000331 # This test is Linux specific for simplicity to at least have
332 # some coverage. It is not a platform specific bug.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000333 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
334 "Linux specific")
335 # Test for the fd leak reported in http://bugs.python.org/issue2791.
336 def test_communicate_pipe_fd_leak(self):
337 fd_directory = '/proc/%d/fd' % os.getpid()
338 num_fds_before_popen = len(os.listdir(fd_directory))
339 p = subprocess.Popen([sys.executable, "-c", "print()"],
340 stdout=subprocess.PIPE)
341 p.communicate()
342 num_fds_after_communicate = len(os.listdir(fd_directory))
343 del p
344 num_fds_after_destruction = len(os.listdir(fd_directory))
345 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
346 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000347
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000348 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000349 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000350 p = subprocess.Popen([sys.executable, "-c",
351 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000352 (stdout, stderr) = p.communicate()
353 self.assertEqual(stdout, None)
354 self.assertEqual(stderr, None)
355
356 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000357 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000358 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000359 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000360 x, y = os.pipe()
361 if mswindows:
362 pipe_buf = 512
363 else:
364 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
365 os.close(x)
366 os.close(y)
367 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000368 'import sys,os;'
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000369 'sys.stdout.write(sys.stdin.read(47));'
370 'sys.stderr.write("xyz"*%d);'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000371 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000372 stdin=subprocess.PIPE,
373 stdout=subprocess.PIPE,
374 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000375 string_to_write = "abc"*pipe_buf
376 (stdout, stderr) = p.communicate(string_to_write)
377 self.assertEqual(stdout, string_to_write)
378
379 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000380 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000381 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000382 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000383 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000384 stdin=subprocess.PIPE,
385 stdout=subprocess.PIPE,
386 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000387 p.stdin.write("banana")
388 (stdout, stderr) = p.communicate("split")
389 self.assertEqual(stdout, "bananasplit")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000390 self.assertStderrEqual(stderr, "")
Tim Peterse718f612004-10-12 21:51:32 +0000391
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000392 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000393 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000394 'import sys,os;' + SETBINARY +
395 'sys.stdout.write("line1\\n");'
396 'sys.stdout.flush();'
397 'sys.stdout.write("line2\\r");'
398 'sys.stdout.flush();'
399 'sys.stdout.write("line3\\r\\n");'
400 'sys.stdout.flush();'
401 'sys.stdout.write("line4\\r");'
402 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000403 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000404 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000405 'sys.stdout.write("\\nline6");'],
406 stdout=subprocess.PIPE,
407 universal_newlines=1)
408 stdout = p.stdout.read()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000409 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000410 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000411 self.assertEqual(stdout,
412 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000413 else:
414 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000415 self.assertEqual(stdout,
416 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000417
418 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000419 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000420 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000421 'import sys,os;' + SETBINARY +
422 'sys.stdout.write("line1\\n");'
423 'sys.stdout.flush();'
424 'sys.stdout.write("line2\\r");'
425 'sys.stdout.flush();'
426 'sys.stdout.write("line3\\r\\n");'
427 'sys.stdout.flush();'
428 'sys.stdout.write("line4\\r");'
429 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000430 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000431 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000432 'sys.stdout.write("\\nline6");'],
433 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
434 universal_newlines=1)
435 (stdout, stderr) = p.communicate()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000436 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000437 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000438 self.assertEqual(stdout,
439 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000440 else:
441 # Interpreter without universal newline support
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000442 self.assertEqual(stdout,
443 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000444
445 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000446 # Make sure we leak no resources
Peter Astrandd6b24302006-06-22 20:06:46 +0000447 if not hasattr(test_support, "is_resource_enabled") \
448 or test_support.is_resource_enabled("subprocess") and not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000449 max_handles = 1026 # too much for most UNIX systems
450 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000451 max_handles = 65
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000452 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000453 p = subprocess.Popen([sys.executable, "-c",
454 "import sys;sys.stdout.write(sys.stdin.read())"],
455 stdin=subprocess.PIPE,
456 stdout=subprocess.PIPE,
457 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000458 data = p.communicate("lime")[0]
459 self.assertEqual(data, "lime")
460
461
462 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000463 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
464 '"a b c" d e')
465 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
466 'ab\\"c \\ d')
Gregory P. Smithe047e6d2008-01-19 20:49:02 +0000467 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
468 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000469 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
470 'a\\\\\\b "de fg" h')
471 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
472 'a\\\\\\"b c d')
473 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
474 '"a\\\\b c" d e')
475 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
476 '"a\\\\b\\ c" d e')
Peter Astrand10514a72007-01-13 22:35:35 +0000477 self.assertEqual(subprocess.list2cmdline(['ab', '']),
478 'ab ""')
Gregory P. Smith70eb2f92008-01-19 22:49:37 +0000479 self.assertEqual(subprocess.list2cmdline(['echo', 'foo|bar']),
480 'echo "foo|bar"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000481
482
483 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000484 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000485 "-c", "import time; time.sleep(1)"])
486 count = 0
487 while p.poll() is None:
488 time.sleep(0.1)
489 count += 1
490 # We expect that the poll loop probably went around about 10 times,
491 # but, based on system scheduling we can't control, it's possible
492 # poll() never returned None. It "should be" very rare that it
493 # didn't go around at least twice.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000494 self.assertGreaterEqual(count, 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000495 # Subsequent invocations should just return the returncode
496 self.assertEqual(p.poll(), 0)
497
498
499 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000500 p = subprocess.Popen([sys.executable,
501 "-c", "import time; time.sleep(2)"])
502 self.assertEqual(p.wait(), 0)
503 # Subsequent invocations should just return the returncode
504 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000505
Peter Astrand738131d2004-11-30 21:04:45 +0000506
507 def test_invalid_bufsize(self):
508 # an invalid type of the bufsize argument should raise
509 # TypeError.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000510 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +0000511 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +0000512
Georg Brandlf3715d22009-02-14 17:01:36 +0000513 def test_leaking_fds_on_error(self):
514 # see bug #5179: Popen leaks file descriptors to PIPEs if
515 # the child fails to execute; this will eventually exhaust
516 # the maximum number of open fds. 1024 seems a very common
517 # value for that limit, but Windows has 2048, so we loop
518 # 1024 times (each call leaked two fds).
519 for i in range(1024):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000520 # Windows raises IOError. Others raise OSError.
521 with self.assertRaises(EnvironmentError) as c:
Georg Brandlf3715d22009-02-14 17:01:36 +0000522 subprocess.Popen(['nonexisting_i_hope'],
523 stdout=subprocess.PIPE,
524 stderr=subprocess.PIPE)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000525 if c.exception.errno != 2: # ignore "no such file"
526 raise c.exception
Georg Brandlf3715d22009-02-14 17:01:36 +0000527
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000528
529# context manager
530class _SuppressCoreFiles(object):
531 """Try to prevent core files from being created."""
532 old_limit = None
533
534 def __enter__(self):
535 """Try to save previous ulimit, then set it to (0, 0)."""
536 try:
537 import resource
538 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
539 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
540 except (ImportError, ValueError, resource.error):
541 pass
542
543 def __exit__(self, *args):
544 """Return core file behavior to default."""
545 if self.old_limit is None:
546 return
547 try:
548 import resource
549 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
550 except (ImportError, ValueError, resource.error):
551 pass
552
553
554@unittest.skipIf(sys.platform == "win32", "POSIX specific tests")
555class POSIXProcessTestCase(unittest.TestCase):
556 def setUp(self):
557 # Try to minimize the number of children we have so this test
558 # doesn't crash on some buildbots (Alphas in particular).
559 test_support.reap_children()
560
561 def test_exceptions(self):
562 # caught & re-raised exceptions
563 with self.assertRaises(OSError) as c:
564 p = subprocess.Popen([sys.executable, "-c", ""],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000565 cwd="/this/path/does/not/exist")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000566 # The attribute child_traceback should contain "os.chdir" somewhere.
567 self.assertIn("os.chdir", c.exception.child_traceback)
Tim Peterse718f612004-10-12 21:51:32 +0000568
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000569 def test_run_abort(self):
570 # returncode handles signal termination
571 with _SuppressCoreFiles():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000572 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000573 "import os; os.abort()"])
574 p.wait()
575 self.assertEqual(-p.returncode, signal.SIGABRT)
576
577 def test_preexec(self):
578 # preexec function
579 p = subprocess.Popen([sys.executable, "-c",
580 "import sys, os;"
581 "sys.stdout.write(os.getenv('FRUIT'))"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000582 stdout=subprocess.PIPE,
583 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000584 self.assertEqual(p.stdout.read(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000585
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000586 def test_args_string(self):
587 # args is a string
588 f, fname = mkstemp()
589 os.write(f, "#!/bin/sh\n")
590 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
591 sys.executable)
592 os.close(f)
593 os.chmod(fname, 0o700)
594 p = subprocess.Popen(fname)
595 p.wait()
596 os.remove(fname)
597 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000598
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000599 def test_invalid_args(self):
600 # invalid arguments should raise ValueError
601 self.assertRaises(ValueError, subprocess.call,
602 [sys.executable, "-c",
603 "import sys; sys.exit(47)"],
604 startupinfo=47)
605 self.assertRaises(ValueError, subprocess.call,
606 [sys.executable, "-c",
607 "import sys; sys.exit(47)"],
608 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000609
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000610 def test_shell_sequence(self):
611 # Run command through the shell (sequence)
612 newenv = os.environ.copy()
613 newenv["FRUIT"] = "apple"
614 p = subprocess.Popen(["echo $FRUIT"], shell=1,
615 stdout=subprocess.PIPE,
616 env=newenv)
617 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000618
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000619 def test_shell_string(self):
620 # Run command through the shell (string)
621 newenv = os.environ.copy()
622 newenv["FRUIT"] = "apple"
623 p = subprocess.Popen("echo $FRUIT", shell=1,
624 stdout=subprocess.PIPE,
625 env=newenv)
626 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000627
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000628 def test_call_string(self):
629 # call() function with string argument on UNIX
630 f, fname = mkstemp()
631 os.write(f, "#!/bin/sh\n")
632 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
633 sys.executable)
634 os.close(f)
635 os.chmod(fname, 0700)
636 rc = subprocess.call(fname)
637 os.remove(fname)
638 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000639
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000640 @unittest.skip("See issue #2777")
641 def test_send_signal(self):
642 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimese74c8f22008-04-19 02:23:57 +0000643
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000644 self.assertIs(p.poll(), None)
645 p.send_signal(signal.SIGINT)
Florent Xicluna78fd5212010-02-27 21:15:27 +0000646 self.assertNotEqual(p.wait(), 0)
Christian Heimese74c8f22008-04-19 02:23:57 +0000647
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000648 @unittest.skip("See issue #2777")
649 def test_kill(self):
650 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimese74c8f22008-04-19 02:23:57 +0000651
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000652 self.assertIs(p.poll(), None)
653 p.kill()
654 self.assertEqual(p.wait(), -signal.SIGKILL)
Christian Heimese74c8f22008-04-19 02:23:57 +0000655
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000656 @unittest.skip("See issue #2777")
657 def test_terminate(self):
658 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimese74c8f22008-04-19 02:23:57 +0000659
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000660 self.assertIs(p.poll(), None)
661 p.terminate()
662 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000663
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000664
665@unittest.skipUnless(sys.platform == "win32", "Windows specific tests")
666class Win32ProcessTestCase(unittest.TestCase):
667 def setUp(self):
668 # Try to minimize the number of children we have so this test
669 # doesn't crash on some buildbots (Alphas in particular).
670 test_support.reap_children()
671
672 def test_startupinfo(self):
673 # startupinfo argument
674 # We uses hardcoded constants, because we do not want to
675 # depend on win32all.
676 STARTF_USESHOWWINDOW = 1
677 SW_MAXIMIZE = 3
678 startupinfo = subprocess.STARTUPINFO()
679 startupinfo.dwFlags = STARTF_USESHOWWINDOW
680 startupinfo.wShowWindow = SW_MAXIMIZE
681 # Since Python is a console process, it won't be affected
682 # by wShowWindow, but the argument should be silently
683 # ignored
684 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000685 startupinfo=startupinfo)
686
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000687 def test_creationflags(self):
688 # creationflags argument
689 CREATE_NEW_CONSOLE = 16
690 sys.stderr.write(" a DOS box should flash briefly ...\n")
691 subprocess.call(sys.executable +
692 ' -c "import time; time.sleep(0.25)"',
693 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000694
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000695 def test_invalid_args(self):
696 # invalid arguments should raise ValueError
697 self.assertRaises(ValueError, subprocess.call,
698 [sys.executable, "-c",
699 "import sys; sys.exit(47)"],
700 preexec_fn=lambda: 1)
701 self.assertRaises(ValueError, subprocess.call,
702 [sys.executable, "-c",
703 "import sys; sys.exit(47)"],
704 stdout=subprocess.PIPE,
705 close_fds=True)
706
707 def test_close_fds(self):
708 # close file descriptors
709 rc = subprocess.call([sys.executable, "-c",
710 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000711 close_fds=True)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000712 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000713
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000714 def test_shell_sequence(self):
715 # Run command through the shell (sequence)
716 newenv = os.environ.copy()
717 newenv["FRUIT"] = "physalis"
718 p = subprocess.Popen(["set"], shell=1,
719 stdout=subprocess.PIPE,
720 env=newenv)
721 self.assertIn("physalis", p.stdout.read())
Peter Astrand81a191b2007-05-26 22:18:20 +0000722
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000723 def test_shell_string(self):
724 # Run command through the shell (string)
725 newenv = os.environ.copy()
726 newenv["FRUIT"] = "physalis"
727 p = subprocess.Popen("set", shell=1,
728 stdout=subprocess.PIPE,
729 env=newenv)
730 self.assertIn("physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000731
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000732 def test_call_string(self):
733 # call() function with string argument on Windows
734 rc = subprocess.call(sys.executable +
735 ' -c "import sys; sys.exit(47)"')
736 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000737
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000738 @unittest.skip("See issue #2777")
739 def test_send_signal(self):
740 p = subprocess.Popen([sys.executable, "-c", "input()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000741
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000742 self.assertIs(p.poll(), None)
743 p.send_signal(signal.SIGTERM)
Florent Xicluna78fd5212010-02-27 21:15:27 +0000744 self.assertNotEqual(p.wait(), 0)
Christian Heimese74c8f22008-04-19 02:23:57 +0000745
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000746 @unittest.skip("See issue #2777")
747 def test_kill(self):
748 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimese74c8f22008-04-19 02:23:57 +0000749
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000750 self.assertIs(p.poll(), None)
751 p.kill()
Florent Xicluna78fd5212010-02-27 21:15:27 +0000752 self.assertNotEqual(p.wait(), 0)
Christian Heimese74c8f22008-04-19 02:23:57 +0000753
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000754 @unittest.skip("See issue #2777")
755 def test_terminate(self):
756 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimese74c8f22008-04-19 02:23:57 +0000757
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000758 self.assertIs(p.poll(), None)
759 p.terminate()
Florent Xicluna78fd5212010-02-27 21:15:27 +0000760 self.assertNotEqual(p.wait(), 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000761
Gregory P. Smithdd7ca242009-07-04 01:49:29 +0000762
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000763@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
764 "poll system call not supported")
765class ProcessTestCaseNoPoll(ProcessTestCase):
766 def setUp(self):
767 subprocess._has_poll = False
768 ProcessTestCase.setUp(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +0000769
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000770 def tearDown(self):
771 subprocess._has_poll = True
772 ProcessTestCase.tearDown(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +0000773
774
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000775def test_main():
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000776 unit_tests = (ProcessTestCase,
777 POSIXProcessTestCase,
778 Win32ProcessTestCase,
779 ProcessTestCaseNoPoll)
780
Gregory P. Smithdd7ca242009-07-04 01:49:29 +0000781 test_support.run_unittest(*unit_tests)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000782 test_support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000783
784if __name__ == "__main__":
785 test_main()