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