Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1 | import unittest |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 2 | from test import script_helper |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 4 | import subprocess |
| 5 | import sys |
| 6 | import signal |
Gregory P. Smith | 112bb3a | 2011-03-15 14:55:17 -0400 | [diff] [blame] | 7 | import io |
Andrew Svetlov | 8286071 | 2012-08-19 22:13:41 +0300 | [diff] [blame] | 8 | import locale |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 9 | import os |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +0000 | [diff] [blame] | 10 | import errno |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 11 | import tempfile |
| 12 | import time |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 13 | import re |
Ezio Melotti | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 14 | import sysconfig |
Gregory P. Smith | d23047b | 2010-12-04 09:10:44 +0000 | [diff] [blame] | 15 | import warnings |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 16 | import select |
Gregory P. Smith | 81ce685 | 2011-03-15 02:04:11 -0400 | [diff] [blame] | 17 | import shutil |
Benjamin Peterson | b870aa1 | 2011-12-10 12:44:25 -0500 | [diff] [blame] | 18 | import gc |
Andrew Svetlov | 47ec25d | 2012-08-19 16:25:37 +0300 | [diff] [blame] | 19 | import textwrap |
Benjamin Peterson | 964561b | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 20 | |
| 21 | try: |
| 22 | import resource |
| 23 | except ImportError: |
| 24 | resource = None |
| 25 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 26 | mswindows = (sys.platform == "win32") |
| 27 | |
| 28 | # |
| 29 | # Depends on the following external programs: Python |
| 30 | # |
| 31 | |
| 32 | if mswindows: |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 33 | SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), ' |
| 34 | 'os.O_BINARY);') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 35 | else: |
| 36 | SETBINARY = '' |
| 37 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 38 | |
| 39 | try: |
| 40 | mkstemp = tempfile.mkstemp |
| 41 | except AttributeError: |
| 42 | # tempfile.mkstemp is not available |
| 43 | def mkstemp(): |
| 44 | """Replacement for mkstemp, calling mktemp.""" |
| 45 | fname = tempfile.mktemp() |
| 46 | return os.open(fname, os.O_RDWR|os.O_CREAT), fname |
| 47 | |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 48 | |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 49 | class BaseTestCase(unittest.TestCase): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 50 | def setUp(self): |
| 51 | # Try to minimize the number of children we have so this test |
| 52 | # doesn't crash on some buildbots (Alphas in particular). |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 53 | support.reap_children() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 54 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 55 | def tearDown(self): |
| 56 | for inst in subprocess._active: |
| 57 | inst.wait() |
| 58 | subprocess._cleanup() |
| 59 | self.assertFalse(subprocess._active, "subprocess._active not empty") |
| 60 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 61 | def assertStderrEqual(self, stderr, expected, msg=None): |
| 62 | # In a debug build, stuff like "[6580 refs]" is printed to stderr at |
| 63 | # shutdown time. That frustrates tests trying to check stderr produced |
| 64 | # from a spawned Python process. |
Antoine Pitrou | 62f68ed | 2010-08-04 11:48:56 +0000 | [diff] [blame] | 65 | actual = support.strip_python_stderr(stderr) |
Reid Kleckner | 31aa7dd | 2011-03-14 12:02:10 -0400 | [diff] [blame] | 66 | # strip_python_stderr also strips whitespace, so we do too. |
| 67 | expected = expected.strip() |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 68 | self.assertEqual(actual, expected, msg) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 69 | |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 70 | |
Gregory P. Smith | 3d8e776 | 2012-11-10 22:32:22 -0800 | [diff] [blame] | 71 | class PopenTestException(Exception): |
| 72 | pass |
| 73 | |
| 74 | |
| 75 | class PopenExecuteChildRaises(subprocess.Popen): |
| 76 | """Popen subclass for testing cleanup of subprocess.PIPE filehandles when |
| 77 | _execute_child fails. |
| 78 | """ |
| 79 | def _execute_child(self, *args, **kwargs): |
| 80 | raise PopenTestException("Forced Exception for Test") |
| 81 | |
| 82 | |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 83 | class ProcessTestCase(BaseTestCase): |
| 84 | |
Gregory P. Smith | a1ed539 | 2013-03-23 11:44:25 -0700 | [diff] [blame] | 85 | def test_io_buffered_by_default(self): |
| 86 | p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], |
| 87 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 88 | stderr=subprocess.PIPE) |
| 89 | try: |
| 90 | self.assertIsInstance(p.stdin, io.BufferedIOBase) |
| 91 | self.assertIsInstance(p.stdout, io.BufferedIOBase) |
| 92 | self.assertIsInstance(p.stderr, io.BufferedIOBase) |
| 93 | finally: |
Gregory P. Smith | a1b9ed3 | 2013-03-23 11:54:22 -0700 | [diff] [blame] | 94 | p.stdin.close() |
| 95 | p.stdout.close() |
| 96 | p.stderr.close() |
Gregory P. Smith | a1ed539 | 2013-03-23 11:44:25 -0700 | [diff] [blame] | 97 | p.wait() |
| 98 | |
| 99 | def test_io_unbuffered_works(self): |
| 100 | p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], |
| 101 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 102 | stderr=subprocess.PIPE, bufsize=0) |
| 103 | try: |
| 104 | self.assertIsInstance(p.stdin, io.RawIOBase) |
| 105 | self.assertIsInstance(p.stdout, io.RawIOBase) |
| 106 | self.assertIsInstance(p.stderr, io.RawIOBase) |
| 107 | finally: |
Gregory P. Smith | a1b9ed3 | 2013-03-23 11:54:22 -0700 | [diff] [blame] | 108 | p.stdin.close() |
| 109 | p.stdout.close() |
| 110 | p.stderr.close() |
Gregory P. Smith | a1ed539 | 2013-03-23 11:44:25 -0700 | [diff] [blame] | 111 | p.wait() |
| 112 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 113 | def test_call_seq(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 114 | # call() function with sequence argument |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 115 | rc = subprocess.call([sys.executable, "-c", |
| 116 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 117 | self.assertEqual(rc, 47) |
| 118 | |
Reid Kleckner | 31aa7dd | 2011-03-14 12:02:10 -0400 | [diff] [blame] | 119 | def test_call_timeout(self): |
| 120 | # call() function with timeout argument; we want to test that the child |
| 121 | # process gets killed when the timeout expires. If the child isn't |
| 122 | # killed, this call will deadlock since subprocess.call waits for the |
| 123 | # child. |
| 124 | self.assertRaises(subprocess.TimeoutExpired, subprocess.call, |
| 125 | [sys.executable, "-c", "while True: pass"], |
| 126 | timeout=0.1) |
| 127 | |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 128 | def test_check_call_zero(self): |
| 129 | # check_call() function with zero return code |
| 130 | rc = subprocess.check_call([sys.executable, "-c", |
| 131 | "import sys; sys.exit(0)"]) |
| 132 | self.assertEqual(rc, 0) |
| 133 | |
| 134 | def test_check_call_nonzero(self): |
| 135 | # check_call() function with non-zero return code |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 136 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 137 | subprocess.check_call([sys.executable, "-c", |
| 138 | "import sys; sys.exit(47)"]) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 139 | self.assertEqual(c.exception.returncode, 47) |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 140 | |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 141 | def test_check_output(self): |
| 142 | # check_output() function with zero return code |
| 143 | output = subprocess.check_output( |
| 144 | [sys.executable, "-c", "print('BDFL')"]) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 145 | self.assertIn(b'BDFL', output) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 146 | |
| 147 | def test_check_output_nonzero(self): |
| 148 | # check_call() function with non-zero return code |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 149 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 150 | subprocess.check_output( |
| 151 | [sys.executable, "-c", "import sys; sys.exit(5)"]) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 152 | self.assertEqual(c.exception.returncode, 5) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 153 | |
| 154 | def test_check_output_stderr(self): |
| 155 | # check_output() function stderr redirected to stdout |
| 156 | output = subprocess.check_output( |
| 157 | [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"], |
| 158 | stderr=subprocess.STDOUT) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 159 | self.assertIn(b'BDFL', output) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 160 | |
| 161 | def test_check_output_stdout_arg(self): |
| 162 | # check_output() function stderr redirected to stdout |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 163 | with self.assertRaises(ValueError) as c: |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 164 | output = subprocess.check_output( |
| 165 | [sys.executable, "-c", "print('will not be run')"], |
| 166 | stdout=sys.stdout) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 167 | self.fail("Expected ValueError when stdout arg supplied.") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 168 | self.assertIn('stdout', c.exception.args[0]) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 169 | |
Reid Kleckner | 31aa7dd | 2011-03-14 12:02:10 -0400 | [diff] [blame] | 170 | def test_check_output_timeout(self): |
| 171 | # check_output() function with timeout arg |
| 172 | with self.assertRaises(subprocess.TimeoutExpired) as c: |
| 173 | output = subprocess.check_output( |
| 174 | [sys.executable, "-c", |
Victor Stinner | 149b1c7 | 2011-06-06 23:43:02 +0200 | [diff] [blame] | 175 | "import sys, time\n" |
| 176 | "sys.stdout.write('BDFL')\n" |
Reid Kleckner | 31aa7dd | 2011-03-14 12:02:10 -0400 | [diff] [blame] | 177 | "sys.stdout.flush()\n" |
Victor Stinner | 149b1c7 | 2011-06-06 23:43:02 +0200 | [diff] [blame] | 178 | "time.sleep(3600)"], |
Reid Kleckner | da9ac72 | 2011-03-16 17:08:21 -0400 | [diff] [blame] | 179 | # Some heavily loaded buildbots (sparc Debian 3.x) require |
| 180 | # this much time to start and print. |
| 181 | timeout=3) |
Reid Kleckner | 31aa7dd | 2011-03-14 12:02:10 -0400 | [diff] [blame] | 182 | self.fail("Expected TimeoutExpired.") |
| 183 | self.assertEqual(c.exception.output, b'BDFL') |
| 184 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 185 | def test_call_kwargs(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 186 | # call() function with keyword args |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 187 | newenv = os.environ.copy() |
| 188 | newenv["FRUIT"] = "banana" |
| 189 | rc = subprocess.call([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 190 | 'import sys, os;' |
| 191 | 'sys.exit(os.getenv("FRUIT")=="banana")'], |
| 192 | env=newenv) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 193 | self.assertEqual(rc, 1) |
| 194 | |
Victor Stinner | 87b9bc3 | 2011-06-01 00:57:47 +0200 | [diff] [blame] | 195 | def test_invalid_args(self): |
| 196 | # Popen() called with invalid arguments should raise TypeError |
| 197 | # but Popen.__del__ should not complain (issue #12085) |
| 198 | with support.captured_stderr() as s: |
| 199 | self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1) |
| 200 | argcount = subprocess.Popen.__init__.__code__.co_argcount |
| 201 | too_many_args = [0] * (argcount + 1) |
| 202 | self.assertRaises(TypeError, subprocess.Popen, *too_many_args) |
| 203 | self.assertEqual(s.getvalue(), '') |
| 204 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 205 | def test_stdin_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 206 | # .stdin is None when not redirected |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 207 | p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 208 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 209 | self.addCleanup(p.stdout.close) |
| 210 | self.addCleanup(p.stderr.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 211 | p.wait() |
| 212 | self.assertEqual(p.stdin, None) |
| 213 | |
| 214 | def test_stdout_none(self): |
Ezio Melotti | 42a541b | 2013-03-11 05:53:34 +0200 | [diff] [blame] | 215 | # .stdout is None when not redirected, and the child's stdout will |
| 216 | # be inherited from the parent. In order to test this we run a |
| 217 | # subprocess in a subprocess: |
| 218 | # this_test |
| 219 | # \-- subprocess created by this test (parent) |
| 220 | # \-- subprocess created by the parent subprocess (child) |
| 221 | # The parent doesn't specify stdout, so the child will use the |
| 222 | # parent's stdout. This test checks that the message printed by the |
| 223 | # child goes to the parent stdout. The parent also checks that the |
| 224 | # child's stdout is None. See #11963. |
| 225 | code = ('import sys; from subprocess import Popen, PIPE;' |
| 226 | 'p = Popen([sys.executable, "-c", "print(\'test_stdout_none\')"],' |
| 227 | ' stdin=PIPE, stderr=PIPE);' |
| 228 | 'p.wait(); assert p.stdout is None;') |
| 229 | p = subprocess.Popen([sys.executable, "-c", code], |
| 230 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 231 | self.addCleanup(p.stdout.close) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 232 | self.addCleanup(p.stderr.close) |
Ezio Melotti | 42a541b | 2013-03-11 05:53:34 +0200 | [diff] [blame] | 233 | out, err = p.communicate() |
| 234 | self.assertEqual(p.returncode, 0, err) |
| 235 | self.assertEqual(out.rstrip(), b'test_stdout_none') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 236 | |
| 237 | def test_stderr_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 238 | # .stderr is None when not redirected |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 239 | p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 240 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 241 | self.addCleanup(p.stdout.close) |
| 242 | self.addCleanup(p.stdin.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 243 | p.wait() |
| 244 | self.assertEqual(p.stderr, None) |
| 245 | |
Chris Jerdonek | 776cb19 | 2012-10-08 15:56:43 -0700 | [diff] [blame] | 246 | def _assert_python(self, pre_args, **kwargs): |
| 247 | # We include sys.exit() to prevent the test runner from hanging |
| 248 | # whenever python is found. |
| 249 | args = pre_args + ["import sys; sys.exit(47)"] |
| 250 | p = subprocess.Popen(args, **kwargs) |
| 251 | p.wait() |
| 252 | self.assertEqual(47, p.returncode) |
| 253 | |
| 254 | def test_executable(self): |
| 255 | # Check that the executable argument works. |
Chris Jerdonek | 86b0fb2 | 2012-10-09 13:17:49 -0700 | [diff] [blame] | 256 | # |
| 257 | # On Unix (non-Mac and non-Windows), Python looks at args[0] to |
| 258 | # determine where its standard library is, so we need the directory |
| 259 | # of args[0] to be valid for the Popen() call to Python to succeed. |
| 260 | # See also issue #16170 and issue #7774. |
| 261 | doesnotexist = os.path.join(os.path.dirname(sys.executable), |
| 262 | "doesnotexist") |
| 263 | self._assert_python([doesnotexist, "-c"], executable=sys.executable) |
Chris Jerdonek | 776cb19 | 2012-10-08 15:56:43 -0700 | [diff] [blame] | 264 | |
| 265 | def test_executable_takes_precedence(self): |
| 266 | # Check that the executable argument takes precedence over args[0]. |
| 267 | # |
| 268 | # Verify first that the call succeeds without the executable arg. |
| 269 | pre_args = [sys.executable, "-c"] |
| 270 | self._assert_python(pre_args) |
| 271 | self.assertRaises(FileNotFoundError, self._assert_python, pre_args, |
| 272 | executable="doesnotexist") |
| 273 | |
| 274 | @unittest.skipIf(mswindows, "executable argument replaces shell") |
| 275 | def test_executable_replaces_shell(self): |
| 276 | # Check that the executable argument replaces the default shell |
| 277 | # when shell=True. |
| 278 | self._assert_python([], executable=sys.executable, shell=True) |
| 279 | |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 280 | # For use in the test_cwd* tests below. |
| 281 | def _normalize_cwd(self, cwd): |
| 282 | # Normalize an expected cwd (for Tru64 support). |
| 283 | # We can't use os.path.realpath since it doesn't expand Tru64 {memb} |
| 284 | # strings. See bug #1063571. |
| 285 | original_cwd = os.getcwd() |
| 286 | os.chdir(cwd) |
| 287 | cwd = os.getcwd() |
| 288 | os.chdir(original_cwd) |
| 289 | return cwd |
| 290 | |
| 291 | # For use in the test_cwd* tests below. |
| 292 | def _split_python_path(self): |
| 293 | # Return normalized (python_dir, python_base). |
| 294 | python_path = os.path.realpath(sys.executable) |
| 295 | return os.path.split(python_path) |
| 296 | |
| 297 | # For use in the test_cwd* tests below. |
| 298 | def _assert_cwd(self, expected_cwd, python_arg, **kwargs): |
| 299 | # Invoke Python via Popen, and assert that (1) the call succeeds, |
| 300 | # and that (2) the current working directory of the child process |
| 301 | # matches *expected_cwd*. |
| 302 | p = subprocess.Popen([python_arg, "-c", |
| 303 | "import os, sys; " |
| 304 | "sys.stdout.write(os.getcwd()); " |
| 305 | "sys.exit(47)"], |
| 306 | stdout=subprocess.PIPE, |
| 307 | **kwargs) |
| 308 | self.addCleanup(p.stdout.close) |
Ezio Melotti | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 309 | p.wait() |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 310 | self.assertEqual(47, p.returncode) |
| 311 | normcase = os.path.normcase |
| 312 | self.assertEqual(normcase(expected_cwd), |
| 313 | normcase(p.stdout.read().decode("utf-8"))) |
| 314 | |
| 315 | def test_cwd(self): |
| 316 | # Check that cwd changes the cwd for the child process. |
| 317 | temp_dir = tempfile.gettempdir() |
| 318 | temp_dir = self._normalize_cwd(temp_dir) |
| 319 | self._assert_cwd(temp_dir, sys.executable, cwd=temp_dir) |
| 320 | |
Chris Jerdonek | c2cd626 | 2012-09-30 09:45:00 -0700 | [diff] [blame] | 321 | @unittest.skipIf(mswindows, "pending resolution of issue #15533") |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 322 | def test_cwd_with_relative_arg(self): |
| 323 | # Check that Popen looks for args[0] relative to cwd if args[0] |
| 324 | # is relative. |
| 325 | python_dir, python_base = self._split_python_path() |
| 326 | rel_python = os.path.join(os.curdir, python_base) |
| 327 | with support.temp_cwd() as wrong_dir: |
| 328 | # Before calling with the correct cwd, confirm that the call fails |
| 329 | # without cwd and with the wrong cwd. |
Chris Jerdonek | 28714c8 | 2012-09-30 02:15:37 -0700 | [diff] [blame] | 330 | self.assertRaises(FileNotFoundError, subprocess.Popen, |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 331 | [rel_python]) |
Chris Jerdonek | 28714c8 | 2012-09-30 02:15:37 -0700 | [diff] [blame] | 332 | self.assertRaises(FileNotFoundError, subprocess.Popen, |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 333 | [rel_python], cwd=wrong_dir) |
| 334 | python_dir = self._normalize_cwd(python_dir) |
| 335 | self._assert_cwd(python_dir, rel_python, cwd=python_dir) |
| 336 | |
Chris Jerdonek | c2cd626 | 2012-09-30 09:45:00 -0700 | [diff] [blame] | 337 | @unittest.skipIf(mswindows, "pending resolution of issue #15533") |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 338 | def test_cwd_with_relative_executable(self): |
| 339 | # Check that Popen looks for executable relative to cwd if executable |
| 340 | # is relative (and that executable takes precedence over args[0]). |
| 341 | python_dir, python_base = self._split_python_path() |
| 342 | rel_python = os.path.join(os.curdir, python_base) |
| 343 | doesntexist = "somethingyoudonthave" |
| 344 | with support.temp_cwd() as wrong_dir: |
| 345 | # Before calling with the correct cwd, confirm that the call fails |
| 346 | # without cwd and with the wrong cwd. |
Chris Jerdonek | 28714c8 | 2012-09-30 02:15:37 -0700 | [diff] [blame] | 347 | self.assertRaises(FileNotFoundError, subprocess.Popen, |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 348 | [doesntexist], executable=rel_python) |
Chris Jerdonek | 28714c8 | 2012-09-30 02:15:37 -0700 | [diff] [blame] | 349 | self.assertRaises(FileNotFoundError, subprocess.Popen, |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 350 | [doesntexist], executable=rel_python, |
| 351 | cwd=wrong_dir) |
| 352 | python_dir = self._normalize_cwd(python_dir) |
| 353 | self._assert_cwd(python_dir, doesntexist, executable=rel_python, |
| 354 | cwd=python_dir) |
| 355 | |
| 356 | def test_cwd_with_absolute_arg(self): |
| 357 | # Check that Popen can find the executable when the cwd is wrong |
| 358 | # if args[0] is an absolute path. |
| 359 | python_dir, python_base = self._split_python_path() |
| 360 | abs_python = os.path.join(python_dir, python_base) |
| 361 | rel_python = os.path.join(os.curdir, python_base) |
| 362 | with script_helper.temp_dir() as wrong_dir: |
| 363 | # Before calling with an absolute path, confirm that using a |
| 364 | # relative path fails. |
Chris Jerdonek | 28714c8 | 2012-09-30 02:15:37 -0700 | [diff] [blame] | 365 | self.assertRaises(FileNotFoundError, subprocess.Popen, |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 366 | [rel_python], cwd=wrong_dir) |
| 367 | wrong_dir = self._normalize_cwd(wrong_dir) |
| 368 | self._assert_cwd(wrong_dir, abs_python, cwd=wrong_dir) |
| 369 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 370 | @unittest.skipIf(sys.base_prefix != sys.prefix, |
| 371 | 'Test is not venv-compatible') |
Ezio Melotti | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 372 | def test_executable_with_cwd(self): |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 373 | python_dir, python_base = self._split_python_path() |
| 374 | python_dir = self._normalize_cwd(python_dir) |
| 375 | self._assert_cwd(python_dir, "somethingyoudonthave", |
| 376 | executable=sys.executable, cwd=python_dir) |
Ezio Melotti | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 377 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 378 | @unittest.skipIf(sys.base_prefix != sys.prefix, |
| 379 | 'Test is not venv-compatible') |
Ezio Melotti | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 380 | @unittest.skipIf(sysconfig.is_python_build(), |
| 381 | "need an installed Python. See #7774") |
| 382 | def test_executable_without_cwd(self): |
| 383 | # For a normal installation, it should work without 'cwd' |
| 384 | # argument. For test runs in the build directory, see #7774. |
Chris Jerdonek | ec3ea94 | 2012-09-30 00:10:28 -0700 | [diff] [blame] | 385 | self._assert_cwd('', "somethingyoudonthave", executable=sys.executable) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 386 | |
| 387 | def test_stdin_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 388 | # stdin redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 389 | p = subprocess.Popen([sys.executable, "-c", |
| 390 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 391 | stdin=subprocess.PIPE) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 392 | p.stdin.write(b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 393 | p.stdin.close() |
| 394 | p.wait() |
| 395 | self.assertEqual(p.returncode, 1) |
| 396 | |
| 397 | def test_stdin_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 398 | # stdin is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 399 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 400 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 401 | d = tf.fileno() |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 402 | os.write(d, b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 403 | os.lseek(d, 0, 0) |
| 404 | p = subprocess.Popen([sys.executable, "-c", |
| 405 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 406 | stdin=d) |
| 407 | p.wait() |
| 408 | self.assertEqual(p.returncode, 1) |
| 409 | |
| 410 | def test_stdin_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 411 | # stdin is set to open file object |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 412 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 413 | self.addCleanup(tf.close) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 414 | tf.write(b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 415 | tf.seek(0) |
| 416 | p = subprocess.Popen([sys.executable, "-c", |
| 417 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 418 | stdin=tf) |
| 419 | p.wait() |
| 420 | self.assertEqual(p.returncode, 1) |
| 421 | |
| 422 | def test_stdout_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 423 | # stdout redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 424 | p = subprocess.Popen([sys.executable, "-c", |
| 425 | 'import sys; sys.stdout.write("orange")'], |
| 426 | stdout=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 427 | self.addCleanup(p.stdout.close) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 428 | self.assertEqual(p.stdout.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 429 | |
| 430 | def test_stdout_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 431 | # stdout is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 432 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 433 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 434 | d = tf.fileno() |
| 435 | p = subprocess.Popen([sys.executable, "-c", |
| 436 | 'import sys; sys.stdout.write("orange")'], |
| 437 | stdout=d) |
| 438 | p.wait() |
| 439 | os.lseek(d, 0, 0) |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 440 | self.assertEqual(os.read(d, 1024), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 441 | |
| 442 | def test_stdout_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 443 | # stdout is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 444 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 445 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 446 | p = subprocess.Popen([sys.executable, "-c", |
| 447 | 'import sys; sys.stdout.write("orange")'], |
| 448 | stdout=tf) |
| 449 | p.wait() |
| 450 | tf.seek(0) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 451 | self.assertEqual(tf.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 452 | |
| 453 | def test_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 454 | # stderr redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 455 | p = subprocess.Popen([sys.executable, "-c", |
| 456 | 'import sys; sys.stderr.write("strawberry")'], |
| 457 | stderr=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 458 | self.addCleanup(p.stderr.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 459 | self.assertStderrEqual(p.stderr.read(), b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 460 | |
| 461 | def test_stderr_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 462 | # stderr is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 463 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 464 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 465 | d = tf.fileno() |
| 466 | p = subprocess.Popen([sys.executable, "-c", |
| 467 | 'import sys; sys.stderr.write("strawberry")'], |
| 468 | stderr=d) |
| 469 | p.wait() |
| 470 | os.lseek(d, 0, 0) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 471 | self.assertStderrEqual(os.read(d, 1024), b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 472 | |
| 473 | def test_stderr_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 474 | # stderr is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 475 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 476 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 477 | p = subprocess.Popen([sys.executable, "-c", |
| 478 | 'import sys; sys.stderr.write("strawberry")'], |
| 479 | stderr=tf) |
| 480 | p.wait() |
| 481 | tf.seek(0) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 482 | self.assertStderrEqual(tf.read(), b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 483 | |
| 484 | def test_stdout_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 485 | # capture stdout and stderr to the same pipe |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 486 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 487 | 'import sys;' |
| 488 | 'sys.stdout.write("apple");' |
| 489 | 'sys.stdout.flush();' |
| 490 | 'sys.stderr.write("orange")'], |
| 491 | stdout=subprocess.PIPE, |
| 492 | stderr=subprocess.STDOUT) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 493 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 494 | self.assertStderrEqual(p.stdout.read(), b"appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 495 | |
| 496 | def test_stdout_stderr_file(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 497 | # capture stdout and stderr to the same open file |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 498 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 499 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 500 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 501 | 'import sys;' |
| 502 | 'sys.stdout.write("apple");' |
| 503 | 'sys.stdout.flush();' |
| 504 | 'sys.stderr.write("orange")'], |
| 505 | stdout=tf, |
| 506 | stderr=tf) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 507 | p.wait() |
| 508 | tf.seek(0) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 509 | self.assertStderrEqual(tf.read(), b"appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 510 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 511 | def test_stdout_filedes_of_stdout(self): |
| 512 | # stdout is set to 1 (#1531862). |
Ezio Melotti | 42a541b | 2013-03-11 05:53:34 +0200 | [diff] [blame] | 513 | # To avoid printing the text on stdout, we do something similar to |
| 514 | # test_stdout_none (see above). The parent subprocess calls the child |
| 515 | # subprocess passing stdout=1, and this test uses stdout=PIPE in |
| 516 | # order to capture and check the output of the parent. See #11963. |
| 517 | code = ('import sys, subprocess; ' |
| 518 | 'rc = subprocess.call([sys.executable, "-c", ' |
| 519 | ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), ' |
| 520 | 'b\'test with stdout=1\'))"], stdout=1); ' |
| 521 | 'assert rc == 18') |
| 522 | p = subprocess.Popen([sys.executable, "-c", code], |
| 523 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 524 | self.addCleanup(p.stdout.close) |
| 525 | self.addCleanup(p.stderr.close) |
| 526 | out, err = p.communicate() |
| 527 | self.assertEqual(p.returncode, 0, err) |
| 528 | self.assertEqual(out.rstrip(), b'test with stdout=1') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 529 | |
Ross Lagerwall | ba102ec | 2011-03-16 18:40:25 +0200 | [diff] [blame] | 530 | def test_stdout_devnull(self): |
| 531 | p = subprocess.Popen([sys.executable, "-c", |
| 532 | 'for i in range(10240):' |
| 533 | 'print("x" * 1024)'], |
| 534 | stdout=subprocess.DEVNULL) |
| 535 | p.wait() |
| 536 | self.assertEqual(p.stdout, None) |
| 537 | |
| 538 | def test_stderr_devnull(self): |
| 539 | p = subprocess.Popen([sys.executable, "-c", |
| 540 | 'import sys\n' |
| 541 | 'for i in range(10240):' |
| 542 | 'sys.stderr.write("x" * 1024)'], |
| 543 | stderr=subprocess.DEVNULL) |
| 544 | p.wait() |
| 545 | self.assertEqual(p.stderr, None) |
| 546 | |
| 547 | def test_stdin_devnull(self): |
| 548 | p = subprocess.Popen([sys.executable, "-c", |
| 549 | 'import sys;' |
| 550 | 'sys.stdin.read(1)'], |
| 551 | stdin=subprocess.DEVNULL) |
| 552 | p.wait() |
| 553 | self.assertEqual(p.stdin, None) |
| 554 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 555 | def test_env(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 556 | newenv = os.environ.copy() |
| 557 | newenv["FRUIT"] = "orange" |
Victor Stinner | f1512a2 | 2011-06-21 17:18:38 +0200 | [diff] [blame] | 558 | with subprocess.Popen([sys.executable, "-c", |
| 559 | 'import sys,os;' |
| 560 | 'sys.stdout.write(os.getenv("FRUIT"))'], |
| 561 | stdout=subprocess.PIPE, |
| 562 | env=newenv) as p: |
| 563 | stdout, stderr = p.communicate() |
| 564 | self.assertEqual(stdout, b"orange") |
| 565 | |
Victor Stinner | 62d5118 | 2011-06-23 01:02:25 +0200 | [diff] [blame] | 566 | # Windows requires at least the SYSTEMROOT environment variable to start |
| 567 | # Python |
| 568 | @unittest.skipIf(sys.platform == 'win32', |
| 569 | 'cannot test an empty env on Windows') |
Victor Stinner | 237e5cb | 2011-06-22 21:28:43 +0200 | [diff] [blame] | 570 | @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') is not None, |
Victor Stinner | 372309a | 2011-06-21 21:59:06 +0200 | [diff] [blame] | 571 | 'the python library cannot be loaded ' |
| 572 | 'with an empty environment') |
Victor Stinner | f1512a2 | 2011-06-21 17:18:38 +0200 | [diff] [blame] | 573 | def test_empty_env(self): |
| 574 | with subprocess.Popen([sys.executable, "-c", |
| 575 | 'import os; ' |
Victor Stinner | 372309a | 2011-06-21 21:59:06 +0200 | [diff] [blame] | 576 | 'print(list(os.environ.keys()))'], |
Victor Stinner | f1512a2 | 2011-06-21 17:18:38 +0200 | [diff] [blame] | 577 | stdout=subprocess.PIPE, |
| 578 | env={}) as p: |
| 579 | stdout, stderr = p.communicate() |
Victor Stinner | 237e5cb | 2011-06-22 21:28:43 +0200 | [diff] [blame] | 580 | self.assertIn(stdout.strip(), |
| 581 | (b"[]", |
| 582 | # Mac OS X adds __CF_USER_TEXT_ENCODING variable to an empty |
| 583 | # environment |
| 584 | b"['__CF_USER_TEXT_ENCODING']")) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 585 | |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 586 | def test_communicate_stdin(self): |
| 587 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 588 | 'import sys;' |
| 589 | 'sys.exit(sys.stdin.read() == "pear")'], |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 590 | stdin=subprocess.PIPE) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 591 | p.communicate(b"pear") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 592 | self.assertEqual(p.returncode, 1) |
| 593 | |
| 594 | def test_communicate_stdout(self): |
| 595 | p = subprocess.Popen([sys.executable, "-c", |
| 596 | 'import sys; sys.stdout.write("pineapple")'], |
| 597 | stdout=subprocess.PIPE) |
| 598 | (stdout, stderr) = p.communicate() |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 599 | self.assertEqual(stdout, b"pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 600 | self.assertEqual(stderr, None) |
| 601 | |
| 602 | def test_communicate_stderr(self): |
| 603 | p = subprocess.Popen([sys.executable, "-c", |
| 604 | 'import sys; sys.stderr.write("pineapple")'], |
| 605 | stderr=subprocess.PIPE) |
| 606 | (stdout, stderr) = p.communicate() |
| 607 | self.assertEqual(stdout, None) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 608 | self.assertStderrEqual(stderr, b"pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 609 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 610 | def test_communicate(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 611 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 612 | 'import sys,os;' |
| 613 | 'sys.stderr.write("pineapple");' |
| 614 | 'sys.stdout.write(sys.stdin.read())'], |
| 615 | stdin=subprocess.PIPE, |
| 616 | stdout=subprocess.PIPE, |
| 617 | stderr=subprocess.PIPE) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 618 | self.addCleanup(p.stdout.close) |
| 619 | self.addCleanup(p.stderr.close) |
| 620 | self.addCleanup(p.stdin.close) |
Georg Brandl | 1abcbf8 | 2008-07-01 19:28:43 +0000 | [diff] [blame] | 621 | (stdout, stderr) = p.communicate(b"banana") |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 622 | self.assertEqual(stdout, b"banana") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 623 | self.assertStderrEqual(stderr, b"pineapple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 624 | |
Reid Kleckner | 31aa7dd | 2011-03-14 12:02:10 -0400 | [diff] [blame] | 625 | def test_communicate_timeout(self): |
| 626 | p = subprocess.Popen([sys.executable, "-c", |
| 627 | 'import sys,os,time;' |
| 628 | 'sys.stderr.write("pineapple\\n");' |
| 629 | 'time.sleep(1);' |
| 630 | 'sys.stderr.write("pear\\n");' |
| 631 | 'sys.stdout.write(sys.stdin.read())'], |
| 632 | universal_newlines=True, |
| 633 | stdin=subprocess.PIPE, |
| 634 | stdout=subprocess.PIPE, |
| 635 | stderr=subprocess.PIPE) |
| 636 | self.assertRaises(subprocess.TimeoutExpired, p.communicate, "banana", |
| 637 | timeout=0.3) |
| 638 | # Make sure we can keep waiting for it, and that we get the whole output |
| 639 | # after it completes. |
| 640 | (stdout, stderr) = p.communicate() |
| 641 | self.assertEqual(stdout, "banana") |
| 642 | self.assertStderrEqual(stderr.encode(), b"pineapple\npear\n") |
| 643 | |
| 644 | def test_communicate_timeout_large_ouput(self): |
Ross Lagerwall | 003c7a3 | 2012-02-12 09:02:01 +0200 | [diff] [blame] | 645 | # Test an expiring timeout while the child is outputting lots of data. |
Reid Kleckner | 31aa7dd | 2011-03-14 12:02:10 -0400 | [diff] [blame] | 646 | p = subprocess.Popen([sys.executable, "-c", |
| 647 | 'import sys,os,time;' |
| 648 | 'sys.stdout.write("a" * (64 * 1024));' |
| 649 | 'time.sleep(0.2);' |
| 650 | 'sys.stdout.write("a" * (64 * 1024));' |
| 651 | 'time.sleep(0.2);' |
| 652 | 'sys.stdout.write("a" * (64 * 1024));' |
| 653 | 'time.sleep(0.2);' |
| 654 | 'sys.stdout.write("a" * (64 * 1024));'], |
| 655 | stdout=subprocess.PIPE) |
| 656 | self.assertRaises(subprocess.TimeoutExpired, p.communicate, timeout=0.4) |
| 657 | (stdout, _) = p.communicate() |
| 658 | self.assertEqual(len(stdout), 4 * 64 * 1024) |
| 659 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 660 | # Test for the fd leak reported in http://bugs.python.org/issue2791. |
| 661 | def test_communicate_pipe_fd_leak(self): |
Victor Stinner | 667d4b5 | 2010-12-25 22:40:32 +0000 | [diff] [blame] | 662 | for stdin_pipe in (False, True): |
| 663 | for stdout_pipe in (False, True): |
| 664 | for stderr_pipe in (False, True): |
| 665 | options = {} |
| 666 | if stdin_pipe: |
| 667 | options['stdin'] = subprocess.PIPE |
| 668 | if stdout_pipe: |
| 669 | options['stdout'] = subprocess.PIPE |
| 670 | if stderr_pipe: |
| 671 | options['stderr'] = subprocess.PIPE |
| 672 | if not options: |
| 673 | continue |
| 674 | p = subprocess.Popen((sys.executable, "-c", "pass"), **options) |
| 675 | p.communicate() |
| 676 | if p.stdin is not None: |
| 677 | self.assertTrue(p.stdin.closed) |
| 678 | if p.stdout is not None: |
| 679 | self.assertTrue(p.stdout.closed) |
| 680 | if p.stderr is not None: |
| 681 | self.assertTrue(p.stderr.closed) |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 682 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 683 | def test_communicate_returns(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 684 | # communicate() should return None if no redirection is active |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 685 | p = subprocess.Popen([sys.executable, "-c", |
| 686 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 687 | (stdout, stderr) = p.communicate() |
| 688 | self.assertEqual(stdout, None) |
| 689 | self.assertEqual(stderr, None) |
| 690 | |
| 691 | def test_communicate_pipe_buf(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 692 | # communicate() with writes larger than pipe_buf |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 693 | # This test will probably deadlock rather than fail, if |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 694 | # communicate() does not work properly. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 695 | x, y = os.pipe() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 696 | os.close(x) |
| 697 | os.close(y) |
| 698 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 699 | 'import sys,os;' |
| 700 | 'sys.stdout.write(sys.stdin.read(47));' |
Charles-François Natali | 2d51721 | 2011-05-29 16:36:44 +0200 | [diff] [blame] | 701 | 'sys.stderr.write("x" * %d);' |
| 702 | 'sys.stdout.write(sys.stdin.read())' % |
| 703 | support.PIPE_MAX_SIZE], |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 704 | stdin=subprocess.PIPE, |
| 705 | stdout=subprocess.PIPE, |
| 706 | stderr=subprocess.PIPE) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 707 | self.addCleanup(p.stdout.close) |
| 708 | self.addCleanup(p.stderr.close) |
| 709 | self.addCleanup(p.stdin.close) |
Charles-François Natali | 2d51721 | 2011-05-29 16:36:44 +0200 | [diff] [blame] | 710 | string_to_write = b"a" * support.PIPE_MAX_SIZE |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 711 | (stdout, stderr) = p.communicate(string_to_write) |
| 712 | self.assertEqual(stdout, string_to_write) |
| 713 | |
| 714 | def test_writes_before_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 715 | # stdin.write before communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 716 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 717 | 'import sys,os;' |
| 718 | 'sys.stdout.write(sys.stdin.read())'], |
| 719 | stdin=subprocess.PIPE, |
| 720 | stdout=subprocess.PIPE, |
| 721 | stderr=subprocess.PIPE) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 722 | self.addCleanup(p.stdout.close) |
| 723 | self.addCleanup(p.stderr.close) |
| 724 | self.addCleanup(p.stdin.close) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 725 | p.stdin.write(b"banana") |
| 726 | (stdout, stderr) = p.communicate(b"split") |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 727 | self.assertEqual(stdout, b"bananasplit") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 728 | self.assertStderrEqual(stderr, b"") |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 729 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 730 | def test_universal_newlines(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 731 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 732 | 'import sys,os;' + SETBINARY + |
Antoine Pitrou | ec2d269 | 2012-08-05 00:23:40 +0200 | [diff] [blame] | 733 | 'buf = sys.stdout.buffer;' |
| 734 | 'buf.write(sys.stdin.readline().encode());' |
| 735 | 'buf.flush();' |
| 736 | 'buf.write(b"line2\\n");' |
| 737 | 'buf.flush();' |
| 738 | 'buf.write(sys.stdin.read().encode());' |
| 739 | 'buf.flush();' |
| 740 | 'buf.write(b"line4\\n");' |
| 741 | 'buf.flush();' |
| 742 | 'buf.write(b"line5\\r\\n");' |
| 743 | 'buf.flush();' |
| 744 | 'buf.write(b"line6\\r");' |
| 745 | 'buf.flush();' |
| 746 | 'buf.write(b"\\nline7");' |
| 747 | 'buf.flush();' |
| 748 | 'buf.write(b"\\nline8");'], |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 749 | stdin=subprocess.PIPE, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 750 | stdout=subprocess.PIPE, |
| 751 | universal_newlines=1) |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 752 | p.stdin.write("line1\n") |
| 753 | self.assertEqual(p.stdout.readline(), "line1\n") |
| 754 | p.stdin.write("line3\n") |
| 755 | p.stdin.close() |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 756 | self.addCleanup(p.stdout.close) |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 757 | self.assertEqual(p.stdout.readline(), |
| 758 | "line2\n") |
| 759 | self.assertEqual(p.stdout.read(6), |
| 760 | "line3\n") |
| 761 | self.assertEqual(p.stdout.read(), |
| 762 | "line4\nline5\nline6\nline7\nline8") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 763 | |
| 764 | def test_universal_newlines_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 765 | # universal newlines through communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 766 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 767 | 'import sys,os;' + SETBINARY + |
Antoine Pitrou | ec2d269 | 2012-08-05 00:23:40 +0200 | [diff] [blame] | 768 | 'buf = sys.stdout.buffer;' |
| 769 | 'buf.write(b"line2\\n");' |
| 770 | 'buf.flush();' |
| 771 | 'buf.write(b"line4\\n");' |
| 772 | 'buf.flush();' |
| 773 | 'buf.write(b"line5\\r\\n");' |
| 774 | 'buf.flush();' |
| 775 | 'buf.write(b"line6\\r");' |
| 776 | 'buf.flush();' |
| 777 | 'buf.write(b"\\nline7");' |
| 778 | 'buf.flush();' |
| 779 | 'buf.write(b"\\nline8");'], |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 780 | stderr=subprocess.PIPE, |
| 781 | stdout=subprocess.PIPE, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 782 | universal_newlines=1) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 783 | self.addCleanup(p.stdout.close) |
| 784 | self.addCleanup(p.stderr.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 785 | (stdout, stderr) = p.communicate() |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 786 | self.assertEqual(stdout, |
| 787 | "line2\nline4\nline5\nline6\nline7\nline8") |
| 788 | |
| 789 | def test_universal_newlines_communicate_stdin(self): |
| 790 | # universal newlines through communicate(), with only stdin |
| 791 | p = subprocess.Popen([sys.executable, "-c", |
Andrew Svetlov | 47ec25d | 2012-08-19 16:25:37 +0300 | [diff] [blame] | 792 | 'import sys,os;' + SETBINARY + textwrap.dedent(''' |
| 793 | s = sys.stdin.readline() |
| 794 | assert s == "line1\\n", repr(s) |
| 795 | s = sys.stdin.read() |
| 796 | assert s == "line3\\n", repr(s) |
| 797 | ''')], |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 798 | stdin=subprocess.PIPE, |
| 799 | universal_newlines=1) |
| 800 | (stdout, stderr) = p.communicate("line1\nline3\n") |
| 801 | self.assertEqual(p.returncode, 0) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 802 | |
Andrew Svetlov | f376507 | 2012-08-14 18:35:17 +0300 | [diff] [blame] | 803 | def test_universal_newlines_communicate_input_none(self): |
| 804 | # Test communicate(input=None) with universal newlines. |
| 805 | # |
| 806 | # We set stdout to PIPE because, as of this writing, a different |
| 807 | # code path is tested when the number of pipes is zero or one. |
| 808 | p = subprocess.Popen([sys.executable, "-c", "pass"], |
| 809 | stdin=subprocess.PIPE, |
| 810 | stdout=subprocess.PIPE, |
| 811 | universal_newlines=True) |
| 812 | p.communicate() |
| 813 | self.assertEqual(p.returncode, 0) |
| 814 | |
Andrew Svetlov | 5395d2f | 2012-08-15 22:46:43 +0300 | [diff] [blame] | 815 | def test_universal_newlines_communicate_stdin_stdout_stderr(self): |
Andrew Svetlov | 47ec25d | 2012-08-19 16:25:37 +0300 | [diff] [blame] | 816 | # universal newlines through communicate(), with stdin, stdout, stderr |
Andrew Svetlov | 5395d2f | 2012-08-15 22:46:43 +0300 | [diff] [blame] | 817 | p = subprocess.Popen([sys.executable, "-c", |
Andrew Svetlov | 47ec25d | 2012-08-19 16:25:37 +0300 | [diff] [blame] | 818 | 'import sys,os;' + SETBINARY + textwrap.dedent(''' |
| 819 | s = sys.stdin.buffer.readline() |
| 820 | sys.stdout.buffer.write(s) |
| 821 | sys.stdout.buffer.write(b"line2\\r") |
| 822 | sys.stderr.buffer.write(b"eline2\\n") |
| 823 | s = sys.stdin.buffer.read() |
| 824 | sys.stdout.buffer.write(s) |
| 825 | sys.stdout.buffer.write(b"line4\\n") |
| 826 | sys.stdout.buffer.write(b"line5\\r\\n") |
| 827 | sys.stderr.buffer.write(b"eline6\\r") |
| 828 | sys.stderr.buffer.write(b"eline7\\r\\nz") |
| 829 | ''')], |
Andrew Svetlov | 5395d2f | 2012-08-15 22:46:43 +0300 | [diff] [blame] | 830 | stdin=subprocess.PIPE, |
| 831 | stderr=subprocess.PIPE, |
| 832 | stdout=subprocess.PIPE, |
Andrew Svetlov | 47ec25d | 2012-08-19 16:25:37 +0300 | [diff] [blame] | 833 | universal_newlines=True) |
Andrew Svetlov | 5395d2f | 2012-08-15 22:46:43 +0300 | [diff] [blame] | 834 | self.addCleanup(p.stdout.close) |
| 835 | self.addCleanup(p.stderr.close) |
| 836 | (stdout, stderr) = p.communicate("line1\nline3\n") |
| 837 | self.assertEqual(p.returncode, 0) |
Andrew Svetlov | 943c5b3 | 2012-08-16 20:17:47 +0300 | [diff] [blame] | 838 | self.assertEqual("line1\nline2\nline3\nline4\nline5\n", stdout) |
Andrew Svetlov | 5395d2f | 2012-08-15 22:46:43 +0300 | [diff] [blame] | 839 | # Python debug build push something like "[42442 refs]\n" |
| 840 | # to stderr at exit of subprocess. |
Andrew Svetlov | 943c5b3 | 2012-08-16 20:17:47 +0300 | [diff] [blame] | 841 | # Don't use assertStderrEqual because it strips CR and LF from output. |
| 842 | self.assertTrue(stderr.startswith("eline2\neline6\neline7\n")) |
Andrew Svetlov | 5395d2f | 2012-08-15 22:46:43 +0300 | [diff] [blame] | 843 | |
Andrew Svetlov | 8286071 | 2012-08-19 22:13:41 +0300 | [diff] [blame] | 844 | def test_universal_newlines_communicate_encodings(self): |
| 845 | # Check that universal newlines mode works for various encodings, |
| 846 | # in particular for encodings in the UTF-16 and UTF-32 families. |
| 847 | # See issue #15595. |
| 848 | # |
| 849 | # UTF-16 and UTF-32-BE are sufficient to check both with BOM and |
| 850 | # without, and UTF-16 and UTF-32. |
| 851 | for encoding in ['utf-16', 'utf-32-be']: |
| 852 | old_getpreferredencoding = locale.getpreferredencoding |
| 853 | # Indirectly via io.TextIOWrapper, Popen() defaults to |
| 854 | # locale.getpreferredencoding(False) and earlier in Python 3.2 to |
| 855 | # locale.getpreferredencoding(). |
| 856 | def getpreferredencoding(do_setlocale=True): |
| 857 | return encoding |
| 858 | code = ("import sys; " |
| 859 | r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" % |
| 860 | encoding) |
| 861 | args = [sys.executable, '-c', code] |
| 862 | try: |
| 863 | locale.getpreferredencoding = getpreferredencoding |
| 864 | # We set stdin to be non-None because, as of this writing, |
| 865 | # a different code path is used when the number of pipes is |
| 866 | # zero or one. |
| 867 | popen = subprocess.Popen(args, universal_newlines=True, |
| 868 | stdin=subprocess.PIPE, |
| 869 | stdout=subprocess.PIPE) |
| 870 | stdout, stderr = popen.communicate(input='') |
| 871 | finally: |
| 872 | locale.getpreferredencoding = old_getpreferredencoding |
Andrew Svetlov | 8286071 | 2012-08-19 22:13:41 +0300 | [diff] [blame] | 873 | self.assertEqual(stdout, '1\n2\n3\n4') |
| 874 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 875 | def test_no_leaking(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 876 | # Make sure we leak no resources |
Antoine Pitrou | 8db3027 | 2010-09-18 22:38:48 +0000 | [diff] [blame] | 877 | if not mswindows: |
Peter Astrand | f7f1bb7 | 2005-03-03 20:47:37 +0000 | [diff] [blame] | 878 | max_handles = 1026 # too much for most UNIX systems |
| 879 | else: |
Antoine Pitrou | 8db3027 | 2010-09-18 22:38:48 +0000 | [diff] [blame] | 880 | max_handles = 2050 # too much for (at least some) Windows setups |
| 881 | handles = [] |
Gregory P. Smith | 81ce685 | 2011-03-15 02:04:11 -0400 | [diff] [blame] | 882 | tmpdir = tempfile.mkdtemp() |
Antoine Pitrou | 8db3027 | 2010-09-18 22:38:48 +0000 | [diff] [blame] | 883 | try: |
| 884 | for i in range(max_handles): |
| 885 | try: |
Gregory P. Smith | 81ce685 | 2011-03-15 02:04:11 -0400 | [diff] [blame] | 886 | tmpfile = os.path.join(tmpdir, support.TESTFN) |
| 887 | handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT)) |
Antoine Pitrou | 8db3027 | 2010-09-18 22:38:48 +0000 | [diff] [blame] | 888 | except OSError as e: |
| 889 | if e.errno != errno.EMFILE: |
| 890 | raise |
| 891 | break |
| 892 | else: |
| 893 | self.skipTest("failed to reach the file descriptor limit " |
| 894 | "(tried %d)" % max_handles) |
| 895 | # Close a couple of them (should be enough for a subprocess) |
| 896 | for i in range(10): |
| 897 | os.close(handles.pop()) |
| 898 | # Loop creating some subprocesses. If one of them leaks some fds, |
| 899 | # the next loop iteration will fail by reaching the max fd limit. |
| 900 | for i in range(15): |
| 901 | p = subprocess.Popen([sys.executable, "-c", |
| 902 | "import sys;" |
| 903 | "sys.stdout.write(sys.stdin.read())"], |
| 904 | stdin=subprocess.PIPE, |
| 905 | stdout=subprocess.PIPE, |
| 906 | stderr=subprocess.PIPE) |
| 907 | data = p.communicate(b"lime")[0] |
| 908 | self.assertEqual(data, b"lime") |
| 909 | finally: |
| 910 | for h in handles: |
| 911 | os.close(h) |
Gregory P. Smith | 81ce685 | 2011-03-15 02:04:11 -0400 | [diff] [blame] | 912 | shutil.rmtree(tmpdir) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 913 | |
| 914 | def test_list2cmdline(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 915 | self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), |
| 916 | '"a b c" d e') |
| 917 | self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), |
| 918 | 'ab\\"c \\ d') |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 919 | self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']), |
| 920 | 'ab\\"c " \\\\" d') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 921 | self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']), |
| 922 | 'a\\\\\\b "de fg" h') |
| 923 | self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']), |
| 924 | 'a\\\\\\"b c d') |
| 925 | self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']), |
| 926 | '"a\\\\b c" d e') |
| 927 | self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']), |
| 928 | '"a\\\\b\\ c" d e') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 929 | self.assertEqual(subprocess.list2cmdline(['ab', '']), |
| 930 | 'ab ""') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 931 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 932 | def test_poll(self): |
Ross Lagerwall | ab66d2a | 2012-02-12 09:01:30 +0200 | [diff] [blame] | 933 | p = subprocess.Popen([sys.executable, "-c", |
Ross Lagerwall | e7ad419 | 2012-02-22 06:02:07 +0200 | [diff] [blame] | 934 | "import os; os.read(0, 1)"], |
| 935 | stdin=subprocess.PIPE) |
Ross Lagerwall | ab66d2a | 2012-02-12 09:01:30 +0200 | [diff] [blame] | 936 | self.addCleanup(p.stdin.close) |
| 937 | self.assertIsNone(p.poll()) |
| 938 | os.write(p.stdin.fileno(), b'A') |
| 939 | p.wait() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 940 | # Subsequent invocations should just return the returncode |
| 941 | self.assertEqual(p.poll(), 0) |
| 942 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 943 | def test_wait(self): |
Ross Lagerwall | ab66d2a | 2012-02-12 09:01:30 +0200 | [diff] [blame] | 944 | p = subprocess.Popen([sys.executable, "-c", "pass"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 945 | self.assertEqual(p.wait(), 0) |
| 946 | # Subsequent invocations should just return the returncode |
| 947 | self.assertEqual(p.wait(), 0) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 948 | |
Reid Kleckner | 31aa7dd | 2011-03-14 12:02:10 -0400 | [diff] [blame] | 949 | def test_wait_timeout(self): |
| 950 | p = subprocess.Popen([sys.executable, |
Reid Kleckner | 93479cc | 2011-03-14 19:32:41 -0400 | [diff] [blame] | 951 | "-c", "import time; time.sleep(0.1)"]) |
Reid Kleckner | 2b228f0 | 2011-03-16 16:57:54 -0400 | [diff] [blame] | 952 | with self.assertRaises(subprocess.TimeoutExpired) as c: |
| 953 | p.wait(timeout=0.01) |
| 954 | self.assertIn("0.01", str(c.exception)) # For coverage of __str__. |
Reid Kleckner | da9ac72 | 2011-03-16 17:08:21 -0400 | [diff] [blame] | 955 | # Some heavily loaded buildbots (sparc Debian 3.x) require this much |
| 956 | # time to start. |
| 957 | self.assertEqual(p.wait(timeout=3), 0) |
Reid Kleckner | 31aa7dd | 2011-03-14 12:02:10 -0400 | [diff] [blame] | 958 | |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 959 | def test_invalid_bufsize(self): |
| 960 | # an invalid type of the bufsize argument should raise |
| 961 | # TypeError. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 962 | with self.assertRaises(TypeError): |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 963 | subprocess.Popen([sys.executable, "-c", "pass"], "orange") |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 964 | |
Guido van Rossum | 46a05a7 | 2007-06-07 21:56:45 +0000 | [diff] [blame] | 965 | def test_bufsize_is_none(self): |
| 966 | # bufsize=None should be the same as bufsize=0. |
| 967 | p = subprocess.Popen([sys.executable, "-c", "pass"], None) |
| 968 | self.assertEqual(p.wait(), 0) |
| 969 | # Again with keyword arg |
| 970 | p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None) |
| 971 | self.assertEqual(p.wait(), 0) |
| 972 | |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 973 | def test_leaking_fds_on_error(self): |
| 974 | # see bug #5179: Popen leaks file descriptors to PIPEs if |
| 975 | # the child fails to execute; this will eventually exhaust |
| 976 | # the maximum number of open fds. 1024 seems a very common |
| 977 | # value for that limit, but Windows has 2048, so we loop |
| 978 | # 1024 times (each call leaked two fds). |
| 979 | for i in range(1024): |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 980 | with self.assertRaises(OSError) as c: |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 981 | subprocess.Popen(['nonexisting_i_hope'], |
| 982 | stdout=subprocess.PIPE, |
| 983 | stderr=subprocess.PIPE) |
R David Murray | 384069c | 2011-03-13 22:26:53 -0400 | [diff] [blame] | 984 | # ignore errors that indicate the command was not found |
R David Murray | 6924bd7 | 2011-03-13 22:48:55 -0400 | [diff] [blame] | 985 | if c.exception.errno not in (errno.ENOENT, errno.EACCES): |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 986 | raise c.exception |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 987 | |
Victor Stinner | b369358 | 2010-05-21 20:13:12 +0000 | [diff] [blame] | 988 | def test_issue8780(self): |
| 989 | # Ensure that stdout is inherited from the parent |
| 990 | # if stdout=PIPE is not used |
| 991 | code = ';'.join(( |
| 992 | 'import subprocess, sys', |
| 993 | 'retcode = subprocess.call(' |
| 994 | "[sys.executable, '-c', 'print(\"Hello World!\")'])", |
| 995 | 'assert retcode == 0')) |
| 996 | output = subprocess.check_output([sys.executable, '-c', code]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 997 | self.assertTrue(output.startswith(b'Hello World!'), ascii(output)) |
Victor Stinner | b369358 | 2010-05-21 20:13:12 +0000 | [diff] [blame] | 998 | |
Tim Golden | af5ac39 | 2010-08-06 13:03:56 +0000 | [diff] [blame] | 999 | def test_handles_closed_on_exception(self): |
| 1000 | # If CreateProcess exits with an error, ensure the |
| 1001 | # duplicate output handles are released |
| 1002 | ifhandle, ifname = mkstemp() |
| 1003 | ofhandle, ofname = mkstemp() |
| 1004 | efhandle, efname = mkstemp() |
| 1005 | try: |
| 1006 | subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle, |
| 1007 | stderr=efhandle) |
| 1008 | except OSError: |
| 1009 | os.close(ifhandle) |
| 1010 | os.remove(ifname) |
| 1011 | os.close(ofhandle) |
| 1012 | os.remove(ofname) |
| 1013 | os.close(efhandle) |
| 1014 | os.remove(efname) |
| 1015 | self.assertFalse(os.path.exists(ifname)) |
| 1016 | self.assertFalse(os.path.exists(ofname)) |
| 1017 | self.assertFalse(os.path.exists(efname)) |
| 1018 | |
Ross Lagerwall | 4f61b02 | 2011-04-05 15:34:00 +0200 | [diff] [blame] | 1019 | def test_communicate_epipe(self): |
| 1020 | # Issue 10963: communicate() should hide EPIPE |
| 1021 | p = subprocess.Popen([sys.executable, "-c", 'pass'], |
| 1022 | stdin=subprocess.PIPE, |
| 1023 | stdout=subprocess.PIPE, |
| 1024 | stderr=subprocess.PIPE) |
| 1025 | self.addCleanup(p.stdout.close) |
| 1026 | self.addCleanup(p.stderr.close) |
| 1027 | self.addCleanup(p.stdin.close) |
| 1028 | p.communicate(b"x" * 2**20) |
| 1029 | |
| 1030 | def test_communicate_epipe_only_stdin(self): |
| 1031 | # Issue 10963: communicate() should hide EPIPE |
| 1032 | p = subprocess.Popen([sys.executable, "-c", 'pass'], |
| 1033 | stdin=subprocess.PIPE) |
| 1034 | self.addCleanup(p.stdin.close) |
Ross Lagerwall | ab66d2a | 2012-02-12 09:01:30 +0200 | [diff] [blame] | 1035 | p.wait() |
Ross Lagerwall | 4f61b02 | 2011-04-05 15:34:00 +0200 | [diff] [blame] | 1036 | p.communicate(b"x" * 2**20) |
| 1037 | |
Ross Lagerwall | ab66d2a | 2012-02-12 09:01:30 +0200 | [diff] [blame] | 1038 | @unittest.skipUnless(hasattr(signal, 'SIGUSR1'), |
| 1039 | "Requires signal.SIGUSR1") |
| 1040 | @unittest.skipUnless(hasattr(os, 'kill'), |
| 1041 | "Requires os.kill") |
| 1042 | @unittest.skipUnless(hasattr(os, 'getppid'), |
| 1043 | "Requires os.getppid") |
Victor Stinner | 2cfb6f3 | 2011-07-05 14:00:56 +0200 | [diff] [blame] | 1044 | def test_communicate_eintr(self): |
| 1045 | # Issue #12493: communicate() should handle EINTR |
| 1046 | def handler(signum, frame): |
| 1047 | pass |
Ross Lagerwall | ab66d2a | 2012-02-12 09:01:30 +0200 | [diff] [blame] | 1048 | old_handler = signal.signal(signal.SIGUSR1, handler) |
| 1049 | self.addCleanup(signal.signal, signal.SIGUSR1, old_handler) |
Victor Stinner | 2cfb6f3 | 2011-07-05 14:00:56 +0200 | [diff] [blame] | 1050 | |
Ross Lagerwall | ab66d2a | 2012-02-12 09:01:30 +0200 | [diff] [blame] | 1051 | args = [sys.executable, "-c", |
| 1052 | 'import os, signal;' |
| 1053 | 'os.kill(os.getppid(), signal.SIGUSR1)'] |
Victor Stinner | 2cfb6f3 | 2011-07-05 14:00:56 +0200 | [diff] [blame] | 1054 | for stream in ('stdout', 'stderr'): |
| 1055 | kw = {stream: subprocess.PIPE} |
| 1056 | with subprocess.Popen(args, **kw) as process: |
Ross Lagerwall | ab66d2a | 2012-02-12 09:01:30 +0200 | [diff] [blame] | 1057 | # communicate() will be interrupted by SIGUSR1 |
Victor Stinner | 2cfb6f3 | 2011-07-05 14:00:56 +0200 | [diff] [blame] | 1058 | process.communicate() |
| 1059 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1060 | |
Gregory P. Smith | 3d8e776 | 2012-11-10 22:32:22 -0800 | [diff] [blame] | 1061 | # This test is Linux-ish specific for simplicity to at least have |
| 1062 | # some coverage. It is not a platform specific bug. |
| 1063 | @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()), |
| 1064 | "Linux specific") |
| 1065 | def test_failed_child_execute_fd_leak(self): |
| 1066 | """Test for the fork() failure fd leak reported in issue16327.""" |
| 1067 | fd_directory = '/proc/%d/fd' % os.getpid() |
| 1068 | fds_before_popen = os.listdir(fd_directory) |
| 1069 | with self.assertRaises(PopenTestException): |
| 1070 | PopenExecuteChildRaises( |
| 1071 | [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE, |
| 1072 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 1073 | |
| 1074 | # NOTE: This test doesn't verify that the real _execute_child |
| 1075 | # does not close the file descriptors itself on the way out |
| 1076 | # during an exception. Code inspection has confirmed that. |
| 1077 | |
| 1078 | fds_after_exception = os.listdir(fd_directory) |
| 1079 | self.assertEqual(fds_before_popen, fds_after_exception) |
| 1080 | |
| 1081 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1082 | # context manager |
| 1083 | class _SuppressCoreFiles(object): |
| 1084 | """Try to prevent core files from being created.""" |
| 1085 | old_limit = None |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1086 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1087 | def __enter__(self): |
| 1088 | """Try to save previous ulimit, then set it to (0, 0).""" |
Benjamin Peterson | 964561b | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 1089 | if resource is not None: |
| 1090 | try: |
| 1091 | self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) |
| 1092 | resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) |
| 1093 | except (ValueError, resource.error): |
| 1094 | pass |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1095 | |
Ronald Oussoren | 102d11a | 2010-07-23 09:50:05 +0000 | [diff] [blame] | 1096 | if sys.platform == 'darwin': |
| 1097 | # Check if the 'Crash Reporter' on OSX was configured |
| 1098 | # in 'Developer' mode and warn that it will get triggered |
| 1099 | # when it is. |
| 1100 | # |
| 1101 | # This assumes that this context manager is used in tests |
| 1102 | # that might trigger the next manager. |
| 1103 | value = subprocess.Popen(['/usr/bin/defaults', 'read', |
| 1104 | 'com.apple.CrashReporter', 'DialogType'], |
| 1105 | stdout=subprocess.PIPE).communicate()[0] |
| 1106 | if value.strip() == b'developer': |
| 1107 | print("this tests triggers the Crash Reporter, " |
| 1108 | "that is intentional", end='') |
| 1109 | sys.stdout.flush() |
| 1110 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1111 | def __exit__(self, *args): |
| 1112 | """Return core file behavior to default.""" |
| 1113 | if self.old_limit is None: |
| 1114 | return |
Benjamin Peterson | 964561b | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 1115 | if resource is not None: |
| 1116 | try: |
| 1117 | resource.setrlimit(resource.RLIMIT_CORE, self.old_limit) |
| 1118 | except (ValueError, resource.error): |
| 1119 | pass |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1120 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1121 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 1122 | @unittest.skipIf(mswindows, "POSIX specific tests") |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 1123 | class POSIXProcessTestCase(BaseTestCase): |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 1124 | |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 1125 | def setUp(self): |
| 1126 | super().setUp() |
| 1127 | self._nonexistent_dir = "/_this/pa.th/does/not/exist" |
| 1128 | |
| 1129 | def _get_chdir_exception(self): |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1130 | try: |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 1131 | os.chdir(self._nonexistent_dir) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1132 | except OSError as e: |
| 1133 | # This avoids hard coding the errno value or the OS perror() |
| 1134 | # string and instead capture the exception that we want to see |
| 1135 | # below for comparison. |
| 1136 | desired_exception = e |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 1137 | desired_exception.strerror += ': ' + repr(self._nonexistent_dir) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1138 | else: |
| 1139 | self.fail("chdir to nonexistant directory %s succeeded." % |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 1140 | self._nonexistent_dir) |
| 1141 | return desired_exception |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1142 | |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 1143 | def test_exception_cwd(self): |
| 1144 | """Test error in the child raised in the parent for a bad cwd.""" |
| 1145 | desired_exception = self._get_chdir_exception() |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1146 | try: |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1147 | p = subprocess.Popen([sys.executable, "-c", ""], |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 1148 | cwd=self._nonexistent_dir) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1149 | except OSError as e: |
| 1150 | # Test that the child process chdir failure actually makes |
| 1151 | # it up to the parent process as the correct exception. |
| 1152 | self.assertEqual(desired_exception.errno, e.errno) |
| 1153 | self.assertEqual(desired_exception.strerror, e.strerror) |
| 1154 | else: |
| 1155 | self.fail("Expected OSError: %s" % desired_exception) |
| 1156 | |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 1157 | def test_exception_bad_executable(self): |
| 1158 | """Test error in the child raised in the parent for a bad executable.""" |
| 1159 | desired_exception = self._get_chdir_exception() |
| 1160 | try: |
| 1161 | p = subprocess.Popen([sys.executable, "-c", ""], |
| 1162 | executable=self._nonexistent_dir) |
| 1163 | except OSError as e: |
| 1164 | # Test that the child process exec failure actually makes |
| 1165 | # it up to the parent process as the correct exception. |
| 1166 | self.assertEqual(desired_exception.errno, e.errno) |
| 1167 | self.assertEqual(desired_exception.strerror, e.strerror) |
| 1168 | else: |
| 1169 | self.fail("Expected OSError: %s" % desired_exception) |
| 1170 | |
| 1171 | def test_exception_bad_args_0(self): |
| 1172 | """Test error in the child raised in the parent for a bad args[0].""" |
| 1173 | desired_exception = self._get_chdir_exception() |
| 1174 | try: |
| 1175 | p = subprocess.Popen([self._nonexistent_dir, "-c", ""]) |
| 1176 | except OSError as e: |
| 1177 | # Test that the child process exec failure actually makes |
| 1178 | # it up to the parent process as the correct exception. |
| 1179 | self.assertEqual(desired_exception.errno, e.errno) |
| 1180 | self.assertEqual(desired_exception.strerror, e.strerror) |
| 1181 | else: |
| 1182 | self.fail("Expected OSError: %s" % desired_exception) |
| 1183 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1184 | def test_restore_signals(self): |
| 1185 | # Code coverage for both values of restore_signals to make sure it |
| 1186 | # at least does not blow up. |
| 1187 | # A test for behavior would be complex. Contributions welcome. |
| 1188 | subprocess.call([sys.executable, "-c", ""], restore_signals=True) |
| 1189 | subprocess.call([sys.executable, "-c", ""], restore_signals=False) |
| 1190 | |
| 1191 | def test_start_new_session(self): |
| 1192 | # For code coverage of calling setsid(). We don't care if we get an |
| 1193 | # EPERM error from it depending on the test execution environment, that |
| 1194 | # still indicates that it was called. |
| 1195 | try: |
| 1196 | output = subprocess.check_output( |
| 1197 | [sys.executable, "-c", |
| 1198 | "import os; print(os.getpgid(os.getpid()))"], |
| 1199 | start_new_session=True) |
| 1200 | except OSError as e: |
| 1201 | if e.errno != errno.EPERM: |
| 1202 | raise |
| 1203 | else: |
| 1204 | parent_pgid = os.getpgid(os.getpid()) |
| 1205 | child_pgid = int(output) |
| 1206 | self.assertNotEqual(parent_pgid, child_pgid) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1207 | |
| 1208 | def test_run_abort(self): |
| 1209 | # returncode handles signal termination |
| 1210 | with _SuppressCoreFiles(): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1211 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1212 | 'import os; os.abort()']) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1213 | p.wait() |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1214 | self.assertEqual(-p.returncode, signal.SIGABRT) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1215 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1216 | def test_preexec(self): |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1217 | # DISCLAIMER: Setting environment variables is *not* a good use |
| 1218 | # of a preexec_fn. This is merely a test. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1219 | p = subprocess.Popen([sys.executable, "-c", |
| 1220 | 'import sys,os;' |
| 1221 | 'sys.stdout.write(os.getenv("FRUIT"))'], |
| 1222 | stdout=subprocess.PIPE, |
| 1223 | preexec_fn=lambda: os.putenv("FRUIT", "apple")) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 1224 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1225 | self.assertEqual(p.stdout.read(), b"apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1226 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1227 | def test_preexec_exception(self): |
| 1228 | def raise_it(): |
| 1229 | raise ValueError("What if two swallows carried a coconut?") |
| 1230 | try: |
| 1231 | p = subprocess.Popen([sys.executable, "-c", ""], |
| 1232 | preexec_fn=raise_it) |
Gregory P. Smith | 8d07c26 | 2012-11-10 23:53:47 -0800 | [diff] [blame] | 1233 | except subprocess.SubprocessError as e: |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1234 | self.assertTrue( |
| 1235 | subprocess._posixsubprocess, |
| 1236 | "Expected a ValueError from the preexec_fn") |
| 1237 | except ValueError as e: |
| 1238 | self.assertIn("coconut", e.args[0]) |
| 1239 | else: |
| 1240 | self.fail("Exception raised by preexec_fn did not make it " |
| 1241 | "to the parent process.") |
| 1242 | |
Gregory P. Smith | e27faac | 2012-11-11 09:59:27 -0800 | [diff] [blame] | 1243 | class _TestExecuteChildPopen(subprocess.Popen): |
| 1244 | """Used to test behavior at the end of _execute_child.""" |
| 1245 | def __init__(self, testcase, *args, **kwargs): |
| 1246 | self._testcase = testcase |
| 1247 | subprocess.Popen.__init__(self, *args, **kwargs) |
Gregory P. Smith | 12489d9 | 2012-11-11 01:37:02 -0800 | [diff] [blame] | 1248 | |
Gregory P. Smith | e27faac | 2012-11-11 09:59:27 -0800 | [diff] [blame] | 1249 | def _execute_child(self, *args, **kwargs): |
Gregory P. Smith | 12489d9 | 2012-11-11 01:37:02 -0800 | [diff] [blame] | 1250 | try: |
Gregory P. Smith | e27faac | 2012-11-11 09:59:27 -0800 | [diff] [blame] | 1251 | subprocess.Popen._execute_child(self, *args, **kwargs) |
Gregory P. Smith | 12489d9 | 2012-11-11 01:37:02 -0800 | [diff] [blame] | 1252 | finally: |
| 1253 | # Open a bunch of file descriptors and verify that |
| 1254 | # none of them are the same as the ones the Popen |
| 1255 | # instance is using for stdin/stdout/stderr. |
| 1256 | devzero_fds = [os.open("/dev/zero", os.O_RDONLY) |
| 1257 | for _ in range(8)] |
| 1258 | try: |
| 1259 | for fd in devzero_fds: |
Gregory P. Smith | e27faac | 2012-11-11 09:59:27 -0800 | [diff] [blame] | 1260 | self._testcase.assertNotIn( |
| 1261 | fd, (self.stdin.fileno(), self.stdout.fileno(), |
| 1262 | self.stderr.fileno()), |
Gregory P. Smith | 12489d9 | 2012-11-11 01:37:02 -0800 | [diff] [blame] | 1263 | msg="At least one fd was closed early.") |
| 1264 | finally: |
| 1265 | map(os.close, devzero_fds) |
| 1266 | |
Gregory P. Smith | e27faac | 2012-11-11 09:59:27 -0800 | [diff] [blame] | 1267 | @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.") |
| 1268 | def test_preexec_errpipe_does_not_double_close_pipes(self): |
| 1269 | """Issue16140: Don't double close pipes on preexec error.""" |
| 1270 | |
| 1271 | def raise_it(): |
Gregory P. Smith | 65ee6ec | 2012-11-11 10:12:40 -0800 | [diff] [blame] | 1272 | raise subprocess.SubprocessError( |
| 1273 | "force the _execute_child() errpipe_data path.") |
Gregory P. Smith | 12489d9 | 2012-11-11 01:37:02 -0800 | [diff] [blame] | 1274 | |
Gregory P. Smith | c2c4cb6 | 2012-11-11 01:41:49 -0800 | [diff] [blame] | 1275 | with self.assertRaises(subprocess.SubprocessError): |
Gregory P. Smith | e27faac | 2012-11-11 09:59:27 -0800 | [diff] [blame] | 1276 | self._TestExecuteChildPopen( |
| 1277 | self, [sys.executable, "-c", "pass"], |
Gregory P. Smith | 12489d9 | 2012-11-11 01:37:02 -0800 | [diff] [blame] | 1278 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 1279 | stderr=subprocess.PIPE, preexec_fn=raise_it) |
| 1280 | |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 1281 | def test_preexec_gc_module_failure(self): |
| 1282 | # This tests the code that disables garbage collection if the child |
| 1283 | # process will execute any Python. |
| 1284 | def raise_runtime_error(): |
| 1285 | raise RuntimeError("this shouldn't escape") |
| 1286 | enabled = gc.isenabled() |
| 1287 | orig_gc_disable = gc.disable |
| 1288 | orig_gc_isenabled = gc.isenabled |
| 1289 | try: |
| 1290 | gc.disable() |
| 1291 | self.assertFalse(gc.isenabled()) |
| 1292 | subprocess.call([sys.executable, '-c', ''], |
| 1293 | preexec_fn=lambda: None) |
| 1294 | self.assertFalse(gc.isenabled(), |
| 1295 | "Popen enabled gc when it shouldn't.") |
| 1296 | |
| 1297 | gc.enable() |
| 1298 | self.assertTrue(gc.isenabled()) |
| 1299 | subprocess.call([sys.executable, '-c', ''], |
| 1300 | preexec_fn=lambda: None) |
| 1301 | self.assertTrue(gc.isenabled(), "Popen left gc disabled.") |
| 1302 | |
| 1303 | gc.disable = raise_runtime_error |
| 1304 | self.assertRaises(RuntimeError, subprocess.Popen, |
| 1305 | [sys.executable, '-c', ''], |
| 1306 | preexec_fn=lambda: None) |
| 1307 | |
| 1308 | del gc.isenabled # force an AttributeError |
| 1309 | self.assertRaises(AttributeError, subprocess.Popen, |
| 1310 | [sys.executable, '-c', ''], |
| 1311 | preexec_fn=lambda: None) |
| 1312 | finally: |
| 1313 | gc.disable = orig_gc_disable |
| 1314 | gc.isenabled = orig_gc_isenabled |
| 1315 | if not enabled: |
| 1316 | gc.disable() |
| 1317 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1318 | def test_args_string(self): |
| 1319 | # args is a string |
| 1320 | fd, fname = mkstemp() |
| 1321 | # reopen in text mode |
Victor Stinner | f6782ac | 2010-10-16 23:46:43 +0000 | [diff] [blame] | 1322 | with open(fd, "w", errors="surrogateescape") as fobj: |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1323 | fobj.write("#!/bin/sh\n") |
| 1324 | fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 1325 | sys.executable) |
| 1326 | os.chmod(fname, 0o700) |
| 1327 | p = subprocess.Popen(fname) |
| 1328 | p.wait() |
| 1329 | os.remove(fname) |
| 1330 | self.assertEqual(p.returncode, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1331 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1332 | def test_invalid_args(self): |
| 1333 | # invalid arguments should raise ValueError |
| 1334 | self.assertRaises(ValueError, subprocess.call, |
| 1335 | [sys.executable, "-c", |
| 1336 | "import sys; sys.exit(47)"], |
| 1337 | startupinfo=47) |
| 1338 | self.assertRaises(ValueError, subprocess.call, |
| 1339 | [sys.executable, "-c", |
| 1340 | "import sys; sys.exit(47)"], |
| 1341 | creationflags=47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1342 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1343 | def test_shell_sequence(self): |
| 1344 | # Run command through the shell (sequence) |
| 1345 | newenv = os.environ.copy() |
| 1346 | newenv["FRUIT"] = "apple" |
| 1347 | p = subprocess.Popen(["echo $FRUIT"], shell=1, |
| 1348 | stdout=subprocess.PIPE, |
| 1349 | env=newenv) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 1350 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1351 | self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1352 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1353 | def test_shell_string(self): |
| 1354 | # Run command through the shell (string) |
| 1355 | newenv = os.environ.copy() |
| 1356 | newenv["FRUIT"] = "apple" |
| 1357 | p = subprocess.Popen("echo $FRUIT", shell=1, |
| 1358 | stdout=subprocess.PIPE, |
| 1359 | env=newenv) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 1360 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1361 | self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple") |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1362 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1363 | def test_call_string(self): |
| 1364 | # call() function with string argument on UNIX |
| 1365 | fd, fname = mkstemp() |
| 1366 | # reopen in text mode |
Victor Stinner | f6782ac | 2010-10-16 23:46:43 +0000 | [diff] [blame] | 1367 | with open(fd, "w", errors="surrogateescape") as fobj: |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1368 | fobj.write("#!/bin/sh\n") |
| 1369 | fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 1370 | sys.executable) |
| 1371 | os.chmod(fname, 0o700) |
| 1372 | rc = subprocess.call(fname) |
| 1373 | os.remove(fname) |
| 1374 | self.assertEqual(rc, 47) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1375 | |
Stefan Krah | 9542cc6 | 2010-07-19 14:20:53 +0000 | [diff] [blame] | 1376 | def test_specific_shell(self): |
| 1377 | # Issue #9265: Incorrect name passed as arg[0]. |
| 1378 | shells = [] |
| 1379 | for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']: |
| 1380 | for name in ['bash', 'ksh']: |
| 1381 | sh = os.path.join(prefix, name) |
| 1382 | if os.path.isfile(sh): |
| 1383 | shells.append(sh) |
| 1384 | if not shells: # Will probably work for any shell but csh. |
| 1385 | self.skipTest("bash or ksh required for this test") |
| 1386 | sh = '/bin/sh' |
| 1387 | if os.path.isfile(sh) and not os.path.islink(sh): |
| 1388 | # Test will fail if /bin/sh is a symlink to csh. |
| 1389 | shells.append(sh) |
| 1390 | for sh in shells: |
| 1391 | p = subprocess.Popen("echo $0", executable=sh, shell=True, |
| 1392 | stdout=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 1393 | self.addCleanup(p.stdout.close) |
Stefan Krah | 9542cc6 | 2010-07-19 14:20:53 +0000 | [diff] [blame] | 1394 | self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii')) |
| 1395 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1396 | def _kill_process(self, method, *args): |
Florent Xicluna | 1d8ee3a | 2010-03-05 20:26:54 +0000 | [diff] [blame] | 1397 | # Do not inherit file handles from the parent. |
| 1398 | # It should fix failures on some platforms. |
Antoine Pitrou | 3d8580f | 2010-09-20 01:33:21 +0000 | [diff] [blame] | 1399 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 1400 | import sys, time |
| 1401 | sys.stdout.write('x\\n') |
| 1402 | sys.stdout.flush() |
| 1403 | time.sleep(30) |
| 1404 | """], |
| 1405 | close_fds=True, |
| 1406 | stdin=subprocess.PIPE, |
| 1407 | stdout=subprocess.PIPE, |
| 1408 | stderr=subprocess.PIPE) |
| 1409 | # Wait for the interpreter to be completely initialized before |
| 1410 | # sending any signal. |
| 1411 | p.stdout.read(1) |
| 1412 | getattr(p, method)(*args) |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1413 | return p |
| 1414 | |
Charles-François Natali | 53221e3 | 2013-01-12 16:52:20 +0100 | [diff] [blame] | 1415 | @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')), |
| 1416 | "Due to known OS bug (issue #16762)") |
Antoine Pitrou | 1f9a835 | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 1417 | def _kill_dead_process(self, method, *args): |
| 1418 | # Do not inherit file handles from the parent. |
| 1419 | # It should fix failures on some platforms. |
| 1420 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 1421 | import sys, time |
| 1422 | sys.stdout.write('x\\n') |
| 1423 | sys.stdout.flush() |
| 1424 | """], |
| 1425 | close_fds=True, |
| 1426 | stdin=subprocess.PIPE, |
| 1427 | stdout=subprocess.PIPE, |
| 1428 | stderr=subprocess.PIPE) |
| 1429 | # Wait for the interpreter to be completely initialized before |
| 1430 | # sending any signal. |
| 1431 | p.stdout.read(1) |
| 1432 | # The process should end after this |
| 1433 | time.sleep(1) |
| 1434 | # This shouldn't raise even though the child is now dead |
| 1435 | getattr(p, method)(*args) |
| 1436 | p.communicate() |
| 1437 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1438 | def test_send_signal(self): |
| 1439 | p = self._kill_process('send_signal', signal.SIGINT) |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 1440 | _, stderr = p.communicate() |
| 1441 | self.assertIn(b'KeyboardInterrupt', stderr) |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 1442 | self.assertNotEqual(p.wait(), 0) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1443 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1444 | def test_kill(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1445 | p = self._kill_process('kill') |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 1446 | _, stderr = p.communicate() |
| 1447 | self.assertStderrEqual(stderr, b'') |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1448 | self.assertEqual(p.wait(), -signal.SIGKILL) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1449 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1450 | def test_terminate(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1451 | p = self._kill_process('terminate') |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 1452 | _, stderr = p.communicate() |
| 1453 | self.assertStderrEqual(stderr, b'') |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1454 | self.assertEqual(p.wait(), -signal.SIGTERM) |
| 1455 | |
Antoine Pitrou | 1f9a835 | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 1456 | def test_send_signal_dead(self): |
| 1457 | # Sending a signal to a dead process |
| 1458 | self._kill_dead_process('send_signal', signal.SIGINT) |
| 1459 | |
| 1460 | def test_kill_dead(self): |
| 1461 | # Killing a dead process |
| 1462 | self._kill_dead_process('kill') |
| 1463 | |
| 1464 | def test_terminate_dead(self): |
| 1465 | # Terminating a dead process |
| 1466 | self._kill_dead_process('terminate') |
| 1467 | |
Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 1468 | def check_close_std_fds(self, fds): |
| 1469 | # Issue #9905: test that subprocess pipes still work properly with |
| 1470 | # some standard fds closed |
| 1471 | stdin = 0 |
| 1472 | newfds = [] |
| 1473 | for a in fds: |
| 1474 | b = os.dup(a) |
| 1475 | newfds.append(b) |
| 1476 | if a == 0: |
| 1477 | stdin = b |
| 1478 | try: |
| 1479 | for fd in fds: |
| 1480 | os.close(fd) |
| 1481 | out, err = subprocess.Popen([sys.executable, "-c", |
| 1482 | 'import sys;' |
| 1483 | 'sys.stdout.write("apple");' |
| 1484 | 'sys.stdout.flush();' |
| 1485 | 'sys.stderr.write("orange")'], |
| 1486 | stdin=stdin, |
| 1487 | stdout=subprocess.PIPE, |
| 1488 | stderr=subprocess.PIPE).communicate() |
| 1489 | err = support.strip_python_stderr(err) |
| 1490 | self.assertEqual((out, err), (b'apple', b'orange')) |
| 1491 | finally: |
| 1492 | for b, a in zip(newfds, fds): |
| 1493 | os.dup2(b, a) |
| 1494 | for b in newfds: |
| 1495 | os.close(b) |
| 1496 | |
| 1497 | def test_close_fd_0(self): |
| 1498 | self.check_close_std_fds([0]) |
| 1499 | |
| 1500 | def test_close_fd_1(self): |
| 1501 | self.check_close_std_fds([1]) |
| 1502 | |
| 1503 | def test_close_fd_2(self): |
| 1504 | self.check_close_std_fds([2]) |
| 1505 | |
| 1506 | def test_close_fds_0_1(self): |
| 1507 | self.check_close_std_fds([0, 1]) |
| 1508 | |
| 1509 | def test_close_fds_0_2(self): |
| 1510 | self.check_close_std_fds([0, 2]) |
| 1511 | |
| 1512 | def test_close_fds_1_2(self): |
| 1513 | self.check_close_std_fds([1, 2]) |
| 1514 | |
| 1515 | def test_close_fds_0_1_2(self): |
| 1516 | # Issue #10806: test that subprocess pipes still work properly with |
| 1517 | # all standard fds closed. |
| 1518 | self.check_close_std_fds([0, 1, 2]) |
| 1519 | |
Antoine Pitrou | 95aaeee | 2011-01-03 21:15:48 +0000 | [diff] [blame] | 1520 | def test_remapping_std_fds(self): |
| 1521 | # open up some temporary files |
| 1522 | temps = [mkstemp() for i in range(3)] |
| 1523 | try: |
| 1524 | temp_fds = [fd for fd, fname in temps] |
| 1525 | |
| 1526 | # unlink the files -- we won't need to reopen them |
| 1527 | for fd, fname in temps: |
| 1528 | os.unlink(fname) |
| 1529 | |
| 1530 | # write some data to what will become stdin, and rewind |
| 1531 | os.write(temp_fds[1], b"STDIN") |
| 1532 | os.lseek(temp_fds[1], 0, 0) |
| 1533 | |
| 1534 | # move the standard file descriptors out of the way |
| 1535 | saved_fds = [os.dup(fd) for fd in range(3)] |
| 1536 | try: |
| 1537 | # duplicate the file objects over the standard fd's |
| 1538 | for fd, temp_fd in enumerate(temp_fds): |
| 1539 | os.dup2(temp_fd, fd) |
| 1540 | |
| 1541 | # now use those files in the "wrong" order, so that subprocess |
| 1542 | # has to rearrange them in the child |
| 1543 | p = subprocess.Popen([sys.executable, "-c", |
| 1544 | 'import sys; got = sys.stdin.read();' |
| 1545 | 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'], |
| 1546 | stdin=temp_fds[1], |
| 1547 | stdout=temp_fds[2], |
| 1548 | stderr=temp_fds[0]) |
| 1549 | p.wait() |
| 1550 | finally: |
| 1551 | # restore the original fd's underneath sys.stdin, etc. |
| 1552 | for std, saved in enumerate(saved_fds): |
| 1553 | os.dup2(saved, std) |
| 1554 | os.close(saved) |
| 1555 | |
| 1556 | for fd in temp_fds: |
| 1557 | os.lseek(fd, 0, 0) |
| 1558 | |
| 1559 | out = os.read(temp_fds[2], 1024) |
| 1560 | err = support.strip_python_stderr(os.read(temp_fds[0], 1024)) |
| 1561 | self.assertEqual(out, b"got STDIN") |
| 1562 | self.assertEqual(err, b"err") |
| 1563 | |
| 1564 | finally: |
| 1565 | for fd in temp_fds: |
| 1566 | os.close(fd) |
| 1567 | |
Ross Lagerwall | d98646e | 2011-07-27 07:16:31 +0200 | [diff] [blame] | 1568 | def check_swap_fds(self, stdin_no, stdout_no, stderr_no): |
| 1569 | # open up some temporary files |
| 1570 | temps = [mkstemp() for i in range(3)] |
| 1571 | temp_fds = [fd for fd, fname in temps] |
| 1572 | try: |
| 1573 | # unlink the files -- we won't need to reopen them |
| 1574 | for fd, fname in temps: |
| 1575 | os.unlink(fname) |
| 1576 | |
| 1577 | # save a copy of the standard file descriptors |
| 1578 | saved_fds = [os.dup(fd) for fd in range(3)] |
| 1579 | try: |
| 1580 | # duplicate the temp files over the standard fd's 0, 1, 2 |
| 1581 | for fd, temp_fd in enumerate(temp_fds): |
| 1582 | os.dup2(temp_fd, fd) |
| 1583 | |
| 1584 | # write some data to what will become stdin, and rewind |
| 1585 | os.write(stdin_no, b"STDIN") |
| 1586 | os.lseek(stdin_no, 0, 0) |
| 1587 | |
| 1588 | # now use those files in the given order, so that subprocess |
| 1589 | # has to rearrange them in the child |
| 1590 | p = subprocess.Popen([sys.executable, "-c", |
| 1591 | 'import sys; got = sys.stdin.read();' |
| 1592 | 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'], |
| 1593 | stdin=stdin_no, |
| 1594 | stdout=stdout_no, |
| 1595 | stderr=stderr_no) |
| 1596 | p.wait() |
| 1597 | |
| 1598 | for fd in temp_fds: |
| 1599 | os.lseek(fd, 0, 0) |
| 1600 | |
| 1601 | out = os.read(stdout_no, 1024) |
| 1602 | err = support.strip_python_stderr(os.read(stderr_no, 1024)) |
| 1603 | finally: |
| 1604 | for std, saved in enumerate(saved_fds): |
| 1605 | os.dup2(saved, std) |
| 1606 | os.close(saved) |
| 1607 | |
| 1608 | self.assertEqual(out, b"got STDIN") |
| 1609 | self.assertEqual(err, b"err") |
| 1610 | |
| 1611 | finally: |
| 1612 | for fd in temp_fds: |
| 1613 | os.close(fd) |
| 1614 | |
| 1615 | # When duping fds, if there arises a situation where one of the fds is |
| 1616 | # either 0, 1 or 2, it is possible that it is overwritten (#12607). |
| 1617 | # This tests all combinations of this. |
| 1618 | def test_swap_fds(self): |
| 1619 | self.check_swap_fds(0, 1, 2) |
| 1620 | self.check_swap_fds(0, 2, 1) |
| 1621 | self.check_swap_fds(1, 0, 2) |
| 1622 | self.check_swap_fds(1, 2, 0) |
| 1623 | self.check_swap_fds(2, 0, 1) |
| 1624 | self.check_swap_fds(2, 1, 0) |
| 1625 | |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1626 | def test_surrogates_error_message(self): |
Victor Stinner | 4d07804 | 2010-04-23 19:28:32 +0000 | [diff] [blame] | 1627 | def prepare(): |
| 1628 | raise ValueError("surrogate:\uDCff") |
| 1629 | |
| 1630 | try: |
| 1631 | subprocess.call( |
| 1632 | [sys.executable, "-c", "pass"], |
| 1633 | preexec_fn=prepare) |
| 1634 | except ValueError as err: |
| 1635 | # Pure Python implementations keeps the message |
| 1636 | self.assertIsNone(subprocess._posixsubprocess) |
| 1637 | self.assertEqual(str(err), "surrogate:\uDCff") |
Gregory P. Smith | 8d07c26 | 2012-11-10 23:53:47 -0800 | [diff] [blame] | 1638 | except subprocess.SubprocessError as err: |
Victor Stinner | 4d07804 | 2010-04-23 19:28:32 +0000 | [diff] [blame] | 1639 | # _posixsubprocess uses a default message |
| 1640 | self.assertIsNotNone(subprocess._posixsubprocess) |
| 1641 | self.assertEqual(str(err), "Exception occurred in preexec_fn.") |
| 1642 | else: |
Gregory P. Smith | 8d07c26 | 2012-11-10 23:53:47 -0800 | [diff] [blame] | 1643 | self.fail("Expected ValueError or subprocess.SubprocessError") |
Victor Stinner | 4d07804 | 2010-04-23 19:28:32 +0000 | [diff] [blame] | 1644 | |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1645 | def test_undecodable_env(self): |
| 1646 | for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')): |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1647 | # test str with surrogates |
Antoine Pitrou | fb8db8f | 2010-09-19 22:46:05 +0000 | [diff] [blame] | 1648 | script = "import os; print(ascii(os.getenv(%s)))" % repr(key) |
Victor Stinner | ce2d24d | 2010-04-23 22:55:39 +0000 | [diff] [blame] | 1649 | env = os.environ.copy() |
| 1650 | env[key] = value |
Victor Stinner | 89f3ad1 | 2010-10-14 10:43:31 +0000 | [diff] [blame] | 1651 | # Use C locale to get ascii for the locale encoding to force |
| 1652 | # surrogate-escaping of \xFF in the child process; otherwise it can |
| 1653 | # be decoded as-is if the default locale is latin-1. |
Victor Stinner | ebc78d2 | 2010-10-14 10:38:17 +0000 | [diff] [blame] | 1654 | env['LC_ALL'] = 'C' |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1655 | stdout = subprocess.check_output( |
| 1656 | [sys.executable, "-c", script], |
Victor Stinner | ce2d24d | 2010-04-23 22:55:39 +0000 | [diff] [blame] | 1657 | env=env) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1658 | stdout = stdout.rstrip(b'\n\r') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1659 | self.assertEqual(stdout.decode('ascii'), ascii(value)) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1660 | |
| 1661 | # test bytes |
| 1662 | key = key.encode("ascii", "surrogateescape") |
| 1663 | value = value.encode("ascii", "surrogateescape") |
Antoine Pitrou | fb8db8f | 2010-09-19 22:46:05 +0000 | [diff] [blame] | 1664 | script = "import os; print(ascii(os.getenvb(%s)))" % repr(key) |
Victor Stinner | ce2d24d | 2010-04-23 22:55:39 +0000 | [diff] [blame] | 1665 | env = os.environ.copy() |
| 1666 | env[key] = value |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1667 | stdout = subprocess.check_output( |
| 1668 | [sys.executable, "-c", script], |
Victor Stinner | ce2d24d | 2010-04-23 22:55:39 +0000 | [diff] [blame] | 1669 | env=env) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1670 | stdout = stdout.rstrip(b'\n\r') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1671 | self.assertEqual(stdout.decode('ascii'), ascii(value)) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 1672 | |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1673 | def test_bytes_program(self): |
| 1674 | abs_program = os.fsencode(sys.executable) |
| 1675 | path, program = os.path.split(sys.executable) |
| 1676 | program = os.fsencode(program) |
| 1677 | |
| 1678 | # absolute bytes path |
| 1679 | exitcode = subprocess.call([abs_program, "-c", "pass"]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1680 | self.assertEqual(exitcode, 0) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1681 | |
Victor Stinner | 7b3b20a | 2011-03-03 12:54:05 +0000 | [diff] [blame] | 1682 | # absolute bytes path as a string |
| 1683 | cmd = b"'" + abs_program + b"' -c pass" |
| 1684 | exitcode = subprocess.call(cmd, shell=True) |
| 1685 | self.assertEqual(exitcode, 0) |
| 1686 | |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1687 | # bytes program, unicode PATH |
| 1688 | env = os.environ.copy() |
| 1689 | env["PATH"] = path |
| 1690 | exitcode = subprocess.call([program, "-c", "pass"], env=env) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1691 | self.assertEqual(exitcode, 0) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1692 | |
| 1693 | # bytes program, bytes PATH |
| 1694 | envb = os.environb.copy() |
| 1695 | envb[b"PATH"] = os.fsencode(path) |
| 1696 | exitcode = subprocess.call([program, "-c", "pass"], env=envb) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1697 | self.assertEqual(exitcode, 0) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1698 | |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 1699 | def test_pipe_cloexec(self): |
| 1700 | sleeper = support.findfile("input_reader.py", subdir="subprocessdata") |
| 1701 | fd_status = support.findfile("fd_status.py", subdir="subprocessdata") |
| 1702 | |
| 1703 | p1 = subprocess.Popen([sys.executable, sleeper], |
| 1704 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 1705 | stderr=subprocess.PIPE, close_fds=False) |
| 1706 | |
| 1707 | self.addCleanup(p1.communicate, b'') |
| 1708 | |
| 1709 | p2 = subprocess.Popen([sys.executable, fd_status], |
| 1710 | stdout=subprocess.PIPE, close_fds=False) |
| 1711 | |
| 1712 | output, error = p2.communicate() |
| 1713 | result_fds = set(map(int, output.split(b','))) |
| 1714 | unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(), |
| 1715 | p1.stderr.fileno()]) |
| 1716 | |
| 1717 | self.assertFalse(result_fds & unwanted_fds, |
| 1718 | "Expected no fds from %r to be open in child, " |
| 1719 | "found %r" % |
| 1720 | (unwanted_fds, result_fds & unwanted_fds)) |
| 1721 | |
| 1722 | def test_pipe_cloexec_real_tools(self): |
| 1723 | qcat = support.findfile("qcat.py", subdir="subprocessdata") |
| 1724 | qgrep = support.findfile("qgrep.py", subdir="subprocessdata") |
| 1725 | |
| 1726 | subdata = b'zxcvbn' |
| 1727 | data = subdata * 4 + b'\n' |
| 1728 | |
| 1729 | p1 = subprocess.Popen([sys.executable, qcat], |
| 1730 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 1731 | close_fds=False) |
| 1732 | |
| 1733 | p2 = subprocess.Popen([sys.executable, qgrep, subdata], |
| 1734 | stdin=p1.stdout, stdout=subprocess.PIPE, |
| 1735 | close_fds=False) |
| 1736 | |
| 1737 | self.addCleanup(p1.wait) |
| 1738 | self.addCleanup(p2.wait) |
Gregory P. Smith | 886455c | 2012-01-21 22:05:10 -0800 | [diff] [blame] | 1739 | def kill_p1(): |
| 1740 | try: |
| 1741 | p1.terminate() |
| 1742 | except ProcessLookupError: |
| 1743 | pass |
| 1744 | def kill_p2(): |
| 1745 | try: |
| 1746 | p2.terminate() |
| 1747 | except ProcessLookupError: |
| 1748 | pass |
| 1749 | self.addCleanup(kill_p1) |
| 1750 | self.addCleanup(kill_p2) |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 1751 | |
| 1752 | p1.stdin.write(data) |
| 1753 | p1.stdin.close() |
| 1754 | |
| 1755 | readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10) |
| 1756 | |
| 1757 | self.assertTrue(readfiles, "The child hung") |
| 1758 | self.assertEqual(p2.stdout.read(), data) |
| 1759 | |
Victor Stinner | faa8c13 | 2011-01-03 16:36:00 +0000 | [diff] [blame] | 1760 | p1.stdout.close() |
| 1761 | p2.stdout.close() |
| 1762 | |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 1763 | def test_close_fds(self): |
| 1764 | fd_status = support.findfile("fd_status.py", subdir="subprocessdata") |
| 1765 | |
| 1766 | fds = os.pipe() |
| 1767 | self.addCleanup(os.close, fds[0]) |
| 1768 | self.addCleanup(os.close, fds[1]) |
| 1769 | |
| 1770 | open_fds = set(fds) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 1771 | # add a bunch more fds |
| 1772 | for _ in range(9): |
| 1773 | fd = os.open("/dev/null", os.O_RDONLY) |
| 1774 | self.addCleanup(os.close, fd) |
| 1775 | open_fds.add(fd) |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 1776 | |
| 1777 | p = subprocess.Popen([sys.executable, fd_status], |
| 1778 | stdout=subprocess.PIPE, close_fds=False) |
| 1779 | output, ignored = p.communicate() |
| 1780 | remaining_fds = set(map(int, output.split(b','))) |
| 1781 | |
| 1782 | self.assertEqual(remaining_fds & open_fds, open_fds, |
| 1783 | "Some fds were closed") |
| 1784 | |
| 1785 | p = subprocess.Popen([sys.executable, fd_status], |
| 1786 | stdout=subprocess.PIPE, close_fds=True) |
| 1787 | output, ignored = p.communicate() |
| 1788 | remaining_fds = set(map(int, output.split(b','))) |
| 1789 | |
| 1790 | self.assertFalse(remaining_fds & open_fds, |
| 1791 | "Some fds were left open") |
| 1792 | self.assertIn(1, remaining_fds, "Subprocess failed") |
| 1793 | |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 1794 | # Keep some of the fd's we opened open in the subprocess. |
| 1795 | # This tests _posixsubprocess.c's proper handling of fds_to_keep. |
| 1796 | fds_to_keep = set(open_fds.pop() for _ in range(8)) |
| 1797 | p = subprocess.Popen([sys.executable, fd_status], |
| 1798 | stdout=subprocess.PIPE, close_fds=True, |
| 1799 | pass_fds=()) |
| 1800 | output, ignored = p.communicate() |
| 1801 | remaining_fds = set(map(int, output.split(b','))) |
| 1802 | |
| 1803 | self.assertFalse(remaining_fds & fds_to_keep & open_fds, |
| 1804 | "Some fds not in pass_fds were left open") |
| 1805 | self.assertIn(1, remaining_fds, "Subprocess failed") |
| 1806 | |
Victor Stinner | 88701e2 | 2011-06-01 13:13:04 +0200 | [diff] [blame] | 1807 | # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file |
| 1808 | # descriptor of a pipe closed in the parent process is valid in the |
| 1809 | # child process according to fstat(), but the mode of the file |
| 1810 | # descriptor is invalid, and read or write raise an error. |
| 1811 | @support.requires_mac_ver(10, 5) |
Gregory P. Smith | 8edd99d | 2010-12-14 13:43:30 +0000 | [diff] [blame] | 1812 | def test_pass_fds(self): |
| 1813 | fd_status = support.findfile("fd_status.py", subdir="subprocessdata") |
| 1814 | |
| 1815 | open_fds = set() |
| 1816 | |
| 1817 | for x in range(5): |
| 1818 | fds = os.pipe() |
| 1819 | self.addCleanup(os.close, fds[0]) |
| 1820 | self.addCleanup(os.close, fds[1]) |
| 1821 | open_fds.update(fds) |
| 1822 | |
| 1823 | for fd in open_fds: |
| 1824 | p = subprocess.Popen([sys.executable, fd_status], |
| 1825 | stdout=subprocess.PIPE, close_fds=True, |
| 1826 | pass_fds=(fd, )) |
| 1827 | output, ignored = p.communicate() |
| 1828 | |
| 1829 | remaining_fds = set(map(int, output.split(b','))) |
| 1830 | to_be_closed = open_fds - {fd} |
| 1831 | |
| 1832 | self.assertIn(fd, remaining_fds, "fd to be passed not passed") |
| 1833 | self.assertFalse(remaining_fds & to_be_closed, |
| 1834 | "fd to be closed passed") |
| 1835 | |
| 1836 | # pass_fds overrides close_fds with a warning. |
| 1837 | with self.assertWarns(RuntimeWarning) as context: |
| 1838 | self.assertFalse(subprocess.call( |
| 1839 | [sys.executable, "-c", "import sys; sys.exit(0)"], |
| 1840 | close_fds=False, pass_fds=(fd, ))) |
| 1841 | self.assertIn('overriding close_fds', str(context.warning)) |
| 1842 | |
Gregory P. Smith | 112bb3a | 2011-03-15 14:55:17 -0400 | [diff] [blame] | 1843 | def test_stdout_stdin_are_single_inout_fd(self): |
| 1844 | with io.open(os.devnull, "r+") as inout: |
| 1845 | p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], |
| 1846 | stdout=inout, stdin=inout) |
| 1847 | p.wait() |
| 1848 | |
| 1849 | def test_stdout_stderr_are_single_inout_fd(self): |
| 1850 | with io.open(os.devnull, "r+") as inout: |
| 1851 | p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], |
| 1852 | stdout=inout, stderr=inout) |
| 1853 | p.wait() |
| 1854 | |
| 1855 | def test_stderr_stdin_are_single_inout_fd(self): |
| 1856 | with io.open(os.devnull, "r+") as inout: |
| 1857 | p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], |
| 1858 | stderr=inout, stdin=inout) |
| 1859 | p.wait() |
| 1860 | |
Gregory P. Smith | e85db2b | 2010-12-14 14:38:00 +0000 | [diff] [blame] | 1861 | def test_wait_when_sigchild_ignored(self): |
| 1862 | # NOTE: sigchild_ignore.py may not be an effective test on all OSes. |
| 1863 | sigchild_ignore = support.findfile("sigchild_ignore.py", |
| 1864 | subdir="subprocessdata") |
| 1865 | p = subprocess.Popen([sys.executable, sigchild_ignore], |
| 1866 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 1867 | stdout, stderr = p.communicate() |
| 1868 | self.assertEqual(0, p.returncode, "sigchild_ignore.py exited" |
Gregory P. Smith | a80f4fb | 2010-12-14 15:23:02 +0000 | [diff] [blame] | 1869 | " non-zero with this error:\n%s" % |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 1870 | stderr.decode('utf-8')) |
Gregory P. Smith | e85db2b | 2010-12-14 14:38:00 +0000 | [diff] [blame] | 1871 | |
Antoine Pitrou | 7b98d02 | 2011-03-19 17:04:13 +0100 | [diff] [blame] | 1872 | def test_select_unbuffered(self): |
| 1873 | # Issue #11459: bufsize=0 should really set the pipes as |
| 1874 | # unbuffered (and therefore let select() work properly). |
| 1875 | select = support.import_module("select") |
| 1876 | p = subprocess.Popen([sys.executable, "-c", |
| 1877 | 'import sys;' |
| 1878 | 'sys.stdout.write("apple")'], |
| 1879 | stdout=subprocess.PIPE, |
| 1880 | bufsize=0) |
| 1881 | f = p.stdout |
Ross Lagerwall | 17ace7a | 2011-03-26 21:21:46 +0200 | [diff] [blame] | 1882 | self.addCleanup(f.close) |
Antoine Pitrou | 7b98d02 | 2011-03-19 17:04:13 +0100 | [diff] [blame] | 1883 | try: |
| 1884 | self.assertEqual(f.read(4), b"appl") |
| 1885 | self.assertIn(f, select.select([f], [], [], 0.0)[0]) |
| 1886 | finally: |
| 1887 | p.wait() |
| 1888 | |
Charles-François Natali | 134a8ba | 2011-08-18 18:49:39 +0200 | [diff] [blame] | 1889 | def test_zombie_fast_process_del(self): |
| 1890 | # Issue #12650: on Unix, if Popen.__del__() was called before the |
| 1891 | # process exited, it wouldn't be added to subprocess._active, and would |
| 1892 | # remain a zombie. |
| 1893 | # spawn a Popen, and delete its reference before it exits |
| 1894 | p = subprocess.Popen([sys.executable, "-c", |
| 1895 | 'import sys, time;' |
| 1896 | 'time.sleep(0.2)'], |
| 1897 | stdout=subprocess.PIPE, |
| 1898 | stderr=subprocess.PIPE) |
Nadeem Vawda | 0d7cda3 | 2011-08-19 05:12:01 +0200 | [diff] [blame] | 1899 | self.addCleanup(p.stdout.close) |
| 1900 | self.addCleanup(p.stderr.close) |
Charles-François Natali | 134a8ba | 2011-08-18 18:49:39 +0200 | [diff] [blame] | 1901 | ident = id(p) |
| 1902 | pid = p.pid |
| 1903 | del p |
| 1904 | # check that p is in the active processes list |
| 1905 | self.assertIn(ident, [id(o) for o in subprocess._active]) |
| 1906 | |
Charles-François Natali | 134a8ba | 2011-08-18 18:49:39 +0200 | [diff] [blame] | 1907 | def test_leak_fast_process_del_killed(self): |
| 1908 | # Issue #12650: on Unix, if Popen.__del__() was called before the |
| 1909 | # process exited, and the process got killed by a signal, it would never |
| 1910 | # be removed from subprocess._active, which triggered a FD and memory |
| 1911 | # leak. |
| 1912 | # spawn a Popen, delete its reference and kill it |
| 1913 | p = subprocess.Popen([sys.executable, "-c", |
| 1914 | 'import time;' |
| 1915 | 'time.sleep(3)'], |
| 1916 | stdout=subprocess.PIPE, |
| 1917 | stderr=subprocess.PIPE) |
Nadeem Vawda | 0d7cda3 | 2011-08-19 05:12:01 +0200 | [diff] [blame] | 1918 | self.addCleanup(p.stdout.close) |
| 1919 | self.addCleanup(p.stderr.close) |
Charles-François Natali | 134a8ba | 2011-08-18 18:49:39 +0200 | [diff] [blame] | 1920 | ident = id(p) |
| 1921 | pid = p.pid |
| 1922 | del p |
| 1923 | os.kill(pid, signal.SIGKILL) |
| 1924 | # check that p is in the active processes list |
| 1925 | self.assertIn(ident, [id(o) for o in subprocess._active]) |
| 1926 | |
| 1927 | # let some time for the process to exit, and create a new Popen: this |
| 1928 | # should trigger the wait() of p |
| 1929 | time.sleep(0.2) |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 1930 | with self.assertRaises(OSError) as c: |
Charles-François Natali | 134a8ba | 2011-08-18 18:49:39 +0200 | [diff] [blame] | 1931 | with subprocess.Popen(['nonexisting_i_hope'], |
| 1932 | stdout=subprocess.PIPE, |
| 1933 | stderr=subprocess.PIPE) as proc: |
| 1934 | pass |
| 1935 | # p should have been wait()ed on, and removed from the _active list |
| 1936 | self.assertRaises(OSError, os.waitpid, pid, 0) |
| 1937 | self.assertNotIn(ident, [id(o) for o in subprocess._active]) |
| 1938 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1939 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 1940 | @unittest.skipUnless(mswindows, "Windows specific tests") |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 1941 | class Win32ProcessTestCase(BaseTestCase): |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 1942 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1943 | def test_startupinfo(self): |
| 1944 | # startupinfo argument |
| 1945 | # We uses hardcoded constants, because we do not want to |
| 1946 | # depend on win32all. |
| 1947 | STARTF_USESHOWWINDOW = 1 |
| 1948 | SW_MAXIMIZE = 3 |
| 1949 | startupinfo = subprocess.STARTUPINFO() |
| 1950 | startupinfo.dwFlags = STARTF_USESHOWWINDOW |
| 1951 | startupinfo.wShowWindow = SW_MAXIMIZE |
| 1952 | # Since Python is a console process, it won't be affected |
| 1953 | # by wShowWindow, but the argument should be silently |
| 1954 | # ignored |
| 1955 | subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1956 | startupinfo=startupinfo) |
| 1957 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1958 | def test_creationflags(self): |
| 1959 | # creationflags argument |
| 1960 | CREATE_NEW_CONSOLE = 16 |
| 1961 | sys.stderr.write(" a DOS box should flash briefly ...\n") |
| 1962 | subprocess.call(sys.executable + |
| 1963 | ' -c "import time; time.sleep(0.25)"', |
| 1964 | creationflags=CREATE_NEW_CONSOLE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1965 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1966 | def test_invalid_args(self): |
| 1967 | # invalid arguments should raise ValueError |
| 1968 | self.assertRaises(ValueError, subprocess.call, |
| 1969 | [sys.executable, "-c", |
| 1970 | "import sys; sys.exit(47)"], |
| 1971 | preexec_fn=lambda: 1) |
| 1972 | self.assertRaises(ValueError, subprocess.call, |
| 1973 | [sys.executable, "-c", |
| 1974 | "import sys; sys.exit(47)"], |
| 1975 | stdout=subprocess.PIPE, |
| 1976 | close_fds=True) |
| 1977 | |
| 1978 | def test_close_fds(self): |
| 1979 | # close file descriptors |
| 1980 | rc = subprocess.call([sys.executable, "-c", |
| 1981 | "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1982 | close_fds=True) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1983 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1984 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1985 | def test_shell_sequence(self): |
| 1986 | # Run command through the shell (sequence) |
| 1987 | newenv = os.environ.copy() |
| 1988 | newenv["FRUIT"] = "physalis" |
| 1989 | p = subprocess.Popen(["set"], shell=1, |
| 1990 | stdout=subprocess.PIPE, |
| 1991 | env=newenv) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 1992 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1993 | self.assertIn(b"physalis", p.stdout.read()) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1994 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1995 | def test_shell_string(self): |
| 1996 | # Run command through the shell (string) |
| 1997 | newenv = os.environ.copy() |
| 1998 | newenv["FRUIT"] = "physalis" |
| 1999 | p = subprocess.Popen("set", shell=1, |
| 2000 | stdout=subprocess.PIPE, |
| 2001 | env=newenv) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 2002 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 2003 | self.assertIn(b"physalis", p.stdout.read()) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 2004 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 2005 | def test_call_string(self): |
| 2006 | # call() function with string argument on Windows |
| 2007 | rc = subprocess.call(sys.executable + |
| 2008 | ' -c "import sys; sys.exit(47)"') |
| 2009 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 2010 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 2011 | def _kill_process(self, method, *args): |
| 2012 | # Some win32 buildbot raises EOFError if stdin is inherited |
Antoine Pitrou | a4024e2 | 2010-09-24 18:57:01 +0000 | [diff] [blame] | 2013 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 2014 | import sys, time |
| 2015 | sys.stdout.write('x\\n') |
| 2016 | sys.stdout.flush() |
| 2017 | time.sleep(30) |
| 2018 | """], |
| 2019 | stdin=subprocess.PIPE, |
| 2020 | stdout=subprocess.PIPE, |
| 2021 | stderr=subprocess.PIPE) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 2022 | self.addCleanup(p.stdout.close) |
| 2023 | self.addCleanup(p.stderr.close) |
| 2024 | self.addCleanup(p.stdin.close) |
Antoine Pitrou | a4024e2 | 2010-09-24 18:57:01 +0000 | [diff] [blame] | 2025 | # Wait for the interpreter to be completely initialized before |
| 2026 | # sending any signal. |
| 2027 | p.stdout.read(1) |
| 2028 | getattr(p, method)(*args) |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 2029 | _, stderr = p.communicate() |
| 2030 | self.assertStderrEqual(stderr, b'') |
Antoine Pitrou | a4024e2 | 2010-09-24 18:57:01 +0000 | [diff] [blame] | 2031 | returncode = p.wait() |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 2032 | self.assertNotEqual(returncode, 0) |
| 2033 | |
Antoine Pitrou | 1f9a835 | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 2034 | def _kill_dead_process(self, method, *args): |
| 2035 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 2036 | import sys, time |
| 2037 | sys.stdout.write('x\\n') |
| 2038 | sys.stdout.flush() |
| 2039 | sys.exit(42) |
| 2040 | """], |
| 2041 | stdin=subprocess.PIPE, |
| 2042 | stdout=subprocess.PIPE, |
| 2043 | stderr=subprocess.PIPE) |
| 2044 | self.addCleanup(p.stdout.close) |
| 2045 | self.addCleanup(p.stderr.close) |
| 2046 | self.addCleanup(p.stdin.close) |
| 2047 | # Wait for the interpreter to be completely initialized before |
| 2048 | # sending any signal. |
| 2049 | p.stdout.read(1) |
| 2050 | # The process should end after this |
| 2051 | time.sleep(1) |
| 2052 | # This shouldn't raise even though the child is now dead |
| 2053 | getattr(p, method)(*args) |
| 2054 | _, stderr = p.communicate() |
| 2055 | self.assertStderrEqual(stderr, b'') |
| 2056 | rc = p.wait() |
| 2057 | self.assertEqual(rc, 42) |
| 2058 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 2059 | def test_send_signal(self): |
| 2060 | self._kill_process('send_signal', signal.SIGTERM) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 2061 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 2062 | def test_kill(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 2063 | self._kill_process('kill') |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 2064 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 2065 | def test_terminate(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 2066 | self._kill_process('terminate') |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 2067 | |
Antoine Pitrou | 1f9a835 | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 2068 | def test_send_signal_dead(self): |
| 2069 | self._kill_dead_process('send_signal', signal.SIGTERM) |
| 2070 | |
| 2071 | def test_kill_dead(self): |
| 2072 | self._kill_dead_process('kill') |
| 2073 | |
| 2074 | def test_terminate_dead(self): |
| 2075 | self._kill_dead_process('terminate') |
| 2076 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 2077 | |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 2078 | # The module says: |
| 2079 | # "NB This only works (and is only relevant) for UNIX." |
| 2080 | # |
| 2081 | # Actually, getoutput should work on any platform with an os.popen, but |
| 2082 | # I'll take the comment as given, and skip this suite. |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 2083 | @unittest.skipUnless(os.name == 'posix', "only relevant for UNIX") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 2084 | class CommandTests(unittest.TestCase): |
| 2085 | def test_getoutput(self): |
| 2086 | self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy') |
| 2087 | self.assertEqual(subprocess.getstatusoutput('echo xyzzy'), |
| 2088 | (0, 'xyzzy')) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 2089 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 2090 | # we use mkdtemp in the next line to create an empty directory |
| 2091 | # under our exclusive control; from that, we can invent a pathname |
| 2092 | # that we _know_ won't exist. This is guaranteed to fail. |
| 2093 | dir = None |
| 2094 | try: |
| 2095 | dir = tempfile.mkdtemp() |
| 2096 | name = os.path.join(dir, "foo") |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 2097 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 2098 | status, output = subprocess.getstatusoutput('cat ' + name) |
| 2099 | self.assertNotEqual(status, 0) |
| 2100 | finally: |
| 2101 | if dir is not None: |
| 2102 | os.rmdir(dir) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 2103 | |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 2104 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 2105 | @unittest.skipUnless(getattr(subprocess, '_has_poll', False), |
| 2106 | "poll system call not supported") |
| 2107 | class ProcessTestCaseNoPoll(ProcessTestCase): |
| 2108 | def setUp(self): |
| 2109 | subprocess._has_poll = False |
| 2110 | ProcessTestCase.setUp(self) |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 2111 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 2112 | def tearDown(self): |
| 2113 | subprocess._has_poll = True |
| 2114 | ProcessTestCase.tearDown(self) |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 2115 | |
| 2116 | |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +0000 | [diff] [blame] | 2117 | class HelperFunctionTests(unittest.TestCase): |
Gregory P. Smith | af6d3b8 | 2010-03-01 02:56:44 +0000 | [diff] [blame] | 2118 | @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows") |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +0000 | [diff] [blame] | 2119 | def test_eintr_retry_call(self): |
| 2120 | record_calls = [] |
| 2121 | def fake_os_func(*args): |
| 2122 | record_calls.append(args) |
| 2123 | if len(record_calls) == 2: |
| 2124 | raise OSError(errno.EINTR, "fake interrupted system call") |
| 2125 | return tuple(reversed(args)) |
| 2126 | |
| 2127 | self.assertEqual((999, 256), |
| 2128 | subprocess._eintr_retry_call(fake_os_func, 256, 999)) |
| 2129 | self.assertEqual([(256, 999)], record_calls) |
| 2130 | # This time there will be an EINTR so it will loop once. |
| 2131 | self.assertEqual((666,), |
| 2132 | subprocess._eintr_retry_call(fake_os_func, 666)) |
| 2133 | self.assertEqual([(256, 999), (666,), (666,)], record_calls) |
| 2134 | |
| 2135 | |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 2136 | @unittest.skipUnless(mswindows, "Windows-specific tests") |
| 2137 | class CommandsWithSpaces (BaseTestCase): |
| 2138 | |
| 2139 | def setUp(self): |
| 2140 | super().setUp() |
| 2141 | f, fname = mkstemp(".py", "te st") |
| 2142 | self.fname = fname.lower () |
| 2143 | os.write(f, b"import sys;" |
| 2144 | b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))" |
| 2145 | ) |
| 2146 | os.close(f) |
| 2147 | |
| 2148 | def tearDown(self): |
| 2149 | os.remove(self.fname) |
| 2150 | super().tearDown() |
| 2151 | |
| 2152 | def with_spaces(self, *args, **kwargs): |
| 2153 | kwargs['stdout'] = subprocess.PIPE |
| 2154 | p = subprocess.Popen(*args, **kwargs) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 2155 | self.addCleanup(p.stdout.close) |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 2156 | self.assertEqual( |
| 2157 | p.stdout.read ().decode("mbcs"), |
| 2158 | "2 [%r, 'ab cd']" % self.fname |
| 2159 | ) |
| 2160 | |
| 2161 | def test_shell_string_with_spaces(self): |
| 2162 | # call() function with string argument with spaces on Windows |
Brian Curtin | d835cf1 | 2010-08-13 20:42:57 +0000 | [diff] [blame] | 2163 | self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, |
| 2164 | "ab cd"), shell=1) |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 2165 | |
| 2166 | def test_shell_sequence_with_spaces(self): |
| 2167 | # call() function with sequence argument with spaces on Windows |
Brian Curtin | d835cf1 | 2010-08-13 20:42:57 +0000 | [diff] [blame] | 2168 | self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1) |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 2169 | |
| 2170 | def test_noshell_string_with_spaces(self): |
| 2171 | # call() function with string argument with spaces on Windows |
| 2172 | self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, |
| 2173 | "ab cd")) |
| 2174 | |
| 2175 | def test_noshell_sequence_with_spaces(self): |
| 2176 | # call() function with sequence argument with spaces on Windows |
| 2177 | self.with_spaces([sys.executable, self.fname, "ab cd"]) |
| 2178 | |
Brian Curtin | 79cdb66 | 2010-12-03 02:46:02 +0000 | [diff] [blame] | 2179 | |
Georg Brandl | a86b262 | 2012-02-20 21:34:57 +0100 | [diff] [blame] | 2180 | class ContextManagerTests(BaseTestCase): |
Brian Curtin | 79cdb66 | 2010-12-03 02:46:02 +0000 | [diff] [blame] | 2181 | |
| 2182 | def test_pipe(self): |
| 2183 | with subprocess.Popen([sys.executable, "-c", |
| 2184 | "import sys;" |
| 2185 | "sys.stdout.write('stdout');" |
| 2186 | "sys.stderr.write('stderr');"], |
| 2187 | stdout=subprocess.PIPE, |
| 2188 | stderr=subprocess.PIPE) as proc: |
| 2189 | self.assertEqual(proc.stdout.read(), b"stdout") |
| 2190 | self.assertStderrEqual(proc.stderr.read(), b"stderr") |
| 2191 | |
| 2192 | self.assertTrue(proc.stdout.closed) |
| 2193 | self.assertTrue(proc.stderr.closed) |
| 2194 | |
| 2195 | def test_returncode(self): |
| 2196 | with subprocess.Popen([sys.executable, "-c", |
| 2197 | "import sys; sys.exit(100)"]) as proc: |
Gregory P. Smith | 6b65745 | 2011-05-11 21:42:08 -0700 | [diff] [blame] | 2198 | pass |
| 2199 | # __exit__ calls wait(), so the returncode should be set |
Brian Curtin | 79cdb66 | 2010-12-03 02:46:02 +0000 | [diff] [blame] | 2200 | self.assertEqual(proc.returncode, 100) |
| 2201 | |
| 2202 | def test_communicate_stdin(self): |
| 2203 | with subprocess.Popen([sys.executable, "-c", |
| 2204 | "import sys;" |
| 2205 | "sys.exit(sys.stdin.read() == 'context')"], |
| 2206 | stdin=subprocess.PIPE) as proc: |
| 2207 | proc.communicate(b"context") |
| 2208 | self.assertEqual(proc.returncode, 1) |
| 2209 | |
| 2210 | def test_invalid_args(self): |
Andrew Svetlov | b172697 | 2012-12-26 23:34:54 +0200 | [diff] [blame] | 2211 | with self.assertRaises(FileNotFoundError) as c: |
Brian Curtin | 79cdb66 | 2010-12-03 02:46:02 +0000 | [diff] [blame] | 2212 | with subprocess.Popen(['nonexisting_i_hope'], |
| 2213 | stdout=subprocess.PIPE, |
| 2214 | stderr=subprocess.PIPE) as proc: |
| 2215 | pass |
| 2216 | |
Brian Curtin | 79cdb66 | 2010-12-03 02:46:02 +0000 | [diff] [blame] | 2217 | |
Gregory P. Smith | 3b4652e | 2011-03-15 15:43:39 -0400 | [diff] [blame] | 2218 | def test_main(): |
| 2219 | unit_tests = (ProcessTestCase, |
| 2220 | POSIXProcessTestCase, |
| 2221 | Win32ProcessTestCase, |
Gregory P. Smith | 3b4652e | 2011-03-15 15:43:39 -0400 | [diff] [blame] | 2222 | CommandTests, |
| 2223 | ProcessTestCaseNoPoll, |
| 2224 | HelperFunctionTests, |
| 2225 | CommandsWithSpaces, |
Antoine Pitrou | ab85ff3 | 2011-07-23 22:03:45 +0200 | [diff] [blame] | 2226 | ContextManagerTests, |
| 2227 | ) |
Gregory P. Smith | 3b4652e | 2011-03-15 15:43:39 -0400 | [diff] [blame] | 2228 | |
| 2229 | support.run_unittest(*unit_tests) |
| 2230 | support.reap_children() |
| 2231 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 2232 | if __name__ == "__main__": |
Gregory P. Smith | 112bb3a | 2011-03-15 14:55:17 -0400 | [diff] [blame] | 2233 | unittest.main() |