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