blob: 24fa79bb1343068c2df3088743de33ed1331489a [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00003import subprocess
4import sys
5import signal
6import os
7import tempfile
8import time
Tim Peters3761e8d2004-10-13 04:07:12 +00009import re
Ezio Melotti184bdfb2010-02-18 09:37:05 +000010import sysconfig
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000011
12mswindows = (sys.platform == "win32")
13
14#
15# Depends on the following external programs: Python
16#
17
18if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000019 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
20 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000021else:
22 SETBINARY = ''
23
Florent Xiclunab1e94e82010-02-27 22:12:37 +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):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000036 def setUp(self):
37 # Try to minimize the number of children we have so this test
38 # doesn't crash on some buildbots (Alphas in particular).
Florent Xiclunab1e94e82010-02-27 22:12:37 +000039 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000040
Florent Xiclunab1e94e82010-02-27 22:12:37 +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("\[\d+ refs\]\r?\n?$", "", stderr.decode()).encode()
46 self.assertEqual(actual, expected, msg)
Thomas Wouters0e3f5912006-08-11 14:57:12 +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 Xiclunab1e94e82010-02-27 22:12:37 +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 Xiclunab1e94e82010-02-27 22:12:37 +000065 self.assertEqual(c.exception.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000066
Georg Brandlf9734072008-12-07 15:30:06 +000067 def test_check_output(self):
68 # check_output() function with zero return code
69 output = subprocess.check_output(
70 [sys.executable, "-c", "print('BDFL')"])
Benjamin Peterson577473f2010-01-19 00:09:57 +000071 self.assertIn(b'BDFL', output)
Georg Brandlf9734072008-12-07 15:30:06 +000072
73 def test_check_output_nonzero(self):
74 # check_call() function with non-zero return code
Florent Xiclunab1e94e82010-02-27 22:12:37 +000075 with self.assertRaises(subprocess.CalledProcessError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +000076 subprocess.check_output(
77 [sys.executable, "-c", "import sys; sys.exit(5)"])
Florent Xiclunab1e94e82010-02-27 22:12:37 +000078 self.assertEqual(c.exception.returncode, 5)
Georg Brandlf9734072008-12-07 15:30:06 +000079
80 def test_check_output_stderr(self):
81 # check_output() function stderr redirected to stdout
82 output = subprocess.check_output(
83 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
84 stderr=subprocess.STDOUT)
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_stdout_arg(self):
88 # check_output() function stderr redirected to stdout
Florent Xiclunab1e94e82010-02-27 22:12:37 +000089 with self.assertRaises(ValueError) as c:
Georg Brandlf9734072008-12-07 15:30:06 +000090 output = subprocess.check_output(
91 [sys.executable, "-c", "print('will not be run')"],
92 stdout=sys.stdout)
Georg Brandlf9734072008-12-07 15:30:06 +000093 self.fail("Expected ValueError when stdout arg supplied.")
Florent Xiclunab1e94e82010-02-27 22:12:37 +000094 self.assertIn('stdout', c.exception.args[0])
Georg Brandlf9734072008-12-07 15:30:06 +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",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000101 'import sys, os;'
102 'sys.exit(os.getenv("FRUIT")=="banana")'],
103 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000104 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
Georg Brandl88fc6642007-02-09 21:28:07 +0000108 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000109 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",
Georg Brandl88fc6642007-02-09 21:28:07 +0000116 'print(" this bit of output is from a '
Tim Peters4052fe52004-10-13 03:29:54 +0000117 'test of stdout in a different '
Georg Brandl88fc6642007-02-09 21:28:07 +0000118 'process ...")'],
Tim Peters4052fe52004-10-13 03:29:54 +0000119 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
Georg Brandl88fc6642007-02-09 21:28:07 +0000125 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000126 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
127 p.wait()
128 self.assertEqual(p.stderr, None)
129
Ezio Melotti184bdfb2010-02-18 09:37:05 +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)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000154 p.stdin.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000155 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()
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000163 os.write(d, b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000164 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()
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000174 tf.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000175 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)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000187 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000188
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)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000198 self.assertEqual(os.read(d, 1024), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000199
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)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000208 self.assertEqual(tf.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000209
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 Xiclunab1e94e82010-02-27 22:12:37 +0000215 self.assertStderrEqual(p.stderr.read(), b"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 Xiclunab1e94e82010-02-27 22:12:37 +0000226 self.assertStderrEqual(os.read(d, 1024), b"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 Xiclunab1e94e82010-02-27 22:12:37 +0000236 self.assertStderrEqual(tf.read(), b"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",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000241 'import sys;'
242 'sys.stdout.write("apple");'
243 'sys.stdout.flush();'
244 'sys.stderr.write("orange")'],
245 stdout=subprocess.PIPE,
246 stderr=subprocess.STDOUT)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000247 self.assertStderrEqual(p.stdout.read(), b"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",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000253 'import sys;'
254 'sys.stdout.write("apple");'
255 'sys.stdout.flush();'
256 'sys.stderr.write("orange")'],
257 stdout=tf,
258 stderr=tf)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000259 p.wait()
260 tf.seek(0)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000261 self.assertStderrEqual(tf.read(), b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000262
Thomas Wouters89f507f2006-12-13 04:49:30 +0000263 def test_stdout_filedes_of_stdout(self):
264 # stdout is set to 1 (#1531862).
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000265 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), b'.\n'))"
Thomas Wouters89f507f2006-12-13 04:49:30 +0000266 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000267 self.assertEqual(rc, 2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000268
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000269 def test_cwd(self):
Christian Heimes5fb7c2a2007-12-24 08:52:31 +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",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000278 'import sys,os;'
279 'sys.stdout.write(os.getcwd())'],
280 stdout=subprocess.PIPE,
281 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000282 normcase = os.path.normcase
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000283 self.assertEqual(normcase(p.stdout.read().decode("utf-8")),
284 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",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000290 'import sys,os;'
291 'sys.stdout.write(os.getenv("FRUIT"))'],
292 stdout=subprocess.PIPE,
293 env=newenv)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000294 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000295
Peter Astrandcbac93c2005-03-03 20:24:28 +0000296 def test_communicate_stdin(self):
297 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000298 'import sys;'
299 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000300 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000301 p.communicate(b"pear")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000302 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()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000309 self.assertEqual(stdout, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000310 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 Xiclunab1e94e82010-02-27 22:12:37 +0000318 self.assertStderrEqual(stderr, b"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",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000322 'import sys,os;'
323 'sys.stderr.write("pineapple");'
324 'sys.stdout.write(sys.stdin.read())'],
325 stdin=subprocess.PIPE,
326 stdout=subprocess.PIPE,
327 stderr=subprocess.PIPE)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000328 (stdout, stderr) = p.communicate(b"banana")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000329 self.assertEqual(stdout, b"banana")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000330 self.assertStderrEqual(stderr, b"pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000331
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000332 # This test is Linux specific for simplicity to at least have
333 # some coverage. It is not a platform specific bug.
Florent Xiclunab1e94e82010-02-27 22:12:37 +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)
Georg Brandlf08a9dd2008-06-10 16:57:31 +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",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000369 'import sys,os;'
370 'sys.stdout.write(sys.stdin.read(47));'
371 'sys.stderr.write("xyz"*%d);'
372 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
373 stdin=subprocess.PIPE,
374 stdout=subprocess.PIPE,
375 stderr=subprocess.PIPE)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000376 string_to_write = b"abc"*pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000377 (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",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000383 'import sys,os;'
384 'sys.stdout.write(sys.stdin.read())'],
385 stdin=subprocess.PIPE,
386 stdout=subprocess.PIPE,
387 stderr=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000388 p.stdin.write(b"banana")
389 (stdout, stderr) = p.communicate(b"split")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000390 self.assertEqual(stdout, b"bananasplit")
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000391 self.assertStderrEqual(stderr, b"")
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",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000395 'import sys,os;' + SETBINARY +
396 'sys.stdout.write("line1\\n");'
397 'sys.stdout.flush();'
398 'sys.stdout.write("line2\\n");'
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();'
404 'sys.stdout.write("\\nline5");'
405 'sys.stdout.flush();'
406 'sys.stdout.write("\\nline6");'],
407 stdout=subprocess.PIPE,
408 universal_newlines=1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000409 stdout = p.stdout.read()
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000410 self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000411
412 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000413 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000414 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000415 'import sys,os;' + SETBINARY +
416 'sys.stdout.write("line1\\n");'
417 'sys.stdout.flush();'
418 'sys.stdout.write("line2\\n");'
419 'sys.stdout.flush();'
420 'sys.stdout.write("line3\\r\\n");'
421 'sys.stdout.flush();'
422 'sys.stdout.write("line4\\r");'
423 'sys.stdout.flush();'
424 'sys.stdout.write("\\nline5");'
425 'sys.stdout.flush();'
426 'sys.stdout.write("\\nline6");'],
427 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
428 universal_newlines=1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000429 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000430 self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000431
432 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000433 # Make sure we leak no resources
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000434 if (not hasattr(support, "is_resource_enabled") or
435 support.is_resource_enabled("subprocess") and not mswindows):
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000436 max_handles = 1026 # too much for most UNIX systems
437 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000438 max_handles = 65
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000439 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000440 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000441 "import sys;"
442 "sys.stdout.write(sys.stdin.read())"],
443 stdin=subprocess.PIPE,
444 stdout=subprocess.PIPE,
445 stderr=subprocess.PIPE)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000446 data = p.communicate(b"lime")[0]
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000447 self.assertEqual(data, b"lime")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000448
449
450 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000451 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
452 '"a b c" d e')
453 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
454 'ab\\"c \\ d')
Christian Heimesfdab48e2008-01-20 09:06:41 +0000455 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
456 'ab\\"c " \\\\" d')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000457 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
458 'a\\\\\\b "de fg" h')
459 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
460 'a\\\\\\"b c d')
461 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
462 '"a\\\\b c" d e')
463 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
464 '"a\\\\b\\ c" d e')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000465 self.assertEqual(subprocess.list2cmdline(['ab', '']),
466 'ab ""')
Christian Heimesfdab48e2008-01-20 09:06:41 +0000467 self.assertEqual(subprocess.list2cmdline(['echo', 'foo|bar']),
468 'echo "foo|bar"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000469
470
471 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000472 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000473 "-c", "import time; time.sleep(1)"])
474 count = 0
475 while p.poll() is None:
476 time.sleep(0.1)
477 count += 1
478 # We expect that the poll loop probably went around about 10 times,
479 # but, based on system scheduling we can't control, it's possible
480 # poll() never returned None. It "should be" very rare that it
481 # didn't go around at least twice.
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000482 self.assertGreaterEqual(count, 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000483 # Subsequent invocations should just return the returncode
484 self.assertEqual(p.poll(), 0)
485
486
487 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000488 p = subprocess.Popen([sys.executable,
489 "-c", "import time; time.sleep(2)"])
490 self.assertEqual(p.wait(), 0)
491 # Subsequent invocations should just return the returncode
492 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000493
Peter Astrand738131d2004-11-30 21:04:45 +0000494
495 def test_invalid_bufsize(self):
496 # an invalid type of the bufsize argument should raise
497 # TypeError.
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000498 with self.assertRaises(TypeError):
Peter Astrand738131d2004-11-30 21:04:45 +0000499 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
Peter Astrand738131d2004-11-30 21:04:45 +0000500
Guido van Rossum46a05a72007-06-07 21:56:45 +0000501 def test_bufsize_is_none(self):
502 # bufsize=None should be the same as bufsize=0.
503 p = subprocess.Popen([sys.executable, "-c", "pass"], None)
504 self.assertEqual(p.wait(), 0)
505 # Again with keyword arg
506 p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None)
507 self.assertEqual(p.wait(), 0)
508
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000509 def test_leaking_fds_on_error(self):
510 # see bug #5179: Popen leaks file descriptors to PIPEs if
511 # the child fails to execute; this will eventually exhaust
512 # the maximum number of open fds. 1024 seems a very common
513 # value for that limit, but Windows has 2048, so we loop
514 # 1024 times (each call leaked two fds).
515 for i in range(1024):
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000516 # Windows raises IOError. Others raise OSError.
517 with self.assertRaises(EnvironmentError) as c:
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000518 subprocess.Popen(['nonexisting_i_hope'],
519 stdout=subprocess.PIPE,
520 stderr=subprocess.PIPE)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000521 if c.exception.errno != 2: # ignore "no such file"
522 raise c.exception
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000523
Tim Peterse718f612004-10-12 21:51:32 +0000524
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000525# context manager
526class _SuppressCoreFiles(object):
527 """Try to prevent core files from being created."""
528 old_limit = None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000529
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000530 def __enter__(self):
531 """Try to save previous ulimit, then set it to (0, 0)."""
532 try:
533 import resource
534 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
535 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
536 except (ImportError, ValueError, resource.error):
537 pass
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000538
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000539 def __exit__(self, *args):
540 """Return core file behavior to default."""
541 if self.old_limit is None:
542 return
543 try:
544 import resource
545 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
546 except (ImportError, ValueError, resource.error):
547 pass
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000548
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000549
550@unittest.skipIf(sys.platform == "win32", "POSIX specific tests")
551class POSIXProcessTestCase(unittest.TestCase):
552 def setUp(self):
553 # Try to minimize the number of children we have so this test
554 # doesn't crash on some buildbots (Alphas in particular).
555 support.reap_children()
556
557 def test_exceptions(self):
558 # caught & re-raised exceptions
559 with self.assertRaises(OSError) as c:
560 p = subprocess.Popen([sys.executable, "-c", ""],
561 cwd="/this/path/does/not/exist")
562 # The attribute child_traceback should contain "os.chdir" somewhere.
563 self.assertIn("os.chdir", c.exception.child_traceback)
564
565 def test_run_abort(self):
566 # returncode handles signal termination
567 with _SuppressCoreFiles():
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000568 p = subprocess.Popen([sys.executable, "-c",
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000569 'import os; os.abort()'])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000570 p.wait()
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000571 self.assertEqual(-p.returncode, signal.SIGABRT)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000572
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000573 def test_preexec(self):
574 # preexec function
575 p = subprocess.Popen([sys.executable, "-c",
576 'import sys,os;'
577 'sys.stdout.write(os.getenv("FRUIT"))'],
578 stdout=subprocess.PIPE,
579 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
580 self.assertEqual(p.stdout.read(), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000581
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000582 def test_args_string(self):
583 # args is a string
584 fd, fname = mkstemp()
585 # reopen in text mode
586 with open(fd, "w") as fobj:
587 fobj.write("#!/bin/sh\n")
588 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
589 sys.executable)
590 os.chmod(fname, 0o700)
591 p = subprocess.Popen(fname)
592 p.wait()
593 os.remove(fname)
594 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000595
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000596 def test_invalid_args(self):
597 # invalid arguments should raise ValueError
598 self.assertRaises(ValueError, subprocess.call,
599 [sys.executable, "-c",
600 "import sys; sys.exit(47)"],
601 startupinfo=47)
602 self.assertRaises(ValueError, subprocess.call,
603 [sys.executable, "-c",
604 "import sys; sys.exit(47)"],
605 creationflags=47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000606
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000607 def test_shell_sequence(self):
608 # Run command through the shell (sequence)
609 newenv = os.environ.copy()
610 newenv["FRUIT"] = "apple"
611 p = subprocess.Popen(["echo $FRUIT"], shell=1,
612 stdout=subprocess.PIPE,
613 env=newenv)
614 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000615
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000616 def test_shell_string(self):
617 # Run command through the shell (string)
618 newenv = os.environ.copy()
619 newenv["FRUIT"] = "apple"
620 p = subprocess.Popen("echo $FRUIT", shell=1,
621 stdout=subprocess.PIPE,
622 env=newenv)
623 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Christian Heimesa342c012008-04-20 21:01:16 +0000624
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000625 def test_call_string(self):
626 # call() function with string argument on UNIX
627 fd, fname = mkstemp()
628 # reopen in text mode
629 with open(fd, "w") as fobj:
630 fobj.write("#!/bin/sh\n")
631 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
632 sys.executable)
633 os.chmod(fname, 0o700)
634 rc = subprocess.call(fname)
635 os.remove(fname)
636 self.assertEqual(rc, 47)
Christian Heimesa342c012008-04-20 21:01:16 +0000637
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000638 @unittest.skip("See issue #2777")
639 def test_send_signal(self):
640 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimesa342c012008-04-20 21:01:16 +0000641
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000642 self.assertIs(p.poll(), None)
643 p.send_signal(signal.SIGINT)
644 self.assertIsNot(p.wait(), None)
Christian Heimesa342c012008-04-20 21:01:16 +0000645
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000646 @unittest.skip("See issue #2777")
647 def test_kill(self):
648 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimesa342c012008-04-20 21:01:16 +0000649
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000650 self.assertIs(p.poll(), None)
651 p.kill()
652 self.assertEqual(p.wait(), -signal.SIGKILL)
Tim Peterse718f612004-10-12 21:51:32 +0000653
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000654 @unittest.skip("See issue #2777")
655 def test_terminate(self):
656 p = subprocess.Popen([sys.executable, "-c", "input()"])
657
658 self.assertIs(p.poll(), None)
659 p.terminate()
660 self.assertEqual(p.wait(), -signal.SIGTERM)
661
662
663@unittest.skipUnless(sys.platform == "win32", "Windows specific tests")
664class Win32ProcessTestCase(unittest.TestCase):
665 def setUp(self):
666 # Try to minimize the number of children we have so this test
667 # doesn't crash on some buildbots (Alphas in particular).
668 support.reap_children()
669
670 def test_startupinfo(self):
671 # startupinfo argument
672 # We uses hardcoded constants, because we do not want to
673 # depend on win32all.
674 STARTF_USESHOWWINDOW = 1
675 SW_MAXIMIZE = 3
676 startupinfo = subprocess.STARTUPINFO()
677 startupinfo.dwFlags = STARTF_USESHOWWINDOW
678 startupinfo.wShowWindow = SW_MAXIMIZE
679 # Since Python is a console process, it won't be affected
680 # by wShowWindow, but the argument should be silently
681 # ignored
682 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000683 startupinfo=startupinfo)
684
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000685 def test_creationflags(self):
686 # creationflags argument
687 CREATE_NEW_CONSOLE = 16
688 sys.stderr.write(" a DOS box should flash briefly ...\n")
689 subprocess.call(sys.executable +
690 ' -c "import time; time.sleep(0.25)"',
691 creationflags=CREATE_NEW_CONSOLE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000692
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000693 def test_invalid_args(self):
694 # invalid arguments should raise ValueError
695 self.assertRaises(ValueError, subprocess.call,
696 [sys.executable, "-c",
697 "import sys; sys.exit(47)"],
698 preexec_fn=lambda: 1)
699 self.assertRaises(ValueError, subprocess.call,
700 [sys.executable, "-c",
701 "import sys; sys.exit(47)"],
702 stdout=subprocess.PIPE,
703 close_fds=True)
704
705 def test_close_fds(self):
706 # close file descriptors
707 rc = subprocess.call([sys.executable, "-c",
708 "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000709 close_fds=True)
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000710 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000711
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000712 def test_shell_sequence(self):
713 # Run command through the shell (sequence)
714 newenv = os.environ.copy()
715 newenv["FRUIT"] = "physalis"
716 p = subprocess.Popen(["set"], shell=1,
717 stdout=subprocess.PIPE,
718 env=newenv)
719 self.assertIn(b"physalis", p.stdout.read())
Guido van Rossume7ba4952007-06-06 23:52:48 +0000720
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000721 def test_shell_string(self):
722 # Run command through the shell (string)
723 newenv = os.environ.copy()
724 newenv["FRUIT"] = "physalis"
725 p = subprocess.Popen("set", shell=1,
726 stdout=subprocess.PIPE,
727 env=newenv)
728 self.assertIn(b"physalis", p.stdout.read())
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000729
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000730 def test_call_string(self):
731 # call() function with string argument on Windows
732 rc = subprocess.call(sys.executable +
733 ' -c "import sys; sys.exit(47)"')
734 self.assertEqual(rc, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000735
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000736 @unittest.skip("See issue #2777")
737 def test_send_signal(self):
738 p = subprocess.Popen([sys.executable, "-c", "input()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000739
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000740 self.assertIs(p.poll(), None)
741 p.send_signal(signal.SIGTERM)
742 self.assertNotEqual(p.wait(), 0)
Christian Heimesa342c012008-04-20 21:01:16 +0000743
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000744 @unittest.skip("See issue #2777")
745 def test_kill(self):
746 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimesa342c012008-04-20 21:01:16 +0000747
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000748 self.assertIs(p.poll(), None)
749 p.kill()
750 self.assertNotEqual(p.wait(), 0)
Christian Heimesa342c012008-04-20 21:01:16 +0000751
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000752 @unittest.skip("See issue #2777")
753 def test_terminate(self):
754 p = subprocess.Popen([sys.executable, "-c", "input()"])
Christian Heimesa342c012008-04-20 21:01:16 +0000755
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000756 self.assertIs(p.poll(), None)
757 p.terminate()
758 self.assertNotEqual(p.wait(), 0)
Christian Heimesa342c012008-04-20 21:01:16 +0000759
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000760
Brett Cannona23810f2008-05-26 19:04:21 +0000761# The module says:
762# "NB This only works (and is only relevant) for UNIX."
763#
764# Actually, getoutput should work on any platform with an os.popen, but
765# I'll take the comment as given, and skip this suite.
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000766@unittest.skipUnless(os.name != 'posix', "only relevant for UNIX")
767class CommandTests(unittest.TestCase):
768 def test_getoutput(self):
769 self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
770 self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
771 (0, 'xyzzy'))
Brett Cannona23810f2008-05-26 19:04:21 +0000772
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000773 # we use mkdtemp in the next line to create an empty directory
774 # under our exclusive control; from that, we can invent a pathname
775 # that we _know_ won't exist. This is guaranteed to fail.
776 dir = None
777 try:
778 dir = tempfile.mkdtemp()
779 name = os.path.join(dir, "foo")
Brett Cannona23810f2008-05-26 19:04:21 +0000780
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000781 status, output = subprocess.getstatusoutput('cat ' + name)
782 self.assertNotEqual(status, 0)
783 finally:
784 if dir is not None:
785 os.rmdir(dir)
Brett Cannona23810f2008-05-26 19:04:21 +0000786
Gregory P. Smithd06fa472009-07-04 02:46:54 +0000787
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000788@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
789 "poll system call not supported")
790class ProcessTestCaseNoPoll(ProcessTestCase):
791 def setUp(self):
792 subprocess._has_poll = False
793 ProcessTestCase.setUp(self)
Gregory P. Smithd06fa472009-07-04 02:46:54 +0000794
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000795 def tearDown(self):
796 subprocess._has_poll = True
797 ProcessTestCase.tearDown(self)
Gregory P. Smithd06fa472009-07-04 02:46:54 +0000798
799
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000800def test_main():
Florent Xiclunab1e94e82010-02-27 22:12:37 +0000801 unit_tests = (ProcessTestCase,
802 POSIXProcessTestCase,
803 Win32ProcessTestCase,
804 CommandTests,
805 ProcessTestCaseNoPoll)
806
Gregory P. Smithd06fa472009-07-04 02:46:54 +0000807 support.run_unittest(*unit_tests)
Brett Cannona23810f2008-05-26 19:04:21 +0000808 support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000809
810if __name__ == "__main__":
Brett Cannona23810f2008-05-26 19:04:21 +0000811 test_main()