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