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 | |
| 13 | mswindows = (sys.platform == "win32") |
| 14 | |
| 15 | # |
| 16 | # Depends on the following external programs: Python |
| 17 | # |
| 18 | |
| 19 | if mswindows: |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 20 | SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), ' |
| 21 | 'os.O_BINARY);') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 22 | else: |
| 23 | SETBINARY = '' |
| 24 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 25 | |
| 26 | try: |
| 27 | mkstemp = tempfile.mkstemp |
| 28 | except AttributeError: |
| 29 | # tempfile.mkstemp is not available |
| 30 | def mkstemp(): |
| 31 | """Replacement for mkstemp, calling mktemp.""" |
| 32 | fname = tempfile.mktemp() |
| 33 | return os.open(fname, os.O_RDWR|os.O_CREAT), fname |
| 34 | |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 35 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 36 | class ProcessTestCase(unittest.TestCase): |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 37 | def setUp(self): |
Tim Peters | 38ff36c | 2006-06-30 06:18:39 +0000 | [diff] [blame] | 38 | # Try to minimize the number of children we have so this test |
| 39 | # doesn't crash on some buildbots (Alphas in particular). |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 40 | test_support.reap_children() |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 41 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 42 | def assertStderrEqual(self, stderr, expected, msg=None): |
| 43 | # In a debug build, stuff like "[6580 refs]" is printed to stderr at |
| 44 | # shutdown time. That frustrates tests trying to check stderr produced |
| 45 | # from a spawned Python process. |
| 46 | actual = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr) |
| 47 | self.assertEqual(actual, expected, msg) |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 48 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 49 | def test_call_seq(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 50 | # call() function with sequence argument |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 51 | rc = subprocess.call([sys.executable, "-c", |
| 52 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 53 | self.assertEqual(rc, 47) |
| 54 | |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 55 | def test_check_call_zero(self): |
| 56 | # check_call() function with zero return code |
| 57 | rc = subprocess.check_call([sys.executable, "-c", |
| 58 | "import sys; sys.exit(0)"]) |
| 59 | self.assertEqual(rc, 0) |
| 60 | |
| 61 | def test_check_call_nonzero(self): |
| 62 | # check_call() function with non-zero return code |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 63 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 64 | subprocess.check_call([sys.executable, "-c", |
| 65 | "import sys; sys.exit(47)"]) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 66 | self.assertEqual(c.exception.returncode, 47) |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 67 | |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 68 | def test_check_output(self): |
| 69 | # check_output() function with zero return code |
| 70 | output = subprocess.check_output( |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 71 | [sys.executable, "-c", "print 'BDFL'"]) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 72 | self.assertIn('BDFL', output) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 73 | |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 74 | def test_check_output_nonzero(self): |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 75 | # check_call() function with non-zero return code |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 76 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 77 | subprocess.check_output( |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 78 | [sys.executable, "-c", "import sys; sys.exit(5)"]) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 79 | self.assertEqual(c.exception.returncode, 5) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 80 | |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 81 | def test_check_output_stderr(self): |
| 82 | # check_output() function stderr redirected to stdout |
| 83 | output = subprocess.check_output( |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 84 | [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"], |
| 85 | stderr=subprocess.STDOUT) |
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_stdout_arg(self): |
| 89 | # check_output() function stderr redirected to stdout |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 90 | with self.assertRaises(ValueError) as c: |
Gregory P. Smith | 2657680 | 2008-12-05 02:27:01 +0000 | [diff] [blame] | 91 | output = subprocess.check_output( |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 92 | [sys.executable, "-c", "print 'will not be run'"], |
| 93 | stdout=sys.stdout) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 94 | self.fail("Expected ValueError when stdout arg supplied.") |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 95 | self.assertIn('stdout', c.exception.args[0]) |
Gregory P. Smith | 97f49f4 | 2008-12-04 20:21:09 +0000 | [diff] [blame] | 96 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 97 | def test_call_kwargs(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 98 | # call() function with keyword args |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 99 | newenv = os.environ.copy() |
| 100 | newenv["FRUIT"] = "banana" |
| 101 | rc = subprocess.call([sys.executable, "-c", |
| 102 | 'import sys, os;' \ |
| 103 | 'sys.exit(os.getenv("FRUIT")=="banana")'], |
| 104 | env=newenv) |
| 105 | self.assertEqual(rc, 1) |
| 106 | |
| 107 | def test_stdin_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 108 | # .stdin is None when not redirected |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 109 | p = subprocess.Popen([sys.executable, "-c", 'print "banana"'], |
| 110 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 111 | p.wait() |
| 112 | self.assertEqual(p.stdin, None) |
| 113 | |
| 114 | def test_stdout_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 115 | # .stdout is None when not redirected |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 116 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | 4052fe5 | 2004-10-13 03:29:54 +0000 | [diff] [blame] | 117 | 'print " this bit of output is from a ' |
| 118 | 'test of stdout in a different ' |
| 119 | 'process ..."'], |
| 120 | stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 121 | p.wait() |
| 122 | self.assertEqual(p.stdout, None) |
| 123 | |
| 124 | def test_stderr_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 125 | # .stderr is None when not redirected |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 126 | p = subprocess.Popen([sys.executable, "-c", 'print "banana"'], |
| 127 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 128 | p.wait() |
| 129 | self.assertEqual(p.stderr, None) |
| 130 | |
Ezio Melotti | 8f6a287 | 2010-02-10 21:40:33 +0000 | [diff] [blame] | 131 | def test_executable_with_cwd(self): |
| 132 | python_dir = os.path.dirname(os.path.realpath(sys.executable)) |
| 133 | p = subprocess.Popen(["somethingyoudonthave", "-c", |
| 134 | "import sys; sys.exit(47)"], |
| 135 | executable=sys.executable, cwd=python_dir) |
| 136 | p.wait() |
| 137 | self.assertEqual(p.returncode, 47) |
| 138 | |
| 139 | @unittest.skipIf(sysconfig.is_python_build(), |
| 140 | "need an installed Python. See #7774") |
| 141 | def test_executable_without_cwd(self): |
| 142 | # For a normal installation, it should work without 'cwd' |
| 143 | # argument. For test runs in the build directory, see #7774. |
| 144 | p = subprocess.Popen(["somethingyoudonthave", "-c", |
| 145 | "import sys; sys.exit(47)"], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 146 | executable=sys.executable) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 147 | p.wait() |
| 148 | self.assertEqual(p.returncode, 47) |
| 149 | |
| 150 | def test_stdin_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 151 | # stdin redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 152 | p = subprocess.Popen([sys.executable, "-c", |
| 153 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 154 | stdin=subprocess.PIPE) |
| 155 | p.stdin.write("pear") |
| 156 | p.stdin.close() |
| 157 | p.wait() |
| 158 | self.assertEqual(p.returncode, 1) |
| 159 | |
| 160 | def test_stdin_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 161 | # stdin is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 162 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 163 | d = tf.fileno() |
| 164 | os.write(d, "pear") |
| 165 | os.lseek(d, 0, 0) |
| 166 | p = subprocess.Popen([sys.executable, "-c", |
| 167 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 168 | stdin=d) |
| 169 | p.wait() |
| 170 | self.assertEqual(p.returncode, 1) |
| 171 | |
| 172 | def test_stdin_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 173 | # stdin is set to open file object |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 174 | tf = tempfile.TemporaryFile() |
| 175 | tf.write("pear") |
| 176 | tf.seek(0) |
| 177 | p = subprocess.Popen([sys.executable, "-c", |
| 178 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 179 | stdin=tf) |
| 180 | p.wait() |
| 181 | self.assertEqual(p.returncode, 1) |
| 182 | |
| 183 | def test_stdout_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 184 | # stdout redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 185 | p = subprocess.Popen([sys.executable, "-c", |
| 186 | 'import sys; sys.stdout.write("orange")'], |
| 187 | stdout=subprocess.PIPE) |
| 188 | self.assertEqual(p.stdout.read(), "orange") |
| 189 | |
| 190 | def test_stdout_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 191 | # stdout 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 | p = subprocess.Popen([sys.executable, "-c", |
| 195 | 'import sys; sys.stdout.write("orange")'], |
| 196 | stdout=d) |
| 197 | p.wait() |
| 198 | os.lseek(d, 0, 0) |
| 199 | self.assertEqual(os.read(d, 1024), "orange") |
| 200 | |
| 201 | def test_stdout_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 202 | # stdout is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 203 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 204 | p = subprocess.Popen([sys.executable, "-c", |
| 205 | 'import sys; sys.stdout.write("orange")'], |
| 206 | stdout=tf) |
| 207 | p.wait() |
| 208 | tf.seek(0) |
| 209 | self.assertEqual(tf.read(), "orange") |
| 210 | |
| 211 | def test_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 212 | # stderr redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 213 | p = subprocess.Popen([sys.executable, "-c", |
| 214 | 'import sys; sys.stderr.write("strawberry")'], |
| 215 | stderr=subprocess.PIPE) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 216 | self.assertStderrEqual(p.stderr.read(), "strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 217 | |
| 218 | def test_stderr_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 219 | # stderr is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 220 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 221 | d = tf.fileno() |
| 222 | p = subprocess.Popen([sys.executable, "-c", |
| 223 | 'import sys; sys.stderr.write("strawberry")'], |
| 224 | stderr=d) |
| 225 | p.wait() |
| 226 | os.lseek(d, 0, 0) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 227 | self.assertStderrEqual(os.read(d, 1024), "strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 228 | |
| 229 | def test_stderr_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 230 | # stderr is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 231 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 232 | p = subprocess.Popen([sys.executable, "-c", |
| 233 | 'import sys; sys.stderr.write("strawberry")'], |
| 234 | stderr=tf) |
| 235 | p.wait() |
| 236 | tf.seek(0) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 237 | self.assertStderrEqual(tf.read(), "strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 238 | |
| 239 | def test_stdout_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 240 | # capture stdout and stderr to the same pipe |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 241 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 242 | 'import sys;' |
| 243 | 'sys.stdout.write("apple");' |
| 244 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 245 | 'sys.stderr.write("orange")'], |
| 246 | stdout=subprocess.PIPE, |
| 247 | stderr=subprocess.STDOUT) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 248 | self.assertStderrEqual(p.stdout.read(), "appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 249 | |
| 250 | def test_stdout_stderr_file(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 251 | # capture stdout and stderr to the same open file |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 252 | tf = tempfile.TemporaryFile() |
| 253 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 254 | 'import sys;' |
| 255 | 'sys.stdout.write("apple");' |
| 256 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 257 | 'sys.stderr.write("orange")'], |
| 258 | stdout=tf, |
| 259 | stderr=tf) |
| 260 | p.wait() |
| 261 | tf.seek(0) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 262 | self.assertStderrEqual(tf.read(), "appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 263 | |
Gustavo Niemeyer | c36bede | 2006-09-07 00:48:33 +0000 | [diff] [blame] | 264 | def test_stdout_filedes_of_stdout(self): |
| 265 | # stdout is set to 1 (#1531862). |
| 266 | cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))" |
| 267 | rc = subprocess.call([sys.executable, "-c", cmd], stdout=1) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 268 | self.assertEqual(rc, 2) |
Gustavo Niemeyer | c36bede | 2006-09-07 00:48:33 +0000 | [diff] [blame] | 269 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 270 | def test_cwd(self): |
Guido van Rossum | e9a0e88 | 2007-12-20 17:28:10 +0000 | [diff] [blame] | 271 | tmpdir = tempfile.gettempdir() |
Peter Astrand | 195404f | 2004-11-12 15:51:48 +0000 | [diff] [blame] | 272 | # We cannot use os.path.realpath to canonicalize the path, |
| 273 | # since it doesn't expand Tru64 {memb} strings. See bug 1063571. |
| 274 | cwd = os.getcwd() |
| 275 | os.chdir(tmpdir) |
| 276 | tmpdir = os.getcwd() |
| 277 | os.chdir(cwd) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 278 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 279 | 'import sys,os;' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 280 | 'sys.stdout.write(os.getcwd())'], |
| 281 | stdout=subprocess.PIPE, |
| 282 | cwd=tmpdir) |
Fredrik Lundh | 59c0559 | 2004-10-13 06:55:40 +0000 | [diff] [blame] | 283 | normcase = os.path.normcase |
| 284 | self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir)) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 285 | |
| 286 | def test_env(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 287 | newenv = os.environ.copy() |
| 288 | newenv["FRUIT"] = "orange" |
| 289 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 290 | 'import sys,os;' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 291 | 'sys.stdout.write(os.getenv("FRUIT"))'], |
| 292 | stdout=subprocess.PIPE, |
| 293 | env=newenv) |
| 294 | self.assertEqual(p.stdout.read(), "orange") |
| 295 | |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 296 | def test_communicate_stdin(self): |
| 297 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 298 | 'import sys;' |
| 299 | 'sys.exit(sys.stdin.read() == "pear")'], |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 300 | stdin=subprocess.PIPE) |
| 301 | p.communicate("pear") |
| 302 | self.assertEqual(p.returncode, 1) |
| 303 | |
| 304 | def test_communicate_stdout(self): |
| 305 | p = subprocess.Popen([sys.executable, "-c", |
| 306 | 'import sys; sys.stdout.write("pineapple")'], |
| 307 | stdout=subprocess.PIPE) |
| 308 | (stdout, stderr) = p.communicate() |
| 309 | self.assertEqual(stdout, "pineapple") |
| 310 | self.assertEqual(stderr, None) |
| 311 | |
| 312 | def test_communicate_stderr(self): |
| 313 | p = subprocess.Popen([sys.executable, "-c", |
| 314 | 'import sys; sys.stderr.write("pineapple")'], |
| 315 | stderr=subprocess.PIPE) |
| 316 | (stdout, stderr) = p.communicate() |
| 317 | self.assertEqual(stdout, None) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 318 | self.assertStderrEqual(stderr, "pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 319 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 320 | def test_communicate(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 321 | p = subprocess.Popen([sys.executable, "-c", |
Gregory P. Smith | 4036fd4 | 2008-05-26 20:22:14 +0000 | [diff] [blame] | 322 | 'import sys,os;' |
| 323 | 'sys.stderr.write("pineapple");' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 324 | 'sys.stdout.write(sys.stdin.read())'], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 325 | stdin=subprocess.PIPE, |
| 326 | stdout=subprocess.PIPE, |
| 327 | stderr=subprocess.PIPE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 328 | (stdout, stderr) = p.communicate("banana") |
| 329 | self.assertEqual(stdout, "banana") |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 330 | self.assertStderrEqual(stderr, "pineapple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 331 | |
Gregory P. Smith | 4036fd4 | 2008-05-26 20:22:14 +0000 | [diff] [blame] | 332 | # This test is Linux specific for simplicity to at least have |
| 333 | # some coverage. It is not a platform specific bug. |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 334 | @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()), |
| 335 | "Linux specific") |
| 336 | # Test for the fd leak reported in http://bugs.python.org/issue2791. |
| 337 | def test_communicate_pipe_fd_leak(self): |
| 338 | fd_directory = '/proc/%d/fd' % os.getpid() |
| 339 | num_fds_before_popen = len(os.listdir(fd_directory)) |
| 340 | p = subprocess.Popen([sys.executable, "-c", "print()"], |
| 341 | stdout=subprocess.PIPE) |
| 342 | p.communicate() |
| 343 | num_fds_after_communicate = len(os.listdir(fd_directory)) |
| 344 | del p |
| 345 | num_fds_after_destruction = len(os.listdir(fd_directory)) |
| 346 | self.assertEqual(num_fds_before_popen, num_fds_after_destruction) |
| 347 | self.assertEqual(num_fds_before_popen, num_fds_after_communicate) |
Gregory P. Smith | 4036fd4 | 2008-05-26 20:22:14 +0000 | [diff] [blame] | 348 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 349 | def test_communicate_returns(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 350 | # communicate() should return None if no redirection is active |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 351 | p = subprocess.Popen([sys.executable, "-c", |
| 352 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 353 | (stdout, stderr) = p.communicate() |
| 354 | self.assertEqual(stdout, None) |
| 355 | self.assertEqual(stderr, None) |
| 356 | |
| 357 | def test_communicate_pipe_buf(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 358 | # communicate() with writes larger than pipe_buf |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 359 | # This test will probably deadlock rather than fail, if |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 360 | # communicate() does not work properly. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 361 | x, y = os.pipe() |
| 362 | if mswindows: |
| 363 | pipe_buf = 512 |
| 364 | else: |
| 365 | pipe_buf = os.fpathconf(x, "PC_PIPE_BUF") |
| 366 | os.close(x) |
| 367 | os.close(y) |
| 368 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 369 | 'import sys,os;' |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 370 | 'sys.stdout.write(sys.stdin.read(47));' |
| 371 | 'sys.stderr.write("xyz"*%d);' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 372 | 'sys.stdout.write(sys.stdin.read())' % pipe_buf], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 373 | stdin=subprocess.PIPE, |
| 374 | stdout=subprocess.PIPE, |
| 375 | stderr=subprocess.PIPE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 376 | string_to_write = "abc"*pipe_buf |
| 377 | (stdout, stderr) = p.communicate(string_to_write) |
| 378 | self.assertEqual(stdout, string_to_write) |
| 379 | |
| 380 | def test_writes_before_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 381 | # stdin.write before communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 382 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 383 | 'import sys,os;' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 384 | 'sys.stdout.write(sys.stdin.read())'], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 385 | stdin=subprocess.PIPE, |
| 386 | stdout=subprocess.PIPE, |
| 387 | stderr=subprocess.PIPE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 388 | p.stdin.write("banana") |
| 389 | (stdout, stderr) = p.communicate("split") |
| 390 | self.assertEqual(stdout, "bananasplit") |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 391 | self.assertStderrEqual(stderr, "") |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 392 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 393 | def test_universal_newlines(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 394 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 395 | 'import sys,os;' + SETBINARY + |
| 396 | 'sys.stdout.write("line1\\n");' |
| 397 | 'sys.stdout.flush();' |
| 398 | 'sys.stdout.write("line2\\r");' |
| 399 | 'sys.stdout.flush();' |
| 400 | 'sys.stdout.write("line3\\r\\n");' |
| 401 | 'sys.stdout.flush();' |
| 402 | 'sys.stdout.write("line4\\r");' |
| 403 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 404 | 'sys.stdout.write("\\nline5");' |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 405 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 406 | 'sys.stdout.write("\\nline6");'], |
| 407 | stdout=subprocess.PIPE, |
| 408 | universal_newlines=1) |
| 409 | stdout = p.stdout.read() |
Neal Norwitz | a6d01ce | 2006-05-02 06:23:22 +0000 | [diff] [blame] | 410 | if hasattr(file, 'newlines'): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 411 | # Interpreter with universal newline support |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 412 | self.assertEqual(stdout, |
| 413 | "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 414 | else: |
| 415 | # Interpreter without universal newline support |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 416 | self.assertEqual(stdout, |
| 417 | "line1\nline2\rline3\r\nline4\r\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 418 | |
| 419 | def test_universal_newlines_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 420 | # universal newlines through communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 421 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 422 | 'import sys,os;' + SETBINARY + |
| 423 | 'sys.stdout.write("line1\\n");' |
| 424 | 'sys.stdout.flush();' |
| 425 | 'sys.stdout.write("line2\\r");' |
| 426 | 'sys.stdout.flush();' |
| 427 | 'sys.stdout.write("line3\\r\\n");' |
| 428 | 'sys.stdout.flush();' |
| 429 | 'sys.stdout.write("line4\\r");' |
| 430 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 431 | 'sys.stdout.write("\\nline5");' |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 432 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 433 | 'sys.stdout.write("\\nline6");'], |
| 434 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 435 | universal_newlines=1) |
| 436 | (stdout, stderr) = p.communicate() |
Neal Norwitz | a6d01ce | 2006-05-02 06:23:22 +0000 | [diff] [blame] | 437 | if hasattr(file, 'newlines'): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 438 | # Interpreter with universal newline support |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 439 | self.assertEqual(stdout, |
| 440 | "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 441 | else: |
| 442 | # Interpreter without universal newline support |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 443 | self.assertEqual(stdout, |
| 444 | "line1\nline2\rline3\r\nline4\r\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 445 | |
| 446 | def test_no_leaking(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 447 | # Make sure we leak no resources |
Peter Astrand | d6b2430 | 2006-06-22 20:06:46 +0000 | [diff] [blame] | 448 | if not hasattr(test_support, "is_resource_enabled") \ |
| 449 | or test_support.is_resource_enabled("subprocess") and not mswindows: |
Peter Astrand | f7f1bb7 | 2005-03-03 20:47:37 +0000 | [diff] [blame] | 450 | max_handles = 1026 # too much for most UNIX systems |
| 451 | else: |
Tim Peters | eba28be | 2005-03-28 01:08:02 +0000 | [diff] [blame] | 452 | max_handles = 65 |
Fredrik Lundh | 9e29fc5 | 2004-10-13 07:54:54 +0000 | [diff] [blame] | 453 | for i in range(max_handles): |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 454 | p = subprocess.Popen([sys.executable, "-c", |
| 455 | "import sys;sys.stdout.write(sys.stdin.read())"], |
| 456 | stdin=subprocess.PIPE, |
| 457 | stdout=subprocess.PIPE, |
| 458 | stderr=subprocess.PIPE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 459 | data = p.communicate("lime")[0] |
| 460 | self.assertEqual(data, "lime") |
| 461 | |
| 462 | |
| 463 | def test_list2cmdline(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 464 | self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), |
| 465 | '"a b c" d e') |
| 466 | self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), |
| 467 | 'ab\\"c \\ d') |
Gregory P. Smith | e047e6d | 2008-01-19 20:49:02 +0000 | [diff] [blame] | 468 | self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']), |
| 469 | 'ab\\"c " \\\\" d') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 470 | self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']), |
| 471 | 'a\\\\\\b "de fg" h') |
| 472 | self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']), |
| 473 | 'a\\\\\\"b c d') |
| 474 | self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']), |
| 475 | '"a\\\\b c" d e') |
| 476 | self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']), |
| 477 | '"a\\\\b\\ c" d e') |
Peter Astrand | 10514a7 | 2007-01-13 22:35:35 +0000 | [diff] [blame] | 478 | self.assertEqual(subprocess.list2cmdline(['ab', '']), |
| 479 | 'ab ""') |
Gregory P. Smith | 70eb2f9 | 2008-01-19 22:49:37 +0000 | [diff] [blame] | 480 | self.assertEqual(subprocess.list2cmdline(['echo', 'foo|bar']), |
| 481 | 'echo "foo|bar"') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 482 | |
| 483 | |
| 484 | def test_poll(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 485 | p = subprocess.Popen([sys.executable, |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 486 | "-c", "import time; time.sleep(1)"]) |
| 487 | count = 0 |
| 488 | while p.poll() is None: |
| 489 | time.sleep(0.1) |
| 490 | count += 1 |
| 491 | # We expect that the poll loop probably went around about 10 times, |
| 492 | # but, based on system scheduling we can't control, it's possible |
| 493 | # poll() never returned None. It "should be" very rare that it |
| 494 | # didn't go around at least twice. |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 495 | self.assertGreaterEqual(count, 2) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 496 | # Subsequent invocations should just return the returncode |
| 497 | self.assertEqual(p.poll(), 0) |
| 498 | |
| 499 | |
| 500 | def test_wait(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 501 | p = subprocess.Popen([sys.executable, |
| 502 | "-c", "import time; time.sleep(2)"]) |
| 503 | self.assertEqual(p.wait(), 0) |
| 504 | # Subsequent invocations should just return the returncode |
| 505 | self.assertEqual(p.wait(), 0) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 506 | |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 507 | |
| 508 | def test_invalid_bufsize(self): |
| 509 | # an invalid type of the bufsize argument should raise |
| 510 | # TypeError. |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 511 | with self.assertRaises(TypeError): |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 512 | subprocess.Popen([sys.executable, "-c", "pass"], "orange") |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 513 | |
Georg Brandl | f3715d2 | 2009-02-14 17:01:36 +0000 | [diff] [blame] | 514 | def test_leaking_fds_on_error(self): |
| 515 | # see bug #5179: Popen leaks file descriptors to PIPEs if |
| 516 | # the child fails to execute; this will eventually exhaust |
| 517 | # the maximum number of open fds. 1024 seems a very common |
| 518 | # value for that limit, but Windows has 2048, so we loop |
| 519 | # 1024 times (each call leaked two fds). |
| 520 | for i in range(1024): |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 521 | # Windows raises IOError. Others raise OSError. |
| 522 | with self.assertRaises(EnvironmentError) as c: |
Georg Brandl | f3715d2 | 2009-02-14 17:01:36 +0000 | [diff] [blame] | 523 | subprocess.Popen(['nonexisting_i_hope'], |
| 524 | stdout=subprocess.PIPE, |
| 525 | stderr=subprocess.PIPE) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 526 | if c.exception.errno != 2: # ignore "no such file" |
| 527 | raise c.exception |
Georg Brandl | f3715d2 | 2009-02-14 17:01:36 +0000 | [diff] [blame] | 528 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 529 | |
| 530 | # context manager |
| 531 | class _SuppressCoreFiles(object): |
| 532 | """Try to prevent core files from being created.""" |
| 533 | old_limit = None |
| 534 | |
| 535 | def __enter__(self): |
| 536 | """Try to save previous ulimit, then set it to (0, 0).""" |
| 537 | try: |
| 538 | import resource |
| 539 | self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) |
| 540 | resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) |
| 541 | except (ImportError, ValueError, resource.error): |
| 542 | pass |
| 543 | |
| 544 | def __exit__(self, *args): |
| 545 | """Return core file behavior to default.""" |
| 546 | if self.old_limit is None: |
| 547 | return |
| 548 | try: |
| 549 | import resource |
| 550 | resource.setrlimit(resource.RLIMIT_CORE, self.old_limit) |
| 551 | except (ImportError, ValueError, resource.error): |
| 552 | pass |
| 553 | |
| 554 | |
| 555 | @unittest.skipIf(sys.platform == "win32", "POSIX specific tests") |
| 556 | class POSIXProcessTestCase(unittest.TestCase): |
| 557 | def setUp(self): |
| 558 | # Try to minimize the number of children we have so this test |
| 559 | # doesn't crash on some buildbots (Alphas in particular). |
| 560 | test_support.reap_children() |
| 561 | |
| 562 | def test_exceptions(self): |
| 563 | # caught & re-raised exceptions |
| 564 | with self.assertRaises(OSError) as c: |
| 565 | p = subprocess.Popen([sys.executable, "-c", ""], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 566 | cwd="/this/path/does/not/exist") |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 567 | # The attribute child_traceback should contain "os.chdir" somewhere. |
| 568 | self.assertIn("os.chdir", c.exception.child_traceback) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 569 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 570 | def test_run_abort(self): |
| 571 | # returncode handles signal termination |
| 572 | with _SuppressCoreFiles(): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 573 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 574 | "import os; os.abort()"]) |
| 575 | p.wait() |
| 576 | self.assertEqual(-p.returncode, signal.SIGABRT) |
| 577 | |
| 578 | def test_preexec(self): |
| 579 | # preexec function |
| 580 | p = subprocess.Popen([sys.executable, "-c", |
| 581 | "import sys, os;" |
| 582 | "sys.stdout.write(os.getenv('FRUIT'))"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 583 | stdout=subprocess.PIPE, |
| 584 | preexec_fn=lambda: os.putenv("FRUIT", "apple")) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 585 | self.assertEqual(p.stdout.read(), "apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 586 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 587 | def test_args_string(self): |
| 588 | # args is a string |
| 589 | f, fname = mkstemp() |
| 590 | os.write(f, "#!/bin/sh\n") |
| 591 | os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 592 | sys.executable) |
| 593 | os.close(f) |
| 594 | os.chmod(fname, 0o700) |
| 595 | p = subprocess.Popen(fname) |
| 596 | p.wait() |
| 597 | os.remove(fname) |
| 598 | self.assertEqual(p.returncode, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 599 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 600 | def test_invalid_args(self): |
| 601 | # invalid arguments should raise ValueError |
| 602 | self.assertRaises(ValueError, subprocess.call, |
| 603 | [sys.executable, "-c", |
| 604 | "import sys; sys.exit(47)"], |
| 605 | startupinfo=47) |
| 606 | self.assertRaises(ValueError, subprocess.call, |
| 607 | [sys.executable, "-c", |
| 608 | "import sys; sys.exit(47)"], |
| 609 | creationflags=47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 610 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 611 | def test_shell_sequence(self): |
| 612 | # Run command through the shell (sequence) |
| 613 | newenv = os.environ.copy() |
| 614 | newenv["FRUIT"] = "apple" |
| 615 | p = subprocess.Popen(["echo $FRUIT"], shell=1, |
| 616 | stdout=subprocess.PIPE, |
| 617 | env=newenv) |
| 618 | self.assertEqual(p.stdout.read().strip(), "apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 619 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 620 | def test_shell_string(self): |
| 621 | # Run command through the shell (string) |
| 622 | newenv = os.environ.copy() |
| 623 | newenv["FRUIT"] = "apple" |
| 624 | p = subprocess.Popen("echo $FRUIT", shell=1, |
| 625 | stdout=subprocess.PIPE, |
| 626 | env=newenv) |
| 627 | self.assertEqual(p.stdout.read().strip(), "apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 628 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 629 | def test_call_string(self): |
| 630 | # call() function with string argument on UNIX |
| 631 | f, fname = mkstemp() |
| 632 | os.write(f, "#!/bin/sh\n") |
| 633 | os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 634 | sys.executable) |
| 635 | os.close(f) |
| 636 | os.chmod(fname, 0700) |
| 637 | rc = subprocess.call(fname) |
| 638 | os.remove(fname) |
| 639 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 640 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 641 | @unittest.skip("See issue #2777") |
| 642 | def test_send_signal(self): |
| 643 | p = subprocess.Popen([sys.executable, "-c", "input()"]) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 644 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 645 | self.assertIs(p.poll(), None) |
| 646 | p.send_signal(signal.SIGINT) |
Florent Xicluna | 78fd521 | 2010-02-27 21:15:27 +0000 | [diff] [blame] | 647 | self.assertNotEqual(p.wait(), 0) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 648 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 649 | @unittest.skip("See issue #2777") |
| 650 | def test_kill(self): |
| 651 | p = subprocess.Popen([sys.executable, "-c", "input()"]) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 652 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 653 | self.assertIs(p.poll(), None) |
| 654 | p.kill() |
| 655 | self.assertEqual(p.wait(), -signal.SIGKILL) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 656 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 657 | @unittest.skip("See issue #2777") |
| 658 | def test_terminate(self): |
| 659 | p = subprocess.Popen([sys.executable, "-c", "input()"]) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 660 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 661 | self.assertIs(p.poll(), None) |
| 662 | p.terminate() |
| 663 | self.assertEqual(p.wait(), -signal.SIGTERM) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 664 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 665 | |
| 666 | @unittest.skipUnless(sys.platform == "win32", "Windows specific tests") |
| 667 | class Win32ProcessTestCase(unittest.TestCase): |
| 668 | def setUp(self): |
| 669 | # Try to minimize the number of children we have so this test |
| 670 | # doesn't crash on some buildbots (Alphas in particular). |
| 671 | test_support.reap_children() |
| 672 | |
| 673 | def test_startupinfo(self): |
| 674 | # startupinfo argument |
| 675 | # We uses hardcoded constants, because we do not want to |
| 676 | # depend on win32all. |
| 677 | STARTF_USESHOWWINDOW = 1 |
| 678 | SW_MAXIMIZE = 3 |
| 679 | startupinfo = subprocess.STARTUPINFO() |
| 680 | startupinfo.dwFlags = STARTF_USESHOWWINDOW |
| 681 | startupinfo.wShowWindow = SW_MAXIMIZE |
| 682 | # Since Python is a console process, it won't be affected |
| 683 | # by wShowWindow, but the argument should be silently |
| 684 | # ignored |
| 685 | subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 686 | startupinfo=startupinfo) |
| 687 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 688 | def test_creationflags(self): |
| 689 | # creationflags argument |
| 690 | CREATE_NEW_CONSOLE = 16 |
| 691 | sys.stderr.write(" a DOS box should flash briefly ...\n") |
| 692 | subprocess.call(sys.executable + |
| 693 | ' -c "import time; time.sleep(0.25)"', |
| 694 | creationflags=CREATE_NEW_CONSOLE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 695 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 696 | def test_invalid_args(self): |
| 697 | # invalid arguments should raise ValueError |
| 698 | self.assertRaises(ValueError, subprocess.call, |
| 699 | [sys.executable, "-c", |
| 700 | "import sys; sys.exit(47)"], |
| 701 | preexec_fn=lambda: 1) |
| 702 | self.assertRaises(ValueError, subprocess.call, |
| 703 | [sys.executable, "-c", |
| 704 | "import sys; sys.exit(47)"], |
| 705 | stdout=subprocess.PIPE, |
| 706 | close_fds=True) |
| 707 | |
| 708 | def test_close_fds(self): |
| 709 | # close file descriptors |
| 710 | rc = subprocess.call([sys.executable, "-c", |
| 711 | "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 712 | close_fds=True) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 713 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 714 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 715 | def test_shell_sequence(self): |
| 716 | # Run command through the shell (sequence) |
| 717 | newenv = os.environ.copy() |
| 718 | newenv["FRUIT"] = "physalis" |
| 719 | p = subprocess.Popen(["set"], shell=1, |
| 720 | stdout=subprocess.PIPE, |
| 721 | env=newenv) |
| 722 | self.assertIn("physalis", p.stdout.read()) |
Peter Astrand | 81a191b | 2007-05-26 22:18:20 +0000 | [diff] [blame] | 723 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 724 | def test_shell_string(self): |
| 725 | # Run command through the shell (string) |
| 726 | newenv = os.environ.copy() |
| 727 | newenv["FRUIT"] = "physalis" |
| 728 | p = subprocess.Popen("set", shell=1, |
| 729 | stdout=subprocess.PIPE, |
| 730 | env=newenv) |
| 731 | self.assertIn("physalis", p.stdout.read()) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 732 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 733 | def test_call_string(self): |
| 734 | # call() function with string argument on Windows |
| 735 | rc = subprocess.call(sys.executable + |
| 736 | ' -c "import sys; sys.exit(47)"') |
| 737 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 738 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 739 | @unittest.skip("See issue #2777") |
| 740 | def test_send_signal(self): |
| 741 | p = subprocess.Popen([sys.executable, "-c", "input()"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 742 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 743 | self.assertIs(p.poll(), None) |
| 744 | p.send_signal(signal.SIGTERM) |
Florent Xicluna | 78fd521 | 2010-02-27 21:15:27 +0000 | [diff] [blame] | 745 | self.assertNotEqual(p.wait(), 0) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 746 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 747 | @unittest.skip("See issue #2777") |
| 748 | def test_kill(self): |
| 749 | p = subprocess.Popen([sys.executable, "-c", "input()"]) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 750 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 751 | self.assertIs(p.poll(), None) |
| 752 | p.kill() |
Florent Xicluna | 78fd521 | 2010-02-27 21:15:27 +0000 | [diff] [blame] | 753 | self.assertNotEqual(p.wait(), 0) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 754 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 755 | @unittest.skip("See issue #2777") |
| 756 | def test_terminate(self): |
| 757 | p = subprocess.Popen([sys.executable, "-c", "input()"]) |
Christian Heimes | e74c8f2 | 2008-04-19 02:23:57 +0000 | [diff] [blame] | 758 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 759 | self.assertIs(p.poll(), None) |
| 760 | p.terminate() |
Florent Xicluna | 78fd521 | 2010-02-27 21:15:27 +0000 | [diff] [blame] | 761 | self.assertNotEqual(p.wait(), 0) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 762 | |
Gregory P. Smith | dd7ca24 | 2009-07-04 01:49:29 +0000 | [diff] [blame] | 763 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 764 | @unittest.skipUnless(getattr(subprocess, '_has_poll', False), |
| 765 | "poll system call not supported") |
| 766 | class ProcessTestCaseNoPoll(ProcessTestCase): |
| 767 | def setUp(self): |
| 768 | subprocess._has_poll = False |
| 769 | ProcessTestCase.setUp(self) |
Gregory P. Smith | dd7ca24 | 2009-07-04 01:49:29 +0000 | [diff] [blame] | 770 | |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 771 | def tearDown(self): |
| 772 | subprocess._has_poll = True |
| 773 | ProcessTestCase.tearDown(self) |
Gregory P. Smith | dd7ca24 | 2009-07-04 01:49:29 +0000 | [diff] [blame] | 774 | |
| 775 | |
Gregory P. Smith | cce211f | 2010-03-01 00:05:08 +0000 | [diff] [blame^] | 776 | class HelperFunctionTests(unittest.TestCase): |
| 777 | def test_eintr_retry_call(self): |
| 778 | record_calls = [] |
| 779 | def fake_os_func(*args): |
| 780 | record_calls.append(args) |
| 781 | if len(record_calls) == 2: |
| 782 | raise OSError(errno.EINTR, "fake interrupted system call") |
| 783 | return tuple(reversed(args)) |
| 784 | |
| 785 | self.assertEqual((999, 256), |
| 786 | subprocess._eintr_retry_call(fake_os_func, 256, 999)) |
| 787 | self.assertEqual([(256, 999)], record_calls) |
| 788 | # This time there will be an EINTR so it will loop once. |
| 789 | self.assertEqual((666,), |
| 790 | subprocess._eintr_retry_call(fake_os_func, 666)) |
| 791 | self.assertEqual([(256, 999), (666,), (666,)], record_calls) |
| 792 | |
| 793 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 794 | def test_main(): |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 795 | unit_tests = (ProcessTestCase, |
| 796 | POSIXProcessTestCase, |
| 797 | Win32ProcessTestCase, |
Gregory P. Smith | cce211f | 2010-03-01 00:05:08 +0000 | [diff] [blame^] | 798 | ProcessTestCaseNoPoll, |
| 799 | HelperFunctionTests) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 800 | |
Gregory P. Smith | dd7ca24 | 2009-07-04 01:49:29 +0000 | [diff] [blame] | 801 | test_support.run_unittest(*unit_tests) |
Florent Xicluna | 98e3fc3 | 2010-02-27 19:20:50 +0000 | [diff] [blame] | 802 | test_support.reap_children() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 803 | |
| 804 | if __name__ == "__main__": |
| 805 | test_main() |