Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
| 3 | import subprocess |
| 4 | import sys |
| 5 | import signal |
| 6 | import os |
Gregory P. Smith | cce211f | 2010-03-01 00:05:08 +0000 | [diff] [blame] | 7 | import errno |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 8 | import tempfile |
| 9 | import time |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 10 | import re |
Ezio Melotti | 8f6a287 | 2010-02-10 21:40:33 +0000 | [diff] [blame] | 11 | import sysconfig |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 12 | |
Benjamin Peterson | 8b59c23 | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 13 | try: |
| 14 | import resource |
| 15 | except ImportError: |
| 16 | resource = None |
| 17 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 18 | mswindows = (sys.platform == "win32") |
| 19 | |
| 20 | # |
| 21 | # Depends on the following external programs: Python |
| 22 | # |
| 23 | |
| 24 | if mswindows: |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 25 | SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), ' |
| 26 | 'os.O_BINARY);') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 27 | else: |
| 28 | SETBINARY = '' |
| 29 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 30 | |
| 31 | try: |
| 32 | mkstemp = tempfile.mkstemp |
| 33 | except AttributeError: |
| 34 | # tempfile.mkstemp is not available |
| 35 | def mkstemp(): |
| 36 | """Replacement for mkstemp, calling mktemp.""" |
| 37 | fname = tempfile.mktemp() |
| 38 | return os.open(fname, os.O_RDWR|os.O_CREAT), fname |
| 39 | |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 40 | |
Florent Xicluna | fc4d6d7 | 2010-03-23 14:36:45 +0000 | [diff] [blame] | 41 | class BaseTestCase(unittest.TestCase): |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 42 | def setUp(self): |
Tim Peters | 38ff36c | 2006-06-30 06:18:39 +0000 | [diff] [blame] | 43 | # Try to minimize the number of children we have so this test |
| 44 | # doesn't crash on some buildbots (Alphas in particular). |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 45 | test_support.reap_children() |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 46 | |
Florent Xicluna | ab5e17f | 2010-03-04 21:31:58 +0000 | [diff] [blame] | 47 | def tearDown(self): |
| 48 | for inst in subprocess._active: |
| 49 | inst.wait() |
| 50 | subprocess._cleanup() |
| 51 | self.assertFalse(subprocess._active, "subprocess._active not empty") |
| 52 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 53 | def assertStderrEqual(self, stderr, expected, msg=None): |
| 54 | # In a debug build, stuff like "[6580 refs]" is printed to stderr at |
| 55 | # shutdown time. That frustrates tests trying to check stderr produced |
| 56 | # from a spawned Python process. |
| 57 | actual = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr) |
| 58 | self.assertEqual(actual, expected, msg) |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 59 | |
Florent Xicluna | fc4d6d7 | 2010-03-23 14:36:45 +0000 | [diff] [blame] | 60 | |
| 61 | class ProcessTestCase(BaseTestCase): |
| 62 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 63 | def test_call_seq(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 64 | # call() function with sequence argument |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 65 | rc = subprocess.call([sys.executable, "-c", |
| 66 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 67 | self.assertEqual(rc, 47) |
| 68 | |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 69 | def test_check_call_zero(self): |
| 70 | # check_call() function with zero return code |
| 71 | rc = subprocess.check_call([sys.executable, "-c", |
| 72 | "import sys; sys.exit(0)"]) |
| 73 | self.assertEqual(rc, 0) |
| 74 | |
| 75 | def test_check_call_nonzero(self): |
| 76 | # check_call() function with non-zero return code |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 77 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 78 | subprocess.check_call([sys.executable, "-c", |
| 79 | "import sys; sys.exit(47)"]) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 80 | self.assertEqual(c.exception.returncode, 47) |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 81 | |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 82 | def test_check_output(self): |
| 83 | # check_output() function with zero return code |
| 84 | output = subprocess.check_output( |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 85 | [sys.executable, "-c", "print 'BDFL'"]) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 86 | self.assertIn('BDFL', output) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 87 | |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 88 | def test_check_output_nonzero(self): |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 89 | # check_call() function with non-zero return code |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 90 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 91 | subprocess.check_output( |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 92 | [sys.executable, "-c", "import sys; sys.exit(5)"]) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 93 | self.assertEqual(c.exception.returncode, 5) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 94 | |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 95 | def test_check_output_stderr(self): |
| 96 | # check_output() function stderr redirected to stdout |
| 97 | output = subprocess.check_output( |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 98 | [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"], |
| 99 | stderr=subprocess.STDOUT) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 100 | self.assertIn('BDFL', output) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 101 | |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 102 | def test_check_output_stdout_arg(self): |
| 103 | # check_output() function stderr redirected to stdout |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 104 | with self.assertRaises(ValueError) as c: |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 105 | output = subprocess.check_output( |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 106 | [sys.executable, "-c", "print 'will not be run'"], |
| 107 | stdout=sys.stdout) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 108 | self.fail("Expected ValueError when stdout arg supplied.") |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 109 | self.assertIn('stdout', c.exception.args[0]) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 110 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 111 | def test_call_kwargs(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 112 | # call() function with keyword args |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 113 | newenv = os.environ.copy() |
| 114 | newenv["FRUIT"] = "banana" |
| 115 | rc = subprocess.call([sys.executable, "-c", |
Florent Xicluna | bab22a7 | 2010-03-04 19:40:48 +0000 | [diff] [blame] | 116 | 'import sys, os;' |
| 117 | 'sys.exit(os.getenv("FRUIT")=="banana")'], |
| 118 | env=newenv) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 119 | self.assertEqual(rc, 1) |
| 120 | |
Victor Stinner | 776e69b | 2011-06-01 01:03:00 +0200 | [diff] [blame] | 121 | def test_invalid_args(self): |
| 122 | # Popen() called with invalid arguments should raise TypeError |
| 123 | # but Popen.__del__ should not complain (issue #12085) |
Victor Stinner | e9b185f | 2011-06-01 01:57:48 +0200 | [diff] [blame] | 124 | with test_support.captured_stderr() as s: |
Victor Stinner | 776e69b | 2011-06-01 01:03:00 +0200 | [diff] [blame] | 125 | self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1) |
| 126 | argcount = subprocess.Popen.__init__.__code__.co_argcount |
| 127 | too_many_args = [0] * (argcount + 1) |
| 128 | self.assertRaises(TypeError, subprocess.Popen, *too_many_args) |
| 129 | self.assertEqual(s.getvalue(), '') |
| 130 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 131 | def test_stdin_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 132 | # .stdin is None when not redirected |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 133 | p = subprocess.Popen([sys.executable, "-c", 'print "banana"'], |
| 134 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 135 | self.addCleanup(p.stdout.close) |
| 136 | self.addCleanup(p.stderr.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 137 | p.wait() |
| 138 | self.assertEqual(p.stdin, None) |
| 139 | |
| 140 | def test_stdout_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 141 | # .stdout is None when not redirected |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 142 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | 4052fe5 | 2004-10-13 03:29:54 +0000 | [diff] [blame] | 143 | 'print " this bit of output is from a ' |
| 144 | 'test of stdout in a different ' |
| 145 | 'process ..."'], |
| 146 | stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 147 | self.addCleanup(p.stdin.close) |
| 148 | self.addCleanup(p.stderr.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 149 | p.wait() |
| 150 | self.assertEqual(p.stdout, None) |
| 151 | |
| 152 | def test_stderr_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 153 | # .stderr is None when not redirected |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 154 | p = subprocess.Popen([sys.executable, "-c", 'print "banana"'], |
| 155 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 156 | self.addCleanup(p.stdout.close) |
| 157 | self.addCleanup(p.stdin.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 158 | p.wait() |
| 159 | self.assertEqual(p.stderr, None) |
| 160 | |
Ezio Melotti | 8f6a287 | 2010-02-10 21:40:33 +0000 | [diff] [blame] | 161 | def test_executable_with_cwd(self): |
Florent Xicluna | 6376370 | 2010-03-11 01:50:48 +0000 | [diff] [blame] | 162 | python_dir = os.path.dirname(os.path.realpath(sys.executable)) |
Ezio Melotti | 8f6a287 | 2010-02-10 21:40:33 +0000 | [diff] [blame] | 163 | p = subprocess.Popen(["somethingyoudonthave", "-c", |
| 164 | "import sys; sys.exit(47)"], |
| 165 | executable=sys.executable, cwd=python_dir) |
| 166 | p.wait() |
| 167 | self.assertEqual(p.returncode, 47) |
| 168 | |
| 169 | @unittest.skipIf(sysconfig.is_python_build(), |
| 170 | "need an installed Python. See #7774") |
| 171 | def test_executable_without_cwd(self): |
| 172 | # For a normal installation, it should work without 'cwd' |
| 173 | # argument. For test runs in the build directory, see #7774. |
| 174 | p = subprocess.Popen(["somethingyoudonthave", "-c", |
| 175 | "import sys; sys.exit(47)"], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 176 | executable=sys.executable) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 177 | p.wait() |
| 178 | self.assertEqual(p.returncode, 47) |
| 179 | |
| 180 | def test_stdin_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 181 | # stdin redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 182 | p = subprocess.Popen([sys.executable, "-c", |
| 183 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 184 | stdin=subprocess.PIPE) |
| 185 | p.stdin.write("pear") |
| 186 | p.stdin.close() |
| 187 | p.wait() |
| 188 | self.assertEqual(p.returncode, 1) |
| 189 | |
| 190 | def test_stdin_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 191 | # stdin is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 192 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 193 | d = tf.fileno() |
| 194 | os.write(d, "pear") |
| 195 | os.lseek(d, 0, 0) |
| 196 | p = subprocess.Popen([sys.executable, "-c", |
| 197 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 198 | stdin=d) |
| 199 | p.wait() |
| 200 | self.assertEqual(p.returncode, 1) |
| 201 | |
| 202 | def test_stdin_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 203 | # stdin is set to open file object |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 204 | tf = tempfile.TemporaryFile() |
| 205 | tf.write("pear") |
| 206 | tf.seek(0) |
| 207 | p = subprocess.Popen([sys.executable, "-c", |
| 208 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 209 | stdin=tf) |
| 210 | p.wait() |
| 211 | self.assertEqual(p.returncode, 1) |
| 212 | |
| 213 | def test_stdout_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 214 | # stdout redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 215 | p = subprocess.Popen([sys.executable, "-c", |
| 216 | 'import sys; sys.stdout.write("orange")'], |
| 217 | stdout=subprocess.PIPE) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 218 | self.addCleanup(p.stdout.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 219 | self.assertEqual(p.stdout.read(), "orange") |
| 220 | |
| 221 | def test_stdout_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 222 | # stdout is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 223 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 224 | d = tf.fileno() |
| 225 | p = subprocess.Popen([sys.executable, "-c", |
| 226 | 'import sys; sys.stdout.write("orange")'], |
| 227 | stdout=d) |
| 228 | p.wait() |
| 229 | os.lseek(d, 0, 0) |
| 230 | self.assertEqual(os.read(d, 1024), "orange") |
| 231 | |
| 232 | def test_stdout_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 233 | # stdout is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 234 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 235 | p = subprocess.Popen([sys.executable, "-c", |
| 236 | 'import sys; sys.stdout.write("orange")'], |
| 237 | stdout=tf) |
| 238 | p.wait() |
| 239 | tf.seek(0) |
| 240 | self.assertEqual(tf.read(), "orange") |
| 241 | |
| 242 | def test_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 243 | # stderr redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 244 | p = subprocess.Popen([sys.executable, "-c", |
| 245 | 'import sys; sys.stderr.write("strawberry")'], |
| 246 | stderr=subprocess.PIPE) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 247 | self.addCleanup(p.stderr.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 248 | self.assertStderrEqual(p.stderr.read(), "strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 249 | |
| 250 | def test_stderr_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 251 | # stderr is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 252 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 253 | d = tf.fileno() |
| 254 | p = subprocess.Popen([sys.executable, "-c", |
| 255 | 'import sys; sys.stderr.write("strawberry")'], |
| 256 | stderr=d) |
| 257 | p.wait() |
| 258 | os.lseek(d, 0, 0) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 259 | self.assertStderrEqual(os.read(d, 1024), "strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 260 | |
| 261 | def test_stderr_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 262 | # stderr is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 263 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 264 | p = subprocess.Popen([sys.executable, "-c", |
| 265 | 'import sys; sys.stderr.write("strawberry")'], |
| 266 | stderr=tf) |
| 267 | p.wait() |
| 268 | tf.seek(0) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 269 | self.assertStderrEqual(tf.read(), "strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 270 | |
| 271 | def test_stdout_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 272 | # capture stdout and stderr to the same pipe |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 273 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 274 | 'import sys;' |
| 275 | 'sys.stdout.write("apple");' |
| 276 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 277 | 'sys.stderr.write("orange")'], |
| 278 | stdout=subprocess.PIPE, |
| 279 | stderr=subprocess.STDOUT) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 280 | self.addCleanup(p.stdout.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 281 | self.assertStderrEqual(p.stdout.read(), "appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 282 | |
| 283 | def test_stdout_stderr_file(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 284 | # capture stdout and stderr to the same open file |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 285 | tf = tempfile.TemporaryFile() |
| 286 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 287 | 'import sys;' |
| 288 | 'sys.stdout.write("apple");' |
| 289 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 290 | 'sys.stderr.write("orange")'], |
| 291 | stdout=tf, |
| 292 | stderr=tf) |
| 293 | p.wait() |
| 294 | tf.seek(0) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 295 | self.assertStderrEqual(tf.read(), "appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 296 | |
Gustavo Niemeyer | c36bede | 2006-09-07 00:48:33 +0000 | [diff] [blame] | 297 | def test_stdout_filedes_of_stdout(self): |
| 298 | # stdout is set to 1 (#1531862). |
| 299 | cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))" |
| 300 | rc = subprocess.call([sys.executable, "-c", cmd], stdout=1) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 301 | self.assertEqual(rc, 2) |
Gustavo Niemeyer | c36bede | 2006-09-07 00:48:33 +0000 | [diff] [blame] | 302 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 303 | def test_cwd(self): |
Guido van Rossum | e9a0e88 | 2007-12-20 17:28:10 +0000 | [diff] [blame] | 304 | tmpdir = tempfile.gettempdir() |
Peter Astrand | 195404f | 2004-11-12 15:51:48 +0000 | [diff] [blame] | 305 | # We cannot use os.path.realpath to canonicalize the path, |
| 306 | # since it doesn't expand Tru64 {memb} strings. See bug 1063571. |
| 307 | cwd = os.getcwd() |
| 308 | os.chdir(tmpdir) |
| 309 | tmpdir = os.getcwd() |
| 310 | os.chdir(cwd) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 311 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 312 | 'import sys,os;' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 313 | 'sys.stdout.write(os.getcwd())'], |
| 314 | stdout=subprocess.PIPE, |
| 315 | cwd=tmpdir) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 316 | self.addCleanup(p.stdout.close) |
Fredrik Lundh | 59c0559 | 2004-10-13 06:55:40 +0000 | [diff] [blame] | 317 | normcase = os.path.normcase |
| 318 | self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir)) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 319 | |
| 320 | def test_env(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 321 | newenv = os.environ.copy() |
| 322 | newenv["FRUIT"] = "orange" |
| 323 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 324 | 'import sys,os;' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 325 | 'sys.stdout.write(os.getenv("FRUIT"))'], |
| 326 | stdout=subprocess.PIPE, |
| 327 | env=newenv) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 328 | self.addCleanup(p.stdout.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 329 | self.assertEqual(p.stdout.read(), "orange") |
| 330 | |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 331 | def test_communicate_stdin(self): |
| 332 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 333 | 'import sys;' |
| 334 | 'sys.exit(sys.stdin.read() == "pear")'], |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 335 | stdin=subprocess.PIPE) |
| 336 | p.communicate("pear") |
| 337 | self.assertEqual(p.returncode, 1) |
| 338 | |
| 339 | def test_communicate_stdout(self): |
| 340 | p = subprocess.Popen([sys.executable, "-c", |
| 341 | 'import sys; sys.stdout.write("pineapple")'], |
| 342 | stdout=subprocess.PIPE) |
| 343 | (stdout, stderr) = p.communicate() |
| 344 | self.assertEqual(stdout, "pineapple") |
| 345 | self.assertEqual(stderr, None) |
| 346 | |
| 347 | def test_communicate_stderr(self): |
| 348 | p = subprocess.Popen([sys.executable, "-c", |
| 349 | 'import sys; sys.stderr.write("pineapple")'], |
| 350 | stderr=subprocess.PIPE) |
| 351 | (stdout, stderr) = p.communicate() |
| 352 | self.assertEqual(stdout, None) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 353 | self.assertStderrEqual(stderr, "pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 354 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 355 | def test_communicate(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 356 | p = subprocess.Popen([sys.executable, "-c", |
Gregory P. Smith | 4036fd4 | 2008-05-26 20:22:14 +0000 | [diff] [blame] | 357 | 'import sys,os;' |
| 358 | 'sys.stderr.write("pineapple");' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 359 | 'sys.stdout.write(sys.stdin.read())'], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 360 | stdin=subprocess.PIPE, |
| 361 | stdout=subprocess.PIPE, |
| 362 | stderr=subprocess.PIPE) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 363 | self.addCleanup(p.stdout.close) |
| 364 | self.addCleanup(p.stderr.close) |
| 365 | self.addCleanup(p.stdin.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 366 | (stdout, stderr) = p.communicate("banana") |
| 367 | self.assertEqual(stdout, "banana") |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 368 | self.assertStderrEqual(stderr, "pineapple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 369 | |
Gregory P. Smith | 4036fd4 | 2008-05-26 20:22:14 +0000 | [diff] [blame] | 370 | # This test is Linux specific for simplicity to at least have |
| 371 | # some coverage. It is not a platform specific bug. |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 372 | @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()), |
| 373 | "Linux specific") |
| 374 | # Test for the fd leak reported in http://bugs.python.org/issue2791. |
| 375 | def test_communicate_pipe_fd_leak(self): |
| 376 | fd_directory = '/proc/%d/fd' % os.getpid() |
| 377 | num_fds_before_popen = len(os.listdir(fd_directory)) |
| 378 | p = subprocess.Popen([sys.executable, "-c", "print()"], |
| 379 | stdout=subprocess.PIPE) |
| 380 | p.communicate() |
| 381 | num_fds_after_communicate = len(os.listdir(fd_directory)) |
| 382 | del p |
| 383 | num_fds_after_destruction = len(os.listdir(fd_directory)) |
| 384 | self.assertEqual(num_fds_before_popen, num_fds_after_destruction) |
| 385 | self.assertEqual(num_fds_before_popen, num_fds_after_communicate) |
Gregory P. Smith | 4036fd4 | 2008-05-26 20:22:14 +0000 | [diff] [blame] | 386 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 387 | def test_communicate_returns(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 388 | # communicate() should return None if no redirection is active |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 389 | p = subprocess.Popen([sys.executable, "-c", |
| 390 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 391 | (stdout, stderr) = p.communicate() |
| 392 | self.assertEqual(stdout, None) |
| 393 | self.assertEqual(stderr, None) |
| 394 | |
| 395 | def test_communicate_pipe_buf(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 396 | # communicate() with writes larger than pipe_buf |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 397 | # This test will probably deadlock rather than fail, if |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 398 | # communicate() does not work properly. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 399 | x, y = os.pipe() |
| 400 | if mswindows: |
| 401 | pipe_buf = 512 |
| 402 | else: |
| 403 | pipe_buf = os.fpathconf(x, "PC_PIPE_BUF") |
| 404 | os.close(x) |
| 405 | os.close(y) |
| 406 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 407 | 'import sys,os;' |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 408 | 'sys.stdout.write(sys.stdin.read(47));' |
| 409 | 'sys.stderr.write("xyz"*%d);' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 410 | 'sys.stdout.write(sys.stdin.read())' % pipe_buf], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 411 | stdin=subprocess.PIPE, |
| 412 | stdout=subprocess.PIPE, |
| 413 | stderr=subprocess.PIPE) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 414 | self.addCleanup(p.stdout.close) |
| 415 | self.addCleanup(p.stderr.close) |
| 416 | self.addCleanup(p.stdin.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 417 | string_to_write = "abc"*pipe_buf |
| 418 | (stdout, stderr) = p.communicate(string_to_write) |
| 419 | self.assertEqual(stdout, string_to_write) |
| 420 | |
| 421 | def test_writes_before_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 422 | # stdin.write before communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 423 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 424 | 'import sys,os;' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 425 | 'sys.stdout.write(sys.stdin.read())'], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 426 | stdin=subprocess.PIPE, |
| 427 | stdout=subprocess.PIPE, |
| 428 | stderr=subprocess.PIPE) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 429 | self.addCleanup(p.stdout.close) |
| 430 | self.addCleanup(p.stderr.close) |
| 431 | self.addCleanup(p.stdin.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 432 | p.stdin.write("banana") |
| 433 | (stdout, stderr) = p.communicate("split") |
| 434 | self.assertEqual(stdout, "bananasplit") |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 435 | self.assertStderrEqual(stderr, "") |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 436 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 437 | def test_universal_newlines(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 438 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 439 | 'import sys,os;' + SETBINARY + |
| 440 | 'sys.stdout.write("line1\\n");' |
| 441 | 'sys.stdout.flush();' |
| 442 | 'sys.stdout.write("line2\\r");' |
| 443 | 'sys.stdout.flush();' |
| 444 | 'sys.stdout.write("line3\\r\\n");' |
| 445 | 'sys.stdout.flush();' |
| 446 | 'sys.stdout.write("line4\\r");' |
| 447 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 448 | 'sys.stdout.write("\\nline5");' |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 449 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 450 | 'sys.stdout.write("\\nline6");'], |
| 451 | stdout=subprocess.PIPE, |
| 452 | universal_newlines=1) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 453 | self.addCleanup(p.stdout.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 454 | stdout = p.stdout.read() |
Neal Norwitz | a6d01ce | 2006-05-02 06:23:22 +0000 | [diff] [blame] | 455 | if hasattr(file, 'newlines'): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 456 | # Interpreter with universal newline support |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 457 | self.assertEqual(stdout, |
| 458 | "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 459 | else: |
| 460 | # Interpreter without universal newline support |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 461 | self.assertEqual(stdout, |
| 462 | "line1\nline2\rline3\r\nline4\r\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 463 | |
| 464 | def test_universal_newlines_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 465 | # universal newlines through communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 466 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 467 | 'import sys,os;' + SETBINARY + |
| 468 | 'sys.stdout.write("line1\\n");' |
| 469 | 'sys.stdout.flush();' |
| 470 | 'sys.stdout.write("line2\\r");' |
| 471 | 'sys.stdout.flush();' |
| 472 | 'sys.stdout.write("line3\\r\\n");' |
| 473 | 'sys.stdout.flush();' |
| 474 | 'sys.stdout.write("line4\\r");' |
| 475 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 476 | 'sys.stdout.write("\\nline5");' |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 477 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 478 | 'sys.stdout.write("\\nline6");'], |
| 479 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 480 | universal_newlines=1) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 481 | self.addCleanup(p.stdout.close) |
| 482 | self.addCleanup(p.stderr.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 483 | (stdout, stderr) = p.communicate() |
Neal Norwitz | a6d01ce | 2006-05-02 06:23:22 +0000 | [diff] [blame] | 484 | if hasattr(file, 'newlines'): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 485 | # Interpreter with universal newline support |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 486 | self.assertEqual(stdout, |
| 487 | "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 488 | else: |
| 489 | # Interpreter without universal newline support |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 490 | self.assertEqual(stdout, |
| 491 | "line1\nline2\rline3\r\nline4\r\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 492 | |
| 493 | def test_no_leaking(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 494 | # Make sure we leak no resources |
Antoine Pitrou | b0b3bff | 2010-09-18 22:42:30 +0000 | [diff] [blame] | 495 | if not mswindows: |
Peter Astrand | f7f1bb7 | 2005-03-03 20:47:37 +0000 | [diff] [blame] | 496 | max_handles = 1026 # too much for most UNIX systems |
| 497 | else: |
Antoine Pitrou | b0b3bff | 2010-09-18 22:42:30 +0000 | [diff] [blame] | 498 | max_handles = 2050 # too much for (at least some) Windows setups |
| 499 | handles = [] |
| 500 | try: |
| 501 | for i in range(max_handles): |
| 502 | try: |
| 503 | handles.append(os.open(test_support.TESTFN, |
| 504 | os.O_WRONLY | os.O_CREAT)) |
| 505 | except OSError as e: |
| 506 | if e.errno != errno.EMFILE: |
| 507 | raise |
| 508 | break |
| 509 | else: |
| 510 | self.skipTest("failed to reach the file descriptor limit " |
| 511 | "(tried %d)" % max_handles) |
| 512 | # Close a couple of them (should be enough for a subprocess) |
| 513 | for i in range(10): |
| 514 | os.close(handles.pop()) |
| 515 | # Loop creating some subprocesses. If one of them leaks some fds, |
| 516 | # the next loop iteration will fail by reaching the max fd limit. |
| 517 | for i in range(15): |
| 518 | p = subprocess.Popen([sys.executable, "-c", |
| 519 | "import sys;" |
| 520 | "sys.stdout.write(sys.stdin.read())"], |
| 521 | stdin=subprocess.PIPE, |
| 522 | stdout=subprocess.PIPE, |
| 523 | stderr=subprocess.PIPE) |
| 524 | data = p.communicate(b"lime")[0] |
| 525 | self.assertEqual(data, b"lime") |
| 526 | finally: |
| 527 | for h in handles: |
| 528 | os.close(h) |
Mark Dickinson | 313dc9b | 2012-10-07 15:41:38 +0100 | [diff] [blame^] | 529 | test_support.unlink(test_support.TESTFN) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 530 | |
| 531 | def test_list2cmdline(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 532 | self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), |
| 533 | '"a b c" d e') |
| 534 | self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), |
| 535 | 'ab\\"c \\ d') |
Gregory P. Smith | e047e6d | 2008-01-19 20:49:02 +0000 | [diff] [blame] | 536 | self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']), |
| 537 | 'ab\\"c " \\\\" d') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 538 | self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']), |
| 539 | 'a\\\\\\b "de fg" h') |
| 540 | self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']), |
| 541 | 'a\\\\\\"b c d') |
| 542 | self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']), |
| 543 | '"a\\\\b c" d e') |
| 544 | self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']), |
| 545 | '"a\\\\b\\ c" d e') |
Peter Astrand | 10514a7 | 2007-01-13 22:35:35 +0000 | [diff] [blame] | 546 | self.assertEqual(subprocess.list2cmdline(['ab', '']), |
| 547 | 'ab ""') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 548 | |
| 549 | |
| 550 | def test_poll(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 551 | p = subprocess.Popen([sys.executable, |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 552 | "-c", "import time; time.sleep(1)"]) |
| 553 | count = 0 |
| 554 | while p.poll() is None: |
| 555 | time.sleep(0.1) |
| 556 | count += 1 |
| 557 | # We expect that the poll loop probably went around about 10 times, |
| 558 | # but, based on system scheduling we can't control, it's possible |
| 559 | # poll() never returned None. It "should be" very rare that it |
| 560 | # didn't go around at least twice. |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 561 | self.assertGreaterEqual(count, 2) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 562 | # Subsequent invocations should just return the returncode |
| 563 | self.assertEqual(p.poll(), 0) |
| 564 | |
| 565 | |
| 566 | def test_wait(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 567 | p = subprocess.Popen([sys.executable, |
| 568 | "-c", "import time; time.sleep(2)"]) |
| 569 | self.assertEqual(p.wait(), 0) |
| 570 | # Subsequent invocations should just return the returncode |
| 571 | self.assertEqual(p.wait(), 0) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 572 | |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 573 | |
| 574 | def test_invalid_bufsize(self): |
| 575 | # an invalid type of the bufsize argument should raise |
| 576 | # TypeError. |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 577 | with self.assertRaises(TypeError): |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 578 | subprocess.Popen([sys.executable, "-c", "pass"], "orange") |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 579 | |
Georg Brandl | f3715d2 | 2009-02-14 17:01:36 +0000 | [diff] [blame] | 580 | def test_leaking_fds_on_error(self): |
| 581 | # see bug #5179: Popen leaks file descriptors to PIPEs if |
| 582 | # the child fails to execute; this will eventually exhaust |
| 583 | # the maximum number of open fds. 1024 seems a very common |
| 584 | # value for that limit, but Windows has 2048, so we loop |
| 585 | # 1024 times (each call leaked two fds). |
| 586 | for i in range(1024): |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 587 | # Windows raises IOError. Others raise OSError. |
| 588 | with self.assertRaises(EnvironmentError) as c: |
Georg Brandl | f3715d2 | 2009-02-14 17:01:36 +0000 | [diff] [blame] | 589 | subprocess.Popen(['nonexisting_i_hope'], |
| 590 | stdout=subprocess.PIPE, |
| 591 | stderr=subprocess.PIPE) |
R David Murray | cdd5fc9 | 2011-03-13 22:37:18 -0400 | [diff] [blame] | 592 | # ignore errors that indicate the command was not found |
| 593 | if c.exception.errno not in (errno.ENOENT, errno.EACCES): |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 594 | raise c.exception |
Georg Brandl | f3715d2 | 2009-02-14 17:01:36 +0000 | [diff] [blame] | 595 | |
Tim Golden | 90374f5 | 2010-08-06 13:14:33 +0000 | [diff] [blame] | 596 | def test_handles_closed_on_exception(self): |
| 597 | # If CreateProcess exits with an error, ensure the |
| 598 | # duplicate output handles are released |
| 599 | ifhandle, ifname = mkstemp() |
| 600 | ofhandle, ofname = mkstemp() |
| 601 | efhandle, efname = mkstemp() |
| 602 | try: |
| 603 | subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle, |
| 604 | stderr=efhandle) |
| 605 | except OSError: |
| 606 | os.close(ifhandle) |
| 607 | os.remove(ifname) |
| 608 | os.close(ofhandle) |
| 609 | os.remove(ofname) |
| 610 | os.close(efhandle) |
| 611 | os.remove(efname) |
| 612 | self.assertFalse(os.path.exists(ifname)) |
| 613 | self.assertFalse(os.path.exists(ofname)) |
| 614 | self.assertFalse(os.path.exists(efname)) |
| 615 | |
Ross Lagerwall | 104c3f1 | 2011-04-05 15:24:34 +0200 | [diff] [blame] | 616 | def test_communicate_epipe(self): |
| 617 | # Issue 10963: communicate() should hide EPIPE |
| 618 | p = subprocess.Popen([sys.executable, "-c", 'pass'], |
| 619 | stdin=subprocess.PIPE, |
| 620 | stdout=subprocess.PIPE, |
| 621 | stderr=subprocess.PIPE) |
| 622 | self.addCleanup(p.stdout.close) |
| 623 | self.addCleanup(p.stderr.close) |
| 624 | self.addCleanup(p.stdin.close) |
| 625 | p.communicate("x" * 2**20) |
| 626 | |
| 627 | def test_communicate_epipe_only_stdin(self): |
| 628 | # Issue 10963: communicate() should hide EPIPE |
| 629 | p = subprocess.Popen([sys.executable, "-c", 'pass'], |
| 630 | stdin=subprocess.PIPE) |
| 631 | self.addCleanup(p.stdin.close) |
| 632 | time.sleep(2) |
| 633 | p.communicate("x" * 2**20) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 634 | |
| 635 | # context manager |
| 636 | class _SuppressCoreFiles(object): |
| 637 | """Try to prevent core files from being created.""" |
| 638 | old_limit = None |
| 639 | |
| 640 | def __enter__(self): |
| 641 | """Try to save previous ulimit, then set it to (0, 0).""" |
Benjamin Peterson | 8b59c23 | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 642 | if resource is not None: |
| 643 | try: |
| 644 | self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) |
| 645 | resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) |
| 646 | except (ValueError, resource.error): |
| 647 | pass |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 648 | |
Ronald Oussoren | 21b44e0 | 2010-07-23 12:26:30 +0000 | [diff] [blame] | 649 | if sys.platform == 'darwin': |
| 650 | # Check if the 'Crash Reporter' on OSX was configured |
| 651 | # in 'Developer' mode and warn that it will get triggered |
| 652 | # when it is. |
| 653 | # |
| 654 | # This assumes that this context manager is used in tests |
| 655 | # that might trigger the next manager. |
| 656 | value = subprocess.Popen(['/usr/bin/defaults', 'read', |
| 657 | 'com.apple.CrashReporter', 'DialogType'], |
| 658 | stdout=subprocess.PIPE).communicate()[0] |
| 659 | if value.strip() == b'developer': |
| 660 | print "this tests triggers the Crash Reporter, that is intentional" |
| 661 | sys.stdout.flush() |
| 662 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 663 | def __exit__(self, *args): |
| 664 | """Return core file behavior to default.""" |
| 665 | if self.old_limit is None: |
| 666 | return |
Benjamin Peterson | 8b59c23 | 2011-12-10 12:31:42 -0500 | [diff] [blame] | 667 | if resource is not None: |
| 668 | try: |
| 669 | resource.setrlimit(resource.RLIMIT_CORE, self.old_limit) |
| 670 | except (ValueError, resource.error): |
| 671 | pass |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 672 | |
Victor Stinner | b78fed9 | 2011-07-05 14:50:35 +0200 | [diff] [blame] | 673 | @unittest.skipUnless(hasattr(signal, 'SIGALRM'), |
| 674 | "Requires signal.SIGALRM") |
Victor Stinner | e790131 | 2011-07-05 14:08:01 +0200 | [diff] [blame] | 675 | def test_communicate_eintr(self): |
| 676 | # Issue #12493: communicate() should handle EINTR |
| 677 | def handler(signum, frame): |
| 678 | pass |
| 679 | old_handler = signal.signal(signal.SIGALRM, handler) |
| 680 | self.addCleanup(signal.signal, signal.SIGALRM, old_handler) |
| 681 | |
| 682 | # the process is running for 2 seconds |
| 683 | args = [sys.executable, "-c", 'import time; time.sleep(2)'] |
| 684 | for stream in ('stdout', 'stderr'): |
| 685 | kw = {stream: subprocess.PIPE} |
| 686 | with subprocess.Popen(args, **kw) as process: |
| 687 | signal.alarm(1) |
| 688 | # communicate() will be interrupted by SIGALRM |
| 689 | process.communicate() |
| 690 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 691 | |
Florent Xicluna | bab22a7 | 2010-03-04 19:40:48 +0000 | [diff] [blame] | 692 | @unittest.skipIf(mswindows, "POSIX specific tests") |
Florent Xicluna | fc4d6d7 | 2010-03-23 14:36:45 +0000 | [diff] [blame] | 693 | class POSIXProcessTestCase(BaseTestCase): |
Florent Xicluna | ab5e17f | 2010-03-04 21:31:58 +0000 | [diff] [blame] | 694 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 695 | def test_exceptions(self): |
| 696 | # caught & re-raised exceptions |
| 697 | with self.assertRaises(OSError) as c: |
| 698 | p = subprocess.Popen([sys.executable, "-c", ""], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 699 | cwd="/this/path/does/not/exist") |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 700 | # The attribute child_traceback should contain "os.chdir" somewhere. |
| 701 | self.assertIn("os.chdir", c.exception.child_traceback) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 702 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 703 | def test_run_abort(self): |
| 704 | # returncode handles signal termination |
| 705 | with _SuppressCoreFiles(): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 706 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 707 | "import os; os.abort()"]) |
| 708 | p.wait() |
| 709 | self.assertEqual(-p.returncode, signal.SIGABRT) |
| 710 | |
| 711 | def test_preexec(self): |
| 712 | # preexec function |
| 713 | p = subprocess.Popen([sys.executable, "-c", |
| 714 | "import sys, os;" |
| 715 | "sys.stdout.write(os.getenv('FRUIT'))"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 716 | stdout=subprocess.PIPE, |
| 717 | preexec_fn=lambda: os.putenv("FRUIT", "apple")) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 718 | self.addCleanup(p.stdout.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 719 | self.assertEqual(p.stdout.read(), "apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 720 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 721 | def test_args_string(self): |
| 722 | # args is a string |
| 723 | f, fname = mkstemp() |
| 724 | os.write(f, "#!/bin/sh\n") |
| 725 | os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 726 | sys.executable) |
| 727 | os.close(f) |
| 728 | os.chmod(fname, 0o700) |
| 729 | p = subprocess.Popen(fname) |
| 730 | p.wait() |
| 731 | os.remove(fname) |
| 732 | self.assertEqual(p.returncode, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 733 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 734 | def test_invalid_args(self): |
| 735 | # invalid arguments should raise ValueError |
| 736 | self.assertRaises(ValueError, subprocess.call, |
| 737 | [sys.executable, "-c", |
| 738 | "import sys; sys.exit(47)"], |
| 739 | startupinfo=47) |
| 740 | self.assertRaises(ValueError, subprocess.call, |
| 741 | [sys.executable, "-c", |
| 742 | "import sys; sys.exit(47)"], |
| 743 | creationflags=47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 744 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 745 | def test_shell_sequence(self): |
| 746 | # Run command through the shell (sequence) |
| 747 | newenv = os.environ.copy() |
| 748 | newenv["FRUIT"] = "apple" |
| 749 | p = subprocess.Popen(["echo $FRUIT"], shell=1, |
| 750 | stdout=subprocess.PIPE, |
| 751 | env=newenv) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 752 | self.addCleanup(p.stdout.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 753 | self.assertEqual(p.stdout.read().strip(), "apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 754 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 755 | def test_shell_string(self): |
| 756 | # Run command through the shell (string) |
| 757 | newenv = os.environ.copy() |
| 758 | newenv["FRUIT"] = "apple" |
| 759 | p = subprocess.Popen("echo $FRUIT", shell=1, |
| 760 | stdout=subprocess.PIPE, |
| 761 | env=newenv) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 762 | self.addCleanup(p.stdout.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 763 | self.assertEqual(p.stdout.read().strip(), "apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 764 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 765 | def test_call_string(self): |
| 766 | # call() function with string argument on UNIX |
| 767 | f, fname = mkstemp() |
| 768 | os.write(f, "#!/bin/sh\n") |
| 769 | os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 770 | sys.executable) |
| 771 | os.close(f) |
| 772 | os.chmod(fname, 0700) |
| 773 | rc = subprocess.call(fname) |
| 774 | os.remove(fname) |
| 775 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 776 | |
Stefan Krah | e9a6a7d | 2010-07-19 14:41:08 +0000 | [diff] [blame] | 777 | def test_specific_shell(self): |
| 778 | # Issue #9265: Incorrect name passed as arg[0]. |
| 779 | shells = [] |
| 780 | for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']: |
| 781 | for name in ['bash', 'ksh']: |
| 782 | sh = os.path.join(prefix, name) |
| 783 | if os.path.isfile(sh): |
| 784 | shells.append(sh) |
| 785 | if not shells: # Will probably work for any shell but csh. |
| 786 | self.skipTest("bash or ksh required for this test") |
| 787 | sh = '/bin/sh' |
| 788 | if os.path.isfile(sh) and not os.path.islink(sh): |
| 789 | # Test will fail if /bin/sh is a symlink to csh. |
| 790 | shells.append(sh) |
| 791 | for sh in shells: |
| 792 | p = subprocess.Popen("echo $0", executable=sh, shell=True, |
| 793 | stdout=subprocess.PIPE) |
Brian Curtin | d117b56 | 2010-11-05 04:09:09 +0000 | [diff] [blame] | 794 | self.addCleanup(p.stdout.close) |
Stefan Krah | e9a6a7d | 2010-07-19 14:41:08 +0000 | [diff] [blame] | 795 | self.assertEqual(p.stdout.read().strip(), sh) |
| 796 | |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 797 | def _kill_process(self, method, *args): |
Florent Xicluna | cecef39 | 2010-03-05 19:31:21 +0000 | [diff] [blame] | 798 | # Do not inherit file handles from the parent. |
| 799 | # It should fix failures on some platforms. |
Antoine Pitrou | a6166da | 2010-09-20 11:20:44 +0000 | [diff] [blame] | 800 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 801 | import sys, time |
| 802 | sys.stdout.write('x\\n') |
| 803 | sys.stdout.flush() |
| 804 | time.sleep(30) |
| 805 | """], |
| 806 | close_fds=True, |
| 807 | stdin=subprocess.PIPE, |
| 808 | stdout=subprocess.PIPE, |
| 809 | stderr=subprocess.PIPE) |
| 810 | # Wait for the interpreter to be completely initialized before |
| 811 | # sending any signal. |
| 812 | p.stdout.read(1) |
| 813 | getattr(p, method)(*args) |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 814 | return p |
| 815 | |
Antoine Pitrou | f60845b | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 816 | def _kill_dead_process(self, method, *args): |
| 817 | # Do not inherit file handles from the parent. |
| 818 | # It should fix failures on some platforms. |
| 819 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 820 | import sys, time |
| 821 | sys.stdout.write('x\\n') |
| 822 | sys.stdout.flush() |
| 823 | """], |
| 824 | close_fds=True, |
| 825 | stdin=subprocess.PIPE, |
| 826 | stdout=subprocess.PIPE, |
| 827 | stderr=subprocess.PIPE) |
| 828 | # Wait for the interpreter to be completely initialized before |
| 829 | # sending any signal. |
| 830 | p.stdout.read(1) |
| 831 | # The process should end after this |
| 832 | time.sleep(1) |
| 833 | # This shouldn't raise even though the child is now dead |
| 834 | getattr(p, method)(*args) |
| 835 | p.communicate() |
| 836 | |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 837 | def test_send_signal(self): |
| 838 | p = self._kill_process('send_signal', signal.SIGINT) |
Florent Xicluna | fc4d6d7 | 2010-03-23 14:36:45 +0000 | [diff] [blame] | 839 | _, stderr = p.communicate() |
Florent Xicluna | 3c919cf | 2010-03-23 19:19:16 +0000 | [diff] [blame] | 840 | self.assertIn('KeyboardInterrupt', stderr) |
Florent Xicluna | 446ff14 | 2010-03-23 15:05:30 +0000 | [diff] [blame] | 841 | self.assertNotEqual(p.wait(), 0) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 842 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 843 | def test_kill(self): |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 844 | p = self._kill_process('kill') |
Florent Xicluna | 446ff14 | 2010-03-23 15:05:30 +0000 | [diff] [blame] | 845 | _, stderr = p.communicate() |
| 846 | self.assertStderrEqual(stderr, '') |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 847 | self.assertEqual(p.wait(), -signal.SIGKILL) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 848 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 849 | def test_terminate(self): |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 850 | p = self._kill_process('terminate') |
Florent Xicluna | 446ff14 | 2010-03-23 15:05:30 +0000 | [diff] [blame] | 851 | _, stderr = p.communicate() |
| 852 | self.assertStderrEqual(stderr, '') |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 853 | self.assertEqual(p.wait(), -signal.SIGTERM) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 854 | |
Antoine Pitrou | f60845b | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 855 | def test_send_signal_dead(self): |
| 856 | # Sending a signal to a dead process |
| 857 | self._kill_dead_process('send_signal', signal.SIGINT) |
| 858 | |
| 859 | def test_kill_dead(self): |
| 860 | # Killing a dead process |
| 861 | self._kill_dead_process('kill') |
| 862 | |
| 863 | def test_terminate_dead(self): |
| 864 | # Terminating a dead process |
| 865 | self._kill_dead_process('terminate') |
| 866 | |
Antoine Pitrou | 91ce0d9 | 2011-01-03 18:45:09 +0000 | [diff] [blame] | 867 | def check_close_std_fds(self, fds): |
| 868 | # Issue #9905: test that subprocess pipes still work properly with |
| 869 | # some standard fds closed |
| 870 | stdin = 0 |
| 871 | newfds = [] |
| 872 | for a in fds: |
| 873 | b = os.dup(a) |
| 874 | newfds.append(b) |
| 875 | if a == 0: |
| 876 | stdin = b |
| 877 | try: |
| 878 | for fd in fds: |
| 879 | os.close(fd) |
| 880 | out, err = subprocess.Popen([sys.executable, "-c", |
| 881 | 'import sys;' |
| 882 | 'sys.stdout.write("apple");' |
| 883 | 'sys.stdout.flush();' |
| 884 | 'sys.stderr.write("orange")'], |
| 885 | stdin=stdin, |
| 886 | stdout=subprocess.PIPE, |
| 887 | stderr=subprocess.PIPE).communicate() |
| 888 | err = test_support.strip_python_stderr(err) |
| 889 | self.assertEqual((out, err), (b'apple', b'orange')) |
| 890 | finally: |
| 891 | for b, a in zip(newfds, fds): |
| 892 | os.dup2(b, a) |
| 893 | for b in newfds: |
| 894 | os.close(b) |
| 895 | |
| 896 | def test_close_fd_0(self): |
| 897 | self.check_close_std_fds([0]) |
| 898 | |
| 899 | def test_close_fd_1(self): |
| 900 | self.check_close_std_fds([1]) |
| 901 | |
| 902 | def test_close_fd_2(self): |
| 903 | self.check_close_std_fds([2]) |
| 904 | |
| 905 | def test_close_fds_0_1(self): |
| 906 | self.check_close_std_fds([0, 1]) |
| 907 | |
| 908 | def test_close_fds_0_2(self): |
| 909 | self.check_close_std_fds([0, 2]) |
| 910 | |
| 911 | def test_close_fds_1_2(self): |
| 912 | self.check_close_std_fds([1, 2]) |
| 913 | |
| 914 | def test_close_fds_0_1_2(self): |
| 915 | # Issue #10806: test that subprocess pipes still work properly with |
| 916 | # all standard fds closed. |
| 917 | self.check_close_std_fds([0, 1, 2]) |
| 918 | |
Ross Lagerwall | d8e3901 | 2011-07-27 18:54:53 +0200 | [diff] [blame] | 919 | def check_swap_fds(self, stdin_no, stdout_no, stderr_no): |
| 920 | # open up some temporary files |
| 921 | temps = [mkstemp() for i in range(3)] |
| 922 | temp_fds = [fd for fd, fname in temps] |
| 923 | try: |
| 924 | # unlink the files -- we won't need to reopen them |
| 925 | for fd, fname in temps: |
| 926 | os.unlink(fname) |
| 927 | |
| 928 | # save a copy of the standard file descriptors |
| 929 | saved_fds = [os.dup(fd) for fd in range(3)] |
| 930 | try: |
| 931 | # duplicate the temp files over the standard fd's 0, 1, 2 |
| 932 | for fd, temp_fd in enumerate(temp_fds): |
| 933 | os.dup2(temp_fd, fd) |
| 934 | |
| 935 | # write some data to what will become stdin, and rewind |
| 936 | os.write(stdin_no, b"STDIN") |
| 937 | os.lseek(stdin_no, 0, 0) |
| 938 | |
| 939 | # now use those files in the given order, so that subprocess |
| 940 | # has to rearrange them in the child |
| 941 | p = subprocess.Popen([sys.executable, "-c", |
| 942 | 'import sys; got = sys.stdin.read();' |
| 943 | 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'], |
| 944 | stdin=stdin_no, |
| 945 | stdout=stdout_no, |
| 946 | stderr=stderr_no) |
| 947 | p.wait() |
| 948 | |
| 949 | for fd in temp_fds: |
| 950 | os.lseek(fd, 0, 0) |
| 951 | |
| 952 | out = os.read(stdout_no, 1024) |
| 953 | err = test_support.strip_python_stderr(os.read(stderr_no, 1024)) |
| 954 | finally: |
| 955 | for std, saved in enumerate(saved_fds): |
| 956 | os.dup2(saved, std) |
| 957 | os.close(saved) |
| 958 | |
| 959 | self.assertEqual(out, b"got STDIN") |
| 960 | self.assertEqual(err, b"err") |
| 961 | |
| 962 | finally: |
| 963 | for fd in temp_fds: |
| 964 | os.close(fd) |
| 965 | |
| 966 | # When duping fds, if there arises a situation where one of the fds is |
| 967 | # either 0, 1 or 2, it is possible that it is overwritten (#12607). |
| 968 | # This tests all combinations of this. |
| 969 | def test_swap_fds(self): |
| 970 | self.check_swap_fds(0, 1, 2) |
| 971 | self.check_swap_fds(0, 2, 1) |
| 972 | self.check_swap_fds(1, 0, 2) |
| 973 | self.check_swap_fds(1, 2, 0) |
| 974 | self.check_swap_fds(2, 0, 1) |
| 975 | self.check_swap_fds(2, 1, 0) |
| 976 | |
Gregory P. Smith | 312efbc | 2010-12-14 15:02:53 +0000 | [diff] [blame] | 977 | def test_wait_when_sigchild_ignored(self): |
| 978 | # NOTE: sigchild_ignore.py may not be an effective test on all OSes. |
| 979 | sigchild_ignore = test_support.findfile("sigchild_ignore.py", |
| 980 | subdir="subprocessdata") |
| 981 | p = subprocess.Popen([sys.executable, sigchild_ignore], |
| 982 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 983 | stdout, stderr = p.communicate() |
| 984 | self.assertEqual(0, p.returncode, "sigchild_ignore.py exited" |
| 985 | " non-zero with this error:\n%s" % stderr) |
| 986 | |
Charles-François Natali | 100df0f | 2011-08-18 17:56:02 +0200 | [diff] [blame] | 987 | def test_zombie_fast_process_del(self): |
| 988 | # Issue #12650: on Unix, if Popen.__del__() was called before the |
| 989 | # process exited, it wouldn't be added to subprocess._active, and would |
| 990 | # remain a zombie. |
| 991 | # spawn a Popen, and delete its reference before it exits |
| 992 | p = subprocess.Popen([sys.executable, "-c", |
| 993 | 'import sys, time;' |
| 994 | 'time.sleep(0.2)'], |
| 995 | stdout=subprocess.PIPE, |
| 996 | stderr=subprocess.PIPE) |
Nadeem Vawda | 8605936 | 2011-08-19 05:22:24 +0200 | [diff] [blame] | 997 | self.addCleanup(p.stdout.close) |
| 998 | self.addCleanup(p.stderr.close) |
Charles-François Natali | 100df0f | 2011-08-18 17:56:02 +0200 | [diff] [blame] | 999 | ident = id(p) |
| 1000 | pid = p.pid |
| 1001 | del p |
| 1002 | # check that p is in the active processes list |
| 1003 | self.assertIn(ident, [id(o) for o in subprocess._active]) |
| 1004 | |
Charles-François Natali | 100df0f | 2011-08-18 17:56:02 +0200 | [diff] [blame] | 1005 | def test_leak_fast_process_del_killed(self): |
| 1006 | # Issue #12650: on Unix, if Popen.__del__() was called before the |
| 1007 | # process exited, and the process got killed by a signal, it would never |
| 1008 | # be removed from subprocess._active, which triggered a FD and memory |
| 1009 | # leak. |
| 1010 | # spawn a Popen, delete its reference and kill it |
| 1011 | p = subprocess.Popen([sys.executable, "-c", |
| 1012 | 'import time;' |
| 1013 | 'time.sleep(3)'], |
| 1014 | stdout=subprocess.PIPE, |
| 1015 | stderr=subprocess.PIPE) |
Nadeem Vawda | 8605936 | 2011-08-19 05:22:24 +0200 | [diff] [blame] | 1016 | self.addCleanup(p.stdout.close) |
| 1017 | self.addCleanup(p.stderr.close) |
Charles-François Natali | 100df0f | 2011-08-18 17:56:02 +0200 | [diff] [blame] | 1018 | ident = id(p) |
| 1019 | pid = p.pid |
| 1020 | del p |
| 1021 | os.kill(pid, signal.SIGKILL) |
| 1022 | # check that p is in the active processes list |
| 1023 | self.assertIn(ident, [id(o) for o in subprocess._active]) |
| 1024 | |
| 1025 | # let some time for the process to exit, and create a new Popen: this |
| 1026 | # should trigger the wait() of p |
| 1027 | time.sleep(0.2) |
| 1028 | with self.assertRaises(EnvironmentError) as c: |
| 1029 | with subprocess.Popen(['nonexisting_i_hope'], |
| 1030 | stdout=subprocess.PIPE, |
| 1031 | stderr=subprocess.PIPE) as proc: |
| 1032 | pass |
| 1033 | # p should have been wait()ed on, and removed from the _active list |
| 1034 | self.assertRaises(OSError, os.waitpid, pid, 0) |
| 1035 | self.assertNotIn(ident, [id(o) for o in subprocess._active]) |
| 1036 | |
Charles-François Natali | 2a34eb3 | 2011-08-25 21:20:54 +0200 | [diff] [blame] | 1037 | def test_pipe_cloexec(self): |
| 1038 | # Issue 12786: check that the communication pipes' FDs are set CLOEXEC, |
| 1039 | # and are not inherited by another child process. |
| 1040 | p1 = subprocess.Popen([sys.executable, "-c", |
| 1041 | 'import os;' |
| 1042 | 'os.read(0, 1)' |
| 1043 | ], |
| 1044 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 1045 | stderr=subprocess.PIPE) |
| 1046 | |
| 1047 | p2 = subprocess.Popen([sys.executable, "-c", """if True: |
| 1048 | import os, errno, sys |
| 1049 | for fd in %r: |
| 1050 | try: |
| 1051 | os.close(fd) |
| 1052 | except OSError as e: |
| 1053 | if e.errno != errno.EBADF: |
| 1054 | raise |
| 1055 | else: |
| 1056 | sys.exit(1) |
| 1057 | sys.exit(0) |
| 1058 | """ % [f.fileno() for f in (p1.stdin, p1.stdout, |
| 1059 | p1.stderr)] |
| 1060 | ], |
| 1061 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 1062 | stderr=subprocess.PIPE, close_fds=False) |
| 1063 | p1.communicate('foo') |
| 1064 | _, stderr = p2.communicate() |
| 1065 | |
| 1066 | self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr)) |
| 1067 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1068 | |
Florent Xicluna | bab22a7 | 2010-03-04 19:40:48 +0000 | [diff] [blame] | 1069 | @unittest.skipUnless(mswindows, "Windows specific tests") |
Florent Xicluna | fc4d6d7 | 2010-03-23 14:36:45 +0000 | [diff] [blame] | 1070 | class Win32ProcessTestCase(BaseTestCase): |
Florent Xicluna | ab5e17f | 2010-03-04 21:31:58 +0000 | [diff] [blame] | 1071 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1072 | def test_startupinfo(self): |
| 1073 | # startupinfo argument |
| 1074 | # We uses hardcoded constants, because we do not want to |
| 1075 | # depend on win32all. |
| 1076 | STARTF_USESHOWWINDOW = 1 |
| 1077 | SW_MAXIMIZE = 3 |
| 1078 | startupinfo = subprocess.STARTUPINFO() |
| 1079 | startupinfo.dwFlags = STARTF_USESHOWWINDOW |
| 1080 | startupinfo.wShowWindow = SW_MAXIMIZE |
| 1081 | # Since Python is a console process, it won't be affected |
| 1082 | # by wShowWindow, but the argument should be silently |
| 1083 | # ignored |
| 1084 | subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1085 | startupinfo=startupinfo) |
| 1086 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1087 | def test_creationflags(self): |
| 1088 | # creationflags argument |
| 1089 | CREATE_NEW_CONSOLE = 16 |
| 1090 | sys.stderr.write(" a DOS box should flash briefly ...\n") |
| 1091 | subprocess.call(sys.executable + |
| 1092 | ' -c "import time; time.sleep(0.25)"', |
| 1093 | creationflags=CREATE_NEW_CONSOLE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1094 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1095 | def test_invalid_args(self): |
| 1096 | # invalid arguments should raise ValueError |
| 1097 | self.assertRaises(ValueError, subprocess.call, |
| 1098 | [sys.executable, "-c", |
| 1099 | "import sys; sys.exit(47)"], |
| 1100 | preexec_fn=lambda: 1) |
| 1101 | self.assertRaises(ValueError, subprocess.call, |
| 1102 | [sys.executable, "-c", |
| 1103 | "import sys; sys.exit(47)"], |
| 1104 | stdout=subprocess.PIPE, |
| 1105 | close_fds=True) |
| 1106 | |
| 1107 | def test_close_fds(self): |
| 1108 | # close file descriptors |
| 1109 | rc = subprocess.call([sys.executable, "-c", |
| 1110 | "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1111 | close_fds=True) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1112 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1113 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1114 | def test_shell_sequence(self): |
| 1115 | # Run command through the shell (sequence) |
| 1116 | newenv = os.environ.copy() |
| 1117 | newenv["FRUIT"] = "physalis" |
| 1118 | p = subprocess.Popen(["set"], shell=1, |
| 1119 | stdout=subprocess.PIPE, |
| 1120 | env=newenv) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 1121 | self.addCleanup(p.stdout.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1122 | self.assertIn("physalis", p.stdout.read()) |
Peter Astrand | 81a191b | 2007-05-26 22:18:20 +0000 | [diff] [blame] | 1123 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1124 | def test_shell_string(self): |
| 1125 | # Run command through the shell (string) |
| 1126 | newenv = os.environ.copy() |
| 1127 | newenv["FRUIT"] = "physalis" |
| 1128 | p = subprocess.Popen("set", shell=1, |
| 1129 | stdout=subprocess.PIPE, |
| 1130 | env=newenv) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 1131 | self.addCleanup(p.stdout.close) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1132 | self.assertIn("physalis", p.stdout.read()) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1133 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1134 | def test_call_string(self): |
| 1135 | # call() function with string argument on Windows |
| 1136 | rc = subprocess.call(sys.executable + |
| 1137 | ' -c "import sys; sys.exit(47)"') |
| 1138 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1139 | |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 1140 | def _kill_process(self, method, *args): |
Florent Xicluna | 400efc2 | 2010-03-07 17:12:23 +0000 | [diff] [blame] | 1141 | # Some win32 buildbot raises EOFError if stdin is inherited |
Antoine Pitrou | dee0097 | 2010-09-24 19:00:29 +0000 | [diff] [blame] | 1142 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 1143 | import sys, time |
| 1144 | sys.stdout.write('x\\n') |
| 1145 | sys.stdout.flush() |
| 1146 | time.sleep(30) |
| 1147 | """], |
| 1148 | stdin=subprocess.PIPE, |
| 1149 | stdout=subprocess.PIPE, |
| 1150 | stderr=subprocess.PIPE) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 1151 | self.addCleanup(p.stdout.close) |
| 1152 | self.addCleanup(p.stderr.close) |
| 1153 | self.addCleanup(p.stdin.close) |
Antoine Pitrou | dee0097 | 2010-09-24 19:00:29 +0000 | [diff] [blame] | 1154 | # Wait for the interpreter to be completely initialized before |
| 1155 | # sending any signal. |
| 1156 | p.stdout.read(1) |
| 1157 | getattr(p, method)(*args) |
Florent Xicluna | 446ff14 | 2010-03-23 15:05:30 +0000 | [diff] [blame] | 1158 | _, stderr = p.communicate() |
| 1159 | self.assertStderrEqual(stderr, '') |
Antoine Pitrou | dee0097 | 2010-09-24 19:00:29 +0000 | [diff] [blame] | 1160 | returncode = p.wait() |
Florent Xicluna | faf1753 | 2010-03-08 10:59:33 +0000 | [diff] [blame] | 1161 | self.assertNotEqual(returncode, 0) |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 1162 | |
Antoine Pitrou | f60845b | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 1163 | def _kill_dead_process(self, method, *args): |
| 1164 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 1165 | import sys, time |
| 1166 | sys.stdout.write('x\\n') |
| 1167 | sys.stdout.flush() |
| 1168 | sys.exit(42) |
| 1169 | """], |
| 1170 | stdin=subprocess.PIPE, |
| 1171 | stdout=subprocess.PIPE, |
| 1172 | stderr=subprocess.PIPE) |
| 1173 | self.addCleanup(p.stdout.close) |
| 1174 | self.addCleanup(p.stderr.close) |
| 1175 | self.addCleanup(p.stdin.close) |
| 1176 | # Wait for the interpreter to be completely initialized before |
| 1177 | # sending any signal. |
| 1178 | p.stdout.read(1) |
| 1179 | # The process should end after this |
| 1180 | time.sleep(1) |
| 1181 | # This shouldn't raise even though the child is now dead |
| 1182 | getattr(p, method)(*args) |
| 1183 | _, stderr = p.communicate() |
| 1184 | self.assertStderrEqual(stderr, b'') |
| 1185 | rc = p.wait() |
| 1186 | self.assertEqual(rc, 42) |
| 1187 | |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 1188 | def test_send_signal(self): |
| 1189 | self._kill_process('send_signal', signal.SIGTERM) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 1190 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1191 | def test_kill(self): |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 1192 | self._kill_process('kill') |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 1193 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1194 | def test_terminate(self): |
Florent Xicluna | c083864 | 2010-03-07 15:27:39 +0000 | [diff] [blame] | 1195 | self._kill_process('terminate') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1196 | |
Antoine Pitrou | f60845b | 2012-03-11 19:29:12 +0100 | [diff] [blame] | 1197 | def test_send_signal_dead(self): |
| 1198 | self._kill_dead_process('send_signal', signal.SIGTERM) |
| 1199 | |
| 1200 | def test_kill_dead(self): |
| 1201 | self._kill_dead_process('kill') |
| 1202 | |
| 1203 | def test_terminate_dead(self): |
| 1204 | self._kill_dead_process('terminate') |
| 1205 | |
Gregory P. Smith | dd7ca24 | 2009-07-04 01:49:29 +0000 | [diff] [blame] | 1206 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1207 | @unittest.skipUnless(getattr(subprocess, '_has_poll', False), |
| 1208 | "poll system call not supported") |
| 1209 | class ProcessTestCaseNoPoll(ProcessTestCase): |
| 1210 | def setUp(self): |
| 1211 | subprocess._has_poll = False |
| 1212 | ProcessTestCase.setUp(self) |
Gregory P. Smith | dd7ca24 | 2009-07-04 01:49:29 +0000 | [diff] [blame] | 1213 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1214 | def tearDown(self): |
| 1215 | subprocess._has_poll = True |
| 1216 | ProcessTestCase.tearDown(self) |
Gregory P. Smith | dd7ca24 | 2009-07-04 01:49:29 +0000 | [diff] [blame] | 1217 | |
| 1218 | |
Gregory P. Smith | cce211f | 2010-03-01 00:05:08 +0000 | [diff] [blame] | 1219 | class HelperFunctionTests(unittest.TestCase): |
Gregory P. Smith | c1baf4a | 2010-03-01 02:53:24 +0000 | [diff] [blame] | 1220 | @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows") |
Gregory P. Smith | cce211f | 2010-03-01 00:05:08 +0000 | [diff] [blame] | 1221 | def test_eintr_retry_call(self): |
| 1222 | record_calls = [] |
| 1223 | def fake_os_func(*args): |
| 1224 | record_calls.append(args) |
| 1225 | if len(record_calls) == 2: |
| 1226 | raise OSError(errno.EINTR, "fake interrupted system call") |
| 1227 | return tuple(reversed(args)) |
| 1228 | |
| 1229 | self.assertEqual((999, 256), |
| 1230 | subprocess._eintr_retry_call(fake_os_func, 256, 999)) |
| 1231 | self.assertEqual([(256, 999)], record_calls) |
| 1232 | # This time there will be an EINTR so it will loop once. |
| 1233 | self.assertEqual((666,), |
| 1234 | subprocess._eintr_retry_call(fake_os_func, 666)) |
| 1235 | self.assertEqual([(256, 999), (666,), (666,)], record_calls) |
| 1236 | |
Tim Golden | 8e4756c | 2010-08-12 11:00:35 +0000 | [diff] [blame] | 1237 | @unittest.skipUnless(mswindows, "mswindows only") |
| 1238 | class CommandsWithSpaces (BaseTestCase): |
| 1239 | |
| 1240 | def setUp(self): |
| 1241 | super(CommandsWithSpaces, self).setUp() |
| 1242 | f, fname = mkstemp(".py", "te st") |
| 1243 | self.fname = fname.lower () |
| 1244 | os.write(f, b"import sys;" |
| 1245 | b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))" |
| 1246 | ) |
| 1247 | os.close(f) |
| 1248 | |
| 1249 | def tearDown(self): |
| 1250 | os.remove(self.fname) |
| 1251 | super(CommandsWithSpaces, self).tearDown() |
| 1252 | |
| 1253 | def with_spaces(self, *args, **kwargs): |
| 1254 | kwargs['stdout'] = subprocess.PIPE |
| 1255 | p = subprocess.Popen(*args, **kwargs) |
Brian Curtin | 7fe045e | 2010-11-05 17:19:38 +0000 | [diff] [blame] | 1256 | self.addCleanup(p.stdout.close) |
Tim Golden | 8e4756c | 2010-08-12 11:00:35 +0000 | [diff] [blame] | 1257 | self.assertEqual( |
| 1258 | p.stdout.read ().decode("mbcs"), |
| 1259 | "2 [%r, 'ab cd']" % self.fname |
| 1260 | ) |
| 1261 | |
| 1262 | def test_shell_string_with_spaces(self): |
| 1263 | # call() function with string argument with spaces on Windows |
Brian Curtin | e8c4920 | 2010-08-13 21:01:52 +0000 | [diff] [blame] | 1264 | self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, |
| 1265 | "ab cd"), shell=1) |
Tim Golden | 8e4756c | 2010-08-12 11:00:35 +0000 | [diff] [blame] | 1266 | |
| 1267 | def test_shell_sequence_with_spaces(self): |
| 1268 | # call() function with sequence argument with spaces on Windows |
Brian Curtin | e8c4920 | 2010-08-13 21:01:52 +0000 | [diff] [blame] | 1269 | self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1) |
Tim Golden | 8e4756c | 2010-08-12 11:00:35 +0000 | [diff] [blame] | 1270 | |
| 1271 | def test_noshell_string_with_spaces(self): |
| 1272 | # call() function with string argument with spaces on Windows |
| 1273 | self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, |
| 1274 | "ab cd")) |
| 1275 | |
| 1276 | def test_noshell_sequence_with_spaces(self): |
| 1277 | # call() function with sequence argument with spaces on Windows |
| 1278 | self.with_spaces([sys.executable, self.fname, "ab cd"]) |
| 1279 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1280 | def test_main(): |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1281 | unit_tests = (ProcessTestCase, |
| 1282 | POSIXProcessTestCase, |
| 1283 | Win32ProcessTestCase, |
Gregory P. Smith | cce211f | 2010-03-01 00:05:08 +0000 | [diff] [blame] | 1284 | ProcessTestCaseNoPoll, |
Tim Golden | 8e4756c | 2010-08-12 11:00:35 +0000 | [diff] [blame] | 1285 | HelperFunctionTests, |
| 1286 | CommandsWithSpaces) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1287 | |
Gregory P. Smith | dd7ca24 | 2009-07-04 01:49:29 +0000 | [diff] [blame] | 1288 | test_support.run_unittest(*unit_tests) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 1289 | test_support.reap_children() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1290 | |
| 1291 | if __name__ == "__main__": |
| 1292 | test_main() |