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