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