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