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 | 3fff44d | 2010-03-01 00:43:08 +0000 | [diff] [blame] | 7 | import errno |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 8 | import tempfile |
| 9 | import time |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 10 | import re |
Gregory P. Smith | 81ce685 | 2011-03-15 02:04:11 -0400 | [diff] [blame] | 11 | import shutil |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 12 | |
| 13 | mswindows = (sys.platform == "win32") |
| 14 | |
| 15 | # |
| 16 | # Depends on the following external programs: Python |
| 17 | # |
| 18 | |
| 19 | if mswindows: |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 20 | SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), ' |
| 21 | 'os.O_BINARY);') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 22 | else: |
| 23 | SETBINARY = '' |
| 24 | |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 25 | # In a debug build, stuff like "[6580 refs]" is printed to stderr at |
| 26 | # shutdown time. That frustrates tests trying to check stderr produced |
| 27 | # from a spawned Python process. |
| 28 | def remove_stderr_debug_decorations(stderr): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 29 | return re.sub("\[\d+ refs\]\r?\n?$", "", stderr.decode()).encode() |
| 30 | #return re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr) |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 31 | |
Tim Golden | 595c8d3 | 2010-08-12 09:45:25 +0000 | [diff] [blame] | 32 | class BaseTestCase(unittest.TestCase): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 33 | def setUp(self): |
| 34 | # Try to minimize the number of children we have so this test |
| 35 | # doesn't crash on some buildbots (Alphas in particular). |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 36 | if hasattr(support, "reap_children"): |
| 37 | support.reap_children() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 38 | |
| 39 | def tearDown(self): |
| 40 | # Try to minimize the number of children we have so this test |
| 41 | # doesn't crash on some buildbots (Alphas in particular). |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 42 | if hasattr(support, "reap_children"): |
| 43 | support.reap_children() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 44 | |
Tim Golden | 595c8d3 | 2010-08-12 09:45:25 +0000 | [diff] [blame] | 45 | def mkstemp(self, *args, **kwargs): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 46 | """wrapper for mkstemp, calling mktemp if mkstemp is not available""" |
| 47 | if hasattr(tempfile, "mkstemp"): |
Tim Golden | 595c8d3 | 2010-08-12 09:45:25 +0000 | [diff] [blame] | 48 | return tempfile.mkstemp(*args, **kwargs) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 49 | else: |
Tim Golden | 595c8d3 | 2010-08-12 09:45:25 +0000 | [diff] [blame] | 50 | fname = tempfile.mktemp(*args, **kwargs) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 51 | return os.open(fname, os.O_RDWR|os.O_CREAT), fname |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 52 | |
Tim Golden | 595c8d3 | 2010-08-12 09:45:25 +0000 | [diff] [blame] | 53 | class ProcessTestCase(BaseTestCase): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 54 | # |
| 55 | # Generic tests |
| 56 | # |
| 57 | def test_call_seq(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 58 | # call() function with sequence argument |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 59 | rc = subprocess.call([sys.executable, "-c", |
| 60 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 61 | self.assertEqual(rc, 47) |
| 62 | |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 63 | def test_check_call_zero(self): |
| 64 | # check_call() function with zero return code |
| 65 | rc = subprocess.check_call([sys.executable, "-c", |
| 66 | "import sys; sys.exit(0)"]) |
| 67 | self.assertEqual(rc, 0) |
| 68 | |
| 69 | def test_check_call_nonzero(self): |
| 70 | # check_call() function with non-zero return code |
| 71 | try: |
| 72 | subprocess.check_call([sys.executable, "-c", |
| 73 | "import sys; sys.exit(47)"]) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 74 | except subprocess.CalledProcessError as e: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | self.assertEqual(e.returncode, 47) |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 76 | else: |
| 77 | self.fail("Expected CalledProcessError") |
| 78 | |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 79 | def test_check_output(self): |
| 80 | # check_output() function with zero return code |
| 81 | output = subprocess.check_output( |
| 82 | [sys.executable, "-c", "print('BDFL')"]) |
| 83 | self.assertTrue(b'BDFL' in output) |
| 84 | |
| 85 | def test_check_output_nonzero(self): |
| 86 | # check_call() function with non-zero return code |
| 87 | try: |
| 88 | subprocess.check_output( |
| 89 | [sys.executable, "-c", "import sys; sys.exit(5)"]) |
| 90 | except subprocess.CalledProcessError as e: |
| 91 | self.assertEqual(e.returncode, 5) |
| 92 | else: |
| 93 | self.fail("Expected CalledProcessError") |
| 94 | |
| 95 | def test_check_output_stderr(self): |
| 96 | # check_output() function stderr redirected to stdout |
| 97 | output = subprocess.check_output( |
| 98 | [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"], |
| 99 | stderr=subprocess.STDOUT) |
| 100 | self.assertTrue(b'BDFL' in output) |
| 101 | |
| 102 | def test_check_output_stdout_arg(self): |
| 103 | # check_output() function stderr redirected to stdout |
| 104 | try: |
| 105 | output = subprocess.check_output( |
| 106 | [sys.executable, "-c", "print('will not be run')"], |
| 107 | stdout=sys.stdout) |
| 108 | except ValueError as e: |
| 109 | self.assertTrue('stdout' in e.args[0]) |
| 110 | else: |
| 111 | self.fail("Expected ValueError when stdout arg supplied.") |
| 112 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 113 | def test_call_kwargs(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 114 | # call() function with keyword args |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 115 | newenv = os.environ.copy() |
| 116 | newenv["FRUIT"] = "banana" |
| 117 | rc = subprocess.call([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 118 | 'import sys, os;' |
| 119 | 'sys.exit(os.getenv("FRUIT")=="banana")'], |
| 120 | env=newenv) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 121 | self.assertEqual(rc, 1) |
| 122 | |
| 123 | def test_stdin_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 124 | # .stdin is None when not redirected |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 125 | p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 126 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 127 | p.wait() |
| 128 | self.assertEqual(p.stdin, None) |
| 129 | |
| 130 | def test_stdout_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 131 | # .stdout is None when not redirected |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 132 | p = subprocess.Popen([sys.executable, "-c", |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 133 | 'print(" this bit of output is from a ' |
Tim Peters | 4052fe5 | 2004-10-13 03:29:54 +0000 | [diff] [blame] | 134 | 'test of stdout in a different ' |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 135 | 'process ...")'], |
Tim Peters | 4052fe5 | 2004-10-13 03:29:54 +0000 | [diff] [blame] | 136 | stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 137 | p.wait() |
| 138 | self.assertEqual(p.stdout, None) |
| 139 | |
| 140 | def test_stderr_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 141 | # .stderr is None when not redirected |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 142 | p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 143 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 144 | p.wait() |
| 145 | self.assertEqual(p.stderr, None) |
| 146 | |
| 147 | def test_executable(self): |
Antoine Pitrou | 5550365 | 2009-03-29 19:30:55 +0000 | [diff] [blame] | 148 | arg0 = os.path.join(os.path.dirname(sys.executable), |
| 149 | "somethingyoudonthave") |
| 150 | p = subprocess.Popen([arg0, "-c", "import sys; sys.exit(47)"], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 151 | executable=sys.executable) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 152 | p.wait() |
| 153 | self.assertEqual(p.returncode, 47) |
| 154 | |
| 155 | def test_stdin_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 156 | # stdin redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 157 | p = subprocess.Popen([sys.executable, "-c", |
| 158 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 159 | stdin=subprocess.PIPE) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 160 | p.stdin.write(b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 161 | p.stdin.close() |
| 162 | p.wait() |
| 163 | self.assertEqual(p.returncode, 1) |
| 164 | |
| 165 | def test_stdin_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 166 | # stdin is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 167 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 168 | d = tf.fileno() |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 169 | os.write(d, b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 170 | os.lseek(d, 0, 0) |
| 171 | p = subprocess.Popen([sys.executable, "-c", |
| 172 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 173 | stdin=d) |
| 174 | p.wait() |
| 175 | self.assertEqual(p.returncode, 1) |
| 176 | |
| 177 | def test_stdin_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 178 | # stdin is set to open file object |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 179 | tf = tempfile.TemporaryFile() |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 180 | tf.write(b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 181 | tf.seek(0) |
| 182 | p = subprocess.Popen([sys.executable, "-c", |
| 183 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 184 | stdin=tf) |
| 185 | p.wait() |
| 186 | self.assertEqual(p.returncode, 1) |
| 187 | |
| 188 | def test_stdout_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 189 | # stdout redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 190 | p = subprocess.Popen([sys.executable, "-c", |
| 191 | 'import sys; sys.stdout.write("orange")'], |
| 192 | stdout=subprocess.PIPE) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 193 | self.assertEqual(p.stdout.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 194 | |
| 195 | def test_stdout_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 196 | # stdout is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 197 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 198 | d = tf.fileno() |
| 199 | p = subprocess.Popen([sys.executable, "-c", |
| 200 | 'import sys; sys.stdout.write("orange")'], |
| 201 | stdout=d) |
| 202 | p.wait() |
| 203 | os.lseek(d, 0, 0) |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 204 | self.assertEqual(os.read(d, 1024), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 205 | |
| 206 | def test_stdout_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 207 | # stdout is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 208 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 209 | p = subprocess.Popen([sys.executable, "-c", |
| 210 | 'import sys; sys.stdout.write("orange")'], |
| 211 | stdout=tf) |
| 212 | p.wait() |
| 213 | tf.seek(0) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 214 | self.assertEqual(tf.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 215 | |
| 216 | def test_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 217 | # stderr redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 218 | p = subprocess.Popen([sys.executable, "-c", |
| 219 | 'import sys; sys.stderr.write("strawberry")'], |
| 220 | stderr=subprocess.PIPE) |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 221 | self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()), |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 222 | b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 223 | |
| 224 | def test_stderr_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 225 | # stderr is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 226 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 227 | d = tf.fileno() |
| 228 | p = subprocess.Popen([sys.executable, "-c", |
| 229 | 'import sys; sys.stderr.write("strawberry")'], |
| 230 | stderr=d) |
| 231 | p.wait() |
| 232 | os.lseek(d, 0, 0) |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 233 | self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)), |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 234 | b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 235 | |
| 236 | def test_stderr_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 237 | # stderr is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 238 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 239 | p = subprocess.Popen([sys.executable, "-c", |
| 240 | 'import sys; sys.stderr.write("strawberry")'], |
| 241 | stderr=tf) |
| 242 | p.wait() |
| 243 | tf.seek(0) |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 244 | self.assertEqual(remove_stderr_debug_decorations(tf.read()), |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 245 | b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 246 | |
| 247 | def test_stdout_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 248 | # capture stdout and stderr to the same pipe |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 249 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 250 | 'import sys;' |
| 251 | 'sys.stdout.write("apple");' |
| 252 | 'sys.stdout.flush();' |
| 253 | 'sys.stderr.write("orange")'], |
| 254 | stdout=subprocess.PIPE, |
| 255 | stderr=subprocess.STDOUT) |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 256 | output = p.stdout.read() |
| 257 | stripped = remove_stderr_debug_decorations(output) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 258 | self.assertEqual(stripped, 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) |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 272 | output = tf.read() |
| 273 | stripped = remove_stderr_debug_decorations(output) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 274 | self.assertEqual(stripped, b"appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 275 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 276 | def test_stdout_filedes_of_stdout(self): |
| 277 | # stdout is set to 1 (#1531862). |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 278 | 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] | 279 | rc = subprocess.call([sys.executable, "-c", cmd], stdout=1) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 280 | self.assertEqual(rc, 2) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 281 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 282 | def test_cwd(self): |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 283 | tmpdir = tempfile.gettempdir() |
Peter Astrand | 195404f | 2004-11-12 15:51:48 +0000 | [diff] [blame] | 284 | # We cannot use os.path.realpath to canonicalize the path, |
| 285 | # since it doesn't expand Tru64 {memb} strings. See bug 1063571. |
| 286 | cwd = os.getcwd() |
| 287 | os.chdir(tmpdir) |
| 288 | tmpdir = os.getcwd() |
| 289 | os.chdir(cwd) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 290 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 291 | 'import sys,os;' |
| 292 | 'sys.stdout.write(os.getcwd())'], |
| 293 | stdout=subprocess.PIPE, |
| 294 | cwd=tmpdir) |
Fredrik Lundh | 59c0559 | 2004-10-13 06:55:40 +0000 | [diff] [blame] | 295 | normcase = os.path.normcase |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 296 | self.assertEqual(normcase(p.stdout.read().decode("utf-8")), |
| 297 | normcase(tmpdir)) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 298 | |
| 299 | def test_env(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 300 | newenv = os.environ.copy() |
| 301 | newenv["FRUIT"] = "orange" |
| 302 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 303 | 'import sys,os;' |
| 304 | 'sys.stdout.write(os.getenv("FRUIT"))'], |
| 305 | stdout=subprocess.PIPE, |
| 306 | env=newenv) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 307 | self.assertEqual(p.stdout.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 308 | |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 309 | def test_communicate_stdin(self): |
| 310 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 311 | 'import sys;' |
| 312 | 'sys.exit(sys.stdin.read() == "pear")'], |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 313 | stdin=subprocess.PIPE) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 314 | p.communicate(b"pear") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 315 | self.assertEqual(p.returncode, 1) |
| 316 | |
| 317 | def test_communicate_stdout(self): |
| 318 | p = subprocess.Popen([sys.executable, "-c", |
| 319 | 'import sys; sys.stdout.write("pineapple")'], |
| 320 | stdout=subprocess.PIPE) |
| 321 | (stdout, stderr) = p.communicate() |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 322 | self.assertEqual(stdout, b"pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 323 | self.assertEqual(stderr, None) |
| 324 | |
| 325 | def test_communicate_stderr(self): |
| 326 | p = subprocess.Popen([sys.executable, "-c", |
| 327 | 'import sys; sys.stderr.write("pineapple")'], |
| 328 | stderr=subprocess.PIPE) |
| 329 | (stdout, stderr) = p.communicate() |
| 330 | self.assertEqual(stdout, None) |
Brett Cannon | 653a5ad | 2005-03-05 06:40:52 +0000 | [diff] [blame] | 331 | # When running with a pydebug build, the # of references is outputted |
| 332 | # to stderr, so just check if stderr at least started with "pinapple" |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 333 | self.assertEqual(remove_stderr_debug_decorations(stderr), b"pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 334 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 335 | def test_communicate(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 336 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 337 | 'import sys,os;' |
| 338 | 'sys.stderr.write("pineapple");' |
| 339 | 'sys.stdout.write(sys.stdin.read())'], |
| 340 | stdin=subprocess.PIPE, |
| 341 | stdout=subprocess.PIPE, |
| 342 | stderr=subprocess.PIPE) |
Georg Brandl | 1abcbf8 | 2008-07-01 19:28:43 +0000 | [diff] [blame] | 343 | (stdout, stderr) = p.communicate(b"banana") |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 344 | self.assertEqual(stdout, b"banana") |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 345 | self.assertEqual(remove_stderr_debug_decorations(stderr), |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 346 | b"pineapple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 347 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 348 | # This test is Linux specific for simplicity to at least have |
| 349 | # some coverage. It is not a platform specific bug. |
| 350 | if os.path.isdir('/proc/%d/fd' % os.getpid()): |
| 351 | # Test for the fd leak reported in http://bugs.python.org/issue2791. |
| 352 | def test_communicate_pipe_fd_leak(self): |
| 353 | fd_directory = '/proc/%d/fd' % os.getpid() |
| 354 | num_fds_before_popen = len(os.listdir(fd_directory)) |
| 355 | p = subprocess.Popen([sys.executable, '-c', 'print()'], |
| 356 | stdout=subprocess.PIPE) |
| 357 | p.communicate() |
| 358 | num_fds_after_communicate = len(os.listdir(fd_directory)) |
| 359 | del p |
| 360 | num_fds_after_destruction = len(os.listdir(fd_directory)) |
| 361 | self.assertEqual(num_fds_before_popen, num_fds_after_destruction) |
| 362 | self.assertEqual(num_fds_before_popen, num_fds_after_communicate) |
| 363 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 364 | def test_communicate_returns(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 365 | # communicate() should return None if no redirection is active |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 366 | p = subprocess.Popen([sys.executable, "-c", |
| 367 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 368 | (stdout, stderr) = p.communicate() |
| 369 | self.assertEqual(stdout, None) |
| 370 | self.assertEqual(stderr, None) |
| 371 | |
| 372 | def test_communicate_pipe_buf(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 373 | # communicate() with writes larger than pipe_buf |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 374 | # This test will probably deadlock rather than fail, if |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 375 | # communicate() does not work properly. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 376 | x, y = os.pipe() |
| 377 | if mswindows: |
| 378 | pipe_buf = 512 |
| 379 | else: |
| 380 | pipe_buf = os.fpathconf(x, "PC_PIPE_BUF") |
| 381 | os.close(x) |
| 382 | os.close(y) |
| 383 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 384 | 'import sys,os;' |
| 385 | 'sys.stdout.write(sys.stdin.read(47));' |
| 386 | 'sys.stderr.write("xyz"*%d);' |
| 387 | 'sys.stdout.write(sys.stdin.read())' % pipe_buf], |
| 388 | stdin=subprocess.PIPE, |
| 389 | stdout=subprocess.PIPE, |
| 390 | stderr=subprocess.PIPE) |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 391 | string_to_write = b"abc"*pipe_buf |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 392 | (stdout, stderr) = p.communicate(string_to_write) |
| 393 | self.assertEqual(stdout, string_to_write) |
| 394 | |
| 395 | def test_writes_before_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 396 | # stdin.write before communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 397 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 398 | 'import sys,os;' |
| 399 | 'sys.stdout.write(sys.stdin.read())'], |
| 400 | stdin=subprocess.PIPE, |
| 401 | stdout=subprocess.PIPE, |
| 402 | stderr=subprocess.PIPE) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 403 | p.stdin.write(b"banana") |
| 404 | (stdout, stderr) = p.communicate(b"split") |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 405 | self.assertEqual(stdout, b"bananasplit") |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 406 | self.assertEqual(remove_stderr_debug_decorations(stderr), b"") |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 407 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 408 | def test_universal_newlines(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 409 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 410 | 'import sys,os;' + SETBINARY + |
| 411 | 'sys.stdout.write("line1\\n");' |
| 412 | 'sys.stdout.flush();' |
| 413 | 'sys.stdout.write("line2\\n");' |
| 414 | 'sys.stdout.flush();' |
| 415 | 'sys.stdout.write("line3\\r\\n");' |
| 416 | 'sys.stdout.flush();' |
| 417 | 'sys.stdout.write("line4\\r");' |
| 418 | 'sys.stdout.flush();' |
| 419 | 'sys.stdout.write("\\nline5");' |
| 420 | 'sys.stdout.flush();' |
| 421 | 'sys.stdout.write("\\nline6");'], |
| 422 | stdout=subprocess.PIPE, |
| 423 | universal_newlines=1) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 424 | stdout = p.stdout.read() |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 425 | self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 426 | |
| 427 | def test_universal_newlines_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 428 | # universal newlines through communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 429 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 430 | 'import sys,os;' + SETBINARY + |
| 431 | 'sys.stdout.write("line1\\n");' |
| 432 | 'sys.stdout.flush();' |
| 433 | 'sys.stdout.write("line2\\n");' |
| 434 | 'sys.stdout.flush();' |
| 435 | 'sys.stdout.write("line3\\r\\n");' |
| 436 | 'sys.stdout.flush();' |
| 437 | 'sys.stdout.write("line4\\r");' |
| 438 | 'sys.stdout.flush();' |
| 439 | 'sys.stdout.write("\\nline5");' |
| 440 | 'sys.stdout.flush();' |
| 441 | 'sys.stdout.write("\\nline6");'], |
| 442 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 443 | universal_newlines=1) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 444 | (stdout, stderr) = p.communicate() |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 445 | self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 446 | |
| 447 | def test_no_leaking(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 448 | # Make sure we leak no resources |
Antoine Pitrou | 6fab1f6 | 2010-09-18 22:40:56 +0000 | [diff] [blame] | 449 | if not mswindows: |
Peter Astrand | f7f1bb7 | 2005-03-03 20:47:37 +0000 | [diff] [blame] | 450 | max_handles = 1026 # too much for most UNIX systems |
| 451 | else: |
Antoine Pitrou | 6fab1f6 | 2010-09-18 22:40:56 +0000 | [diff] [blame] | 452 | max_handles = 2050 # too much for (at least some) Windows setups |
| 453 | handles = [] |
Gregory P. Smith | 81ce685 | 2011-03-15 02:04:11 -0400 | [diff] [blame] | 454 | tmpdir = tempfile.mkdtemp() |
Antoine Pitrou | 6fab1f6 | 2010-09-18 22:40:56 +0000 | [diff] [blame] | 455 | try: |
| 456 | for i in range(max_handles): |
| 457 | try: |
Gregory P. Smith | 81ce685 | 2011-03-15 02:04:11 -0400 | [diff] [blame] | 458 | tmpfile = os.path.join(tmpdir, support.TESTFN) |
| 459 | handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT)) |
Antoine Pitrou | 6fab1f6 | 2010-09-18 22:40:56 +0000 | [diff] [blame] | 460 | except OSError as e: |
| 461 | if e.errno != errno.EMFILE: |
| 462 | raise |
| 463 | break |
| 464 | else: |
| 465 | self.skipTest("failed to reach the file descriptor limit " |
| 466 | "(tried %d)" % max_handles) |
| 467 | # Close a couple of them (should be enough for a subprocess) |
| 468 | for i in range(10): |
| 469 | os.close(handles.pop()) |
| 470 | # Loop creating some subprocesses. If one of them leaks some fds, |
| 471 | # the next loop iteration will fail by reaching the max fd limit. |
| 472 | for i in range(15): |
| 473 | p = subprocess.Popen([sys.executable, "-c", |
| 474 | "import sys;" |
| 475 | "sys.stdout.write(sys.stdin.read())"], |
| 476 | stdin=subprocess.PIPE, |
| 477 | stdout=subprocess.PIPE, |
| 478 | stderr=subprocess.PIPE) |
| 479 | data = p.communicate(b"lime")[0] |
| 480 | self.assertEqual(data, b"lime") |
| 481 | finally: |
| 482 | for h in handles: |
| 483 | os.close(h) |
Gregory P. Smith | 81ce685 | 2011-03-15 02:04:11 -0400 | [diff] [blame] | 484 | shutil.rmtree(tmpdir) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 485 | |
| 486 | def test_list2cmdline(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 487 | self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), |
| 488 | '"a b c" d e') |
| 489 | self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), |
| 490 | 'ab\\"c \\ d') |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 491 | self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']), |
| 492 | 'ab\\"c " \\\\" d') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 493 | self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']), |
| 494 | 'a\\\\\\b "de fg" h') |
| 495 | self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']), |
| 496 | 'a\\\\\\"b c d') |
| 497 | self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']), |
| 498 | '"a\\\\b c" d e') |
| 499 | self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']), |
| 500 | '"a\\\\b\\ c" d e') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 501 | self.assertEqual(subprocess.list2cmdline(['ab', '']), |
| 502 | 'ab ""') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 503 | |
| 504 | |
| 505 | def test_poll(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 506 | p = subprocess.Popen([sys.executable, |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 507 | "-c", "import time; time.sleep(1)"]) |
| 508 | count = 0 |
| 509 | while p.poll() is None: |
| 510 | time.sleep(0.1) |
| 511 | count += 1 |
| 512 | # We expect that the poll loop probably went around about 10 times, |
| 513 | # but, based on system scheduling we can't control, it's possible |
| 514 | # poll() never returned None. It "should be" very rare that it |
| 515 | # didn't go around at least twice. |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 516 | self.assertTrue(count >= 2) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 517 | # Subsequent invocations should just return the returncode |
| 518 | self.assertEqual(p.poll(), 0) |
| 519 | |
| 520 | |
| 521 | def test_wait(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 522 | p = subprocess.Popen([sys.executable, |
| 523 | "-c", "import time; time.sleep(2)"]) |
| 524 | self.assertEqual(p.wait(), 0) |
| 525 | # Subsequent invocations should just return the returncode |
| 526 | self.assertEqual(p.wait(), 0) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 527 | |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 528 | |
| 529 | def test_invalid_bufsize(self): |
| 530 | # an invalid type of the bufsize argument should raise |
| 531 | # TypeError. |
| 532 | try: |
| 533 | subprocess.Popen([sys.executable, "-c", "pass"], "orange") |
| 534 | except TypeError: |
| 535 | pass |
| 536 | else: |
| 537 | self.fail("Expected TypeError") |
| 538 | |
Guido van Rossum | 46a05a7 | 2007-06-07 21:56:45 +0000 | [diff] [blame] | 539 | def test_bufsize_is_none(self): |
| 540 | # bufsize=None should be the same as bufsize=0. |
| 541 | p = subprocess.Popen([sys.executable, "-c", "pass"], None) |
| 542 | self.assertEqual(p.wait(), 0) |
| 543 | # Again with keyword arg |
| 544 | p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None) |
| 545 | self.assertEqual(p.wait(), 0) |
| 546 | |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 547 | def test_leaking_fds_on_error(self): |
| 548 | # see bug #5179: Popen leaks file descriptors to PIPEs if |
| 549 | # the child fails to execute; this will eventually exhaust |
| 550 | # the maximum number of open fds. 1024 seems a very common |
| 551 | # value for that limit, but Windows has 2048, so we loop |
| 552 | # 1024 times (each call leaked two fds). |
| 553 | for i in range(1024): |
| 554 | try: |
| 555 | subprocess.Popen(['nonexisting_i_hope'], |
| 556 | stdout=subprocess.PIPE, |
| 557 | stderr=subprocess.PIPE) |
| 558 | # Windows raises IOError |
| 559 | except (IOError, OSError) as err: |
R David Murray | d79210a | 2011-03-13 22:13:09 -0400 | [diff] [blame] | 560 | # ignore errors that indicate the command was not found |
Antoine Pitrou | 126848a | 2011-03-15 21:17:10 +0100 | [diff] [blame] | 561 | if err.errno not in (errno.ENOENT, errno.EACCES): |
Benjamin Peterson | 4b06819 | 2009-02-20 03:19:25 +0000 | [diff] [blame] | 562 | raise |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 563 | |
Victor Stinner | 86c73bf | 2010-05-21 20:39:17 +0000 | [diff] [blame] | 564 | def test_issue8780(self): |
| 565 | # Ensure that stdout is inherited from the parent |
| 566 | # if stdout=PIPE is not used |
| 567 | code = ';'.join(( |
| 568 | 'import subprocess, sys', |
| 569 | 'retcode = subprocess.call(' |
| 570 | "[sys.executable, '-c', 'print(\"Hello World!\")'])", |
| 571 | 'assert retcode == 0')) |
| 572 | output = subprocess.check_output([sys.executable, '-c', code]) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 573 | self.assertTrue(output.startswith(b'Hello World!'), ascii(output)) |
Victor Stinner | 86c73bf | 2010-05-21 20:39:17 +0000 | [diff] [blame] | 574 | |
Tim Golden | 40b3744 | 2010-08-06 13:20:12 +0000 | [diff] [blame] | 575 | def test_handles_closed_on_exception(self): |
| 576 | # If CreateProcess exits with an error, ensure the |
| 577 | # duplicate output handles are released |
| 578 | ifhandle, ifname = self.mkstemp() |
| 579 | ofhandle, ofname = self.mkstemp() |
| 580 | efhandle, efname = self.mkstemp() |
| 581 | try: |
| 582 | subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle, |
| 583 | stderr=efhandle) |
| 584 | except OSError: |
| 585 | os.close(ifhandle) |
| 586 | os.remove(ifname) |
| 587 | os.close(ofhandle) |
| 588 | os.remove(ofname) |
| 589 | os.close(efhandle) |
| 590 | os.remove(efname) |
| 591 | self.assertFalse(os.path.exists(ifname)) |
| 592 | self.assertFalse(os.path.exists(ofname)) |
| 593 | self.assertFalse(os.path.exists(efname)) |
| 594 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 595 | # |
| 596 | # POSIX tests |
| 597 | # |
| 598 | if not mswindows: |
| 599 | def test_exceptions(self): |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 600 | # caught & re-raised exceptions |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 601 | try: |
| 602 | p = subprocess.Popen([sys.executable, "-c", ""], |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 603 | cwd="/this/path/does/not/exist") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 604 | except OSError as e: |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 605 | # The attribute child_traceback should contain "os.chdir" |
| 606 | # somewhere. |
| 607 | self.assertNotEqual(e.child_traceback.find("os.chdir"), -1) |
| 608 | else: |
| 609 | self.fail("Expected OSError") |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 610 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 611 | def _suppress_core_files(self): |
| 612 | """Try to prevent core files from being created. |
| 613 | Returns previous ulimit if successful, else None. |
| 614 | """ |
Ronald Oussoren | d7eb3a8 | 2010-07-23 10:35:20 +0000 | [diff] [blame] | 615 | if sys.platform == 'darwin': |
| 616 | # Check if the 'Crash Reporter' on OSX was configured |
| 617 | # in 'Developer' mode and warn that it will get triggered |
| 618 | # when it is. |
| 619 | # |
| 620 | # This assumes that this context manager is used in tests |
| 621 | # that might trigger the next manager. |
| 622 | value = subprocess.Popen(['/usr/bin/defaults', 'read', |
| 623 | 'com.apple.CrashReporter', 'DialogType'], |
| 624 | stdout=subprocess.PIPE).communicate()[0] |
| 625 | if value.strip() == b'developer': |
| 626 | print("this tests triggers the Crash Reporter, " |
| 627 | "that is intentional", end='') |
| 628 | sys.stdout.flush() |
| 629 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 630 | try: |
| 631 | import resource |
| 632 | old_limit = resource.getrlimit(resource.RLIMIT_CORE) |
| 633 | resource.setrlimit(resource.RLIMIT_CORE, (0,0)) |
| 634 | return old_limit |
| 635 | except (ImportError, ValueError, resource.error): |
| 636 | return None |
| 637 | |
Ronald Oussoren | d7eb3a8 | 2010-07-23 10:35:20 +0000 | [diff] [blame] | 638 | |
| 639 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 640 | def _unsuppress_core_files(self, old_limit): |
| 641 | """Return core file behavior to default.""" |
| 642 | if old_limit is None: |
| 643 | return |
| 644 | try: |
| 645 | import resource |
| 646 | resource.setrlimit(resource.RLIMIT_CORE, old_limit) |
| 647 | except (ImportError, ValueError, resource.error): |
| 648 | return |
| 649 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 650 | def test_run_abort(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 651 | # returncode handles signal termination |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 652 | old_limit = self._suppress_core_files() |
| 653 | try: |
| 654 | p = subprocess.Popen([sys.executable, |
| 655 | "-c", "import os; os.abort()"]) |
| 656 | finally: |
| 657 | self._unsuppress_core_files(old_limit) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 658 | p.wait() |
| 659 | self.assertEqual(-p.returncode, signal.SIGABRT) |
| 660 | |
| 661 | def test_preexec(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 662 | # preexec function |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 663 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 664 | 'import sys,os;' |
| 665 | 'sys.stdout.write(os.getenv("FRUIT"))'], |
| 666 | stdout=subprocess.PIPE, |
| 667 | preexec_fn=lambda: os.putenv("FRUIT", |
| 668 | "apple")) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 669 | self.assertEqual(p.stdout.read(), b"apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 670 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 671 | def test_args_string(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 672 | # args is a string |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 673 | fd, fname = self.mkstemp() |
| 674 | # reopen in text mode |
| 675 | with open(fd, "w") as fobj: |
| 676 | fobj.write("#!/bin/sh\n") |
| 677 | fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 678 | sys.executable) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 679 | os.chmod(fname, 0o700) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 680 | p = subprocess.Popen(fname) |
| 681 | p.wait() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 682 | os.remove(fname) |
Peter Astrand | 2224be6 | 2004-11-17 20:06:35 +0000 | [diff] [blame] | 683 | self.assertEqual(p.returncode, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 684 | |
| 685 | def test_invalid_args(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 686 | # invalid arguments should raise ValueError |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 687 | self.assertRaises(ValueError, subprocess.call, |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 688 | [sys.executable, |
| 689 | "-c", "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 690 | startupinfo=47) |
| 691 | self.assertRaises(ValueError, subprocess.call, |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 692 | [sys.executable, |
| 693 | "-c", "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 694 | creationflags=47) |
| 695 | |
| 696 | def test_shell_sequence(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 697 | # Run command through the shell (sequence) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 698 | newenv = os.environ.copy() |
| 699 | newenv["FRUIT"] = "apple" |
| 700 | p = subprocess.Popen(["echo $FRUIT"], shell=1, |
| 701 | stdout=subprocess.PIPE, |
| 702 | env=newenv) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 703 | 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] | 704 | |
| 705 | def test_shell_string(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 706 | # Run command through the shell (string) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 707 | newenv = os.environ.copy() |
| 708 | newenv["FRUIT"] = "apple" |
| 709 | p = subprocess.Popen("echo $FRUIT", shell=1, |
| 710 | stdout=subprocess.PIPE, |
| 711 | env=newenv) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 712 | 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] | 713 | |
| 714 | def test_call_string(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 715 | # call() function with string argument on UNIX |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 716 | fd, fname = self.mkstemp() |
| 717 | # reopen in text mode |
| 718 | with open(fd, "w") as fobj: |
| 719 | fobj.write("#!/bin/sh\n") |
| 720 | fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 721 | sys.executable) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 722 | os.chmod(fname, 0o700) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 723 | rc = subprocess.call(fname) |
Peter Astrand | 2224be6 | 2004-11-17 20:06:35 +0000 | [diff] [blame] | 724 | os.remove(fname) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 725 | self.assertEqual(rc, 47) |
| 726 | |
Stefan Krah | 8db99c8 | 2010-07-19 14:39:36 +0000 | [diff] [blame] | 727 | def test_specific_shell(self): |
| 728 | # Issue #9265: Incorrect name passed as arg[0]. |
| 729 | shells = [] |
| 730 | for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']: |
| 731 | for name in ['bash', 'ksh']: |
| 732 | sh = os.path.join(prefix, name) |
| 733 | if os.path.isfile(sh): |
| 734 | shells.append(sh) |
| 735 | if not shells: # Will probably work for any shell but csh. |
| 736 | self.skipTest("bash or ksh required for this test") |
| 737 | sh = '/bin/sh' |
| 738 | if os.path.isfile(sh) and not os.path.islink(sh): |
| 739 | # Test will fail if /bin/sh is a symlink to csh. |
| 740 | shells.append(sh) |
| 741 | for sh in shells: |
| 742 | p = subprocess.Popen("echo $0", executable=sh, shell=True, |
| 743 | stdout=subprocess.PIPE) |
| 744 | self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii')) |
| 745 | |
Christian Heimes | 75ca4ea | 2008-05-06 23:48:04 +0000 | [diff] [blame] | 746 | def DISABLED_test_send_signal(self): |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 747 | p = subprocess.Popen([sys.executable, |
| 748 | "-c", "input()"]) |
| 749 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 750 | self.assertTrue(p.poll() is None, p.poll()) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 751 | p.send_signal(signal.SIGINT) |
| 752 | self.assertNotEqual(p.wait(), 0) |
| 753 | |
Christian Heimes | 75ca4ea | 2008-05-06 23:48:04 +0000 | [diff] [blame] | 754 | def DISABLED_test_kill(self): |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 755 | p = subprocess.Popen([sys.executable, |
| 756 | "-c", "input()"]) |
| 757 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 758 | self.assertTrue(p.poll() is None, p.poll()) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 759 | p.kill() |
| 760 | self.assertEqual(p.wait(), -signal.SIGKILL) |
| 761 | |
Christian Heimes | 75ca4ea | 2008-05-06 23:48:04 +0000 | [diff] [blame] | 762 | def DISABLED_test_terminate(self): |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 763 | p = subprocess.Popen([sys.executable, |
| 764 | "-c", "input()"]) |
| 765 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +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.terminate() |
| 768 | self.assertEqual(p.wait(), -signal.SIGTERM) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 769 | |
Victor Stinner | 097d1b7 | 2010-04-27 18:29:45 +0000 | [diff] [blame] | 770 | def test_undecodable_env(self): |
| 771 | for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')): |
Antoine Pitrou | 4a5dd5c | 2010-09-20 11:17:39 +0000 | [diff] [blame] | 772 | value_repr = ascii(value).encode("ascii") |
Victor Stinner | 097d1b7 | 2010-04-27 18:29:45 +0000 | [diff] [blame] | 773 | |
| 774 | # test str with surrogates |
Antoine Pitrou | 4a5dd5c | 2010-09-20 11:17:39 +0000 | [diff] [blame] | 775 | script = "import os; print(ascii(os.getenv(%s)))" % repr(key) |
Victor Stinner | 097d1b7 | 2010-04-27 18:29:45 +0000 | [diff] [blame] | 776 | env = os.environ.copy() |
| 777 | env[key] = value |
Antoine Pitrou | 4a5dd5c | 2010-09-20 11:17:39 +0000 | [diff] [blame] | 778 | # Force surrogate-escaping of \xFF in the child process; |
| 779 | # otherwise it can be decoded as-is if the default locale |
| 780 | # is latin-1. |
| 781 | env['PYTHONFSENCODING'] = 'ascii' |
Victor Stinner | 097d1b7 | 2010-04-27 18:29:45 +0000 | [diff] [blame] | 782 | stdout = subprocess.check_output( |
| 783 | [sys.executable, "-c", script], |
| 784 | env=env) |
| 785 | stdout = stdout.rstrip(b'\n\r') |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 786 | self.assertEqual(stdout, value_repr) |
Victor Stinner | 097d1b7 | 2010-04-27 18:29:45 +0000 | [diff] [blame] | 787 | |
| 788 | # test bytes |
| 789 | key = key.encode("ascii", "surrogateescape") |
| 790 | value = value.encode("ascii", "surrogateescape") |
Antoine Pitrou | 4a5dd5c | 2010-09-20 11:17:39 +0000 | [diff] [blame] | 791 | script = "import os; print(ascii(os.getenv(%s)))" % repr(key) |
Victor Stinner | 097d1b7 | 2010-04-27 18:29:45 +0000 | [diff] [blame] | 792 | env = os.environ.copy() |
| 793 | env[key] = value |
| 794 | stdout = subprocess.check_output( |
| 795 | [sys.executable, "-c", script], |
| 796 | env=env) |
| 797 | stdout = stdout.rstrip(b'\n\r') |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 798 | self.assertEqual(stdout, value_repr) |
Victor Stinner | 097d1b7 | 2010-04-27 18:29:45 +0000 | [diff] [blame] | 799 | |
Gregory P. Smith | b740e76 | 2010-12-14 15:16:24 +0000 | [diff] [blame] | 800 | def test_wait_when_sigchild_ignored(self): |
| 801 | # NOTE: sigchild_ignore.py may not be an effective test on all OSes. |
| 802 | sigchild_ignore = support.findfile("sigchild_ignore.py", |
| 803 | subdir="subprocessdata") |
| 804 | p = subprocess.Popen([sys.executable, sigchild_ignore], |
| 805 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 806 | stdout, stderr = p.communicate() |
| 807 | self.assertEqual(0, p.returncode, "sigchild_ignore.py exited" |
Gregory P. Smith | 773d7df | 2010-12-14 15:25:20 +0000 | [diff] [blame] | 808 | " non-zero with this error:\n%s" % |
| 809 | stderr.decode('utf8')) |
Gregory P. Smith | b740e76 | 2010-12-14 15:16:24 +0000 | [diff] [blame] | 810 | |
Antoine Pitrou | f50a6b6 | 2011-01-03 18:36:36 +0000 | [diff] [blame] | 811 | def check_close_std_fds(self, fds): |
| 812 | # Issue #9905: test that subprocess pipes still work properly with |
| 813 | # some standard fds closed |
| 814 | stdin = 0 |
| 815 | newfds = [] |
| 816 | for a in fds: |
| 817 | b = os.dup(a) |
| 818 | newfds.append(b) |
| 819 | if a == 0: |
| 820 | stdin = b |
| 821 | try: |
| 822 | for fd in fds: |
| 823 | os.close(fd) |
| 824 | out, err = subprocess.Popen([sys.executable, "-c", |
| 825 | 'import sys;' |
| 826 | 'sys.stdout.write("apple");' |
| 827 | 'sys.stdout.flush();' |
| 828 | 'sys.stderr.write("orange")'], |
| 829 | stdin=stdin, |
| 830 | stdout=subprocess.PIPE, |
| 831 | stderr=subprocess.PIPE).communicate() |
| 832 | err = support.strip_python_stderr(err) |
| 833 | self.assertEqual((out, err), (b'apple', b'orange')) |
| 834 | finally: |
| 835 | for b, a in zip(newfds, fds): |
| 836 | os.dup2(b, a) |
| 837 | for b in newfds: |
| 838 | os.close(b) |
| 839 | |
| 840 | def test_close_fd_0(self): |
| 841 | self.check_close_std_fds([0]) |
| 842 | |
| 843 | def test_close_fd_1(self): |
| 844 | self.check_close_std_fds([1]) |
| 845 | |
| 846 | def test_close_fd_2(self): |
| 847 | self.check_close_std_fds([2]) |
| 848 | |
| 849 | def test_close_fds_0_1(self): |
| 850 | self.check_close_std_fds([0, 1]) |
| 851 | |
| 852 | def test_close_fds_0_2(self): |
| 853 | self.check_close_std_fds([0, 2]) |
| 854 | |
| 855 | def test_close_fds_1_2(self): |
| 856 | self.check_close_std_fds([1, 2]) |
| 857 | |
| 858 | def test_close_fds_0_1_2(self): |
| 859 | # Issue #10806: test that subprocess pipes still work properly with |
| 860 | # all standard fds closed. |
| 861 | self.check_close_std_fds([0, 1, 2]) |
| 862 | |
| 863 | def test_surrogates_error_message(self): |
| 864 | def prepare(): |
| 865 | raise ValueError("surrogate:\uDCff") |
| 866 | |
Gregory P. Smith | b740e76 | 2010-12-14 15:16:24 +0000 | [diff] [blame] | 867 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 868 | # |
| 869 | # Windows tests |
| 870 | # |
| 871 | if mswindows: |
| 872 | def test_startupinfo(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 873 | # startupinfo argument |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 874 | # We uses hardcoded constants, because we do not want to |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 875 | # depend on win32all. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 876 | STARTF_USESHOWWINDOW = 1 |
| 877 | SW_MAXIMIZE = 3 |
| 878 | startupinfo = subprocess.STARTUPINFO() |
| 879 | startupinfo.dwFlags = STARTF_USESHOWWINDOW |
| 880 | startupinfo.wShowWindow = SW_MAXIMIZE |
| 881 | # Since Python is a console process, it won't be affected |
| 882 | # by wShowWindow, but the argument should be silently |
| 883 | # ignored |
| 884 | subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], |
| 885 | startupinfo=startupinfo) |
| 886 | |
| 887 | def test_creationflags(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 888 | # creationflags argument |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 889 | CREATE_NEW_CONSOLE = 16 |
Tim Peters | 876c432 | 2004-10-13 03:21:35 +0000 | [diff] [blame] | 890 | sys.stderr.write(" a DOS box should flash briefly ...\n") |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 891 | subprocess.call(sys.executable + |
Tim Peters | 876c432 | 2004-10-13 03:21:35 +0000 | [diff] [blame] | 892 | ' -c "import time; time.sleep(0.25)"', |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 893 | creationflags=CREATE_NEW_CONSOLE) |
| 894 | |
| 895 | def test_invalid_args(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 896 | # invalid arguments should raise ValueError |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 897 | self.assertRaises(ValueError, subprocess.call, |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 898 | [sys.executable, |
| 899 | "-c", "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 900 | preexec_fn=lambda: 1) |
| 901 | self.assertRaises(ValueError, subprocess.call, |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 902 | [sys.executable, |
| 903 | "-c", "import sys; sys.exit(47)"], |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 904 | stdout=subprocess.PIPE, |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 905 | close_fds=True) |
| 906 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 907 | def test_close_fds(self): |
| 908 | # close file descriptors |
| 909 | rc = subprocess.call([sys.executable, "-c", |
| 910 | "import sys; sys.exit(47)"], |
| 911 | close_fds=True) |
| 912 | self.assertEqual(rc, 47) |
| 913 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 914 | def test_shell_sequence(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 915 | # Run command through the shell (sequence) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 916 | newenv = os.environ.copy() |
| 917 | newenv["FRUIT"] = "physalis" |
| 918 | p = subprocess.Popen(["set"], shell=1, |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 919 | stdout=subprocess.PIPE, |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 920 | env=newenv) |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 921 | self.assertNotEqual(p.stdout.read().find(b"physalis"), -1) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 922 | |
| 923 | def test_shell_string(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 924 | # Run command through the shell (string) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 925 | newenv = os.environ.copy() |
| 926 | newenv["FRUIT"] = "physalis" |
| 927 | p = subprocess.Popen("set", shell=1, |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 928 | stdout=subprocess.PIPE, |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 929 | env=newenv) |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 930 | self.assertNotEqual(p.stdout.read().find(b"physalis"), -1) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 931 | |
| 932 | def test_call_string(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 933 | # call() function with string argument on Windows |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 934 | rc = subprocess.call(sys.executable + |
| 935 | ' -c "import sys; sys.exit(47)"') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 936 | self.assertEqual(rc, 47) |
| 937 | |
Christian Heimes | 75ca4ea | 2008-05-06 23:48:04 +0000 | [diff] [blame] | 938 | def DISABLED_test_send_signal(self): |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 939 | p = subprocess.Popen([sys.executable, |
| 940 | "-c", "input()"]) |
| 941 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 942 | self.assertTrue(p.poll() is None, p.poll()) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 943 | p.send_signal(signal.SIGTERM) |
| 944 | self.assertNotEqual(p.wait(), 0) |
| 945 | |
Christian Heimes | 75ca4ea | 2008-05-06 23:48:04 +0000 | [diff] [blame] | 946 | def DISABLED_test_kill(self): |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 947 | p = subprocess.Popen([sys.executable, |
| 948 | "-c", "input()"]) |
| 949 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 950 | self.assertTrue(p.poll() is None, p.poll()) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 951 | p.kill() |
| 952 | self.assertNotEqual(p.wait(), 0) |
| 953 | |
Christian Heimes | 75ca4ea | 2008-05-06 23:48:04 +0000 | [diff] [blame] | 954 | def DISABLED_test_terminate(self): |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 955 | p = subprocess.Popen([sys.executable, |
| 956 | "-c", "input()"]) |
| 957 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 958 | self.assertTrue(p.poll() is None, p.poll()) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 959 | p.terminate() |
| 960 | self.assertNotEqual(p.wait(), 0) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 961 | |
Tim Golden | 595c8d3 | 2010-08-12 09:45:25 +0000 | [diff] [blame] | 962 | |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 963 | class CommandTests(unittest.TestCase): |
| 964 | # The module says: |
| 965 | # "NB This only works (and is only relevant) for UNIX." |
| 966 | # |
| 967 | # Actually, getoutput should work on any platform with an os.popen, but |
| 968 | # I'll take the comment as given, and skip this suite. |
| 969 | if os.name == 'posix': |
| 970 | |
| 971 | def test_getoutput(self): |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 972 | self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy') |
| 973 | self.assertEqual(subprocess.getstatusoutput('echo xyzzy'), |
| 974 | (0, 'xyzzy')) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 975 | |
| 976 | # we use mkdtemp in the next line to create an empty directory |
| 977 | # under our exclusive control; from that, we can invent a pathname |
| 978 | # that we _know_ won't exist. This is guaranteed to fail. |
| 979 | dir = None |
| 980 | try: |
| 981 | dir = tempfile.mkdtemp() |
| 982 | name = os.path.join(dir, "foo") |
| 983 | |
| 984 | status, output = subprocess.getstatusoutput('cat ' + name) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 985 | self.assertNotEqual(status, 0) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 986 | finally: |
| 987 | if dir is not None: |
| 988 | os.rmdir(dir) |
| 989 | |
Georg Brandl | ae83d6e | 2009-08-13 09:04:31 +0000 | [diff] [blame] | 990 | |
| 991 | unit_tests = [ProcessTestCase, CommandTests] |
| 992 | |
Tim Golden | 595c8d3 | 2010-08-12 09:45:25 +0000 | [diff] [blame] | 993 | if mswindows: |
| 994 | class CommandsWithSpaces (BaseTestCase): |
| 995 | |
| 996 | def setUp(self): |
| 997 | super().setUp() |
| 998 | f, fname = self.mkstemp(".py", "te st") |
| 999 | self.fname = fname.lower () |
| 1000 | os.write(f, b"import sys;" |
| 1001 | b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))" |
| 1002 | ) |
| 1003 | os.close(f) |
| 1004 | |
| 1005 | def tearDown(self): |
| 1006 | os.remove(self.fname) |
| 1007 | super().tearDown() |
| 1008 | |
| 1009 | def with_spaces(self, *args, **kwargs): |
| 1010 | kwargs['stdout'] = subprocess.PIPE |
| 1011 | p = subprocess.Popen(*args, **kwargs) |
| 1012 | self.assertEqual( |
| 1013 | p.stdout.read ().decode("mbcs"), |
| 1014 | "2 [%r, 'ab cd']" % self.fname |
| 1015 | ) |
| 1016 | |
| 1017 | def test_shell_string_with_spaces(self): |
| 1018 | # call() function with string argument with spaces on Windows |
Brian Curtin | f263c05 | 2010-08-13 20:59:27 +0000 | [diff] [blame] | 1019 | self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, |
| 1020 | "ab cd"), shell=1) |
Tim Golden | 595c8d3 | 2010-08-12 09:45:25 +0000 | [diff] [blame] | 1021 | |
| 1022 | def test_shell_sequence_with_spaces(self): |
| 1023 | # call() function with sequence argument with spaces on Windows |
Brian Curtin | f263c05 | 2010-08-13 20:59:27 +0000 | [diff] [blame] | 1024 | self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1) |
Tim Golden | 595c8d3 | 2010-08-12 09:45:25 +0000 | [diff] [blame] | 1025 | |
| 1026 | def test_noshell_string_with_spaces(self): |
| 1027 | # call() function with string argument with spaces on Windows |
| 1028 | self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, |
| 1029 | "ab cd")) |
| 1030 | |
| 1031 | def test_noshell_sequence_with_spaces(self): |
| 1032 | # call() function with sequence argument with spaces on Windows |
| 1033 | self.with_spaces([sys.executable, self.fname, "ab cd"]) |
| 1034 | |
| 1035 | unit_tests.append(CommandsWithSpaces) |
| 1036 | |
| 1037 | |
Gregory P. Smith | 10d2952 | 2009-08-13 18:33:30 +0000 | [diff] [blame] | 1038 | if getattr(subprocess, '_has_poll', False): |
Georg Brandl | ae83d6e | 2009-08-13 09:04:31 +0000 | [diff] [blame] | 1039 | class ProcessTestCaseNoPoll(ProcessTestCase): |
| 1040 | def setUp(self): |
| 1041 | subprocess._has_poll = False |
| 1042 | ProcessTestCase.setUp(self) |
| 1043 | |
| 1044 | def tearDown(self): |
| 1045 | subprocess._has_poll = True |
| 1046 | ProcessTestCase.tearDown(self) |
| 1047 | |
| 1048 | unit_tests.append(ProcessTestCaseNoPoll) |
| 1049 | |
| 1050 | |
Gregory P. Smith | 3fff44d | 2010-03-01 00:43:08 +0000 | [diff] [blame] | 1051 | class HelperFunctionTests(unittest.TestCase): |
Gregory P. Smith | 5cab281 | 2010-03-01 02:58:43 +0000 | [diff] [blame] | 1052 | @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows") |
Gregory P. Smith | 3fff44d | 2010-03-01 00:43:08 +0000 | [diff] [blame] | 1053 | def test_eintr_retry_call(self): |
| 1054 | record_calls = [] |
| 1055 | def fake_os_func(*args): |
| 1056 | record_calls.append(args) |
| 1057 | if len(record_calls) == 2: |
| 1058 | raise OSError(errno.EINTR, "fake interrupted system call") |
| 1059 | return tuple(reversed(args)) |
| 1060 | |
| 1061 | self.assertEqual((999, 256), |
| 1062 | subprocess._eintr_retry_call(fake_os_func, 256, 999)) |
| 1063 | self.assertEqual([(256, 999)], record_calls) |
| 1064 | # This time there will be an EINTR so it will loop once. |
| 1065 | self.assertEqual((666,), |
| 1066 | subprocess._eintr_retry_call(fake_os_func, 666)) |
| 1067 | self.assertEqual([(256, 999), (666,), (666,)], record_calls) |
| 1068 | |
| 1069 | unit_tests.append(HelperFunctionTests) |
| 1070 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1071 | def test_main(): |
Georg Brandl | ae83d6e | 2009-08-13 09:04:31 +0000 | [diff] [blame] | 1072 | support.run_unittest(*unit_tests) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 1073 | support.reap_children() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1074 | |
| 1075 | if __name__ == "__main__": |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 1076 | test_main() |