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