annotate Lib/test/test_subprocess.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 786a106fc6b7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1 import unittest
79314
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
2 from test import script_helper
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46710
diff changeset
3 from test import support
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
4 import subprocess
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
5 import sys
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
6 import signal
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
7 import io
78655
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
8 import locale
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
9 import os
59209
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
10 import errno
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
11 import tempfile
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
12 import time
33739
76355363101b New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents: 33738
diff changeset
13 import re
87008
71b618f0c8e9 Issue #18923: Update subprocess to use the new selectors module.
Charles-François Natali <cf.natali@gmail.com>
parents: 86880
diff changeset
14 import selectors
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
15 import sysconfig
66676
b37da60c9072 issue7213 + issue2320: Cause a DeprecationWarning if the close_fds argument is
Gregory P. Smith <greg@mad-scientist.com>
parents: 66622
diff changeset
16 import warnings
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
17 import select
68491
141bbcb19c06 Fix the @test_NNNN_tmp file terds being left in whatever your $PWD was
Gregory P. Smith <greg@krypto.org>
parents: 68432
diff changeset
18 import shutil
73925
6040c248a199 we're always going to have gc
Benjamin Peterson <benjamin@python.org>
parents: 73923
diff changeset
19 import gc
78652
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
20 import textwrap
73923
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
21
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
22 try:
85466
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
23 import threading
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
24 except ImportError:
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
25 threading = None
73923
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
26
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
27 mswindows = (sys.platform == "win32")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
28
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
29 #
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
30 # Depends on the following external programs: Python
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
31 #
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
32
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
33 if mswindows:
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
34 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
35 'os.O_BINARY);')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
36 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
37 SETBINARY = ''
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
38
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
39
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
40 try:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
41 mkstemp = tempfile.mkstemp
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
42 except AttributeError:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
43 # tempfile.mkstemp is not available
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
44 def mkstemp():
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
45 """Replacement for mkstemp, calling mktemp."""
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
46 fname = tempfile.mktemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
47 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
48
33739
76355363101b New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents: 33738
diff changeset
49
63930
8499ae5d9719 #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63843
diff changeset
50 class BaseTestCase(unittest.TestCase):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
51 def setUp(self):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
52 # Try to minimize the number of children we have so this test
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
53 # doesn't crash on some buildbots (Alphas in particular).
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
54 support.reap_children()
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
55
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
56 def tearDown(self):
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
57 for inst in subprocess._active:
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
58 inst.wait()
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
59 subprocess._cleanup()
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
60 self.assertFalse(subprocess._active, "subprocess._active not empty")
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
61
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
62 def assertStderrEqual(self, stderr, expected, msg=None):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
63 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
64 # shutdown time. That frustrates tests trying to check stderr produced
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
65 # from a spawned Python process.
63727
5931112eabe7 Factor out stripping of interpreter debug output in test.support.strip_python_stderr()
Antoine Pitrou <solipsis@pitrou.net>
parents: 63141
diff changeset
66 actual = support.strip_python_stderr(stderr)
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
67 # strip_python_stderr also strips whitespace, so we do too.
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
68 expected = expected.strip()
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
69 self.assertEqual(actual, expected, msg)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
70
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
71
80368
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
72 class PopenTestException(Exception):
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
73 pass
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
74
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
75
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
76 class PopenExecuteChildRaises(subprocess.Popen):
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
77 """Popen subclass for testing cleanup of subprocess.PIPE filehandles when
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
78 _execute_child fails.
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
79 """
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
80 def _execute_child(self, *args, **kwargs):
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
81 raise PopenTestException("Forced Exception for Test")
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
82
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
83
63930
8499ae5d9719 #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63843
diff changeset
84 class ProcessTestCase(BaseTestCase):
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
85
82916
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
86 def test_io_buffered_by_default(self):
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
87 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
88 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
89 stderr=subprocess.PIPE)
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
90 try:
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
91 self.assertIsInstance(p.stdin, io.BufferedIOBase)
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
92 self.assertIsInstance(p.stdout, io.BufferedIOBase)
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
93 self.assertIsInstance(p.stderr, io.BufferedIOBase)
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
94 finally:
82917
4c2fc172afcc Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82596 82916
diff changeset
95 p.stdin.close()
4c2fc172afcc Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82596 82916
diff changeset
96 p.stdout.close()
4c2fc172afcc Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82596 82916
diff changeset
97 p.stderr.close()
82916
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
98 p.wait()
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
99
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
100 def test_io_unbuffered_works(self):
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
101 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
102 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
103 stderr=subprocess.PIPE, bufsize=0)
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
104 try:
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
105 self.assertIsInstance(p.stdin, io.RawIOBase)
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
106 self.assertIsInstance(p.stdout, io.RawIOBase)
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
107 self.assertIsInstance(p.stderr, io.RawIOBase)
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
108 finally:
82917
4c2fc172afcc Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82596 82916
diff changeset
109 p.stdin.close()
4c2fc172afcc Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82596 82916
diff changeset
110 p.stdout.close()
4c2fc172afcc Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82596 82916
diff changeset
111 p.stderr.close()
82916
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
112 p.wait()
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
113
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
114 def test_call_seq(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
115 # call() function with sequence argument
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
116 rc = subprocess.call([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
117 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
118 self.assertEqual(rc, 47)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
119
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
120 def test_call_timeout(self):
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
121 # call() function with timeout argument; we want to test that the child
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
122 # process gets killed when the timeout expires. If the child isn't
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
123 # killed, this call will deadlock since subprocess.call waits for the
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
124 # child.
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
125 self.assertRaises(subprocess.TimeoutExpired, subprocess.call,
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
126 [sys.executable, "-c", "while True: pass"],
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
127 timeout=0.1)
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
128
34302
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
129 def test_check_call_zero(self):
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
130 # check_call() function with zero return code
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
131 rc = subprocess.check_call([sys.executable, "-c",
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
132 "import sys; sys.exit(0)"])
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
133 self.assertEqual(rc, 0)
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
134
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
135 def test_check_call_nonzero(self):
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
136 # check_call() function with non-zero return code
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
137 with self.assertRaises(subprocess.CalledProcessError) as c:
34302
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
138 subprocess.check_call([sys.executable, "-c",
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
139 "import sys; sys.exit(47)"])
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
140 self.assertEqual(c.exception.returncode, 47)
34302
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
141
50074
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
142 def test_check_output(self):
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
143 # check_output() function with zero return code
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
144 output = subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
145 [sys.executable, "-c", "print('BDFL')"])
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55626
diff changeset
146 self.assertIn(b'BDFL', output)
50074
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
147
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
148 def test_check_output_nonzero(self):
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
149 # check_call() function with non-zero return code
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
150 with self.assertRaises(subprocess.CalledProcessError) as c:
50074
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
151 subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
152 [sys.executable, "-c", "import sys; sys.exit(5)"])
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
153 self.assertEqual(c.exception.returncode, 5)
50074
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
154
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
155 def test_check_output_stderr(self):
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
156 # check_output() function stderr redirected to stdout
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
157 output = subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
158 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
159 stderr=subprocess.STDOUT)
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55626
diff changeset
160 self.assertIn(b'BDFL', output)
50074
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
161
83495
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
162 def test_check_output_stdin_arg(self):
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
163 # check_output() can be called with stdin set to a file
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
164 tf = tempfile.TemporaryFile()
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
165 self.addCleanup(tf.close)
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
166 tf.write(b'pear')
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
167 tf.seek(0)
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
168 output = subprocess.check_output(
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
169 [sys.executable, "-c",
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
170 "import sys; sys.stdout.write(sys.stdin.read().upper())"],
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
171 stdin=tf)
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
172 self.assertIn(b'PEAR', output)
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
173
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
174 def test_check_output_input_arg(self):
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
175 # check_output() can be called with input set to a string
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
176 output = subprocess.check_output(
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
177 [sys.executable, "-c",
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
178 "import sys; sys.stdout.write(sys.stdin.read().upper())"],
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
179 input=b'pear')
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
180 self.assertIn(b'PEAR', output)
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
181
50074
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
182 def test_check_output_stdout_arg(self):
83495
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
183 # check_output() refuses to accept 'stdout' argument
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
184 with self.assertRaises(ValueError) as c:
50074
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
185 output = subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
186 [sys.executable, "-c", "print('will not be run')"],
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
187 stdout=sys.stdout)
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
188 self.fail("Expected ValueError when stdout arg supplied.")
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
189 self.assertIn('stdout', c.exception.args[0])
50074
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
190
83495
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
191 def test_check_output_stdin_with_input_arg(self):
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
192 # check_output() refuses to accept 'stdin' with 'input'
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
193 tf = tempfile.TemporaryFile()
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
194 self.addCleanup(tf.close)
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
195 tf.write(b'pear')
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
196 tf.seek(0)
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
197 with self.assertRaises(ValueError) as c:
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
198 output = subprocess.check_output(
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
199 [sys.executable, "-c", "print('will not be run')"],
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
200 stdin=tf, input=b'hare')
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
201 self.fail("Expected ValueError when stdin and input args supplied.")
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
202 self.assertIn('stdin', c.exception.args[0])
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
203 self.assertIn('input', c.exception.args[0])
5c45d0ca984f Issue #16624: `subprocess.check_output` now accepts an `input` argument,
Serhiy Storchaka <storchaka@gmail.com>
parents: 82918
diff changeset
204
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
205 def test_check_output_timeout(self):
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
206 # check_output() function with timeout arg
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
207 with self.assertRaises(subprocess.TimeoutExpired) as c:
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
208 output = subprocess.check_output(
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
209 [sys.executable, "-c",
70693
509622ce587a Use a sleep for test_subprocess timeout test
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70576
diff changeset
210 "import sys, time\n"
509622ce587a Use a sleep for test_subprocess timeout test
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70576
diff changeset
211 "sys.stdout.write('BDFL')\n"
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
212 "sys.stdout.flush()\n"
70693
509622ce587a Use a sleep for test_subprocess timeout test
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70576
diff changeset
213 "time.sleep(3600)"],
68619
2e4879d44604 Bump up the subprocess timeouts even more. :(
Reid Kleckner <reid@kleckner.net>
parents: 68618
diff changeset
214 # Some heavily loaded buildbots (sparc Debian 3.x) require
2e4879d44604 Bump up the subprocess timeouts even more. :(
Reid Kleckner <reid@kleckner.net>
parents: 68618
diff changeset
215 # this much time to start and print.
2e4879d44604 Bump up the subprocess timeouts even more. :(
Reid Kleckner <reid@kleckner.net>
parents: 68618
diff changeset
216 timeout=3)
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
217 self.fail("Expected TimeoutExpired.")
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
218 self.assertEqual(c.exception.output, b'BDFL')
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
219
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
220 def test_call_kwargs(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
221 # call() function with keyword args
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
222 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
223 newenv["FRUIT"] = "banana"
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
224 rc = subprocess.call([sys.executable, "-c",
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
225 'import sys, os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
226 'sys.exit(os.getenv("FRUIT")=="banana")'],
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
227 env=newenv)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
228 self.assertEqual(rc, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
229
70554
0f2714e49583 Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70465
diff changeset
230 def test_invalid_args(self):
0f2714e49583 Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70465
diff changeset
231 # Popen() called with invalid arguments should raise TypeError
0f2714e49583 Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70465
diff changeset
232 # but Popen.__del__ should not complain (issue #12085)
0f2714e49583 Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70465
diff changeset
233 with support.captured_stderr() as s:
0f2714e49583 Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70465
diff changeset
234 self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
0f2714e49583 Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70465
diff changeset
235 argcount = subprocess.Popen.__init__.__code__.co_argcount
0f2714e49583 Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70465
diff changeset
236 too_many_args = [0] * (argcount + 1)
0f2714e49583 Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70465
diff changeset
237 self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
0f2714e49583 Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70465
diff changeset
238 self.assertEqual(s.getvalue(), '')
0f2714e49583 Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70465
diff changeset
239
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
240 def test_stdin_none(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
241 # .stdin is None when not redirected
40788
e9a6234b26af * Remove PRINT_ITEM(_TO), PRINT_NEWLINE(_TO) opcodes.
Georg Brandl <georg@python.org>
parents: 40643
diff changeset
242 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
243 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
244 self.addCleanup(p.stdout.close)
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
245 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
246 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
247 self.assertEqual(p.stdin, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
248
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
249 def test_stdout_none(self):
82595
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
250 # .stdout is None when not redirected, and the child's stdout will
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
251 # be inherited from the parent. In order to test this we run a
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
252 # subprocess in a subprocess:
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
253 # this_test
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
254 # \-- subprocess created by this test (parent)
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
255 # \-- subprocess created by the parent subprocess (child)
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
256 # The parent doesn't specify stdout, so the child will use the
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
257 # parent's stdout. This test checks that the message printed by the
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
258 # child goes to the parent stdout. The parent also checks that the
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
259 # child's stdout is None. See #11963.
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
260 code = ('import sys; from subprocess import Popen, PIPE;'
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
261 'p = Popen([sys.executable, "-c", "print(\'test_stdout_none\')"],'
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
262 ' stdin=PIPE, stderr=PIPE);'
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
263 'p.wait(); assert p.stdout is None;')
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
264 p = subprocess.Popen([sys.executable, "-c", code],
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
265 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
266 self.addCleanup(p.stdout.close)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
267 self.addCleanup(p.stderr.close)
82595
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
268 out, err = p.communicate()
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
269 self.assertEqual(p.returncode, 0, err)
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
270 self.assertEqual(out.rstrip(), b'test_stdout_none')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
271
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
272 def test_stderr_none(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
273 # .stderr is None when not redirected
40788
e9a6234b26af * Remove PRINT_ITEM(_TO), PRINT_NEWLINE(_TO) opcodes.
Georg Brandl <georg@python.org>
parents: 40643
diff changeset
274 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
275 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
276 self.addCleanup(p.stdout.close)
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
277 self.addCleanup(p.stdin.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
278 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
279 self.assertEqual(p.stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
280
79598
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
281 def _assert_python(self, pre_args, **kwargs):
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
282 # We include sys.exit() to prevent the test runner from hanging
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
283 # whenever python is found.
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
284 args = pre_args + ["import sys; sys.exit(47)"]
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
285 p = subprocess.Popen(args, **kwargs)
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
286 p.wait()
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
287 self.assertEqual(47, p.returncode)
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
288
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
289 def test_executable(self):
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
290 # Check that the executable argument works.
79630
9451908da615 Issue #16170: Remove Linux skip from test_subprocess's test_executable test.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79600
diff changeset
291 #
9451908da615 Issue #16170: Remove Linux skip from test_subprocess's test_executable test.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79600
diff changeset
292 # On Unix (non-Mac and non-Windows), Python looks at args[0] to
9451908da615 Issue #16170: Remove Linux skip from test_subprocess's test_executable test.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79600
diff changeset
293 # determine where its standard library is, so we need the directory
9451908da615 Issue #16170: Remove Linux skip from test_subprocess's test_executable test.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79600
diff changeset
294 # of args[0] to be valid for the Popen() call to Python to succeed.
9451908da615 Issue #16170: Remove Linux skip from test_subprocess's test_executable test.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79600
diff changeset
295 # See also issue #16170 and issue #7774.
9451908da615 Issue #16170: Remove Linux skip from test_subprocess's test_executable test.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79600
diff changeset
296 doesnotexist = os.path.join(os.path.dirname(sys.executable),
9451908da615 Issue #16170: Remove Linux skip from test_subprocess's test_executable test.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79600
diff changeset
297 "doesnotexist")
9451908da615 Issue #16170: Remove Linux skip from test_subprocess's test_executable test.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79600
diff changeset
298 self._assert_python([doesnotexist, "-c"], executable=sys.executable)
79598
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
299
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
300 def test_executable_takes_precedence(self):
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
301 # Check that the executable argument takes precedence over args[0].
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
302 #
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
303 # Verify first that the call succeeds without the executable arg.
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
304 pre_args = [sys.executable, "-c"]
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
305 self._assert_python(pre_args)
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
306 self.assertRaises(FileNotFoundError, self._assert_python, pre_args,
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
307 executable="doesnotexist")
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
308
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
309 @unittest.skipIf(mswindows, "executable argument replaces shell")
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
310 def test_executable_replaces_shell(self):
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
311 # Check that the executable argument replaces the default shell
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
312 # when shell=True.
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
313 self._assert_python([], executable=sys.executable, shell=True)
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
314
79314
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
315 # For use in the test_cwd* tests below.
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
316 def _normalize_cwd(self, cwd):
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
317 # Normalize an expected cwd (for Tru64 support).
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
318 # We can't use os.path.realpath since it doesn't expand Tru64 {memb}
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
319 # strings. See bug #1063571.
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
320 original_cwd = os.getcwd()
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
321 os.chdir(cwd)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
322 cwd = os.getcwd()
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
323 os.chdir(original_cwd)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
324 return cwd
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
325
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
326 # For use in the test_cwd* tests below.
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
327 def _split_python_path(self):
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
328 # Return normalized (python_dir, python_base).
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
329 python_path = os.path.realpath(sys.executable)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
330 return os.path.split(python_path)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
331
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
332 # For use in the test_cwd* tests below.
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
333 def _assert_cwd(self, expected_cwd, python_arg, **kwargs):
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
334 # Invoke Python via Popen, and assert that (1) the call succeeds,
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
335 # and that (2) the current working directory of the child process
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
336 # matches *expected_cwd*.
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
337 p = subprocess.Popen([python_arg, "-c",
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
338 "import os, sys; "
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
339 "sys.stdout.write(os.getcwd()); "
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
340 "sys.exit(47)"],
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
341 stdout=subprocess.PIPE,
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
342 **kwargs)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
343 self.addCleanup(p.stdout.close)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
344 p.wait()
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
345 self.assertEqual(47, p.returncode)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
346 normcase = os.path.normcase
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
347 self.assertEqual(normcase(expected_cwd),
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
348 normcase(p.stdout.read().decode("utf-8")))
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
349
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
350 def test_cwd(self):
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
351 # Check that cwd changes the cwd for the child process.
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
352 temp_dir = tempfile.gettempdir()
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
353 temp_dir = self._normalize_cwd(temp_dir)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
354 self._assert_cwd(temp_dir, sys.executable, cwd=temp_dir)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
355
79334
d8d52b5b4bc2 Issue #15533: Skip test_cwd_with_relative_*() tests on Windows pending resolution of issue.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79314
diff changeset
356 @unittest.skipIf(mswindows, "pending resolution of issue #15533")
79314
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
357 def test_cwd_with_relative_arg(self):
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
358 # Check that Popen looks for args[0] relative to cwd if args[0]
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
359 # is relative.
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
360 python_dir, python_base = self._split_python_path()
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
361 rel_python = os.path.join(os.curdir, python_base)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
362 with support.temp_cwd() as wrong_dir:
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
363 # Before calling with the correct cwd, confirm that the call fails
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
364 # without cwd and with the wrong cwd.
79315
f66ff96f0030 Issue #15533: Merge fix from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78656 79314
diff changeset
365 self.assertRaises(FileNotFoundError, subprocess.Popen,
79314
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
366 [rel_python])
79315
f66ff96f0030 Issue #15533: Merge fix from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78656 79314
diff changeset
367 self.assertRaises(FileNotFoundError, subprocess.Popen,
79314
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
368 [rel_python], cwd=wrong_dir)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
369 python_dir = self._normalize_cwd(python_dir)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
370 self._assert_cwd(python_dir, rel_python, cwd=python_dir)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
371
79334
d8d52b5b4bc2 Issue #15533: Skip test_cwd_with_relative_*() tests on Windows pending resolution of issue.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79314
diff changeset
372 @unittest.skipIf(mswindows, "pending resolution of issue #15533")
79314
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
373 def test_cwd_with_relative_executable(self):
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
374 # Check that Popen looks for executable relative to cwd if executable
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
375 # is relative (and that executable takes precedence over args[0]).
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
376 python_dir, python_base = self._split_python_path()
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
377 rel_python = os.path.join(os.curdir, python_base)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
378 doesntexist = "somethingyoudonthave"
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
379 with support.temp_cwd() as wrong_dir:
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
380 # Before calling with the correct cwd, confirm that the call fails
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
381 # without cwd and with the wrong cwd.
79315
f66ff96f0030 Issue #15533: Merge fix from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78656 79314
diff changeset
382 self.assertRaises(FileNotFoundError, subprocess.Popen,
79314
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
383 [doesntexist], executable=rel_python)
79315
f66ff96f0030 Issue #15533: Merge fix from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78656 79314
diff changeset
384 self.assertRaises(FileNotFoundError, subprocess.Popen,
79314
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
385 [doesntexist], executable=rel_python,
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
386 cwd=wrong_dir)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
387 python_dir = self._normalize_cwd(python_dir)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
388 self._assert_cwd(python_dir, doesntexist, executable=rel_python,
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
389 cwd=python_dir)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
390
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
391 def test_cwd_with_absolute_arg(self):
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
392 # Check that Popen can find the executable when the cwd is wrong
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
393 # if args[0] is an absolute path.
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
394 python_dir, python_base = self._split_python_path()
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
395 abs_python = os.path.join(python_dir, python_base)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
396 rel_python = os.path.join(os.curdir, python_base)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
397 with script_helper.temp_dir() as wrong_dir:
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
398 # Before calling with an absolute path, confirm that using a
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
399 # relative path fails.
79315
f66ff96f0030 Issue #15533: Merge fix from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78656 79314
diff changeset
400 self.assertRaises(FileNotFoundError, subprocess.Popen,
79314
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
401 [rel_python], cwd=wrong_dir)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
402 wrong_dir = self._normalize_cwd(wrong_dir)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
403 self._assert_cwd(wrong_dir, abs_python, cwd=wrong_dir)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
404
77154
294f8aeb4d4b Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75525
diff changeset
405 @unittest.skipIf(sys.base_prefix != sys.prefix,
294f8aeb4d4b Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75525
diff changeset
406 'Test is not venv-compatible')
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
407 def test_executable_with_cwd(self):
79314
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
408 python_dir, python_base = self._split_python_path()
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
409 python_dir = self._normalize_cwd(python_dir)
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
410 self._assert_cwd(python_dir, "somethingyoudonthave",
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
411 executable=sys.executable, cwd=python_dir)
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
412
77154
294f8aeb4d4b Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75525
diff changeset
413 @unittest.skipIf(sys.base_prefix != sys.prefix,
294f8aeb4d4b Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75525
diff changeset
414 'Test is not venv-compatible')
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
415 @unittest.skipIf(sysconfig.is_python_build(),
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
416 "need an installed Python. See #7774")
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
417 def test_executable_without_cwd(self):
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
418 # For a normal installation, it should work without 'cwd'
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
419 # argument. For test runs in the build directory, see #7774.
84980
f37b336bfbca Issue #17046: Fix test_subprocess test_executable_without_cwd broken test case.
Ned Deily <nad@acm.org>
parents: 84081
diff changeset
420 self._assert_cwd(os.getcwd(), "somethingyoudonthave",
f37b336bfbca Issue #17046: Fix test_subprocess test_executable_without_cwd broken test case.
Ned Deily <nad@acm.org>
parents: 84081
diff changeset
421 executable=sys.executable)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
422
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
423 def test_stdin_pipe(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
424 # stdin redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
425 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
426 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
427 stdin=subprocess.PIPE)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
428 p.stdin.write(b"pear")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
429 p.stdin.close()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
430 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
431 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
432
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
433 def test_stdin_filedes(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
434 # stdin is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
435 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
436 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
437 d = tf.fileno()
49200
bdb660dca74d Issue #3782: os.write() must not accept unicode strings
Antoine Pitrou <solipsis@pitrou.net>
parents: 48395
diff changeset
438 os.write(d, b"pear")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
439 os.lseek(d, 0, 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
440 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
441 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
442 stdin=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
443 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
444 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
445
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
446 def test_stdin_fileobj(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
447 # stdin is set to open file object
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
448 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
449 self.addCleanup(tf.close)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
450 tf.write(b"pear")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
451 tf.seek(0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
452 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
453 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
454 stdin=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
455 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
456 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
457
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
458 def test_stdout_pipe(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
459 # stdout redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
460 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
461 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
462 stdout=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
463 self.addCleanup(p.stdout.close)
41845
9ce8fa0a0899 Make test_subprocess work. Fix universal newlines in io.py.
Guido van Rossum <guido@python.org>
parents: 41732
diff changeset
464 self.assertEqual(p.stdout.read(), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
465
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
466 def test_stdout_filedes(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
467 # stdout is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
468 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
469 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
470 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
471 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
472 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
473 stdout=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
474 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
475 os.lseek(d, 0, 0)
41732
bbb099d4ec80 Make test_subprocess pass. The subprocess module now reads and writes
Guido van Rossum <guido@python.org>
parents: 40788
diff changeset
476 self.assertEqual(os.read(d, 1024), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
477
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
478 def test_stdout_fileobj(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
479 # stdout is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
480 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
481 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
482 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
483 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
484 stdout=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
485 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
486 tf.seek(0)
41845
9ce8fa0a0899 Make test_subprocess work. Fix universal newlines in io.py.
Guido van Rossum <guido@python.org>
parents: 41732
diff changeset
487 self.assertEqual(tf.read(), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
488
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
489 def test_stderr_pipe(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
490 # stderr redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
491 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
492 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
493 stderr=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
494 self.addCleanup(p.stderr.close)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
495 self.assertStderrEqual(p.stderr.read(), b"strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
496
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
497 def test_stderr_filedes(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
498 # stderr is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
499 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
500 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
501 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
502 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
503 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
504 stderr=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
505 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
506 os.lseek(d, 0, 0)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
507 self.assertStderrEqual(os.read(d, 1024), b"strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
508
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
509 def test_stderr_fileobj(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
510 # stderr is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
511 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
512 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
513 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
514 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
515 stderr=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
516 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
517 tf.seek(0)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
518 self.assertStderrEqual(tf.read(), b"strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
519
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
520 def test_stdout_stderr_pipe(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
521 # capture stdout and stderr to the same pipe
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
522 p = subprocess.Popen([sys.executable, "-c",
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
523 'import sys;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
524 'sys.stdout.write("apple");'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
525 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
526 'sys.stderr.write("orange")'],
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
527 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
528 stderr=subprocess.STDOUT)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
529 self.addCleanup(p.stdout.close)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
530 self.assertStderrEqual(p.stdout.read(), b"appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
531
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
532 def test_stdout_stderr_file(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
533 # capture stdout and stderr to the same open file
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
534 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
535 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
536 p = subprocess.Popen([sys.executable, "-c",
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
537 'import sys;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
538 'sys.stdout.write("apple");'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
539 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
540 'sys.stderr.write("orange")'],
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
541 stdout=tf,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
542 stderr=tf)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
543 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
544 tf.seek(0)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
545 self.assertStderrEqual(tf.read(), b"appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
546
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
547 def test_stdout_filedes_of_stdout(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
548 # stdout is set to 1 (#1531862).
82595
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
549 # To avoid printing the text on stdout, we do something similar to
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
550 # test_stdout_none (see above). The parent subprocess calls the child
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
551 # subprocess passing stdout=1, and this test uses stdout=PIPE in
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
552 # order to capture and check the output of the parent. See #11963.
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
553 code = ('import sys, subprocess; '
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
554 'rc = subprocess.call([sys.executable, "-c", '
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
555 ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), '
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
556 'b\'test with stdout=1\'))"], stdout=1); '
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
557 'assert rc == 18')
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
558 p = subprocess.Popen([sys.executable, "-c", code],
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
559 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
560 self.addCleanup(p.stdout.close)
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
561 self.addCleanup(p.stderr.close)
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
562 out, err = p.communicate()
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
563 self.assertEqual(p.returncode, 0, err)
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
564 self.assertEqual(out.rstrip(), b'test with stdout=1')
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
565
68600
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
566 def test_stdout_devnull(self):
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
567 p = subprocess.Popen([sys.executable, "-c",
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
568 'for i in range(10240):'
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
569 'print("x" * 1024)'],
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
570 stdout=subprocess.DEVNULL)
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
571 p.wait()
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
572 self.assertEqual(p.stdout, None)
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
573
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
574 def test_stderr_devnull(self):
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
575 p = subprocess.Popen([sys.executable, "-c",
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
576 'import sys\n'
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
577 'for i in range(10240):'
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
578 'sys.stderr.write("x" * 1024)'],
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
579 stderr=subprocess.DEVNULL)
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
580 p.wait()
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
581 self.assertEqual(p.stderr, None)
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
582
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
583 def test_stdin_devnull(self):
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
584 p = subprocess.Popen([sys.executable, "-c",
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
585 'import sys;'
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
586 'sys.stdin.read(1)'],
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
587 stdin=subprocess.DEVNULL)
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
588 p.wait()
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
589 self.assertEqual(p.stdin, None)
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
590
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
591 def test_env(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
592 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
593 newenv["FRUIT"] = "orange"
70921
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
594 with subprocess.Popen([sys.executable, "-c",
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
595 'import sys,os;'
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
596 'sys.stdout.write(os.getenv("FRUIT"))'],
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
597 stdout=subprocess.PIPE,
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
598 env=newenv) as p:
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
599 stdout, stderr = p.communicate()
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
600 self.assertEqual(stdout, b"orange")
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
601
70929
52c5f80122dd Issue #12383: skip test_empty_env() of subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70926
diff changeset
602 # Windows requires at least the SYSTEMROOT environment variable to start
52c5f80122dd Issue #12383: skip test_empty_env() of subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70926
diff changeset
603 # Python
52c5f80122dd Issue #12383: skip test_empty_env() of subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70926
diff changeset
604 @unittest.skipIf(sys.platform == 'win32',
52c5f80122dd Issue #12383: skip test_empty_env() of subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70926
diff changeset
605 'cannot test an empty env on Windows')
70926
da3af4b131d7 Issue #12383: fix test_empty_env() of subprocess on Mac OS X
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70923
diff changeset
606 @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') is not None,
70923
8d46e84cd812 Issue #12383: skip test_empty_env() if compiled is compiled in shared mode
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70921
diff changeset
607 'the python library cannot be loaded '
8d46e84cd812 Issue #12383: skip test_empty_env() if compiled is compiled in shared mode
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70921
diff changeset
608 'with an empty environment')
70921
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
609 def test_empty_env(self):
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
610 with subprocess.Popen([sys.executable, "-c",
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
611 'import os; '
70923
8d46e84cd812 Issue #12383: skip test_empty_env() if compiled is compiled in shared mode
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70921
diff changeset
612 'print(list(os.environ.keys()))'],
70921
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
613 stdout=subprocess.PIPE,
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
614 env={}) as p:
b5963fceddad Close #12383: Fix subprocess module with env={}: don't copy the environment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70575
diff changeset
615 stdout, stderr = p.communicate()
70926
da3af4b131d7 Issue #12383: fix test_empty_env() of subprocess on Mac OS X
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70923
diff changeset
616 self.assertIn(stdout.strip(),
da3af4b131d7 Issue #12383: fix test_empty_env() of subprocess on Mac OS X
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70923
diff changeset
617 (b"[]",
da3af4b131d7 Issue #12383: fix test_empty_env() of subprocess on Mac OS X
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70923
diff changeset
618 # Mac OS X adds __CF_USER_TEXT_ENCODING variable to an empty
da3af4b131d7 Issue #12383: fix test_empty_env() of subprocess on Mac OS X
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70923
diff changeset
619 # environment
da3af4b131d7 Issue #12383: fix test_empty_env() of subprocess on Mac OS X
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70923
diff changeset
620 b"['__CF_USER_TEXT_ENCODING']"))
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
621
34671
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
622 def test_communicate_stdin(self):
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
623 p = subprocess.Popen([sys.executable, "-c",
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
624 'import sys;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
625 'sys.exit(sys.stdin.read() == "pear")'],
34671
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
626 stdin=subprocess.PIPE)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
627 p.communicate(b"pear")
34671
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
628 self.assertEqual(p.returncode, 1)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
629
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
630 def test_communicate_stdout(self):
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
631 p = subprocess.Popen([sys.executable, "-c",
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
632 'import sys; sys.stdout.write("pineapple")'],
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
633 stdout=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
634 (stdout, stderr) = p.communicate()
41845
9ce8fa0a0899 Make test_subprocess work. Fix universal newlines in io.py.
Guido van Rossum <guido@python.org>
parents: 41732
diff changeset
635 self.assertEqual(stdout, b"pineapple")
34671
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
636 self.assertEqual(stderr, None)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
637
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
638 def test_communicate_stderr(self):
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
639 p = subprocess.Popen([sys.executable, "-c",
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
640 'import sys; sys.stderr.write("pineapple")'],
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
641 stderr=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
642 (stdout, stderr) = p.communicate()
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
643 self.assertEqual(stdout, None)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
644 self.assertStderrEqual(stderr, b"pineapple")
34671
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
645
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
646 def test_communicate(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
647 p = subprocess.Popen([sys.executable, "-c",
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
648 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
649 'sys.stderr.write("pineapple");'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
650 'sys.stdout.write(sys.stdin.read())'],
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
651 stdin=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
652 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
653 stderr=subprocess.PIPE)
65977
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
654 self.addCleanup(p.stdout.close)
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
655 self.addCleanup(p.stderr.close)
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
656 self.addCleanup(p.stdin.close)
48395
bb65f9b76c1f #2683: Popen.communicate() argument must be bytes.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
657 (stdout, stderr) = p.communicate(b"banana")
41732
bbb099d4ec80 Make test_subprocess pass. The subprocess module now reads and writes
Guido van Rossum <guido@python.org>
parents: 40788
diff changeset
658 self.assertEqual(stdout, b"banana")
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
659 self.assertStderrEqual(stderr, b"pineapple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
660
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
661 def test_communicate_timeout(self):
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
662 p = subprocess.Popen([sys.executable, "-c",
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
663 'import sys,os,time;'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
664 'sys.stderr.write("pineapple\\n");'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
665 'time.sleep(1);'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
666 'sys.stderr.write("pear\\n");'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
667 'sys.stdout.write(sys.stdin.read())'],
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
668 universal_newlines=True,
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
669 stdin=subprocess.PIPE,
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
670 stdout=subprocess.PIPE,
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
671 stderr=subprocess.PIPE)
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
672 self.assertRaises(subprocess.TimeoutExpired, p.communicate, "banana",
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
673 timeout=0.3)
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
674 # Make sure we can keep waiting for it, and that we get the whole output
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
675 # after it completes.
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
676 (stdout, stderr) = p.communicate()
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
677 self.assertEqual(stdout, "banana")
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
678 self.assertStderrEqual(stderr.encode(), b"pineapple\npear\n")
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
679
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
680 def test_communicate_timeout_large_ouput(self):
74886
b27a28a30612 Fix a typo.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74885
diff changeset
681 # Test an expiring timeout while the child is outputting lots of data.
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
682 p = subprocess.Popen([sys.executable, "-c",
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
683 'import sys,os,time;'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
684 'sys.stdout.write("a" * (64 * 1024));'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
685 'time.sleep(0.2);'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
686 'sys.stdout.write("a" * (64 * 1024));'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
687 'time.sleep(0.2);'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
688 'sys.stdout.write("a" * (64 * 1024));'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
689 'time.sleep(0.2);'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
690 'sys.stdout.write("a" * (64 * 1024));'],
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
691 stdout=subprocess.PIPE)
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
692 self.assertRaises(subprocess.TimeoutExpired, p.communicate, timeout=0.4)
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
693 (stdout, _) = p.communicate()
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
694 self.assertEqual(len(stdout), 4 * 64 * 1024)
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
695
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
696 # Test for the fd leak reported in http://bugs.python.org/issue2791.
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
697 def test_communicate_pipe_fd_leak(self):
67130
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
698 for stdin_pipe in (False, True):
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
699 for stdout_pipe in (False, True):
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
700 for stderr_pipe in (False, True):
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
701 options = {}
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
702 if stdin_pipe:
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
703 options['stdin'] = subprocess.PIPE
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
704 if stdout_pipe:
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
705 options['stdout'] = subprocess.PIPE
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
706 if stderr_pipe:
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
707 options['stderr'] = subprocess.PIPE
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
708 if not options:
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
709 continue
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
710 p = subprocess.Popen((sys.executable, "-c", "pass"), **options)
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
711 p.communicate()
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
712 if p.stdin is not None:
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
713 self.assertTrue(p.stdin.closed)
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
714 if p.stdout is not None:
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
715 self.assertTrue(p.stdout.closed)
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
716 if p.stderr is not None:
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
717 self.assertTrue(p.stderr.closed)
47557
98827a21e352 Merged revisions 63724,63726,63732,63744,63754-63755,63757-63758,63760,63775,63781-63782,63787,63805-63808,63818-63819,63823-63824 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47331
diff changeset
718
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
719 def test_communicate_returns(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
720 # communicate() should return None if no redirection is active
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
721 p = subprocess.Popen([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
722 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
723 (stdout, stderr) = p.communicate()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
724 self.assertEqual(stdout, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
725 self.assertEqual(stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
726
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
727 def test_communicate_pipe_buf(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
728 # communicate() with writes larger than pipe_buf
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
729 # This test will probably deadlock rather than fail, if
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
730 # communicate() does not work properly.
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
731 x, y = os.pipe()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
732 os.close(x)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
733 os.close(y)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
734 p = subprocess.Popen([sys.executable, "-c",
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
735 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
736 'sys.stdout.write(sys.stdin.read(47));'
70474
866d959dea8e Issue #12196: Add PIPE_MAX_SIZE to test.support, constant larger than the
Charles-François Natali <neologix@free.fr>
parents: 70469
diff changeset
737 'sys.stderr.write("x" * %d);'
866d959dea8e Issue #12196: Add PIPE_MAX_SIZE to test.support, constant larger than the
Charles-François Natali <neologix@free.fr>
parents: 70469
diff changeset
738 'sys.stdout.write(sys.stdin.read())' %
866d959dea8e Issue #12196: Add PIPE_MAX_SIZE to test.support, constant larger than the
Charles-François Natali <neologix@free.fr>
parents: 70469
diff changeset
739 support.PIPE_MAX_SIZE],
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
740 stdin=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
741 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
742 stderr=subprocess.PIPE)
65977
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
743 self.addCleanup(p.stdout.close)
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
744 self.addCleanup(p.stderr.close)
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
745 self.addCleanup(p.stdin.close)
70474
866d959dea8e Issue #12196: Add PIPE_MAX_SIZE to test.support, constant larger than the
Charles-François Natali <neologix@free.fr>
parents: 70469
diff changeset
746 string_to_write = b"a" * support.PIPE_MAX_SIZE
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
747 (stdout, stderr) = p.communicate(string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
748 self.assertEqual(stdout, string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
749
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
750 def test_writes_before_communicate(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
751 # stdin.write before communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
752 p = subprocess.Popen([sys.executable, "-c",
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
753 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
754 'sys.stdout.write(sys.stdin.read())'],
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
755 stdin=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
756 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
757 stderr=subprocess.PIPE)
65977
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
758 self.addCleanup(p.stdout.close)
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
759 self.addCleanup(p.stderr.close)
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
760 self.addCleanup(p.stdin.close)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
761 p.stdin.write(b"banana")
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
762 (stdout, stderr) = p.communicate(b"split")
41732
bbb099d4ec80 Make test_subprocess pass. The subprocess module now reads and writes
Guido van Rossum <guido@python.org>
parents: 40788
diff changeset
763 self.assertEqual(stdout, b"bananasplit")
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
764 self.assertStderrEqual(stderr, b"")
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
765
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
766 def test_universal_newlines(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
767 p = subprocess.Popen([sys.executable, "-c",
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
768 'import sys,os;' + SETBINARY +
78421
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
769 'buf = sys.stdout.buffer;'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
770 'buf.write(sys.stdin.readline().encode());'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
771 'buf.flush();'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
772 'buf.write(b"line2\\n");'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
773 'buf.flush();'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
774 'buf.write(sys.stdin.read().encode());'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
775 'buf.flush();'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
776 'buf.write(b"line4\\n");'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
777 'buf.flush();'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
778 'buf.write(b"line5\\r\\n");'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
779 'buf.flush();'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
780 'buf.write(b"line6\\r");'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
781 'buf.flush();'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
782 'buf.write(b"\\nline7");'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
783 'buf.flush();'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
784 'buf.write(b"\\nline8");'],
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
785 stdin=subprocess.PIPE,
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
786 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
787 universal_newlines=1)
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
788 p.stdin.write("line1\n")
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
789 self.assertEqual(p.stdout.readline(), "line1\n")
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
790 p.stdin.write("line3\n")
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
791 p.stdin.close()
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
792 self.addCleanup(p.stdout.close)
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
793 self.assertEqual(p.stdout.readline(),
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
794 "line2\n")
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
795 self.assertEqual(p.stdout.read(6),
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
796 "line3\n")
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
797 self.assertEqual(p.stdout.read(),
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
798 "line4\nline5\nline6\nline7\nline8")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
799
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
800 def test_universal_newlines_communicate(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
801 # universal newlines through communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
802 p = subprocess.Popen([sys.executable, "-c",
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
803 'import sys,os;' + SETBINARY +
78421
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
804 'buf = sys.stdout.buffer;'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
805 'buf.write(b"line2\\n");'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
806 'buf.flush();'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
807 'buf.write(b"line4\\n");'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
808 'buf.flush();'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
809 'buf.write(b"line5\\r\\n");'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
810 'buf.flush();'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
811 'buf.write(b"line6\\r");'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
812 'buf.flush();'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
813 'buf.write(b"\\nline7");'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
814 'buf.flush();'
481f5d9ef577 Fix universal newlines test to avoid the newline translation done by sys.stdout.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75524
diff changeset
815 'buf.write(b"\\nline8");'],
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
816 stderr=subprocess.PIPE,
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
817 stdout=subprocess.PIPE,
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
818 universal_newlines=1)
65977
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
819 self.addCleanup(p.stdout.close)
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
820 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
821 (stdout, stderr) = p.communicate()
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
822 self.assertEqual(stdout,
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
823 "line2\nline4\nline5\nline6\nline7\nline8")
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
824
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
825 def test_universal_newlines_communicate_stdin(self):
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
826 # universal newlines through communicate(), with only stdin
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
827 p = subprocess.Popen([sys.executable, "-c",
78652
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
828 'import sys,os;' + SETBINARY + textwrap.dedent('''
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
829 s = sys.stdin.readline()
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
830 assert s == "line1\\n", repr(s)
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
831 s = sys.stdin.read()
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
832 assert s == "line3\\n", repr(s)
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
833 ''')],
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
834 stdin=subprocess.PIPE,
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
835 universal_newlines=1)
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
836 (stdout, stderr) = p.communicate("line1\nline3\n")
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
837 self.assertEqual(p.returncode, 0)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
838
78571
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
839 def test_universal_newlines_communicate_input_none(self):
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
840 # Test communicate(input=None) with universal newlines.
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
841 #
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
842 # We set stdout to PIPE because, as of this writing, a different
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
843 # code path is tested when the number of pipes is zero or one.
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
844 p = subprocess.Popen([sys.executable, "-c", "pass"],
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
845 stdin=subprocess.PIPE,
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
846 stdout=subprocess.PIPE,
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
847 universal_newlines=True)
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
848 p.communicate()
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
849 self.assertEqual(p.returncode, 0)
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
850
81997
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
851 def test_universal_newlines_communicate_stdin_stdout_stderr(self):
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
852 # universal newlines through communicate(), with stdin, stdout, stderr
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
853 p = subprocess.Popen([sys.executable, "-c",
78652
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
854 'import sys,os;' + SETBINARY + textwrap.dedent('''
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
855 s = sys.stdin.buffer.readline()
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
856 sys.stdout.buffer.write(s)
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
857 sys.stdout.buffer.write(b"line2\\r")
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
858 sys.stderr.buffer.write(b"eline2\\n")
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
859 s = sys.stdin.buffer.read()
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
860 sys.stdout.buffer.write(s)
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
861 sys.stdout.buffer.write(b"line4\\n")
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
862 sys.stdout.buffer.write(b"line5\\r\\n")
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
863 sys.stderr.buffer.write(b"eline6\\r")
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
864 sys.stderr.buffer.write(b"eline7\\r\\nz")
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
865 ''')],
81997
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
866 stdin=subprocess.PIPE,
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
867 stderr=subprocess.PIPE,
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
868 stdout=subprocess.PIPE,
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
869 universal_newlines=True)
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
870 self.addCleanup(p.stdout.close)
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
871 self.addCleanup(p.stderr.close)
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
872 (stdout, stderr) = p.communicate("line1\nline3\n")
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
873 self.assertEqual(p.returncode, 0)
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
874 self.assertEqual("line1\nline2\nline3\nline4\nline5\n", stdout)
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
875 # Python debug build push something like "[42442 refs]\n"
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
876 # to stderr at exit of subprocess.
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
877 # Don't use assertStderrEqual because it strips CR and LF from output.
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
878 self.assertTrue(stderr.startswith("eline2\neline6\neline7\n"))
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
879
78655
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
880 def test_universal_newlines_communicate_encodings(self):
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
881 # Check that universal newlines mode works for various encodings,
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
882 # in particular for encodings in the UTF-16 and UTF-32 families.
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
883 # See issue #15595.
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
884 #
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
885 # UTF-16 and UTF-32-BE are sufficient to check both with BOM and
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
886 # without, and UTF-16 and UTF-32.
86212
fbbf8b160e8d Issue #9548: Add a minimal "_bootlocale" module that is imported by the _io module instead of the full locale module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 86164
diff changeset
887 import _bootlocale
78655
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
888 for encoding in ['utf-16', 'utf-32-be']:
86212
fbbf8b160e8d Issue #9548: Add a minimal "_bootlocale" module that is imported by the _io module instead of the full locale module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 86164
diff changeset
889 old_getpreferredencoding = _bootlocale.getpreferredencoding
78655
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
890 # Indirectly via io.TextIOWrapper, Popen() defaults to
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
891 # locale.getpreferredencoding(False) and earlier in Python 3.2 to
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
892 # locale.getpreferredencoding().
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
893 def getpreferredencoding(do_setlocale=True):
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
894 return encoding
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
895 code = ("import sys; "
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
896 r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" %
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
897 encoding)
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
898 args = [sys.executable, '-c', code]
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
899 try:
86212
fbbf8b160e8d Issue #9548: Add a minimal "_bootlocale" module that is imported by the _io module instead of the full locale module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 86164
diff changeset
900 _bootlocale.getpreferredencoding = getpreferredencoding
78655
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
901 # We set stdin to be non-None because, as of this writing,
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
902 # a different code path is used when the number of pipes is
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
903 # zero or one.
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
904 popen = subprocess.Popen(args, universal_newlines=True,
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
905 stdin=subprocess.PIPE,
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
906 stdout=subprocess.PIPE)
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
907 stdout, stderr = popen.communicate(input='')
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
908 finally:
86212
fbbf8b160e8d Issue #9548: Add a minimal "_bootlocale" module that is imported by the _io module instead of the full locale module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 86164
diff changeset
909 _bootlocale.getpreferredencoding = old_getpreferredencoding
78655
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
910 self.assertEqual(stdout, '1\n2\n3\n4')
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
911
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
912 def test_no_leaking(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
913 # Make sure we leak no resources
64814
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
914 if not mswindows:
34673
00e47eb136b2 Only run extensive subprocess tests if -usubprocess to regrtest is specified. Fixes #1124637
Peter Astrand <astrand@lysator.liu.se>
parents: 34671
diff changeset
915 max_handles = 1026 # too much for most UNIX systems
00e47eb136b2 Only run extensive subprocess tests if -usubprocess to regrtest is specified. Fixes #1124637
Peter Astrand <astrand@lysator.liu.se>
parents: 34671
diff changeset
916 else:
64814
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
917 max_handles = 2050 # too much for (at least some) Windows setups
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
918 handles = []
68491
141bbcb19c06 Fix the @test_NNNN_tmp file terds being left in whatever your $PWD was
Gregory P. Smith <greg@krypto.org>
parents: 68432
diff changeset
919 tmpdir = tempfile.mkdtemp()
64814
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
920 try:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
921 for i in range(max_handles):
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
922 try:
68491
141bbcb19c06 Fix the @test_NNNN_tmp file terds being left in whatever your $PWD was
Gregory P. Smith <greg@krypto.org>
parents: 68432
diff changeset
923 tmpfile = os.path.join(tmpdir, support.TESTFN)
141bbcb19c06 Fix the @test_NNNN_tmp file terds being left in whatever your $PWD was
Gregory P. Smith <greg@krypto.org>
parents: 68432
diff changeset
924 handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT))
64814
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
925 except OSError as e:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
926 if e.errno != errno.EMFILE:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
927 raise
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
928 break
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
929 else:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
930 self.skipTest("failed to reach the file descriptor limit "
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
931 "(tried %d)" % max_handles)
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
932 # Close a couple of them (should be enough for a subprocess)
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
933 for i in range(10):
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
934 os.close(handles.pop())
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
935 # Loop creating some subprocesses. If one of them leaks some fds,
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
936 # the next loop iteration will fail by reaching the max fd limit.
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
937 for i in range(15):
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
938 p = subprocess.Popen([sys.executable, "-c",
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
939 "import sys;"
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
940 "sys.stdout.write(sys.stdin.read())"],
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
941 stdin=subprocess.PIPE,
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
942 stdout=subprocess.PIPE,
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
943 stderr=subprocess.PIPE)
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
944 data = p.communicate(b"lime")[0]
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
945 self.assertEqual(data, b"lime")
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
946 finally:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
947 for h in handles:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
948 os.close(h)
68491
141bbcb19c06 Fix the @test_NNNN_tmp file terds being left in whatever your $PWD was
Gregory P. Smith <greg@krypto.org>
parents: 68432
diff changeset
949 shutil.rmtree(tmpdir)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
950
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
951 def test_list2cmdline(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
952 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
953 '"a b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
954 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
955 'ab\\"c \\ d')
44810
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44373
diff changeset
956 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44373
diff changeset
957 'ab\\"c " \\\\" d')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
958 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
959 'a\\\\\\b "de fg" h')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
960 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
961 'a\\\\\\"b c d')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
962 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
963 '"a\\\\b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
964 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
965 '"a\\\\b\\ c" d e')
40643
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
966 self.assertEqual(subprocess.list2cmdline(['ab', '']),
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
967 'ab ""')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
968
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
969 def test_poll(self):
74885
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
970 p = subprocess.Popen([sys.executable, "-c",
75166
e5a94b56d6bc Fix sporadic test_subprocess regression introduced by 834650d63130.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 75097
diff changeset
971 "import os; os.read(0, 1)"],
e5a94b56d6bc Fix sporadic test_subprocess regression introduced by 834650d63130.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 75097
diff changeset
972 stdin=subprocess.PIPE)
74885
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
973 self.addCleanup(p.stdin.close)
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
974 self.assertIsNone(p.poll())
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
975 os.write(p.stdin.fileno(), b'A')
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
976 p.wait()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
977 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
978 self.assertEqual(p.poll(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
979
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
980 def test_wait(self):
74885
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
981 p = subprocess.Popen([sys.executable, "-c", "pass"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
982 self.assertEqual(p.wait(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
983 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
984 self.assertEqual(p.wait(), 0)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
985
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
986 def test_wait_timeout(self):
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
987 p = subprocess.Popen([sys.executable,
83841
256b8dc71046 Try to make test more reliable (saw some sporadic failures on buildbots)
Antoine Pitrou <solipsis@pitrou.net>
parents: 82917
diff changeset
988 "-c", "import time; time.sleep(0.3)"])
68618
a161081e8f7c Include the timeout value in TimeoutExpired.
Reid Kleckner <reid@kleckner.net>
parents: 68600
diff changeset
989 with self.assertRaises(subprocess.TimeoutExpired) as c:
83841
256b8dc71046 Try to make test more reliable (saw some sporadic failures on buildbots)
Antoine Pitrou <solipsis@pitrou.net>
parents: 82917
diff changeset
990 p.wait(timeout=0.0001)
256b8dc71046 Try to make test more reliable (saw some sporadic failures on buildbots)
Antoine Pitrou <solipsis@pitrou.net>
parents: 82917
diff changeset
991 self.assertIn("0.0001", str(c.exception)) # For coverage of __str__.
68619
2e4879d44604 Bump up the subprocess timeouts even more. :(
Reid Kleckner <reid@kleckner.net>
parents: 68618
diff changeset
992 # Some heavily loaded buildbots (sparc Debian 3.x) require this much
2e4879d44604 Bump up the subprocess timeouts even more. :(
Reid Kleckner <reid@kleckner.net>
parents: 68618
diff changeset
993 # time to start.
2e4879d44604 Bump up the subprocess timeouts even more. :(
Reid Kleckner <reid@kleckner.net>
parents: 68618
diff changeset
994 self.assertEqual(p.wait(timeout=3), 0)
34052
79d2ae0a62ef Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents: 33998
diff changeset
995
79d2ae0a62ef Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents: 33998
diff changeset
996 def test_invalid_bufsize(self):
79d2ae0a62ef Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents: 33998
diff changeset
997 # an invalid type of the bufsize argument should raise
79d2ae0a62ef Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents: 33998
diff changeset
998 # TypeError.
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
999 with self.assertRaises(TypeError):
34052
79d2ae0a62ef Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents: 33998
diff changeset
1000 subprocess.Popen([sys.executable, "-c", "pass"], "orange")
79d2ae0a62ef Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents: 33998
diff changeset
1001
41984
e420039368b7 The bufsize argument to Popen() should accept None meaning the default (0).
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
1002 def test_bufsize_is_none(self):
e420039368b7 The bufsize argument to Popen() should accept None meaning the default (0).
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
1003 # bufsize=None should be the same as bufsize=0.
e420039368b7 The bufsize argument to Popen() should accept None meaning the default (0).
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
1004 p = subprocess.Popen([sys.executable, "-c", "pass"], None)
e420039368b7 The bufsize argument to Popen() should accept None meaning the default (0).
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
1005 self.assertEqual(p.wait(), 0)
e420039368b7 The bufsize argument to Popen() should accept None meaning the default (0).
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
1006 # Again with keyword arg
e420039368b7 The bufsize argument to Popen() should accept None meaning the default (0).
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
1007 p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None)
e420039368b7 The bufsize argument to Popen() should accept None meaning the default (0).
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
1008 self.assertEqual(p.wait(), 0)
e420039368b7 The bufsize argument to Popen() should accept None meaning the default (0).
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
1009
51721
33d5d7daa8c7 Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50074
diff changeset
1010 def test_leaking_fds_on_error(self):
33d5d7daa8c7 Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50074
diff changeset
1011 # see bug #5179: Popen leaks file descriptors to PIPEs if
33d5d7daa8c7 Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50074
diff changeset
1012 # the child fails to execute; this will eventually exhaust
33d5d7daa8c7 Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50074
diff changeset
1013 # the maximum number of open fds. 1024 seems a very common
33d5d7daa8c7 Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50074
diff changeset
1014 # value for that limit, but Windows has 2048, so we loop
33d5d7daa8c7 Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50074
diff changeset
1015 # 1024 times (each call leaked two fds).
33d5d7daa8c7 Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50074
diff changeset
1016 for i in range(1024):
80922
061e9a439f54 Get rig of EnvironmentError (#16705)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80392
diff changeset
1017 with self.assertRaises(OSError) as c:
51721
33d5d7daa8c7 Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50074
diff changeset
1018 subprocess.Popen(['nonexisting_i_hope'],
33d5d7daa8c7 Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50074
diff changeset
1019 stdout=subprocess.PIPE,
33d5d7daa8c7 Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50074
diff changeset
1020 stderr=subprocess.PIPE)
68433
67f4ef6094ed Merge fix for #11490 from 3.1.
R David Murray <rdmurray@bitdance.com>
parents: 68257 68432
diff changeset
1021 # ignore errors that indicate the command was not found
68437
f65aad1226b7 Fix whitespace
R David Murray <rdmurray@bitdance.com>
parents: 68433
diff changeset
1022 if c.exception.errno not in (errno.ENOENT, errno.EACCES):
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1023 raise c.exception
51721
33d5d7daa8c7 Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50074
diff changeset
1024
85466
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1025 @unittest.skipIf(threading is None, "threading required")
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1026 def test_double_close_on_error(self):
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1027 # Issue #18851
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1028 fds = []
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1029 def open_fds():
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1030 for i in range(20):
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1031 fds.extend(os.pipe())
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1032 time.sleep(0.001)
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1033 t = threading.Thread(target=open_fds)
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1034 t.start()
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1035 try:
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1036 with self.assertRaises(EnvironmentError):
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1037 subprocess.Popen(['nonexisting_i_hope'],
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1038 stdin=subprocess.PIPE,
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1039 stdout=subprocess.PIPE,
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1040 stderr=subprocess.PIPE)
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1041 finally:
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1042 t.join()
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1043 exc = None
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1044 for fd in fds:
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1045 # If a double close occurred, some of those fds will
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1046 # already have been closed by mistake, and os.close()
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1047 # here will raise.
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1048 try:
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1049 os.close(fd)
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1050 except OSError as e:
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1051 exc = e
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1052 if exc is not None:
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1053 raise exc
c11754defe1c Forward port new tests from Issue #18851.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85456
diff changeset
1054
61798
73416b1bbcd5 Issue #8780: Only backport the new test, the fix is not needed
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61014
diff changeset
1055 def test_issue8780(self):
73416b1bbcd5 Issue #8780: Only backport the new test, the fix is not needed
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61014
diff changeset
1056 # Ensure that stdout is inherited from the parent
73416b1bbcd5 Issue #8780: Only backport the new test, the fix is not needed
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61014
diff changeset
1057 # if stdout=PIPE is not used
73416b1bbcd5 Issue #8780: Only backport the new test, the fix is not needed
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61014
diff changeset
1058 code = ';'.join((
73416b1bbcd5 Issue #8780: Only backport the new test, the fix is not needed
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61014
diff changeset
1059 'import subprocess, sys',
73416b1bbcd5 Issue #8780: Only backport the new test, the fix is not needed
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61014
diff changeset
1060 'retcode = subprocess.call('
73416b1bbcd5 Issue #8780: Only backport the new test, the fix is not needed
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61014
diff changeset
1061 "[sys.executable, '-c', 'print(\"Hello World!\")'])",
73416b1bbcd5 Issue #8780: Only backport the new test, the fix is not needed
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61014
diff changeset
1062 'assert retcode == 0'))
73416b1bbcd5 Issue #8780: Only backport the new test, the fix is not needed
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61014
diff changeset
1063 output = subprocess.check_output([sys.executable, '-c', code])
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64853
diff changeset
1064 self.assertTrue(output.startswith(b'Hello World!'), ascii(output))
61798
73416b1bbcd5 Issue #8780: Only backport the new test, the fix is not needed
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61014
diff changeset
1065
63774
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1066 def test_handles_closed_on_exception(self):
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1067 # If CreateProcess exits with an error, ensure the
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1068 # duplicate output handles are released
63772
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
1069 ifhandle, ifname = mkstemp()
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
1070 ofhandle, ofname = mkstemp()
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
1071 efhandle, efname = mkstemp()
63774
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1072 try:
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1073 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1074 stderr=efhandle)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1075 except OSError:
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1076 os.close(ifhandle)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1077 os.remove(ifname)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1078 os.close(ofhandle)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1079 os.remove(ofname)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1080 os.close(efhandle)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1081 os.remove(efname)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1082 self.assertFalse(os.path.exists(ifname))
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1083 self.assertFalse(os.path.exists(ofname))
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1084 self.assertFalse(os.path.exists(efname))
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1085
69154
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1086 def test_communicate_epipe(self):
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1087 # Issue 10963: communicate() should hide EPIPE
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1088 p = subprocess.Popen([sys.executable, "-c", 'pass'],
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1089 stdin=subprocess.PIPE,
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1090 stdout=subprocess.PIPE,
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1091 stderr=subprocess.PIPE)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1092 self.addCleanup(p.stdout.close)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1093 self.addCleanup(p.stderr.close)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1094 self.addCleanup(p.stdin.close)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1095 p.communicate(b"x" * 2**20)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1096
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1097 def test_communicate_epipe_only_stdin(self):
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1098 # Issue 10963: communicate() should hide EPIPE
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1099 p = subprocess.Popen([sys.executable, "-c", 'pass'],
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1100 stdin=subprocess.PIPE)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1101 self.addCleanup(p.stdin.close)
74885
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1102 p.wait()
69154
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1103 p.communicate(b"x" * 2**20)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1104
74885
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1105 @unittest.skipUnless(hasattr(signal, 'SIGUSR1'),
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1106 "Requires signal.SIGUSR1")
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1107 @unittest.skipUnless(hasattr(os, 'kill'),
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1108 "Requires os.kill")
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1109 @unittest.skipUnless(hasattr(os, 'getppid'),
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1110 "Requires os.getppid")
71228
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1111 def test_communicate_eintr(self):
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1112 # Issue #12493: communicate() should handle EINTR
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1113 def handler(signum, frame):
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1114 pass
74885
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1115 old_handler = signal.signal(signal.SIGUSR1, handler)
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1116 self.addCleanup(signal.signal, signal.SIGUSR1, old_handler)
71228
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1117
74885
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1118 args = [sys.executable, "-c",
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1119 'import os, signal;'
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1120 'os.kill(os.getppid(), signal.SIGUSR1)']
71228
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1121 for stream in ('stdout', 'stderr'):
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1122 kw = {stream: subprocess.PIPE}
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1123 with subprocess.Popen(args, **kw) as process:
74885
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1124 # communicate() will be interrupted by SIGUSR1
71228
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1125 process.communicate()
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1126
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1127
80368
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1128 # This test is Linux-ish specific for simplicity to at least have
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1129 # some coverage. It is not a platform specific bug.
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1130 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1131 "Linux specific")
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1132 def test_failed_child_execute_fd_leak(self):
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1133 """Test for the fork() failure fd leak reported in issue16327."""
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1134 fd_directory = '/proc/%d/fd' % os.getpid()
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1135 fds_before_popen = os.listdir(fd_directory)
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1136 with self.assertRaises(PopenTestException):
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1137 PopenExecuteChildRaises(
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1138 [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE,
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1139 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1140
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1141 # NOTE: This test doesn't verify that the real _execute_child
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1142 # does not close the file descriptors itself on the way out
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1143 # during an exception. Code inspection has confirmed that.
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1144
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1145 fds_after_exception = os.listdir(fd_directory)
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1146 self.assertEqual(fds_before_popen, fds_after_exception)
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1147
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
1148 @unittest.skipIf(mswindows, "POSIX specific tests")
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1149 class POSIXProcessTestCase(BaseTestCase):
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
1150
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1151 def setUp(self):
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1152 super().setUp()
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1153 self._nonexistent_dir = "/_this/pa.th/does/not/exist"
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1154
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1155 def _get_chdir_exception(self):
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1156 try:
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1157 os.chdir(self._nonexistent_dir)
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1158 except OSError as e:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1159 # This avoids hard coding the errno value or the OS perror()
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1160 # string and instead capture the exception that we want to see
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1161 # below for comparison.
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1162 desired_exception = e
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1163 desired_exception.strerror += ': ' + repr(self._nonexistent_dir)
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1164 else:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1165 self.fail("chdir to nonexistant directory %s succeeded." %
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1166 self._nonexistent_dir)
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1167 return desired_exception
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1168
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1169 def test_exception_cwd(self):
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1170 """Test error in the child raised in the parent for a bad cwd."""
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1171 desired_exception = self._get_chdir_exception()
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1172 try:
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1173 p = subprocess.Popen([sys.executable, "-c", ""],
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1174 cwd=self._nonexistent_dir)
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1175 except OSError as e:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1176 # Test that the child process chdir failure actually makes
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1177 # it up to the parent process as the correct exception.
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1178 self.assertEqual(desired_exception.errno, e.errno)
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1179 self.assertEqual(desired_exception.strerror, e.strerror)
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1180 else:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1181 self.fail("Expected OSError: %s" % desired_exception)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1182
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1183 def test_exception_bad_executable(self):
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1184 """Test error in the child raised in the parent for a bad executable."""
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1185 desired_exception = self._get_chdir_exception()
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1186 try:
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1187 p = subprocess.Popen([sys.executable, "-c", ""],
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1188 executable=self._nonexistent_dir)
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1189 except OSError as e:
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1190 # Test that the child process exec failure actually makes
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1191 # it up to the parent process as the correct exception.
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1192 self.assertEqual(desired_exception.errno, e.errno)
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1193 self.assertEqual(desired_exception.strerror, e.strerror)
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1194 else:
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1195 self.fail("Expected OSError: %s" % desired_exception)
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1196
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1197 def test_exception_bad_args_0(self):
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1198 """Test error in the child raised in the parent for a bad args[0]."""
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1199 desired_exception = self._get_chdir_exception()
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1200 try:
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1201 p = subprocess.Popen([self._nonexistent_dir, "-c", ""])
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1202 except OSError as e:
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1203 # Test that the child process exec failure actually makes
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1204 # it up to the parent process as the correct exception.
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1205 self.assertEqual(desired_exception.errno, e.errno)
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1206 self.assertEqual(desired_exception.strerror, e.strerror)
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1207 else:
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1208 self.fail("Expected OSError: %s" % desired_exception)
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1209
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1210 def test_restore_signals(self):
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1211 # Code coverage for both values of restore_signals to make sure it
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1212 # at least does not blow up.
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1213 # A test for behavior would be complex. Contributions welcome.
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1214 subprocess.call([sys.executable, "-c", ""], restore_signals=True)
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1215 subprocess.call([sys.executable, "-c", ""], restore_signals=False)
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1216
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1217 def test_start_new_session(self):
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1218 # For code coverage of calling setsid(). We don't care if we get an
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1219 # EPERM error from it depending on the test execution environment, that
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1220 # still indicates that it was called.
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1221 try:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1222 output = subprocess.check_output(
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1223 [sys.executable, "-c",
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1224 "import os; print(os.getpgid(os.getpid()))"],
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1225 start_new_session=True)
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1226 except OSError as e:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1227 if e.errno != errno.EPERM:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1228 raise
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1229 else:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1230 parent_pgid = os.getpgid(os.getpid())
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1231 child_pgid = int(output)
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1232 self.assertNotEqual(parent_pgid, child_pgid)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1233
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1234 def test_run_abort(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1235 # returncode handles signal termination
86164
1cbd3d9f7d61 Issue #18948: improve SuppressCoreFiles to include Windows crash popup suppression, and use it in more tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85576
diff changeset
1236 with support.SuppressCrashReport():
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1237 p = subprocess.Popen([sys.executable, "-c",
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1238 'import os; os.abort()'])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1239 p.wait()
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1240 self.assertEqual(-p.returncode, signal.SIGABRT)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1241
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1242 def test_preexec(self):
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1243 # DISCLAIMER: Setting environment variables is *not* a good use
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1244 # of a preexec_fn. This is merely a test.
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1245 p = subprocess.Popen([sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1246 'import sys,os;'
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1247 'sys.stdout.write(os.getenv("FRUIT"))'],
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1248 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1249 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
1250 self.addCleanup(p.stdout.close)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1251 self.assertEqual(p.stdout.read(), b"apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1252
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1253 def test_preexec_exception(self):
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1254 def raise_it():
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1255 raise ValueError("What if two swallows carried a coconut?")
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1256 try:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1257 p = subprocess.Popen([sys.executable, "-c", ""],
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1258 preexec_fn=raise_it)
80375
9180fb4eccc7 Raise our own SubprocessError rather than a RuntimeError in when dealing with
Gregory P. Smith <greg@krypto.org>
parents: 80369
diff changeset
1259 except subprocess.SubprocessError as e:
59605
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1260 self.assertTrue(
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1261 subprocess._posixsubprocess,
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1262 "Expected a ValueError from the preexec_fn")
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1263 except ValueError as e:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1264 self.assertIn("coconut", e.args[0])
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1265 else:
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1266 self.fail("Exception raised by preexec_fn did not make it "
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1267 "to the parent process.")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1268
80390
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1269 class _TestExecuteChildPopen(subprocess.Popen):
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1270 """Used to test behavior at the end of _execute_child."""
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1271 def __init__(self, testcase, *args, **kwargs):
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1272 self._testcase = testcase
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1273 subprocess.Popen.__init__(self, *args, **kwargs)
80379
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1274
80390
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1275 def _execute_child(self, *args, **kwargs):
80379
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1276 try:
80390
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1277 subprocess.Popen._execute_child(self, *args, **kwargs)
80379
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1278 finally:
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1279 # Open a bunch of file descriptors and verify that
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1280 # none of them are the same as the ones the Popen
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1281 # instance is using for stdin/stdout/stderr.
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1282 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1283 for _ in range(8)]
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1284 try:
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1285 for fd in devzero_fds:
80390
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1286 self._testcase.assertNotIn(
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1287 fd, (self.stdin.fileno(), self.stdout.fileno(),
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1288 self.stderr.fileno()),
80379
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1289 msg="At least one fd was closed early.")
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1290 finally:
84081
46fe1bb0723c Issue #18174: Fix fd leaks in tests.
Richard Oudkerk <shibturn@gmail.com>
parents: 83841
diff changeset
1291 for fd in devzero_fds:
46fe1bb0723c Issue #18174: Fix fd leaks in tests.
Richard Oudkerk <shibturn@gmail.com>
parents: 83841
diff changeset
1292 os.close(fd)
80379
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1293
80390
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1294 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1295 def test_preexec_errpipe_does_not_double_close_pipes(self):
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1296 """Issue16140: Don't double close pipes on preexec error."""
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1297
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1298 def raise_it():
80392
c971687e5c66 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80381 80391
diff changeset
1299 raise subprocess.SubprocessError(
c971687e5c66 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80381 80391
diff changeset
1300 "force the _execute_child() errpipe_data path.")
80379
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1301
80381
d51df72dadb7 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80375 80380
diff changeset
1302 with self.assertRaises(subprocess.SubprocessError):
80390
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1303 self._TestExecuteChildPopen(
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1304 self, [sys.executable, "-c", "pass"],
80379
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1305 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1306 stderr=subprocess.PIPE, preexec_fn=raise_it)
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1307
59723
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1308 def test_preexec_gc_module_failure(self):
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1309 # This tests the code that disables garbage collection if the child
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1310 # process will execute any Python.
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1311 def raise_runtime_error():
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1312 raise RuntimeError("this shouldn't escape")
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1313 enabled = gc.isenabled()
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1314 orig_gc_disable = gc.disable
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1315 orig_gc_isenabled = gc.isenabled
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1316 try:
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1317 gc.disable()
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1318 self.assertFalse(gc.isenabled())
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1319 subprocess.call([sys.executable, '-c', ''],
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1320 preexec_fn=lambda: None)
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1321 self.assertFalse(gc.isenabled(),
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1322 "Popen enabled gc when it shouldn't.")
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1323
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1324 gc.enable()
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1325 self.assertTrue(gc.isenabled())
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1326 subprocess.call([sys.executable, '-c', ''],
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1327 preexec_fn=lambda: None)
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1328 self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1329
59723
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1330 gc.disable = raise_runtime_error
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1331 self.assertRaises(RuntimeError, subprocess.Popen,
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1332 [sys.executable, '-c', ''],
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1333 preexec_fn=lambda: None)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1334
59723
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1335 del gc.isenabled # force an AttributeError
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1336 self.assertRaises(AttributeError, subprocess.Popen,
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1337 [sys.executable, '-c', ''],
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1338 preexec_fn=lambda: None)
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1339 finally:
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1340 gc.disable = orig_gc_disable
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1341 gc.isenabled = orig_gc_isenabled
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1342 if not enabled:
00daa8b736cc * Fix a refleak when a preexec_fn was supplied (preexec_fn_args_tuple was not
Gregory P. Smith <greg@mad-scientist.com>
parents: 59605
diff changeset
1343 gc.disable()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1344
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1345 def test_args_string(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1346 # args is a string
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1347 fd, fname = mkstemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1348 # reopen in text mode
65415
340359d80ca0 test_subprocess: use surrogateescape error handler to write shell scripts
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65302
diff changeset
1349 with open(fd, "w", errors="surrogateescape") as fobj:
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1350 fobj.write("#!/bin/sh\n")
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1351 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1352 sys.executable)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1353 os.chmod(fname, 0o700)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1354 p = subprocess.Popen(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1355 p.wait()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1356 os.remove(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1357 self.assertEqual(p.returncode, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1358
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1359 def test_invalid_args(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1360 # invalid arguments should raise ValueError
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1361 self.assertRaises(ValueError, subprocess.call,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1362 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1363 "import sys; sys.exit(47)"],
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1364 startupinfo=47)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1365 self.assertRaises(ValueError, subprocess.call,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1366 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1367 "import sys; sys.exit(47)"],
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1368 creationflags=47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1369
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1370 def test_shell_sequence(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1371 # Run command through the shell (sequence)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1372 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1373 newenv["FRUIT"] = "apple"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1374 p = subprocess.Popen(["echo $FRUIT"], shell=1,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1375 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1376 env=newenv)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
1377 self.addCleanup(p.stdout.close)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1378 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1379
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1380 def test_shell_string(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1381 # Run command through the shell (string)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1382 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1383 newenv["FRUIT"] = "apple"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1384 p = subprocess.Popen("echo $FRUIT", shell=1,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1385 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1386 env=newenv)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
1387 self.addCleanup(p.stdout.close)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1388 self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1389
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1390 def test_call_string(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1391 # call() function with string argument on UNIX
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1392 fd, fname = mkstemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1393 # reopen in text mode
65415
340359d80ca0 test_subprocess: use surrogateescape error handler to write shell scripts
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65302
diff changeset
1394 with open(fd, "w", errors="surrogateescape") as fobj:
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1395 fobj.write("#!/bin/sh\n")
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1396 fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1397 sys.executable)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1398 os.chmod(fname, 0o700)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1399 rc = subprocess.call(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1400 os.remove(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1401 self.assertEqual(rc, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1402
63094
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1403 def test_specific_shell(self):
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1404 # Issue #9265: Incorrect name passed as arg[0].
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1405 shells = []
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1406 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1407 for name in ['bash', 'ksh']:
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1408 sh = os.path.join(prefix, name)
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1409 if os.path.isfile(sh):
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1410 shells.append(sh)
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1411 if not shells: # Will probably work for any shell but csh.
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1412 self.skipTest("bash or ksh required for this test")
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1413 sh = '/bin/sh'
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1414 if os.path.isfile(sh) and not os.path.islink(sh):
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1415 # Test will fail if /bin/sh is a symlink to csh.
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1416 shells.append(sh)
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1417 for sh in shells:
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1418 p = subprocess.Popen("echo $0", executable=sh, shell=True,
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1419 stdout=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
1420 self.addCleanup(p.stdout.close)
63094
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1421 self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1422
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
1423 def _kill_process(self, method, *args):
59376
d9e6f318568f Merged revisions 78701 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59353
diff changeset
1424 # Do not inherit file handles from the parent.
d9e6f318568f Merged revisions 78701 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59353
diff changeset
1425 # It should fix failures on some platforms.
85456
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1426 # Also set the SIGINT handler to the default to make sure it's not
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1427 # being ignored (some tests rely on that.)
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1428 old_handler = signal.signal(signal.SIGINT, signal.default_int_handler)
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1429 try:
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1430 p = subprocess.Popen([sys.executable, "-c", """if 1:
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1431 import sys, time
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1432 sys.stdout.write('x\\n')
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1433 sys.stdout.flush()
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1434 time.sleep(30)
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1435 """],
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1436 close_fds=True,
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1437 stdin=subprocess.PIPE,
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1438 stdout=subprocess.PIPE,
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1439 stderr=subprocess.PIPE)
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1440 finally:
9810844126e1 Fixes issue #15507: test_subprocess's test_send_signal could fail if the test
Gregory P. Smith <greg@krypto.org>
parents: 85391
diff changeset
1441 signal.signal(signal.SIGINT, old_handler)
64843
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1442 # Wait for the interpreter to be completely initialized before
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1443 # sending any signal.
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1444 p.stdout.read(1)
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1445 getattr(p, method)(*args)
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
1446 return p
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1447
81455
58ce6ac61ada Issue #16762: Fix some test_subprocess failures on NetBSD and OpenBSD: kill()
Charles-François Natali <cf.natali@gmail.com>
parents: 81075
diff changeset
1448 @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')),
58ce6ac61ada Issue #16762: Fix some test_subprocess failures on NetBSD and OpenBSD: kill()
Charles-François Natali <cf.natali@gmail.com>
parents: 81075
diff changeset
1449 "Due to known OS bug (issue #16762)")
75524
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1450 def _kill_dead_process(self, method, *args):
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1451 # Do not inherit file handles from the parent.
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1452 # It should fix failures on some platforms.
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1453 p = subprocess.Popen([sys.executable, "-c", """if 1:
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1454 import sys, time
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1455 sys.stdout.write('x\\n')
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1456 sys.stdout.flush()
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1457 """],
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1458 close_fds=True,
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1459 stdin=subprocess.PIPE,
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1460 stdout=subprocess.PIPE,
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1461 stderr=subprocess.PIPE)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1462 # Wait for the interpreter to be completely initialized before
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1463 # sending any signal.
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1464 p.stdout.read(1)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1465 # The process should end after this
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1466 time.sleep(1)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1467 # This shouldn't raise even though the child is now dead
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1468 getattr(p, method)(*args)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1469 p.communicate()
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1470
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
1471 def test_send_signal(self):
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
1472 p = self._kill_process('send_signal', signal.SIGINT)
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1473 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1474 self.assertIn(b'KeyboardInterrupt', stderr)
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
1475 self.assertNotEqual(p.wait(), 0)
46505
63965327b3ef Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44810
diff changeset
1476
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1477 def test_kill(self):
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
1478 p = self._kill_process('kill')
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1479 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1480 self.assertStderrEqual(stderr, b'')
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1481 self.assertEqual(p.wait(), -signal.SIGKILL)
46505
63965327b3ef Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44810
diff changeset
1482
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1483 def test_terminate(self):
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
1484 p = self._kill_process('terminate')
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1485 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1486 self.assertStderrEqual(stderr, b'')
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1487 self.assertEqual(p.wait(), -signal.SIGTERM)
63095
ad66eff9800b Merged revisions 82971 via svnmerge from
Stefan Krah <stefan@bytereef.org>
parents: 62404
diff changeset
1488
75524
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1489 def test_send_signal_dead(self):
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1490 # Sending a signal to a dead process
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1491 self._kill_dead_process('send_signal', signal.SIGINT)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1492
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1493 def test_kill_dead(self):
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1494 # Killing a dead process
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1495 self._kill_dead_process('kill')
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1496
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1497 def test_terminate_dead(self):
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1498 # Terminating a dead process
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1499 self._kill_dead_process('terminate')
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
1500
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1501 def _save_fds(self, save_fds):
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1502 fds = []
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1503 for fd in save_fds:
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1504 inheritable = os.get_inheritable(fd)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1505 saved = os.dup(fd)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1506 fds.append((fd, saved, inheritable))
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1507 return fds
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1508
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1509 def _restore_fds(self, fds):
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1510 for fd, saved, inheritable in fds:
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1511 os.dup2(saved, fd, inheritable=inheritable)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1512 os.close(saved)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1513
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1514 def check_close_std_fds(self, fds):
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1515 # Issue #9905: test that subprocess pipes still work properly with
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1516 # some standard fds closed
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1517 stdin = 0
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1518 saved_fds = self._save_fds(fds)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1519 for fd, saved, inheritable in saved_fds:
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1520 if fd == 0:
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1521 stdin = saved
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1522 break
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1523 try:
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1524 for fd in fds:
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1525 os.close(fd)
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1526 out, err = subprocess.Popen([sys.executable, "-c",
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1527 'import sys;'
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1528 'sys.stdout.write("apple");'
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1529 'sys.stdout.flush();'
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1530 'sys.stderr.write("orange")'],
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1531 stdin=stdin,
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1532 stdout=subprocess.PIPE,
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1533 stderr=subprocess.PIPE).communicate()
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1534 err = support.strip_python_stderr(err)
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1535 self.assertEqual((out, err), (b'apple', b'orange'))
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1536 finally:
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1537 self._restore_fds(saved_fds)
46505
63965327b3ef Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44810
diff changeset
1538
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1539 def test_close_fd_0(self):
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1540 self.check_close_std_fds([0])
46505
63965327b3ef Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44810
diff changeset
1541
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1542 def test_close_fd_1(self):
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1543 self.check_close_std_fds([1])
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1544
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1545 def test_close_fd_2(self):
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1546 self.check_close_std_fds([2])
46505
63965327b3ef Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44810
diff changeset
1547
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1548 def test_close_fds_0_1(self):
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1549 self.check_close_std_fds([0, 1])
46505
63965327b3ef Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44810
diff changeset
1550
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1551 def test_close_fds_0_2(self):
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1552 self.check_close_std_fds([0, 2])
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1553
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1554 def test_close_fds_1_2(self):
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1555 self.check_close_std_fds([1, 2])
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
1556
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1557 def test_close_fds_0_1_2(self):
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1558 # Issue #10806: test that subprocess pipes still work properly with
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1559 # all standard fds closed.
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1560 self.check_close_std_fds([0, 1, 2])
61014
6a90dc967fed test_subprocess: test_undecodable_env() is specific to POSIX system
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60975
diff changeset
1561
87686
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1562 def test_small_errpipe_write_fd(self):
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1563 """Issue #15798: Popen should work when stdio fds are available."""
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1564 new_stdin = os.dup(0)
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1565 new_stdout = os.dup(1)
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1566 try:
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1567 os.close(0)
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1568 os.close(1)
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1569
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1570 # Side test: if errpipe_write fails to have its CLOEXEC
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1571 # flag set this should cause the parent to think the exec
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1572 # failed. Extremely unlikely: everyone supports CLOEXEC.
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1573 subprocess.Popen([
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1574 sys.executable, "-c",
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1575 "print('AssertionError:0:CLOEXEC failure.')"]).wait()
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1576 finally:
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1577 # Restore original stdin and stdout
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1578 os.dup2(new_stdin, 0)
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1579 os.dup2(new_stdout, 1)
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1580 os.close(new_stdin)
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1581 os.close(new_stdout)
07425df887b5 Fixes issue #15798: subprocess.Popen() no longer fails if file
Gregory P. Smith <greg@krypto.org>
parents: 87674
diff changeset
1582
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1583 def test_remapping_std_fds(self):
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1584 # open up some temporary files
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1585 temps = [mkstemp() for i in range(3)]
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1586 try:
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1587 temp_fds = [fd for fd, fname in temps]
61014
6a90dc967fed test_subprocess: test_undecodable_env() is specific to POSIX system
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60975
diff changeset
1588
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1589 # unlink the files -- we won't need to reopen them
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1590 for fd, fname in temps:
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1591 os.unlink(fname)
61014
6a90dc967fed test_subprocess: test_undecodable_env() is specific to POSIX system
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60975
diff changeset
1592
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1593 # write some data to what will become stdin, and rewind
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1594 os.write(temp_fds[1], b"STDIN")
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1595 os.lseek(temp_fds[1], 0, 0)
66895
b066bd0b64e7 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66340
diff changeset
1596
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1597 # move the standard file descriptors out of the way
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1598 saved_fds = self._save_fds(range(3))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1599 try:
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1600 # duplicate the file objects over the standard fd's
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1601 for fd, temp_fd in enumerate(temp_fds):
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1602 os.dup2(temp_fd, fd)
46505
63965327b3ef Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44810
diff changeset
1603
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1604 # now use those files in the "wrong" order, so that subprocess
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1605 # has to rearrange them in the child
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1606 p = subprocess.Popen([sys.executable, "-c",
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1607 'import sys; got = sys.stdin.read();'
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1608 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1609 stdin=temp_fds[1],
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1610 stdout=temp_fds[2],
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1611 stderr=temp_fds[0])
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1612 p.wait()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1613 finally:
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1614 self._restore_fds(saved_fds)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
1615
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1616 for fd in temp_fds:
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1617 os.lseek(fd, 0, 0)
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1618
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1619 out = os.read(temp_fds[2], 1024)
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1620 err = support.strip_python_stderr(os.read(temp_fds[0], 1024))
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1621 self.assertEqual(out, b"got STDIN")
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1622 self.assertEqual(err, b"err")
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1623
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1624 finally:
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1625 for fd in temp_fds:
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1626 os.close(fd)
61014
6a90dc967fed test_subprocess: test_undecodable_env() is specific to POSIX system
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60975
diff changeset
1627
71524
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1628 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1629 # open up some temporary files
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1630 temps = [mkstemp() for i in range(3)]
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1631 temp_fds = [fd for fd, fname in temps]
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1632 try:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1633 # unlink the files -- we won't need to reopen them
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1634 for fd, fname in temps:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1635 os.unlink(fname)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1636
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1637 # save a copy of the standard file descriptors
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1638 saved_fds = self._save_fds(range(3))
71524
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1639 try:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1640 # duplicate the temp files over the standard fd's 0, 1, 2
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1641 for fd, temp_fd in enumerate(temp_fds):
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1642 os.dup2(temp_fd, fd)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1643
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1644 # write some data to what will become stdin, and rewind
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1645 os.write(stdin_no, b"STDIN")
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1646 os.lseek(stdin_no, 0, 0)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1647
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1648 # now use those files in the given order, so that subprocess
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1649 # has to rearrange them in the child
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1650 p = subprocess.Popen([sys.executable, "-c",
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1651 'import sys; got = sys.stdin.read();'
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1652 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1653 stdin=stdin_no,
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1654 stdout=stdout_no,
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1655 stderr=stderr_no)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1656 p.wait()
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1657
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1658 for fd in temp_fds:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1659 os.lseek(fd, 0, 0)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1660
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1661 out = os.read(stdout_no, 1024)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1662 err = support.strip_python_stderr(os.read(stderr_no, 1024))
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1663 finally:
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1664 self._restore_fds(saved_fds)
71524
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1665
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1666 self.assertEqual(out, b"got STDIN")
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1667 self.assertEqual(err, b"err")
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1668
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1669 finally:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1670 for fd in temp_fds:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1671 os.close(fd)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1672
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1673 # When duping fds, if there arises a situation where one of the fds is
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1674 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1675 # This tests all combinations of this.
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1676 def test_swap_fds(self):
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1677 self.check_swap_fds(0, 1, 2)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1678 self.check_swap_fds(0, 2, 1)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1679 self.check_swap_fds(1, 0, 2)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1680 self.check_swap_fds(1, 2, 0)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1681 self.check_swap_fds(2, 0, 1)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1682 self.check_swap_fds(2, 1, 0)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1683
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1684 def test_surrogates_error_message(self):
60896
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1685 def prepare():
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1686 raise ValueError("surrogate:\uDCff")
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1687
60896
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1688 try:
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1689 subprocess.call(
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1690 [sys.executable, "-c", "pass"],
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1691 preexec_fn=prepare)
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1692 except ValueError as err:
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1693 # Pure Python implementations keeps the message
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1694 self.assertIsNone(subprocess._posixsubprocess)
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1695 self.assertEqual(str(err), "surrogate:\uDCff")
80375
9180fb4eccc7 Raise our own SubprocessError rather than a RuntimeError in when dealing with
Gregory P. Smith <greg@krypto.org>
parents: 80369
diff changeset
1696 except subprocess.SubprocessError as err:
60896
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1697 # _posixsubprocess uses a default message
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1698 self.assertIsNotNone(subprocess._posixsubprocess)
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1699 self.assertEqual(str(err), "Exception occurred in preexec_fn.")
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1700 else:
80375
9180fb4eccc7 Raise our own SubprocessError rather than a RuntimeError in when dealing with
Gregory P. Smith <greg@krypto.org>
parents: 80369
diff changeset
1701 self.fail("Expected ValueError or subprocess.SubprocessError")
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1702
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1703 def test_undecodable_env(self):
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1704 for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')):
87280
e651036191ad Issue #19637: fix test_undecodable_env() of test_subprocess on AIX
Victor Stinner <victor.stinner@gmail.com>
parents: 87008
diff changeset
1705 encoded_value = value.encode("ascii", "surrogateescape")
e651036191ad Issue #19637: fix test_undecodable_env() of test_subprocess on AIX
Victor Stinner <victor.stinner@gmail.com>
parents: 87008
diff changeset
1706
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1707 # test str with surrogates
64839
4374b1eaead1 Try to fix test_subprocess on "x86 debian parallel 3.x" buildbot
Antoine Pitrou <solipsis@pitrou.net>
parents: 64813
diff changeset
1708 script = "import os; print(ascii(os.getenv(%s)))" % repr(key)
60905
e79a6c802934 Fix test_undecodable_env of test_subproces for non-ASCII directory
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
1709 env = os.environ.copy()
e79a6c802934 Fix test_undecodable_env of test_subproces for non-ASCII directory
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
1710 env[key] = value
87280
e651036191ad Issue #19637: fix test_undecodable_env() of test_subprocess on AIX
Victor Stinner <victor.stinner@gmail.com>
parents: 87008
diff changeset
1711 # Use C locale to get ASCII for the locale encoding to force
65302
e2c98c60dd64 test_subprocess doesn't need to C locale to test os.environb
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65301
diff changeset
1712 # surrogate-escaping of \xFF in the child process; otherwise it can
e2c98c60dd64 test_subprocess doesn't need to C locale to test os.environb
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65301
diff changeset
1713 # be decoded as-is if the default locale is latin-1.
65301
2853cad9cbc0 test_subprocess: use C locale to get ascii locale encoding
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65265
diff changeset
1714 env['LC_ALL'] = 'C'
87280
e651036191ad Issue #19637: fix test_undecodable_env() of test_subprocess on AIX
Victor Stinner <victor.stinner@gmail.com>
parents: 87008
diff changeset
1715 if sys.platform.startswith("aix"):
e651036191ad Issue #19637: fix test_undecodable_env() of test_subprocess on AIX
Victor Stinner <victor.stinner@gmail.com>
parents: 87008
diff changeset
1716 # On AIX, the C locale uses the Latin1 encoding
e651036191ad Issue #19637: fix test_undecodable_env() of test_subprocess on AIX
Victor Stinner <victor.stinner@gmail.com>
parents: 87008
diff changeset
1717 decoded_value = encoded_value.decode("latin1", "surrogateescape")
e651036191ad Issue #19637: fix test_undecodable_env() of test_subprocess on AIX
Victor Stinner <victor.stinner@gmail.com>
parents: 87008
diff changeset
1718 else:
e651036191ad Issue #19637: fix test_undecodable_env() of test_subprocess on AIX
Victor Stinner <victor.stinner@gmail.com>
parents: 87008
diff changeset
1719 # On other UNIXes, the C locale uses the ASCII encoding
e651036191ad Issue #19637: fix test_undecodable_env() of test_subprocess on AIX
Victor Stinner <victor.stinner@gmail.com>
parents: 87008
diff changeset
1720 decoded_value = value
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1721 stdout = subprocess.check_output(
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1722 [sys.executable, "-c", script],
60905
e79a6c802934 Fix test_undecodable_env of test_subproces for non-ASCII directory
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
1723 env=env)
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1724 stdout = stdout.rstrip(b'\n\r')
87280
e651036191ad Issue #19637: fix test_undecodable_env() of test_subprocess on AIX
Victor Stinner <victor.stinner@gmail.com>
parents: 87008
diff changeset
1725 self.assertEqual(stdout.decode('ascii'), ascii(decoded_value))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1726
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1727 # test bytes
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1728 key = key.encode("ascii", "surrogateescape")
64839
4374b1eaead1 Try to fix test_subprocess on "x86 debian parallel 3.x" buildbot
Antoine Pitrou <solipsis@pitrou.net>
parents: 64813
diff changeset
1729 script = "import os; print(ascii(os.getenvb(%s)))" % repr(key)
60905
e79a6c802934 Fix test_undecodable_env of test_subproces for non-ASCII directory
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
1730 env = os.environ.copy()
87280
e651036191ad Issue #19637: fix test_undecodable_env() of test_subprocess on AIX
Victor Stinner <victor.stinner@gmail.com>
parents: 87008
diff changeset
1731 env[key] = encoded_value
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1732 stdout = subprocess.check_output(
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1733 [sys.executable, "-c", script],
60905
e79a6c802934 Fix test_undecodable_env of test_subproces for non-ASCII directory
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
1734 env=env)
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1735 stdout = stdout.rstrip(b'\n\r')
87280
e651036191ad Issue #19637: fix test_undecodable_env() of test_subprocess on AIX
Victor Stinner <victor.stinner@gmail.com>
parents: 87008
diff changeset
1736 self.assertEqual(stdout.decode('ascii'), ascii(encoded_value))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1737
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1738 def test_bytes_program(self):
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1739 abs_program = os.fsencode(sys.executable)
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1740 path, program = os.path.split(sys.executable)
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1741 program = os.fsencode(program)
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1742
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1743 # absolute bytes path
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1744 exitcode = subprocess.call([abs_program, "-c", "pass"])
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66307
diff changeset
1745 self.assertEqual(exitcode, 0)
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1746
68214
738feec9ced0 Issue #8513: On UNIX, subprocess supports bytes command string.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68142
diff changeset
1747 # absolute bytes path as a string
738feec9ced0 Issue #8513: On UNIX, subprocess supports bytes command string.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68142
diff changeset
1748 cmd = b"'" + abs_program + b"' -c pass"
738feec9ced0 Issue #8513: On UNIX, subprocess supports bytes command string.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68142
diff changeset
1749 exitcode = subprocess.call(cmd, shell=True)
738feec9ced0 Issue #8513: On UNIX, subprocess supports bytes command string.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68142
diff changeset
1750 self.assertEqual(exitcode, 0)
738feec9ced0 Issue #8513: On UNIX, subprocess supports bytes command string.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68142
diff changeset
1751
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1752 # bytes program, unicode PATH
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1753 env = os.environ.copy()
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1754 env["PATH"] = path
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1755 exitcode = subprocess.call([program, "-c", "pass"], env=env)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66307
diff changeset
1756 self.assertEqual(exitcode, 0)
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1757
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1758 # bytes program, bytes PATH
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1759 envb = os.environb.copy()
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1760 envb[b"PATH"] = os.fsencode(path)
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1761 exitcode = subprocess.call([program, "-c", "pass"], env=envb)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66307
diff changeset
1762 self.assertEqual(exitcode, 0)
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1763
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1764 def test_pipe_cloexec(self):
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1765 sleeper = support.findfile("input_reader.py", subdir="subprocessdata")
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1766 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1767
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1768 p1 = subprocess.Popen([sys.executable, sleeper],
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1769 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1770 stderr=subprocess.PIPE, close_fds=False)
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1771
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1772 self.addCleanup(p1.communicate, b'')
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1773
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1774 p2 = subprocess.Popen([sys.executable, fd_status],
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1775 stdout=subprocess.PIPE, close_fds=False)
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1776
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1777 output, error = p2.communicate()
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1778 result_fds = set(map(int, output.split(b',')))
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1779 unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(),
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1780 p1.stderr.fileno()])
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1781
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1782 self.assertFalse(result_fds & unwanted_fds,
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1783 "Expected no fds from %r to be open in child, "
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1784 "found %r" %
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1785 (unwanted_fds, result_fds & unwanted_fds))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1786
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1787 def test_pipe_cloexec_real_tools(self):
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1788 qcat = support.findfile("qcat.py", subdir="subprocessdata")
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1789 qgrep = support.findfile("qgrep.py", subdir="subprocessdata")
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1790
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1791 subdata = b'zxcvbn'
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1792 data = subdata * 4 + b'\n'
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1793
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1794 p1 = subprocess.Popen([sys.executable, qcat],
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1795 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1796 close_fds=False)
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1797
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1798 p2 = subprocess.Popen([sys.executable, qgrep, subdata],
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1799 stdin=p1.stdout, stdout=subprocess.PIPE,
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1800 close_fds=False)
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1801
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1802 self.addCleanup(p1.wait)
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1803 self.addCleanup(p2.wait)
74575
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1804 def kill_p1():
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1805 try:
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1806 p1.terminate()
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1807 except ProcessLookupError:
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1808 pass
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1809 def kill_p2():
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1810 try:
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1811 p2.terminate()
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1812 except ProcessLookupError:
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1813 pass
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1814 self.addCleanup(kill_p1)
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1815 self.addCleanup(kill_p2)
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1816
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1817 p1.stdin.write(data)
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1818 p1.stdin.close()
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1819
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1820 readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10)
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1821
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1822 self.assertTrue(readfiles, "The child hung")
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1823 self.assertEqual(p2.stdout.read(), data)
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1824
67303
77483f3e491a test_subprocess: close pipes at the end of test_pipe_cloexec_real_tools()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67130
diff changeset
1825 p1.stdout.close()
77483f3e491a test_subprocess: close pipes at the end of test_pipe_cloexec_real_tools()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67130
diff changeset
1826 p2.stdout.close()
77483f3e491a test_subprocess: close pipes at the end of test_pipe_cloexec_real_tools()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67130
diff changeset
1827
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1828 def test_close_fds(self):
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1829 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1830
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1831 fds = os.pipe()
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1832 self.addCleanup(os.close, fds[0])
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1833 self.addCleanup(os.close, fds[1])
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1834
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1835 open_fds = set(fds)
74563
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1836 # add a bunch more fds
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1837 for _ in range(9):
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1838 fd = os.open("/dev/null", os.O_RDONLY)
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1839 self.addCleanup(os.close, fd)
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1840 open_fds.add(fd)
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1841
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1842 for fd in open_fds:
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1843 os.set_inheritable(fd, True)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1844
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1845 p = subprocess.Popen([sys.executable, fd_status],
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1846 stdout=subprocess.PIPE, close_fds=False)
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1847 output, ignored = p.communicate()
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1848 remaining_fds = set(map(int, output.split(b',')))
66895
b066bd0b64e7 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66340
diff changeset
1849
66868
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1850 self.assertEqual(remaining_fds & open_fds, open_fds,
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1851 "Some fds were closed")
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1852
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1853 p = subprocess.Popen([sys.executable, fd_status],
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1854 stdout=subprocess.PIPE, close_fds=True)
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1855 output, ignored = p.communicate()
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1856 remaining_fds = set(map(int, output.split(b',')))
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1857
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1858 self.assertFalse(remaining_fds & open_fds,
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1859 "Some fds were left open")
de05d846795e issue7213: Open the pipes used by subprocesses with the FD_CLOEXEC flag from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66867
diff changeset
1860 self.assertIn(1, remaining_fds, "Subprocess failed")
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1861
74563
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1862 # Keep some of the fd's we opened open in the subprocess.
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1863 # This tests _posixsubprocess.c's proper handling of fds_to_keep.
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1864 fds_to_keep = set(open_fds.pop() for _ in range(8))
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1865 p = subprocess.Popen([sys.executable, fd_status],
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1866 stdout=subprocess.PIPE, close_fds=True,
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1867 pass_fds=())
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1868 output, ignored = p.communicate()
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1869 remaining_fds = set(map(int, output.split(b',')))
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1870
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1871 self.assertFalse(remaining_fds & fds_to_keep & open_fds,
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1872 "Some fds not in pass_fds were left open")
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1873 self.assertIn(1, remaining_fds, "Subprocess failed")
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1874
70575
22457ac2c2df Close #12230: Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70554
diff changeset
1875 # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
22457ac2c2df Close #12230: Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70554
diff changeset
1876 # descriptor of a pipe closed in the parent process is valid in the
22457ac2c2df Close #12230: Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70554
diff changeset
1877 # child process according to fstat(), but the mode of the file
22457ac2c2df Close #12230: Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70554
diff changeset
1878 # descriptor is invalid, and read or write raise an error.
22457ac2c2df Close #12230: Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70554
diff changeset
1879 @support.requires_mac_ver(10, 5)
66889
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1880 def test_pass_fds(self):
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1881 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1882
66889
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1883 open_fds = set()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1884
66889
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1885 for x in range(5):
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1886 fds = os.pipe()
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1887 self.addCleanup(os.close, fds[0])
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1888 self.addCleanup(os.close, fds[1])
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1889 os.set_inheritable(fds[0], True)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1890 os.set_inheritable(fds[1], True)
66889
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1891 open_fds.update(fds)
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1892
66889
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1893 for fd in open_fds:
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1894 p = subprocess.Popen([sys.executable, fd_status],
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1895 stdout=subprocess.PIPE, close_fds=True,
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1896 pass_fds=(fd, ))
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1897 output, ignored = p.communicate()
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1898
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1899 remaining_fds = set(map(int, output.split(b',')))
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1900 to_be_closed = open_fds - {fd}
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1901
66889
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1902 self.assertIn(fd, remaining_fds, "fd to be passed not passed")
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1903 self.assertFalse(remaining_fds & to_be_closed,
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1904 "fd to be closed passed")
66895
b066bd0b64e7 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66340
diff changeset
1905
66889
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1906 # pass_fds overrides close_fds with a warning.
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1907 with self.assertWarns(RuntimeWarning) as context:
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1908 self.assertFalse(subprocess.call(
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1909 [sys.executable, "-c", "import sys; sys.exit(0)"],
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1910 close_fds=False, pass_fds=(fd, )))
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1911 self.assertIn('overriding close_fds', str(context.warning))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1912
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1913 def test_pass_fds_inheritable(self):
85491
c27527dce71e Issue #18571: Merge duplicate test code
Victor Stinner <victor.stinner@gmail.com>
parents: 85467
diff changeset
1914 script = support.findfile("fd_status.py", subdir="subprocessdata")
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1915
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1916 inheritable, non_inheritable = os.pipe()
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1917 self.addCleanup(os.close, inheritable)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1918 self.addCleanup(os.close, non_inheritable)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1919 os.set_inheritable(inheritable, True)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1920 os.set_inheritable(non_inheritable, False)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1921 pass_fds = (inheritable, non_inheritable)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1922 args = [sys.executable, script]
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1923 args += list(map(str, pass_fds))
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1924
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1925 p = subprocess.Popen(args,
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1926 stdout=subprocess.PIPE, close_fds=True,
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1927 pass_fds=pass_fds)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1928 output, ignored = p.communicate()
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1929 fds = set(map(int, output.split(b',')))
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1930
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1931 # the inheritable file descriptor must be inherited, so its inheritable
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1932 # flag must be set in the child process after fork() and before exec()
85491
c27527dce71e Issue #18571: Merge duplicate test code
Victor Stinner <victor.stinner@gmail.com>
parents: 85467
diff changeset
1933 self.assertEqual(fds, set(pass_fds), "output=%a" % output)
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1934
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1935 # inheritable flag must not be changed in the parent process
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1936 self.assertEqual(os.get_inheritable(inheritable), True)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1937 self.assertEqual(os.get_inheritable(non_inheritable), False)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 85392
diff changeset
1938
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1939 def test_stdout_stdin_are_single_inout_fd(self):
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1940 with io.open(os.devnull, "r+") as inout:
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1941 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1942 stdout=inout, stdin=inout)
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1943 p.wait()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1944
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1945 def test_stdout_stderr_are_single_inout_fd(self):
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1946 with io.open(os.devnull, "r+") as inout:
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1947 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1948 stdout=inout, stderr=inout)
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1949 p.wait()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1950
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1951 def test_stderr_stdin_are_single_inout_fd(self):
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1952 with io.open(os.devnull, "r+") as inout:
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1953 p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1954 stderr=inout, stdin=inout)
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1955 p.wait()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1956
66893
767420808a62 Issue #1731717: Fixed the problem where subprocess.wait() could cause an
Gregory P. Smith <greg@mad-scientist.com>
parents: 66889
diff changeset
1957 def test_wait_when_sigchild_ignored(self):
767420808a62 Issue #1731717: Fixed the problem where subprocess.wait() could cause an
Gregory P. Smith <greg@mad-scientist.com>
parents: 66889
diff changeset
1958 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
767420808a62 Issue #1731717: Fixed the problem where subprocess.wait() could cause an
Gregory P. Smith <greg@mad-scientist.com>
parents: 66889
diff changeset
1959 sigchild_ignore = support.findfile("sigchild_ignore.py",
767420808a62 Issue #1731717: Fixed the problem where subprocess.wait() could cause an
Gregory P. Smith <greg@mad-scientist.com>
parents: 66889
diff changeset
1960 subdir="subprocessdata")
767420808a62 Issue #1731717: Fixed the problem where subprocess.wait() could cause an
Gregory P. Smith <greg@mad-scientist.com>
parents: 66889
diff changeset
1961 p = subprocess.Popen([sys.executable, sigchild_ignore],
767420808a62 Issue #1731717: Fixed the problem where subprocess.wait() could cause an
Gregory P. Smith <greg@mad-scientist.com>
parents: 66889
diff changeset
1962 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
767420808a62 Issue #1731717: Fixed the problem where subprocess.wait() could cause an
Gregory P. Smith <greg@mad-scientist.com>
parents: 66889
diff changeset
1963 stdout, stderr = p.communicate()
767420808a62 Issue #1731717: Fixed the problem where subprocess.wait() could cause an
Gregory P. Smith <greg@mad-scientist.com>
parents: 66889
diff changeset
1964 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
66896
12381f19e2ad Fix "BytesWarning: str() on a bytes instance"
Gregory P. Smith <greg@mad-scientist.com>
parents: 66893
diff changeset
1965 " non-zero with this error:\n%s" %
68142
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 68088
diff changeset
1966 stderr.decode('utf-8'))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1967
68680
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1968 def test_select_unbuffered(self):
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1969 # Issue #11459: bufsize=0 should really set the pipes as
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1970 # unbuffered (and therefore let select() work properly).
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1971 select = support.import_module("select")
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1972 p = subprocess.Popen([sys.executable, "-c",
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1973 'import sys;'
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1974 'sys.stdout.write("apple")'],
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1975 stdout=subprocess.PIPE,
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1976 bufsize=0)
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1977 f = p.stdout
68981
72f6f5a29e7a Merge with 3.1
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68680 68980
diff changeset
1978 self.addCleanup(f.close)
68680
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1979 try:
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1980 self.assertEqual(f.read(4), b"appl")
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1981 self.assertIn(f, select.select([f], [], [], 0.0)[0])
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1982 finally:
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1983 p.wait()
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1984
71903
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1985 def test_zombie_fast_process_del(self):
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1986 # Issue #12650: on Unix, if Popen.__del__() was called before the
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1987 # process exited, it wouldn't be added to subprocess._active, and would
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1988 # remain a zombie.
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1989 # spawn a Popen, and delete its reference before it exits
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1990 p = subprocess.Popen([sys.executable, "-c",
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1991 'import sys, time;'
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1992 'time.sleep(0.2)'],
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1993 stdout=subprocess.PIPE,
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1994 stderr=subprocess.PIPE)
71913
f6b0c794495a Fix ResourceWarnings in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71910
diff changeset
1995 self.addCleanup(p.stdout.close)
f6b0c794495a Fix ResourceWarnings in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71910
diff changeset
1996 self.addCleanup(p.stderr.close)
71903
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1997 ident = id(p)
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1998 pid = p.pid
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1999 del p
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2000 # check that p is in the active processes list
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2001 self.assertIn(ident, [id(o) for o in subprocess._active])
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2002
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2003 def test_leak_fast_process_del_killed(self):
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2004 # Issue #12650: on Unix, if Popen.__del__() was called before the
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2005 # process exited, and the process got killed by a signal, it would never
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2006 # be removed from subprocess._active, which triggered a FD and memory
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2007 # leak.
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2008 # spawn a Popen, delete its reference and kill it
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2009 p = subprocess.Popen([sys.executable, "-c",
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2010 'import time;'
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2011 'time.sleep(3)'],
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2012 stdout=subprocess.PIPE,
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2013 stderr=subprocess.PIPE)
71913
f6b0c794495a Fix ResourceWarnings in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71910
diff changeset
2014 self.addCleanup(p.stdout.close)
f6b0c794495a Fix ResourceWarnings in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71910
diff changeset
2015 self.addCleanup(p.stderr.close)
71903
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2016 ident = id(p)
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2017 pid = p.pid
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2018 del p
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2019 os.kill(pid, signal.SIGKILL)
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2020 # check that p is in the active processes list
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2021 self.assertIn(ident, [id(o) for o in subprocess._active])
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2022
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2023 # let some time for the process to exit, and create a new Popen: this
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2024 # should trigger the wait() of p
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2025 time.sleep(0.2)
80922
061e9a439f54 Get rig of EnvironmentError (#16705)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80392
diff changeset
2026 with self.assertRaises(OSError) as c:
71903
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2027 with subprocess.Popen(['nonexisting_i_hope'],
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2028 stdout=subprocess.PIPE,
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2029 stderr=subprocess.PIPE) as proc:
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2030 pass
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2031 # p should have been wait()ed on, and removed from the _active list
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2032 self.assertRaises(OSError, os.waitpid, pid, 0)
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2033 self.assertNotIn(ident, [id(o) for o in subprocess._active])
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
2034
85391
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2035 def test_close_fds_after_preexec(self):
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2036 fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2037
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2038 # this FD is used as dup2() target by preexec_fn, and should be closed
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2039 # in the child process
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2040 fd = os.dup(1)
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2041 self.addCleanup(os.close, fd)
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2042
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2043 p = subprocess.Popen([sys.executable, fd_status],
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2044 stdout=subprocess.PIPE, close_fds=True,
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2045 preexec_fn=lambda: os.dup2(1, fd))
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2046 output, ignored = p.communicate()
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2047
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2048 remaining_fds = set(map(int, output.split(b',')))
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2049
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2050 self.assertNotIn(fd, remaining_fds)
4c52d4bac03c Issue #18763: subprocess: The file descriptors are now closed after calling the
Charles-François Natali <cf.natali@gmail.com>
parents: 84980
diff changeset
2051
66895
b066bd0b64e7 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66340
diff changeset
2052
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
2053 @unittest.skipUnless(mswindows, "Windows specific tests")
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
2054 class Win32ProcessTestCase(BaseTestCase):
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
2055
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2056 def test_startupinfo(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2057 # startupinfo argument
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2058 # We uses hardcoded constants, because we do not want to
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2059 # depend on win32all.
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2060 STARTF_USESHOWWINDOW = 1
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2061 SW_MAXIMIZE = 3
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2062 startupinfo = subprocess.STARTUPINFO()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2063 startupinfo.dwFlags = STARTF_USESHOWWINDOW
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2064 startupinfo.wShowWindow = SW_MAXIMIZE
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2065 # Since Python is a console process, it won't be affected
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2066 # by wShowWindow, but the argument should be silently
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2067 # ignored
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2068 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2069 startupinfo=startupinfo)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2070
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2071 def test_creationflags(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2072 # creationflags argument
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2073 CREATE_NEW_CONSOLE = 16
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2074 sys.stderr.write(" a DOS box should flash briefly ...\n")
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2075 subprocess.call(sys.executable +
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2076 ' -c "import time; time.sleep(0.25)"',
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2077 creationflags=CREATE_NEW_CONSOLE)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2078
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2079 def test_invalid_args(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2080 # invalid arguments should raise ValueError
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2081 self.assertRaises(ValueError, subprocess.call,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2082 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2083 "import sys; sys.exit(47)"],
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2084 preexec_fn=lambda: 1)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2085 self.assertRaises(ValueError, subprocess.call,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2086 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2087 "import sys; sys.exit(47)"],
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2088 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2089 close_fds=True)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2090
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2091 def test_close_fds(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2092 # close file descriptors
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2093 rc = subprocess.call([sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2094 "import sys; sys.exit(47)"],
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2095 close_fds=True)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2096 self.assertEqual(rc, 47)
41969
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41845
diff changeset
2097
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2098 def test_shell_sequence(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2099 # Run command through the shell (sequence)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2100 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2101 newenv["FRUIT"] = "physalis"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2102 p = subprocess.Popen(["set"], shell=1,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2103 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2104 env=newenv)
65977
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
2105 self.addCleanup(p.stdout.close)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2106 self.assertIn(b"physalis", p.stdout.read())
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2107
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2108 def test_shell_string(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2109 # Run command through the shell (string)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2110 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2111 newenv["FRUIT"] = "physalis"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2112 p = subprocess.Popen("set", shell=1,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2113 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2114 env=newenv)
65977
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
2115 self.addCleanup(p.stdout.close)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2116 self.assertIn(b"physalis", p.stdout.read())
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2117
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2118 def test_call_string(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2119 # call() function with string argument on Windows
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2120 rc = subprocess.call(sys.executable +
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2121 ' -c "import sys; sys.exit(47)"')
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2122 self.assertEqual(rc, 47)
46505
63965327b3ef Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44810
diff changeset
2123
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
2124 def _kill_process(self, method, *args):
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
2125 # Some win32 buildbot raises EOFError if stdin is inherited
64927
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2126 p = subprocess.Popen([sys.executable, "-c", """if 1:
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2127 import sys, time
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2128 sys.stdout.write('x\\n')
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2129 sys.stdout.flush()
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2130 time.sleep(30)
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2131 """],
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2132 stdin=subprocess.PIPE,
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2133 stdout=subprocess.PIPE,
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2134 stderr=subprocess.PIPE)
65977
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
2135 self.addCleanup(p.stdout.close)
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
2136 self.addCleanup(p.stderr.close)
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
2137 self.addCleanup(p.stdin.close)
64927
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2138 # Wait for the interpreter to be completely initialized before
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2139 # sending any signal.
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2140 p.stdout.read(1)
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2141 getattr(p, method)(*args)
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
2142 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
2143 self.assertStderrEqual(stderr, b'')
64927
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2144 returncode = p.wait()
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
2145 self.assertNotEqual(returncode, 0)
46505
63965327b3ef Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44810
diff changeset
2146
75524
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2147 def _kill_dead_process(self, method, *args):
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2148 p = subprocess.Popen([sys.executable, "-c", """if 1:
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2149 import sys, time
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2150 sys.stdout.write('x\\n')
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2151 sys.stdout.flush()
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2152 sys.exit(42)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2153 """],
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2154 stdin=subprocess.PIPE,
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2155 stdout=subprocess.PIPE,
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2156 stderr=subprocess.PIPE)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2157 self.addCleanup(p.stdout.close)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2158 self.addCleanup(p.stderr.close)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2159 self.addCleanup(p.stdin.close)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2160 # Wait for the interpreter to be completely initialized before
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2161 # sending any signal.
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2162 p.stdout.read(1)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2163 # The process should end after this
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2164 time.sleep(1)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2165 # This shouldn't raise even though the child is now dead
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2166 getattr(p, method)(*args)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2167 _, stderr = p.communicate()
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2168 self.assertStderrEqual(stderr, b'')
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2169 rc = p.wait()
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2170 self.assertEqual(rc, 42)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2171
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2172 def test_send_signal(self):
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
2173 self._kill_process('send_signal', signal.SIGTERM)
46505
63965327b3ef Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44810
diff changeset
2174
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2175 def test_kill(self):
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
2176 self._kill_process('kill')
46505
63965327b3ef Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44810
diff changeset
2177
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2178 def test_terminate(self):
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
2179 self._kill_process('terminate')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2180
75524
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2181 def test_send_signal_dead(self):
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2182 self._kill_dead_process('send_signal', signal.SIGTERM)
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2183
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2184 def test_kill_dead(self):
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2185 self._kill_dead_process('kill')
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2186
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2187 def test_terminate_dead(self):
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2188 self._kill_dead_process('terminate')
41b1fe5a75a6 Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75075
diff changeset
2189
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2190 class CommandTests(unittest.TestCase):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2191 def test_getoutput(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2192 self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2193 self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2194 (0, 'xyzzy'))
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
2195
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2196 # we use mkdtemp in the next line to create an empty directory
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2197 # under our exclusive control; from that, we can invent a pathname
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2198 # that we _know_ won't exist. This is guaranteed to fail.
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2199 dir = None
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2200 try:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2201 dir = tempfile.mkdtemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2202 name = os.path.join(dir, "foo")
86879
c34e163c0086 Issue #10197 Rework subprocess.get[status]output to use subprocess functionality and thus to work on Windows. Patch by Nick Coghlan.
Tim Golden <mail@timgolden.me.uk>
parents: 85466
diff changeset
2203 status, output = subprocess.getstatusoutput(
c34e163c0086 Issue #10197 Rework subprocess.get[status]output to use subprocess functionality and thus to work on Windows. Patch by Nick Coghlan.
Tim Golden <mail@timgolden.me.uk>
parents: 85466
diff changeset
2204 ("type " if mswindows else "cat ") + name)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2205 self.assertNotEqual(status, 0)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2206 finally:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2207 if dir is not None:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2208 os.rmdir(dir)
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
2209
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
2210
87008
71b618f0c8e9 Issue #18923: Update subprocess to use the new selectors module.
Charles-François Natali <cf.natali@gmail.com>
parents: 86880
diff changeset
2211 @unittest.skipUnless(hasattr(selectors, 'PollSelector'),
71b618f0c8e9 Issue #18923: Update subprocess to use the new selectors module.
Charles-François Natali <cf.natali@gmail.com>
parents: 86880
diff changeset
2212 "Test needs selectors.PollSelector")
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2213 class ProcessTestCaseNoPoll(ProcessTestCase):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2214 def setUp(self):
87008
71b618f0c8e9 Issue #18923: Update subprocess to use the new selectors module.
Charles-François Natali <cf.natali@gmail.com>
parents: 86880
diff changeset
2215 self.orig_selector = subprocess._PopenSelector
71b618f0c8e9 Issue #18923: Update subprocess to use the new selectors module.
Charles-François Natali <cf.natali@gmail.com>
parents: 86880
diff changeset
2216 subprocess._PopenSelector = selectors.SelectSelector
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2217 ProcessTestCase.setUp(self)
63930
8499ae5d9719 #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63843
diff changeset
2218
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2219 def tearDown(self):
87008
71b618f0c8e9 Issue #18923: Update subprocess to use the new selectors module.
Charles-François Natali <cf.natali@gmail.com>
parents: 86880
diff changeset
2220 subprocess._PopenSelector = self.orig_selector
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2221 ProcessTestCase.tearDown(self)
63930
8499ae5d9719 #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63843
diff changeset
2222
8499ae5d9719 #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63843
diff changeset
2223
59209
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2224 class HelperFunctionTests(unittest.TestCase):
59217
d1c62882c73b Merged revisions 78533 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59209
diff changeset
2225 @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
59209
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2226 def test_eintr_retry_call(self):
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2227 record_calls = []
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2228 def fake_os_func(*args):
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2229 record_calls.append(args)
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2230 if len(record_calls) == 2:
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2231 raise OSError(errno.EINTR, "fake interrupted system call")
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2232 return tuple(reversed(args))
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2233
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2234 self.assertEqual((999, 256),
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2235 subprocess._eintr_retry_call(fake_os_func, 256, 999))
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2236 self.assertEqual([(256, 999)], record_calls)
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2237 # This time there will be an EINTR so it will loop once.
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2238 self.assertEqual((666,),
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2239 subprocess._eintr_retry_call(fake_os_func, 666))
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2240 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2241
59207
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
2242
63922
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2243 @unittest.skipUnless(mswindows, "Windows-specific tests")
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2244 class CommandsWithSpaces (BaseTestCase):
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2245
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2246 def setUp(self):
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2247 super().setUp()
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2248 f, fname = mkstemp(".py", "te st")
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2249 self.fname = fname.lower ()
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2250 os.write(f, b"import sys;"
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2251 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2252 )
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2253 os.close(f)
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2254
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2255 def tearDown(self):
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2256 os.remove(self.fname)
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2257 super().tearDown()
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2258
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2259 def with_spaces(self, *args, **kwargs):
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2260 kwargs['stdout'] = subprocess.PIPE
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2261 p = subprocess.Popen(*args, **kwargs)
65977
fe8962fc0e11 Fix a number of ResourceWarnings on Windows due to open pipes.
Brian Curtin <brian.curtin@gmail.com>
parents: 65955
diff changeset
2262 self.addCleanup(p.stdout.close)
63922
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2263 self.assertEqual(
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2264 p.stdout.read ().decode("mbcs"),
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2265 "2 [%r, 'ab cd']" % self.fname
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2266 )
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2267
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2268 def test_shell_string_with_spaces(self):
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2269 # call() function with string argument with spaces on Windows
63956
aa9c8e82c435 Fix #9588. Add sys.executable to two shell=True tests.
Brian Curtin <brian.curtin@gmail.com>
parents: 63922
diff changeset
2270 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
aa9c8e82c435 Fix #9588. Add sys.executable to two shell=True tests.
Brian Curtin <brian.curtin@gmail.com>
parents: 63922
diff changeset
2271 "ab cd"), shell=1)
63922
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2272
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2273 def test_shell_sequence_with_spaces(self):
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2274 # call() function with sequence argument with spaces on Windows
63956
aa9c8e82c435 Fix #9588. Add sys.executable to two shell=True tests.
Brian Curtin <brian.curtin@gmail.com>
parents: 63922
diff changeset
2275 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
63922
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2276
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2277 def test_noshell_string_with_spaces(self):
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2278 # call() function with string argument with spaces on Windows
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2279 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2280 "ab cd"))
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2281
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2282 def test_noshell_sequence_with_spaces(self):
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2283 # call() function with sequence argument with spaces on Windows
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2284 self.with_spaces([sys.executable, self.fname, "ab cd"])
56066a5825ca #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63840
diff changeset
2285
66622
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2286
75075
61b3f1582588 Fix bad inheritance in test_subprocess that led to a number of tests being executed twice.
Georg Brandl <georg@python.org>
parents: 74575
diff changeset
2287 class ContextManagerTests(BaseTestCase):
66622
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2288
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2289 def test_pipe(self):
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2290 with subprocess.Popen([sys.executable, "-c",
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2291 "import sys;"
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2292 "sys.stdout.write('stdout');"
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2293 "sys.stderr.write('stderr');"],
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2294 stdout=subprocess.PIPE,
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2295 stderr=subprocess.PIPE) as proc:
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2296 self.assertEqual(proc.stdout.read(), b"stdout")
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2297 self.assertStderrEqual(proc.stderr.read(), b"stderr")
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2298
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2299 self.assertTrue(proc.stdout.closed)
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2300 self.assertTrue(proc.stderr.closed)
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2301
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2302 def test_returncode(self):
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2303 with subprocess.Popen([sys.executable, "-c",
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2304 "import sys; sys.exit(100)"]) as proc:
70049
b00a64a5cb93 merge - 7a3f3ad83676 Fixes Issue #12044.
Gregory P. Smith <greg@krypto.org>
parents: 69155
diff changeset
2305 pass
b00a64a5cb93 merge - 7a3f3ad83676 Fixes Issue #12044.
Gregory P. Smith <greg@krypto.org>
parents: 69155
diff changeset
2306 # __exit__ calls wait(), so the returncode should be set
66622
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2307 self.assertEqual(proc.returncode, 100)
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2308
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2309 def test_communicate_stdin(self):
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2310 with subprocess.Popen([sys.executable, "-c",
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2311 "import sys;"
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2312 "sys.exit(sys.stdin.read() == 'context')"],
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2313 stdin=subprocess.PIPE) as proc:
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2314 proc.communicate(b"context")
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2315 self.assertEqual(proc.returncode, 1)
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2316
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2317 def test_invalid_args(self):
81076
5d172190c37c fix test for subprocess (#16644)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80391 81075
diff changeset
2318 with self.assertRaises(FileNotFoundError) as c:
66622
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2319 with subprocess.Popen(['nonexisting_i_hope'],
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2320 stdout=subprocess.PIPE,
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2321 stderr=subprocess.PIPE) as proc:
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2322 pass
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2323
59209
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2324
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2325 def test_main():
68527
8c79e3abdb31 revert the test_main() change from 08daf3ef6509 so that regrtest continues to run this properly.
Gregory P. Smith <greg@krypto.org>
parents: 68526
diff changeset
2326 unit_tests = (ProcessTestCase,
8c79e3abdb31 revert the test_main() change from 08daf3ef6509 so that regrtest continues to run this properly.
Gregory P. Smith <greg@krypto.org>
parents: 68526
diff changeset
2327 POSIXProcessTestCase,
8c79e3abdb31 revert the test_main() change from 08daf3ef6509 so that regrtest continues to run this properly.
Gregory P. Smith <greg@krypto.org>
parents: 68526
diff changeset
2328 Win32ProcessTestCase,
8c79e3abdb31 revert the test_main() change from 08daf3ef6509 so that regrtest continues to run this properly.
Gregory P. Smith <greg@krypto.org>
parents: 68526
diff changeset
2329 CommandTests,
8c79e3abdb31 revert the test_main() change from 08daf3ef6509 so that regrtest continues to run this properly.
Gregory P. Smith <greg@krypto.org>
parents: 68526
diff changeset
2330 ProcessTestCaseNoPoll,
8c79e3abdb31 revert the test_main() change from 08daf3ef6509 so that regrtest continues to run this properly.
Gregory P. Smith <greg@krypto.org>
parents: 68526
diff changeset
2331 HelperFunctionTests,
8c79e3abdb31 revert the test_main() change from 08daf3ef6509 so that regrtest continues to run this properly.
Gregory P. Smith <greg@krypto.org>
parents: 68526
diff changeset
2332 CommandsWithSpaces,
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
2333 ContextManagerTests,
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
2334 )
68527
8c79e3abdb31 revert the test_main() change from 08daf3ef6509 so that regrtest continues to run this properly.
Gregory P. Smith <greg@krypto.org>
parents: 68526
diff changeset
2335
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
2336 support.run_unittest(*unit_tests)
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
2337 support.reap_children()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2338
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2339 if __name__ == "__main__":
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
2340 unittest.main()