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