Debian Codegraph @

Lib/test/test_subprocess.py

raw
1 import unittest
2 from unittest import mock
3 from test import support
4 from test.support import check_sanitizer
5 from test.support import import_helper
6 from test.support import os_helper
7 from test.support import strace_helper
8 from test.support import warnings_helper
9 from test.support.script_helper import assert_python_ok
10 import subprocess
11 import sys
12 import signal
13 import io
14 import itertools
15 import os
16 import errno
17 import tempfile
18 import time
19 import traceback
20 import types
21 import selectors
22 import sysconfig
23 import select
24 import shutil
25 import socket
26 import threading
27 import gc
28 import textwrap
29 import json
30 from test.support.os_helper import FakePath
31
32 try:
33 import _testcapi
34 except ImportError:
35 _testcapi = None
36
37 try:
38 import pwd
39 except ImportError:
40 pwd = None
41 try:
42 import grp
43 except ImportError:
44 grp = None
45 try:
46 import resource
47 except ImportError:
48 resource = None
49
50 try:
51 import fcntl
52 except:
53 fcntl = None
54
55 if support.PGO:
56 raise unittest.SkipTest("test is not helpful for PGO")
57
58 if not support.has_subprocess_support:
59 raise unittest.SkipTest("test module requires subprocess")
60
61 mswindows = (sys.platform == "win32")
62
63 #
64 # Depends on the following external programs: Python
65 #
66
67 if mswindows:
68 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
69 'os.O_BINARY);')
70 else:
71 SETBINARY = ''
72
73 NONEXISTING_CMD = ('nonexisting_i_hope',)
74 # Ignore errors that indicate the command was not found
75 NONEXISTING_ERRORS = (FileNotFoundError, NotADirectoryError, PermissionError)
76
77 ZERO_RETURN_CMD = (sys.executable, '-c', 'pass')
78
79
80 def setUpModule():
81 shell_true = shutil.which('true')
82 if shell_true is None:
83 return
84 if (os.access(shell_true, os.X_OK) and
85 subprocess.run([shell_true]).returncode == 0):
86 global ZERO_RETURN_CMD
87 ZERO_RETURN_CMD = (shell_true,) # Faster than Python startup.
88
89
90 class BaseTestCase(unittest.TestCase):
91 def setUp(self):
92 # Try to minimize the number of children we have so this test
93 # doesn't crash on some buildbots (Alphas in particular).
94 support.reap_children()
95
96 def tearDown(self):
97 if not mswindows:
98 # subprocess._active is not used on Windows and is set to None.
99 for inst in subprocess._active:
100 inst.wait()
101 subprocess._cleanup()
102 self.assertFalse(
103 subprocess._active, "subprocess._active not empty"
104 )
105 self.doCleanups()
106 support.reap_children()
107
108
109 class PopenTestException(Exception):
110 pass
111
112
113 class PopenExecuteChildRaises(subprocess.Popen):
114 """Popen subclass for testing cleanup of subprocess.PIPE filehandles when
115 _execute_child fails.
116 """
117 def _execute_child(self, *args, **kwargs):
118 raise PopenTestException("Forced Exception for Test")
119
120
121 class ProcessTestCase(BaseTestCase):
122
123 def test_io_buffered_by_default(self):
124 p = subprocess.Popen(ZERO_RETURN_CMD,
125 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
126 stderr=subprocess.PIPE)
127 try:
128 self.assertIsInstance(p.stdin, io.BufferedIOBase)
129 self.assertIsInstance(p.stdout, io.BufferedIOBase)
130 self.assertIsInstance(p.stderr, io.BufferedIOBase)
131 finally:
132 p.stdin.close()
133 p.stdout.close()
134 p.stderr.close()
135 p.wait()
136
137 def test_io_unbuffered_works(self):
138 p = subprocess.Popen(ZERO_RETURN_CMD,
139 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
140 stderr=subprocess.PIPE, bufsize=0)
141 try:
142 self.assertIsInstance(p.stdin, io.RawIOBase)
143 self.assertIsInstance(p.stdout, io.RawIOBase)
144 self.assertIsInstance(p.stderr, io.RawIOBase)
145 finally:
146 p.stdin.close()
147 p.stdout.close()
148 p.stderr.close()
149 p.wait()
150
151 def test_call_seq(self):
152 # call() function with sequence argument
153 rc = subprocess.call([sys.executable, "-c",
154 "import sys; sys.exit(47)"])
155 self.assertEqual(rc, 47)
156
157 def test_call_timeout(self):
158 # call() function with timeout argument; we want to test that the child
159 # process gets killed when the timeout expires. If the child isn't
160 # killed, this call will deadlock since subprocess.call waits for the
161 # child.
162 self.assertRaises(subprocess.TimeoutExpired, subprocess.call,
163 [sys.executable, "-c", "while True: pass"],
164 timeout=0.1)
165
166 def test_timeout_exception(self):
167 try:
168 subprocess.run([sys.executable, '-c', 'import time;time.sleep(9)'], timeout = -1)
169 except subprocess.TimeoutExpired as e:
170 self.assertIn("-1 seconds", str(e))
171 else:
172 self.fail("Expected TimeoutExpired exception not raised")
173 try:
174 subprocess.run([sys.executable, '-c', 'import time;time.sleep(9)'], timeout = 0)
175 except subprocess.TimeoutExpired as e:
176 self.assertIn("0 seconds", str(e))
177 else:
178 self.fail("Expected TimeoutExpired exception not raised")
179
180 def test_check_call_zero(self):
181 # check_call() function with zero return code
182 rc = subprocess.check_call(ZERO_RETURN_CMD)
183 self.assertEqual(rc, 0)
184
185 def test_check_call_nonzero(self):
186 # check_call() function with non-zero return code
187 with self.assertRaises(subprocess.CalledProcessError) as c:
188 subprocess.check_call([sys.executable, "-c",
189 "import sys; sys.exit(47)"])
190 self.assertEqual(c.exception.returncode, 47)
191
192 def test_check_output(self):
193 # check_output() function with zero return code
194 output = subprocess.check_output(
195 [sys.executable, "-c", "print('BDFL')"])
196 self.assertIn(b'BDFL', output)
197
198 with self.assertRaisesRegex(ValueError,
199 "stdout argument not allowed, it will be overridden"):
200 subprocess.check_output([], stdout=None)
201
202 with self.assertRaisesRegex(ValueError,
203 "check argument not allowed, it will be overridden"):
204 subprocess.check_output([], check=False)
205
206 def test_check_output_nonzero(self):
207 # check_call() function with non-zero return code
208 with self.assertRaises(subprocess.CalledProcessError) as c:
209 subprocess.check_output(
210 [sys.executable, "-c", "import sys; sys.exit(5)"])
211 self.assertEqual(c.exception.returncode, 5)
212
213 def test_check_output_stderr(self):
214 # check_output() function stderr redirected to stdout
215 output = subprocess.check_output(
216 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
217 stderr=subprocess.STDOUT)
218 self.assertIn(b'BDFL', output)
219
220 def test_check_output_stdin_arg(self):
221 # check_output() can be called with stdin set to a file
222 tf = tempfile.TemporaryFile()
223 self.addCleanup(tf.close)
224 tf.write(b'pear')
225 tf.seek(0)
226 output = subprocess.check_output(
227 [sys.executable, "-c",
228 "import sys; sys.stdout.write(sys.stdin.read().upper())"],
229 stdin=tf)
230 self.assertIn(b'PEAR', output)
231
232 def test_check_output_input_arg(self):
233 # check_output() can be called with input set to a string
234 output = subprocess.check_output(
235 [sys.executable, "-c",
236 "import sys; sys.stdout.write(sys.stdin.read().upper())"],
237 input=b'pear')
238 self.assertIn(b'PEAR', output)
239
240 def test_check_output_input_none(self):
241 """input=None has a legacy meaning of input='' on check_output."""
242 output = subprocess.check_output(
243 [sys.executable, "-c",
244 "import sys; print('XX' if sys.stdin.read() else '')"],
245 input=None)
246 self.assertNotIn(b'XX', output)
247
248 def test_check_output_input_none_text(self):
249 output = subprocess.check_output(
250 [sys.executable, "-c",
251 "import sys; print('XX' if sys.stdin.read() else '')"],
252 input=None, text=True)
253 self.assertNotIn('XX', output)
254
255 def test_check_output_input_none_universal_newlines(self):
256 output = subprocess.check_output(
257 [sys.executable, "-c",
258 "import sys; print('XX' if sys.stdin.read() else '')"],
259 input=None, universal_newlines=True)
260 self.assertNotIn('XX', output)
261
262 def test_check_output_input_none_encoding_errors(self):
263 output = subprocess.check_output(
264 [sys.executable, "-c", "print('foo')"],
265 input=None, encoding='utf-8', errors='ignore')
266 self.assertIn('foo', output)
267
268 def test_check_output_stdout_arg(self):
269 # check_output() refuses to accept 'stdout' argument
270 with self.assertRaises(ValueError) as c:
271 output = subprocess.check_output(
272 [sys.executable, "-c", "print('will not be run')"],
273 stdout=sys.stdout)
274 self.fail("Expected ValueError when stdout arg supplied.")
275 self.assertIn('stdout', c.exception.args[0])
276
277 def test_check_output_stdin_with_input_arg(self):
278 # check_output() refuses to accept 'stdin' with 'input'
279 tf = tempfile.TemporaryFile()
280 self.addCleanup(tf.close)
281 tf.write(b'pear')
282 tf.seek(0)
283 with self.assertRaises(ValueError) as c:
284 output = subprocess.check_output(
285 [sys.executable, "-c", "print('will not be run')"],
286 stdin=tf, input=b'hare')
287 self.fail("Expected ValueError when stdin and input args supplied.")
288 self.assertIn('stdin', c.exception.args[0])
289 self.assertIn('input', c.exception.args[0])
290
291 def test_check_output_timeout(self):
292 # check_output() function with timeout arg
293 with self.assertRaises(subprocess.TimeoutExpired) as c:
294 output = subprocess.check_output(
295 [sys.executable, "-c",
296 "import time; time.sleep(3600)"],
297 timeout=0.1)
298
299 def test_call_kwargs(self):
300 # call() function with keyword args
301 newenv = os.environ.copy()
302 newenv["FRUIT"] = "banana"
303 rc = subprocess.call([sys.executable, "-c",
304 'import sys, os;'
305 'sys.exit(os.getenv("FRUIT")=="banana")'],
306 env=newenv)
307 self.assertEqual(rc, 1)
308
309 def test_invalid_args(self):
310 # Popen() called with invalid arguments should raise TypeError
311 # but Popen.__del__ should not complain (issue #12085)
312 with support.captured_stderr() as s:
313 self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
314 argcount = subprocess.Popen.__init__.__code__.co_argcount
315 too_many_args = [0] * (argcount + 1)
316 self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
317 self.assertEqual(s.getvalue(), '')
318
319 def test_stdin_none(self):
320 # .stdin is None when not redirected
321 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
322 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
323 self.addCleanup(p.stdout.close)
324 self.addCleanup(p.stderr.close)
325 p.wait()
326 self.assertEqual(p.stdin, None)
327
328 def test_stdout_none(self):
329 # .stdout is None when not redirected, and the child's stdout will
330 # be inherited from the parent. In order to test this we run a
331 # subprocess in a subprocess:
332 # this_test
333 # \-- subprocess created by this test (parent)
334 # \-- subprocess created by the parent subprocess (child)
335 # The parent doesn't specify stdout, so the child will use the
336 # parent's stdout. This test checks that the message printed by the
337 # child goes to the parent stdout. The parent also checks that the
338 # child's stdout is None. See #11963.
339 code = ('import sys; from subprocess import Popen, PIPE;'
340 'p = Popen([sys.executable, "-c", "print(\'test_stdout_none\')"],'
341 ' stdin=PIPE, stderr=PIPE);'
342 'p.wait(); assert p.stdout is None;')
343 p = subprocess.Popen([sys.executable, "-c", code],
344 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
345 self.addCleanup(p.stdout.close)
346 self.addCleanup(p.stderr.close)
347 out, err = p.communicate()
348 self.assertEqual(p.returncode, 0, err)
349 self.assertEqual(out.rstrip(), b'test_stdout_none')
350
351 def test_stderr_none(self):
352 # .stderr is None when not redirected
353 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
354 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
355 self.addCleanup(p.stdout.close)
356 self.addCleanup(p.stdin.close)
357 p.wait()
358 self.assertEqual(p.stderr, None)
359
360 def _assert_python(self, pre_args, **kwargs):
361 # We include sys.exit() to prevent the test runner from hanging
362 # whenever python is found.
363 args = pre_args + ["import sys; sys.exit(47)"]
364 p = subprocess.Popen(args, **kwargs)
365 p.wait()
366 self.assertEqual(47, p.returncode)
367
368 def test_executable(self):
369 # Check that the executable argument works.
370 #
371 # On Unix (non-Mac and non-Windows), Python looks at args[0] to
372 # determine where its standard library is, so we need the directory
373 # of args[0] to be valid for the Popen() call to Python to succeed.
374 # See also issue #16170 and issue #7774.
375 doesnotexist = os.path.join(os.path.dirname(sys.executable),
376 "doesnotexist")
377 self._assert_python([doesnotexist, "-c"], executable=sys.executable)
378
379 def test_bytes_executable(self):
380 doesnotexist = os.path.join(os.path.dirname(sys.executable),
381 "doesnotexist")
382 self._assert_python([doesnotexist, "-c"],
383 executable=os.fsencode(sys.executable))
384
385 def test_pathlike_executable(self):
386 doesnotexist = os.path.join(os.path.dirname(sys.executable),
387 "doesnotexist")
388 self._assert_python([doesnotexist, "-c"],
389 executable=FakePath(sys.executable))
390
391 def test_executable_takes_precedence(self):
392 # Check that the executable argument takes precedence over args[0].
393 #
394 # Verify first that the call succeeds without the executable arg.
395 pre_args = [sys.executable, "-c"]
396 self._assert_python(pre_args)
397 self.assertRaises(NONEXISTING_ERRORS,
398 self._assert_python, pre_args,
399 executable=NONEXISTING_CMD[0])
400
401 @unittest.skipIf(mswindows, "executable argument replaces shell")
402 def test_executable_replaces_shell(self):
403 # Check that the executable argument replaces the default shell
404 # when shell=True.
405 self._assert_python([], executable=sys.executable, shell=True)
406
407 @unittest.skipIf(mswindows, "executable argument replaces shell")
408 def test_bytes_executable_replaces_shell(self):
409 self._assert_python([], executable=os.fsencode(sys.executable),
410 shell=True)
411
412 @unittest.skipIf(mswindows, "executable argument replaces shell")
413 def test_pathlike_executable_replaces_shell(self):
414 self._assert_python([], executable=FakePath(sys.executable),
415 shell=True)
416
417 # For use in the test_cwd* tests below.
418 def _normalize_cwd(self, cwd):
419 # Normalize an expected cwd (for Tru64 support).
420 # We can't use os.path.realpath since it doesn't expand Tru64 {memb}
421 # strings. See bug #1063571.
422 with os_helper.change_cwd(cwd):
423 return os.getcwd()
424
425 # For use in the test_cwd* tests below.
426 def _split_python_path(self):
427 # Return normalized (python_dir, python_base).
428 python_path = os.path.realpath(sys.executable)
429 return os.path.split(python_path)
430
431 # For use in the test_cwd* tests below.
432 def _assert_cwd(self, expected_cwd, python_arg, **kwargs):
433 # Invoke Python via Popen, and assert that (1) the call succeeds,
434 # and that (2) the current working directory of the child process
435 # matches *expected_cwd*.
436 p = subprocess.Popen([python_arg, "-c",
437 "import os, sys; "
438 "buf = sys.stdout.buffer; "
439 "buf.write(os.getcwd().encode()); "
440 "buf.flush(); "
441 "sys.exit(47)"],
442 stdout=subprocess.PIPE,
443 **kwargs)
444 self.addCleanup(p.stdout.close)
445 p.wait()
446 self.assertEqual(47, p.returncode)
447 normcase = os.path.normcase
448 self.assertEqual(normcase(expected_cwd),
449 normcase(p.stdout.read().decode()))
450
451 def test_cwd(self):
452 # Check that cwd changes the cwd for the child process.
453 temp_dir = tempfile.gettempdir()
454 temp_dir = self._normalize_cwd(temp_dir)
455 self._assert_cwd(temp_dir, sys.executable, cwd=temp_dir)
456
457 def test_cwd_with_bytes(self):
458 temp_dir = tempfile.gettempdir()
459 temp_dir = self._normalize_cwd(temp_dir)
460 self._assert_cwd(temp_dir, sys.executable, cwd=os.fsencode(temp_dir))
461
462 def test_cwd_with_pathlike(self):
463 temp_dir = tempfile.gettempdir()
464 temp_dir = self._normalize_cwd(temp_dir)
465 self._assert_cwd(temp_dir, sys.executable, cwd=FakePath(temp_dir))
466
467 @unittest.skipIf(mswindows, "pending resolution of issue #15533")
468 def test_cwd_with_relative_arg(self):
469 # Check that Popen looks for args[0] relative to cwd if args[0]
470 # is relative.
471 python_dir, python_base = self._split_python_path()
472 rel_python = os.path.join(os.curdir, python_base)
473 with os_helper.temp_cwd() as wrong_dir:
474 # Before calling with the correct cwd, confirm that the call fails
475 # without cwd and with the wrong cwd.
476 self.assertRaises(FileNotFoundError, subprocess.Popen,
477 [rel_python])
478 self.assertRaises(FileNotFoundError, subprocess.Popen,
479 [rel_python], cwd=wrong_dir)
480 python_dir = self._normalize_cwd(python_dir)
481 self._assert_cwd(python_dir, rel_python, cwd=python_dir)
482
483 @unittest.skipIf(mswindows, "pending resolution of issue #15533")
484 def test_cwd_with_relative_executable(self):
485 # Check that Popen looks for executable relative to cwd if executable
486 # is relative (and that executable takes precedence over args[0]).
487 python_dir, python_base = self._split_python_path()
488 rel_python = os.path.join(os.curdir, python_base)
489 doesntexist = "somethingyoudonthave"
490 with os_helper.temp_cwd() as wrong_dir:
491 # Before calling with the correct cwd, confirm that the call fails
492 # without cwd and with the wrong cwd.
493 self.assertRaises(FileNotFoundError, subprocess.Popen,
494 [doesntexist], executable=rel_python)
495 self.assertRaises(FileNotFoundError, subprocess.Popen,
496 [doesntexist], executable=rel_python,
497 cwd=wrong_dir)
498 python_dir = self._normalize_cwd(python_dir)
499 self._assert_cwd(python_dir, doesntexist, executable=rel_python,
500 cwd=python_dir)
501
502 def test_cwd_with_absolute_arg(self):
503 # Check that Popen can find the executable when the cwd is wrong
504 # if args[0] is an absolute path.
505 python_dir, python_base = self._split_python_path()
506 abs_python = os.path.join(python_dir, python_base)
507 rel_python = os.path.join(os.curdir, python_base)
508 with os_helper.temp_dir() as wrong_dir:
509 # Before calling with an absolute path, confirm that using a
510 # relative path fails.
511 self.assertRaises(FileNotFoundError, subprocess.Popen,
512 [rel_python], cwd=wrong_dir)
513 wrong_dir = self._normalize_cwd(wrong_dir)
514 self._assert_cwd(wrong_dir, abs_python, cwd=wrong_dir)
515
516 @unittest.skipIf(sys.base_prefix != sys.prefix,
517 'Test is not venv-compatible')
518 def test_executable_with_cwd(self):
519 python_dir, python_base = self._split_python_path()
520 python_dir = self._normalize_cwd(python_dir)
521 self._assert_cwd(python_dir, "somethingyoudonthave",
522 executable=sys.executable, cwd=python_dir)
523
524 @unittest.skipIf(sys.base_prefix != sys.prefix,
525 'Test is not venv-compatible')
526 @unittest.skipIf(sysconfig.is_python_build(),
527 "need an installed Python. See #7774")
528 def test_executable_without_cwd(self):
529 # For a normal installation, it should work without 'cwd'
530 # argument. For test runs in the build directory, see #7774.
531 self._assert_cwd(os.getcwd(), "somethingyoudonthave",
532 executable=sys.executable)
533
534 def test_stdin_pipe(self):
535 # stdin redirection
536 p = subprocess.Popen([sys.executable, "-c",
537 'import sys; sys.exit(sys.stdin.read() == "pear")'],
538 stdin=subprocess.PIPE)
539 p.stdin.write(b"pear")
540 p.stdin.close()
541 p.wait()
542 self.assertEqual(p.returncode, 1)
543
544 def test_stdin_filedes(self):
545 # stdin is set to open file descriptor
546 tf = tempfile.TemporaryFile()
547 self.addCleanup(tf.close)
548 d = tf.fileno()
549 os.write(d, b"pear")
550 os.lseek(d, 0, 0)
551 p = subprocess.Popen([sys.executable, "-c",
552 'import sys; sys.exit(sys.stdin.read() == "pear")'],
553 stdin=d)
554 p.wait()
555 self.assertEqual(p.returncode, 1)
556
557 def test_stdin_fileobj(self):
558 # stdin is set to open file object
559 tf = tempfile.TemporaryFile()
560 self.addCleanup(tf.close)
561 tf.write(b"pear")
562 tf.seek(0)
563 p = subprocess.Popen([sys.executable, "-c",
564 'import sys; sys.exit(sys.stdin.read() == "pear")'],
565 stdin=tf)
566 p.wait()
567 self.assertEqual(p.returncode, 1)
568
569 def test_stdout_pipe(self):
570 # stdout redirection
571 p = subprocess.Popen([sys.executable, "-c",
572 'import sys; sys.stdout.write("orange")'],
573 stdout=subprocess.PIPE)
574 with p:
575 self.assertEqual(p.stdout.read(), b"orange")
576
577 def test_stdout_filedes(self):
578 # stdout is set to open file descriptor
579 tf = tempfile.TemporaryFile()
580 self.addCleanup(tf.close)
581 d = tf.fileno()
582 p = subprocess.Popen([sys.executable, "-c",
583 'import sys; sys.stdout.write("orange")'],
584 stdout=d)
585 p.wait()
586 os.lseek(d, 0, 0)
587 self.assertEqual(os.read(d, 1024), b"orange")
588
589 def test_stdout_fileobj(self):
590 # stdout is set to open file object
591 tf = tempfile.TemporaryFile()
592 self.addCleanup(tf.close)
593 p = subprocess.Popen([sys.executable, "-c",
594 'import sys; sys.stdout.write("orange")'],
595 stdout=tf)
596 p.wait()
597 tf.seek(0)
598 self.assertEqual(tf.read(), b"orange")
599
600 def test_stderr_pipe(self):
601 # stderr redirection
602 p = subprocess.Popen([sys.executable, "-c",
603 'import sys; sys.stderr.write("strawberry")'],
604 stderr=subprocess.PIPE)
605 with p:
606 self.assertEqual(p.stderr.read(), b"strawberry")
607
608 def test_stderr_filedes(self):
609 # stderr is set to open file descriptor
610 tf = tempfile.TemporaryFile()
611 self.addCleanup(tf.close)
612 d = tf.fileno()
613 p = subprocess.Popen([sys.executable, "-c",
614 'import sys; sys.stderr.write("strawberry")'],
615 stderr=d)
616 p.wait()
617 os.lseek(d, 0, 0)
618 self.assertEqual(os.read(d, 1024), b"strawberry")
619
620 def test_stderr_fileobj(self):
621 # stderr is set to open file object
622 tf = tempfile.TemporaryFile()
623 self.addCleanup(tf.close)
624 p = subprocess.Popen([sys.executable, "-c",
625 'import sys; sys.stderr.write("strawberry")'],
626 stderr=tf)
627 p.wait()
628 tf.seek(0)
629 self.assertEqual(tf.read(), b"strawberry")
630
631 def test_stderr_redirect_with_no_stdout_redirect(self):
632 # test stderr=STDOUT while stdout=None (not set)
633
634 # - grandchild prints to stderr
635 # - child redirects grandchild's stderr to its stdout
636 # - the parent should get grandchild's stderr in child's stdout
637 p = subprocess.Popen([sys.executable, "-c",
638 'import sys, subprocess;'
639 'rc = subprocess.call([sys.executable, "-c",'
640 ' "import sys;"'
641 ' "sys.stderr.write(\'42\')"],'
642 ' stderr=subprocess.STDOUT);'
643 'sys.exit(rc)'],
644 stdout=subprocess.PIPE,
645 stderr=subprocess.PIPE)
646 stdout, stderr = p.communicate()
647 #NOTE: stdout should get stderr from grandchild
648 self.assertEqual(stdout, b'42')
649 self.assertEqual(stderr, b'') # should be empty
650 self.assertEqual(p.returncode, 0)
651
652 def test_stdout_stderr_pipe(self):
653 # capture stdout and stderr to the same pipe
654 p = subprocess.Popen([sys.executable, "-c",
655 'import sys;'
656 'sys.stdout.write("apple");'
657 'sys.stdout.flush();'
658 'sys.stderr.write("orange")'],
659 stdout=subprocess.PIPE,
660 stderr=subprocess.STDOUT)
661 with p:
662 self.assertEqual(p.stdout.read(), b"appleorange")
663
664 def test_stdout_stderr_file(self):
665 # capture stdout and stderr to the same open file
666 tf = tempfile.TemporaryFile()
667 self.addCleanup(tf.close)
668 p = subprocess.Popen([sys.executable, "-c",
669 'import sys;'
670 'sys.stdout.write("apple");'
671 'sys.stdout.flush();'
672 'sys.stderr.write("orange")'],
673 stdout=tf,
674 stderr=tf)
675 p.wait()
676 tf.seek(0)
677 self.assertEqual(tf.read(), b"appleorange")
678
679 def test_stdout_filedes_of_stdout(self):
680 # stdout is set to 1 (#1531862).
681 # To avoid printing the text on stdout, we do something similar to
682 # test_stdout_none (see above). The parent subprocess calls the child
683 # subprocess passing stdout=1, and this test uses stdout=PIPE in
684 # order to capture and check the output of the parent. See #11963.
685 code = ('import sys, subprocess; '
686 'rc = subprocess.call([sys.executable, "-c", '
687 ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), '
688 'b\'test with stdout=1\'))"], stdout=1); '
689 'assert rc == 18')
690 p = subprocess.Popen([sys.executable, "-c", code],
691 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
692 self.addCleanup(p.stdout.close)
693 self.addCleanup(p.stderr.close)
694 out, err = p.communicate()
695 self.assertEqual(p.returncode, 0, err)
696 self.assertEqual(out.rstrip(), b'test with stdout=1')
697
698 def test_stdout_devnull(self):
699 p = subprocess.Popen([sys.executable, "-c",
700 'for i in range(10240):'
701 'print("x" * 1024)'],
702 stdout=subprocess.DEVNULL)
703 p.wait()
704 self.assertEqual(p.stdout, None)
705
706 def test_stderr_devnull(self):
707 p = subprocess.Popen([sys.executable, "-c",
708 'import sys\n'
709 'for i in range(10240):'
710 'sys.stderr.write("x" * 1024)'],
711 stderr=subprocess.DEVNULL)
712 p.wait()
713 self.assertEqual(p.stderr, None)
714
715 def test_stdin_devnull(self):
716 p = subprocess.Popen([sys.executable, "-c",
717 'import sys;'
718 'sys.stdin.read(1)'],
719 stdin=subprocess.DEVNULL)
720 p.wait()
721 self.assertEqual(p.stdin, None)
722
723 @unittest.skipUnless(fcntl and hasattr(fcntl, 'F_GETPIPE_SZ'),
724 'fcntl.F_GETPIPE_SZ required for test.')
725 def test_pipesizes(self):
726 test_pipe_r, test_pipe_w = os.pipe()
727 try:
728 # Get the default pipesize with F_GETPIPE_SZ
729 pipesize_default = fcntl.fcntl(test_pipe_w, fcntl.F_GETPIPE_SZ)
730 finally:
731 os.close(test_pipe_r)
732 os.close(test_pipe_w)
733 pipesize = pipesize_default // 2
734 pagesize_default = support.get_pagesize()
735 if pipesize < pagesize_default: # the POSIX minimum
736 raise unittest.SkipTest(
737 'default pipesize too small to perform test.')
738 p = subprocess.Popen(
739 [sys.executable, "-c",
740 'import sys; sys.stdin.read(); sys.stdout.write("out"); '
741 'sys.stderr.write("error!")'],
742 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
743 stderr=subprocess.PIPE, pipesize=pipesize)
744 try:
745 for fifo in [p.stdin, p.stdout, p.stderr]:
746 self.assertEqual(
747 fcntl.fcntl(fifo.fileno(), fcntl.F_GETPIPE_SZ),
748 pipesize)
749 # Windows pipe size can be acquired via GetNamedPipeInfoFunction
750 # https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-getnamedpipeinfo
751 # However, this function is not yet in _winapi.
752 p.stdin.write(b"pear")
753 p.stdin.close()
754 p.stdout.close()
755 p.stderr.close()
756 finally:
757 p.kill()
758 p.wait()
759
760 @unittest.skipUnless(fcntl and hasattr(fcntl, 'F_GETPIPE_SZ'),
761 'fcntl.F_GETPIPE_SZ required for test.')
762 def test_pipesize_default(self):
763 proc = subprocess.Popen(
764 [sys.executable, "-c",
765 'import sys; sys.stdin.read(); sys.stdout.write("out"); '
766 'sys.stderr.write("error!")'],
767 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
768 stderr=subprocess.PIPE, pipesize=-1)
769
770 with proc:
771 try:
772 fp_r, fp_w = os.pipe()
773 try:
774 default_read_pipesize = fcntl.fcntl(fp_r, fcntl.F_GETPIPE_SZ)
775 default_write_pipesize = fcntl.fcntl(fp_w, fcntl.F_GETPIPE_SZ)
776 finally:
777 os.close(fp_r)
778 os.close(fp_w)
779
780 self.assertEqual(
781 fcntl.fcntl(proc.stdin.fileno(), fcntl.F_GETPIPE_SZ),
782 default_read_pipesize)
783 self.assertEqual(
784 fcntl.fcntl(proc.stdout.fileno(), fcntl.F_GETPIPE_SZ),
785 default_write_pipesize)
786 self.assertEqual(
787 fcntl.fcntl(proc.stderr.fileno(), fcntl.F_GETPIPE_SZ),
788 default_write_pipesize)
789 # On other platforms we cannot test the pipe size (yet). But above
790 # code using pipesize=-1 should not crash.
791 finally:
792 proc.kill()
793
794 def test_env(self):
795 newenv = os.environ.copy()
796 newenv["FRUIT"] = "orange"
797 with subprocess.Popen([sys.executable, "-c",
798 'import sys,os;'
799 'sys.stdout.write(os.getenv("FRUIT"))'],
800 stdout=subprocess.PIPE,
801 env=newenv) as p:
802 stdout, stderr = p.communicate()
803 self.assertEqual(stdout, b"orange")
804
805 @unittest.skipUnless(sys.platform == "win32", "Windows only issue")
806 def test_win32_duplicate_envs(self):
807 newenv = os.environ.copy()
808 newenv["fRUit"] = "cherry"
809 newenv["fruit"] = "lemon"
810 newenv["FRUIT"] = "orange"
811 newenv["frUit"] = "banana"
812 with subprocess.Popen(["CMD", "/c", "SET", "fruit"],
813 stdout=subprocess.PIPE,
814 env=newenv) as p:
815 stdout, _ = p.communicate()
816 self.assertEqual(stdout.strip(), b"frUit=banana")
817
818 # Windows requires at least the SYSTEMROOT environment variable to start
819 # Python
820 @unittest.skipIf(sys.platform == 'win32',
821 'cannot test an empty env on Windows')
822 @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') == 1,
823 'The Python shared library cannot be loaded '
824 'with an empty environment.')
825 @unittest.skipIf(check_sanitizer(address=True),
826 'AddressSanitizer adds to the environment.')
827 def test_empty_env(self):
828 """Verify that env={} is as empty as possible."""
829
830 def is_env_var_to_ignore(n):
831 """Determine if an environment variable is under our control."""
832 # This excludes some __CF_* and VERSIONER_* keys MacOS insists
833 # on adding even when the environment in exec is empty.
834 # Gentoo sandboxes also force LD_PRELOAD and SANDBOX_* to exist.
835 return ('VERSIONER' in n or '__CF' in n or # MacOS
836 n == 'LD_PRELOAD' or n.startswith('SANDBOX') or # Gentoo
837 n == 'LC_CTYPE') # Locale coercion triggered
838
839 with subprocess.Popen([sys.executable, "-c",
840 'import os; print(list(os.environ.keys()))'],
841 stdout=subprocess.PIPE, env={}) as p:
842 stdout, stderr = p.communicate()
843 child_env_names = eval(stdout.strip())
844 self.assertIsInstance(child_env_names, list)
845 child_env_names = [k for k in child_env_names
846 if not is_env_var_to_ignore(k)]
847 self.assertEqual(child_env_names, [])
848
849 @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') == 1,
850 'The Python shared library cannot be loaded '
851 'without some system environments.')
852 @unittest.skipIf(check_sanitizer(address=True),
853 'AddressSanitizer adds to the environment.')
854 def test_one_environment_variable(self):
855 newenv = {'fruit': 'orange'}
856 cmd = [sys.executable, '-c',
857 'import sys,os;'
858 'sys.stdout.write("fruit="+os.getenv("fruit"))']
859 if sys.platform == "win32":
860 cmd = ["CMD", "/c", "SET", "fruit"]
861 with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=newenv) as p:
862 stdout, stderr = p.communicate()
863 if p.returncode and support.verbose:
864 print("STDOUT:", stdout.decode("ascii", "replace"))
865 print("STDERR:", stderr.decode("ascii", "replace"))
866 self.assertEqual(p.returncode, 0)
867 self.assertEqual(stdout.strip(), b"fruit=orange")
868
869 def test_invalid_cmd(self):
870 # null character in the command name
871 cmd = sys.executable + '\0'
872 with self.assertRaises(ValueError):
873 subprocess.Popen([cmd, "-c", "pass"])
874
875 # null character in the command argument
876 with self.assertRaises(ValueError):
877 subprocess.Popen([sys.executable, "-c", "pass#\0"])
878
879 def test_invalid_env(self):
880 # null character in the environment variable name
881 newenv = os.environ.copy()
882 newenv["FRUIT\0VEGETABLE"] = "cabbage"
883 with self.assertRaises(ValueError):
884 subprocess.Popen(ZERO_RETURN_CMD, env=newenv)
885
886 # null character in the environment variable value
887 newenv = os.environ.copy()
888 newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
889 with self.assertRaises(ValueError):
890 subprocess.Popen(ZERO_RETURN_CMD, env=newenv)
891
892 # equal character in the environment variable name
893 newenv = os.environ.copy()
894 newenv["FRUIT=ORANGE"] = "lemon"
895 with self.assertRaises(ValueError):
896 subprocess.Popen(ZERO_RETURN_CMD, env=newenv)
897
898 # equal character in the environment variable value
899 newenv = os.environ.copy()
900 newenv["FRUIT"] = "orange=lemon"
901 with subprocess.Popen([sys.executable, "-c",
902 'import sys, os;'
903 'sys.stdout.write(os.getenv("FRUIT"))'],
904 stdout=subprocess.PIPE,
905 env=newenv) as p:
906 stdout, stderr = p.communicate()
907 self.assertEqual(stdout, b"orange=lemon")
908
909 @unittest.skipUnless(sys.platform == "win32", "Windows only issue")
910 def test_win32_invalid_env(self):
911 # '=' in the environment variable name
912 newenv = os.environ.copy()
913 newenv["FRUIT=VEGETABLE"] = "cabbage"
914 with self.assertRaises(ValueError):
915 subprocess.Popen(ZERO_RETURN_CMD, env=newenv)
916
917 newenv = os.environ.copy()
918 newenv["==FRUIT"] = "cabbage"
919 with self.assertRaises(ValueError):
920 subprocess.Popen(ZERO_RETURN_CMD, env=newenv)
921
922 def test_communicate_stdin(self):
923 p = subprocess.Popen([sys.executable, "-c",
924 'import sys;'
925 'sys.exit(sys.stdin.read() == "pear")'],
926 stdin=subprocess.PIPE)
927 p.communicate(b"pear")
928 self.assertEqual(p.returncode, 1)
929
930 def test_communicate_stdout(self):
931 p = subprocess.Popen([sys.executable, "-c",
932 'import sys; sys.stdout.write("pineapple")'],
933 stdout=subprocess.PIPE)
934 (stdout, stderr) = p.communicate()
935 self.assertEqual(stdout, b"pineapple")
936 self.assertEqual(stderr, None)
937
938 def test_communicate_stderr(self):
939 p = subprocess.Popen([sys.executable, "-c",
940 'import sys; sys.stderr.write("pineapple")'],
941 stderr=subprocess.PIPE)
942 (stdout, stderr) = p.communicate()
943 self.assertEqual(stdout, None)
944 self.assertEqual(stderr, b"pineapple")
945
946 def test_communicate(self):
947 p = subprocess.Popen([sys.executable, "-c",
948 'import sys,os;'
949 'sys.stderr.write("pineapple");'
950 'sys.stdout.write(sys.stdin.read())'],
951 stdin=subprocess.PIPE,
952 stdout=subprocess.PIPE,
953 stderr=subprocess.PIPE)
954 self.addCleanup(p.stdout.close)
955 self.addCleanup(p.stderr.close)
956 self.addCleanup(p.stdin.close)
957 (stdout, stderr) = p.communicate(b"banana")
958 self.assertEqual(stdout, b"banana")
959 self.assertEqual(stderr, b"pineapple")
960
961 def test_communicate_memoryview_input(self):
962 # Test memoryview input with byte elements
963 test_data = b"Hello, memoryview!"
964 mv = memoryview(test_data)
965 p = subprocess.Popen([sys.executable, "-c",
966 'import sys; sys.stdout.write(sys.stdin.read())'],
967 stdin=subprocess.PIPE,
968 stdout=subprocess.PIPE)
969 self.addCleanup(p.stdout.close)
970 self.addCleanup(p.stdin.close)
971 (stdout, stderr) = p.communicate(mv)
972 self.assertEqual(stdout, test_data)
973 self.assertIsNone(stderr)
974
975 def test_communicate_memoryview_input_nonbyte(self):
976 # Test memoryview input with non-byte elements (e.g., int32)
977 # This tests the fix for gh-134453 where non-byte memoryviews
978 # had incorrect length tracking on POSIX
979 import array
980 # Create an array of 32-bit integers that's large enough to trigger
981 # the chunked writing behavior (> PIPE_BUF)
982 pipe_buf = getattr(select, 'PIPE_BUF', 512)
983 # Each 'i' element is 4 bytes, so we need more than pipe_buf/4 elements
984 # Add some extra to ensure we exceed the buffer size
985 num_elements = pipe_buf + 1
986 test_array = array.array('i', [0x64306f66 for _ in range(num_elements)])
987 expected_bytes = test_array.tobytes()
988 mv = memoryview(test_array)
989
990 p = subprocess.Popen([sys.executable, "-c",
991 'import sys; '
992 'data = sys.stdin.buffer.read(); '
993 'sys.stdout.buffer.write(data)'],
994 stdin=subprocess.PIPE,
995 stdout=subprocess.PIPE)
996 self.addCleanup(p.stdout.close)
997 self.addCleanup(p.stdin.close)
998 (stdout, stderr) = p.communicate(mv)
999 self.assertEqual(stdout, expected_bytes,
1000 msg=f"{len(stdout)=} =? {len(expected_bytes)=}")
1001 self.assertIsNone(stderr)
1002
1003 def test_communicate_timeout(self):
1004 p = subprocess.Popen([sys.executable, "-c",
1005 'import sys,os,time;'
1006 'sys.stderr.write("pineapple\\n");'
1007 'time.sleep(1);'
1008 'sys.stderr.write("pear\\n");'
1009 'sys.stdout.write(sys.stdin.read())'],
1010 universal_newlines=True,
1011 stdin=subprocess.PIPE,
1012 stdout=subprocess.PIPE,
1013 stderr=subprocess.PIPE)
1014 self.assertRaises(subprocess.TimeoutExpired, p.communicate, "banana",
1015 timeout=0.3)
1016 # Make sure we can keep waiting for it, and that we get the whole output
1017 # after it completes.
1018 (stdout, stderr) = p.communicate()
1019 self.assertEqual(stdout, "banana")
1020 self.assertEqual(stderr.encode(), b"pineapple\npear\n")
1021
1022 def test_communicate_timeout_large_output(self):
1023 # Test an expiring timeout while the child is outputting lots of data.
1024 p = subprocess.Popen([sys.executable, "-c",
1025 'import sys,os,time;'
1026 'sys.stdout.write("a" * (64 * 1024));'
1027 'time.sleep(0.2);'
1028 'sys.stdout.write("a" * (64 * 1024));'
1029 'time.sleep(0.2);'
1030 'sys.stdout.write("a" * (64 * 1024));'
1031 'time.sleep(0.2);'
1032 'sys.stdout.write("a" * (64 * 1024));'],
1033 stdout=subprocess.PIPE)
1034 self.assertRaises(subprocess.TimeoutExpired, p.communicate, timeout=0.4)
1035 (stdout, _) = p.communicate()
1036 self.assertEqual(len(stdout), 4 * 64 * 1024)
1037
1038 def test_communicate_timeout_large_input(self):
1039 # Test that timeout is enforced when writing large input to a
1040 # slow-to-read subprocess, and that partial input is preserved
1041 # for continuation after timeout (gh-141473).
1042 #
1043 # This is a regression test for Windows matching POSIX behavior.
1044 # On POSIX, select() is used to multiplex I/O with timeout checking.
1045 # On Windows, stdin writing must also honor the timeout rather than
1046 # blocking indefinitely when the pipe buffer fills.
1047
1048 input_data = b"x" * (128 * 1024) # > typical pipe buffer
1049
1050 # Cross-platform wake mechanism: the slow reader connects to a
1051 # loopback TCP socket and blocks in select() on it (capped at 9s
1052 # as a safety net we don't expect to hit). After phase 1 raises
1053 # TimeoutExpired, the parent sends a byte to release the child so
1054 # it drains stdin. A socket (rather than a raw pipe) is required
1055 # because Windows select() only supports sockets, not arbitrary
1056 # file descriptors.
1057 server = socket.create_server(('127.0.0.1', 0), backlog=1)
1058 server.settimeout(10) # bound the accept() if the child fails to start
1059 port = server.getsockname()[1]
1060 # The child sends one byte (low byte of its PID) first so the parent
1061 # can detect the rare case of an unrelated process on the same host
1062 # connecting to our ephemeral port before our child does. A single
1063 # byte gives 1/256 collision odds, which is plenty for flake-prevention.
1064 slow_reader = (
1065 "import os, socket, sys, select; "
1066 f"s = socket.create_connection(('127.0.0.1', {port}), timeout=9); "
1067 "s.sendall(bytes([os.getpid() & 0xff])); "
1068 "select.select([s], [], [], 9); "
1069 "sys.stdout.buffer.write(sys.stdin.buffer.read())"
1070 )
1071
1072 p = subprocess.Popen(
1073 [sys.executable, "-c", slow_reader],
1074 stdin=subprocess.PIPE,
1075 stdout=subprocess.PIPE,
1076 stderr=subprocess.PIPE)
1077
1078 conn = None
1079 try:
1080 conn, _ = server.accept()
1081 server.close()
1082 server = None
1083
1084 conn.settimeout(5)
1085 peer_byte = conn.recv(1)
1086 conn.settimeout(None)
1087 self.assertEqual(peer_byte, bytes([p.pid & 0xff]),
1088 f"loopback handshake byte {peer_byte!r} != "
1089 f"low byte of child PID {p.pid} ({p.pid & 0xff:#x})")
1090
1091 timeout = 0.2
1092 start = time.monotonic()
1093 try:
1094 p.communicate(input_data, timeout=timeout)
1095 # If we get here without TimeoutExpired, the timeout was ignored
1096 elapsed = time.monotonic() - start
1097 self.fail(
1098 f"TimeoutExpired not raised. communicate() completed in "
1099 f"{elapsed:.2f}s, but slow reader stalls for up to 9s. "
1100 "Stdin writing blocked without enforcing timeout.")
1101 except subprocess.TimeoutExpired:
1102 elapsed = time.monotonic() - start
1103
1104 # Timeout should occur close to the specified timeout value,
1105 # not after waiting for the subprocess to finish sleeping.
1106 # Allow generous margin for slow CI, but must be well under
1107 # the slow-reader's stall cap.
1108 self.assertLess(elapsed, 5.0,
1109 f"TimeoutExpired raised after {elapsed:.2f}s; expected ~{timeout}s. "
1110 "Stdin writing blocked without checking timeout.")
1111
1112 # Release the slow reader so it stops blocking and drains stdin.
1113 conn.sendall(b'go')
1114 conn.close()
1115 conn = None
1116
1117 # After timeout, continue communication. The remaining input
1118 # should be sent and we should receive all data back.
1119 stdout, stderr = p.communicate()
1120
1121 # Verify all input was eventually received by the subprocess
1122 self.assertEqual(len(stdout), len(input_data),
1123 f"Expected {len(input_data)} bytes output but got {len(stdout)}")
1124 self.assertEqual(stdout, input_data)
1125 finally:
1126 if conn is not None:
1127 conn.close()
1128 if server is not None:
1129 server.close()
1130 p.kill()
1131 p.wait()
1132
1133 # Test for the fd leak reported in http://bugs.python.org/issue2791.
1134 def test_communicate_pipe_fd_leak(self):
1135 for stdin_pipe in (False, True):
1136 for stdout_pipe in (False, True):
1137 for stderr_pipe in (False, True):
1138 options = {}
1139 if stdin_pipe:
1140 options['stdin'] = subprocess.PIPE
1141 if stdout_pipe:
1142 options['stdout'] = subprocess.PIPE
1143 if stderr_pipe:
1144 options['stderr'] = subprocess.PIPE
1145 if not options:
1146 continue
1147 p = subprocess.Popen(ZERO_RETURN_CMD, **options)
1148 p.communicate()
1149 if p.stdin is not None:
1150 self.assertTrue(p.stdin.closed)
1151 if p.stdout is not None:
1152 self.assertTrue(p.stdout.closed)
1153 if p.stderr is not None:
1154 self.assertTrue(p.stderr.closed)
1155
1156 def test_communicate_returns(self):
1157 # communicate() should return None if no redirection is active
1158 p = subprocess.Popen([sys.executable, "-c",
1159 "import sys; sys.exit(47)"])
1160 (stdout, stderr) = p.communicate()
1161 self.assertEqual(stdout, None)
1162 self.assertEqual(stderr, None)
1163
1164 def test_communicate_pipe_buf(self):
1165 # communicate() with writes larger than pipe_buf
1166 # This test will probably deadlock rather than fail, if
1167 # communicate() does not work properly.
1168 x, y = os.pipe()
1169 os.close(x)
1170 os.close(y)
1171 p = subprocess.Popen([sys.executable, "-c",
1172 'import sys,os;'
1173 'sys.stdout.write(sys.stdin.read(47));'
1174 'sys.stderr.write("x" * %d);'
1175 'sys.stdout.write(sys.stdin.read())' %
1176 support.PIPE_MAX_SIZE],
1177 stdin=subprocess.PIPE,
1178 stdout=subprocess.PIPE,
1179 stderr=subprocess.PIPE)
1180 self.addCleanup(p.stdout.close)
1181 self.addCleanup(p.stderr.close)
1182 self.addCleanup(p.stdin.close)
1183 string_to_write = b"a" * support.PIPE_MAX_SIZE
1184 (stdout, stderr) = p.communicate(string_to_write)
1185 self.assertEqual(stdout, string_to_write)
1186
1187 def test_writes_before_communicate(self):
1188 # stdin.write before communicate()
1189 p = subprocess.Popen([sys.executable, "-c",
1190 'import sys,os;'
1191 'sys.stdout.write(sys.stdin.read())'],
1192 stdin=subprocess.PIPE,
1193 stdout=subprocess.PIPE,
1194 stderr=subprocess.PIPE)
1195 self.addCleanup(p.stdout.close)
1196 self.addCleanup(p.stderr.close)
1197 self.addCleanup(p.stdin.close)
1198 p.stdin.write(b"banana")
1199 (stdout, stderr) = p.communicate(b"split")
1200 self.assertEqual(stdout, b"bananasplit")
1201 self.assertEqual(stderr, b"")
1202
1203 def test_communicate_stdin_closed_before_call(self):
1204 # gh-70560, gh-74389: stdin.close() before communicate()
1205 # should not raise ValueError from stdin.flush()
1206 with subprocess.Popen([sys.executable, "-c",
1207 'import sys; sys.exit(0)'],
1208 stdin=subprocess.PIPE,
1209 stdout=subprocess.PIPE,
1210 stderr=subprocess.PIPE) as p:
1211 p.stdin.close() # Close stdin before communicate
1212 # This should not raise ValueError
1213 (stdout, stderr) = p.communicate()
1214 self.assertEqual(p.returncode, 0)
1215
1216 def test_universal_newlines_and_text(self):
1217 args = [
1218 sys.executable, "-c",
1219 'import sys,os;' + SETBINARY +
1220 'buf = sys.stdout.buffer;'
1221 'buf.write(sys.stdin.readline().encode());'
1222 'buf.flush();'
1223 'buf.write(b"line2\\n");'
1224 'buf.flush();'
1225 'buf.write(sys.stdin.read().encode());'
1226 'buf.flush();'
1227 'buf.write(b"line4\\n");'
1228 'buf.flush();'
1229 'buf.write(b"line5\\r\\n");'
1230 'buf.flush();'
1231 'buf.write(b"line6\\r");'
1232 'buf.flush();'
1233 'buf.write(b"\\nline7");'
1234 'buf.flush();'
1235 'buf.write(b"\\nline8");']
1236
1237 for extra_kwarg in ('universal_newlines', 'text'):
1238 p = subprocess.Popen(args, **{'stdin': subprocess.PIPE,
1239 'stdout': subprocess.PIPE,
1240 extra_kwarg: True})
1241 with p:
1242 p.stdin.write("line1\n")
1243 p.stdin.flush()
1244 self.assertEqual(p.stdout.readline(), "line1\n")
1245 p.stdin.write("line3\n")
1246 p.stdin.close()
1247 self.addCleanup(p.stdout.close)
1248 self.assertEqual(p.stdout.readline(),
1249 "line2\n")
1250 self.assertEqual(p.stdout.read(6),
1251 "line3\n")
1252 self.assertEqual(p.stdout.read(),
1253 "line4\nline5\nline6\nline7\nline8")
1254
1255 def test_universal_newlines_communicate(self):
1256 # universal newlines through communicate()
1257 p = subprocess.Popen([sys.executable, "-c",
1258 'import sys,os;' + SETBINARY +
1259 'buf = sys.stdout.buffer;'
1260 'buf.write(b"line2\\n");'
1261 'buf.flush();'
1262 'buf.write(b"line4\\n");'
1263 'buf.flush();'
1264 'buf.write(b"line5\\r\\n");'
1265 'buf.flush();'
1266 'buf.write(b"line6\\r");'
1267 'buf.flush();'
1268 'buf.write(b"\\nline7");'
1269 'buf.flush();'
1270 'buf.write(b"\\nline8");'],
1271 stderr=subprocess.PIPE,
1272 stdout=subprocess.PIPE,
1273 universal_newlines=1)
1274 self.addCleanup(p.stdout.close)
1275 self.addCleanup(p.stderr.close)
1276 (stdout, stderr) = p.communicate()
1277 self.assertEqual(stdout,
1278 "line2\nline4\nline5\nline6\nline7\nline8")
1279
1280 def test_universal_newlines_communicate_stdin(self):
1281 # universal newlines through communicate(), with only stdin
1282 p = subprocess.Popen([sys.executable, "-c",
1283 'import sys,os;' + SETBINARY + textwrap.dedent('''
1284 s = sys.stdin.readline()
1285 assert s == "line1\\n", repr(s)
1286 s = sys.stdin.read()
1287 assert s == "line3\\n", repr(s)
1288 ''')],
1289 stdin=subprocess.PIPE,
1290 universal_newlines=1)
1291 (stdout, stderr) = p.communicate("line1\nline3\n")
1292 self.assertEqual(p.returncode, 0)
1293
1294 def test_universal_newlines_communicate_input_none(self):
1295 # Test communicate(input=None) with universal newlines.
1296 #
1297 # We set stdout to PIPE because, as of this writing, a different
1298 # code path is tested when the number of pipes is zero or one.
1299 p = subprocess.Popen(ZERO_RETURN_CMD,
1300 stdin=subprocess.PIPE,
1301 stdout=subprocess.PIPE,
1302 universal_newlines=True)
1303 p.communicate()
1304 self.assertEqual(p.returncode, 0)
1305
1306 def test_universal_newlines_communicate_stdin_stdout_stderr(self):
1307 # universal newlines through communicate(), with stdin, stdout, stderr
1308 p = subprocess.Popen([sys.executable, "-c",
1309 'import sys,os;' + SETBINARY + textwrap.dedent('''
1310 s = sys.stdin.buffer.readline()
1311 sys.stdout.buffer.write(s)
1312 sys.stdout.buffer.write(b"line2\\r")
1313 sys.stderr.buffer.write(b"eline2\\n")
1314 s = sys.stdin.buffer.read()
1315 sys.stdout.buffer.write(s)
1316 sys.stdout.buffer.write(b"line4\\n")
1317 sys.stdout.buffer.write(b"line5\\r\\n")
1318 sys.stderr.buffer.write(b"eline6\\r")
1319 sys.stderr.buffer.write(b"eline7\\r\\nz")
1320 ''')],
1321 stdin=subprocess.PIPE,
1322 stderr=subprocess.PIPE,
1323 stdout=subprocess.PIPE,
1324 universal_newlines=True)
1325 self.addCleanup(p.stdout.close)
1326 self.addCleanup(p.stderr.close)
1327 (stdout, stderr) = p.communicate("line1\nline3\n")
1328 self.assertEqual(p.returncode, 0)
1329 self.assertEqual("line1\nline2\nline3\nline4\nline5\n", stdout)
1330 # Python debug build push something like "[42442 refs]\n"
1331 # to stderr at exit of subprocess.
1332 self.assertStartsWith(stderr, "eline2\neline6\neline7\n")
1333
1334 def test_universal_newlines_communicate_encodings(self):
1335 # Check that universal newlines mode works for various encodings,
1336 # in particular for encodings in the UTF-16 and UTF-32 families.
1337 # See issue #15595.
1338 #
1339 # UTF-16 and UTF-32-BE are sufficient to check both with BOM and
1340 # without, and UTF-16 and UTF-32.
1341 for encoding in ['utf-16', 'utf-32-be']:
1342 code = ("import sys; "
1343 r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" %
1344 encoding)
1345 args = [sys.executable, '-c', code]
1346 # We set stdin to be non-None because, as of this writing,
1347 # a different code path is used when the number of pipes is
1348 # zero or one.
1349 popen = subprocess.Popen(args,
1350 stdin=subprocess.PIPE,
1351 stdout=subprocess.PIPE,
1352 encoding=encoding)
1353 stdout, stderr = popen.communicate(input='')
1354 self.assertEqual(stdout, '1\n2\n3\n4')
1355
1356 def test_communicate_errors(self):
1357 for errors, expected in [
1358 ('ignore', ''),
1359 ('replace', '\ufffd\ufffd'),
1360 ('surrogateescape', '\udc80\udc80'),
1361 ('backslashreplace', '\\x80\\x80'),
1362 ]:
1363 code = ("import sys; "
1364 r"sys.stdout.buffer.write(b'[\x80\x80]')")
1365 args = [sys.executable, '-c', code]
1366 # We set stdin to be non-None because, as of this writing,
1367 # a different code path is used when the number of pipes is
1368 # zero or one.
1369 popen = subprocess.Popen(args,
1370 stdin=subprocess.PIPE,
1371 stdout=subprocess.PIPE,
1372 encoding='utf-8',
1373 errors=errors)
1374 stdout, stderr = popen.communicate(input='')
1375 self.assertEqual(stdout, '[{}]'.format(expected))
1376
1377 def test_no_leaking(self):
1378 # Make sure we leak no resources
1379 if not mswindows:
1380 max_handles = 1026 # too much for most UNIX systems
1381 else:
1382 max_handles = 2050 # too much for (at least some) Windows setups
1383 if resource:
1384 # And if it is not too much, try to make it too much.
1385 try:
1386 soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
1387 if soft > 1024:
1388 resource.setrlimit(resource.RLIMIT_NOFILE, (1024, hard))
1389 self.addCleanup(resource.setrlimit, resource.RLIMIT_NOFILE,
1390 (soft, hard))
1391 except (OSError, ValueError):
1392 pass
1393 handles = []
1394 tmpdir = tempfile.mkdtemp()
1395 try:
1396 for i in range(max_handles):
1397 try:
1398 tmpfile = os.path.join(tmpdir, os_helper.TESTFN)
1399 handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT))
1400 except OSError as e:
1401 if e.errno != errno.EMFILE:
1402 raise
1403 break
1404 else:
1405 self.skipTest("failed to reach the file descriptor limit "
1406 "(tried %d)" % max_handles)
1407 # Close a couple of them (should be enough for a subprocess).
1408 # Close lower file descriptors, so select() will work.
1409 handles.reverse()
1410 for i in range(10):
1411 os.close(handles.pop())
1412 # Loop creating some subprocesses. If one of them leaks some fds,
1413 # the next loop iteration will fail by reaching the max fd limit.
1414 for i in range(15):
1415 p = subprocess.Popen([sys.executable, "-c",
1416 "import sys;"
1417 "sys.stdout.write(sys.stdin.read())"],
1418 stdin=subprocess.PIPE,
1419 stdout=subprocess.PIPE,
1420 stderr=subprocess.PIPE)
1421 data = p.communicate(b"lime")[0]
1422 self.assertEqual(data, b"lime")
1423 finally:
1424 for h in handles:
1425 os.close(h)
1426 shutil.rmtree(tmpdir)
1427
1428 def test_list2cmdline(self):
1429 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
1430 '"a b c" d e')
1431 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
1432 'ab\\"c \\ d')
1433 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
1434 'ab\\"c " \\\\" d')
1435 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
1436 'a\\\\\\b "de fg" h')
1437 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
1438 'a\\\\\\"b c d')
1439 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
1440 '"a\\\\b c" d e')
1441 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
1442 '"a\\\\b\\ c" d e')
1443 self.assertEqual(subprocess.list2cmdline(['ab', '']),
1444 'ab ""')
1445
1446 def test_poll(self):
1447 p = subprocess.Popen([sys.executable, "-c",
1448 "import os; os.read(0, 1)"],
1449 stdin=subprocess.PIPE)
1450 self.addCleanup(p.stdin.close)
1451 self.assertIsNone(p.poll())
1452 os.write(p.stdin.fileno(), b'A')
1453 p.wait()
1454 # Subsequent invocations should just return the returncode
1455 self.assertEqual(p.poll(), 0)
1456
1457 def test_wait(self):
1458 p = subprocess.Popen(ZERO_RETURN_CMD)
1459 self.assertEqual(p.wait(), 0)
1460 # Subsequent invocations should just return the returncode
1461 self.assertEqual(p.wait(), 0)
1462
1463 def test_wait_timeout(self):
1464 p = subprocess.Popen([sys.executable,
1465 "-c", "import time; time.sleep(0.3)"])
1466 with self.assertRaises(subprocess.TimeoutExpired) as c:
1467 p.wait(timeout=0.0001)
1468 self.assertIn("0.0001", str(c.exception)) # For coverage of __str__.
1469 self.assertEqual(p.wait(timeout=support.SHORT_TIMEOUT), 0)
1470
1471 def test_invalid_bufsize(self):
1472 # an invalid type of the bufsize argument should raise
1473 # TypeError.
1474 with self.assertRaises(TypeError):
1475 subprocess.Popen(ZERO_RETURN_CMD, "orange")
1476
1477 def test_bufsize_is_none(self):
1478 # bufsize=None should be the same as bufsize=0.
1479 p = subprocess.Popen(ZERO_RETURN_CMD, None)
1480 self.assertEqual(p.wait(), 0)
1481 # Again with keyword arg
1482 p = subprocess.Popen(ZERO_RETURN_CMD, bufsize=None)
1483 self.assertEqual(p.wait(), 0)
1484
1485 def _test_bufsize_equal_one(self, line, expected, universal_newlines):
1486 # subprocess may deadlock with bufsize=1, see issue #21332
1487 with subprocess.Popen([sys.executable, "-c", "import sys;"
1488 "sys.stdout.write(sys.stdin.readline());"
1489 "sys.stdout.flush()"],
1490 stdin=subprocess.PIPE,
1491 stdout=subprocess.PIPE,
1492 stderr=subprocess.DEVNULL,
1493 bufsize=1,
1494 universal_newlines=universal_newlines) as p:
1495 p.stdin.write(line) # expect that it flushes the line in text mode
1496 os.close(p.stdin.fileno()) # close it without flushing the buffer
1497 read_line = p.stdout.readline()
1498 with support.SuppressCrashReport():
1499 try:
1500 p.stdin.close()
1501 except OSError:
1502 pass
1503 p.stdin = None
1504 self.assertEqual(p.returncode, 0)
1505 self.assertEqual(read_line, expected)
1506
1507 def test_bufsize_equal_one_text_mode(self):
1508 # line is flushed in text mode with bufsize=1.
1509 # we should get the full line in return
1510 line = "line\n"
1511 self._test_bufsize_equal_one(line, line, universal_newlines=True)
1512
1513 def test_bufsize_equal_one_binary_mode(self):
1514 # line is not flushed in binary mode with bufsize=1.
1515 # we should get empty response
1516 line = b'line' + os.linesep.encode() # assume ascii-based locale
1517 with self.assertWarnsRegex(RuntimeWarning, 'line buffering'):
1518 self._test_bufsize_equal_one(line, b'', universal_newlines=False)
1519
1520 @support.requires_resource('cpu')
1521 def test_leaking_fds_on_error(self):
1522 # see bug #5179: Popen leaks file descriptors to PIPEs if
1523 # the child fails to execute; this will eventually exhaust
1524 # the maximum number of open fds. 1024 seems a very common
1525 # value for that limit, but Windows has 2048, so we loop
1526 # 1024 times (each call leaked two fds).
1527 for i in range(1024):
1528 with self.assertRaises(NONEXISTING_ERRORS):
1529 subprocess.Popen(NONEXISTING_CMD,
1530 stdout=subprocess.PIPE,
1531 stderr=subprocess.PIPE)
1532
1533 def test_nonexisting_with_pipes(self):
1534 # bpo-30121: Popen with pipes must close properly pipes on error.
1535 # Previously, os.close() was called with a Windows handle which is not
1536 # a valid file descriptor.
1537 #
1538 # Run the test in a subprocess to control how the CRT reports errors
1539 # and to get stderr content.
1540 try:
1541 import msvcrt
1542 msvcrt.CrtSetReportMode
1543 except (AttributeError, ImportError):
1544 self.skipTest("need msvcrt.CrtSetReportMode")
1545
1546 code = textwrap.dedent(f"""
1547 import msvcrt
1548 import subprocess
1549
1550 cmd = {NONEXISTING_CMD!r}
1551
1552 for report_type in [msvcrt.CRT_WARN,
1553 msvcrt.CRT_ERROR,
1554 msvcrt.CRT_ASSERT]:
1555 msvcrt.CrtSetReportMode(report_type, msvcrt.CRTDBG_MODE_FILE)
1556 msvcrt.CrtSetReportFile(report_type, msvcrt.CRTDBG_FILE_STDERR)
1557
1558 try:
1559 subprocess.Popen(cmd,
1560 stdout=subprocess.PIPE,
1561 stderr=subprocess.PIPE)
1562 except OSError:
1563 pass
1564 """)
1565 cmd = [sys.executable, "-c", code]
1566 proc = subprocess.Popen(cmd,
1567 stderr=subprocess.PIPE,
1568 universal_newlines=True)
1569 with proc:
1570 stderr = proc.communicate()[1]
1571 self.assertEqual(stderr, "")
1572 self.assertEqual(proc.returncode, 0)
1573
1574 def test_double_close_on_error(self):
1575 # Issue #18851
1576 fds = []
1577 def open_fds():
1578 for i in range(20):
1579 fds.extend(os.pipe())
1580 time.sleep(0.001)
1581 t = threading.Thread(target=open_fds)
1582 t.start()
1583 try:
1584 with self.assertRaises(OSError):
1585 subprocess.Popen(NONEXISTING_CMD,
1586 stdin=subprocess.PIPE,
1587 stdout=subprocess.PIPE,
1588 stderr=subprocess.PIPE)
1589 finally:
1590 t.join()
1591 exc = None
1592 for fd in fds:
1593 # If a double close occurred, some of those fds will
1594 # already have been closed by mistake, and os.close()
1595 # here will raise.
1596 try:
1597 os.close(fd)
1598 except OSError as e:
1599 exc = e
1600 if exc is not None:
1601 raise exc
1602
1603 def test_threadsafe_wait(self):
1604 """Issue21291: Popen.wait() needs to be threadsafe for returncode."""
1605 proc = subprocess.Popen([sys.executable, '-c',
1606 'import time; time.sleep(12)'])
1607 self.assertEqual(proc.returncode, None)
1608 results = []
1609
1610 def kill_proc_timer_thread():
1611 results.append(('thread-start-poll-result', proc.poll()))
1612 # terminate it from the thread and wait for the result.
1613 proc.kill()
1614 proc.wait()
1615 results.append(('thread-after-kill-and-wait', proc.returncode))
1616 # this wait should be a no-op given the above.
1617 proc.wait()
1618 results.append(('thread-after-second-wait', proc.returncode))
1619
1620 # This is a timing sensitive test, the failure mode is
1621 # triggered when both the main thread and this thread are in
1622 # the wait() call at once. The delay here is to allow the
1623 # main thread to most likely be blocked in its wait() call.
1624 t = threading.Timer(0.2, kill_proc_timer_thread)
1625 t.start()
1626
1627 if mswindows:
1628 expected_errorcode = 1
1629 else:
1630 # Should be -9 because of the proc.kill() from the thread.
1631 expected_errorcode = -9
1632
1633 # Wait for the process to finish; the thread should kill it
1634 # long before it finishes on its own. Supplying a timeout
1635 # triggers a different code path for better coverage.
1636 proc.wait(timeout=support.SHORT_TIMEOUT)
1637 self.assertEqual(proc.returncode, expected_errorcode,
1638 msg="unexpected result in wait from main thread")
1639
1640 # This should be a no-op with no change in returncode.
1641 proc.wait()
1642 self.assertEqual(proc.returncode, expected_errorcode,
1643 msg="unexpected result in second main wait.")
1644
1645 t.join()
1646 # Ensure that all of the thread results are as expected.
1647 # When a race condition occurs in wait(), the returncode could
1648 # be set by the wrong thread that doesn't actually have it
1649 # leading to an incorrect value.
1650 self.assertEqual([('thread-start-poll-result', None),
1651 ('thread-after-kill-and-wait', expected_errorcode),
1652 ('thread-after-second-wait', expected_errorcode)],
1653 results)
1654
1655 def test_issue8780(self):
1656 # Ensure that stdout is inherited from the parent
1657 # if stdout=PIPE is not used
1658 code = ';'.join((
1659 'import subprocess, sys',
1660 'retcode = subprocess.call('
1661 "[sys.executable, '-c', 'print(\"Hello World!\")'])",
1662 'assert retcode == 0'))
1663 output = subprocess.check_output([sys.executable, '-c', code])
1664 self.assertStartsWith(output, b'Hello World!')
1665
1666 def test_handles_closed_on_exception(self):
1667 # If CreateProcess exits with an error, ensure the
1668 # duplicate output handles are released
1669 ifhandle, ifname = tempfile.mkstemp()
1670 ofhandle, ofname = tempfile.mkstemp()
1671 efhandle, efname = tempfile.mkstemp()
1672 try:
1673 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
1674 stderr=efhandle)
1675 except OSError:
1676 os.close(ifhandle)
1677 os.remove(ifname)
1678 os.close(ofhandle)
1679 os.remove(ofname)
1680 os.close(efhandle)
1681 os.remove(efname)
1682 self.assertFalse(os.path.exists(ifname))
1683 self.assertFalse(os.path.exists(ofname))
1684 self.assertFalse(os.path.exists(efname))
1685
1686 def test_communicate_epipe(self):
1687 # Issue 10963: communicate() should hide EPIPE
1688 p = subprocess.Popen(ZERO_RETURN_CMD,
1689 stdin=subprocess.PIPE,
1690 stdout=subprocess.PIPE,
1691 stderr=subprocess.PIPE)
1692 self.addCleanup(p.stdout.close)
1693 self.addCleanup(p.stderr.close)
1694 self.addCleanup(p.stdin.close)
1695 p.communicate(b"x" * 2**20)
1696
1697 def test_repr(self):
1698 cases = [
1699 ("ls", True, 123, "<Popen: returncode: 123 args: 'ls'>"),
1700 ('a' * 100, True, 0,
1701 "<Popen: returncode: 0 args: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...>"),
1702 (["ls"], False, None, "<Popen: returncode: None args: ['ls']>"),
1703 (["ls", '--my-opts', 'a' * 100], False, None,
1704 "<Popen: returncode: None args: ['ls', '--my-opts', 'aaaaaaaaaaaaaaaaaaaaaaaa...>"),
1705 (os_helper.FakePath("my-tool.py"), False, 7,
1706 "<Popen: returncode: 7 args: <FakePath 'my-tool.py'>>")
1707 ]
1708 with unittest.mock.patch.object(subprocess.Popen, '_execute_child'):
1709 for cmd, shell, code, sx in cases:
1710 p = subprocess.Popen(cmd, shell=shell)
1711 p.returncode = code
1712 self.assertEqual(repr(p), sx)
1713
1714 def test_communicate_epipe_only_stdin(self):
1715 # Issue 10963: communicate() should hide EPIPE
1716 p = subprocess.Popen(ZERO_RETURN_CMD,
1717 stdin=subprocess.PIPE)
1718 self.addCleanup(p.stdin.close)
1719 p.wait()
1720 p.communicate(b"x" * 2**20)
1721
1722 @unittest.skipUnless(hasattr(signal, 'SIGUSR1'),
1723 "Requires signal.SIGUSR1")
1724 @unittest.skipUnless(hasattr(os, 'kill'),
1725 "Requires os.kill")
1726 @unittest.skipUnless(hasattr(os, 'getppid'),
1727 "Requires os.getppid")
1728 def test_communicate_eintr(self):
1729 # Issue #12493: communicate() should handle EINTR
1730 def handler(signum, frame):
1731 pass
1732 old_handler = signal.signal(signal.SIGUSR1, handler)
1733 self.addCleanup(signal.signal, signal.SIGUSR1, old_handler)
1734
1735 args = [sys.executable, "-c",
1736 'import os, signal;'
1737 'os.kill(os.getppid(), signal.SIGUSR1)']
1738 for stream in ('stdout', 'stderr'):
1739 kw = {stream: subprocess.PIPE}
1740 with subprocess.Popen(args, **kw) as process:
1741 # communicate() will be interrupted by SIGUSR1
1742 process.communicate()
1743
1744
1745 # This test is Linux-ish specific for simplicity to at least have
1746 # some coverage. It is not a platform specific bug.
1747 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
1748 "Linux specific")
1749 def test_failed_child_execute_fd_leak(self):
1750 """Test for the fork() failure fd leak reported in issue16327."""
1751 fd_directory = '/proc/%d/fd' % os.getpid()
1752 fds_before_popen = os.listdir(fd_directory)
1753 with self.assertRaises(PopenTestException):
1754 PopenExecuteChildRaises(
1755 ZERO_RETURN_CMD, stdin=subprocess.PIPE,
1756 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1757
1758 # NOTE: This test doesn't verify that the real _execute_child
1759 # does not close the file descriptors itself on the way out
1760 # during an exception. Code inspection has confirmed that.
1761
1762 fds_after_exception = os.listdir(fd_directory)
1763 self.assertEqual(fds_before_popen, fds_after_exception)
1764
1765 @unittest.skipIf(mswindows, "behavior currently not supported on Windows")
1766 def test_file_not_found_includes_filename(self):
1767 with self.assertRaises(FileNotFoundError) as c:
1768 subprocess.call(['/opt/nonexistent_binary', 'with', 'some', 'args'])
1769 self.assertEqual(c.exception.filename, '/opt/nonexistent_binary')
1770
1771 @unittest.skipIf(mswindows, "behavior currently not supported on Windows")
1772 def test_file_not_found_with_bad_cwd(self):
1773 with self.assertRaises(FileNotFoundError) as c:
1774 subprocess.Popen(['exit', '0'], cwd='/some/nonexistent/directory')
1775 self.assertEqual(c.exception.filename, '/some/nonexistent/directory')
1776
1777 def test_class_getitems(self):
1778 self.assertIsInstance(subprocess.Popen[bytes], types.GenericAlias)
1779 self.assertIsInstance(subprocess.CompletedProcess[str], types.GenericAlias)
1780
1781 @unittest.skipUnless(hasattr(subprocess, '_winapi'),
1782 'need subprocess._winapi')
1783 def test_wait_negative_timeout(self):
1784 proc = subprocess.Popen(ZERO_RETURN_CMD)
1785 with proc:
1786 patch = mock.patch.object(
1787 subprocess._winapi,
1788 'WaitForSingleObject',
1789 return_value=subprocess._winapi.WAIT_OBJECT_0)
1790 with patch as mock_wait:
1791 proc.wait(-1) # negative timeout
1792 mock_wait.assert_called_once_with(proc._handle, 0)
1793 proc.returncode = None
1794
1795 self.assertEqual(proc.wait(), 0)
1796
1797 def test_post_timeout_communicate_sends_input(self):
1798 """GH-141473 regression test; the stdin pipe must close"""
1799 with subprocess.Popen(
1800 [sys.executable, "-uc", """\
1801 import sys
1802 while c := sys.stdin.read(512):
1803 sys.stdout.write(c)
1804 print()
1805 """],
1806 stdin=subprocess.PIPE,
1807 stdout=subprocess.PIPE,
1808 stderr=subprocess.PIPE,
1809 text=True,
1810 ) as proc:
1811 try:
1812 data = f"spam{'#'*4096}beans"
1813 proc.communicate(
1814 input=data,
1815 timeout=0,
1816 )
1817 except subprocess.TimeoutExpired as exc:
1818 pass
1819 # Prior to the bugfix, this would hang as the stdin
1820 # pipe to the child had not been closed.
1821 try:
1822 stdout, stderr = proc.communicate(timeout=15)
1823 except subprocess.TimeoutExpired as exc:
1824 self.fail("communicate() hung waiting on child process that should have seen its stdin pipe close and exit")
1825 self.assertEqual(
1826 proc.returncode, 0,
1827 msg=f"STDERR:\n{stderr}\nSTDOUT:\n{stdout}")
1828 self.assertStartsWith(stdout, "spam")
1829 self.assertIn("beans", stdout)
1830
1831
1832 class RunFuncTestCase(BaseTestCase):
1833 def run_python(self, code, **kwargs):
1834 """Run Python code in a subprocess using subprocess.run"""
1835 argv = [sys.executable, "-c", code]
1836 return subprocess.run(argv, **kwargs)
1837
1838 def test_returncode(self):
1839 # call() function with sequence argument
1840 cp = self.run_python("import sys; sys.exit(47)")
1841 self.assertEqual(cp.returncode, 47)
1842 with self.assertRaises(subprocess.CalledProcessError):
1843 cp.check_returncode()
1844
1845 def test_check(self):
1846 with self.assertRaises(subprocess.CalledProcessError) as c:
1847 self.run_python("import sys; sys.exit(47)", check=True)
1848 self.assertEqual(c.exception.returncode, 47)
1849
1850 def test_check_zero(self):
1851 # check_returncode shouldn't raise when returncode is zero
1852 cp = subprocess.run(ZERO_RETURN_CMD, check=True)
1853 self.assertEqual(cp.returncode, 0)
1854
1855 def test_timeout(self):
1856 # run() function with timeout argument; we want to test that the child
1857 # process gets killed when the timeout expires. If the child isn't
1858 # killed, this call will deadlock since subprocess.run waits for the
1859 # child.
1860 with self.assertRaises(subprocess.TimeoutExpired):
1861 self.run_python("while True: pass", timeout=0.0001)
1862
1863 def test_capture_stdout(self):
1864 # capture stdout with zero return code
1865 cp = self.run_python("print('BDFL')", stdout=subprocess.PIPE)
1866 self.assertIn(b'BDFL', cp.stdout)
1867
1868 def test_capture_stderr(self):
1869 cp = self.run_python("import sys; sys.stderr.write('BDFL')",
1870 stderr=subprocess.PIPE)
1871 self.assertIn(b'BDFL', cp.stderr)
1872
1873 def test_check_output_stdin_arg(self):
1874 # run() can be called with stdin set to a file
1875 tf = tempfile.TemporaryFile()
1876 self.addCleanup(tf.close)
1877 tf.write(b'pear')
1878 tf.seek(0)
1879 cp = self.run_python(
1880 "import sys; sys.stdout.write(sys.stdin.read().upper())",
1881 stdin=tf, stdout=subprocess.PIPE)
1882 self.assertIn(b'PEAR', cp.stdout)
1883
1884 def test_check_output_input_arg(self):
1885 # check_output() can be called with input set to a string
1886 cp = self.run_python(
1887 "import sys; sys.stdout.write(sys.stdin.read().upper())",
1888 input=b'pear', stdout=subprocess.PIPE)
1889 self.assertIn(b'PEAR', cp.stdout)
1890
1891 def test_check_output_stdin_with_input_arg(self):
1892 # run() refuses to accept 'stdin' with 'input'
1893 tf = tempfile.TemporaryFile()
1894 self.addCleanup(tf.close)
1895 tf.write(b'pear')
1896 tf.seek(0)
1897 with self.assertRaises(ValueError,
1898 msg="Expected ValueError when stdin and input args supplied.") as c:
1899 output = self.run_python("print('will not be run')",
1900 stdin=tf, input=b'hare')
1901 self.assertIn('stdin', c.exception.args[0])
1902 self.assertIn('input', c.exception.args[0])
1903
1904 def test_check_output_timeout(self):
1905 with self.assertRaises(subprocess.TimeoutExpired) as c:
1906 cp = self.run_python(
1907 "import time; time.sleep(3600)",
1908 timeout=0.1, stdout=subprocess.PIPE)
1909
1910 def test_run_kwargs(self):
1911 newenv = os.environ.copy()
1912 newenv["FRUIT"] = "banana"
1913 cp = self.run_python(('import sys, os;'
1914 'sys.exit(33 if os.getenv("FRUIT")=="banana" else 31)'),
1915 env=newenv)
1916 self.assertEqual(cp.returncode, 33)
1917
1918 def test_run_with_pathlike_path(self):
1919 # bpo-31961: test run(pathlike_object)
1920 # the name of a command that can be run without
1921 # any arguments that exit fast
1922 prog = 'tree.com' if mswindows else 'ls'
1923 path = shutil.which(prog)
1924 if path is None:
1925 self.skipTest(f'{prog} required for this test')
1926 path = FakePath(path)
1927 res = subprocess.run(path, stdout=subprocess.DEVNULL)
1928 self.assertEqual(res.returncode, 0)
1929 with self.assertRaises(TypeError):
1930 subprocess.run(path, stdout=subprocess.DEVNULL, shell=True)
1931
1932 def test_run_with_bytes_path_and_arguments(self):
1933 # bpo-31961: test run([bytes_object, b'additional arguments'])
1934 path = os.fsencode(sys.executable)
1935 args = [path, '-c', b'import sys; sys.exit(57)']
1936 res = subprocess.run(args)
1937 self.assertEqual(res.returncode, 57)
1938
1939 def test_run_with_pathlike_path_and_arguments(self):
1940 # bpo-31961: test run([pathlike_object, 'additional arguments'])
1941 path = FakePath(sys.executable)
1942 args = [path, '-c', 'import sys; sys.exit(57)']
1943 res = subprocess.run(args)
1944 self.assertEqual(res.returncode, 57)
1945
1946 @unittest.skipUnless(mswindows, "Maybe test trigger a leak on Ubuntu")
1947 def test_run_with_an_empty_env(self):
1948 # gh-105436: fix subprocess.run(..., env={}) broken on Windows
1949 args = [sys.executable, "-c", 'pass']
1950 # Ignore subprocess errors - we only care that the API doesn't
1951 # raise an OSError
1952 subprocess.run(args, env={})
1953
1954 def test_capture_output(self):
1955 cp = self.run_python(("import sys;"
1956 "sys.stdout.write('BDFL'); "
1957 "sys.stderr.write('FLUFL')"),
1958 capture_output=True)
1959 self.assertIn(b'BDFL', cp.stdout)
1960 self.assertIn(b'FLUFL', cp.stderr)
1961
1962 def test_stdout_stdout(self):
1963 # run() refuses to accept stdout=STDOUT
1964 with self.assertRaises(ValueError,
1965 msg=("STDOUT can only be used for stderr")):
1966 self.run_python("print('will not be run')",
1967 stdout=subprocess.STDOUT)
1968
1969 def test_stdout_with_capture_output_arg(self):
1970 # run() refuses to accept 'stdout' with 'capture_output'
1971 tf = tempfile.TemporaryFile()
1972 self.addCleanup(tf.close)
1973 with self.assertRaises(ValueError,
1974 msg=("Expected ValueError when stdout and capture_output "
1975 "args supplied.")) as c:
1976 output = self.run_python("print('will not be run')",
1977 capture_output=True, stdout=tf)
1978 self.assertIn('stdout', c.exception.args[0])
1979 self.assertIn('capture_output', c.exception.args[0])
1980
1981 def test_stderr_with_capture_output_arg(self):
1982 # run() refuses to accept 'stderr' with 'capture_output'
1983 tf = tempfile.TemporaryFile()
1984 self.addCleanup(tf.close)
1985 with self.assertRaises(ValueError,
1986 msg=("Expected ValueError when stderr and capture_output "
1987 "args supplied.")) as c:
1988 output = self.run_python("print('will not be run')",
1989 capture_output=True, stderr=tf)
1990 self.assertIn('stderr', c.exception.args[0])
1991 self.assertIn('capture_output', c.exception.args[0])
1992
1993 # This test _might_ wind up a bit fragile on loaded build+test machines
1994 # as it depends on the timing with wide enough margins for normal situations
1995 # but does assert that it happened "soon enough" to believe the right thing
1996 # happened.
1997 @unittest.skipIf(mswindows, "requires posix like 'sleep' shell command")
1998 def test_run_with_shell_timeout_and_capture_output(self):
1999 """Output capturing after a timeout mustn't hang forever on open filehandles."""
2000 before_secs = time.monotonic()
2001 try:
2002 subprocess.run('sleep 3', shell=True, timeout=0.1,
2003 capture_output=True) # New session unspecified.
2004 except subprocess.TimeoutExpired as exc:
2005 after_secs = time.monotonic()
2006 stacks = traceback.format_exc() # assertRaises doesn't give this.
2007 else:
2008 self.fail("TimeoutExpired not raised.")
2009 self.assertLess(after_secs - before_secs, 1.5,
2010 msg="TimeoutExpired was delayed! Bad traceback:\n```\n"
2011 f"{stacks}```")
2012
2013 def test_encoding_warning(self):
2014 code = textwrap.dedent("""\
2015 from subprocess import *
2016 run("echo hello", shell=True, text=True)
2017 check_output("echo hello", shell=True, text=True)
2018 """)
2019 cp = subprocess.run([sys.executable, "-Xwarn_default_encoding", "-c", code],
2020 capture_output=True)
2021 lines = cp.stderr.splitlines()
2022 self.assertEqual(len(lines), 2, lines)
2023 self.assertStartsWith(lines[0], b"<string>:2: EncodingWarning: ")
2024 self.assertStartsWith(lines[1], b"<string>:3: EncodingWarning: ")
2025
2026
2027 def _get_test_grp_name():
2028 for name_group in ('staff', 'nogroup', 'grp', 'nobody', 'nfsnobody'):
2029 if grp:
2030 try:
2031 grp.getgrnam(name_group)
2032 except KeyError:
2033 continue
2034 return name_group
2035 else:
2036 raise unittest.SkipTest('No identified group name to use for this test on this platform.')
2037
2038
2039 @unittest.skipIf(mswindows, "POSIX specific tests")
2040 class POSIXProcessTestCase(BaseTestCase):
2041
2042 def setUp(self):
2043 super().setUp()
2044 self._nonexistent_dir = "/_this/pa.th/does/not/exist"
2045
2046 def _get_chdir_exception(self):
2047 try:
2048 os.chdir(self._nonexistent_dir)
2049 except OSError as e:
2050 # This avoids hard coding the errno value or the OS perror()
2051 # string and instead capture the exception that we want to see
2052 # below for comparison.
2053 desired_exception = e
2054 else:
2055 self.fail("chdir to nonexistent directory %s succeeded." %
2056 self._nonexistent_dir)
2057 return desired_exception
2058
2059 def test_exception_cwd(self):
2060 """Test error in the child raised in the parent for a bad cwd."""
2061 desired_exception = self._get_chdir_exception()
2062 try:
2063 p = subprocess.Popen([sys.executable, "-c", ""],
2064 cwd=self._nonexistent_dir)
2065 except OSError as e:
2066 # Test that the child process chdir failure actually makes
2067 # it up to the parent process as the correct exception.
2068 self.assertEqual(desired_exception.errno, e.errno)
2069 self.assertEqual(desired_exception.strerror, e.strerror)
2070 self.assertEqual(desired_exception.filename, e.filename)
2071 else:
2072 self.fail("Expected OSError: %s" % desired_exception)
2073
2074 def test_exception_bad_executable(self):
2075 """Test error in the child raised in the parent for a bad executable."""
2076 desired_exception = self._get_chdir_exception()
2077 try:
2078 p = subprocess.Popen([sys.executable, "-c", ""],
2079 executable=self._nonexistent_dir)
2080 except OSError as e:
2081 # Test that the child process exec failure actually makes
2082 # it up to the parent process as the correct exception.
2083 self.assertEqual(desired_exception.errno, e.errno)
2084 self.assertEqual(desired_exception.strerror, e.strerror)
2085 self.assertEqual(desired_exception.filename, e.filename)
2086 else:
2087 self.fail("Expected OSError: %s" % desired_exception)
2088
2089 def test_exception_bad_args_0(self):
2090 """Test error in the child raised in the parent for a bad args[0]."""
2091 desired_exception = self._get_chdir_exception()
2092 try:
2093 p = subprocess.Popen([self._nonexistent_dir, "-c", ""])
2094 except OSError as e:
2095 # Test that the child process exec failure actually makes
2096 # it up to the parent process as the correct exception.
2097 self.assertEqual(desired_exception.errno, e.errno)
2098 self.assertEqual(desired_exception.strerror, e.strerror)
2099 self.assertEqual(desired_exception.filename, e.filename)
2100 else:
2101 self.fail("Expected OSError: %s" % desired_exception)
2102
2103 # We mock the __del__ method for Popen in the next two tests
2104 # because it does cleanup based on the pid returned by fork_exec
2105 # along with issuing a resource warning if it still exists. Since
2106 # we don't actually spawn a process in these tests we can forego
2107 # the destructor. An alternative would be to set _child_created to
2108 # False before the destructor is called but there is no easy way
2109 # to do that
2110 class PopenNoDestructor(subprocess.Popen):
2111 def __del__(self):
2112 pass
2113
2114 @mock.patch("subprocess._fork_exec")
2115 def test_exception_errpipe_normal(self, fork_exec):
2116 """Test error passing done through errpipe_write in the good case"""
2117 def proper_error(*args):
2118 errpipe_write = args[13]
2119 # Write the hex for the error code EISDIR: 'is a directory'
2120 err_code = '{:x}'.format(errno.EISDIR).encode()
2121 os.write(errpipe_write, b"OSError:" + err_code + b":")
2122 return 0
2123
2124 fork_exec.side_effect = proper_error
2125
2126 with mock.patch("subprocess.os.waitpid",
2127 side_effect=ChildProcessError):
2128 with self.assertRaises(IsADirectoryError):
2129 self.PopenNoDestructor(["non_existent_command"])
2130
2131 @mock.patch("subprocess._fork_exec")
2132 def test_exception_errpipe_bad_data(self, fork_exec):
2133 """Test error passing done through errpipe_write where its not
2134 in the expected format"""
2135 error_data = b"\xFF\x00\xDE\xAD"
2136 def bad_error(*args):
2137 errpipe_write = args[13]
2138 # Anything can be in the pipe, no assumptions should
2139 # be made about its encoding, so we'll write some
2140 # arbitrary hex bytes to test it out
2141 os.write(errpipe_write, error_data)
2142 return 0
2143
2144 fork_exec.side_effect = bad_error
2145
2146 with mock.patch("subprocess.os.waitpid",
2147 side_effect=ChildProcessError):
2148 with self.assertRaises(subprocess.SubprocessError) as e:
2149 self.PopenNoDestructor(["non_existent_command"])
2150
2151 self.assertIn(repr(error_data), str(e.exception))
2152
2153 @unittest.skipIf(not os.path.exists('/proc/self/status'),
2154 "need /proc/self/status")
2155 def test_restore_signals(self):
2156 # Blindly assume that cat exists on systems with /proc/self/status...
2157 default_proc_status = subprocess.check_output(
2158 ['cat', '/proc/self/status'],
2159 restore_signals=False)
2160 for line in default_proc_status.splitlines():
2161 if line.startswith(b'SigIgn'):
2162 default_sig_ign_mask = line
2163 break
2164 else:
2165 self.skipTest("SigIgn not found in /proc/self/status.")
2166 restored_proc_status = subprocess.check_output(
2167 ['cat', '/proc/self/status'],
2168 restore_signals=True)
2169 for line in restored_proc_status.splitlines():
2170 if line.startswith(b'SigIgn'):
2171 restored_sig_ign_mask = line
2172 break
2173 self.assertNotEqual(default_sig_ign_mask, restored_sig_ign_mask,
2174 msg="restore_signals=True should've unblocked "
2175 "SIGPIPE and friends.")
2176
2177 def test_start_new_session(self):
2178 # For code coverage of calling setsid(). We don't care if we get an
2179 # EPERM error from it depending on the test execution environment, that
2180 # still indicates that it was called.
2181 try:
2182 output = subprocess.check_output(
2183 [sys.executable, "-c", "import os; print(os.getsid(0))"],
2184 start_new_session=True)
2185 except PermissionError as e:
2186 if e.errno != errno.EPERM:
2187 raise # EACCES?
2188 else:
2189 parent_sid = os.getsid(0)
2190 child_sid = int(output)
2191 self.assertNotEqual(parent_sid, child_sid)
2192
2193 @unittest.skipUnless(hasattr(os, 'setpgid') and hasattr(os, 'getpgid'),
2194 'no setpgid or getpgid on platform')
2195 def test_process_group_0(self):
2196 # For code coverage of calling setpgid(). We don't care if we get an
2197 # EPERM error from it depending on the test execution environment, that
2198 # still indicates that it was called.
2199 try:
2200 output = subprocess.check_output(
2201 [sys.executable, "-c", "import os; print(os.getpgid(0))"],
2202 process_group=0)
2203 except PermissionError as e:
2204 if e.errno != errno.EPERM:
2205 raise # EACCES?
2206 else:
2207 parent_pgid = os.getpgid(0)
2208 child_pgid = int(output)
2209 self.assertNotEqual(parent_pgid, child_pgid)
2210
2211 @unittest.skipUnless(hasattr(os, 'setreuid'), 'no setreuid on platform')
2212 def test_user(self):
2213 # For code coverage of the user parameter. We don't care if we get a
2214 # permission error from it depending on the test execution environment,
2215 # that still indicates that it was called.
2216
2217 uid = os.geteuid()
2218 test_users = [65534 if uid != 65534 else 65533, uid]
2219 name_uid = "nobody" if sys.platform != 'darwin' else "unknown"
2220
2221 if pwd is not None:
2222 try:
2223 pwd.getpwnam(name_uid)
2224 test_users.append(name_uid)
2225 except KeyError:
2226 # unknown user name
2227 name_uid = None
2228
2229 for user in test_users:
2230 # posix_spawn() may be used with close_fds=False
2231 for close_fds in (False, True):
2232 with self.subTest(user=user, close_fds=close_fds):
2233 try:
2234 output = subprocess.check_output(
2235 [sys.executable, "-c",
2236 "import os; print(os.getuid())"],
2237 user=user,
2238 close_fds=close_fds)
2239 except PermissionError as e: # (EACCES, EPERM)
2240 if e.errno == errno.EACCES:
2241 self.assertEqual(e.filename, sys.executable)
2242 else:
2243 self.assertIsNone(e.filename)
2244 else:
2245 if isinstance(user, str):
2246 user_uid = pwd.getpwnam(user).pw_uid
2247 else:
2248 user_uid = user
2249 child_user = int(output)
2250 self.assertEqual(child_user, user_uid)
2251
2252 with self.assertRaises(ValueError):
2253 subprocess.check_call(ZERO_RETURN_CMD, user=-1)
2254
2255 with self.assertRaises(OverflowError):
2256 subprocess.check_call(ZERO_RETURN_CMD,
2257 cwd=os.curdir, env=os.environ, user=2**64)
2258
2259 if pwd is None and name_uid is not None:
2260 with self.assertRaises(ValueError):
2261 subprocess.check_call(ZERO_RETURN_CMD, user=name_uid)
2262
2263 @unittest.skipIf(hasattr(os, 'setreuid'), 'setreuid() available on platform')
2264 def test_user_error(self):
2265 with self.assertRaises(ValueError):
2266 subprocess.check_call(ZERO_RETURN_CMD, user=65535)
2267
2268 @unittest.skipUnless(hasattr(os, 'setregid'), 'no setregid() on platform')
2269 def test_group(self):
2270 gid = os.getegid()
2271 group_list = [65534 if gid != 65534 else 65533]
2272 name_group = _get_test_grp_name()
2273
2274 if grp is not None:
2275 group_list.append(name_group)
2276
2277 for group in group_list + [gid]:
2278 # posix_spawn() may be used with close_fds=False
2279 for close_fds in (False, True):
2280 with self.subTest(group=group, close_fds=close_fds):
2281 try:
2282 output = subprocess.check_output(
2283 [sys.executable, "-c",
2284 "import os; print(os.getgid())"],
2285 group=group,
2286 close_fds=close_fds)
2287 except PermissionError as e: # (EACCES, EPERM)
2288 self.assertIsNone(e.filename)
2289 else:
2290 if isinstance(group, str):
2291 group_gid = grp.getgrnam(group).gr_gid
2292 else:
2293 group_gid = group
2294
2295 child_group = int(output)
2296 self.assertEqual(child_group, group_gid)
2297
2298 # make sure we bomb on negative values
2299 with self.assertRaises(ValueError):
2300 subprocess.check_call(ZERO_RETURN_CMD, group=-1)
2301
2302 with self.assertRaises(OverflowError):
2303 subprocess.check_call(ZERO_RETURN_CMD,
2304 cwd=os.curdir, env=os.environ, group=2**64)
2305
2306 if grp is None:
2307 with self.assertRaises(ValueError):
2308 subprocess.check_call(ZERO_RETURN_CMD, group=name_group)
2309
2310 @unittest.skipIf(hasattr(os, 'setregid'), 'setregid() available on platform')
2311 def test_group_error(self):
2312 with self.assertRaises(ValueError):
2313 subprocess.check_call(ZERO_RETURN_CMD, group=65535)
2314
2315 @unittest.skipUnless(hasattr(os, 'setgroups'), 'no setgroups() on platform')
2316 def test_extra_groups(self):
2317 gid = os.getegid()
2318 group_list = [65534 if gid != 65534 else 65533]
2319 self._test_extra_groups_impl(gid=gid, group_list=group_list)
2320
2321 @unittest.skipUnless(hasattr(os, 'setgroups'), 'no setgroups() on platform')
2322 def test_extra_groups_empty_list(self):
2323 self._test_extra_groups_impl(gid=os.getegid(), group_list=[])
2324
2325 def _test_extra_groups_impl(self, *, gid, group_list):
2326 name_group = _get_test_grp_name()
2327
2328 if grp is not None:
2329 group_list.append(name_group)
2330
2331 try:
2332 output = subprocess.check_output(
2333 [sys.executable, "-c",
2334 "import os, sys, json; json.dump(os.getgroups(), sys.stdout)"],
2335 extra_groups=group_list)
2336 except PermissionError as e:
2337 self.assertIsNone(e.filename)
2338 self.skipTest("setgroup() EPERM; this test may require root.")
2339 else:
2340 parent_groups = os.getgroups()
2341 child_groups = json.loads(output)
2342
2343 if grp is not None:
2344 desired_gids = [grp.getgrnam(g).gr_gid if isinstance(g, str) else g
2345 for g in group_list]
2346 else:
2347 desired_gids = group_list
2348
2349 self.assertEqual(set(desired_gids), set(child_groups))
2350
2351 if grp is None:
2352 with self.assertRaises(ValueError):
2353 subprocess.check_call(ZERO_RETURN_CMD,
2354 extra_groups=[name_group])
2355
2356 # No skip necessary, this test won't make it to a setgroup() call.
2357 def test_extra_groups_invalid_gid_t_values(self):
2358 with self.assertRaises(ValueError):
2359 subprocess.check_call(ZERO_RETURN_CMD, extra_groups=[-1])
2360
2361 with self.assertRaises(ValueError):
2362 subprocess.check_call(ZERO_RETURN_CMD,
2363 cwd=os.curdir, env=os.environ,
2364 extra_groups=[2**64])
2365
2366 @unittest.skipIf(mswindows or not hasattr(os, 'umask'),
2367 'POSIX umask() is not available.')
2368 def test_umask(self):
2369 tmpdir = None
2370 try:
2371 tmpdir = tempfile.mkdtemp()
2372 name = os.path.join(tmpdir, "beans")
2373 # We set an unusual umask in the child so as a unique mode
2374 # for us to test the child's touched file for.
2375 subprocess.check_call(
2376 [sys.executable, "-c", f"open({name!r}, 'w').close()"],
2377 umask=0o053)
2378 # Ignore execute permissions entirely in our test,
2379 # filesystems could be mounted to ignore or force that.
2380 st_mode = os.stat(name).st_mode & 0o666
2381 expected_mode = 0o624
2382 self.assertEqual(expected_mode, st_mode,
2383 msg=f'{oct(expected_mode)} != {oct(st_mode)}')
2384 finally:
2385 if tmpdir is not None:
2386 shutil.rmtree(tmpdir)
2387
2388 def test_run_abort(self):
2389 # returncode handles signal termination
2390 with support.SuppressCrashReport():
2391 p = subprocess.Popen([sys.executable, "-c",
2392 'import os; os.abort()'])
2393 p.wait()
2394 self.assertEqual(-p.returncode, signal.SIGABRT)
2395
2396 def test_CalledProcessError_str_signal(self):
2397 err = subprocess.CalledProcessError(-int(signal.SIGABRT), "fake cmd")
2398 error_string = str(err)
2399 # We're relying on the repr() of the signal.Signals intenum to provide
2400 # the word signal, the signal name and the numeric value.
2401 self.assertIn("signal", error_string.lower())
2402 # We're not being specific about the signal name as some signals have
2403 # multiple names and which name is revealed can vary.
2404 self.assertIn("SIG", error_string)
2405 self.assertIn(str(signal.SIGABRT), error_string)
2406
2407 def test_CalledProcessError_str_unknown_signal(self):
2408 err = subprocess.CalledProcessError(-9876543, "fake cmd")
2409 error_string = str(err)
2410 self.assertIn("unknown signal 9876543.", error_string)
2411
2412 def test_CalledProcessError_str_non_zero(self):
2413 err = subprocess.CalledProcessError(2, "fake cmd")
2414 error_string = str(err)
2415 self.assertIn("non-zero exit status 2.", error_string)
2416
2417 def test_preexec(self):
2418 # DISCLAIMER: Setting environment variables is *not* a good use
2419 # of a preexec_fn. This is merely a test.
2420 p = subprocess.Popen([sys.executable, "-c",
2421 'import sys,os;'
2422 'sys.stdout.write(os.getenv("FRUIT"))'],
2423 stdout=subprocess.PIPE,
2424 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
2425 with p:
2426 self.assertEqual(p.stdout.read(), b"apple")
2427
2428 def test_preexec_exception(self):
2429 def raise_it():
2430 raise ValueError("What if two swallows carried a coconut?")
2431 try:
2432 p = subprocess.Popen([sys.executable, "-c", ""],
2433 preexec_fn=raise_it)
2434 except subprocess.SubprocessError as e:
2435 self.assertTrue(
2436 subprocess._fork_exec,
2437 "Expected a ValueError from the preexec_fn")
2438 except ValueError as e:
2439 self.assertIn("coconut", e.args[0])
2440 else:
2441 self.fail("Exception raised by preexec_fn did not make it "
2442 "to the parent process.")
2443
2444 class _TestExecuteChildPopen(subprocess.Popen):
2445 """Used to test behavior at the end of _execute_child."""
2446 def __init__(self, testcase, *args, **kwargs):
2447 self._testcase = testcase
2448 subprocess.Popen.__init__(self, *args, **kwargs)
2449
2450 def _execute_child(self, *args, **kwargs):
2451 try:
2452 subprocess.Popen._execute_child(self, *args, **kwargs)
2453 finally:
2454 # Open a bunch of file descriptors and verify that
2455 # none of them are the same as the ones the Popen
2456 # instance is using for stdin/stdout/stderr.
2457 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
2458 for _ in range(8)]
2459 try:
2460 for fd in devzero_fds:
2461 self._testcase.assertNotIn(
2462 fd, (self.stdin.fileno(), self.stdout.fileno(),
2463 self.stderr.fileno()),
2464 msg="At least one fd was closed early.")
2465 finally:
2466 for fd in devzero_fds:
2467 os.close(fd)
2468
2469 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
2470 def test_preexec_errpipe_does_not_double_close_pipes(self):
2471 """Issue16140: Don't double close pipes on preexec error."""
2472
2473 def raise_it():
2474 raise subprocess.SubprocessError(
2475 "force the _execute_child() errpipe_data path.")
2476
2477 with self.assertRaises(subprocess.SubprocessError):
2478 self._TestExecuteChildPopen(
2479 self, ZERO_RETURN_CMD,
2480 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2481 stderr=subprocess.PIPE, preexec_fn=raise_it)
2482
2483 def test_preexec_gc_module_failure(self):
2484 # This tests the code that disables garbage collection if the child
2485 # process will execute any Python.
2486 enabled = gc.isenabled()
2487 try:
2488 gc.disable()
2489 self.assertFalse(gc.isenabled())
2490 subprocess.call([sys.executable, '-c', ''],
2491 preexec_fn=lambda: None)
2492 self.assertFalse(gc.isenabled(),
2493 "Popen enabled gc when it shouldn't.")
2494
2495 gc.enable()
2496 self.assertTrue(gc.isenabled())
2497 subprocess.call([sys.executable, '-c', ''],
2498 preexec_fn=lambda: None)
2499 self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
2500 finally:
2501 if not enabled:
2502 gc.disable()
2503
2504 @unittest.skipIf(
2505 sys.platform == 'darwin', 'setrlimit() seems to fail on OS X')
2506 def test_preexec_fork_failure(self):
2507 # The internal code did not preserve the previous exception when
2508 # re-enabling garbage collection
2509 try:
2510 from resource import getrlimit, setrlimit, RLIMIT_NPROC
2511 except ImportError as err:
2512 self.skipTest(err) # RLIMIT_NPROC is specific to Linux and BSD
2513 limits = getrlimit(RLIMIT_NPROC)
2514 [_, hard] = limits
2515 setrlimit(RLIMIT_NPROC, (0, hard))
2516 self.addCleanup(setrlimit, RLIMIT_NPROC, limits)
2517 try:
2518 subprocess.call([sys.executable, '-c', ''],
2519 preexec_fn=lambda: None)
2520 except BlockingIOError:
2521 # Forking should raise EAGAIN, translated to BlockingIOError
2522 pass
2523 else:
2524 self.skipTest('RLIMIT_NPROC had no effect; probably superuser')
2525
2526 def test_args_string(self):
2527 # args is a string
2528 fd, fname = tempfile.mkstemp()
2529 # reopen in text mode
2530 with open(fd, "w", errors="surrogateescape") as fobj:
2531 fobj.write("#!%s\n" % support.unix_shell)
2532 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
2533 sys.executable)
2534 os.chmod(fname, 0o700)
2535 p = subprocess.Popen(fname)
2536 p.wait()
2537 os.remove(fname)
2538 self.assertEqual(p.returncode, 47)
2539
2540 def test_invalid_args(self):
2541 # invalid arguments should raise ValueError
2542 self.assertRaises(ValueError, subprocess.call,
2543 [sys.executable, "-c",
2544 "import sys; sys.exit(47)"],
2545 startupinfo=47)
2546 self.assertRaises(ValueError, subprocess.call,
2547 [sys.executable, "-c",
2548 "import sys; sys.exit(47)"],
2549 creationflags=47)
2550
2551 def test_shell_sequence(self):
2552 # Run command through the shell (sequence)
2553 newenv = os.environ.copy()
2554 newenv["FRUIT"] = "apple"
2555 p = subprocess.Popen(["echo $FRUIT"], shell=1,
2556 stdout=subprocess.PIPE,
2557 env=newenv)
2558 with p:
2559 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
2560
2561 def test_shell_string(self):
2562 # Run command through the shell (string)
2563 newenv = os.environ.copy()
2564 newenv["FRUIT"] = "apple"
2565 p = subprocess.Popen("echo $FRUIT", shell=1,
2566 stdout=subprocess.PIPE,
2567 env=newenv)
2568 with p:
2569 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
2570
2571 def test_call_string(self):
2572 # call() function with string argument on UNIX
2573 fd, fname = tempfile.mkstemp()
2574 # reopen in text mode
2575 with open(fd, "w", errors="surrogateescape") as fobj:
2576 fobj.write("#!%s\n" % support.unix_shell)
2577 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
2578 sys.executable)
2579 os.chmod(fname, 0o700)
2580 rc = subprocess.call(fname)
2581 os.remove(fname)
2582 self.assertEqual(rc, 47)
2583
2584 def test_specific_shell(self):
2585 # Issue #9265: Incorrect name passed as arg[0].
2586 shells = []
2587 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
2588 for name in ['bash', 'ksh']:
2589 sh = os.path.join(prefix, name)
2590 if os.path.isfile(sh):
2591 shells.append(sh)
2592 if not shells: # Will probably work for any shell but csh.
2593 self.skipTest("bash or ksh required for this test")
2594 sh = '/bin/sh'
2595 if os.path.isfile(sh) and not os.path.islink(sh):
2596 # Test will fail if /bin/sh is a symlink to csh.
2597 shells.append(sh)
2598 for sh in shells:
2599 p = subprocess.Popen("echo $0", executable=sh, shell=True,
2600 stdout=subprocess.PIPE)
2601 with p:
2602 self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
2603
2604 def _kill_process(self, method, *args):
2605 # Do not inherit file handles from the parent.
2606 # It should fix failures on some platforms.
2607 # Also set the SIGINT handler to the default to make sure it's not
2608 # being ignored (some tests rely on that.)
2609 old_handler = signal.signal(signal.SIGINT, signal.default_int_handler)
2610 try:
2611 p = subprocess.Popen([sys.executable, "-c", """if 1:
2612 import sys, time
2613 sys.stdout.write('x\\n')
2614 sys.stdout.flush()
2615 time.sleep(30)
2616 """],
2617 close_fds=True,
2618 stdin=subprocess.PIPE,
2619 stdout=subprocess.PIPE,
2620 stderr=subprocess.PIPE)
2621 finally:
2622 signal.signal(signal.SIGINT, old_handler)
2623 # Wait for the interpreter to be completely initialized before
2624 # sending any signal.
2625 p.stdout.read(1)
2626 getattr(p, method)(*args)
2627 return p
2628
2629 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
2630 "Due to known OS bug (issue #16762)")
2631 def _kill_dead_process(self, method, *args):
2632 # Do not inherit file handles from the parent.
2633 # It should fix failures on some platforms.
2634 p = subprocess.Popen([sys.executable, "-c", """if 1:
2635 import sys, time
2636 sys.stdout.write('x\\n')
2637 sys.stdout.flush()
2638 """],
2639 close_fds=True,
2640 stdin=subprocess.PIPE,
2641 stdout=subprocess.PIPE,
2642 stderr=subprocess.PIPE)
2643 # Wait for the interpreter to be completely initialized before
2644 # sending any signal.
2645 p.stdout.read(1)
2646 # The process should end after this
2647 time.sleep(1)
2648 # This shouldn't raise even though the child is now dead
2649 getattr(p, method)(*args)
2650 p.communicate()
2651
2652 def test_send_signal(self):
2653 p = self._kill_process('send_signal', signal.SIGINT)
2654 _, stderr = p.communicate()
2655 self.assertIn(b'KeyboardInterrupt', stderr)
2656 self.assertNotEqual(p.wait(), 0)
2657
2658 def test_kill(self):
2659 p = self._kill_process('kill')
2660 _, stderr = p.communicate()
2661 self.assertEqual(stderr, b'')
2662 self.assertEqual(p.wait(), -signal.SIGKILL)
2663
2664 def test_terminate(self):
2665 p = self._kill_process('terminate')
2666 _, stderr = p.communicate()
2667 self.assertEqual(stderr, b'')
2668 self.assertEqual(p.wait(), -signal.SIGTERM)
2669
2670 def test_send_signal_dead(self):
2671 # Sending a signal to a dead process
2672 self._kill_dead_process('send_signal', signal.SIGINT)
2673
2674 def test_kill_dead(self):
2675 # Killing a dead process
2676 self._kill_dead_process('kill')
2677
2678 def test_terminate_dead(self):
2679 # Terminating a dead process
2680 self._kill_dead_process('terminate')
2681
2682 def _save_fds(self, save_fds):
2683 fds = []
2684 for fd in save_fds:
2685 inheritable = os.get_inheritable(fd)
2686 saved = os.dup(fd)
2687 fds.append((fd, saved, inheritable))
2688 return fds
2689
2690 def _restore_fds(self, fds):
2691 for fd, saved, inheritable in fds:
2692 os.dup2(saved, fd, inheritable=inheritable)
2693 os.close(saved)
2694
2695 def check_close_std_fds(self, fds):
2696 # Issue #9905: test that subprocess pipes still work properly with
2697 # some standard fds closed
2698 stdin = 0
2699 saved_fds = self._save_fds(fds)
2700 for fd, saved, inheritable in saved_fds:
2701 if fd == 0:
2702 stdin = saved
2703 break
2704 try:
2705 for fd in fds:
2706 os.close(fd)
2707 out, err = subprocess.Popen([sys.executable, "-c",
2708 'import sys;'
2709 'sys.stdout.write("apple");'
2710 'sys.stdout.flush();'
2711 'sys.stderr.write("orange")'],
2712 stdin=stdin,
2713 stdout=subprocess.PIPE,
2714 stderr=subprocess.PIPE).communicate()
2715 self.assertEqual(out, b'apple')
2716 self.assertEqual(err, b'orange')
2717 finally:
2718 self._restore_fds(saved_fds)
2719
2720 def test_close_fd_0(self):
2721 self.check_close_std_fds([0])
2722
2723 def test_close_fd_1(self):
2724 self.check_close_std_fds([1])
2725
2726 def test_close_fd_2(self):
2727 self.check_close_std_fds([2])
2728
2729 def test_close_fds_0_1(self):
2730 self.check_close_std_fds([0, 1])
2731
2732 def test_close_fds_0_2(self):
2733 self.check_close_std_fds([0, 2])
2734
2735 def test_close_fds_1_2(self):
2736 self.check_close_std_fds([1, 2])
2737
2738 def test_close_fds_0_1_2(self):
2739 # Issue #10806: test that subprocess pipes still work properly with
2740 # all standard fds closed.
2741 self.check_close_std_fds([0, 1, 2])
2742
2743 def test_small_errpipe_write_fd(self):
2744 """Issue #15798: Popen should work when stdio fds are available."""
2745 new_stdin = os.dup(0)
2746 new_stdout = os.dup(1)
2747 try:
2748 os.close(0)
2749 os.close(1)
2750
2751 # Side test: if errpipe_write fails to have its CLOEXEC
2752 # flag set this should cause the parent to think the exec
2753 # failed. Extremely unlikely: everyone supports CLOEXEC.
2754 subprocess.Popen([
2755 sys.executable, "-c",
2756 "print('AssertionError:0:CLOEXEC failure.')"]).wait()
2757 finally:
2758 # Restore original stdin and stdout
2759 os.dup2(new_stdin, 0)
2760 os.dup2(new_stdout, 1)
2761 os.close(new_stdin)
2762 os.close(new_stdout)
2763
2764 def test_remapping_std_fds(self):
2765 # open up some temporary files
2766 temps = [tempfile.mkstemp() for i in range(3)]
2767 try:
2768 temp_fds = [fd for fd, fname in temps]
2769
2770 # unlink the files -- we won't need to reopen them
2771 for fd, fname in temps:
2772 os.unlink(fname)
2773
2774 # write some data to what will become stdin, and rewind
2775 os.write(temp_fds[1], b"STDIN")
2776 os.lseek(temp_fds[1], 0, 0)
2777
2778 # move the standard file descriptors out of the way
2779 saved_fds = self._save_fds(range(3))
2780 try:
2781 # duplicate the file objects over the standard fd's
2782 for fd, temp_fd in enumerate(temp_fds):
2783 os.dup2(temp_fd, fd)
2784
2785 # now use those files in the "wrong" order, so that subprocess
2786 # has to rearrange them in the child
2787 p = subprocess.Popen([sys.executable, "-c",
2788 'import sys; got = sys.stdin.read();'
2789 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
2790 stdin=temp_fds[1],
2791 stdout=temp_fds[2],
2792 stderr=temp_fds[0])
2793 p.wait()
2794 finally:
2795 self._restore_fds(saved_fds)
2796
2797 for fd in temp_fds:
2798 os.lseek(fd, 0, 0)
2799
2800 out = os.read(temp_fds[2], 1024)
2801 err = os.read(temp_fds[0], 1024).strip()
2802 self.assertEqual(out, b"got STDIN")
2803 self.assertEqual(err, b"err")
2804
2805 finally:
2806 for fd in temp_fds:
2807 os.close(fd)
2808
2809 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
2810 # open up some temporary files
2811 temps = [tempfile.mkstemp() for i in range(3)]
2812 temp_fds = [fd for fd, fname in temps]
2813 try:
2814 # unlink the files -- we won't need to reopen them
2815 for fd, fname in temps:
2816 os.unlink(fname)
2817
2818 # save a copy of the standard file descriptors
2819 saved_fds = self._save_fds(range(3))
2820 try:
2821 # duplicate the temp files over the standard fd's 0, 1, 2
2822 for fd, temp_fd in enumerate(temp_fds):
2823 os.dup2(temp_fd, fd)
2824
2825 # write some data to what will become stdin, and rewind
2826 os.write(stdin_no, b"STDIN")
2827 os.lseek(stdin_no, 0, 0)
2828
2829 # now use those files in the given order, so that subprocess
2830 # has to rearrange them in the child
2831 p = subprocess.Popen([sys.executable, "-c",
2832 'import sys; got = sys.stdin.read();'
2833 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
2834 stdin=stdin_no,
2835 stdout=stdout_no,
2836 stderr=stderr_no)
2837 p.wait()
2838
2839 for fd in temp_fds:
2840 os.lseek(fd, 0, 0)
2841
2842 out = os.read(stdout_no, 1024)
2843 err = os.read(stderr_no, 1024).strip()
2844 finally:
2845 self._restore_fds(saved_fds)
2846
2847 self.assertEqual(out, b"got STDIN")
2848 self.assertEqual(err, b"err")
2849
2850 finally:
2851 for fd in temp_fds:
2852 os.close(fd)
2853
2854 # When duping fds, if there arises a situation where one of the fds is
2855 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
2856 # This tests all combinations of this.
2857 def test_swap_fds(self):
2858 self.check_swap_fds(0, 1, 2)
2859 self.check_swap_fds(0, 2, 1)
2860 self.check_swap_fds(1, 0, 2)
2861 self.check_swap_fds(1, 2, 0)
2862 self.check_swap_fds(2, 0, 1)
2863 self.check_swap_fds(2, 1, 0)
2864
2865 def _check_swap_std_fds_with_one_closed(self, from_fds, to_fds):
2866 saved_fds = self._save_fds(range(3))
2867 try:
2868 for from_fd in from_fds:
2869 with tempfile.TemporaryFile() as f:
2870 os.dup2(f.fileno(), from_fd)
2871
2872 fd_to_close = (set(range(3)) - set(from_fds)).pop()
2873 os.close(fd_to_close)
2874
2875 arg_names = ['stdin', 'stdout', 'stderr']
2876 kwargs = {}
2877 for from_fd, to_fd in zip(from_fds, to_fds):
2878 kwargs[arg_names[to_fd]] = from_fd
2879
2880 code = textwrap.dedent(r'''
2881 import os, sys
2882 skipped_fd = int(sys.argv[1])
2883 for fd in range(3):
2884 if fd != skipped_fd:
2885 os.write(fd, str(fd).encode('ascii'))
2886 ''')
2887
2888 skipped_fd = (set(range(3)) - set(to_fds)).pop()
2889
2890 rc = subprocess.call([sys.executable, '-c', code, str(skipped_fd)],
2891 **kwargs)
2892 self.assertEqual(rc, 0)
2893
2894 for from_fd, to_fd in zip(from_fds, to_fds):
2895 os.lseek(from_fd, 0, os.SEEK_SET)
2896 read_bytes = os.read(from_fd, 1024)
2897 read_fds = list(map(int, read_bytes.decode('ascii')))
2898 msg = textwrap.dedent(f"""
2899 When testing {from_fds} to {to_fds} redirection,
2900 parent descriptor {from_fd} got redirected
2901 to descriptor(s) {read_fds} instead of descriptor {to_fd}.
2902 """)
2903 self.assertEqual([to_fd], read_fds, msg)
2904 finally:
2905 self._restore_fds(saved_fds)
2906
2907 # Check that subprocess can remap std fds correctly even
2908 # if one of them is closed (#32844).
2909 def test_swap_std_fds_with_one_closed(self):
2910 for from_fds in itertools.combinations(range(3), 2):
2911 for to_fds in itertools.permutations(range(3), 2):
2912 self._check_swap_std_fds_with_one_closed(from_fds, to_fds)
2913
2914 def test_surrogates_error_message(self):
2915 def prepare():
2916 raise ValueError("surrogate:\uDCff")
2917
2918 try:
2919 subprocess.call(
2920 ZERO_RETURN_CMD,
2921 preexec_fn=prepare)
2922 except ValueError as err:
2923 # Pure Python implementations keeps the message
2924 self.assertIsNone(subprocess._fork_exec)
2925 self.assertEqual(str(err), "surrogate:\uDCff")
2926 except subprocess.SubprocessError as err:
2927 # _posixsubprocess uses a default message
2928 self.assertIsNotNone(subprocess._fork_exec)
2929 self.assertEqual(str(err), "Exception occurred in preexec_fn.")
2930 else:
2931 self.fail("Expected ValueError or subprocess.SubprocessError")
2932
2933 def test_undecodable_env(self):
2934 for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')):
2935 encoded_value = value.encode("ascii", "surrogateescape")
2936
2937 # test str with surrogates
2938 script = "import os; print(ascii(os.getenv(%s)))" % repr(key)
2939 env = os.environ.copy()
2940 env[key] = value
2941 # Use C locale to get ASCII for the locale encoding to force
2942 # surrogate-escaping of \xFF in the child process
2943 env['LC_ALL'] = 'C'
2944 decoded_value = value
2945 stdout = subprocess.check_output(
2946 [sys.executable, "-c", script],
2947 env=env)
2948 stdout = stdout.rstrip(b'\n\r')
2949 self.assertEqual(stdout.decode('ascii'), ascii(decoded_value))
2950
2951 # test bytes
2952 key = key.encode("ascii", "surrogateescape")
2953 script = "import os; print(ascii(os.getenvb(%s)))" % repr(key)
2954 env = os.environ.copy()
2955 env[key] = encoded_value
2956 stdout = subprocess.check_output(
2957 [sys.executable, "-c", script],
2958 env=env)
2959 stdout = stdout.rstrip(b'\n\r')
2960 self.assertEqual(stdout.decode('ascii'), ascii(encoded_value))
2961
2962 def test_bytes_program(self):
2963 abs_program = os.fsencode(ZERO_RETURN_CMD[0])
2964 args = list(ZERO_RETURN_CMD[1:])
2965 path, program = os.path.split(ZERO_RETURN_CMD[0])
2966 program = os.fsencode(program)
2967
2968 # absolute bytes path
2969 exitcode = subprocess.call([abs_program]+args)
2970 self.assertEqual(exitcode, 0)
2971
2972 # absolute bytes path as a string
2973 cmd = b"'%s' %s" % (abs_program, " ".join(args).encode("utf-8"))
2974 exitcode = subprocess.call(cmd, shell=True)
2975 self.assertEqual(exitcode, 0)
2976
2977 # bytes program, unicode PATH
2978 env = os.environ.copy()
2979 env["PATH"] = path
2980 exitcode = subprocess.call([program]+args, env=env)
2981 self.assertEqual(exitcode, 0)
2982
2983 # bytes program, bytes PATH
2984 envb = os.environb.copy()
2985 envb[b"PATH"] = os.fsencode(path)
2986 exitcode = subprocess.call([program]+args, env=envb)
2987 self.assertEqual(exitcode, 0)
2988
2989 def test_pipe_cloexec(self):
2990 sleeper = support.findfile("input_reader.py", subdir="subprocessdata")
2991 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2992
2993 p1 = subprocess.Popen([sys.executable, sleeper],
2994 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2995 stderr=subprocess.PIPE, close_fds=False)
2996
2997 self.addCleanup(p1.communicate, b'')
2998
2999 p2 = subprocess.Popen([sys.executable, fd_status],
3000 stdout=subprocess.PIPE, close_fds=False)
3001
3002 output, error = p2.communicate()
3003 result_fds = set(map(int, output.split(b',')))
3004 unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(),
3005 p1.stderr.fileno()])
3006
3007 self.assertFalse(result_fds & unwanted_fds,
3008 "Expected no fds from %r to be open in child, "
3009 "found %r" %
3010 (unwanted_fds, result_fds & unwanted_fds))
3011
3012 def test_pipe_cloexec_real_tools(self):
3013 qcat = support.findfile("qcat.py", subdir="subprocessdata")
3014 qgrep = support.findfile("qgrep.py", subdir="subprocessdata")
3015
3016 subdata = b'zxcvbn'
3017 data = subdata * 4 + b'\n'
3018
3019 p1 = subprocess.Popen([sys.executable, qcat],
3020 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
3021 close_fds=False)
3022
3023 p2 = subprocess.Popen([sys.executable, qgrep, subdata],
3024 stdin=p1.stdout, stdout=subprocess.PIPE,
3025 close_fds=False)
3026
3027 self.addCleanup(p1.wait)
3028 self.addCleanup(p2.wait)
3029 def kill_p1():
3030 try:
3031 p1.terminate()
3032 except ProcessLookupError:
3033 pass
3034 def kill_p2():
3035 try:
3036 p2.terminate()
3037 except ProcessLookupError:
3038 pass
3039 self.addCleanup(kill_p1)
3040 self.addCleanup(kill_p2)
3041
3042 p1.stdin.write(data)
3043 p1.stdin.close()
3044
3045 readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10)
3046
3047 self.assertTrue(readfiles, "The child hung")
3048 self.assertEqual(p2.stdout.read(), data)
3049
3050 p1.stdout.close()
3051 p2.stdout.close()
3052
3053 def test_close_fds(self):
3054 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
3055
3056 fds = os.pipe()
3057 self.addCleanup(os.close, fds[0])
3058 self.addCleanup(os.close, fds[1])
3059
3060 open_fds = set(fds)
3061 # add a bunch more fds
3062 for _ in range(9):
3063 fd = os.open(os.devnull, os.O_RDONLY)
3064 self.addCleanup(os.close, fd)
3065 open_fds.add(fd)
3066
3067 for fd in open_fds:
3068 os.set_inheritable(fd, True)
3069
3070 p = subprocess.Popen([sys.executable, fd_status],
3071 stdout=subprocess.PIPE, close_fds=False)
3072 output, ignored = p.communicate()
3073 remaining_fds = set(map(int, output.split(b',')))
3074
3075 self.assertEqual(remaining_fds & open_fds, open_fds,
3076 "Some fds were closed")
3077
3078 p = subprocess.Popen([sys.executable, fd_status],
3079 stdout=subprocess.PIPE, close_fds=True)
3080 output, ignored = p.communicate()
3081 remaining_fds = set(map(int, output.split(b',')))
3082
3083 self.assertFalse(remaining_fds & open_fds,
3084 "Some fds were left open")
3085 self.assertIn(1, remaining_fds, "Subprocess failed")
3086
3087 # Keep some of the fd's we opened open in the subprocess.
3088 # This tests _posixsubprocess.c's proper handling of fds_to_keep.
3089 fds_to_keep = set(open_fds.pop() for _ in range(8))
3090 p = subprocess.Popen([sys.executable, fd_status],
3091 stdout=subprocess.PIPE, close_fds=True,
3092 pass_fds=fds_to_keep)
3093 output, ignored = p.communicate()
3094 remaining_fds = set(map(int, output.split(b',')))
3095
3096 self.assertFalse((remaining_fds - fds_to_keep) & open_fds,
3097 "Some fds not in pass_fds were left open")
3098 self.assertIn(1, remaining_fds, "Subprocess failed")
3099
3100
3101 @unittest.skipIf(sys.platform.startswith("freebsd") and
3102 os.stat("/dev").st_dev == os.stat("/dev/fd").st_dev,
3103 "Requires fdescfs mounted on /dev/fd on FreeBSD")
3104 def test_close_fds_when_max_fd_is_lowered(self):
3105 """Confirm that issue21618 is fixed (may fail under valgrind)."""
3106 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
3107
3108 # This launches the meat of the test in a child process to
3109 # avoid messing with the larger unittest processes maximum
3110 # number of file descriptors.
3111 # This process launches:
3112 # +--> Process that lowers its RLIMIT_NOFILE aftr setting up
3113 # a bunch of high open fds above the new lower rlimit.
3114 # Those are reported via stdout before launching a new
3115 # process with close_fds=False to run the actual test:
3116 # +--> The TEST: This one launches a fd_status.py
3117 # subprocess with close_fds=True so we can find out if
3118 # any of the fds above the lowered rlimit are still open.
3119 p = subprocess.Popen([sys.executable, '-c', textwrap.dedent(
3120 '''
3121 import os, resource, subprocess, sys, textwrap
3122 open_fds = set()
3123 # Add a bunch more fds to pass down.
3124 for _ in range(40):
3125 fd = os.open(os.devnull, os.O_RDONLY)
3126 open_fds.add(fd)
3127
3128 # Leave a two pairs of low ones available for use by the
3129 # internal child error pipe and the stdout pipe.
3130 # We also leave 10 more open as some Python buildbots run into
3131 # "too many open files" errors during the test if we do not.
3132 for fd in sorted(open_fds)[:14]:
3133 os.close(fd)
3134 open_fds.remove(fd)
3135
3136 for fd in open_fds:
3137 #self.addCleanup(os.close, fd)
3138 os.set_inheritable(fd, True)
3139
3140 max_fd_open = max(open_fds)
3141
3142 # Communicate the open_fds to the parent unittest.TestCase process.
3143 print(','.join(map(str, sorted(open_fds))))
3144 sys.stdout.flush()
3145
3146 rlim_cur, rlim_max = resource.getrlimit(resource.RLIMIT_NOFILE)
3147 try:
3148 # 29 is lower than the highest fds we are leaving open.
3149 resource.setrlimit(resource.RLIMIT_NOFILE, (29, rlim_max))
3150 # Launch a new Python interpreter with our low fd rlim_cur that
3151 # inherits open fds above that limit. It then uses subprocess
3152 # with close_fds=True to get a report of open fds in the child.
3153 # An explicit list of fds to check is passed to fd_status.py as
3154 # letting fd_status rely on its default logic would miss the
3155 # fds above rlim_cur as it normally only checks up to that limit.
3156 subprocess.Popen(
3157 [sys.executable, '-c',
3158 textwrap.dedent("""
3159 import subprocess, sys
3160 subprocess.Popen([sys.executable, %r] +
3161 [str(x) for x in range({max_fd})],
3162 close_fds=True).wait()
3163 """.format(max_fd=max_fd_open+1))],
3164 close_fds=False).wait()
3165 finally:
3166 resource.setrlimit(resource.RLIMIT_NOFILE, (rlim_cur, rlim_max))
3167 ''' % fd_status)], stdout=subprocess.PIPE)
3168
3169 output, unused_stderr = p.communicate()
3170 output_lines = output.splitlines()
3171 self.assertEqual(len(output_lines), 2,
3172 msg="expected exactly two lines of output:\n%r" % output)
3173 opened_fds = set(map(int, output_lines[0].strip().split(b',')))
3174 remaining_fds = set(map(int, output_lines[1].strip().split(b',')))
3175
3176 self.assertFalse(remaining_fds & opened_fds,
3177 msg="Some fds were left open.")
3178
3179
3180 # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
3181 # descriptor of a pipe closed in the parent process is valid in the
3182 # child process according to fstat(), but the mode of the file
3183 # descriptor is invalid, and read or write raise an error.
3184 @support.requires_mac_ver(10, 5)
3185 def test_pass_fds(self):
3186 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
3187
3188 open_fds = set()
3189
3190 for x in range(5):
3191 fds = os.pipe()
3192 self.addCleanup(os.close, fds[0])
3193 self.addCleanup(os.close, fds[1])
3194 os.set_inheritable(fds[0], True)
3195 os.set_inheritable(fds[1], True)
3196 open_fds.update(fds)
3197
3198 for fd in open_fds:
3199 p = subprocess.Popen([sys.executable, fd_status],
3200 stdout=subprocess.PIPE, close_fds=True,
3201 pass_fds=(fd, ))
3202 output, ignored = p.communicate()
3203
3204 remaining_fds = set(map(int, output.split(b',')))
3205 to_be_closed = open_fds - {fd}
3206
3207 self.assertIn(fd, remaining_fds, "fd to be passed not passed")
3208 self.assertFalse(remaining_fds & to_be_closed,
3209 "fd to be closed passed")
3210
3211 # pass_fds overrides close_fds with a warning.
3212 with self.assertWarns(RuntimeWarning) as context:
3213 self.assertFalse(subprocess.call(
3214 ZERO_RETURN_CMD,
3215 close_fds=False, pass_fds=(fd, )))
3216 self.assertIn('overriding close_fds', str(context.warning))
3217
3218 def test_pass_fds_inheritable(self):
3219 script = support.findfile("fd_status.py", subdir="subprocessdata")
3220
3221 inheritable, non_inheritable = os.pipe()
3222 self.addCleanup(os.close, inheritable)
3223 self.addCleanup(os.close, non_inheritable)
3224 os.set_inheritable(inheritable, True)
3225 os.set_inheritable(non_inheritable, False)
3226 pass_fds = (inheritable, non_inheritable)
3227 args = [sys.executable, script]
3228 args += list(map(str, pass_fds))
3229
3230 p = subprocess.Popen(args,
3231 stdout=subprocess.PIPE, close_fds=True,
3232 pass_fds=pass_fds)
3233 output, ignored = p.communicate()
3234 fds = set(map(int, output.split(b',')))
3235
3236 # the inheritable file descriptor must be inherited, so its inheritable
3237 # flag must be set in the child process after fork() and before exec()
3238 self.assertEqual(fds, set(pass_fds), "output=%a" % output)
3239
3240 # inheritable flag must not be changed in the parent process
3241 self.assertEqual(os.get_inheritable(inheritable), True)
3242 self.assertEqual(os.get_inheritable(non_inheritable), False)
3243
3244
3245 # bpo-32270: Ensure that descriptors specified in pass_fds
3246 # are inherited even if they are used in redirections.
3247 # Contributed by @izbyshev.
3248 def test_pass_fds_redirected(self):
3249 """Regression test for https://bugs.python.org/issue32270."""
3250 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
3251 pass_fds = []
3252 for _ in range(2):
3253 fd = os.open(os.devnull, os.O_RDWR)
3254 self.addCleanup(os.close, fd)
3255 pass_fds.append(fd)
3256
3257 stdout_r, stdout_w = os.pipe()
3258 self.addCleanup(os.close, stdout_r)
3259 self.addCleanup(os.close, stdout_w)
3260 pass_fds.insert(1, stdout_w)
3261
3262 with subprocess.Popen([sys.executable, fd_status],
3263 stdin=pass_fds[0],
3264 stdout=pass_fds[1],
3265 stderr=pass_fds[2],
3266 close_fds=True,
3267 pass_fds=pass_fds):
3268 output = os.read(stdout_r, 1024)
3269 fds = {int(num) for num in output.split(b',')}
3270
3271 self.assertEqual(fds, {0, 1, 2} | frozenset(pass_fds), f"output={output!a}")
3272
3273
3274 def test_stdout_stdin_are_single_inout_fd(self):
3275 with io.open(os.devnull, "r+") as inout:
3276 p = subprocess.Popen(ZERO_RETURN_CMD,
3277 stdout=inout, stdin=inout)
3278 p.wait()
3279
3280 def test_stdout_stderr_are_single_inout_fd(self):
3281 with io.open(os.devnull, "r+") as inout:
3282 p = subprocess.Popen(ZERO_RETURN_CMD,
3283 stdout=inout, stderr=inout)
3284 p.wait()
3285
3286 def test_stderr_stdin_are_single_inout_fd(self):
3287 with io.open(os.devnull, "r+") as inout:
3288 p = subprocess.Popen(ZERO_RETURN_CMD,
3289 stderr=inout, stdin=inout)
3290 p.wait()
3291
3292 def test_wait_when_sigchild_ignored(self):
3293 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
3294 sigchild_ignore = support.findfile("sigchild_ignore.py",
3295 subdir="subprocessdata")
3296 p = subprocess.Popen([sys.executable, sigchild_ignore],
3297 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
3298 stdout, stderr = p.communicate()
3299 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
3300 " non-zero with this error:\n%s" %
3301 stderr.decode('utf-8'))
3302
3303 def test_select_unbuffered(self):
3304 # Issue #11459: bufsize=0 should really set the pipes as
3305 # unbuffered (and therefore let select() work properly).
3306 select = import_helper.import_module("select")
3307 p = subprocess.Popen([sys.executable, "-c",
3308 'import sys;'
3309 'sys.stdout.write("apple")'],
3310 stdout=subprocess.PIPE,
3311 bufsize=0)
3312 f = p.stdout
3313 self.addCleanup(f.close)
3314 try:
3315 self.assertEqual(f.read(4), b"appl")
3316 self.assertIn(f, select.select([f], [], [], 0.0)[0])
3317 finally:
3318 p.wait()
3319
3320 def test_zombie_fast_process_del(self):
3321 # Issue #12650: on Unix, if Popen.__del__() was called before the
3322 # process exited, it wouldn't be added to subprocess._active, and would
3323 # remain a zombie.
3324 # spawn a Popen, and delete its reference before it exits
3325 p = subprocess.Popen([sys.executable, "-c",
3326 'import sys, time;'
3327 'time.sleep(0.2)'],
3328 stdout=subprocess.PIPE,
3329 stderr=subprocess.PIPE)
3330 self.addCleanup(p.stdout.close)
3331 self.addCleanup(p.stderr.close)
3332 ident = id(p)
3333 pid = p.pid
3334 with warnings_helper.check_warnings(('', ResourceWarning)):
3335 p = None
3336
3337 if mswindows:
3338 # subprocess._active is not used on Windows and is set to None.
3339 self.assertIsNone(subprocess._active)
3340 else:
3341 # check that p is in the active processes list
3342 self.assertIn(ident, [id(o) for o in subprocess._active])
3343
3344 def test_leak_fast_process_del_killed(self):
3345 # Issue #12650: on Unix, if Popen.__del__() was called before the
3346 # process exited, and the process got killed by a signal, it would never
3347 # be removed from subprocess._active, which triggered a FD and memory
3348 # leak.
3349 # spawn a Popen, delete its reference and kill it
3350 p = subprocess.Popen([sys.executable, "-c",
3351 'import time;'
3352 'time.sleep(3)'],
3353 stdout=subprocess.PIPE,
3354 stderr=subprocess.PIPE)
3355 self.addCleanup(p.stdout.close)
3356 self.addCleanup(p.stderr.close)
3357 ident = id(p)
3358 pid = p.pid
3359 with warnings_helper.check_warnings(('', ResourceWarning)):
3360 p = None
3361 support.gc_collect() # For PyPy or other GCs.
3362
3363 os.kill(pid, signal.SIGKILL)
3364 if mswindows:
3365 # subprocess._active is not used on Windows and is set to None.
3366 self.assertIsNone(subprocess._active)
3367 else:
3368 # check that p is in the active processes list
3369 self.assertIn(ident, [id(o) for o in subprocess._active])
3370
3371 # let some time for the process to exit, and create a new Popen: this
3372 # should trigger the wait() of p
3373 time.sleep(0.2)
3374 with self.assertRaises(OSError):
3375 with subprocess.Popen(NONEXISTING_CMD,
3376 stdout=subprocess.PIPE,
3377 stderr=subprocess.PIPE) as proc:
3378 pass
3379 # p should have been wait()ed on, and removed from the _active list
3380 self.assertRaises(OSError, os.waitpid, pid, 0)
3381 if mswindows:
3382 # subprocess._active is not used on Windows and is set to None.
3383 self.assertIsNone(subprocess._active)
3384 else:
3385 self.assertNotIn(ident, [id(o) for o in subprocess._active])
3386
3387 def test_close_fds_after_preexec(self):
3388 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
3389
3390 # this FD is used as dup2() target by preexec_fn, and should be closed
3391 # in the child process
3392 fd = os.dup(1)
3393 self.addCleanup(os.close, fd)
3394
3395 p = subprocess.Popen([sys.executable, fd_status],
3396 stdout=subprocess.PIPE, close_fds=True,
3397 preexec_fn=lambda: os.dup2(1, fd))
3398 output, ignored = p.communicate()
3399
3400 remaining_fds = set(map(int, output.split(b',')))
3401
3402 self.assertNotIn(fd, remaining_fds)
3403
3404 @support.cpython_only
3405 def test_fork_exec(self):
3406 # Issue #22290: fork_exec() must not crash on memory allocation failure
3407 # or other errors
3408 import _posixsubprocess
3409 gc_enabled = gc.isenabled()
3410 try:
3411 # Use a preexec function and enable the garbage collector
3412 # to force fork_exec() to re-enable the garbage collector
3413 # on error.
3414 func = lambda: None
3415 gc.enable()
3416
3417 for args, exe_list, cwd, env_list in (
3418 (123, [b"exe"], None, [b"env"]),
3419 ([b"arg"], 123, None, [b"env"]),
3420 ([b"arg"], [b"exe"], 123, [b"env"]),
3421 ([b"arg"], [b"exe"], None, 123),
3422 ):
3423 with self.assertRaises(TypeError) as err:
3424 _posixsubprocess.fork_exec(
3425 args, exe_list,
3426 True, (), cwd, env_list,
3427 -1, -1, -1, -1,
3428 1, 2, 3, 4,
3429 True, True, 0,
3430 False, [], 0, -1,
3431 func, False)
3432 # Attempt to prevent
3433 # "TypeError: fork_exec() takes exactly N arguments (M given)"
3434 # from passing the test. More refactoring to have us start
3435 # with a valid *args list, confirm a good call with that works
3436 # before mutating it in various ways to ensure that bad calls
3437 # with individual arg type errors raise a typeerror would be
3438 # ideal. Saving that for a future PR...
3439 self.assertNotIn('takes exactly', str(err.exception))
3440 finally:
3441 if not gc_enabled:
3442 gc.disable()
3443
3444 @support.cpython_only
3445 def test_fork_exec_sorted_fd_sanity_check(self):
3446 # Issue #23564: sanity check the fork_exec() fds_to_keep sanity check.
3447 import _posixsubprocess
3448 class BadInt:
3449 first = True
3450 def __init__(self, value):
3451 self.value = value
3452 def __int__(self):
3453 if self.first:
3454 self.first = False
3455 return self.value
3456 raise ValueError
3457
3458 gc_enabled = gc.isenabled()
3459 try:
3460 gc.enable()
3461
3462 for fds_to_keep in (
3463 (-1, 2, 3, 4, 5), # Negative number.
3464 ('str', 4), # Not an int.
3465 (18, 23, 42, 2**63), # Out of range.
3466 (5, 4), # Not sorted.
3467 (6, 7, 7, 8), # Duplicate.
3468 (BadInt(1), BadInt(2)),
3469 ):
3470 with self.assertRaises(
3471 ValueError,
3472 msg='fds_to_keep={}'.format(fds_to_keep)) as c:
3473 _posixsubprocess.fork_exec(
3474 [b"false"], [b"false"],
3475 True, fds_to_keep, None, [b"env"],
3476 -1, -1, -1, -1,
3477 1, 2, 3, 4,
3478 True, True, 0,
3479 None, None, None, -1,
3480 None)
3481 self.assertIn('fds_to_keep', str(c.exception))
3482 finally:
3483 if not gc_enabled:
3484 gc.disable()
3485
3486 def test_communicate_BrokenPipeError_stdin_close(self):
3487 # By not setting stdout or stderr or a timeout we force the fast path
3488 # that just calls _stdin_write() internally due to our mock.
3489 proc = subprocess.Popen(ZERO_RETURN_CMD)
3490 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
3491 mock_proc_stdin.close.side_effect = BrokenPipeError
3492 proc.communicate() # Should swallow BrokenPipeError from close.
3493 mock_proc_stdin.close.assert_called_with()
3494
3495 def test_communicate_BrokenPipeError_stdin_write(self):
3496 # By not setting stdout or stderr or a timeout we force the fast path
3497 # that just calls _stdin_write() internally due to our mock.
3498 proc = subprocess.Popen(ZERO_RETURN_CMD)
3499 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
3500 mock_proc_stdin.write.side_effect = BrokenPipeError
3501 proc.communicate(b'stuff') # Should swallow the BrokenPipeError.
3502 mock_proc_stdin.write.assert_called_once_with(b'stuff')
3503 mock_proc_stdin.close.assert_called_once_with()
3504
3505 def test_communicate_BrokenPipeError_stdin_flush(self):
3506 # Setting stdin and stdout forces the ._communicate() code path.
3507 # python -h exits faster than python -c pass (but spams stdout).
3508 proc = subprocess.Popen([sys.executable, '-h'],
3509 stdin=subprocess.PIPE,
3510 stdout=subprocess.PIPE)
3511 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin, \
3512 open(os.devnull, 'wb') as dev_null:
3513 mock_proc_stdin.flush.side_effect = BrokenPipeError
3514 # because _communicate registers a selector using proc.stdin...
3515 mock_proc_stdin.fileno.return_value = dev_null.fileno()
3516 # _communicate() should swallow BrokenPipeError from flush.
3517 proc.communicate(b'stuff')
3518 mock_proc_stdin.flush.assert_called_once_with()
3519
3520 def test_communicate_BrokenPipeError_stdin_close_with_timeout(self):
3521 # Setting stdin and stdout forces the ._communicate() code path.
3522 # python -h exits faster than python -c pass (but spams stdout).
3523 proc = subprocess.Popen([sys.executable, '-h'],
3524 stdin=subprocess.PIPE,
3525 stdout=subprocess.PIPE)
3526 with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin:
3527 mock_proc_stdin.close.side_effect = BrokenPipeError
3528 # _communicate() should swallow BrokenPipeError from close.
3529 proc.communicate(timeout=999)
3530 mock_proc_stdin.close.assert_called_once_with()
3531
3532 @unittest.skipUnless(_testcapi is not None
3533 and hasattr(_testcapi, 'W_STOPCODE'),
3534 'need _testcapi.W_STOPCODE')
3535 def test_stopped(self):
3536 """Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
3537 args = ZERO_RETURN_CMD
3538 proc = subprocess.Popen(args)
3539
3540 # Wait until the real process completes to avoid zombie process
3541 support.wait_process(proc.pid, exitcode=0)
3542
3543 status = _testcapi.W_STOPCODE(3)
3544 with mock.patch('subprocess.os.waitpid', return_value=(proc.pid, status)):
3545 returncode = proc.wait()
3546
3547 self.assertEqual(returncode, -3)
3548
3549 def test_send_signal_race(self):
3550 # bpo-38630: send_signal() must poll the process exit status to reduce
3551 # the risk of sending the signal to the wrong process.
3552 proc = subprocess.Popen(ZERO_RETURN_CMD)
3553
3554 # wait until the process completes without using the Popen APIs.
3555 support.wait_process(proc.pid, exitcode=0)
3556
3557 # returncode is still None but the process completed.
3558 self.assertIsNone(proc.returncode)
3559
3560 with mock.patch("os.kill") as mock_kill:
3561 proc.send_signal(signal.SIGTERM)
3562
3563 # send_signal() didn't call os.kill() since the process already
3564 # completed.
3565 mock_kill.assert_not_called()
3566
3567 # Don't check the returncode value: the test reads the exit status,
3568 # so Popen failed to read it and uses a default returncode instead.
3569 self.assertIsNotNone(proc.returncode)
3570
3571 def test_send_signal_race2(self):
3572 # bpo-40550: the process might exist between the returncode check and
3573 # the kill operation
3574 p = subprocess.Popen([sys.executable, '-c', 'exit(1)'])
3575
3576 # wait for process to exit
3577 while not p.returncode:
3578 p.poll()
3579
3580 with mock.patch.object(p, 'poll', new=lambda: None):
3581 p.returncode = None
3582 p.send_signal(signal.SIGTERM)
3583 p.kill()
3584
3585 def test_communicate_repeated_call_after_stdout_close(self):
3586 proc = subprocess.Popen([sys.executable, '-c',
3587 'import os, time; os.close(1), time.sleep(2)'],
3588 stdout=subprocess.PIPE)
3589 while True:
3590 try:
3591 proc.communicate(timeout=0.1)
3592 return
3593 except subprocess.TimeoutExpired:
3594 pass
3595
3596 def test_preexec_at_exit(self):
3597 code = f"""if 1:
3598 import atexit
3599 import subprocess
3600
3601 def dummy():
3602 pass
3603
3604 class AtFinalization:
3605 def __del__(self):
3606 print("OK")
3607 subprocess.Popen({ZERO_RETURN_CMD}, preexec_fn=dummy)
3608 print("shouldn't be printed")
3609 at_finalization = AtFinalization()
3610 """
3611 _, out, err = assert_python_ok("-c", code)
3612 self.assertEqual(out.strip(), b"OK")
3613 self.assertIn(b"preexec_fn not supported at interpreter shutdown", err)
3614
3615 @unittest.skipIf(not sysconfig.get_config_var("HAVE_VFORK"),
3616 "vfork() not enabled by configure.")
3617 @strace_helper.requires_strace()
3618 @mock.patch("subprocess._USE_POSIX_SPAWN", new=False)
3619 def test_vfork_used_when_expected(self):
3620 # This is a performance regression test to ensure we default to using
3621 # vfork() when possible.
3622 # Technically this test could pass when posix_spawn is used as well
3623 # because libc tends to implement that internally using vfork. But
3624 # that'd just be testing a libc+kernel implementation detail.
3625
3626 # Are intersted in the system calls:
3627 # clone,clone2,clone3,fork,vfork,exit,exit_group
3628 # Unfortunately using `--trace` with that list to strace fails because
3629 # not all are supported on all platforms (ex. clone2 is ia64 only...)
3630 # So instead use `%process` which is recommended by strace, and contains
3631 # the above.
3632 true_binary = "/bin/true"
3633 strace_args = ["--trace=%process"]
3634
3635 with self.subTest(name="default_is_vfork"):
3636 vfork_result = strace_helper.strace_python(
3637 f"""\
3638 import subprocess
3639 subprocess.check_call([{true_binary!r}])""",
3640 strace_args
3641 )
3642 # Match both vfork() and clone(..., flags=...|CLONE_VFORK|...)
3643 self.assertRegex(vfork_result.event_bytes, br"(?i)vfork")
3644 # Do NOT check that fork() or other clones did not happen.
3645 # If the OS denys the vfork it'll fallback to plain fork().
3646
3647 # Test that each individual thing that would disable the use of vfork
3648 # actually disables it.
3649 for sub_name, preamble, sp_kwarg, expect_permission_error in (
3650 ("preexec", "", "preexec_fn=lambda: None", False),
3651 ("setgid", "", f"group={os.getgid()}", True),
3652 ("setuid", "", f"user={os.getuid()}", True),
3653 ("setgroups", "", "extra_groups=[]", True),
3654 ):
3655 with self.subTest(name=sub_name):
3656 non_vfork_result = strace_helper.strace_python(
3657 f"""\
3658 import subprocess
3659 {preamble}
3660 try:
3661 subprocess.check_call(
3662 [{true_binary!r}], **dict({sp_kwarg}))
3663 except PermissionError:
3664 if not {expect_permission_error}:
3665 raise""",
3666 strace_args
3667 )
3668 # Ensure neither vfork() or clone(..., flags=...|CLONE_VFORK|...).
3669 self.assertNotRegex(non_vfork_result.event_bytes, br"(?i)vfork")
3670
3671
3672 @unittest.skipUnless(mswindows, "Windows specific tests")
3673 class Win32ProcessTestCase(BaseTestCase):
3674
3675 def test_startupinfo(self):
3676 # startupinfo argument
3677 # We uses hardcoded constants, because we do not want to
3678 # depend on win32all.
3679 STARTF_USESHOWWINDOW = 1
3680 SW_MAXIMIZE = 3
3681 startupinfo = subprocess.STARTUPINFO()
3682 startupinfo.dwFlags = STARTF_USESHOWWINDOW
3683 startupinfo.wShowWindow = SW_MAXIMIZE
3684 # Since Python is a console process, it won't be affected
3685 # by wShowWindow, but the argument should be silently
3686 # ignored
3687 subprocess.call(ZERO_RETURN_CMD,
3688 startupinfo=startupinfo)
3689
3690 def test_startupinfo_keywords(self):
3691 # startupinfo argument
3692 # We use hardcoded constants, because we do not want to
3693 # depend on win32all.
3694 STARTF_USERSHOWWINDOW = 1
3695 SW_MAXIMIZE = 3
3696 startupinfo = subprocess.STARTUPINFO(
3697 dwFlags=STARTF_USERSHOWWINDOW,
3698 wShowWindow=SW_MAXIMIZE
3699 )
3700 # Since Python is a console process, it won't be affected
3701 # by wShowWindow, but the argument should be silently
3702 # ignored
3703 subprocess.call(ZERO_RETURN_CMD,
3704 startupinfo=startupinfo)
3705
3706 def test_startupinfo_copy(self):
3707 # bpo-34044: Popen must not modify input STARTUPINFO structure
3708 startupinfo = subprocess.STARTUPINFO()
3709 startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW
3710 startupinfo.wShowWindow = subprocess.SW_HIDE
3711
3712 # Call Popen() twice with the same startupinfo object to make sure
3713 # that it's not modified
3714 for _ in range(2):
3715 cmd = ZERO_RETURN_CMD
3716 with open(os.devnull, 'w') as null:
3717 proc = subprocess.Popen(cmd,
3718 stdout=null,
3719 stderr=subprocess.STDOUT,
3720 startupinfo=startupinfo)
3721 with proc:
3722 proc.communicate()
3723 self.assertEqual(proc.returncode, 0)
3724
3725 self.assertEqual(startupinfo.dwFlags,
3726 subprocess.STARTF_USESHOWWINDOW)
3727 self.assertIsNone(startupinfo.hStdInput)
3728 self.assertIsNone(startupinfo.hStdOutput)
3729 self.assertIsNone(startupinfo.hStdError)
3730 self.assertEqual(startupinfo.wShowWindow, subprocess.SW_HIDE)
3731 self.assertEqual(startupinfo.lpAttributeList, {"handle_list": []})
3732
3733 def test_creationflags(self):
3734 # creationflags argument
3735 CREATE_NEW_CONSOLE = 16
3736 sys.stderr.write(" a DOS box should flash briefly ...\n")
3737 subprocess.call(sys.executable +
3738 ' -c "import time; time.sleep(0.25)"',
3739 creationflags=CREATE_NEW_CONSOLE)
3740
3741 def test_invalid_args(self):
3742 # invalid arguments should raise ValueError
3743 self.assertRaises(ValueError, subprocess.call,
3744 [sys.executable, "-c",
3745 "import sys; sys.exit(47)"],
3746 preexec_fn=lambda: 1)
3747
3748 @support.cpython_only
3749 def test_issue31471(self):
3750 # There shouldn't be an assertion failure in Popen() in case the env
3751 # argument has a bad keys() method.
3752 class BadEnv(dict):
3753 keys = None
3754 with self.assertRaises(TypeError):
3755 subprocess.Popen(ZERO_RETURN_CMD, env=BadEnv())
3756
3757 def test_close_fds(self):
3758 # close file descriptors
3759 rc = subprocess.call([sys.executable, "-c",
3760 "import sys; sys.exit(47)"],
3761 close_fds=True)
3762 self.assertEqual(rc, 47)
3763
3764 def test_close_fds_with_stdio(self):
3765 import msvcrt
3766
3767 fds = os.pipe()
3768 self.addCleanup(os.close, fds[0])
3769 self.addCleanup(os.close, fds[1])
3770
3771 handles = []
3772 for fd in fds:
3773 os.set_inheritable(fd, True)
3774 handles.append(msvcrt.get_osfhandle(fd))
3775
3776 p = subprocess.Popen([sys.executable, "-c",
3777 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
3778 stdout=subprocess.PIPE, close_fds=False)
3779 stdout, stderr = p.communicate()
3780 self.assertEqual(p.returncode, 0)
3781 int(stdout.strip()) # Check that stdout is an integer
3782
3783 p = subprocess.Popen([sys.executable, "-c",
3784 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
3785 stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
3786 stdout, stderr = p.communicate()
3787 self.assertEqual(p.returncode, 1)
3788 self.assertIn(b"OSError", stderr)
3789
3790 # The same as the previous call, but with an empty handle_list
3791 handle_list = []
3792 startupinfo = subprocess.STARTUPINFO()
3793 startupinfo.lpAttributeList = {"handle_list": handle_list}
3794 p = subprocess.Popen([sys.executable, "-c",
3795 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
3796 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
3797 startupinfo=startupinfo, close_fds=True)
3798 stdout, stderr = p.communicate()
3799 self.assertEqual(p.returncode, 1)
3800 self.assertIn(b"OSError", stderr)
3801
3802 # Check for a warning due to using handle_list and close_fds=False
3803 with warnings_helper.check_warnings((".*overriding close_fds",
3804 RuntimeWarning)):
3805 startupinfo = subprocess.STARTUPINFO()
3806 startupinfo.lpAttributeList = {"handle_list": handles[:]}
3807 p = subprocess.Popen([sys.executable, "-c",
3808 "import msvcrt; print(msvcrt.open_osfhandle({}, 0))".format(handles[0])],
3809 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
3810 startupinfo=startupinfo, close_fds=False)
3811 stdout, stderr = p.communicate()
3812 self.assertEqual(p.returncode, 0)
3813
3814 def test_empty_attribute_list(self):
3815 startupinfo = subprocess.STARTUPINFO()
3816 startupinfo.lpAttributeList = {}
3817 subprocess.call(ZERO_RETURN_CMD,
3818 startupinfo=startupinfo)
3819
3820 def test_empty_handle_list(self):
3821 startupinfo = subprocess.STARTUPINFO()
3822 startupinfo.lpAttributeList = {"handle_list": []}
3823 subprocess.call(ZERO_RETURN_CMD,
3824 startupinfo=startupinfo)
3825
3826 def test_shell_sequence(self):
3827 # Run command through the shell (sequence)
3828 newenv = os.environ.copy()
3829 newenv["FRUIT"] = "physalis"
3830 p = subprocess.Popen(["set"], shell=1,
3831 stdout=subprocess.PIPE,
3832 env=newenv)
3833 with p:
3834 self.assertIn(b"physalis", p.stdout.read())
3835
3836 def test_shell_string(self):
3837 # Run command through the shell (string)
3838 newenv = os.environ.copy()
3839 newenv["FRUIT"] = "physalis"
3840 p = subprocess.Popen("set", shell=1,
3841 stdout=subprocess.PIPE,
3842 env=newenv)
3843 with p:
3844 self.assertIn(b"physalis", p.stdout.read())
3845
3846 def test_shell_encodings(self):
3847 # Run command through the shell (string)
3848 for enc in ['ansi', 'oem']:
3849 newenv = os.environ.copy()
3850 newenv["FRUIT"] = "physalis"
3851 p = subprocess.Popen("set", shell=1,
3852 stdout=subprocess.PIPE,
3853 env=newenv,
3854 encoding=enc)
3855 with p:
3856 self.assertIn("physalis", p.stdout.read(), enc)
3857
3858 def test_call_string(self):
3859 # call() function with string argument on Windows
3860 rc = subprocess.call(sys.executable +
3861 ' -c "import sys; sys.exit(47)"')
3862 self.assertEqual(rc, 47)
3863
3864 def _kill_process(self, method, *args):
3865 # Some win32 buildbot raises EOFError if stdin is inherited
3866 p = subprocess.Popen([sys.executable, "-c", """if 1:
3867 import sys, time
3868 sys.stdout.write('x\\n')
3869 sys.stdout.flush()
3870 time.sleep(30)
3871 """],
3872 stdin=subprocess.PIPE,
3873 stdout=subprocess.PIPE,
3874 stderr=subprocess.PIPE)
3875 with p:
3876 # Wait for the interpreter to be completely initialized before
3877 # sending any signal.
3878 p.stdout.read(1)
3879 getattr(p, method)(*args)
3880 _, stderr = p.communicate()
3881 self.assertEqual(stderr, b'')
3882 returncode = p.wait()
3883 self.assertNotEqual(returncode, 0)
3884
3885 def _kill_dead_process(self, method, *args):
3886 p = subprocess.Popen([sys.executable, "-c", """if 1:
3887 import sys, time
3888 sys.stdout.write('x\\n')
3889 sys.stdout.flush()
3890 sys.exit(42)
3891 """],
3892 stdin=subprocess.PIPE,
3893 stdout=subprocess.PIPE,
3894 stderr=subprocess.PIPE)
3895 with p:
3896 # Wait for the interpreter to be completely initialized before
3897 # sending any signal.
3898 p.stdout.read(1)
3899 # The process should end after this
3900 time.sleep(1)
3901 # This shouldn't raise even though the child is now dead
3902 getattr(p, method)(*args)
3903 _, stderr = p.communicate()
3904 self.assertEqual(stderr, b'')
3905 rc = p.wait()
3906 self.assertEqual(rc, 42)
3907
3908 def test_send_signal(self):
3909 self._kill_process('send_signal', signal.SIGTERM)
3910
3911 def test_kill(self):
3912 self._kill_process('kill')
3913
3914 def test_terminate(self):
3915 self._kill_process('terminate')
3916
3917 def test_send_signal_dead(self):
3918 self._kill_dead_process('send_signal', signal.SIGTERM)
3919
3920 def test_kill_dead(self):
3921 self._kill_dead_process('kill')
3922
3923 def test_terminate_dead(self):
3924 self._kill_dead_process('terminate')
3925
3926 class MiscTests(unittest.TestCase):
3927
3928 class RecordingPopen(subprocess.Popen):
3929 """A Popen that saves a reference to each instance for testing."""
3930 instances_created = []
3931
3932 def __init__(self, *args, **kwargs):
3933 super().__init__(*args, **kwargs)
3934 self.instances_created.append(self)
3935
3936 @mock.patch.object(subprocess.Popen, "_communicate")
3937 def _test_keyboardinterrupt_no_kill(self, popener, mock__communicate,
3938 **kwargs):
3939 """Fake a SIGINT happening during Popen._communicate() and ._wait().
3940
3941 This avoids the need to actually try and get test environments to send
3942 and receive signals reliably across platforms. The net effect of a ^C
3943 happening during a blocking subprocess execution which we want to clean
3944 up from is a KeyboardInterrupt coming out of communicate() or wait().
3945 """
3946
3947 mock__communicate.side_effect = KeyboardInterrupt
3948 try:
3949 with mock.patch.object(subprocess.Popen, "_wait") as mock__wait:
3950 # We patch out _wait() as no signal was involved so the
3951 # child process isn't actually going to exit rapidly.
3952 mock__wait.side_effect = KeyboardInterrupt
3953 with mock.patch.object(subprocess, "Popen",
3954 self.RecordingPopen):
3955 with self.assertRaises(KeyboardInterrupt):
3956 popener([sys.executable, "-c",
3957 "import time\ntime.sleep(9)\nimport sys\n"
3958 "sys.stderr.write('\\n!runaway child!\\n')"],
3959 stdout=subprocess.DEVNULL, **kwargs)
3960 for call in mock__wait.call_args_list[1:]:
3961 self.assertNotEqual(
3962 call, mock.call(timeout=None),
3963 "no open-ended wait() after the first allowed: "
3964 f"{mock__wait.call_args_list}")
3965 sigint_calls = []
3966 for call in mock__wait.call_args_list:
3967 if call == mock.call(timeout=0.25): # from Popen.__init__
3968 sigint_calls.append(call)
3969 self.assertLessEqual(mock__wait.call_count, 2,
3970 msg=mock__wait.call_args_list)
3971 self.assertEqual(len(sigint_calls), 1,
3972 msg=mock__wait.call_args_list)
3973 finally:
3974 # cleanup the forgotten (due to our mocks) child process
3975 process = self.RecordingPopen.instances_created.pop()
3976 process.kill()
3977 process.wait()
3978 self.assertEqual([], self.RecordingPopen.instances_created)
3979
3980 def test_call_keyboardinterrupt_no_kill(self):
3981 self._test_keyboardinterrupt_no_kill(subprocess.call, timeout=6.282)
3982
3983 def test_run_keyboardinterrupt_no_kill(self):
3984 self._test_keyboardinterrupt_no_kill(subprocess.run, timeout=6.282)
3985
3986 def test_context_manager_keyboardinterrupt_no_kill(self):
3987 def popen_via_context_manager(*args, **kwargs):
3988 with subprocess.Popen(*args, **kwargs) as unused_process:
3989 raise KeyboardInterrupt # Test how __exit__ handles ^C.
3990 self._test_keyboardinterrupt_no_kill(popen_via_context_manager)
3991
3992 def test_getoutput(self):
3993 self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
3994 self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
3995 (0, 'xyzzy'))
3996
3997 # we use mkdtemp in the next line to create an empty directory
3998 # under our exclusive control; from that, we can invent a pathname
3999 # that we _know_ won't exist. This is guaranteed to fail.
4000 dir = None
4001 try:
4002 dir = tempfile.mkdtemp()
4003 name = os.path.join(dir, "foo")
4004 status, output = subprocess.getstatusoutput(
4005 ("type " if mswindows else "cat ") + name)
4006 self.assertNotEqual(status, 0)
4007 finally:
4008 if dir is not None:
4009 os.rmdir(dir)
4010
4011 def test__all__(self):
4012 """Ensure that __all__ is populated properly."""
4013 intentionally_excluded = {"list2cmdline", "Handle", "pwd", "grp", "fcntl"}
4014 exported = set(subprocess.__all__)
4015 possible_exports = set()
4016 import types
4017 for name, value in subprocess.__dict__.items():
4018 if name.startswith('_'):
4019 continue
4020 if isinstance(value, (types.ModuleType,)):
4021 continue
4022 possible_exports.add(name)
4023 self.assertEqual(exported, possible_exports - intentionally_excluded)
4024
4025
4026 @unittest.skipUnless(hasattr(selectors, 'PollSelector'),
4027 "Test needs selectors.PollSelector")
4028 class ProcessTestCaseNoPoll(ProcessTestCase):
4029 def setUp(self):
4030 self.orig_selector = subprocess._PopenSelector
4031 subprocess._PopenSelector = selectors.SelectSelector
4032 ProcessTestCase.setUp(self)
4033
4034 def tearDown(self):
4035 subprocess._PopenSelector = self.orig_selector
4036 ProcessTestCase.tearDown(self)
4037
4038
4039 @unittest.skipUnless(mswindows, "Windows-specific tests")
4040 class CommandsWithSpaces (BaseTestCase):
4041
4042 def setUp(self):
4043 super().setUp()
4044 f, fname = tempfile.mkstemp(".py", "te st")
4045 self.fname = fname.lower ()
4046 os.write(f, b"import sys;"
4047 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
4048 )
4049 os.close(f)
4050
4051 def tearDown(self):
4052 os.remove(self.fname)
4053 super().tearDown()
4054
4055 def with_spaces(self, *args, **kwargs):
4056 kwargs['stdout'] = subprocess.PIPE
4057 p = subprocess.Popen(*args, **kwargs)
4058 with p:
4059 self.assertEqual(
4060 p.stdout.read ().decode("mbcs"),
4061 "2 [%r, 'ab cd']" % self.fname
4062 )
4063
4064 def test_shell_string_with_spaces(self):
4065 # call() function with string argument with spaces on Windows
4066 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
4067 "ab cd"), shell=1)
4068
4069 def test_shell_sequence_with_spaces(self):
4070 # call() function with sequence argument with spaces on Windows
4071 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
4072
4073 def test_noshell_string_with_spaces(self):
4074 # call() function with string argument with spaces on Windows
4075 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
4076 "ab cd"))
4077
4078 def test_noshell_sequence_with_spaces(self):
4079 # call() function with sequence argument with spaces on Windows
4080 self.with_spaces([sys.executable, self.fname, "ab cd"])
4081
4082
4083 class ContextManagerTests(BaseTestCase):
4084
4085 def test_pipe(self):
4086 with subprocess.Popen([sys.executable, "-c",
4087 "import sys;"
4088 "sys.stdout.write('stdout');"
4089 "sys.stderr.write('stderr');"],
4090 stdout=subprocess.PIPE,
4091 stderr=subprocess.PIPE) as proc:
4092 self.assertEqual(proc.stdout.read(), b"stdout")
4093 self.assertEqual(proc.stderr.read(), b"stderr")
4094
4095 self.assertTrue(proc.stdout.closed)
4096 self.assertTrue(proc.stderr.closed)
4097
4098 def test_returncode(self):
4099 with subprocess.Popen([sys.executable, "-c",
4100 "import sys; sys.exit(100)"]) as proc:
4101 pass
4102 # __exit__ calls wait(), so the returncode should be set
4103 self.assertEqual(proc.returncode, 100)
4104
4105 def test_communicate_stdin(self):
4106 with subprocess.Popen([sys.executable, "-c",
4107 "import sys;"
4108 "sys.exit(sys.stdin.read() == 'context')"],
4109 stdin=subprocess.PIPE) as proc:
4110 proc.communicate(b"context")
4111 self.assertEqual(proc.returncode, 1)
4112
4113 def test_invalid_args(self):
4114 with self.assertRaises(NONEXISTING_ERRORS):
4115 with subprocess.Popen(NONEXISTING_CMD,
4116 stdout=subprocess.PIPE,
4117 stderr=subprocess.PIPE) as proc:
4118 pass
4119
4120 def test_broken_pipe_cleanup(self):
4121 """Broken pipe error should not prevent wait() (Issue 21619)"""
4122 proc = subprocess.Popen(ZERO_RETURN_CMD,
4123 stdin=subprocess.PIPE,
4124 bufsize=support.PIPE_MAX_SIZE*2)
4125 proc = proc.__enter__()
4126 # Prepare to send enough data to overflow any OS pipe buffering and
4127 # guarantee a broken pipe error. Data is held in BufferedWriter
4128 # buffer until closed.
4129 proc.stdin.write(b'x' * support.PIPE_MAX_SIZE)
4130 self.assertIsNone(proc.returncode)
4131 # EPIPE expected under POSIX; EINVAL under Windows
4132 self.assertRaises(OSError, proc.__exit__, None, None, None)
4133 self.assertEqual(proc.returncode, 0)
4134 self.assertTrue(proc.stdin.closed)
4135
4136
4137 if __name__ == "__main__":
4138 unittest.main()