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