Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 3 | import subprocess |
| 4 | import sys |
| 5 | import signal |
| 6 | import os |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +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 | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 11 | import sysconfig |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 12 | try: |
| 13 | import gc |
| 14 | except ImportError: |
| 15 | gc = None |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 16 | |
| 17 | mswindows = (sys.platform == "win32") |
| 18 | |
| 19 | # |
| 20 | # Depends on the following external programs: Python |
| 21 | # |
| 22 | |
| 23 | if mswindows: |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 24 | SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), ' |
| 25 | 'os.O_BINARY);') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 26 | else: |
| 27 | SETBINARY = '' |
| 28 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 29 | |
| 30 | try: |
| 31 | mkstemp = tempfile.mkstemp |
| 32 | except AttributeError: |
| 33 | # tempfile.mkstemp is not available |
| 34 | def mkstemp(): |
| 35 | """Replacement for mkstemp, calling mktemp.""" |
| 36 | fname = tempfile.mktemp() |
| 37 | return os.open(fname, os.O_RDWR|os.O_CREAT), fname |
| 38 | |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 39 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 40 | class ProcessTestCase(unittest.TestCase): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 41 | def setUp(self): |
| 42 | # Try to minimize the number of children we have so this test |
| 43 | # doesn't crash on some buildbots (Alphas in particular). |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 44 | support.reap_children() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 45 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 46 | def tearDown(self): |
| 47 | for inst in subprocess._active: |
| 48 | inst.wait() |
| 49 | subprocess._cleanup() |
| 50 | self.assertFalse(subprocess._active, "subprocess._active not empty") |
| 51 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 52 | def assertStderrEqual(self, stderr, expected, msg=None): |
| 53 | # In a debug build, stuff like "[6580 refs]" is printed to stderr at |
| 54 | # shutdown time. That frustrates tests trying to check stderr produced |
| 55 | # from a spawned Python process. |
| 56 | actual = re.sub("\[\d+ refs\]\r?\n?$", "", stderr.decode()).encode() |
| 57 | self.assertEqual(actual, expected, msg) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 58 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 59 | def test_call_seq(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 60 | # call() function with sequence argument |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 61 | rc = subprocess.call([sys.executable, "-c", |
| 62 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 63 | self.assertEqual(rc, 47) |
| 64 | |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 65 | def test_check_call_zero(self): |
| 66 | # check_call() function with zero return code |
| 67 | rc = subprocess.check_call([sys.executable, "-c", |
| 68 | "import sys; sys.exit(0)"]) |
| 69 | self.assertEqual(rc, 0) |
| 70 | |
| 71 | def test_check_call_nonzero(self): |
| 72 | # check_call() function with non-zero return code |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 73 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 74 | subprocess.check_call([sys.executable, "-c", |
| 75 | "import sys; sys.exit(47)"]) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 76 | self.assertEqual(c.exception.returncode, 47) |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 77 | |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 78 | def test_check_output(self): |
| 79 | # check_output() function with zero return code |
| 80 | output = subprocess.check_output( |
| 81 | [sys.executable, "-c", "print('BDFL')"]) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 82 | self.assertIn(b'BDFL', output) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 83 | |
| 84 | def test_check_output_nonzero(self): |
| 85 | # check_call() function with non-zero return code |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 86 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 87 | subprocess.check_output( |
| 88 | [sys.executable, "-c", "import sys; sys.exit(5)"]) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 89 | self.assertEqual(c.exception.returncode, 5) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 90 | |
| 91 | def test_check_output_stderr(self): |
| 92 | # check_output() function stderr redirected to stdout |
| 93 | output = subprocess.check_output( |
| 94 | [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"], |
| 95 | stderr=subprocess.STDOUT) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 96 | self.assertIn(b'BDFL', output) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 97 | |
| 98 | def test_check_output_stdout_arg(self): |
| 99 | # check_output() function stderr redirected to stdout |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 100 | with self.assertRaises(ValueError) as c: |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 101 | output = subprocess.check_output( |
| 102 | [sys.executable, "-c", "print('will not be run')"], |
| 103 | stdout=sys.stdout) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 104 | self.fail("Expected ValueError when stdout arg supplied.") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 105 | self.assertIn('stdout', c.exception.args[0]) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 106 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 107 | def test_call_kwargs(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 108 | # call() function with keyword args |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 109 | newenv = os.environ.copy() |
| 110 | newenv["FRUIT"] = "banana" |
| 111 | rc = subprocess.call([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 112 | 'import sys, os;' |
| 113 | 'sys.exit(os.getenv("FRUIT")=="banana")'], |
| 114 | env=newenv) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 115 | self.assertEqual(rc, 1) |
| 116 | |
| 117 | def test_stdin_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 118 | # .stdin is None when not redirected |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 119 | p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 120 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 121 | p.wait() |
| 122 | self.assertEqual(p.stdin, None) |
| 123 | |
| 124 | def test_stdout_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 125 | # .stdout is None when not redirected |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 126 | p = subprocess.Popen([sys.executable, "-c", |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 127 | 'print(" this bit of output is from a ' |
Tim Peters | 4052fe5 | 2004-10-13 03:29:54 +0000 | [diff] [blame] | 128 | 'test of stdout in a different ' |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 129 | 'process ...")'], |
Tim Peters | 4052fe5 | 2004-10-13 03:29:54 +0000 | [diff] [blame] | 130 | stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 131 | p.wait() |
| 132 | self.assertEqual(p.stdout, None) |
| 133 | |
| 134 | def test_stderr_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 135 | # .stderr is None when not redirected |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 136 | p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 137 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 138 | p.wait() |
| 139 | self.assertEqual(p.stderr, None) |
| 140 | |
Ezio Melotti | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 141 | def test_executable_with_cwd(self): |
Florent Xicluna | 1d1ab97 | 2010-03-11 01:53:10 +0000 | [diff] [blame] | 142 | python_dir = os.path.dirname(os.path.realpath(sys.executable)) |
Ezio Melotti | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 143 | p = subprocess.Popen(["somethingyoudonthave", "-c", |
| 144 | "import sys; sys.exit(47)"], |
| 145 | executable=sys.executable, cwd=python_dir) |
| 146 | p.wait() |
| 147 | self.assertEqual(p.returncode, 47) |
| 148 | |
| 149 | @unittest.skipIf(sysconfig.is_python_build(), |
| 150 | "need an installed Python. See #7774") |
| 151 | def test_executable_without_cwd(self): |
| 152 | # For a normal installation, it should work without 'cwd' |
| 153 | # argument. For test runs in the build directory, see #7774. |
| 154 | p = subprocess.Popen(["somethingyoudonthave", "-c", |
| 155 | "import sys; sys.exit(47)"], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 156 | executable=sys.executable) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 157 | p.wait() |
| 158 | self.assertEqual(p.returncode, 47) |
| 159 | |
| 160 | def test_stdin_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 161 | # stdin redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 162 | p = subprocess.Popen([sys.executable, "-c", |
| 163 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 164 | stdin=subprocess.PIPE) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 165 | p.stdin.write(b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 166 | p.stdin.close() |
| 167 | p.wait() |
| 168 | self.assertEqual(p.returncode, 1) |
| 169 | |
| 170 | def test_stdin_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 171 | # stdin is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 172 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 173 | d = tf.fileno() |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 174 | os.write(d, b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 175 | os.lseek(d, 0, 0) |
| 176 | p = subprocess.Popen([sys.executable, "-c", |
| 177 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 178 | stdin=d) |
| 179 | p.wait() |
| 180 | self.assertEqual(p.returncode, 1) |
| 181 | |
| 182 | def test_stdin_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 183 | # stdin is set to open file object |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 184 | tf = tempfile.TemporaryFile() |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 185 | tf.write(b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 186 | tf.seek(0) |
| 187 | p = subprocess.Popen([sys.executable, "-c", |
| 188 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 189 | stdin=tf) |
| 190 | p.wait() |
| 191 | self.assertEqual(p.returncode, 1) |
| 192 | |
| 193 | def test_stdout_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 194 | # stdout redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 195 | p = subprocess.Popen([sys.executable, "-c", |
| 196 | 'import sys; sys.stdout.write("orange")'], |
| 197 | stdout=subprocess.PIPE) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 198 | self.assertEqual(p.stdout.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 199 | |
| 200 | def test_stdout_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 201 | # stdout is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 202 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 203 | d = tf.fileno() |
| 204 | p = subprocess.Popen([sys.executable, "-c", |
| 205 | 'import sys; sys.stdout.write("orange")'], |
| 206 | stdout=d) |
| 207 | p.wait() |
| 208 | os.lseek(d, 0, 0) |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 209 | self.assertEqual(os.read(d, 1024), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 210 | |
| 211 | def test_stdout_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 212 | # stdout is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 213 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 214 | p = subprocess.Popen([sys.executable, "-c", |
| 215 | 'import sys; sys.stdout.write("orange")'], |
| 216 | stdout=tf) |
| 217 | p.wait() |
| 218 | tf.seek(0) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 219 | self.assertEqual(tf.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 220 | |
| 221 | def test_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 222 | # stderr redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 223 | p = subprocess.Popen([sys.executable, "-c", |
| 224 | 'import sys; sys.stderr.write("strawberry")'], |
| 225 | stderr=subprocess.PIPE) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 226 | self.assertStderrEqual(p.stderr.read(), b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 227 | |
| 228 | def test_stderr_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 229 | # stderr is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 230 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 231 | d = tf.fileno() |
| 232 | p = subprocess.Popen([sys.executable, "-c", |
| 233 | 'import sys; sys.stderr.write("strawberry")'], |
| 234 | stderr=d) |
| 235 | p.wait() |
| 236 | os.lseek(d, 0, 0) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 237 | self.assertStderrEqual(os.read(d, 1024), b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 238 | |
| 239 | def test_stderr_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 240 | # stderr is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 241 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 242 | p = subprocess.Popen([sys.executable, "-c", |
| 243 | 'import sys; sys.stderr.write("strawberry")'], |
| 244 | stderr=tf) |
| 245 | p.wait() |
| 246 | tf.seek(0) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 247 | self.assertStderrEqual(tf.read(), b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 248 | |
| 249 | def test_stdout_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 250 | # capture stdout and stderr to the same pipe |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 251 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 252 | 'import sys;' |
| 253 | 'sys.stdout.write("apple");' |
| 254 | 'sys.stdout.flush();' |
| 255 | 'sys.stderr.write("orange")'], |
| 256 | stdout=subprocess.PIPE, |
| 257 | stderr=subprocess.STDOUT) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 258 | self.assertStderrEqual(p.stdout.read(), b"appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 259 | |
| 260 | def test_stdout_stderr_file(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 261 | # capture stdout and stderr to the same open file |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 262 | tf = tempfile.TemporaryFile() |
| 263 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 264 | 'import sys;' |
| 265 | 'sys.stdout.write("apple");' |
| 266 | 'sys.stdout.flush();' |
| 267 | 'sys.stderr.write("orange")'], |
| 268 | stdout=tf, |
| 269 | stderr=tf) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 270 | p.wait() |
| 271 | tf.seek(0) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 272 | self.assertStderrEqual(tf.read(), b"appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 273 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 274 | def test_stdout_filedes_of_stdout(self): |
| 275 | # stdout is set to 1 (#1531862). |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 276 | cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), b'.\n'))" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 277 | rc = subprocess.call([sys.executable, "-c", cmd], stdout=1) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 278 | self.assertEqual(rc, 2) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 279 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 280 | def test_cwd(self): |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 281 | tmpdir = tempfile.gettempdir() |
Peter Astrand | 195404f | 2004-11-12 15:51:48 +0000 | [diff] [blame] | 282 | # We cannot use os.path.realpath to canonicalize the path, |
| 283 | # since it doesn't expand Tru64 {memb} strings. See bug 1063571. |
| 284 | cwd = os.getcwd() |
| 285 | os.chdir(tmpdir) |
| 286 | tmpdir = os.getcwd() |
| 287 | os.chdir(cwd) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 288 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 289 | 'import sys,os;' |
| 290 | 'sys.stdout.write(os.getcwd())'], |
| 291 | stdout=subprocess.PIPE, |
| 292 | cwd=tmpdir) |
Fredrik Lundh | 59c0559 | 2004-10-13 06:55:40 +0000 | [diff] [blame] | 293 | normcase = os.path.normcase |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 294 | self.assertEqual(normcase(p.stdout.read().decode("utf-8")), |
| 295 | normcase(tmpdir)) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 296 | |
| 297 | def test_env(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 298 | newenv = os.environ.copy() |
| 299 | newenv["FRUIT"] = "orange" |
| 300 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 301 | 'import sys,os;' |
| 302 | 'sys.stdout.write(os.getenv("FRUIT"))'], |
| 303 | stdout=subprocess.PIPE, |
| 304 | env=newenv) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 305 | self.assertEqual(p.stdout.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 306 | |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 307 | def test_communicate_stdin(self): |
| 308 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 309 | 'import sys;' |
| 310 | 'sys.exit(sys.stdin.read() == "pear")'], |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 311 | stdin=subprocess.PIPE) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 312 | p.communicate(b"pear") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 313 | self.assertEqual(p.returncode, 1) |
| 314 | |
| 315 | def test_communicate_stdout(self): |
| 316 | p = subprocess.Popen([sys.executable, "-c", |
| 317 | 'import sys; sys.stdout.write("pineapple")'], |
| 318 | stdout=subprocess.PIPE) |
| 319 | (stdout, stderr) = p.communicate() |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 320 | self.assertEqual(stdout, b"pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 321 | self.assertEqual(stderr, None) |
| 322 | |
| 323 | def test_communicate_stderr(self): |
| 324 | p = subprocess.Popen([sys.executable, "-c", |
| 325 | 'import sys; sys.stderr.write("pineapple")'], |
| 326 | stderr=subprocess.PIPE) |
| 327 | (stdout, stderr) = p.communicate() |
| 328 | self.assertEqual(stdout, None) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 329 | self.assertStderrEqual(stderr, b"pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 330 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 331 | def test_communicate(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 332 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 333 | 'import sys,os;' |
| 334 | 'sys.stderr.write("pineapple");' |
| 335 | 'sys.stdout.write(sys.stdin.read())'], |
| 336 | stdin=subprocess.PIPE, |
| 337 | stdout=subprocess.PIPE, |
| 338 | stderr=subprocess.PIPE) |
Georg Brandl | 1abcbf8 | 2008-07-01 19:28:43 +0000 | [diff] [blame] | 339 | (stdout, stderr) = p.communicate(b"banana") |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 340 | self.assertEqual(stdout, b"banana") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 341 | self.assertStderrEqual(stderr, b"pineapple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 342 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 343 | # This test is Linux specific for simplicity to at least have |
| 344 | # some coverage. It is not a platform specific bug. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 345 | @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()), |
| 346 | "Linux specific") |
| 347 | # Test for the fd leak reported in http://bugs.python.org/issue2791. |
| 348 | def test_communicate_pipe_fd_leak(self): |
| 349 | fd_directory = '/proc/%d/fd' % os.getpid() |
| 350 | num_fds_before_popen = len(os.listdir(fd_directory)) |
| 351 | p = subprocess.Popen([sys.executable, "-c", "print()"], |
| 352 | stdout=subprocess.PIPE) |
| 353 | p.communicate() |
| 354 | num_fds_after_communicate = len(os.listdir(fd_directory)) |
| 355 | del p |
| 356 | num_fds_after_destruction = len(os.listdir(fd_directory)) |
| 357 | self.assertEqual(num_fds_before_popen, num_fds_after_destruction) |
| 358 | self.assertEqual(num_fds_before_popen, num_fds_after_communicate) |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 359 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 360 | def test_communicate_returns(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 361 | # communicate() should return None if no redirection is active |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 362 | p = subprocess.Popen([sys.executable, "-c", |
| 363 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 364 | (stdout, stderr) = p.communicate() |
| 365 | self.assertEqual(stdout, None) |
| 366 | self.assertEqual(stderr, None) |
| 367 | |
| 368 | def test_communicate_pipe_buf(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 369 | # communicate() with writes larger than pipe_buf |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 370 | # This test will probably deadlock rather than fail, if |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 371 | # communicate() does not work properly. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 372 | x, y = os.pipe() |
| 373 | if mswindows: |
| 374 | pipe_buf = 512 |
| 375 | else: |
| 376 | pipe_buf = os.fpathconf(x, "PC_PIPE_BUF") |
| 377 | os.close(x) |
| 378 | os.close(y) |
| 379 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 380 | 'import sys,os;' |
| 381 | 'sys.stdout.write(sys.stdin.read(47));' |
| 382 | 'sys.stderr.write("xyz"*%d);' |
| 383 | 'sys.stdout.write(sys.stdin.read())' % pipe_buf], |
| 384 | stdin=subprocess.PIPE, |
| 385 | stdout=subprocess.PIPE, |
| 386 | stderr=subprocess.PIPE) |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 387 | string_to_write = b"abc"*pipe_buf |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 388 | (stdout, stderr) = p.communicate(string_to_write) |
| 389 | self.assertEqual(stdout, string_to_write) |
| 390 | |
| 391 | def test_writes_before_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 392 | # stdin.write before communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 393 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 394 | 'import sys,os;' |
| 395 | 'sys.stdout.write(sys.stdin.read())'], |
| 396 | stdin=subprocess.PIPE, |
| 397 | stdout=subprocess.PIPE, |
| 398 | stderr=subprocess.PIPE) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 399 | p.stdin.write(b"banana") |
| 400 | (stdout, stderr) = p.communicate(b"split") |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 401 | self.assertEqual(stdout, b"bananasplit") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 402 | self.assertStderrEqual(stderr, b"") |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 403 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 404 | def test_universal_newlines(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 405 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 406 | 'import sys,os;' + SETBINARY + |
| 407 | 'sys.stdout.write("line1\\n");' |
| 408 | 'sys.stdout.flush();' |
| 409 | 'sys.stdout.write("line2\\n");' |
| 410 | 'sys.stdout.flush();' |
| 411 | 'sys.stdout.write("line3\\r\\n");' |
| 412 | 'sys.stdout.flush();' |
| 413 | 'sys.stdout.write("line4\\r");' |
| 414 | 'sys.stdout.flush();' |
| 415 | 'sys.stdout.write("\\nline5");' |
| 416 | 'sys.stdout.flush();' |
| 417 | 'sys.stdout.write("\\nline6");'], |
| 418 | stdout=subprocess.PIPE, |
| 419 | universal_newlines=1) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 420 | stdout = p.stdout.read() |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 421 | self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 422 | |
| 423 | def test_universal_newlines_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 424 | # universal newlines through communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 425 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 426 | 'import sys,os;' + SETBINARY + |
| 427 | 'sys.stdout.write("line1\\n");' |
| 428 | 'sys.stdout.flush();' |
| 429 | 'sys.stdout.write("line2\\n");' |
| 430 | 'sys.stdout.flush();' |
| 431 | 'sys.stdout.write("line3\\r\\n");' |
| 432 | 'sys.stdout.flush();' |
| 433 | 'sys.stdout.write("line4\\r");' |
| 434 | 'sys.stdout.flush();' |
| 435 | 'sys.stdout.write("\\nline5");' |
| 436 | 'sys.stdout.flush();' |
| 437 | 'sys.stdout.write("\\nline6");'], |
| 438 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 439 | universal_newlines=1) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 440 | (stdout, stderr) = p.communicate() |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 441 | self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 442 | |
| 443 | def test_no_leaking(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 444 | # Make sure we leak no resources |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 445 | if (not hasattr(support, "is_resource_enabled") or |
| 446 | support.is_resource_enabled("subprocess") and not mswindows): |
Peter Astrand | f7f1bb7 | 2005-03-03 20:47:37 +0000 | [diff] [blame] | 447 | max_handles = 1026 # too much for most UNIX systems |
| 448 | else: |
Tim Peters | eba28be | 2005-03-28 01:08:02 +0000 | [diff] [blame] | 449 | max_handles = 65 |
Fredrik Lundh | 9e29fc5 | 2004-10-13 07:54:54 +0000 | [diff] [blame] | 450 | for i in range(max_handles): |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 451 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 452 | "import sys;" |
| 453 | "sys.stdout.write(sys.stdin.read())"], |
| 454 | stdin=subprocess.PIPE, |
| 455 | stdout=subprocess.PIPE, |
| 456 | stderr=subprocess.PIPE) |
Georg Brandl | 1abcbf8 | 2008-07-01 19:28:43 +0000 | [diff] [blame] | 457 | data = p.communicate(b"lime")[0] |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 458 | self.assertEqual(data, b"lime") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 459 | |
| 460 | |
| 461 | def test_list2cmdline(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 462 | self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), |
| 463 | '"a b c" d e') |
| 464 | self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), |
| 465 | 'ab\\"c \\ d') |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 466 | self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']), |
| 467 | 'ab\\"c " \\\\" d') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 468 | self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']), |
| 469 | 'a\\\\\\b "de fg" h') |
| 470 | self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']), |
| 471 | 'a\\\\\\"b c d') |
| 472 | self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']), |
| 473 | '"a\\\\b c" d e') |
| 474 | self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']), |
| 475 | '"a\\\\b\\ c" d e') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 476 | self.assertEqual(subprocess.list2cmdline(['ab', '']), |
| 477 | 'ab ""') |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 478 | self.assertEqual(subprocess.list2cmdline(['echo', 'foo|bar']), |
| 479 | 'echo "foo|bar"') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 480 | |
| 481 | |
| 482 | def test_poll(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 483 | p = subprocess.Popen([sys.executable, |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 484 | "-c", "import time; time.sleep(1)"]) |
| 485 | count = 0 |
| 486 | while p.poll() is None: |
| 487 | time.sleep(0.1) |
| 488 | count += 1 |
| 489 | # We expect that the poll loop probably went around about 10 times, |
| 490 | # but, based on system scheduling we can't control, it's possible |
| 491 | # poll() never returned None. It "should be" very rare that it |
| 492 | # didn't go around at least twice. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 493 | self.assertGreaterEqual(count, 2) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 494 | # Subsequent invocations should just return the returncode |
| 495 | self.assertEqual(p.poll(), 0) |
| 496 | |
| 497 | |
| 498 | def test_wait(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 499 | p = subprocess.Popen([sys.executable, |
| 500 | "-c", "import time; time.sleep(2)"]) |
| 501 | self.assertEqual(p.wait(), 0) |
| 502 | # Subsequent invocations should just return the returncode |
| 503 | self.assertEqual(p.wait(), 0) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 504 | |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 505 | |
| 506 | def test_invalid_bufsize(self): |
| 507 | # an invalid type of the bufsize argument should raise |
| 508 | # TypeError. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 509 | with self.assertRaises(TypeError): |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 510 | subprocess.Popen([sys.executable, "-c", "pass"], "orange") |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 511 | |
Guido van Rossum | 46a05a7 | 2007-06-07 21:56:45 +0000 | [diff] [blame] | 512 | def test_bufsize_is_none(self): |
| 513 | # bufsize=None should be the same as bufsize=0. |
| 514 | p = subprocess.Popen([sys.executable, "-c", "pass"], None) |
| 515 | self.assertEqual(p.wait(), 0) |
| 516 | # Again with keyword arg |
| 517 | p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None) |
| 518 | self.assertEqual(p.wait(), 0) |
| 519 | |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 520 | def test_leaking_fds_on_error(self): |
| 521 | # see bug #5179: Popen leaks file descriptors to PIPEs if |
| 522 | # the child fails to execute; this will eventually exhaust |
| 523 | # the maximum number of open fds. 1024 seems a very common |
| 524 | # value for that limit, but Windows has 2048, so we loop |
| 525 | # 1024 times (each call leaked two fds). |
| 526 | for i in range(1024): |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 527 | # Windows raises IOError. Others raise OSError. |
| 528 | with self.assertRaises(EnvironmentError) as c: |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 529 | subprocess.Popen(['nonexisting_i_hope'], |
| 530 | stdout=subprocess.PIPE, |
| 531 | stderr=subprocess.PIPE) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 532 | if c.exception.errno != 2: # ignore "no such file" |
| 533 | raise c.exception |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 534 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 535 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 536 | # context manager |
| 537 | class _SuppressCoreFiles(object): |
| 538 | """Try to prevent core files from being created.""" |
| 539 | old_limit = None |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 540 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 541 | def __enter__(self): |
| 542 | """Try to save previous ulimit, then set it to (0, 0).""" |
| 543 | try: |
| 544 | import resource |
| 545 | self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) |
| 546 | resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) |
| 547 | except (ImportError, ValueError, resource.error): |
| 548 | pass |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 549 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 550 | def __exit__(self, *args): |
| 551 | """Return core file behavior to default.""" |
| 552 | if self.old_limit is None: |
| 553 | return |
| 554 | try: |
| 555 | import resource |
| 556 | resource.setrlimit(resource.RLIMIT_CORE, self.old_limit) |
| 557 | except (ImportError, ValueError, resource.error): |
| 558 | pass |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 559 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 560 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 561 | @unittest.skipIf(mswindows, "POSIX specific tests") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 562 | class POSIXProcessTestCase(unittest.TestCase): |
| 563 | def setUp(self): |
| 564 | # Try to minimize the number of children we have so this test |
| 565 | # doesn't crash on some buildbots (Alphas in particular). |
| 566 | support.reap_children() |
| 567 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 568 | def tearDown(self): |
| 569 | for inst in subprocess._active: |
| 570 | inst.wait() |
| 571 | subprocess._cleanup() |
| 572 | self.assertFalse(subprocess._active, "subprocess._active not empty") |
| 573 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 574 | def test_exceptions(self): |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 575 | nonexistent_dir = "/_this/pa.th/does/not/exist" |
| 576 | try: |
| 577 | os.chdir(nonexistent_dir) |
| 578 | except OSError as e: |
| 579 | # This avoids hard coding the errno value or the OS perror() |
| 580 | # string and instead capture the exception that we want to see |
| 581 | # below for comparison. |
| 582 | desired_exception = e |
| 583 | else: |
| 584 | self.fail("chdir to nonexistant directory %s succeeded." % |
| 585 | nonexistent_dir) |
| 586 | |
| 587 | # Error in the child re-raised in the parent. |
| 588 | try: |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 589 | p = subprocess.Popen([sys.executable, "-c", ""], |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 590 | cwd=nonexistent_dir) |
| 591 | except OSError as e: |
| 592 | # Test that the child process chdir failure actually makes |
| 593 | # it up to the parent process as the correct exception. |
| 594 | self.assertEqual(desired_exception.errno, e.errno) |
| 595 | self.assertEqual(desired_exception.strerror, e.strerror) |
| 596 | else: |
| 597 | self.fail("Expected OSError: %s" % desired_exception) |
| 598 | |
| 599 | def test_restore_signals(self): |
| 600 | # Code coverage for both values of restore_signals to make sure it |
| 601 | # at least does not blow up. |
| 602 | # A test for behavior would be complex. Contributions welcome. |
| 603 | subprocess.call([sys.executable, "-c", ""], restore_signals=True) |
| 604 | subprocess.call([sys.executable, "-c", ""], restore_signals=False) |
| 605 | |
| 606 | def test_start_new_session(self): |
| 607 | # For code coverage of calling setsid(). We don't care if we get an |
| 608 | # EPERM error from it depending on the test execution environment, that |
| 609 | # still indicates that it was called. |
| 610 | try: |
| 611 | output = subprocess.check_output( |
| 612 | [sys.executable, "-c", |
| 613 | "import os; print(os.getpgid(os.getpid()))"], |
| 614 | start_new_session=True) |
| 615 | except OSError as e: |
| 616 | if e.errno != errno.EPERM: |
| 617 | raise |
| 618 | else: |
| 619 | parent_pgid = os.getpgid(os.getpid()) |
| 620 | child_pgid = int(output) |
| 621 | self.assertNotEqual(parent_pgid, child_pgid) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 622 | |
| 623 | def test_run_abort(self): |
| 624 | # returncode handles signal termination |
| 625 | with _SuppressCoreFiles(): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 626 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 627 | 'import os; os.abort()']) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 628 | p.wait() |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 629 | self.assertEqual(-p.returncode, signal.SIGABRT) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 630 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 631 | def test_preexec(self): |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 632 | # DISCLAIMER: Setting environment variables is *not* a good use |
| 633 | # of a preexec_fn. This is merely a test. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 634 | p = subprocess.Popen([sys.executable, "-c", |
| 635 | 'import sys,os;' |
| 636 | 'sys.stdout.write(os.getenv("FRUIT"))'], |
| 637 | stdout=subprocess.PIPE, |
| 638 | preexec_fn=lambda: os.putenv("FRUIT", "apple")) |
| 639 | self.assertEqual(p.stdout.read(), b"apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 640 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 641 | def test_preexec_exception(self): |
| 642 | def raise_it(): |
| 643 | raise ValueError("What if two swallows carried a coconut?") |
| 644 | try: |
| 645 | p = subprocess.Popen([sys.executable, "-c", ""], |
| 646 | preexec_fn=raise_it) |
| 647 | except RuntimeError as e: |
| 648 | self.assertTrue( |
| 649 | subprocess._posixsubprocess, |
| 650 | "Expected a ValueError from the preexec_fn") |
| 651 | except ValueError as e: |
| 652 | self.assertIn("coconut", e.args[0]) |
| 653 | else: |
| 654 | self.fail("Exception raised by preexec_fn did not make it " |
| 655 | "to the parent process.") |
| 656 | |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 657 | @unittest.skipUnless(gc, "Requires a gc module.") |
| 658 | def test_preexec_gc_module_failure(self): |
| 659 | # This tests the code that disables garbage collection if the child |
| 660 | # process will execute any Python. |
| 661 | def raise_runtime_error(): |
| 662 | raise RuntimeError("this shouldn't escape") |
| 663 | enabled = gc.isenabled() |
| 664 | orig_gc_disable = gc.disable |
| 665 | orig_gc_isenabled = gc.isenabled |
| 666 | try: |
| 667 | gc.disable() |
| 668 | self.assertFalse(gc.isenabled()) |
| 669 | subprocess.call([sys.executable, '-c', ''], |
| 670 | preexec_fn=lambda: None) |
| 671 | self.assertFalse(gc.isenabled(), |
| 672 | "Popen enabled gc when it shouldn't.") |
| 673 | |
| 674 | gc.enable() |
| 675 | self.assertTrue(gc.isenabled()) |
| 676 | subprocess.call([sys.executable, '-c', ''], |
| 677 | preexec_fn=lambda: None) |
| 678 | self.assertTrue(gc.isenabled(), "Popen left gc disabled.") |
| 679 | |
| 680 | gc.disable = raise_runtime_error |
| 681 | self.assertRaises(RuntimeError, subprocess.Popen, |
| 682 | [sys.executable, '-c', ''], |
| 683 | preexec_fn=lambda: None) |
| 684 | |
| 685 | del gc.isenabled # force an AttributeError |
| 686 | self.assertRaises(AttributeError, subprocess.Popen, |
| 687 | [sys.executable, '-c', ''], |
| 688 | preexec_fn=lambda: None) |
| 689 | finally: |
| 690 | gc.disable = orig_gc_disable |
| 691 | gc.isenabled = orig_gc_isenabled |
| 692 | if not enabled: |
| 693 | gc.disable() |
| 694 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 695 | def test_args_string(self): |
| 696 | # args is a string |
| 697 | fd, fname = mkstemp() |
| 698 | # reopen in text mode |
| 699 | with open(fd, "w") as fobj: |
| 700 | fobj.write("#!/bin/sh\n") |
| 701 | fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 702 | sys.executable) |
| 703 | os.chmod(fname, 0o700) |
| 704 | p = subprocess.Popen(fname) |
| 705 | p.wait() |
| 706 | os.remove(fname) |
| 707 | self.assertEqual(p.returncode, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 708 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 709 | def test_invalid_args(self): |
| 710 | # invalid arguments should raise ValueError |
| 711 | self.assertRaises(ValueError, subprocess.call, |
| 712 | [sys.executable, "-c", |
| 713 | "import sys; sys.exit(47)"], |
| 714 | startupinfo=47) |
| 715 | self.assertRaises(ValueError, subprocess.call, |
| 716 | [sys.executable, "-c", |
| 717 | "import sys; sys.exit(47)"], |
| 718 | creationflags=47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 719 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 720 | def test_shell_sequence(self): |
| 721 | # Run command through the shell (sequence) |
| 722 | newenv = os.environ.copy() |
| 723 | newenv["FRUIT"] = "apple" |
| 724 | p = subprocess.Popen(["echo $FRUIT"], shell=1, |
| 725 | stdout=subprocess.PIPE, |
| 726 | env=newenv) |
| 727 | self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 728 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 729 | def test_shell_string(self): |
| 730 | # Run command through the shell (string) |
| 731 | newenv = os.environ.copy() |
| 732 | newenv["FRUIT"] = "apple" |
| 733 | p = subprocess.Popen("echo $FRUIT", shell=1, |
| 734 | stdout=subprocess.PIPE, |
| 735 | env=newenv) |
| 736 | self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple") |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 737 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 738 | def test_call_string(self): |
| 739 | # call() function with string argument on UNIX |
| 740 | fd, fname = mkstemp() |
| 741 | # reopen in text mode |
| 742 | with open(fd, "w") as fobj: |
| 743 | fobj.write("#!/bin/sh\n") |
| 744 | fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 745 | sys.executable) |
| 746 | os.chmod(fname, 0o700) |
| 747 | rc = subprocess.call(fname) |
| 748 | os.remove(fname) |
| 749 | self.assertEqual(rc, 47) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 750 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 751 | def _kill_process(self, method, *args): |
Florent Xicluna | 1d8ee3a | 2010-03-05 20:26:54 +0000 | [diff] [blame] | 752 | # Do not inherit file handles from the parent. |
| 753 | # It should fix failures on some platforms. |
| 754 | p = subprocess.Popen([sys.executable, "-c", "input()"], close_fds=True, |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 755 | stdin=subprocess.PIPE) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 756 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 757 | # Let the process initialize (Issue #3137) |
Florent Xicluna | 129226d | 2010-03-05 00:52:00 +0000 | [diff] [blame] | 758 | time.sleep(0.1) |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 759 | # The process should not terminate prematurely |
Florent Xicluna | b8f22b1 | 2010-03-05 01:18:04 +0000 | [diff] [blame] | 760 | self.assertIsNone(p.poll()) |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 761 | # Retry if the process do not receive the signal. |
Florent Xicluna | 129226d | 2010-03-05 00:52:00 +0000 | [diff] [blame] | 762 | count, maxcount = 0, 3 |
Florent Xicluna | 129226d | 2010-03-05 00:52:00 +0000 | [diff] [blame] | 763 | while count < maxcount and p.poll() is None: |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 764 | getattr(p, method)(*args) |
Florent Xicluna | 129226d | 2010-03-05 00:52:00 +0000 | [diff] [blame] | 765 | time.sleep(0.1) |
| 766 | count += 1 |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 767 | |
| 768 | self.assertIsNotNone(p.poll(), "the subprocess did not terminate") |
Florent Xicluna | b8f22b1 | 2010-03-05 01:18:04 +0000 | [diff] [blame] | 769 | if count > 1: |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 770 | print("p.{}{} succeeded after " |
| 771 | "{} attempts".format(method, args, count), file=sys.stderr) |
| 772 | return p |
| 773 | |
| 774 | def test_send_signal(self): |
| 775 | p = self._kill_process('send_signal', signal.SIGINT) |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 776 | self.assertNotEqual(p.wait(), 0) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 777 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 778 | def test_kill(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 779 | p = self._kill_process('kill') |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 780 | self.assertEqual(p.wait(), -signal.SIGKILL) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 781 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 782 | def test_terminate(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 783 | p = self._kill_process('terminate') |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 784 | self.assertEqual(p.wait(), -signal.SIGTERM) |
| 785 | |
| 786 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 787 | @unittest.skipUnless(mswindows, "Windows specific tests") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 788 | class Win32ProcessTestCase(unittest.TestCase): |
| 789 | def setUp(self): |
| 790 | # Try to minimize the number of children we have so this test |
| 791 | # doesn't crash on some buildbots (Alphas in particular). |
| 792 | support.reap_children() |
| 793 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 794 | def tearDown(self): |
| 795 | for inst in subprocess._active: |
| 796 | inst.wait() |
| 797 | subprocess._cleanup() |
| 798 | self.assertFalse(subprocess._active, "subprocess._active not empty") |
| 799 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 800 | def test_startupinfo(self): |
| 801 | # startupinfo argument |
| 802 | # We uses hardcoded constants, because we do not want to |
| 803 | # depend on win32all. |
| 804 | STARTF_USESHOWWINDOW = 1 |
| 805 | SW_MAXIMIZE = 3 |
| 806 | startupinfo = subprocess.STARTUPINFO() |
| 807 | startupinfo.dwFlags = STARTF_USESHOWWINDOW |
| 808 | startupinfo.wShowWindow = SW_MAXIMIZE |
| 809 | # Since Python is a console process, it won't be affected |
| 810 | # by wShowWindow, but the argument should be silently |
| 811 | # ignored |
| 812 | subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 813 | startupinfo=startupinfo) |
| 814 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 815 | def test_creationflags(self): |
| 816 | # creationflags argument |
| 817 | CREATE_NEW_CONSOLE = 16 |
| 818 | sys.stderr.write(" a DOS box should flash briefly ...\n") |
| 819 | subprocess.call(sys.executable + |
| 820 | ' -c "import time; time.sleep(0.25)"', |
| 821 | creationflags=CREATE_NEW_CONSOLE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 822 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 823 | def test_invalid_args(self): |
| 824 | # invalid arguments should raise ValueError |
| 825 | self.assertRaises(ValueError, subprocess.call, |
| 826 | [sys.executable, "-c", |
| 827 | "import sys; sys.exit(47)"], |
| 828 | preexec_fn=lambda: 1) |
| 829 | self.assertRaises(ValueError, subprocess.call, |
| 830 | [sys.executable, "-c", |
| 831 | "import sys; sys.exit(47)"], |
| 832 | stdout=subprocess.PIPE, |
| 833 | close_fds=True) |
| 834 | |
| 835 | def test_close_fds(self): |
| 836 | # close file descriptors |
| 837 | rc = subprocess.call([sys.executable, "-c", |
| 838 | "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 839 | close_fds=True) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 840 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 841 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 842 | def test_shell_sequence(self): |
| 843 | # Run command through the shell (sequence) |
| 844 | newenv = os.environ.copy() |
| 845 | newenv["FRUIT"] = "physalis" |
| 846 | p = subprocess.Popen(["set"], shell=1, |
| 847 | stdout=subprocess.PIPE, |
| 848 | env=newenv) |
| 849 | self.assertIn(b"physalis", p.stdout.read()) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 850 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 851 | def test_shell_string(self): |
| 852 | # Run command through the shell (string) |
| 853 | newenv = os.environ.copy() |
| 854 | newenv["FRUIT"] = "physalis" |
| 855 | p = subprocess.Popen("set", shell=1, |
| 856 | stdout=subprocess.PIPE, |
| 857 | env=newenv) |
| 858 | self.assertIn(b"physalis", p.stdout.read()) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 859 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 860 | def test_call_string(self): |
| 861 | # call() function with string argument on Windows |
| 862 | rc = subprocess.call(sys.executable + |
| 863 | ' -c "import sys; sys.exit(47)"') |
| 864 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 865 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 866 | def _kill_process(self, method, *args): |
| 867 | # Some win32 buildbot raises EOFError if stdin is inherited |
| 868 | p = subprocess.Popen([sys.executable, "-c", "input()"], |
| 869 | stdin=subprocess.PIPE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 870 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 871 | # Let the process initialize (Issue #3137) |
| 872 | time.sleep(0.1) |
| 873 | # The process should not terminate prematurely |
| 874 | self.assertIsNone(p.poll()) |
| 875 | # Retry if the process do not receive the signal. |
| 876 | count, maxcount = 0, 3 |
| 877 | while count < maxcount and p.poll() is None: |
| 878 | getattr(p, method)(*args) |
| 879 | time.sleep(0.1) |
| 880 | count += 1 |
| 881 | |
| 882 | returncode = p.poll() |
| 883 | self.assertIsNotNone(returncode, "the subprocess did not terminate") |
| 884 | if count > 1: |
| 885 | print("p.{}{} succeeded after " |
| 886 | "{} attempts".format(method, args, count), file=sys.stderr) |
| 887 | self.assertEqual(p.wait(), returncode) |
| 888 | self.assertNotEqual(returncode, 0) |
| 889 | |
| 890 | def test_send_signal(self): |
| 891 | self._kill_process('send_signal', signal.SIGTERM) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 892 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 893 | def test_kill(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 894 | self._kill_process('kill') |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 895 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 896 | def test_terminate(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 897 | self._kill_process('terminate') |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 898 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 899 | |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 900 | # The module says: |
| 901 | # "NB This only works (and is only relevant) for UNIX." |
| 902 | # |
| 903 | # Actually, getoutput should work on any platform with an os.popen, but |
| 904 | # I'll take the comment as given, and skip this suite. |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 905 | @unittest.skipUnless(os.name == 'posix', "only relevant for UNIX") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 906 | class CommandTests(unittest.TestCase): |
| 907 | def test_getoutput(self): |
| 908 | self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy') |
| 909 | self.assertEqual(subprocess.getstatusoutput('echo xyzzy'), |
| 910 | (0, 'xyzzy')) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 911 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 912 | # we use mkdtemp in the next line to create an empty directory |
| 913 | # under our exclusive control; from that, we can invent a pathname |
| 914 | # that we _know_ won't exist. This is guaranteed to fail. |
| 915 | dir = None |
| 916 | try: |
| 917 | dir = tempfile.mkdtemp() |
| 918 | name = os.path.join(dir, "foo") |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 919 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 920 | status, output = subprocess.getstatusoutput('cat ' + name) |
| 921 | self.assertNotEqual(status, 0) |
| 922 | finally: |
| 923 | if dir is not None: |
| 924 | os.rmdir(dir) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 925 | |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 926 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 927 | @unittest.skipUnless(getattr(subprocess, '_has_poll', False), |
| 928 | "poll system call not supported") |
| 929 | class ProcessTestCaseNoPoll(ProcessTestCase): |
| 930 | def setUp(self): |
| 931 | subprocess._has_poll = False |
| 932 | ProcessTestCase.setUp(self) |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 933 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 934 | def tearDown(self): |
| 935 | subprocess._has_poll = True |
| 936 | ProcessTestCase.tearDown(self) |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 937 | |
| 938 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 939 | @unittest.skipUnless(getattr(subprocess, '_posixsubprocess', False), |
| 940 | "_posixsubprocess extension module not found.") |
| 941 | class ProcessTestCasePOSIXPurePython(ProcessTestCase, POSIXProcessTestCase): |
| 942 | def setUp(self): |
| 943 | subprocess._posixsubprocess = None |
| 944 | ProcessTestCase.setUp(self) |
| 945 | POSIXProcessTestCase.setUp(self) |
| 946 | |
| 947 | def tearDown(self): |
| 948 | subprocess._posixsubprocess = sys.modules['_posixsubprocess'] |
| 949 | POSIXProcessTestCase.tearDown(self) |
| 950 | ProcessTestCase.tearDown(self) |
| 951 | |
| 952 | |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +0000 | [diff] [blame] | 953 | class HelperFunctionTests(unittest.TestCase): |
Gregory P. Smith | af6d3b8 | 2010-03-01 02:56:44 +0000 | [diff] [blame] | 954 | @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows") |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +0000 | [diff] [blame] | 955 | def test_eintr_retry_call(self): |
| 956 | record_calls = [] |
| 957 | def fake_os_func(*args): |
| 958 | record_calls.append(args) |
| 959 | if len(record_calls) == 2: |
| 960 | raise OSError(errno.EINTR, "fake interrupted system call") |
| 961 | return tuple(reversed(args)) |
| 962 | |
| 963 | self.assertEqual((999, 256), |
| 964 | subprocess._eintr_retry_call(fake_os_func, 256, 999)) |
| 965 | self.assertEqual([(256, 999)], record_calls) |
| 966 | # This time there will be an EINTR so it will loop once. |
| 967 | self.assertEqual((666,), |
| 968 | subprocess._eintr_retry_call(fake_os_func, 666)) |
| 969 | self.assertEqual([(256, 999), (666,), (666,)], record_calls) |
| 970 | |
| 971 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 972 | def test_main(): |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 973 | unit_tests = (ProcessTestCase, |
| 974 | POSIXProcessTestCase, |
| 975 | Win32ProcessTestCase, |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 976 | ProcessTestCasePOSIXPurePython, |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 977 | CommandTests, |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +0000 | [diff] [blame] | 978 | ProcessTestCaseNoPoll, |
| 979 | HelperFunctionTests) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 980 | |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 981 | support.run_unittest(*unit_tests) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 982 | support.reap_children() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 983 | |
| 984 | if __name__ == "__main__": |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 985 | test_main() |