blob: 4e45e116f1e015393d7c2deaaa73455990ce575c [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
Gregory P. Smithcce211f2010-03-01 00:05:08 +00007import errno
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00008import tempfile
9import time
Tim Peters3761e8d2004-10-13 04:07:12 +000010import re
Ezio Melotti8f6a2872010-02-10 21:40:33 +000011import sysconfig
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000012
13mswindows = (sys.platform == "win32")
14
15#
16# Depends on the following external programs: Python
17#
18
19if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000020 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
21 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000022else:
23 SETBINARY = ''
24
Florent Xicluna98e3fc32010-02-27 19:20:50 +000025
26try:
27 mkstemp = tempfile.mkstemp
28except AttributeError:
29 # tempfile.mkstemp is not available
30 def mkstemp():
31 """Replacement for mkstemp, calling mktemp."""
32 fname = tempfile.mktemp()
33 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
34
Tim Peters3761e8d2004-10-13 04:07:12 +000035
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000036class ProcessTestCase(unittest.TestCase):
Neal Norwitzb15ac312006-06-29 04:10:08 +000037 def setUp(self):
Tim Peters38ff36c2006-06-30 06:18:39 +000038 # Try to minimize the number of children we have so this test
39 # doesn't crash on some buildbots (Alphas in particular).
Florent Xicluna98e3fc32010-02-27 19:20:50 +000040 test_support.reap_children()
Neal Norwitzb15ac312006-06-29 04:10:08 +000041
Florent Xicluna98e3fc32010-02-27 19:20:50 +000042 def assertStderrEqual(self, stderr, expected, msg=None):
43 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
44 # shutdown time. That frustrates tests trying to check stderr produced
45 # from a spawned Python process.
46 actual = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
47 self.assertEqual(actual, expected, msg)
Neal Norwitzb15ac312006-06-29 04:10:08 +000048
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000049 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000050 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000051 rc = subprocess.call([sys.executable, "-c",
52 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000053 self.assertEqual(rc, 47)
54
Peter Astrand454f7672005-01-01 09:36:35 +000055 def test_check_call_zero(self):
56 # check_call() function with zero return code
57 rc = subprocess.check_call([sys.executable, "-c",
58 "import sys; sys.exit(0)"])
59 self.assertEqual(rc, 0)
60
61 def test_check_call_nonzero(self):
62 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +000063 with self.assertRaises(subprocess.CalledProcessError) as c:
Peter Astrand454f7672005-01-01 09:36:35 +000064 subprocess.check_call([sys.executable, "-c",
65 "import sys; sys.exit(47)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +000066 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000067
Gregory P. Smith26576802008-12-05 02:27:01 +000068 def test_check_output(self):
69 # check_output() function with zero return code
70 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000071 [sys.executable, "-c", "print 'BDFL'"])
Ezio Melottiaa980582010-01-23 23:04:36 +000072 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000073
Gregory P. Smith26576802008-12-05 02:27:01 +000074 def test_check_output_nonzero(self):
Gregory P. Smith97f49f42008-12-04 20:21:09 +000075 # check_call() function with non-zero return code
Florent Xicluna98e3fc32010-02-27 19:20:50 +000076 with self.assertRaises(subprocess.CalledProcessError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +000077 subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000078 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xicluna98e3fc32010-02-27 19:20:50 +000079 self.assertEqual(c.exception.returncode, 5)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000080
Gregory P. Smith26576802008-12-05 02:27:01 +000081 def test_check_output_stderr(self):
82 # check_output() function stderr redirected to stdout
83 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000084 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
85 stderr=subprocess.STDOUT)
Ezio Melottiaa980582010-01-23 23:04:36 +000086 self.assertIn('BDFL', output)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000087
Gregory P. Smith26576802008-12-05 02:27:01 +000088 def test_check_output_stdout_arg(self):
89 # check_output() function stderr redirected to stdout
Florent Xicluna98e3fc32010-02-27 19:20:50 +000090 with self.assertRaises(ValueError) as c:
Gregory P. Smith26576802008-12-05 02:27:01 +000091 output = subprocess.check_output(
Gregory P. Smith97f49f42008-12-04 20:21:09 +000092 [sys.executable, "-c", "print 'will not be run'"],
93 stdout=sys.stdout)
Gregory P. Smith97f49f42008-12-04 20:21:09 +000094 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xicluna98e3fc32010-02-27 19:20:50 +000095 self.assertIn('stdout', c.exception.args[0])
Gregory P. Smith97f49f42008-12-04 20:21:09 +000096
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000097 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +000098 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000099 newenv = os.environ.copy()
100 newenv["FRUIT"] = "banana"
101 rc = subprocess.call([sys.executable, "-c",
102 'import sys, os;' \
103 'sys.exit(os.getenv("FRUIT")=="banana")'],
104 env=newenv)
105 self.assertEqual(rc, 1)
106
107 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000108 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000109 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
110 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
111 p.wait()
112 self.assertEqual(p.stdin, None)
113
114 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000115 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +0000116 p = subprocess.Popen([sys.executable, "-c",
Tim Peters4052fe52004-10-13 03:29:54 +0000117 'print " this bit of output is from a '
118 'test of stdout in a different '
119 'process ..."'],
120 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000121 p.wait()
122 self.assertEqual(p.stdout, None)
123
124 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000125 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000126 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
127 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
128 p.wait()
129 self.assertEqual(p.stderr, None)
130
Ezio Melotti8f6a2872010-02-10 21:40:33 +0000131 def test_executable_with_cwd(self):
132 python_dir = os.path.dirname(os.path.realpath(sys.executable))
133 p = subprocess.Popen(["somethingyoudonthave", "-c",
134 "import sys; sys.exit(47)"],
135 executable=sys.executable, cwd=python_dir)
136 p.wait()
137 self.assertEqual(p.returncode, 47)
138
139 @unittest.skipIf(sysconfig.is_python_build(),
140 "need an installed Python. See #7774")
141 def test_executable_without_cwd(self):
142 # For a normal installation, it should work without 'cwd'
143 # argument. For test runs in the build directory, see #7774.
144 p = subprocess.Popen(["somethingyoudonthave", "-c",
145 "import sys; sys.exit(47)"],
Tim Peters3b01a702004-10-12 22:19:32 +0000146 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000147 p.wait()
148 self.assertEqual(p.returncode, 47)
149
150 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000151 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000152 p = subprocess.Popen([sys.executable, "-c",
153 'import sys; sys.exit(sys.stdin.read() == "pear")'],
154 stdin=subprocess.PIPE)
155 p.stdin.write("pear")
156 p.stdin.close()
157 p.wait()
158 self.assertEqual(p.returncode, 1)
159
160 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000161 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000162 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000163 d = tf.fileno()
164 os.write(d, "pear")
165 os.lseek(d, 0, 0)
166 p = subprocess.Popen([sys.executable, "-c",
167 'import sys; sys.exit(sys.stdin.read() == "pear")'],
168 stdin=d)
169 p.wait()
170 self.assertEqual(p.returncode, 1)
171
172 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000173 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000174 tf = tempfile.TemporaryFile()
175 tf.write("pear")
176 tf.seek(0)
177 p = subprocess.Popen([sys.executable, "-c",
178 'import sys; sys.exit(sys.stdin.read() == "pear")'],
179 stdin=tf)
180 p.wait()
181 self.assertEqual(p.returncode, 1)
182
183 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000184 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000185 p = subprocess.Popen([sys.executable, "-c",
186 'import sys; sys.stdout.write("orange")'],
187 stdout=subprocess.PIPE)
188 self.assertEqual(p.stdout.read(), "orange")
189
190 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000191 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000192 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000193 d = tf.fileno()
194 p = subprocess.Popen([sys.executable, "-c",
195 'import sys; sys.stdout.write("orange")'],
196 stdout=d)
197 p.wait()
198 os.lseek(d, 0, 0)
199 self.assertEqual(os.read(d, 1024), "orange")
200
201 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000202 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000203 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000204 p = subprocess.Popen([sys.executable, "-c",
205 'import sys; sys.stdout.write("orange")'],
206 stdout=tf)
207 p.wait()
208 tf.seek(0)
209 self.assertEqual(tf.read(), "orange")
210
211 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000212 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000213 p = subprocess.Popen([sys.executable, "-c",
214 'import sys; sys.stderr.write("strawberry")'],
215 stderr=subprocess.PIPE)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000216 self.assertStderrEqual(p.stderr.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000217
218 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000219 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000220 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000221 d = tf.fileno()
222 p = subprocess.Popen([sys.executable, "-c",
223 'import sys; sys.stderr.write("strawberry")'],
224 stderr=d)
225 p.wait()
226 os.lseek(d, 0, 0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000227 self.assertStderrEqual(os.read(d, 1024), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000228
229 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000230 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000231 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000232 p = subprocess.Popen([sys.executable, "-c",
233 'import sys; sys.stderr.write("strawberry")'],
234 stderr=tf)
235 p.wait()
236 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000237 self.assertStderrEqual(tf.read(), "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000238
239 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000240 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000241 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000242 'import sys;'
243 'sys.stdout.write("apple");'
244 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000245 'sys.stderr.write("orange")'],
246 stdout=subprocess.PIPE,
247 stderr=subprocess.STDOUT)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000248 self.assertStderrEqual(p.stdout.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000249
250 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000251 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000252 tf = tempfile.TemporaryFile()
253 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000254 'import sys;'
255 'sys.stdout.write("apple");'
256 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000257 'sys.stderr.write("orange")'],
258 stdout=tf,
259 stderr=tf)
260 p.wait()
261 tf.seek(0)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000262 self.assertStderrEqual(tf.read(), "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000263
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000264 def test_stdout_filedes_of_stdout(self):
265 # stdout is set to 1 (#1531862).
266 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))"
267 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000268 self.assertEqual(rc, 2)
Gustavo Niemeyerc36bede2006-09-07 00:48:33 +0000269
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000270 def test_cwd(self):
Guido van Rossume9a0e882007-12-20 17:28:10 +0000271 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000272 # We cannot use os.path.realpath to canonicalize the path,
273 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
274 cwd = os.getcwd()
275 os.chdir(tmpdir)
276 tmpdir = os.getcwd()
277 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000278 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000279 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000280 'sys.stdout.write(os.getcwd())'],
281 stdout=subprocess.PIPE,
282 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000283 normcase = os.path.normcase
284 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000285
286 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000287 newenv = os.environ.copy()
288 newenv["FRUIT"] = "orange"
289 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000290 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000291 'sys.stdout.write(os.getenv("FRUIT"))'],
292 stdout=subprocess.PIPE,
293 env=newenv)
294 self.assertEqual(p.stdout.read(), "orange")
295
Peter Astrandcbac93c2005-03-03 20:24:28 +0000296 def test_communicate_stdin(self):
297 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000298 'import sys;'
299 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000300 stdin=subprocess.PIPE)
301 p.communicate("pear")
302 self.assertEqual(p.returncode, 1)
303
304 def test_communicate_stdout(self):
305 p = subprocess.Popen([sys.executable, "-c",
306 'import sys; sys.stdout.write("pineapple")'],
307 stdout=subprocess.PIPE)
308 (stdout, stderr) = p.communicate()
309 self.assertEqual(stdout, "pineapple")
310 self.assertEqual(stderr, None)
311
312 def test_communicate_stderr(self):
313 p = subprocess.Popen([sys.executable, "-c",
314 'import sys; sys.stderr.write("pineapple")'],
315 stderr=subprocess.PIPE)
316 (stdout, stderr) = p.communicate()
317 self.assertEqual(stdout, None)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000318 self.assertStderrEqual(stderr, "pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000319
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000320 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000321 p = subprocess.Popen([sys.executable, "-c",
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000322 'import sys,os;'
323 'sys.stderr.write("pineapple");'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000324 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000325 stdin=subprocess.PIPE,
326 stdout=subprocess.PIPE,
327 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000328 (stdout, stderr) = p.communicate("banana")
329 self.assertEqual(stdout, "banana")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000330 self.assertStderrEqual(stderr, "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000331
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000332 # This test is Linux specific for simplicity to at least have
333 # some coverage. It is not a platform specific bug.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000334 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
335 "Linux specific")
336 # Test for the fd leak reported in http://bugs.python.org/issue2791.
337 def test_communicate_pipe_fd_leak(self):
338 fd_directory = '/proc/%d/fd' % os.getpid()
339 num_fds_before_popen = len(os.listdir(fd_directory))
340 p = subprocess.Popen([sys.executable, "-c", "print()"],
341 stdout=subprocess.PIPE)
342 p.communicate()
343 num_fds_after_communicate = len(os.listdir(fd_directory))
344 del p
345 num_fds_after_destruction = len(os.listdir(fd_directory))
346 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
347 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
Gregory P. Smith4036fd42008-05-26 20:22:14 +0000348
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000349 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000350 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000351 p = subprocess.Popen([sys.executable, "-c",
352 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000353 (stdout, stderr) = p.communicate()
354 self.assertEqual(stdout, None)
355 self.assertEqual(stderr, None)
356
357 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000358 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000359 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000360 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000361 x, y = os.pipe()
362 if mswindows:
363 pipe_buf = 512
364 else:
365 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
366 os.close(x)
367 os.close(y)
368 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000369 'import sys,os;'
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000370 'sys.stdout.write(sys.stdin.read(47));'
371 'sys.stderr.write("xyz"*%d);'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000372 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000373 stdin=subprocess.PIPE,
374 stdout=subprocess.PIPE,
375 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000376 string_to_write = "abc"*pipe_buf
377 (stdout, stderr) = p.communicate(string_to_write)
378 self.assertEqual(stdout, string_to_write)
379
380 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000381 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000382 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000383 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000384 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000385 stdin=subprocess.PIPE,
386 stdout=subprocess.PIPE,
387 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000388 p.stdin.write("banana")
389 (stdout, stderr) = p.communicate("split")
390 self.assertEqual(stdout, "bananasplit")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000391 self.assertStderrEqual(stderr, "")
Tim Peterse718f612004-10-12 21:51:32 +0000392
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000393 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000394 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000395 'import sys,os;' + SETBINARY +
396 'sys.stdout.write("line1\\n");'
397 'sys.stdout.flush();'
398 'sys.stdout.write("line2\\r");'
399 'sys.stdout.flush();'
400 'sys.stdout.write("line3\\r\\n");'
401 'sys.stdout.flush();'
402 'sys.stdout.write("line4\\r");'
403 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000404 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000405 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000406 'sys.stdout.write("\\nline6");'],
407 stdout=subprocess.PIPE,
408 universal_newlines=1)
409 stdout = p.stdout.read()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000410 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000411 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000412 self.assertEqual(stdout,
413 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000414 else:
415 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000416 self.assertEqual(stdout,
417 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000418
419 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000420 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000421 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000422 'import sys,os;' + SETBINARY +
423 'sys.stdout.write("line1\\n");'
424 'sys.stdout.flush();'
425 'sys.stdout.write("line2\\r");'
426 'sys.stdout.flush();'
427 'sys.stdout.write("line3\\r\\n");'
428 'sys.stdout.flush();'
429 'sys.stdout.write("line4\\r");'
430 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000431 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000432 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000433 'sys.stdout.write("\\nline6");'],
434 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
435 universal_newlines=1)
436 (stdout, stderr) = p.communicate()
Neal Norwitza6d01ce2006-05-02 06:23:22 +0000437 if hasattr(file, 'newlines'):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000438 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000439 self.assertEqual(stdout,
440 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000441 else:
442 # Interpreter without universal newline support
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000443 self.assertEqual(stdout,
444 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000445
446 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000447 # Make sure we leak no resources
Peter Astrandd6b24302006-06-22 20:06:46 +0000448 if not hasattr(test_support, "is_resource_enabled") \
449 or test_support.is_resource_enabled("subprocess") and not mswindows:
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000450 max_handles = 1026 # too much for most UNIX systems
451 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000452 max_handles = 65
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000453 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000454 p = subprocess.Popen([sys.executable, "-c",
455 "import sys;sys.stdout.write(sys.stdin.read())"],
456 stdin=subprocess.PIPE,
457 stdout=subprocess.PIPE,
458 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000459 data = p.communicate("lime")[0]
460 self.assertEqual(data, "lime")
461
462
463 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000464 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
465 '"a b c" d e')
466 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
467 'ab\\"c \\ d')
Gregory P. Smithe047e6d2008-01-19 20:49:02 +0000468 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
469 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000470 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
471 'a\\\\\\b "de fg" h')
472 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
473 'a\\\\\\"b c d')
474 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
475 '"a\\\\b c" d e')
476 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
477 '"a\\\\b\\ c" d e')
Peter Astrand10514a72007-01-13 22:35:35 +0000478 self.assertEqual(subprocess.list2cmdline(['ab', '']),
479 'ab ""')
Gregory P. Smith70eb2f92008-01-19 22:49:37 +0000480 self.assertEqual(subprocess.list2cmdline(['echo', 'foo|bar']),
481 'echo "foo|bar"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000482
483
484 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000485 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000486 "-c", "import time; time.sleep(1)"])
487 count = 0
488 while p.poll() is None:
489 time.sleep(0.1)
490 count += 1
491 # We expect that the poll loop probably went around about 10 times,
492 # but, based on system scheduling we can't control, it's possible
493 # poll() never returned None. It "should be" very rare that it
494 # didn't go around at least twice.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000495 self.assertGreaterEqual(count, 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000496 # Subsequent invocations should just return the returncode
497 self.assertEqual(p.poll(), 0)
498
499
500 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000501 p = subprocess.Popen([sys.executable,
502 "-c", "import time; time.sleep(2)"])
503 self.assertEqual(p.wait(), 0)
504 # Subsequent invocations should just return the returncode
505 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000506
Peter Astrand738131d2004-11-30 21:04:45 +0000507
508 def test_invalid_bufsize(self):
509 # an invalid type of the bufsize argument should raise
510 # TypeError.
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000511 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +0000512 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +0000513
Georg Brandlf3715d22009-02-14 17:01:36 +0000514 def test_leaking_fds_on_error(self):
515 # see bug #5179: Popen leaks file descriptors to PIPEs if
516 # the child fails to execute; this will eventually exhaust
517 # the maximum number of open fds. 1024 seems a very common
518 # value for that limit, but Windows has 2048, so we loop
519 # 1024 times (each call leaked two fds).
520 for i in range(1024):
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000521 # Windows raises IOError. Others raise OSError.
522 with self.assertRaises(EnvironmentError) as c:
Georg Brandlf3715d22009-02-14 17:01:36 +0000523 subprocess.Popen(['nonexisting_i_hope'],
524 stdout=subprocess.PIPE,
525 stderr=subprocess.PIPE)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000526 if c.exception.errno != 2: # ignore "no such file"
527 raise c.exception
Georg Brandlf3715d22009-02-14 17:01:36 +0000528
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000529
530# context manager
531class _SuppressCoreFiles(object):
532 """Try to prevent core files from being created."""
533 old_limit = None
534
535 def __enter__(self):
536 """Try to save previous ulimit, then set it to (0, 0)."""
537 try:
538 import resource
539 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
540 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
541 except (ImportError, ValueError, resource.error):
542 pass
543
544 def __exit__(self, *args):
545 """Return core file behavior to default."""
546 if self.old_limit is None:
547 return
548 try:
549 import resource
550 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
551 except (ImportError, ValueError, resource.error):
552 pass
553
554
555@unittest.skipIf(sys.platform == "win32", "POSIX specific tests")
556class POSIXProcessTestCase(unittest.TestCase):
557 def setUp(self):
558 # Try to minimize the number of children we have so this test
559 # doesn't crash on some buildbots (Alphas in particular).
560 test_support.reap_children()
561
562 def test_exceptions(self):
563 # caught & re-raised exceptions
564 with self.assertRaises(OSError) as c:
565 p = subprocess.Popen([sys.executable, "-c", ""],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000566 cwd="/this/path/does/not/exist")
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000567 # The attribute child_traceback should contain "os.chdir" somewhere.
568 self.assertIn("os.chdir", c.exception.child_traceback)
Tim Peterse718f612004-10-12 21:51:32 +0000569
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000570 def test_run_abort(self):
571 # returncode handles signal termination
572 with _SuppressCoreFiles():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000573 p = subprocess.Popen([sys.executable, "-c",
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000574 "import os; os.abort()"])
575 p.wait()
576 self.assertEqual(-p.returncode, signal.SIGABRT)
577
578 def test_preexec(self):
579 # preexec function
580 p = subprocess.Popen([sys.executable, "-c",
581 "import sys, os;"
582 "sys.stdout.write(os.getenv('FRUIT'))"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000583 stdout=subprocess.PIPE,
584 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000585 self.assertEqual(p.stdout.read(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000586
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000587 def test_args_string(self):
588 # args is a string
589 f, fname = mkstemp()
590 os.write(f, "#!/bin/sh\n")
591 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
592 sys.executable)
593 os.close(f)
594 os.chmod(fname, 0o700)
595 p = subprocess.Popen(fname)
596 p.wait()
597 os.remove(fname)
598 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000599
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000600 def test_invalid_args(self):
601 # invalid arguments should raise ValueError
602 self.assertRaises(ValueError, subprocess.call,
603 [sys.executable, "-c",
604 "import sys; sys.exit(47)"],
605 startupinfo=47)
606 self.assertRaises(ValueError, subprocess.call,
607 [sys.executable, "-c",
608 "import sys; sys.exit(47)"],
609 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000610
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000611 def test_shell_sequence(self):
612 # Run command through the shell (sequence)
613 newenv = os.environ.copy()
614 newenv["FRUIT"] = "apple"
615 p = subprocess.Popen(["echo $FRUIT"], shell=1,
616 stdout=subprocess.PIPE,
617 env=newenv)
618 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000619
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000620 def test_shell_string(self):
621 # Run command through the shell (string)
622 newenv = os.environ.copy()
623 newenv["FRUIT"] = "apple"
624 p = subprocess.Popen("echo $FRUIT", shell=1,
625 stdout=subprocess.PIPE,
626 env=newenv)
627 self.assertEqual(p.stdout.read().strip(), "apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000628
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000629 def test_call_string(self):
630 # call() function with string argument on UNIX
631 f, fname = mkstemp()
632 os.write(f, "#!/bin/sh\n")
633 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
634 sys.executable)
635 os.close(f)
636 os.chmod(fname, 0700)
637 rc = subprocess.call(fname)
638 os.remove(fname)
639 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000640
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000641 @unittest.skip("See issue #2777")
642 def test_send_signal(self):
643 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimese74c8f22008-04-19 02:23:57 +0000644
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000645 self.assertIs(p.poll(), None)
646 p.send_signal(signal.SIGINT)
Florent Xicluna78fd5212010-02-27 21:15:27 +0000647 self.assertNotEqual(p.wait(), 0)
Christian Heimese74c8f22008-04-19 02:23:57 +0000648
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000649 @unittest.skip("See issue #2777")
650 def test_kill(self):
651 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimese74c8f22008-04-19 02:23:57 +0000652
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000653 self.assertIs(p.poll(), None)
654 p.kill()
655 self.assertEqual(p.wait(), -signal.SIGKILL)
Christian Heimese74c8f22008-04-19 02:23:57 +0000656
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000657 @unittest.skip("See issue #2777")
658 def test_terminate(self):
659 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimese74c8f22008-04-19 02:23:57 +0000660
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000661 self.assertIs(p.poll(), None)
662 p.terminate()
663 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000664
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000665
666@unittest.skipUnless(sys.platform == "win32", "Windows specific tests")
667class Win32ProcessTestCase(unittest.TestCase):
668 def setUp(self):
669 # Try to minimize the number of children we have so this test
670 # doesn't crash on some buildbots (Alphas in particular).
671 test_support.reap_children()
672
673 def test_startupinfo(self):
674 # startupinfo argument
675 # We uses hardcoded constants, because we do not want to
676 # depend on win32all.
677 STARTF_USESHOWWINDOW = 1
678 SW_MAXIMIZE = 3
679 startupinfo = subprocess.STARTUPINFO()
680 startupinfo.dwFlags = STARTF_USESHOWWINDOW
681 startupinfo.wShowWindow = SW_MAXIMIZE
682 # Since Python is a console process, it won't be affected
683 # by wShowWindow, but the argument should be silently
684 # ignored
685 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000686 startupinfo=startupinfo)
687
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000688 def test_creationflags(self):
689 # creationflags argument
690 CREATE_NEW_CONSOLE = 16
691 sys.stderr.write(" a DOS box should flash briefly ...\n")
692 subprocess.call(sys.executable +
693 ' -c "import time; time.sleep(0.25)"',
694 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000695
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000696 def test_invalid_args(self):
697 # invalid arguments should raise ValueError
698 self.assertRaises(ValueError, subprocess.call,
699 [sys.executable, "-c",
700 "import sys; sys.exit(47)"],
701 preexec_fn=lambda: 1)
702 self.assertRaises(ValueError, subprocess.call,
703 [sys.executable, "-c",
704 "import sys; sys.exit(47)"],
705 stdout=subprocess.PIPE,
706 close_fds=True)
707
708 def test_close_fds(self):
709 # close file descriptors
710 rc = subprocess.call([sys.executable, "-c",
711 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000712 close_fds=True)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000713 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000714
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000715 def test_shell_sequence(self):
716 # Run command through the shell (sequence)
717 newenv = os.environ.copy()
718 newenv["FRUIT"] = "physalis"
719 p = subprocess.Popen(["set"], shell=1,
720 stdout=subprocess.PIPE,
721 env=newenv)
722 self.assertIn("physalis", p.stdout.read())
Peter Astrand81a191b2007-05-26 22:18:20 +0000723
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000724 def test_shell_string(self):
725 # Run command through the shell (string)
726 newenv = os.environ.copy()
727 newenv["FRUIT"] = "physalis"
728 p = subprocess.Popen("set", shell=1,
729 stdout=subprocess.PIPE,
730 env=newenv)
731 self.assertIn("physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000732
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000733 def test_call_string(self):
734 # call() function with string argument on Windows
735 rc = subprocess.call(sys.executable +
736 ' -c "import sys; sys.exit(47)"')
737 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000738
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000739 @unittest.skip("See issue #2777")
740 def test_send_signal(self):
741 p = subprocess.Popen([sys.executable, "-c", "input()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000742
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000743 self.assertIs(p.poll(), None)
744 p.send_signal(signal.SIGTERM)
Florent Xicluna78fd5212010-02-27 21:15:27 +0000745 self.assertNotEqual(p.wait(), 0)
Christian Heimese74c8f22008-04-19 02:23:57 +0000746
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000747 @unittest.skip("See issue #2777")
748 def test_kill(self):
749 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimese74c8f22008-04-19 02:23:57 +0000750
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000751 self.assertIs(p.poll(), None)
752 p.kill()
Florent Xicluna78fd5212010-02-27 21:15:27 +0000753 self.assertNotEqual(p.wait(), 0)
Christian Heimese74c8f22008-04-19 02:23:57 +0000754
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000755 @unittest.skip("See issue #2777")
756 def test_terminate(self):
757 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimese74c8f22008-04-19 02:23:57 +0000758
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000759 self.assertIs(p.poll(), None)
760 p.terminate()
Florent Xicluna78fd5212010-02-27 21:15:27 +0000761 self.assertNotEqual(p.wait(), 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000762
Gregory P. Smithdd7ca242009-07-04 01:49:29 +0000763
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000764@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
765 "poll system call not supported")
766class ProcessTestCaseNoPoll(ProcessTestCase):
767 def setUp(self):
768 subprocess._has_poll = False
769 ProcessTestCase.setUp(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +0000770
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000771 def tearDown(self):
772 subprocess._has_poll = True
773 ProcessTestCase.tearDown(self)
Gregory P. Smithdd7ca242009-07-04 01:49:29 +0000774
775
Gregory P. Smithcce211f2010-03-01 00:05:08 +0000776class HelperFunctionTests(unittest.TestCase):
777 def test_eintr_retry_call(self):
778 record_calls = []
779 def fake_os_func(*args):
780 record_calls.append(args)
781 if len(record_calls) == 2:
782 raise OSError(errno.EINTR, "fake interrupted system call")
783 return tuple(reversed(args))
784
785 self.assertEqual((999, 256),
786 subprocess._eintr_retry_call(fake_os_func, 256, 999))
787 self.assertEqual([(256, 999)], record_calls)
788 # This time there will be an EINTR so it will loop once.
789 self.assertEqual((666,),
790 subprocess._eintr_retry_call(fake_os_func, 666))
791 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
792
793
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000794def test_main():
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000795 unit_tests = (ProcessTestCase,
796 POSIXProcessTestCase,
797 Win32ProcessTestCase,
Gregory P. Smithcce211f2010-03-01 00:05:08 +0000798 ProcessTestCaseNoPoll,
799 HelperFunctionTests)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000800
Gregory P. Smithdd7ca242009-07-04 01:49:29 +0000801 test_support.run_unittest(*unit_tests)
Florent Xicluna98e3fc32010-02-27 19:20:50 +0000802 test_support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000803
804if __name__ == "__main__":
805 test_main()