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 |
Gregory P. Smith | d23047b | 2010-12-04 09:10:44 +0000 | [diff] [blame] | 12 | import warnings |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 13 | import select |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 14 | try: |
| 15 | import gc |
| 16 | except ImportError: |
| 17 | gc = None |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 18 | |
| 19 | mswindows = (sys.platform == "win32") |
| 20 | |
| 21 | # |
| 22 | # Depends on the following external programs: Python |
| 23 | # |
| 24 | |
| 25 | if mswindows: |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 26 | SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), ' |
| 27 | 'os.O_BINARY);') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 28 | else: |
| 29 | SETBINARY = '' |
| 30 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 31 | |
| 32 | try: |
| 33 | mkstemp = tempfile.mkstemp |
| 34 | except AttributeError: |
| 35 | # tempfile.mkstemp is not available |
| 36 | def mkstemp(): |
| 37 | """Replacement for mkstemp, calling mktemp.""" |
| 38 | fname = tempfile.mktemp() |
| 39 | return os.open(fname, os.O_RDWR|os.O_CREAT), fname |
| 40 | |
Tim Peters | 3761e8d | 2004-10-13 04:07:12 +0000 | [diff] [blame] | 41 | |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 42 | class BaseTestCase(unittest.TestCase): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 43 | def setUp(self): |
| 44 | # Try to minimize the number of children we have so this test |
| 45 | # doesn't crash on some buildbots (Alphas in particular). |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 46 | support.reap_children() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 47 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 48 | def tearDown(self): |
| 49 | for inst in subprocess._active: |
| 50 | inst.wait() |
| 51 | subprocess._cleanup() |
| 52 | self.assertFalse(subprocess._active, "subprocess._active not empty") |
| 53 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 54 | def assertStderrEqual(self, stderr, expected, msg=None): |
| 55 | # In a debug build, stuff like "[6580 refs]" is printed to stderr at |
| 56 | # shutdown time. That frustrates tests trying to check stderr produced |
| 57 | # from a spawned Python process. |
Antoine Pitrou | 62f68ed | 2010-08-04 11:48:56 +0000 | [diff] [blame] | 58 | actual = support.strip_python_stderr(stderr) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 59 | self.assertEqual(actual, expected, msg) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 60 | |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 61 | |
| 62 | class ProcessTestCase(BaseTestCase): |
| 63 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 64 | def test_call_seq(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 65 | # call() function with sequence argument |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 66 | rc = subprocess.call([sys.executable, "-c", |
| 67 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 68 | self.assertEqual(rc, 47) |
| 69 | |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 70 | def test_check_call_zero(self): |
| 71 | # check_call() function with zero return code |
| 72 | rc = subprocess.check_call([sys.executable, "-c", |
| 73 | "import sys; sys.exit(0)"]) |
| 74 | self.assertEqual(rc, 0) |
| 75 | |
| 76 | def test_check_call_nonzero(self): |
| 77 | # check_call() function with non-zero return code |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 78 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 79 | subprocess.check_call([sys.executable, "-c", |
| 80 | "import sys; sys.exit(47)"]) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 81 | self.assertEqual(c.exception.returncode, 47) |
Peter Astrand | 454f767 | 2005-01-01 09:36:35 +0000 | [diff] [blame] | 82 | |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 83 | def test_check_output(self): |
| 84 | # check_output() function with zero return code |
| 85 | output = subprocess.check_output( |
| 86 | [sys.executable, "-c", "print('BDFL')"]) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 87 | self.assertIn(b'BDFL', output) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 88 | |
| 89 | def test_check_output_nonzero(self): |
| 90 | # check_call() function with non-zero return code |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 91 | with self.assertRaises(subprocess.CalledProcessError) as c: |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 92 | subprocess.check_output( |
| 93 | [sys.executable, "-c", "import sys; sys.exit(5)"]) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 94 | self.assertEqual(c.exception.returncode, 5) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 95 | |
| 96 | def test_check_output_stderr(self): |
| 97 | # check_output() function stderr redirected to stdout |
| 98 | output = subprocess.check_output( |
| 99 | [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"], |
| 100 | stderr=subprocess.STDOUT) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 101 | self.assertIn(b'BDFL', output) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 102 | |
| 103 | def test_check_output_stdout_arg(self): |
| 104 | # check_output() function stderr redirected to stdout |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 105 | with self.assertRaises(ValueError) as c: |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 106 | output = subprocess.check_output( |
| 107 | [sys.executable, "-c", "print('will not be run')"], |
| 108 | stdout=sys.stdout) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 109 | self.fail("Expected ValueError when stdout arg supplied.") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 110 | self.assertIn('stdout', c.exception.args[0]) |
Georg Brandl | f973407 | 2008-12-07 15:30:06 +0000 | [diff] [blame] | 111 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 112 | def test_call_kwargs(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 113 | # call() function with keyword args |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 114 | newenv = os.environ.copy() |
| 115 | newenv["FRUIT"] = "banana" |
| 116 | rc = subprocess.call([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 117 | 'import sys, os;' |
| 118 | 'sys.exit(os.getenv("FRUIT")=="banana")'], |
| 119 | env=newenv) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 120 | self.assertEqual(rc, 1) |
| 121 | |
| 122 | def test_stdin_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 123 | # .stdin is None when not redirected |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 124 | p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 125 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 126 | self.addCleanup(p.stdout.close) |
| 127 | self.addCleanup(p.stderr.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 128 | p.wait() |
| 129 | self.assertEqual(p.stdin, None) |
| 130 | |
| 131 | def test_stdout_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 132 | # .stdout is None when not redirected |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 133 | p = subprocess.Popen([sys.executable, "-c", |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 134 | 'print(" this bit of output is from a ' |
Tim Peters | 4052fe5 | 2004-10-13 03:29:54 +0000 | [diff] [blame] | 135 | 'test of stdout in a different ' |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 136 | 'process ...")'], |
Tim Peters | 4052fe5 | 2004-10-13 03:29:54 +0000 | [diff] [blame] | 137 | stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 138 | self.addCleanup(p.stdin.close) |
| 139 | self.addCleanup(p.stderr.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 140 | p.wait() |
| 141 | self.assertEqual(p.stdout, None) |
| 142 | |
| 143 | def test_stderr_none(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 144 | # .stderr is None when not redirected |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 145 | p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 146 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 147 | self.addCleanup(p.stdout.close) |
| 148 | self.addCleanup(p.stdin.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 149 | p.wait() |
| 150 | self.assertEqual(p.stderr, None) |
| 151 | |
Ezio Melotti | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 152 | def test_executable_with_cwd(self): |
Florent Xicluna | 1d1ab97 | 2010-03-11 01:53:10 +0000 | [diff] [blame] | 153 | python_dir = os.path.dirname(os.path.realpath(sys.executable)) |
Ezio Melotti | 184bdfb | 2010-02-18 09:37:05 +0000 | [diff] [blame] | 154 | p = subprocess.Popen(["somethingyoudonthave", "-c", |
| 155 | "import sys; sys.exit(47)"], |
| 156 | executable=sys.executable, cwd=python_dir) |
| 157 | p.wait() |
| 158 | self.assertEqual(p.returncode, 47) |
| 159 | |
| 160 | @unittest.skipIf(sysconfig.is_python_build(), |
| 161 | "need an installed Python. See #7774") |
| 162 | def test_executable_without_cwd(self): |
| 163 | # For a normal installation, it should work without 'cwd' |
| 164 | # argument. For test runs in the build directory, see #7774. |
| 165 | p = subprocess.Popen(["somethingyoudonthave", "-c", |
| 166 | "import sys; sys.exit(47)"], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 167 | executable=sys.executable) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 168 | p.wait() |
| 169 | self.assertEqual(p.returncode, 47) |
| 170 | |
| 171 | def test_stdin_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 172 | # stdin redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 173 | p = subprocess.Popen([sys.executable, "-c", |
| 174 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 175 | stdin=subprocess.PIPE) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 176 | p.stdin.write(b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 177 | p.stdin.close() |
| 178 | p.wait() |
| 179 | self.assertEqual(p.returncode, 1) |
| 180 | |
| 181 | def test_stdin_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 182 | # stdin is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 183 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 184 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 185 | d = tf.fileno() |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 186 | os.write(d, b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 187 | os.lseek(d, 0, 0) |
| 188 | p = subprocess.Popen([sys.executable, "-c", |
| 189 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 190 | stdin=d) |
| 191 | p.wait() |
| 192 | self.assertEqual(p.returncode, 1) |
| 193 | |
| 194 | def test_stdin_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 195 | # stdin is set to open file object |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 196 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 197 | self.addCleanup(tf.close) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 198 | tf.write(b"pear") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 199 | tf.seek(0) |
| 200 | p = subprocess.Popen([sys.executable, "-c", |
| 201 | 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
| 202 | stdin=tf) |
| 203 | p.wait() |
| 204 | self.assertEqual(p.returncode, 1) |
| 205 | |
| 206 | def test_stdout_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 207 | # stdout redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 208 | p = subprocess.Popen([sys.executable, "-c", |
| 209 | 'import sys; sys.stdout.write("orange")'], |
| 210 | stdout=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 211 | self.addCleanup(p.stdout.close) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 212 | self.assertEqual(p.stdout.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 213 | |
| 214 | def test_stdout_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 215 | # stdout is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 216 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 217 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 218 | d = tf.fileno() |
| 219 | p = subprocess.Popen([sys.executable, "-c", |
| 220 | 'import sys; sys.stdout.write("orange")'], |
| 221 | stdout=d) |
| 222 | p.wait() |
| 223 | os.lseek(d, 0, 0) |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 224 | self.assertEqual(os.read(d, 1024), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 225 | |
| 226 | def test_stdout_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 227 | # stdout is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 228 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 229 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 230 | p = subprocess.Popen([sys.executable, "-c", |
| 231 | 'import sys; sys.stdout.write("orange")'], |
| 232 | stdout=tf) |
| 233 | p.wait() |
| 234 | tf.seek(0) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 235 | self.assertEqual(tf.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 236 | |
| 237 | def test_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 238 | # stderr redirection |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 239 | p = subprocess.Popen([sys.executable, "-c", |
| 240 | 'import sys; sys.stderr.write("strawberry")'], |
| 241 | stderr=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 242 | self.addCleanup(p.stderr.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 243 | self.assertStderrEqual(p.stderr.read(), b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 244 | |
| 245 | def test_stderr_filedes(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 246 | # stderr is set to open file descriptor |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 247 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 248 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 249 | d = tf.fileno() |
| 250 | p = subprocess.Popen([sys.executable, "-c", |
| 251 | 'import sys; sys.stderr.write("strawberry")'], |
| 252 | stderr=d) |
| 253 | p.wait() |
| 254 | os.lseek(d, 0, 0) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 255 | self.assertStderrEqual(os.read(d, 1024), b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 256 | |
| 257 | def test_stderr_fileobj(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 258 | # stderr is set to open file object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 259 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 260 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 261 | p = subprocess.Popen([sys.executable, "-c", |
| 262 | 'import sys; sys.stderr.write("strawberry")'], |
| 263 | stderr=tf) |
| 264 | p.wait() |
| 265 | tf.seek(0) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 266 | self.assertStderrEqual(tf.read(), b"strawberry") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 267 | |
| 268 | def test_stdout_stderr_pipe(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 269 | # capture stdout and stderr to the same pipe |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 270 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 271 | 'import sys;' |
| 272 | 'sys.stdout.write("apple");' |
| 273 | 'sys.stdout.flush();' |
| 274 | 'sys.stderr.write("orange")'], |
| 275 | stdout=subprocess.PIPE, |
| 276 | stderr=subprocess.STDOUT) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 277 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 278 | self.assertStderrEqual(p.stdout.read(), b"appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 279 | |
| 280 | def test_stdout_stderr_file(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 281 | # capture stdout and stderr to the same open file |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 282 | tf = tempfile.TemporaryFile() |
Benjamin Peterson | cc221b2 | 2010-10-31 02:06:21 +0000 | [diff] [blame] | 283 | self.addCleanup(tf.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 284 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 285 | 'import sys;' |
| 286 | 'sys.stdout.write("apple");' |
| 287 | 'sys.stdout.flush();' |
| 288 | 'sys.stderr.write("orange")'], |
| 289 | stdout=tf, |
| 290 | stderr=tf) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 291 | p.wait() |
| 292 | tf.seek(0) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 293 | self.assertStderrEqual(tf.read(), b"appleorange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 294 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 295 | def test_stdout_filedes_of_stdout(self): |
| 296 | # stdout is set to 1 (#1531862). |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 297 | 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] | 298 | rc = subprocess.call([sys.executable, "-c", cmd], stdout=1) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 299 | self.assertEqual(rc, 2) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 300 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 301 | def test_cwd(self): |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 302 | tmpdir = tempfile.gettempdir() |
Peter Astrand | 195404f | 2004-11-12 15:51:48 +0000 | [diff] [blame] | 303 | # We cannot use os.path.realpath to canonicalize the path, |
| 304 | # since it doesn't expand Tru64 {memb} strings. See bug 1063571. |
| 305 | cwd = os.getcwd() |
| 306 | os.chdir(tmpdir) |
| 307 | tmpdir = os.getcwd() |
| 308 | os.chdir(cwd) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 309 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 310 | 'import sys,os;' |
| 311 | 'sys.stdout.write(os.getcwd())'], |
| 312 | stdout=subprocess.PIPE, |
| 313 | cwd=tmpdir) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 314 | self.addCleanup(p.stdout.close) |
Fredrik Lundh | 59c0559 | 2004-10-13 06:55:40 +0000 | [diff] [blame] | 315 | normcase = os.path.normcase |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 316 | self.assertEqual(normcase(p.stdout.read().decode("utf-8")), |
| 317 | normcase(tmpdir)) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 318 | |
| 319 | def test_env(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 320 | newenv = os.environ.copy() |
| 321 | newenv["FRUIT"] = "orange" |
| 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.stdout.write(os.getenv("FRUIT"))'], |
| 325 | stdout=subprocess.PIPE, |
| 326 | env=newenv) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 327 | self.addCleanup(p.stdout.close) |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 328 | self.assertEqual(p.stdout.read(), b"orange") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 329 | |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 330 | def test_communicate_stdin(self): |
| 331 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 332 | 'import sys;' |
| 333 | 'sys.exit(sys.stdin.read() == "pear")'], |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 334 | stdin=subprocess.PIPE) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 335 | p.communicate(b"pear") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 336 | self.assertEqual(p.returncode, 1) |
| 337 | |
| 338 | def test_communicate_stdout(self): |
| 339 | p = subprocess.Popen([sys.executable, "-c", |
| 340 | 'import sys; sys.stdout.write("pineapple")'], |
| 341 | stdout=subprocess.PIPE) |
| 342 | (stdout, stderr) = p.communicate() |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 343 | self.assertEqual(stdout, b"pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 344 | self.assertEqual(stderr, None) |
| 345 | |
| 346 | def test_communicate_stderr(self): |
| 347 | p = subprocess.Popen([sys.executable, "-c", |
| 348 | 'import sys; sys.stderr.write("pineapple")'], |
| 349 | stderr=subprocess.PIPE) |
| 350 | (stdout, stderr) = p.communicate() |
| 351 | self.assertEqual(stdout, None) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 352 | self.assertStderrEqual(stderr, b"pineapple") |
Peter Astrand | cbac93c | 2005-03-03 20:24:28 +0000 | [diff] [blame] | 353 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 354 | def test_communicate(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 355 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 356 | 'import sys,os;' |
| 357 | 'sys.stderr.write("pineapple");' |
| 358 | 'sys.stdout.write(sys.stdin.read())'], |
| 359 | stdin=subprocess.PIPE, |
| 360 | stdout=subprocess.PIPE, |
| 361 | stderr=subprocess.PIPE) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 362 | self.addCleanup(p.stdout.close) |
| 363 | self.addCleanup(p.stderr.close) |
| 364 | self.addCleanup(p.stdin.close) |
Georg Brandl | 1abcbf8 | 2008-07-01 19:28:43 +0000 | [diff] [blame] | 365 | (stdout, stderr) = p.communicate(b"banana") |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 366 | self.assertEqual(stdout, b"banana") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 367 | self.assertStderrEqual(stderr, b"pineapple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 368 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 369 | # This test is Linux specific for simplicity to at least have |
| 370 | # some coverage. It is not a platform specific bug. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 371 | @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()), |
| 372 | "Linux specific") |
| 373 | # Test for the fd leak reported in http://bugs.python.org/issue2791. |
| 374 | def test_communicate_pipe_fd_leak(self): |
| 375 | fd_directory = '/proc/%d/fd' % os.getpid() |
| 376 | num_fds_before_popen = len(os.listdir(fd_directory)) |
| 377 | p = subprocess.Popen([sys.executable, "-c", "print()"], |
| 378 | stdout=subprocess.PIPE) |
| 379 | p.communicate() |
| 380 | num_fds_after_communicate = len(os.listdir(fd_directory)) |
| 381 | del p |
| 382 | num_fds_after_destruction = len(os.listdir(fd_directory)) |
| 383 | self.assertEqual(num_fds_before_popen, num_fds_after_destruction) |
| 384 | self.assertEqual(num_fds_before_popen, num_fds_after_communicate) |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 385 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 386 | def test_communicate_returns(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 387 | # communicate() should return None if no redirection is active |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame] | 388 | p = subprocess.Popen([sys.executable, "-c", |
| 389 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 390 | (stdout, stderr) = p.communicate() |
| 391 | self.assertEqual(stdout, None) |
| 392 | self.assertEqual(stderr, None) |
| 393 | |
| 394 | def test_communicate_pipe_buf(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 395 | # communicate() with writes larger than pipe_buf |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 396 | # This test will probably deadlock rather than fail, if |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 397 | # communicate() does not work properly. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 398 | x, y = os.pipe() |
| 399 | if mswindows: |
| 400 | pipe_buf = 512 |
| 401 | else: |
| 402 | pipe_buf = os.fpathconf(x, "PC_PIPE_BUF") |
| 403 | os.close(x) |
| 404 | os.close(y) |
| 405 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 406 | 'import sys,os;' |
| 407 | 'sys.stdout.write(sys.stdin.read(47));' |
| 408 | 'sys.stderr.write("xyz"*%d);' |
| 409 | 'sys.stdout.write(sys.stdin.read())' % pipe_buf], |
| 410 | stdin=subprocess.PIPE, |
| 411 | stdout=subprocess.PIPE, |
| 412 | stderr=subprocess.PIPE) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 413 | self.addCleanup(p.stdout.close) |
| 414 | self.addCleanup(p.stderr.close) |
| 415 | self.addCleanup(p.stdin.close) |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 416 | string_to_write = b"abc"*pipe_buf |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 417 | (stdout, stderr) = p.communicate(string_to_write) |
| 418 | self.assertEqual(stdout, string_to_write) |
| 419 | |
| 420 | def test_writes_before_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 421 | # stdin.write before communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 422 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 423 | 'import sys,os;' |
| 424 | 'sys.stdout.write(sys.stdin.read())'], |
| 425 | stdin=subprocess.PIPE, |
| 426 | stdout=subprocess.PIPE, |
| 427 | stderr=subprocess.PIPE) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 428 | self.addCleanup(p.stdout.close) |
| 429 | self.addCleanup(p.stderr.close) |
| 430 | self.addCleanup(p.stdin.close) |
Guido van Rossum | bb839ef | 2007-08-27 23:58:21 +0000 | [diff] [blame] | 431 | p.stdin.write(b"banana") |
| 432 | (stdout, stderr) = p.communicate(b"split") |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 433 | self.assertEqual(stdout, b"bananasplit") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 434 | self.assertStderrEqual(stderr, b"") |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 435 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 436 | def test_universal_newlines(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 437 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 438 | 'import sys,os;' + SETBINARY + |
| 439 | 'sys.stdout.write("line1\\n");' |
| 440 | 'sys.stdout.flush();' |
| 441 | 'sys.stdout.write("line2\\n");' |
| 442 | 'sys.stdout.flush();' |
| 443 | 'sys.stdout.write("line3\\r\\n");' |
| 444 | 'sys.stdout.flush();' |
| 445 | 'sys.stdout.write("line4\\r");' |
| 446 | 'sys.stdout.flush();' |
| 447 | 'sys.stdout.write("\\nline5");' |
| 448 | 'sys.stdout.flush();' |
| 449 | 'sys.stdout.write("\\nline6");'], |
| 450 | stdout=subprocess.PIPE, |
| 451 | universal_newlines=1) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 452 | self.addCleanup(p.stdout.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 453 | stdout = p.stdout.read() |
Guido van Rossum | c9e363c | 2007-05-15 23:18:55 +0000 | [diff] [blame] | 454 | self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 455 | |
| 456 | def test_universal_newlines_communicate(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 457 | # universal newlines through communicate() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 458 | p = subprocess.Popen([sys.executable, "-c", |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 459 | 'import sys,os;' + SETBINARY + |
| 460 | 'sys.stdout.write("line1\\n");' |
| 461 | 'sys.stdout.flush();' |
| 462 | 'sys.stdout.write("line2\\n");' |
| 463 | 'sys.stdout.flush();' |
| 464 | 'sys.stdout.write("line3\\r\\n");' |
| 465 | 'sys.stdout.flush();' |
| 466 | 'sys.stdout.write("line4\\r");' |
| 467 | 'sys.stdout.flush();' |
| 468 | 'sys.stdout.write("\\nline5");' |
| 469 | 'sys.stdout.flush();' |
| 470 | 'sys.stdout.write("\\nline6");'], |
| 471 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 472 | universal_newlines=1) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 473 | self.addCleanup(p.stdout.close) |
| 474 | self.addCleanup(p.stderr.close) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 475 | (stdout, stderr) = p.communicate() |
Guido van Rossum | fa0054a | 2007-05-24 04:05:35 +0000 | [diff] [blame] | 476 | self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 477 | |
| 478 | def test_no_leaking(self): |
Tim Peters | 7b759da | 2004-10-12 22:29:54 +0000 | [diff] [blame] | 479 | # Make sure we leak no resources |
Antoine Pitrou | 8db3027 | 2010-09-18 22:38:48 +0000 | [diff] [blame] | 480 | if not mswindows: |
Peter Astrand | f7f1bb7 | 2005-03-03 20:47:37 +0000 | [diff] [blame] | 481 | max_handles = 1026 # too much for most UNIX systems |
| 482 | else: |
Antoine Pitrou | 8db3027 | 2010-09-18 22:38:48 +0000 | [diff] [blame] | 483 | max_handles = 2050 # too much for (at least some) Windows setups |
| 484 | handles = [] |
| 485 | try: |
| 486 | for i in range(max_handles): |
| 487 | try: |
| 488 | handles.append(os.open(support.TESTFN, |
| 489 | os.O_WRONLY | os.O_CREAT)) |
| 490 | except OSError as e: |
| 491 | if e.errno != errno.EMFILE: |
| 492 | raise |
| 493 | break |
| 494 | else: |
| 495 | self.skipTest("failed to reach the file descriptor limit " |
| 496 | "(tried %d)" % max_handles) |
| 497 | # Close a couple of them (should be enough for a subprocess) |
| 498 | for i in range(10): |
| 499 | os.close(handles.pop()) |
| 500 | # Loop creating some subprocesses. If one of them leaks some fds, |
| 501 | # the next loop iteration will fail by reaching the max fd limit. |
| 502 | for i in range(15): |
| 503 | p = subprocess.Popen([sys.executable, "-c", |
| 504 | "import sys;" |
| 505 | "sys.stdout.write(sys.stdin.read())"], |
| 506 | stdin=subprocess.PIPE, |
| 507 | stdout=subprocess.PIPE, |
| 508 | stderr=subprocess.PIPE) |
| 509 | data = p.communicate(b"lime")[0] |
| 510 | self.assertEqual(data, b"lime") |
| 511 | finally: |
| 512 | for h in handles: |
| 513 | os.close(h) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 514 | |
| 515 | def test_list2cmdline(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 516 | self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), |
| 517 | '"a b c" d e') |
| 518 | self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), |
| 519 | 'ab\\"c \\ d') |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 520 | self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']), |
| 521 | 'ab\\"c " \\\\" d') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 522 | self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']), |
| 523 | 'a\\\\\\b "de fg" h') |
| 524 | self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']), |
| 525 | 'a\\\\\\"b c d') |
| 526 | self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']), |
| 527 | '"a\\\\b c" d e') |
| 528 | self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']), |
| 529 | '"a\\\\b\\ c" d e') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 530 | self.assertEqual(subprocess.list2cmdline(['ab', '']), |
| 531 | 'ab ""') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 532 | |
| 533 | |
| 534 | def test_poll(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 535 | p = subprocess.Popen([sys.executable, |
Tim Peters | 29b6b4f | 2004-10-13 03:43:40 +0000 | [diff] [blame] | 536 | "-c", "import time; time.sleep(1)"]) |
| 537 | count = 0 |
| 538 | while p.poll() is None: |
| 539 | time.sleep(0.1) |
| 540 | count += 1 |
| 541 | # We expect that the poll loop probably went around about 10 times, |
| 542 | # but, based on system scheduling we can't control, it's possible |
| 543 | # poll() never returned None. It "should be" very rare that it |
| 544 | # didn't go around at least twice. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 545 | self.assertGreaterEqual(count, 2) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 546 | # Subsequent invocations should just return the returncode |
| 547 | self.assertEqual(p.poll(), 0) |
| 548 | |
| 549 | |
| 550 | def test_wait(self): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 551 | p = subprocess.Popen([sys.executable, |
| 552 | "-c", "import time; time.sleep(2)"]) |
| 553 | self.assertEqual(p.wait(), 0) |
| 554 | # Subsequent invocations should just return the returncode |
| 555 | self.assertEqual(p.wait(), 0) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 556 | |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 557 | |
| 558 | def test_invalid_bufsize(self): |
| 559 | # an invalid type of the bufsize argument should raise |
| 560 | # TypeError. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 561 | with self.assertRaises(TypeError): |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 562 | subprocess.Popen([sys.executable, "-c", "pass"], "orange") |
Peter Astrand | 738131d | 2004-11-30 21:04:45 +0000 | [diff] [blame] | 563 | |
Guido van Rossum | 46a05a7 | 2007-06-07 21:56:45 +0000 | [diff] [blame] | 564 | def test_bufsize_is_none(self): |
| 565 | # bufsize=None should be the same as bufsize=0. |
| 566 | p = subprocess.Popen([sys.executable, "-c", "pass"], None) |
| 567 | self.assertEqual(p.wait(), 0) |
| 568 | # Again with keyword arg |
| 569 | p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None) |
| 570 | self.assertEqual(p.wait(), 0) |
| 571 | |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 572 | def test_leaking_fds_on_error(self): |
| 573 | # see bug #5179: Popen leaks file descriptors to PIPEs if |
| 574 | # the child fails to execute; this will eventually exhaust |
| 575 | # the maximum number of open fds. 1024 seems a very common |
| 576 | # value for that limit, but Windows has 2048, so we loop |
| 577 | # 1024 times (each call leaked two fds). |
| 578 | for i in range(1024): |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 579 | # Windows raises IOError. Others raise OSError. |
| 580 | with self.assertRaises(EnvironmentError) as c: |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 581 | subprocess.Popen(['nonexisting_i_hope'], |
| 582 | stdout=subprocess.PIPE, |
| 583 | stderr=subprocess.PIPE) |
Antoine Pitrou | 679e0f2 | 2010-09-18 17:56:02 +0000 | [diff] [blame] | 584 | if c.exception.errno != errno.ENOENT: # ignore "no such file" |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 585 | raise c.exception |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 586 | |
Victor Stinner | b369358 | 2010-05-21 20:13:12 +0000 | [diff] [blame] | 587 | def test_issue8780(self): |
| 588 | # Ensure that stdout is inherited from the parent |
| 589 | # if stdout=PIPE is not used |
| 590 | code = ';'.join(( |
| 591 | 'import subprocess, sys', |
| 592 | 'retcode = subprocess.call(' |
| 593 | "[sys.executable, '-c', 'print(\"Hello World!\")'])", |
| 594 | 'assert retcode == 0')) |
| 595 | output = subprocess.check_output([sys.executable, '-c', code]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 596 | self.assertTrue(output.startswith(b'Hello World!'), ascii(output)) |
Victor Stinner | b369358 | 2010-05-21 20:13:12 +0000 | [diff] [blame] | 597 | |
Tim Golden | af5ac39 | 2010-08-06 13:03:56 +0000 | [diff] [blame] | 598 | def test_handles_closed_on_exception(self): |
| 599 | # If CreateProcess exits with an error, ensure the |
| 600 | # duplicate output handles are released |
| 601 | ifhandle, ifname = mkstemp() |
| 602 | ofhandle, ofname = mkstemp() |
| 603 | efhandle, efname = mkstemp() |
| 604 | try: |
| 605 | subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle, |
| 606 | stderr=efhandle) |
| 607 | except OSError: |
| 608 | os.close(ifhandle) |
| 609 | os.remove(ifname) |
| 610 | os.close(ofhandle) |
| 611 | os.remove(ofname) |
| 612 | os.close(efhandle) |
| 613 | os.remove(efname) |
| 614 | self.assertFalse(os.path.exists(ifname)) |
| 615 | self.assertFalse(os.path.exists(ofname)) |
| 616 | self.assertFalse(os.path.exists(efname)) |
| 617 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 618 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 619 | # context manager |
| 620 | class _SuppressCoreFiles(object): |
| 621 | """Try to prevent core files from being created.""" |
| 622 | old_limit = None |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 623 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 624 | def __enter__(self): |
| 625 | """Try to save previous ulimit, then set it to (0, 0).""" |
| 626 | try: |
| 627 | import resource |
| 628 | self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) |
| 629 | resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) |
| 630 | except (ImportError, ValueError, resource.error): |
| 631 | pass |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 632 | |
Ronald Oussoren | 102d11a | 2010-07-23 09:50:05 +0000 | [diff] [blame] | 633 | if sys.platform == 'darwin': |
| 634 | # Check if the 'Crash Reporter' on OSX was configured |
| 635 | # in 'Developer' mode and warn that it will get triggered |
| 636 | # when it is. |
| 637 | # |
| 638 | # This assumes that this context manager is used in tests |
| 639 | # that might trigger the next manager. |
| 640 | value = subprocess.Popen(['/usr/bin/defaults', 'read', |
| 641 | 'com.apple.CrashReporter', 'DialogType'], |
| 642 | stdout=subprocess.PIPE).communicate()[0] |
| 643 | if value.strip() == b'developer': |
| 644 | print("this tests triggers the Crash Reporter, " |
| 645 | "that is intentional", end='') |
| 646 | sys.stdout.flush() |
| 647 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 648 | def __exit__(self, *args): |
| 649 | """Return core file behavior to default.""" |
| 650 | if self.old_limit is None: |
| 651 | return |
| 652 | try: |
| 653 | import resource |
| 654 | resource.setrlimit(resource.RLIMIT_CORE, self.old_limit) |
| 655 | except (ImportError, ValueError, resource.error): |
| 656 | pass |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 657 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 658 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 659 | @unittest.skipIf(mswindows, "POSIX specific tests") |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 660 | class POSIXProcessTestCase(BaseTestCase): |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 661 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 662 | def test_exceptions(self): |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 663 | nonexistent_dir = "/_this/pa.th/does/not/exist" |
| 664 | try: |
| 665 | os.chdir(nonexistent_dir) |
| 666 | except OSError as e: |
| 667 | # This avoids hard coding the errno value or the OS perror() |
| 668 | # string and instead capture the exception that we want to see |
| 669 | # below for comparison. |
| 670 | desired_exception = e |
Benjamin Peterson | 5f78040 | 2010-11-20 18:07:52 +0000 | [diff] [blame] | 671 | desired_exception.strerror += ': ' + repr(sys.executable) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 672 | else: |
| 673 | self.fail("chdir to nonexistant directory %s succeeded." % |
| 674 | nonexistent_dir) |
| 675 | |
| 676 | # Error in the child re-raised in the parent. |
| 677 | try: |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 678 | p = subprocess.Popen([sys.executable, "-c", ""], |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 679 | cwd=nonexistent_dir) |
| 680 | except OSError as e: |
| 681 | # Test that the child process chdir failure actually makes |
| 682 | # it up to the parent process as the correct exception. |
| 683 | self.assertEqual(desired_exception.errno, e.errno) |
| 684 | self.assertEqual(desired_exception.strerror, e.strerror) |
| 685 | else: |
| 686 | self.fail("Expected OSError: %s" % desired_exception) |
| 687 | |
| 688 | def test_restore_signals(self): |
| 689 | # Code coverage for both values of restore_signals to make sure it |
| 690 | # at least does not blow up. |
| 691 | # A test for behavior would be complex. Contributions welcome. |
| 692 | subprocess.call([sys.executable, "-c", ""], restore_signals=True) |
| 693 | subprocess.call([sys.executable, "-c", ""], restore_signals=False) |
| 694 | |
| 695 | def test_start_new_session(self): |
| 696 | # For code coverage of calling setsid(). We don't care if we get an |
| 697 | # EPERM error from it depending on the test execution environment, that |
| 698 | # still indicates that it was called. |
| 699 | try: |
| 700 | output = subprocess.check_output( |
| 701 | [sys.executable, "-c", |
| 702 | "import os; print(os.getpgid(os.getpid()))"], |
| 703 | start_new_session=True) |
| 704 | except OSError as e: |
| 705 | if e.errno != errno.EPERM: |
| 706 | raise |
| 707 | else: |
| 708 | parent_pgid = os.getpgid(os.getpid()) |
| 709 | child_pgid = int(output) |
| 710 | self.assertNotEqual(parent_pgid, child_pgid) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 711 | |
| 712 | def test_run_abort(self): |
| 713 | # returncode handles signal termination |
| 714 | with _SuppressCoreFiles(): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 715 | p = subprocess.Popen([sys.executable, "-c", |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 716 | 'import os; os.abort()']) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 717 | p.wait() |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 718 | self.assertEqual(-p.returncode, signal.SIGABRT) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 719 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 720 | def test_preexec(self): |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 721 | # DISCLAIMER: Setting environment variables is *not* a good use |
| 722 | # of a preexec_fn. This is merely a test. |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 723 | p = subprocess.Popen([sys.executable, "-c", |
| 724 | 'import sys,os;' |
| 725 | 'sys.stdout.write(os.getenv("FRUIT"))'], |
| 726 | stdout=subprocess.PIPE, |
| 727 | preexec_fn=lambda: os.putenv("FRUIT", "apple")) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 728 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 729 | self.assertEqual(p.stdout.read(), b"apple") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 730 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 731 | def test_preexec_exception(self): |
| 732 | def raise_it(): |
| 733 | raise ValueError("What if two swallows carried a coconut?") |
| 734 | try: |
| 735 | p = subprocess.Popen([sys.executable, "-c", ""], |
| 736 | preexec_fn=raise_it) |
| 737 | except RuntimeError as e: |
| 738 | self.assertTrue( |
| 739 | subprocess._posixsubprocess, |
| 740 | "Expected a ValueError from the preexec_fn") |
| 741 | except ValueError as e: |
| 742 | self.assertIn("coconut", e.args[0]) |
| 743 | else: |
| 744 | self.fail("Exception raised by preexec_fn did not make it " |
| 745 | "to the parent process.") |
| 746 | |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 747 | @unittest.skipUnless(gc, "Requires a gc module.") |
| 748 | def test_preexec_gc_module_failure(self): |
| 749 | # This tests the code that disables garbage collection if the child |
| 750 | # process will execute any Python. |
| 751 | def raise_runtime_error(): |
| 752 | raise RuntimeError("this shouldn't escape") |
| 753 | enabled = gc.isenabled() |
| 754 | orig_gc_disable = gc.disable |
| 755 | orig_gc_isenabled = gc.isenabled |
| 756 | try: |
| 757 | gc.disable() |
| 758 | self.assertFalse(gc.isenabled()) |
| 759 | subprocess.call([sys.executable, '-c', ''], |
| 760 | preexec_fn=lambda: None) |
| 761 | self.assertFalse(gc.isenabled(), |
| 762 | "Popen enabled gc when it shouldn't.") |
| 763 | |
| 764 | gc.enable() |
| 765 | self.assertTrue(gc.isenabled()) |
| 766 | subprocess.call([sys.executable, '-c', ''], |
| 767 | preexec_fn=lambda: None) |
| 768 | self.assertTrue(gc.isenabled(), "Popen left gc disabled.") |
| 769 | |
| 770 | gc.disable = raise_runtime_error |
| 771 | self.assertRaises(RuntimeError, subprocess.Popen, |
| 772 | [sys.executable, '-c', ''], |
| 773 | preexec_fn=lambda: None) |
| 774 | |
| 775 | del gc.isenabled # force an AttributeError |
| 776 | self.assertRaises(AttributeError, subprocess.Popen, |
| 777 | [sys.executable, '-c', ''], |
| 778 | preexec_fn=lambda: None) |
| 779 | finally: |
| 780 | gc.disable = orig_gc_disable |
| 781 | gc.isenabled = orig_gc_isenabled |
| 782 | if not enabled: |
| 783 | gc.disable() |
| 784 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 785 | def test_args_string(self): |
| 786 | # args is a string |
| 787 | fd, fname = mkstemp() |
| 788 | # reopen in text mode |
Victor Stinner | f6782ac | 2010-10-16 23:46:43 +0000 | [diff] [blame] | 789 | with open(fd, "w", errors="surrogateescape") as fobj: |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 790 | fobj.write("#!/bin/sh\n") |
| 791 | fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 792 | sys.executable) |
| 793 | os.chmod(fname, 0o700) |
| 794 | p = subprocess.Popen(fname) |
| 795 | p.wait() |
| 796 | os.remove(fname) |
| 797 | self.assertEqual(p.returncode, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 798 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 799 | def test_invalid_args(self): |
| 800 | # invalid arguments should raise ValueError |
| 801 | self.assertRaises(ValueError, subprocess.call, |
| 802 | [sys.executable, "-c", |
| 803 | "import sys; sys.exit(47)"], |
| 804 | startupinfo=47) |
| 805 | self.assertRaises(ValueError, subprocess.call, |
| 806 | [sys.executable, "-c", |
| 807 | "import sys; sys.exit(47)"], |
| 808 | creationflags=47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 809 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 810 | def test_shell_sequence(self): |
| 811 | # Run command through the shell (sequence) |
| 812 | newenv = os.environ.copy() |
| 813 | newenv["FRUIT"] = "apple" |
| 814 | p = subprocess.Popen(["echo $FRUIT"], shell=1, |
| 815 | stdout=subprocess.PIPE, |
| 816 | env=newenv) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 817 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 818 | 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] | 819 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 820 | def test_shell_string(self): |
| 821 | # Run command through the shell (string) |
| 822 | newenv = os.environ.copy() |
| 823 | newenv["FRUIT"] = "apple" |
| 824 | p = subprocess.Popen("echo $FRUIT", shell=1, |
| 825 | stdout=subprocess.PIPE, |
| 826 | env=newenv) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 827 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 828 | 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] | 829 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 830 | def test_call_string(self): |
| 831 | # call() function with string argument on UNIX |
| 832 | fd, fname = mkstemp() |
| 833 | # reopen in text mode |
Victor Stinner | f6782ac | 2010-10-16 23:46:43 +0000 | [diff] [blame] | 834 | with open(fd, "w", errors="surrogateescape") as fobj: |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 835 | fobj.write("#!/bin/sh\n") |
| 836 | fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 837 | sys.executable) |
| 838 | os.chmod(fname, 0o700) |
| 839 | rc = subprocess.call(fname) |
| 840 | os.remove(fname) |
| 841 | self.assertEqual(rc, 47) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 842 | |
Stefan Krah | 9542cc6 | 2010-07-19 14:20:53 +0000 | [diff] [blame] | 843 | def test_specific_shell(self): |
| 844 | # Issue #9265: Incorrect name passed as arg[0]. |
| 845 | shells = [] |
| 846 | for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']: |
| 847 | for name in ['bash', 'ksh']: |
| 848 | sh = os.path.join(prefix, name) |
| 849 | if os.path.isfile(sh): |
| 850 | shells.append(sh) |
| 851 | if not shells: # Will probably work for any shell but csh. |
| 852 | self.skipTest("bash or ksh required for this test") |
| 853 | sh = '/bin/sh' |
| 854 | if os.path.isfile(sh) and not os.path.islink(sh): |
| 855 | # Test will fail if /bin/sh is a symlink to csh. |
| 856 | shells.append(sh) |
| 857 | for sh in shells: |
| 858 | p = subprocess.Popen("echo $0", executable=sh, shell=True, |
| 859 | stdout=subprocess.PIPE) |
Brian Curtin | 3c6a951 | 2010-11-05 03:58:52 +0000 | [diff] [blame] | 860 | self.addCleanup(p.stdout.close) |
Stefan Krah | 9542cc6 | 2010-07-19 14:20:53 +0000 | [diff] [blame] | 861 | self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii')) |
| 862 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 863 | def _kill_process(self, method, *args): |
Florent Xicluna | 1d8ee3a | 2010-03-05 20:26:54 +0000 | [diff] [blame] | 864 | # Do not inherit file handles from the parent. |
| 865 | # It should fix failures on some platforms. |
Antoine Pitrou | 3d8580f | 2010-09-20 01:33:21 +0000 | [diff] [blame] | 866 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 867 | import sys, time |
| 868 | sys.stdout.write('x\\n') |
| 869 | sys.stdout.flush() |
| 870 | time.sleep(30) |
| 871 | """], |
| 872 | close_fds=True, |
| 873 | stdin=subprocess.PIPE, |
| 874 | stdout=subprocess.PIPE, |
| 875 | stderr=subprocess.PIPE) |
| 876 | # Wait for the interpreter to be completely initialized before |
| 877 | # sending any signal. |
| 878 | p.stdout.read(1) |
| 879 | getattr(p, method)(*args) |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 880 | return p |
| 881 | |
| 882 | def test_send_signal(self): |
| 883 | p = self._kill_process('send_signal', signal.SIGINT) |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 884 | _, stderr = p.communicate() |
| 885 | self.assertIn(b'KeyboardInterrupt', stderr) |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 886 | self.assertNotEqual(p.wait(), 0) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 887 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 888 | def test_kill(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 889 | p = self._kill_process('kill') |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 890 | _, stderr = p.communicate() |
| 891 | self.assertStderrEqual(stderr, b'') |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 892 | self.assertEqual(p.wait(), -signal.SIGKILL) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 893 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 894 | def test_terminate(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 895 | p = self._kill_process('terminate') |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 896 | _, stderr = p.communicate() |
| 897 | self.assertStderrEqual(stderr, b'') |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 898 | self.assertEqual(p.wait(), -signal.SIGTERM) |
| 899 | |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 900 | def test_surrogates_error_message(self): |
Victor Stinner | 4d07804 | 2010-04-23 19:28:32 +0000 | [diff] [blame] | 901 | def prepare(): |
| 902 | raise ValueError("surrogate:\uDCff") |
| 903 | |
| 904 | try: |
| 905 | subprocess.call( |
| 906 | [sys.executable, "-c", "pass"], |
| 907 | preexec_fn=prepare) |
| 908 | except ValueError as err: |
| 909 | # Pure Python implementations keeps the message |
| 910 | self.assertIsNone(subprocess._posixsubprocess) |
| 911 | self.assertEqual(str(err), "surrogate:\uDCff") |
| 912 | except RuntimeError as err: |
| 913 | # _posixsubprocess uses a default message |
| 914 | self.assertIsNotNone(subprocess._posixsubprocess) |
| 915 | self.assertEqual(str(err), "Exception occurred in preexec_fn.") |
| 916 | else: |
| 917 | self.fail("Expected ValueError or RuntimeError") |
| 918 | |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 919 | def test_undecodable_env(self): |
| 920 | for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')): |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 921 | # test str with surrogates |
Antoine Pitrou | fb8db8f | 2010-09-19 22:46:05 +0000 | [diff] [blame] | 922 | script = "import os; print(ascii(os.getenv(%s)))" % repr(key) |
Victor Stinner | ce2d24d | 2010-04-23 22:55:39 +0000 | [diff] [blame] | 923 | env = os.environ.copy() |
| 924 | env[key] = value |
Victor Stinner | 89f3ad1 | 2010-10-14 10:43:31 +0000 | [diff] [blame] | 925 | # Use C locale to get ascii for the locale encoding to force |
| 926 | # surrogate-escaping of \xFF in the child process; otherwise it can |
| 927 | # be decoded as-is if the default locale is latin-1. |
Victor Stinner | ebc78d2 | 2010-10-14 10:38:17 +0000 | [diff] [blame] | 928 | env['LC_ALL'] = 'C' |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 929 | stdout = subprocess.check_output( |
| 930 | [sys.executable, "-c", script], |
Victor Stinner | ce2d24d | 2010-04-23 22:55:39 +0000 | [diff] [blame] | 931 | env=env) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 932 | stdout = stdout.rstrip(b'\n\r') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 933 | self.assertEqual(stdout.decode('ascii'), ascii(value)) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 934 | |
| 935 | # test bytes |
| 936 | key = key.encode("ascii", "surrogateescape") |
| 937 | value = value.encode("ascii", "surrogateescape") |
Antoine Pitrou | fb8db8f | 2010-09-19 22:46:05 +0000 | [diff] [blame] | 938 | script = "import os; print(ascii(os.getenvb(%s)))" % repr(key) |
Victor Stinner | ce2d24d | 2010-04-23 22:55:39 +0000 | [diff] [blame] | 939 | env = os.environ.copy() |
| 940 | env[key] = value |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 941 | stdout = subprocess.check_output( |
| 942 | [sys.executable, "-c", script], |
Victor Stinner | ce2d24d | 2010-04-23 22:55:39 +0000 | [diff] [blame] | 943 | env=env) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 944 | stdout = stdout.rstrip(b'\n\r') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 945 | self.assertEqual(stdout.decode('ascii'), ascii(value)) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 946 | |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 947 | def test_bytes_program(self): |
| 948 | abs_program = os.fsencode(sys.executable) |
| 949 | path, program = os.path.split(sys.executable) |
| 950 | program = os.fsencode(program) |
| 951 | |
| 952 | # absolute bytes path |
| 953 | exitcode = subprocess.call([abs_program, "-c", "pass"]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 954 | self.assertEqual(exitcode, 0) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 955 | |
| 956 | # bytes program, unicode PATH |
| 957 | env = os.environ.copy() |
| 958 | env["PATH"] = path |
| 959 | exitcode = subprocess.call([program, "-c", "pass"], env=env) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 960 | self.assertEqual(exitcode, 0) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 961 | |
| 962 | # bytes program, bytes PATH |
| 963 | envb = os.environb.copy() |
| 964 | envb[b"PATH"] = os.fsencode(path) |
| 965 | exitcode = subprocess.call([program, "-c", "pass"], env=envb) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 966 | self.assertEqual(exitcode, 0) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 967 | |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 968 | def test_pipe_cloexec(self): |
| 969 | sleeper = support.findfile("input_reader.py", subdir="subprocessdata") |
| 970 | fd_status = support.findfile("fd_status.py", subdir="subprocessdata") |
| 971 | |
| 972 | p1 = subprocess.Popen([sys.executable, sleeper], |
| 973 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 974 | stderr=subprocess.PIPE, close_fds=False) |
| 975 | |
| 976 | self.addCleanup(p1.communicate, b'') |
| 977 | |
| 978 | p2 = subprocess.Popen([sys.executable, fd_status], |
| 979 | stdout=subprocess.PIPE, close_fds=False) |
| 980 | |
| 981 | output, error = p2.communicate() |
| 982 | result_fds = set(map(int, output.split(b','))) |
| 983 | unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(), |
| 984 | p1.stderr.fileno()]) |
| 985 | |
| 986 | self.assertFalse(result_fds & unwanted_fds, |
| 987 | "Expected no fds from %r to be open in child, " |
| 988 | "found %r" % |
| 989 | (unwanted_fds, result_fds & unwanted_fds)) |
| 990 | |
| 991 | def test_pipe_cloexec_real_tools(self): |
| 992 | qcat = support.findfile("qcat.py", subdir="subprocessdata") |
| 993 | qgrep = support.findfile("qgrep.py", subdir="subprocessdata") |
| 994 | |
| 995 | subdata = b'zxcvbn' |
| 996 | data = subdata * 4 + b'\n' |
| 997 | |
| 998 | p1 = subprocess.Popen([sys.executable, qcat], |
| 999 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 1000 | close_fds=False) |
| 1001 | |
| 1002 | p2 = subprocess.Popen([sys.executable, qgrep, subdata], |
| 1003 | stdin=p1.stdout, stdout=subprocess.PIPE, |
| 1004 | close_fds=False) |
| 1005 | |
| 1006 | self.addCleanup(p1.wait) |
| 1007 | self.addCleanup(p2.wait) |
| 1008 | self.addCleanup(p1.terminate) |
| 1009 | self.addCleanup(p2.terminate) |
| 1010 | |
| 1011 | p1.stdin.write(data) |
| 1012 | p1.stdin.close() |
| 1013 | |
| 1014 | readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10) |
| 1015 | |
| 1016 | self.assertTrue(readfiles, "The child hung") |
| 1017 | self.assertEqual(p2.stdout.read(), data) |
| 1018 | |
| 1019 | def test_close_fds(self): |
| 1020 | fd_status = support.findfile("fd_status.py", subdir="subprocessdata") |
| 1021 | |
| 1022 | fds = os.pipe() |
| 1023 | self.addCleanup(os.close, fds[0]) |
| 1024 | self.addCleanup(os.close, fds[1]) |
| 1025 | |
| 1026 | open_fds = set(fds) |
| 1027 | |
| 1028 | p = subprocess.Popen([sys.executable, fd_status], |
| 1029 | stdout=subprocess.PIPE, close_fds=False) |
| 1030 | output, ignored = p.communicate() |
| 1031 | remaining_fds = set(map(int, output.split(b','))) |
| 1032 | |
| 1033 | self.assertEqual(remaining_fds & open_fds, open_fds, |
| 1034 | "Some fds were closed") |
| 1035 | |
| 1036 | p = subprocess.Popen([sys.executable, fd_status], |
| 1037 | stdout=subprocess.PIPE, close_fds=True) |
| 1038 | output, ignored = p.communicate() |
| 1039 | remaining_fds = set(map(int, output.split(b','))) |
| 1040 | |
| 1041 | self.assertFalse(remaining_fds & open_fds, |
| 1042 | "Some fds were left open") |
| 1043 | self.assertIn(1, remaining_fds, "Subprocess failed") |
| 1044 | |
Gregory P. Smith | 8edd99d | 2010-12-14 13:43:30 +0000 | [diff] [blame] | 1045 | def test_pass_fds(self): |
| 1046 | fd_status = support.findfile("fd_status.py", subdir="subprocessdata") |
| 1047 | |
| 1048 | open_fds = set() |
| 1049 | |
| 1050 | for x in range(5): |
| 1051 | fds = os.pipe() |
| 1052 | self.addCleanup(os.close, fds[0]) |
| 1053 | self.addCleanup(os.close, fds[1]) |
| 1054 | open_fds.update(fds) |
| 1055 | |
| 1056 | for fd in open_fds: |
| 1057 | p = subprocess.Popen([sys.executable, fd_status], |
| 1058 | stdout=subprocess.PIPE, close_fds=True, |
| 1059 | pass_fds=(fd, )) |
| 1060 | output, ignored = p.communicate() |
| 1061 | |
| 1062 | remaining_fds = set(map(int, output.split(b','))) |
| 1063 | to_be_closed = open_fds - {fd} |
| 1064 | |
| 1065 | self.assertIn(fd, remaining_fds, "fd to be passed not passed") |
| 1066 | self.assertFalse(remaining_fds & to_be_closed, |
| 1067 | "fd to be closed passed") |
| 1068 | |
| 1069 | # pass_fds overrides close_fds with a warning. |
| 1070 | with self.assertWarns(RuntimeWarning) as context: |
| 1071 | self.assertFalse(subprocess.call( |
| 1072 | [sys.executable, "-c", "import sys; sys.exit(0)"], |
| 1073 | close_fds=False, pass_fds=(fd, ))) |
| 1074 | self.assertIn('overriding close_fds', str(context.warning)) |
| 1075 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1076 | |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 1077 | @unittest.skipUnless(mswindows, "Windows specific tests") |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 1078 | class Win32ProcessTestCase(BaseTestCase): |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 1079 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1080 | def test_startupinfo(self): |
| 1081 | # startupinfo argument |
| 1082 | # We uses hardcoded constants, because we do not want to |
| 1083 | # depend on win32all. |
| 1084 | STARTF_USESHOWWINDOW = 1 |
| 1085 | SW_MAXIMIZE = 3 |
| 1086 | startupinfo = subprocess.STARTUPINFO() |
| 1087 | startupinfo.dwFlags = STARTF_USESHOWWINDOW |
| 1088 | startupinfo.wShowWindow = SW_MAXIMIZE |
| 1089 | # Since Python is a console process, it won't be affected |
| 1090 | # by wShowWindow, but the argument should be silently |
| 1091 | # ignored |
| 1092 | subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1093 | startupinfo=startupinfo) |
| 1094 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1095 | def test_creationflags(self): |
| 1096 | # creationflags argument |
| 1097 | CREATE_NEW_CONSOLE = 16 |
| 1098 | sys.stderr.write(" a DOS box should flash briefly ...\n") |
| 1099 | subprocess.call(sys.executable + |
| 1100 | ' -c "import time; time.sleep(0.25)"', |
| 1101 | creationflags=CREATE_NEW_CONSOLE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1102 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1103 | def test_invalid_args(self): |
| 1104 | # invalid arguments should raise ValueError |
| 1105 | self.assertRaises(ValueError, subprocess.call, |
| 1106 | [sys.executable, "-c", |
| 1107 | "import sys; sys.exit(47)"], |
| 1108 | preexec_fn=lambda: 1) |
| 1109 | self.assertRaises(ValueError, subprocess.call, |
| 1110 | [sys.executable, "-c", |
| 1111 | "import sys; sys.exit(47)"], |
| 1112 | stdout=subprocess.PIPE, |
| 1113 | close_fds=True) |
| 1114 | |
| 1115 | def test_close_fds(self): |
| 1116 | # close file descriptors |
| 1117 | rc = subprocess.call([sys.executable, "-c", |
| 1118 | "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1119 | close_fds=True) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1120 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1121 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1122 | def test_shell_sequence(self): |
| 1123 | # Run command through the shell (sequence) |
| 1124 | newenv = os.environ.copy() |
| 1125 | newenv["FRUIT"] = "physalis" |
| 1126 | p = subprocess.Popen(["set"], shell=1, |
| 1127 | stdout=subprocess.PIPE, |
| 1128 | env=newenv) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 1129 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1130 | self.assertIn(b"physalis", p.stdout.read()) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1131 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1132 | def test_shell_string(self): |
| 1133 | # Run command through the shell (string) |
| 1134 | newenv = os.environ.copy() |
| 1135 | newenv["FRUIT"] = "physalis" |
| 1136 | p = subprocess.Popen("set", shell=1, |
| 1137 | stdout=subprocess.PIPE, |
| 1138 | env=newenv) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 1139 | self.addCleanup(p.stdout.close) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1140 | self.assertIn(b"physalis", p.stdout.read()) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1141 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1142 | def test_call_string(self): |
| 1143 | # call() function with string argument on Windows |
| 1144 | rc = subprocess.call(sys.executable + |
| 1145 | ' -c "import sys; sys.exit(47)"') |
| 1146 | self.assertEqual(rc, 47) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1147 | |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1148 | def _kill_process(self, method, *args): |
| 1149 | # Some win32 buildbot raises EOFError if stdin is inherited |
Antoine Pitrou | a4024e2 | 2010-09-24 18:57:01 +0000 | [diff] [blame] | 1150 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 1151 | import sys, time |
| 1152 | sys.stdout.write('x\\n') |
| 1153 | sys.stdout.flush() |
| 1154 | time.sleep(30) |
| 1155 | """], |
| 1156 | stdin=subprocess.PIPE, |
| 1157 | stdout=subprocess.PIPE, |
| 1158 | stderr=subprocess.PIPE) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 1159 | self.addCleanup(p.stdout.close) |
| 1160 | self.addCleanup(p.stderr.close) |
| 1161 | self.addCleanup(p.stdin.close) |
Antoine Pitrou | a4024e2 | 2010-09-24 18:57:01 +0000 | [diff] [blame] | 1162 | # Wait for the interpreter to be completely initialized before |
| 1163 | # sending any signal. |
| 1164 | p.stdout.read(1) |
| 1165 | getattr(p, method)(*args) |
Florent Xicluna | c049d87 | 2010-03-27 22:47:23 +0000 | [diff] [blame] | 1166 | _, stderr = p.communicate() |
| 1167 | self.assertStderrEqual(stderr, b'') |
Antoine Pitrou | a4024e2 | 2010-09-24 18:57:01 +0000 | [diff] [blame] | 1168 | returncode = p.wait() |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1169 | self.assertNotEqual(returncode, 0) |
| 1170 | |
| 1171 | def test_send_signal(self): |
| 1172 | self._kill_process('send_signal', signal.SIGTERM) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1173 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1174 | def test_kill(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1175 | self._kill_process('kill') |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1176 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1177 | def test_terminate(self): |
Florent Xicluna | 4886d24 | 2010-03-08 13:27:26 +0000 | [diff] [blame] | 1178 | self._kill_process('terminate') |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1179 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1180 | |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 1181 | # The module says: |
| 1182 | # "NB This only works (and is only relevant) for UNIX." |
| 1183 | # |
| 1184 | # Actually, getoutput should work on any platform with an os.popen, but |
| 1185 | # I'll take the comment as given, and skip this suite. |
Florent Xicluna | f0cbd82 | 2010-03-04 21:50:56 +0000 | [diff] [blame] | 1186 | @unittest.skipUnless(os.name == 'posix', "only relevant for UNIX") |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1187 | class CommandTests(unittest.TestCase): |
| 1188 | def test_getoutput(self): |
| 1189 | self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy') |
| 1190 | self.assertEqual(subprocess.getstatusoutput('echo xyzzy'), |
| 1191 | (0, 'xyzzy')) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 1192 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1193 | # we use mkdtemp in the next line to create an empty directory |
| 1194 | # under our exclusive control; from that, we can invent a pathname |
| 1195 | # that we _know_ won't exist. This is guaranteed to fail. |
| 1196 | dir = None |
| 1197 | try: |
| 1198 | dir = tempfile.mkdtemp() |
| 1199 | name = os.path.join(dir, "foo") |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 1200 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1201 | status, output = subprocess.getstatusoutput('cat ' + name) |
| 1202 | self.assertNotEqual(status, 0) |
| 1203 | finally: |
| 1204 | if dir is not None: |
| 1205 | os.rmdir(dir) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 1206 | |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 1207 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1208 | @unittest.skipUnless(getattr(subprocess, '_has_poll', False), |
| 1209 | "poll system call not supported") |
| 1210 | class ProcessTestCaseNoPoll(ProcessTestCase): |
| 1211 | def setUp(self): |
| 1212 | subprocess._has_poll = False |
| 1213 | ProcessTestCase.setUp(self) |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 1214 | |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1215 | def tearDown(self): |
| 1216 | subprocess._has_poll = True |
| 1217 | ProcessTestCase.tearDown(self) |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 1218 | |
| 1219 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1220 | @unittest.skipUnless(getattr(subprocess, '_posixsubprocess', False), |
| 1221 | "_posixsubprocess extension module not found.") |
| 1222 | class ProcessTestCasePOSIXPurePython(ProcessTestCase, POSIXProcessTestCase): |
| 1223 | def setUp(self): |
| 1224 | subprocess._posixsubprocess = None |
| 1225 | ProcessTestCase.setUp(self) |
| 1226 | POSIXProcessTestCase.setUp(self) |
| 1227 | |
| 1228 | def tearDown(self): |
| 1229 | subprocess._posixsubprocess = sys.modules['_posixsubprocess'] |
| 1230 | POSIXProcessTestCase.tearDown(self) |
| 1231 | ProcessTestCase.tearDown(self) |
| 1232 | |
| 1233 | |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +0000 | [diff] [blame] | 1234 | class HelperFunctionTests(unittest.TestCase): |
Gregory P. Smith | af6d3b8 | 2010-03-01 02:56:44 +0000 | [diff] [blame] | 1235 | @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows") |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +0000 | [diff] [blame] | 1236 | def test_eintr_retry_call(self): |
| 1237 | record_calls = [] |
| 1238 | def fake_os_func(*args): |
| 1239 | record_calls.append(args) |
| 1240 | if len(record_calls) == 2: |
| 1241 | raise OSError(errno.EINTR, "fake interrupted system call") |
| 1242 | return tuple(reversed(args)) |
| 1243 | |
| 1244 | self.assertEqual((999, 256), |
| 1245 | subprocess._eintr_retry_call(fake_os_func, 256, 999)) |
| 1246 | self.assertEqual([(256, 999)], record_calls) |
| 1247 | # This time there will be an EINTR so it will loop once. |
| 1248 | self.assertEqual((666,), |
| 1249 | subprocess._eintr_retry_call(fake_os_func, 666)) |
| 1250 | self.assertEqual([(256, 999), (666,), (666,)], record_calls) |
| 1251 | |
| 1252 | |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 1253 | @unittest.skipUnless(mswindows, "Windows-specific tests") |
| 1254 | class CommandsWithSpaces (BaseTestCase): |
| 1255 | |
| 1256 | def setUp(self): |
| 1257 | super().setUp() |
| 1258 | f, fname = mkstemp(".py", "te st") |
| 1259 | self.fname = fname.lower () |
| 1260 | os.write(f, b"import sys;" |
| 1261 | b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))" |
| 1262 | ) |
| 1263 | os.close(f) |
| 1264 | |
| 1265 | def tearDown(self): |
| 1266 | os.remove(self.fname) |
| 1267 | super().tearDown() |
| 1268 | |
| 1269 | def with_spaces(self, *args, **kwargs): |
| 1270 | kwargs['stdout'] = subprocess.PIPE |
| 1271 | p = subprocess.Popen(*args, **kwargs) |
Brian Curtin | 19a5379 | 2010-11-05 17:09:05 +0000 | [diff] [blame] | 1272 | self.addCleanup(p.stdout.close) |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 1273 | self.assertEqual( |
| 1274 | p.stdout.read ().decode("mbcs"), |
| 1275 | "2 [%r, 'ab cd']" % self.fname |
| 1276 | ) |
| 1277 | |
| 1278 | def test_shell_string_with_spaces(self): |
| 1279 | # call() function with string argument with spaces on Windows |
Brian Curtin | d835cf1 | 2010-08-13 20:42:57 +0000 | [diff] [blame] | 1280 | self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, |
| 1281 | "ab cd"), shell=1) |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 1282 | |
| 1283 | def test_shell_sequence_with_spaces(self): |
| 1284 | # call() function with sequence argument with spaces on Windows |
Brian Curtin | d835cf1 | 2010-08-13 20:42:57 +0000 | [diff] [blame] | 1285 | self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1) |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 1286 | |
| 1287 | def test_noshell_string_with_spaces(self): |
| 1288 | # call() function with string argument with spaces on Windows |
| 1289 | self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, |
| 1290 | "ab cd")) |
| 1291 | |
| 1292 | def test_noshell_sequence_with_spaces(self): |
| 1293 | # call() function with sequence argument with spaces on Windows |
| 1294 | self.with_spaces([sys.executable, self.fname, "ab cd"]) |
| 1295 | |
Brian Curtin | 79cdb66 | 2010-12-03 02:46:02 +0000 | [diff] [blame] | 1296 | |
| 1297 | class ContextManagerTests(ProcessTestCase): |
| 1298 | |
| 1299 | def test_pipe(self): |
| 1300 | with subprocess.Popen([sys.executable, "-c", |
| 1301 | "import sys;" |
| 1302 | "sys.stdout.write('stdout');" |
| 1303 | "sys.stderr.write('stderr');"], |
| 1304 | stdout=subprocess.PIPE, |
| 1305 | stderr=subprocess.PIPE) as proc: |
| 1306 | self.assertEqual(proc.stdout.read(), b"stdout") |
| 1307 | self.assertStderrEqual(proc.stderr.read(), b"stderr") |
| 1308 | |
| 1309 | self.assertTrue(proc.stdout.closed) |
| 1310 | self.assertTrue(proc.stderr.closed) |
| 1311 | |
| 1312 | def test_returncode(self): |
| 1313 | with subprocess.Popen([sys.executable, "-c", |
| 1314 | "import sys; sys.exit(100)"]) as proc: |
| 1315 | proc.wait() |
| 1316 | self.assertEqual(proc.returncode, 100) |
| 1317 | |
| 1318 | def test_communicate_stdin(self): |
| 1319 | with subprocess.Popen([sys.executable, "-c", |
| 1320 | "import sys;" |
| 1321 | "sys.exit(sys.stdin.read() == 'context')"], |
| 1322 | stdin=subprocess.PIPE) as proc: |
| 1323 | proc.communicate(b"context") |
| 1324 | self.assertEqual(proc.returncode, 1) |
| 1325 | |
| 1326 | def test_invalid_args(self): |
| 1327 | with self.assertRaises(EnvironmentError) as c: |
| 1328 | with subprocess.Popen(['nonexisting_i_hope'], |
| 1329 | stdout=subprocess.PIPE, |
| 1330 | stderr=subprocess.PIPE) as proc: |
| 1331 | pass |
| 1332 | |
| 1333 | if c.exception.errno != errno.ENOENT: # ignore "no such file" |
| 1334 | raise c.exception |
| 1335 | |
| 1336 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1337 | def test_main(): |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1338 | unit_tests = (ProcessTestCase, |
| 1339 | POSIXProcessTestCase, |
| 1340 | Win32ProcessTestCase, |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1341 | ProcessTestCasePOSIXPurePython, |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1342 | CommandTests, |
Gregory P. Smith | a59c59f | 2010-03-01 00:17:40 +0000 | [diff] [blame] | 1343 | ProcessTestCaseNoPoll, |
Tim Golden | 126c296 | 2010-08-11 14:20:40 +0000 | [diff] [blame] | 1344 | HelperFunctionTests, |
Brian Curtin | 79cdb66 | 2010-12-03 02:46:02 +0000 | [diff] [blame] | 1345 | CommandsWithSpaces, |
Gregory P. Smith | f560485 | 2010-12-13 06:45:02 +0000 | [diff] [blame] | 1346 | ContextManagerTests) |
Florent Xicluna | b1e94e8 | 2010-02-27 22:12:37 +0000 | [diff] [blame] | 1347 | |
Gregory P. Smith | d06fa47 | 2009-07-04 02:46:54 +0000 | [diff] [blame] | 1348 | support.run_unittest(*unit_tests) |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 1349 | support.reap_children() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1350 | |
| 1351 | if __name__ == "__main__": |
Brett Cannon | a23810f | 2008-05-26 19:04:21 +0000 | [diff] [blame] | 1352 | test_main() |