blob: 265859a21d9065ee2489f09df7109b879c87f531 [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
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000010
11mswindows = (sys.platform == "win32")
12
13#
14# Depends on the following external programs: Python
15#
16
17if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000018 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
19 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000020else:
21 SETBINARY = ''
22
Tim Peters3761e8d2004-10-13 04:07:12 +000023# In a debug build, stuff like "[6580 refs]" is printed to stderr at
24# shutdown time. That frustrates tests trying to check stderr produced
25# from a spawned Python process.
26def remove_stderr_debug_decorations(stderr):
Guido van Rossum98297ee2007-11-06 21:34:58 +000027 return re.sub("\[\d+ refs\]\r?\n?$", "", stderr.decode()).encode()
28 #return re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
Tim Peters3761e8d2004-10-13 04:07:12 +000029
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000030class ProcessTestCase(unittest.TestCase):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000031 def setUp(self):
32 # Try to minimize the number of children we have so this test
33 # doesn't crash on some buildbots (Alphas in particular).
Benjamin Petersonee8712c2008-05-20 21:35:26 +000034 if hasattr(support, "reap_children"):
35 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000036
37 def tearDown(self):
38 # Try to minimize the number of children we have so this test
39 # doesn't crash on some buildbots (Alphas in particular).
Benjamin Petersonee8712c2008-05-20 21:35:26 +000040 if hasattr(support, "reap_children"):
41 support.reap_children()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000043 def mkstemp(self):
44 """wrapper for mkstemp, calling mktemp if mkstemp is not available"""
45 if hasattr(tempfile, "mkstemp"):
46 return tempfile.mkstemp()
47 else:
48 fname = tempfile.mktemp()
49 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
Tim Peterse718f612004-10-12 21:51:32 +000050
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000051 #
52 # Generic tests
53 #
54 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000055 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000056 rc = subprocess.call([sys.executable, "-c",
57 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000058 self.assertEqual(rc, 47)
59
Peter Astrand454f7672005-01-01 09:36:35 +000060 def test_check_call_zero(self):
61 # check_call() function with zero return code
62 rc = subprocess.check_call([sys.executable, "-c",
63 "import sys; sys.exit(0)"])
64 self.assertEqual(rc, 0)
65
66 def test_check_call_nonzero(self):
67 # check_call() function with non-zero return code
68 try:
69 subprocess.check_call([sys.executable, "-c",
70 "import sys; sys.exit(47)"])
Guido van Rossumb940e112007-01-10 16:19:56 +000071 except subprocess.CalledProcessError as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072 self.assertEqual(e.returncode, 47)
Peter Astrand454f7672005-01-01 09:36:35 +000073 else:
74 self.fail("Expected CalledProcessError")
75
Georg Brandlf9734072008-12-07 15:30:06 +000076 def test_check_output(self):
77 # check_output() function with zero return code
78 output = subprocess.check_output(
79 [sys.executable, "-c", "print('BDFL')"])
80 self.assertTrue(b'BDFL' in output)
81
82 def test_check_output_nonzero(self):
83 # check_call() function with non-zero return code
84 try:
85 subprocess.check_output(
86 [sys.executable, "-c", "import sys; sys.exit(5)"])
87 except subprocess.CalledProcessError as e:
88 self.assertEqual(e.returncode, 5)
89 else:
90 self.fail("Expected CalledProcessError")
91
92 def test_check_output_stderr(self):
93 # check_output() function stderr redirected to stdout
94 output = subprocess.check_output(
95 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
96 stderr=subprocess.STDOUT)
97 self.assertTrue(b'BDFL' in output)
98
99 def test_check_output_stdout_arg(self):
100 # check_output() function stderr redirected to stdout
101 try:
102 output = subprocess.check_output(
103 [sys.executable, "-c", "print('will not be run')"],
104 stdout=sys.stdout)
105 except ValueError as e:
106 self.assertTrue('stdout' in e.args[0])
107 else:
108 self.fail("Expected ValueError when stdout arg supplied.")
109
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000110 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000111 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000112 newenv = os.environ.copy()
113 newenv["FRUIT"] = "banana"
114 rc = subprocess.call([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000115 'import sys, os;'
116 'sys.exit(os.getenv("FRUIT")=="banana")'],
117 env=newenv)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000118 self.assertEqual(rc, 1)
119
120 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000121 # .stdin is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000122 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000123 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
124 p.wait()
125 self.assertEqual(p.stdin, None)
126
127 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000128 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +0000129 p = subprocess.Popen([sys.executable, "-c",
Georg Brandl88fc6642007-02-09 21:28:07 +0000130 'print(" this bit of output is from a '
Tim Peters4052fe52004-10-13 03:29:54 +0000131 'test of stdout in a different '
Georg Brandl88fc6642007-02-09 21:28:07 +0000132 'process ...")'],
Tim Peters4052fe52004-10-13 03:29:54 +0000133 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000134 p.wait()
135 self.assertEqual(p.stdout, None)
136
137 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000138 # .stderr is None when not redirected
Georg Brandl88fc6642007-02-09 21:28:07 +0000139 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000140 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
141 p.wait()
142 self.assertEqual(p.stderr, None)
143
144 def test_executable(self):
Antoine Pitrou55503652009-03-29 19:30:55 +0000145 arg0 = os.path.join(os.path.dirname(sys.executable),
146 "somethingyoudonthave")
147 p = subprocess.Popen([arg0, "-c", "import sys; sys.exit(47)"],
Tim Peters3b01a702004-10-12 22:19:32 +0000148 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000149 p.wait()
150 self.assertEqual(p.returncode, 47)
151
152 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000153 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000154 p = subprocess.Popen([sys.executable, "-c",
155 'import sys; sys.exit(sys.stdin.read() == "pear")'],
156 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000157 p.stdin.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000158 p.stdin.close()
159 p.wait()
160 self.assertEqual(p.returncode, 1)
161
162 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000163 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000164 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000165 d = tf.fileno()
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000166 os.write(d, b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000167 os.lseek(d, 0, 0)
168 p = subprocess.Popen([sys.executable, "-c",
169 'import sys; sys.exit(sys.stdin.read() == "pear")'],
170 stdin=d)
171 p.wait()
172 self.assertEqual(p.returncode, 1)
173
174 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000175 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000176 tf = tempfile.TemporaryFile()
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000177 tf.write(b"pear")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000178 tf.seek(0)
179 p = subprocess.Popen([sys.executable, "-c",
180 'import sys; sys.exit(sys.stdin.read() == "pear")'],
181 stdin=tf)
182 p.wait()
183 self.assertEqual(p.returncode, 1)
184
185 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000186 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000187 p = subprocess.Popen([sys.executable, "-c",
188 'import sys; sys.stdout.write("orange")'],
189 stdout=subprocess.PIPE)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000190 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000191
192 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000193 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000194 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000195 d = tf.fileno()
196 p = subprocess.Popen([sys.executable, "-c",
197 'import sys; sys.stdout.write("orange")'],
198 stdout=d)
199 p.wait()
200 os.lseek(d, 0, 0)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000201 self.assertEqual(os.read(d, 1024), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000202
203 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000204 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000205 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000206 p = subprocess.Popen([sys.executable, "-c",
207 'import sys; sys.stdout.write("orange")'],
208 stdout=tf)
209 p.wait()
210 tf.seek(0)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000211 self.assertEqual(tf.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000212
213 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000214 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000215 p = subprocess.Popen([sys.executable, "-c",
216 'import sys; sys.stderr.write("strawberry")'],
217 stderr=subprocess.PIPE)
Tim Peters3761e8d2004-10-13 04:07:12 +0000218 self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()),
Guido van Rossum98297ee2007-11-06 21:34:58 +0000219 b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000220
221 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000222 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000223 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000224 d = tf.fileno()
225 p = subprocess.Popen([sys.executable, "-c",
226 'import sys; sys.stderr.write("strawberry")'],
227 stderr=d)
228 p.wait()
229 os.lseek(d, 0, 0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000230 self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)),
Guido van Rossum98297ee2007-11-06 21:34:58 +0000231 b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000232
233 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000234 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000235 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000236 p = subprocess.Popen([sys.executable, "-c",
237 'import sys; sys.stderr.write("strawberry")'],
238 stderr=tf)
239 p.wait()
240 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000241 self.assertEqual(remove_stderr_debug_decorations(tf.read()),
Guido van Rossum98297ee2007-11-06 21:34:58 +0000242 b"strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000243
244 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000245 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000246 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000247 'import sys;'
248 'sys.stdout.write("apple");'
249 'sys.stdout.flush();'
250 'sys.stderr.write("orange")'],
251 stdout=subprocess.PIPE,
252 stderr=subprocess.STDOUT)
Tim Peters3761e8d2004-10-13 04:07:12 +0000253 output = p.stdout.read()
254 stripped = remove_stderr_debug_decorations(output)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000255 self.assertEqual(stripped, b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000256
257 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000258 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000259 tf = tempfile.TemporaryFile()
260 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000261 'import sys;'
262 'sys.stdout.write("apple");'
263 'sys.stdout.flush();'
264 'sys.stderr.write("orange")'],
265 stdout=tf,
266 stderr=tf)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000267 p.wait()
268 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000269 output = tf.read()
270 stripped = remove_stderr_debug_decorations(output)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000271 self.assertEqual(stripped, b"appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000272
Thomas Wouters89f507f2006-12-13 04:49:30 +0000273 def test_stdout_filedes_of_stdout(self):
274 # stdout is set to 1 (#1531862).
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000275 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), b'.\n'))"
Thomas Wouters89f507f2006-12-13 04:49:30 +0000276 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
277 self.assertEquals(rc, 2)
278
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000279 def test_cwd(self):
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000280 tmpdir = tempfile.gettempdir()
Peter Astrand195404f2004-11-12 15:51:48 +0000281 # We cannot use os.path.realpath to canonicalize the path,
282 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
283 cwd = os.getcwd()
284 os.chdir(tmpdir)
285 tmpdir = os.getcwd()
286 os.chdir(cwd)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000287 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000288 'import sys,os;'
289 'sys.stdout.write(os.getcwd())'],
290 stdout=subprocess.PIPE,
291 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000292 normcase = os.path.normcase
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000293 self.assertEqual(normcase(p.stdout.read().decode("utf-8")),
294 normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000295
296 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000297 newenv = os.environ.copy()
298 newenv["FRUIT"] = "orange"
299 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000300 'import sys,os;'
301 'sys.stdout.write(os.getenv("FRUIT"))'],
302 stdout=subprocess.PIPE,
303 env=newenv)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000304 self.assertEqual(p.stdout.read(), b"orange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000305
Peter Astrandcbac93c2005-03-03 20:24:28 +0000306 def test_communicate_stdin(self):
307 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000308 'import sys;'
309 'sys.exit(sys.stdin.read() == "pear")'],
Peter Astrandcbac93c2005-03-03 20:24:28 +0000310 stdin=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000311 p.communicate(b"pear")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000312 self.assertEqual(p.returncode, 1)
313
314 def test_communicate_stdout(self):
315 p = subprocess.Popen([sys.executable, "-c",
316 'import sys; sys.stdout.write("pineapple")'],
317 stdout=subprocess.PIPE)
318 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000319 self.assertEqual(stdout, b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000320 self.assertEqual(stderr, None)
321
322 def test_communicate_stderr(self):
323 p = subprocess.Popen([sys.executable, "-c",
324 'import sys; sys.stderr.write("pineapple")'],
325 stderr=subprocess.PIPE)
326 (stdout, stderr) = p.communicate()
327 self.assertEqual(stdout, None)
Brett Cannon653a5ad2005-03-05 06:40:52 +0000328 # When running with a pydebug build, the # of references is outputted
329 # to stderr, so just check if stderr at least started with "pinapple"
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000330 self.assertEqual(remove_stderr_debug_decorations(stderr), b"pineapple")
Peter Astrandcbac93c2005-03-03 20:24:28 +0000331
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000332 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000333 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000334 'import sys,os;'
335 'sys.stderr.write("pineapple");'
336 'sys.stdout.write(sys.stdin.read())'],
337 stdin=subprocess.PIPE,
338 stdout=subprocess.PIPE,
339 stderr=subprocess.PIPE)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000340 (stdout, stderr) = p.communicate(b"banana")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000341 self.assertEqual(stdout, b"banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000342 self.assertEqual(remove_stderr_debug_decorations(stderr),
Guido van Rossum98297ee2007-11-06 21:34:58 +0000343 b"pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000344
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000345 # This test is Linux specific for simplicity to at least have
346 # some coverage. It is not a platform specific bug.
347 if os.path.isdir('/proc/%d/fd' % os.getpid()):
348 # Test for the fd leak reported in http://bugs.python.org/issue2791.
349 def test_communicate_pipe_fd_leak(self):
350 fd_directory = '/proc/%d/fd' % os.getpid()
351 num_fds_before_popen = len(os.listdir(fd_directory))
352 p = subprocess.Popen([sys.executable, '-c', 'print()'],
353 stdout=subprocess.PIPE)
354 p.communicate()
355 num_fds_after_communicate = len(os.listdir(fd_directory))
356 del p
357 num_fds_after_destruction = len(os.listdir(fd_directory))
358 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
359 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
360
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000361 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000362 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000363 p = subprocess.Popen([sys.executable, "-c",
364 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000365 (stdout, stderr) = p.communicate()
366 self.assertEqual(stdout, None)
367 self.assertEqual(stderr, None)
368
369 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000370 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000371 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000372 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000373 x, y = os.pipe()
374 if mswindows:
375 pipe_buf = 512
376 else:
377 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
378 os.close(x)
379 os.close(y)
380 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000381 'import sys,os;'
382 'sys.stdout.write(sys.stdin.read(47));'
383 'sys.stderr.write("xyz"*%d);'
384 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
385 stdin=subprocess.PIPE,
386 stdout=subprocess.PIPE,
387 stderr=subprocess.PIPE)
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000388 string_to_write = b"abc"*pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000389 (stdout, stderr) = p.communicate(string_to_write)
390 self.assertEqual(stdout, string_to_write)
391
392 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000393 # stdin.write before communicate()
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;'
396 'sys.stdout.write(sys.stdin.read())'],
397 stdin=subprocess.PIPE,
398 stdout=subprocess.PIPE,
399 stderr=subprocess.PIPE)
Guido van Rossumbb839ef2007-08-27 23:58:21 +0000400 p.stdin.write(b"banana")
401 (stdout, stderr) = p.communicate(b"split")
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000402 self.assertEqual(stdout, b"bananasplit")
Guido van Rossum98297ee2007-11-06 21:34:58 +0000403 self.assertEqual(remove_stderr_debug_decorations(stderr), b"")
Tim Peterse718f612004-10-12 21:51:32 +0000404
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000405 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000406 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000407 'import sys,os;' + SETBINARY +
408 'sys.stdout.write("line1\\n");'
409 'sys.stdout.flush();'
410 'sys.stdout.write("line2\\n");'
411 'sys.stdout.flush();'
412 'sys.stdout.write("line3\\r\\n");'
413 'sys.stdout.flush();'
414 'sys.stdout.write("line4\\r");'
415 'sys.stdout.flush();'
416 'sys.stdout.write("\\nline5");'
417 'sys.stdout.flush();'
418 'sys.stdout.write("\\nline6");'],
419 stdout=subprocess.PIPE,
420 universal_newlines=1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000421 stdout = p.stdout.read()
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000422 self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000423
424 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000425 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000426 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000427 'import sys,os;' + SETBINARY +
428 'sys.stdout.write("line1\\n");'
429 'sys.stdout.flush();'
430 'sys.stdout.write("line2\\n");'
431 'sys.stdout.flush();'
432 'sys.stdout.write("line3\\r\\n");'
433 'sys.stdout.flush();'
434 'sys.stdout.write("line4\\r");'
435 'sys.stdout.flush();'
436 'sys.stdout.write("\\nline5");'
437 'sys.stdout.flush();'
438 'sys.stdout.write("\\nline6");'],
439 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
440 universal_newlines=1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000441 (stdout, stderr) = p.communicate()
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000442 self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000443
444 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000445 # Make sure we leak no resources
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000446 if (not hasattr(support, "is_resource_enabled") or
447 support.is_resource_enabled("subprocess") and not mswindows):
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000448 max_handles = 1026 # too much for most UNIX systems
449 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000450 max_handles = 65
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000451 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000452 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000453 "import sys;"
454 "sys.stdout.write(sys.stdin.read())"],
455 stdin=subprocess.PIPE,
456 stdout=subprocess.PIPE,
457 stderr=subprocess.PIPE)
Georg Brandl1abcbf82008-07-01 19:28:43 +0000458 data = p.communicate(b"lime")[0]
Guido van Rossumc9e363c2007-05-15 23:18:55 +0000459 self.assertEqual(data, b"lime")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000460
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')
Christian Heimesfdab48e2008-01-20 09:06:41 +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')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000477 self.assertEqual(subprocess.list2cmdline(['ab', '']),
478 'ab ""')
Christian Heimesfdab48e2008-01-20 09:06:41 +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.
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000494 self.assertTrue(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.
510 try:
511 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
512 except TypeError:
513 pass
514 else:
515 self.fail("Expected TypeError")
516
Guido van Rossum46a05a72007-06-07 21:56:45 +0000517 def test_bufsize_is_none(self):
518 # bufsize=None should be the same as bufsize=0.
519 p = subprocess.Popen([sys.executable, "-c", "pass"], None)
520 self.assertEqual(p.wait(), 0)
521 # Again with keyword arg
522 p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None)
523 self.assertEqual(p.wait(), 0)
524
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000525 def test_leaking_fds_on_error(self):
526 # see bug #5179: Popen leaks file descriptors to PIPEs if
527 # the child fails to execute; this will eventually exhaust
528 # the maximum number of open fds. 1024 seems a very common
529 # value for that limit, but Windows has 2048, so we loop
530 # 1024 times (each call leaked two fds).
531 for i in range(1024):
532 try:
533 subprocess.Popen(['nonexisting_i_hope'],
534 stdout=subprocess.PIPE,
535 stderr=subprocess.PIPE)
536 # Windows raises IOError
537 except (IOError, OSError) as err:
538 if err.errno != 2: # ignore "no such file"
Benjamin Peterson4b068192009-02-20 03:19:25 +0000539 raise
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000540
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000541 #
542 # POSIX tests
543 #
544 if not mswindows:
545 def test_exceptions(self):
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000546 # caught & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000547 try:
548 p = subprocess.Popen([sys.executable, "-c", ""],
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000549 cwd="/this/path/does/not/exist")
Guido van Rossumb940e112007-01-10 16:19:56 +0000550 except OSError as e:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000551 # The attribute child_traceback should contain "os.chdir"
552 # somewhere.
553 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
554 else:
555 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000556
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000557 def _suppress_core_files(self):
558 """Try to prevent core files from being created.
559 Returns previous ulimit if successful, else None.
560 """
561 try:
562 import resource
563 old_limit = resource.getrlimit(resource.RLIMIT_CORE)
564 resource.setrlimit(resource.RLIMIT_CORE, (0,0))
565 return old_limit
566 except (ImportError, ValueError, resource.error):
567 return None
568
569 def _unsuppress_core_files(self, old_limit):
570 """Return core file behavior to default."""
571 if old_limit is None:
572 return
573 try:
574 import resource
575 resource.setrlimit(resource.RLIMIT_CORE, old_limit)
576 except (ImportError, ValueError, resource.error):
577 return
578
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000579 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000580 # returncode handles signal termination
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000581 old_limit = self._suppress_core_files()
582 try:
583 p = subprocess.Popen([sys.executable,
584 "-c", "import os; os.abort()"])
585 finally:
586 self._unsuppress_core_files(old_limit)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000587 p.wait()
588 self.assertEqual(-p.returncode, signal.SIGABRT)
589
590 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000591 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000592 p = subprocess.Popen([sys.executable, "-c",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000593 'import sys,os;'
594 'sys.stdout.write(os.getenv("FRUIT"))'],
595 stdout=subprocess.PIPE,
596 preexec_fn=lambda: os.putenv("FRUIT",
597 "apple"))
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000598 self.assertEqual(p.stdout.read(), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000599
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000600 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000601 # args is a string
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000602 fd, fname = self.mkstemp()
603 # reopen in text mode
604 with open(fd, "w") as fobj:
605 fobj.write("#!/bin/sh\n")
606 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
607 sys.executable)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000608 os.chmod(fname, 0o700)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000609 p = subprocess.Popen(fname)
610 p.wait()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000611 os.remove(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000612 self.assertEqual(p.returncode, 47)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000613
614 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000615 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000616 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000617 [sys.executable,
618 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000619 startupinfo=47)
620 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000621 [sys.executable,
622 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000623 creationflags=47)
624
625 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000626 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000627 newenv = os.environ.copy()
628 newenv["FRUIT"] = "apple"
629 p = subprocess.Popen(["echo $FRUIT"], shell=1,
630 stdout=subprocess.PIPE,
631 env=newenv)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000632 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000633
634 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000635 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000636 newenv = os.environ.copy()
637 newenv["FRUIT"] = "apple"
638 p = subprocess.Popen("echo $FRUIT", shell=1,
639 stdout=subprocess.PIPE,
640 env=newenv)
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000641 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000642
643 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000644 # call() function with string argument on UNIX
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000645 fd, fname = self.mkstemp()
646 # reopen in text mode
647 with open(fd, "w") as fobj:
648 fobj.write("#!/bin/sh\n")
649 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
650 sys.executable)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000651 os.chmod(fname, 0o700)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000652 rc = subprocess.call(fname)
Peter Astrand2224be62004-11-17 20:06:35 +0000653 os.remove(fname)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000654 self.assertEqual(rc, 47)
655
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000656 def DISABLED_test_send_signal(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000657 p = subprocess.Popen([sys.executable,
658 "-c", "input()"])
659
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000660 self.assertTrue(p.poll() is None, p.poll())
Christian Heimesa342c012008-04-20 21:01:16 +0000661 p.send_signal(signal.SIGINT)
662 self.assertNotEqual(p.wait(), 0)
663
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000664 def DISABLED_test_kill(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000665 p = subprocess.Popen([sys.executable,
666 "-c", "input()"])
667
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000668 self.assertTrue(p.poll() is None, p.poll())
Christian Heimesa342c012008-04-20 21:01:16 +0000669 p.kill()
670 self.assertEqual(p.wait(), -signal.SIGKILL)
671
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000672 def DISABLED_test_terminate(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000673 p = subprocess.Popen([sys.executable,
674 "-c", "input()"])
675
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000676 self.assertTrue(p.poll() is None, p.poll())
Christian Heimesa342c012008-04-20 21:01:16 +0000677 p.terminate()
678 self.assertEqual(p.wait(), -signal.SIGTERM)
Tim Peterse718f612004-10-12 21:51:32 +0000679
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000680 #
681 # Windows tests
682 #
683 if mswindows:
684 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000685 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000686 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000687 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000688 STARTF_USESHOWWINDOW = 1
689 SW_MAXIMIZE = 3
690 startupinfo = subprocess.STARTUPINFO()
691 startupinfo.dwFlags = STARTF_USESHOWWINDOW
692 startupinfo.wShowWindow = SW_MAXIMIZE
693 # Since Python is a console process, it won't be affected
694 # by wShowWindow, but the argument should be silently
695 # ignored
696 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
697 startupinfo=startupinfo)
698
699 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000700 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000701 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000702 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000703 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000704 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000705 creationflags=CREATE_NEW_CONSOLE)
706
707 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000708 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000709 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000710 [sys.executable,
711 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000712 preexec_fn=lambda: 1)
713 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000714 [sys.executable,
715 "-c", "import sys; sys.exit(47)"],
Guido van Rossume7ba4952007-06-06 23:52:48 +0000716 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000717 close_fds=True)
718
Guido van Rossume7ba4952007-06-06 23:52:48 +0000719 def test_close_fds(self):
720 # close file descriptors
721 rc = subprocess.call([sys.executable, "-c",
722 "import sys; sys.exit(47)"],
723 close_fds=True)
724 self.assertEqual(rc, 47)
725
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000726 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000727 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000728 newenv = os.environ.copy()
729 newenv["FRUIT"] = "physalis"
730 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000731 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000732 env=newenv)
Guido van Rossumc12a8132007-10-26 04:29:23 +0000733 self.assertNotEqual(p.stdout.read().find(b"physalis"), -1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000734
735 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000736 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000737 newenv = os.environ.copy()
738 newenv["FRUIT"] = "physalis"
739 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000740 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000741 env=newenv)
Guido van Rossumc12a8132007-10-26 04:29:23 +0000742 self.assertNotEqual(p.stdout.read().find(b"physalis"), -1)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000743
744 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000745 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000746 rc = subprocess.call(sys.executable +
747 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000748 self.assertEqual(rc, 47)
749
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000750 def DISABLED_test_send_signal(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000751 p = subprocess.Popen([sys.executable,
752 "-c", "input()"])
753
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000754 self.assertTrue(p.poll() is None, p.poll())
Christian Heimesa342c012008-04-20 21:01:16 +0000755 p.send_signal(signal.SIGTERM)
756 self.assertNotEqual(p.wait(), 0)
757
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000758 def DISABLED_test_kill(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000759 p = subprocess.Popen([sys.executable,
760 "-c", "input()"])
761
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000762 self.assertTrue(p.poll() is None, p.poll())
Christian Heimesa342c012008-04-20 21:01:16 +0000763 p.kill()
764 self.assertNotEqual(p.wait(), 0)
765
Christian Heimes75ca4ea2008-05-06 23:48:04 +0000766 def DISABLED_test_terminate(self):
Christian Heimesa342c012008-04-20 21:01:16 +0000767 p = subprocess.Popen([sys.executable,
768 "-c", "input()"])
769
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000770 self.assertTrue(p.poll() is None, p.poll())
Christian Heimesa342c012008-04-20 21:01:16 +0000771 p.terminate()
772 self.assertNotEqual(p.wait(), 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000773
Brett Cannona23810f2008-05-26 19:04:21 +0000774class CommandTests(unittest.TestCase):
775# The module says:
776# "NB This only works (and is only relevant) for UNIX."
777#
778# Actually, getoutput should work on any platform with an os.popen, but
779# I'll take the comment as given, and skip this suite.
780 if os.name == 'posix':
781
782 def test_getoutput(self):
783 self.assertEquals(subprocess.getoutput('echo xyzzy'), 'xyzzy')
784 self.assertEquals(subprocess.getstatusoutput('echo xyzzy'),
785 (0, 'xyzzy'))
786
787 # we use mkdtemp in the next line to create an empty directory
788 # under our exclusive control; from that, we can invent a pathname
789 # that we _know_ won't exist. This is guaranteed to fail.
790 dir = None
791 try:
792 dir = tempfile.mkdtemp()
793 name = os.path.join(dir, "foo")
794
795 status, output = subprocess.getstatusoutput('cat ' + name)
796 self.assertNotEquals(status, 0)
797 finally:
798 if dir is not None:
799 os.rmdir(dir)
800
Gregory P. Smithd06fa472009-07-04 02:46:54 +0000801
802unit_tests = [ProcessTestCase, CommandTests]
803
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +0000804if getattr(subprocess, '_has_poll', False):
Gregory P. Smithd06fa472009-07-04 02:46:54 +0000805 class ProcessTestCaseNoPoll(ProcessTestCase):
806 def setUp(self):
807 subprocess._has_poll = False
808 ProcessTestCase.setUp(self)
809
810 def tearDown(self):
811 subprocess._has_poll = True
812 ProcessTestCase.tearDown(self)
813
814 unit_tests.append(ProcessTestCaseNoPoll)
815
816
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000817def test_main():
Gregory P. Smithd06fa472009-07-04 02:46:54 +0000818 support.run_unittest(*unit_tests)
Brett Cannona23810f2008-05-26 19:04:21 +0000819 support.reap_children()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000820
821if __name__ == "__main__":
Brett Cannona23810f2008-05-26 19:04:21 +0000822 test_main()