annotate Lib/test/test_subprocess.py @ 84007:dbfbdf2b5c19 3.3

Fix #17967: For ftp urls CWD to target instead of hopping to each directory towards target. This fixes a bug where target is accessible, but parent directories are restricted.
author Senthil Kumaran <senthil@uthcode.com>
date Sat, 01 Jun 2013 08:27:06 -0700
parents 256b8dc71046
children 46fe1bb0723c 79bf4e7e4004
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
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
14 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
15 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
16 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
17 import shutil
73925
6040c248a199 we're always going to have gc
Benjamin Peterson <benjamin@python.org>
parents: 73923
diff changeset
18 import gc
78652
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
19 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
20
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
21 try:
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
22 import resource
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
23 except ImportError:
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
24 resource = None
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
25
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
26 mswindows = (sys.platform == "win32")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
27
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 # Depends on the following external programs: Python
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
30 #
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 if mswindows:
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
33 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
34 'os.O_BINARY);')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
35 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
36 SETBINARY = ''
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
37
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
38
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
39 try:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
40 mkstemp = tempfile.mkstemp
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
41 except AttributeError:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
42 # 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
43 def mkstemp():
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
44 """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
45 fname = tempfile.mktemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
46 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
47
33739
76355363101b New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents: 33738
diff changeset
48
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
49 class BaseTestCase(unittest.TestCase):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
50 def setUp(self):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
51 # 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
52 # 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
53 support.reap_children()
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
54
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
55 def tearDown(self):
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
56 for inst in subprocess._active:
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
57 inst.wait()
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
58 subprocess._cleanup()
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
59 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
60
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
61 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
62 # 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
63 # 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
64 # 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
65 actual = support.strip_python_stderr(stderr)
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
66 # 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
67 expected = expected.strip()
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
68 self.assertEqual(actual, expected, msg)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
69
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
70
80368
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
71 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
72 pass
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
73
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 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
76 """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
77 _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
78 """
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
79 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
80 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
81
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
82
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
83 class ProcessTestCase(BaseTestCase):
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
84
82916
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
85 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
86 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
87 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
88 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
89 try:
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
90 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
91 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
92 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
93 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
94 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
95 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
96 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
97 p.wait()
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
98
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
99 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
100 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
101 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
102 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
103 try:
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
104 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
105 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
106 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
107 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
108 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
109 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
110 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
111 p.wait()
3ecf709dfe69 Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
Gregory P. Smith <greg@krypto.org>
parents: 82595
diff changeset
112
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
113 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
114 # call() function with sequence argument
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
115 rc = subprocess.call([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
116 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
117 self.assertEqual(rc, 47)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
118
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
119 def test_call_timeout(self):
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
120 # 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
121 # 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
122 # 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
123 # child.
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
124 self.assertRaises(subprocess.TimeoutExpired, subprocess.call,
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
125 [sys.executable, "-c", "while True: pass"],
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
126 timeout=0.1)
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
127
34302
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
128 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
129 # 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
130 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
131 "import sys; sys.exit(0)"])
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
132 self.assertEqual(rc, 0)
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
133
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
134 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
135 # 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
136 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
137 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
138 "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
139 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
140
50074
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
141 def test_check_output(self):
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
142 # 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
143 output = subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
144 [sys.executable, "-c", "print('BDFL')"])
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55626
diff changeset
145 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
146
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
147 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
148 # 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
149 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
150 subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
151 [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
152 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
153
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
154 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
155 # 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
156 output = subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
157 [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
158 stderr=subprocess.STDOUT)
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55626
diff changeset
159 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
160
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
161 def test_check_output_stdout_arg(self):
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
162 # check_output() function stderr redirected to stdout
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
163 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
164 output = subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
165 [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
166 stdout=sys.stdout)
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
167 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
168 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
169
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
170 def test_check_output_timeout(self):
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
171 # check_output() function with timeout arg
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
172 with self.assertRaises(subprocess.TimeoutExpired) as c:
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
173 output = subprocess.check_output(
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
174 [sys.executable, "-c",
70693
509622ce587a Use a sleep for test_subprocess timeout test
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70576
diff changeset
175 "import sys, time\n"
509622ce587a Use a sleep for test_subprocess timeout test
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70576
diff changeset
176 "sys.stdout.write('BDFL')\n"
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
177 "sys.stdout.flush()\n"
70693
509622ce587a Use a sleep for test_subprocess timeout test
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70576
diff changeset
178 "time.sleep(3600)"],
68619
2e4879d44604 Bump up the subprocess timeouts even more. :(
Reid Kleckner <reid@kleckner.net>
parents: 68618
diff changeset
179 # 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
180 # this much time to start and print.
2e4879d44604 Bump up the subprocess timeouts even more. :(
Reid Kleckner <reid@kleckner.net>
parents: 68618
diff changeset
181 timeout=3)
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
182 self.fail("Expected TimeoutExpired.")
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
183 self.assertEqual(c.exception.output, b'BDFL')
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
184
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
185 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
186 # call() function with keyword args
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
187 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
188 newenv["FRUIT"] = "banana"
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
189 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
190 'import sys, os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
191 '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
192 env=newenv)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
193 self.assertEqual(rc, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
194
70554
0f2714e49583 Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70465
diff changeset
195 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
196 # 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
197 # 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
198 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
199 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
200 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
201 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
202 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
203 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
204
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
205 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
206 # .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
207 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
208 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
209 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
210 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
211 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
212 self.assertEqual(p.stdin, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
213
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
214 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
215 # .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
216 # 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
217 # 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
218 # this_test
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
219 # \-- 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
220 # \-- 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
221 # 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
222 # 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
223 # 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
224 # 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
225 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
226 '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
227 ' 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
228 '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
229 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
230 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
231 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
232 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
233 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
234 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
235 self.assertEqual(out.rstrip(), b'test_stdout_none')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
236
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
237 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
238 # .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
239 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
240 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
241 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
242 self.addCleanup(p.stdin.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
243 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
244 self.assertEqual(p.stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
245
79598
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
246 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
247 # 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
248 # 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
249 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
250 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
251 p.wait()
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
252 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
253
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
254 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
255 # 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
256 #
9451908da615 Issue #16170: Remove Linux skip from test_subprocess's test_executable test.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79600
diff changeset
257 # 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
258 # 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
259 # 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
260 # 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
261 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
262 "doesnotexist")
9451908da615 Issue #16170: Remove Linux skip from test_subprocess's test_executable test.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79600
diff changeset
263 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
264
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
265 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
266 # 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
267 #
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
268 # 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
269 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
270 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
271 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
272 executable="doesnotexist")
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
273
862430c68fec Issue #16115: Improve testing of the executable argument to subprocess.Popen().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 79527
diff changeset
274 @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
275 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
276 # 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
277 # 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
278 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
279
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
280 # 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
281 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
282 # 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
283 # 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
284 # 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
285 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
286 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
287 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
288 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
289 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
290
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
291 # 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
292 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
293 # 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
294 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
295 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
296
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
297 # 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
298 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
299 # 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
300 # 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
301 # 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
302 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
303 "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
304 "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
305 "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
306 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
307 **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
308 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
309 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
310 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
311 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
312 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
313 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
314
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 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
316 # 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
317 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
318 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
319 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
320
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
321 @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
322 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
323 # 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
324 # 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
325 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
326 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
327 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
328 # 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
329 # 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
330 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
331 [rel_python])
79315
f66ff96f0030 Issue #15533: Merge fix from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78656 79314
diff changeset
332 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
333 [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
334 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
335 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
336
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
337 @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
338 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
339 # 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
340 # 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
341 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
342 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
343 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
344 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
345 # 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
346 # 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
347 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
348 [doesntexist], executable=rel_python)
79315
f66ff96f0030 Issue #15533: Merge fix from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78656 79314
diff changeset
349 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
350 [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
351 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
352 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
353 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
354 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
355
abfaa4368263 Issue #15533: Clarify docs and add tests for subprocess.Popen()'s cwd argument.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78655
diff changeset
356 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
357 # 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
358 # 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
359 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
360 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
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 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
363 # 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
364 # relative path fails.
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], 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
367 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
368 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
369
77154
294f8aeb4d4b Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75525
diff changeset
370 @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
371 'Test is not venv-compatible')
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
372 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
373 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
374 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
375 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
376 executable=sys.executable, cwd=python_dir)
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
377
77154
294f8aeb4d4b Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75525
diff changeset
378 @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
379 'Test is not venv-compatible')
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
380 @unittest.skipIf(sysconfig.is_python_build(),
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
381 "need an installed Python. See #7774")
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
382 def test_executable_without_cwd(self):
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
383 # 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
384 # argument. For test runs in the build directory, see #7774.
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 self._assert_cwd('', "somethingyoudonthave", executable=sys.executable)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
386
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
387 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
388 # stdin redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
389 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
390 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
391 stdin=subprocess.PIPE)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
392 p.stdin.write(b"pear")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
393 p.stdin.close()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
394 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
395 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
396
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
397 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
398 # stdin is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
399 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
400 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
401 d = tf.fileno()
49200
bdb660dca74d Issue #3782: os.write() must not accept unicode strings
Antoine Pitrou <solipsis@pitrou.net>
parents: 48395
diff changeset
402 os.write(d, b"pear")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
403 os.lseek(d, 0, 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
404 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
405 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
406 stdin=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
407 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
408 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
409
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
410 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
411 # stdin is set to open file object
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
412 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
413 self.addCleanup(tf.close)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
414 tf.write(b"pear")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
415 tf.seek(0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
416 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
417 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
418 stdin=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
419 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
420 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
421
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
422 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
423 # stdout redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
424 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
425 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
426 stdout=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
427 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
428 self.assertEqual(p.stdout.read(), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
429
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
430 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
431 # stdout is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
432 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
433 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
434 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
435 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
436 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
437 stdout=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
438 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
439 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
440 self.assertEqual(os.read(d, 1024), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
441
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
442 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
443 # stdout is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
444 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
445 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
446 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
447 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
448 stdout=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
449 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
450 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
451 self.assertEqual(tf.read(), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
452
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
453 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
454 # stderr redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
455 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
456 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
457 stderr=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
458 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
459 self.assertStderrEqual(p.stderr.read(), b"strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
460
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
461 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
462 # stderr is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
463 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
464 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
465 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
466 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
467 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
468 stderr=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
469 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
470 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
471 self.assertStderrEqual(os.read(d, 1024), b"strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
472
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
473 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
474 # stderr is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
475 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
476 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
477 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
478 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
479 stderr=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
480 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
481 tf.seek(0)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
482 self.assertStderrEqual(tf.read(), b"strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
483
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
484 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
485 # capture stdout and stderr to the same pipe
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
486 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
487 'import sys;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
488 '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
489 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
490 '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
491 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
492 stderr=subprocess.STDOUT)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
493 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
494 self.assertStderrEqual(p.stdout.read(), b"appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
495
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
496 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
497 # 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
498 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
499 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
500 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
501 'import sys;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
502 '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
503 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
504 '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
505 stdout=tf,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
506 stderr=tf)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
507 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
508 tf.seek(0)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
509 self.assertStderrEqual(tf.read(), b"appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
510
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
511 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
512 # 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
513 # 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
514 # 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
515 # 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
516 # 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
517 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
518 '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
519 ' "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
520 '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
521 'assert rc == 18')
222505a5aeac #11963: remove human verification from test_parser and test_subprocess.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81997
diff changeset
522 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
523 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
524 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
525 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
526 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
527 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
528 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
529
68600
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
530 def test_stdout_devnull(self):
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
531 p = subprocess.Popen([sys.executable, "-c",
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
532 'for i in range(10240):'
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
533 'print("x" * 1024)'],
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
534 stdout=subprocess.DEVNULL)
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
535 p.wait()
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
536 self.assertEqual(p.stdout, None)
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
537
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
538 def test_stderr_devnull(self):
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
539 p = subprocess.Popen([sys.executable, "-c",
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
540 'import sys\n'
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
541 'for i in range(10240):'
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
542 'sys.stderr.write("x" * 1024)'],
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
543 stderr=subprocess.DEVNULL)
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
544 p.wait()
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
545 self.assertEqual(p.stderr, None)
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
546
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
547 def test_stdin_devnull(self):
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
548 p = subprocess.Popen([sys.executable, "-c",
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
549 'import sys;'
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
550 'sys.stdin.read(1)'],
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
551 stdin=subprocess.DEVNULL)
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
552 p.wait()
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
553 self.assertEqual(p.stdin, None)
eaf93e156dff Issue #5870: Add subprocess.DEVNULL constant.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68555
diff changeset
554
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
555 def test_env(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
556 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
557 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
558 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
559 '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
560 '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
561 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
562 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
563 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
564 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
565
70929
52c5f80122dd Issue #12383: skip test_empty_env() of subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70926
diff changeset
566 # 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
567 # Python
52c5f80122dd Issue #12383: skip test_empty_env() of subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70926
diff changeset
568 @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
569 '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
570 @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
571 '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
572 '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
573 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
574 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
575 '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
576 '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
577 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
578 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
579 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
580 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
581 (b"[]",
da3af4b131d7 Issue #12383: fix test_empty_env() of subprocess on Mac OS X
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70923
diff changeset
582 # 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
583 # environment
da3af4b131d7 Issue #12383: fix test_empty_env() of subprocess on Mac OS X
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70923
diff changeset
584 b"['__CF_USER_TEXT_ENCODING']"))
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
585
34671
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
586 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
587 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
588 'import sys;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
589 '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
590 stdin=subprocess.PIPE)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
591 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
592 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
593
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
594 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
595 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
596 '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
597 stdout=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
598 (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
599 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
600 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
601
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
602 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
603 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
604 '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
605 stderr=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
606 (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
607 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
608 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
609
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
610 def test_communicate(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
611 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
612 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
613 '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
614 '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
615 stdin=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
616 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
617 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
618 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
619 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
620 self.addCleanup(p.stdin.close)
48395
bb65f9b76c1f #2683: Popen.communicate() argument must be bytes.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
621 (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
622 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
623 self.assertStderrEqual(stderr, b"pineapple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
624
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
625 def test_communicate_timeout(self):
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
626 p = subprocess.Popen([sys.executable, "-c",
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
627 'import sys,os,time;'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
628 'sys.stderr.write("pineapple\\n");'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
629 'time.sleep(1);'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
630 'sys.stderr.write("pear\\n");'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
631 'sys.stdout.write(sys.stdin.read())'],
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
632 universal_newlines=True,
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
633 stdin=subprocess.PIPE,
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
634 stdout=subprocess.PIPE,
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
635 stderr=subprocess.PIPE)
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
636 self.assertRaises(subprocess.TimeoutExpired, p.communicate, "banana",
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
637 timeout=0.3)
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
638 # 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
639 # after it completes.
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
640 (stdout, stderr) = p.communicate()
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
641 self.assertEqual(stdout, "banana")
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
642 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
643
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
644 def test_communicate_timeout_large_ouput(self):
74886
b27a28a30612 Fix a typo.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74885
diff changeset
645 # 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
646 p = subprocess.Popen([sys.executable, "-c",
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
647 'import sys,os,time;'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
648 'sys.stdout.write("a" * (64 * 1024));'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
649 'time.sleep(0.2);'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
650 'sys.stdout.write("a" * (64 * 1024));'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
651 'time.sleep(0.2);'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
652 'sys.stdout.write("a" * (64 * 1024));'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
653 'time.sleep(0.2);'
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
654 'sys.stdout.write("a" * (64 * 1024));'],
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
655 stdout=subprocess.PIPE)
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
656 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
657 (stdout, _) = p.communicate()
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
658 self.assertEqual(len(stdout), 4 * 64 * 1024)
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
659
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
660 # 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
661 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
662 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
663 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
664 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
665 options = {}
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
666 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
667 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
668 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
669 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
670 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
671 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
672 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
673 continue
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
674 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
675 p.communicate()
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
676 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
677 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
678 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
679 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
680 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
681 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
682
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
683 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
684 # communicate() should return None if no redirection is active
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
685 p = subprocess.Popen([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
686 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
687 (stdout, stderr) = p.communicate()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
688 self.assertEqual(stdout, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
689 self.assertEqual(stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
690
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
691 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
692 # communicate() with writes larger than pipe_buf
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
693 # This test will probably deadlock rather than fail, if
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
694 # communicate() does not work properly.
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
695 x, y = os.pipe()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
696 os.close(x)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
697 os.close(y)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
698 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
699 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
700 '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
701 '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
702 '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
703 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
704 stdin=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
705 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
706 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
707 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
708 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
709 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
710 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
711 (stdout, stderr) = p.communicate(string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
712 self.assertEqual(stdout, string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
713
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
714 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
715 # stdin.write before communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
716 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
717 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
718 '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
719 stdin=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
720 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
721 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
722 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
723 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
724 self.addCleanup(p.stdin.close)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
725 p.stdin.write(b"banana")
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
726 (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
727 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
728 self.assertStderrEqual(stderr, b"")
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
729
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
730 def test_universal_newlines(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
731 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
732 '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
733 '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
734 '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
735 '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
736 '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
737 '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
738 '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
739 '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
740 '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
741 '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
742 '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
743 '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
744 '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
745 '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
746 '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
747 '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
748 '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
749 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
750 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
751 universal_newlines=1)
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
752 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
753 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
754 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
755 p.stdin.close()
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
756 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
757 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
758 "line2\n")
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
759 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
760 "line3\n")
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
761 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
762 "line4\nline5\nline6\nline7\nline8")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
763
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
764 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
765 # universal newlines through communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
766 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
767 '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
768 '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
769 '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
770 '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
771 '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
772 '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
773 '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
774 '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
775 '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
776 '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
777 '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
778 '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
779 '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
780 stderr=subprocess.PIPE,
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
781 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
782 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
783 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
784 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
785 (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
786 self.assertEqual(stdout,
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
787 "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
788
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
789 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
790 # 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
791 p = subprocess.Popen([sys.executable, "-c",
78652
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
792 'import sys,os;' + SETBINARY + textwrap.dedent('''
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
793 s = sys.stdin.readline()
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
794 assert s == "line1\\n", repr(s)
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
795 s = sys.stdin.read()
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
796 assert s == "line3\\n", repr(s)
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
797 ''')],
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
798 stdin=subprocess.PIPE,
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
799 universal_newlines=1)
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
800 (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
801 self.assertEqual(p.returncode, 0)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
802
78571
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
803 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
804 # 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
805 #
839bd8f98539 Add test to explicit check the absence regression in subprocess (issue #15592).
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78421
diff changeset
806 # 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
807 # 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
808 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
809 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
810 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
811 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
812 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
813 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
814
81997
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
815 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
816 # 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
817 p = subprocess.Popen([sys.executable, "-c",
78652
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
818 'import sys,os;' + SETBINARY + textwrap.dedent('''
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
819 s = sys.stdin.buffer.readline()
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
820 sys.stdout.buffer.write(s)
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
821 sys.stdout.buffer.write(b"line2\\r")
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
822 sys.stderr.buffer.write(b"eline2\\n")
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
823 s = sys.stdin.buffer.read()
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
824 sys.stdout.buffer.write(s)
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
825 sys.stdout.buffer.write(b"line4\\n")
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
826 sys.stdout.buffer.write(b"line5\\r\\n")
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
827 sys.stderr.buffer.write(b"eline6\\r")
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
828 sys.stderr.buffer.write(b"eline7\\r\\nz")
53fc878cc7b3 Brush subprocess tests a bit.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78611
diff changeset
829 ''')],
81997
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
830 stdin=subprocess.PIPE,
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
831 stderr=subprocess.PIPE,
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
832 stdout=subprocess.PIPE,
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
833 universal_newlines=True)
4206f91c974c Issue #16903: Popen.communicate() on Unix now accepts strings when
Serhiy Storchaka <storchaka@gmail.com>
parents: 81455
diff changeset
834 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
835 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
836 (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
837 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
838 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
839 # 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
840 # 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
841 # 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
842 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
843
78655
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
844 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
845 # 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
846 # 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
847 # See issue #15595.
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
848 #
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
849 # 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
850 # without, and UTF-16 and UTF-32.
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
851 for encoding in ['utf-16', 'utf-32-be']:
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
852 old_getpreferredencoding = locale.getpreferredencoding
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
853 # 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
854 # 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
855 # locale.getpreferredencoding().
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
856 def getpreferredencoding(do_setlocale=True):
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
857 return encoding
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
858 code = ("import sys; "
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
859 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
860 encoding)
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
861 args = [sys.executable, '-c', code]
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
862 try:
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
863 locale.getpreferredencoding = getpreferredencoding
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
864 # 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
865 # 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
866 # zero or one.
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
867 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
868 stdin=subprocess.PIPE,
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
869 stdout=subprocess.PIPE)
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
870 stdout, stderr = popen.communicate(input='')
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
871 finally:
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
872 locale.getpreferredencoding = old_getpreferredencoding
a0f7c2f79bce Issue #15595: Fix subprocess.Popen(universal_newlines=True)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78571
diff changeset
873 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
874
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
875 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
876 # Make sure we leak no resources
64814
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
877 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
878 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
879 else:
64814
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
880 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
881 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
882 tmpdir = tempfile.mkdtemp()
64814
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
883 try:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
884 for i in range(max_handles):
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
885 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
886 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
887 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
888 except OSError as e:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
889 if e.errno != errno.EMFILE:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
890 raise
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
891 break
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
892 else:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
893 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
894 "(tried %d)" % max_handles)
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
895 # 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
896 for i in range(10):
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
897 os.close(handles.pop())
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
898 # 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
899 # 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
900 for i in range(15):
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
901 p = subprocess.Popen([sys.executable, "-c",
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
902 "import sys;"
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
903 "sys.stdout.write(sys.stdin.read())"],
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
904 stdin=subprocess.PIPE,
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
905 stdout=subprocess.PIPE,
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
906 stderr=subprocess.PIPE)
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
907 data = p.communicate(b"lime")[0]
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
908 self.assertEqual(data, b"lime")
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
909 finally:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
910 for h in handles:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
911 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
912 shutil.rmtree(tmpdir)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
913
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
914 def test_list2cmdline(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
915 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
916 '"a b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
917 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
918 'ab\\"c \\ d')
44810
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44373
diff changeset
919 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44373
diff changeset
920 'ab\\"c " \\\\" d')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
921 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
922 'a\\\\\\b "de fg" h')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
923 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
924 'a\\\\\\"b c d')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
925 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
926 '"a\\\\b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
927 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
928 '"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
929 self.assertEqual(subprocess.list2cmdline(['ab', '']),
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
930 'ab ""')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
931
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
932 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
933 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
934 "import os; os.read(0, 1)"],
e5a94b56d6bc Fix sporadic test_subprocess regression introduced by 834650d63130.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 75097
diff changeset
935 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
936 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
937 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
938 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
939 p.wait()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
940 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
941 self.assertEqual(p.poll(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
942
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
943 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
944 p = subprocess.Popen([sys.executable, "-c", "pass"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
945 self.assertEqual(p.wait(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
946 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
947 self.assertEqual(p.wait(), 0)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
948
68454
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
949 def test_wait_timeout(self):
c4a0fa6e687c Add a 'timeout' argument to subprocess.Popen.
Reid Kleckner <reid@kleckner.net>
parents: 68438
diff changeset
950 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
951 "-c", "import time; time.sleep(0.3)"])
68618
a161081e8f7c Include the timeout value in TimeoutExpired.
Reid Kleckner <reid@kleckner.net>
parents: 68600
diff changeset
952 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
953 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
954 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
955 # 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
956 # time to start.
2e4879d44604 Bump up the subprocess timeouts even more. :(
Reid Kleckner <reid@kleckner.net>
parents: 68618
diff changeset
957 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
958
79d2ae0a62ef Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents: 33998
diff changeset
959 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
960 # 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
961 # TypeError.
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
962 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
963 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
964
41984
e420039368b7 The bufsize argument to Popen() should accept None meaning the default (0).
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
965 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
966 # 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
967 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
968 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
969 # 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
970 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
971 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
972
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
973 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
974 # 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
975 # 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
976 # 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
977 # 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
978 # 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
979 for i in range(1024):
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
980 # Windows raises IOError. Others raise OSError.
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
981 with self.assertRaises(EnvironmentError) 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
982 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
983 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
984 stderr=subprocess.PIPE)
68433
67f4ef6094ed Merge fix for #11490 from 3.1.
R David Murray <rdmurray@bitdance.com>
parents: 68257 68432
diff changeset
985 # ignore errors that indicate the command was not found
68437
f65aad1226b7 Fix whitespace
R David Murray <rdmurray@bitdance.com>
parents: 68433
diff changeset
986 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
987 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
988
61798
73416b1bbcd5 Issue #8780: Only backport the new test, the fix is not needed
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61014
diff changeset
989 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
990 # 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
991 # 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
992 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
993 '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
994 '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
995 "[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
996 '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
997 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
998 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
999
63774
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1000 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
1001 # 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
1002 # 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
1003 ifhandle, ifname = mkstemp()
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
1004 ofhandle, ofname = mkstemp()
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
1005 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
1006 try:
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1007 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
1008 stderr=efhandle)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1009 except OSError:
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1010 os.close(ifhandle)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1011 os.remove(ifname)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1012 os.close(ofhandle)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1013 os.remove(ofname)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1014 os.close(efhandle)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1015 os.remove(efname)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
1016 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
1017 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
1018 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
1019
69154
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1020 def test_communicate_epipe(self):
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1021 # 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
1022 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
1023 stdin=subprocess.PIPE,
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1024 stdout=subprocess.PIPE,
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1025 stderr=subprocess.PIPE)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1026 self.addCleanup(p.stdout.close)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1027 self.addCleanup(p.stderr.close)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1028 self.addCleanup(p.stdin.close)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1029 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
1030
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1031 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
1032 # 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
1033 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
1034 stdin=subprocess.PIPE)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1035 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
1036 p.wait()
69154
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
1037 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
1038
74885
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1039 @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
1040 "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
1041 @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
1042 "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
1043 @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
1044 "Requires os.getppid")
71228
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1045 def test_communicate_eintr(self):
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1046 # Issue #12493: communicate() should handle EINTR
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1047 def handler(signum, frame):
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1048 pass
74885
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1049 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
1050 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
1051
74885
834650d63130 Attempt to speed up some subprocess tests (and hopefully keep them reliable).
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 74576
diff changeset
1052 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
1053 '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
1054 'os.kill(os.getppid(), signal.SIGUSR1)']
71228
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1055 for stream in ('stdout', 'stderr'):
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1056 kw = {stream: subprocess.PIPE}
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1057 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
1058 # communicate() will be interrupted by SIGUSR1
71228
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1059 process.communicate()
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
1060
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1061
80368
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1062 # 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
1063 # 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
1064 @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
1065 "Linux specific")
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1066 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
1067 """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
1068 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
1069 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
1070 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
1071 PopenExecuteChildRaises(
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1072 [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
1073 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
1074
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1075 # 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
1076 # 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
1077 # 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
1078
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1079 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
1080 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
1081
63ff4c9a2ed2 Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79632
diff changeset
1082
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1083 # context manager
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1084 class _SuppressCoreFiles(object):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1085 """Try to prevent core files from being created."""
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1086 old_limit = None
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
1087
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1088 def __enter__(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1089 """Try to save previous ulimit, then set it to (0, 0)."""
73923
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
1090 if resource is not None:
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
1091 try:
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
1092 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
1093 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
1094 except (ValueError, resource.error):
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
1095 pass
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
1096
63141
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
1097 if sys.platform == 'darwin':
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
1098 # Check if the 'Crash Reporter' on OSX was configured
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
1099 # in 'Developer' mode and warn that it will get triggered
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
1100 # when it is.
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
1101 #
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
1102 # This assumes that this context manager is used in tests
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
1103 # that might trigger the next manager.
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
1104 value = subprocess.Popen(['/usr/bin/defaults', 'read',
63142
5a698a152f30 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63095
diff changeset
1105 'com.apple.CrashReporter', 'DialogType'],
5a698a152f30 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63095
diff changeset
1106 stdout=subprocess.PIPE).communicate()[0]
63141
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
1107 if value.strip() == b'developer':
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
1108 print("this tests triggers the Crash Reporter, "
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
1109 "that is intentional", end='')
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
1110 sys.stdout.flush()
63142
5a698a152f30 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63095
diff changeset
1111
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1112 def __exit__(self, *args):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1113 """Return core file behavior to default."""
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1114 if self.old_limit is None:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1115 return
73923
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
1116 if resource is not None:
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
1117 try:
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
1118 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
1119 except (ValueError, resource.error):
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
1120 pass
63142
5a698a152f30 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63095
diff changeset
1121
5a698a152f30 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63095
diff changeset
1122
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
1123 @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
1124 class POSIXProcessTestCase(BaseTestCase):
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
1125
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1126 def setUp(self):
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1127 super().setUp()
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1128 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
1129
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1130 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
1131 try:
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1132 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
1133 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
1134 # 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
1135 # 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
1136 # 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
1137 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
1138 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
1139 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
1140 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
1141 self._nonexistent_dir)
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1142 return desired_exception
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1143
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1144 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
1145 """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
1146 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
1147 try:
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1148 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
1149 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
1150 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
1151 # 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
1152 # 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
1153 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
1154 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
1155 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
1156 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
1157
79632
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1158 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
1159 """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
1160 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
1161 try:
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1162 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
1163 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
1164 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
1165 # 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
1166 # 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
1167 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
1168 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
1169 else:
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1170 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
1171
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1172 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
1173 """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
1174 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
1175 try:
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1176 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
1177 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
1178 # 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
1179 # 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
1180 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
1181 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
1182 else:
e938fa6be509 Fixes Issue #16114: The subprocess module no longer provides a
Gregory P. Smith <greg@krypto.org>
parents: 79526
diff changeset
1183 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
1184
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
1185 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
1186 # 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
1187 # 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
1188 # 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
1189 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
1190 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
1191
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1192 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
1193 # 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
1194 # 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
1195 # 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
1196 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
1197 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
1198 [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
1199 "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
1200 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
1201 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
1202 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
1203 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
1204 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
1205 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
1206 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
1207 self.assertNotEqual(parent_pgid, child_pgid)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1208
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1209 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
1210 # returncode handles signal termination
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1211 with _SuppressCoreFiles():
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1212 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
1213 'import os; os.abort()'])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1214 p.wait()
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1215 self.assertEqual(-p.returncode, signal.SIGABRT)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1216
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1217 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
1218 # 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
1219 # 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
1220 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
1221 'import sys,os;'
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1222 '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
1223 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1224 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
1225 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
1226 self.assertEqual(p.stdout.read(), b"apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1227
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
1228 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
1229 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
1230 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
1231 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
1232 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
1233 preexec_fn=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
1234 except RuntimeError 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
1235 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
1236 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
1237 "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
1238 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
1239 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
1240 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
1241 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
1242 "to the parent process.")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1243
80390
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1244 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
1245 """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
1246 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
1247 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
1248 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
1249
80390
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1250 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
1251 try:
80390
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1252 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
1253 finally:
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1254 # 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
1255 # 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
1256 # 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
1257 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
1258 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
1259 try:
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1260 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
1261 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
1262 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
1263 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
1264 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
1265 finally:
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1266 map(os.close, devzero_fds)
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1267
80390
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1268 @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
1269 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
1270 """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
1271
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1272 def raise_it():
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1273 raise RuntimeError("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
1274
6ebfdfe74c04 Fixes issue #16140: The subprocess module no longer double closes its
Gregory P. Smith <greg@krypto.org>
parents: 80368
diff changeset
1275 with self.assertRaises(RuntimeError):
80390
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1276 self._TestExecuteChildPopen(
d09b6cb719b5 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80379
diff changeset
1277 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
1278 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
1279 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
1280
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
1281 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
1282 # 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
1283 # 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
1284 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
1285 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
1286 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
1287 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
1288 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
1289 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
1290 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
1291 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
1292 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
1293 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
1294 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
1295 "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
1296
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
1297 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
1298 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
1299 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
1300 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
1301 self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1302
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
1303 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
1304 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
1305 [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
1306 preexec_fn=lambda: None)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
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 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
1309 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
1310 [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
1311 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
1312 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
1313 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
1314 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
1315 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
1316 gc.disable()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1317
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1318 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
1319 # args is a string
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1320 fd, fname = mkstemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1321 # 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
1322 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
1323 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
1324 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
1325 sys.executable)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1326 os.chmod(fname, 0o700)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1327 p = subprocess.Popen(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1328 p.wait()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1329 os.remove(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1330 self.assertEqual(p.returncode, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1331
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1332 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
1333 # 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
1334 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
1335 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1336 "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
1337 startupinfo=47)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1338 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
1339 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1340 "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
1341 creationflags=47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1342
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1343 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
1344 # 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
1345 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1346 newenv["FRUIT"] = "apple"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1347 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
1348 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1349 env=newenv)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
1350 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
1351 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
1352
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1353 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
1354 # 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
1355 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1356 newenv["FRUIT"] = "apple"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1357 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
1358 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1359 env=newenv)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
1360 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
1361 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
1362
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1363 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
1364 # 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
1365 fd, fname = mkstemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1366 # 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
1367 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
1368 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
1369 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
1370 sys.executable)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1371 os.chmod(fname, 0o700)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1372 rc = subprocess.call(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1373 os.remove(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1374 self.assertEqual(rc, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1375
63094
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1376 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
1377 # 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
1378 shells = []
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1379 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
1380 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
1381 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
1382 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
1383 shells.append(sh)
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1384 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
1385 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
1386 sh = '/bin/sh'
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1387 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
1388 # 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
1389 shells.append(sh)
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
1390 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
1391 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
1392 stdout=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
1393 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
1394 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
1395
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
1396 def _kill_process(self, method, *args):
59376
d9e6f318568f Merged revisions 78701 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59353
diff changeset
1397 # 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
1398 # It should fix failures on some platforms.
64843
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1399 p = subprocess.Popen([sys.executable, "-c", """if 1:
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1400 import sys, time
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1401 sys.stdout.write('x\\n')
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1402 sys.stdout.flush()
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1403 time.sleep(30)
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1404 """],
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1405 close_fds=True,
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1406 stdin=subprocess.PIPE,
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1407 stdout=subprocess.PIPE,
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1408 stderr=subprocess.PIPE)
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1409 # 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
1410 # sending any signal.
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1411 p.stdout.read(1)
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
1412 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
1413 return p
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1414
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
1415 @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
1416 "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
1417 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
1418 # 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
1419 # 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
1420 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
1421 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
1422 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
1423 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
1424 """],
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
1425 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
1426 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
1427 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
1428 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
1429 # 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
1430 # 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
1431 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
1432 # 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
1433 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
1434 # 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
1435 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
1436 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
1437
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
1438 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
1439 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
1440 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1441 self.assertIn(b'KeyboardInterrupt', stderr)
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
1442 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
1443
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1444 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
1445 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
1446 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1447 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
1448 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
1449
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1450 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
1451 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
1452 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1453 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
1454 self.assertEqual(p.wait(), -signal.SIGTERM)
63095
ad66eff9800b Merged revisions 82971 via svnmerge from
Stefan Krah <stefan@bytereef.org>
parents: 62404
diff changeset
1455
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
1456 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
1457 # 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
1458 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
1459
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 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
1461 # 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
1462 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
1463
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 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
1465 # 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
1466 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
1467
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1468 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
1469 # 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
1470 # 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
1471 stdin = 0
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1472 newfds = []
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1473 for a in fds:
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1474 b = os.dup(a)
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1475 newfds.append(b)
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1476 if a == 0:
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1477 stdin = b
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1478 try:
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1479 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
1480 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
1481 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
1482 'import sys;'
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1483 '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
1484 '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
1485 '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
1486 stdin=stdin,
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1487 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
1488 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
1489 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
1490 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
1491 finally:
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1492 for b, a in zip(newfds, fds):
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1493 os.dup2(b, a)
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1494 for b in newfds:
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1495 os.close(b)
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
1496
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1497 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
1498 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
1499
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1500 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
1501 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
1502
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1503 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
1504 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
1505
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1506 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
1507 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
1508
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1509 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
1510 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
1511
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1512 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
1513 self.check_close_std_fds([1, 2])
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
1514
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1515 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
1516 # 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
1517 # 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
1518 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
1519
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1520 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
1521 # 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
1522 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
1523 try:
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1524 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
1525
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1526 # 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
1527 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
1528 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
1529
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1530 # 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
1531 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
1532 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
1533
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1534 # move the standard file descriptors out of the way
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1535 saved_fds = [os.dup(fd) for fd in range(3)]
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1536 try:
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1537 # 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
1538 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
1539 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
1540
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1541 # 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
1542 # 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
1543 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
1544 '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
1545 '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
1546 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
1547 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
1548 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
1549 p.wait()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1550 finally:
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1551 # restore the original fd's underneath sys.stdin, etc.
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1552 for std, saved in enumerate(saved_fds):
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1553 os.dup2(saved, std)
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1554 os.close(saved)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
1555
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1556 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
1557 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
1558
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1559 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
1560 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
1561 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
1562 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
1563
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1564 finally:
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1565 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
1566 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
1567
71524
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1568 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
1569 # 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
1570 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
1571 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
1572 try:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1573 # 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
1574 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
1575 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
1576
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1577 # save a copy of the standard file descriptors
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1578 saved_fds = [os.dup(fd) for fd 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
1579 try:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1580 # 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
1581 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
1582 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
1583
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1584 # 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
1585 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
1586 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
1587
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1588 # 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
1589 # 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
1590 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
1591 '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
1592 '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
1593 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
1594 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
1595 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
1596 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
1597
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1598 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
1599 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
1600
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1601 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
1602 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
1603 finally:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1604 for std, saved in enumerate(saved_fds):
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1605 os.dup2(saved, std)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1606 os.close(saved)
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1607
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1608 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
1609 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
1610
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1611 finally:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1612 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
1613 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
1614
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1615 # 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
1616 # 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
1617 # 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
1618 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
1619 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
1620 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
1621 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
1622 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
1623 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
1624 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
1625
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1626 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
1627 def prepare():
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1628 raise ValueError("surrogate:\uDCff")
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1629
60896
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1630 try:
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1631 subprocess.call(
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1632 [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
1633 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
1634 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
1635 # 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
1636 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
1637 self.assertEqual(str(err), "surrogate:\uDCff")
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1638 except RuntimeError as err:
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1639 # _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
1640 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
1641 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
1642 else:
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1643 self.fail("Expected ValueError or RuntimeError")
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1644
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1645 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
1646 for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')):
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1647 # 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
1648 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
1649 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
1650 env[key] = value
65302
e2c98c60dd64 test_subprocess doesn't need to C locale to test os.environb
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65301
diff changeset
1651 # Use C locale to get ascii for the locale encoding to force
e2c98c60dd64 test_subprocess doesn't need to C locale to test os.environb
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65301
diff changeset
1652 # 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
1653 # 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
1654 env['LC_ALL'] = 'C'
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1655 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
1656 [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
1657 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
1658 stdout = stdout.rstrip(b'\n\r')
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66307
diff changeset
1659 self.assertEqual(stdout.decode('ascii'), ascii(value))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1660
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1661 # 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
1662 key = key.encode("ascii", "surrogateescape")
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1663 value = value.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
1664 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
1665 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
1666 env[key] = 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
1667 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
1668 [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
1669 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
1670 stdout = stdout.rstrip(b'\n\r')
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66307
diff changeset
1671 self.assertEqual(stdout.decode('ascii'), ascii(value))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1672
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
1673 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
1674 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
1675 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
1676 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
1677
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1678 # 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
1679 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
1680 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
1681
68214
738feec9ced0 Issue #8513: On UNIX, subprocess supports bytes command string.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68142
diff changeset
1682 # 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
1683 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
1684 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
1685 self.assertEqual(exitcode, 0)
738feec9ced0 Issue #8513: On UNIX, subprocess supports bytes command string.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68142
diff changeset
1686
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
1687 # 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
1688 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
1689 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
1690 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
1691 self.assertEqual(exitcode, 0)
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1692
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
1693 # 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
1694 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
1695 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
1696 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
1697 self.assertEqual(exitcode, 0)
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1698
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
1699 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
1700 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
1701 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
1702
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
1703 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
1704 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
1705 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
1706
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
1707 self.addCleanup(p1.communicate, b'')
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1708
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
1709 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
1710 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
1711
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
1712 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
1713 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
1714 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
1715 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
1716
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
1717 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
1718 "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
1719 "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
1720 (unwanted_fds, result_fds & unwanted_fds))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1721
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
1722 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
1723 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
1724 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
1725
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
1726 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
1727 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
1728
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
1729 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
1730 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
1731 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
1732
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
1733 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
1734 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
1735 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
1736
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
1737 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
1738 self.addCleanup(p2.wait)
74575
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1739 def kill_p1():
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1740 try:
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1741 p1.terminate()
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1742 except ProcessLookupError:
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1743 pass
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1744 def kill_p2():
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1745 try:
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1746 p2.terminate()
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1747 except ProcessLookupError:
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1748 pass
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1749 self.addCleanup(kill_p1)
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1750 self.addCleanup(kill_p2)
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1751
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
1752 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
1753 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
1754
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
1755 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
1756
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
1757 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
1758 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
1759
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
1760 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
1761 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
1762
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
1763 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
1764 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
1765
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 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
1767 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
1768 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
1769
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 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
1771 # 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
1772 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
1773 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
1774 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
1775 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
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 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
1778 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
1779 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
1780 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
1781
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
1782 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
1783 "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
1784
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 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
1786 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
1787 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
1788 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
1789
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 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
1791 "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
1792 self.assertIn(1, remaining_fds, "Subprocess failed")
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1793
74563
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1794 # 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
1795 # 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
1796 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
1797 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
1798 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
1799 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
1800 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
1801 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
1802
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1803 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
1804 "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
1805 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
1806
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
1807 # 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
1808 # 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
1809 # 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
1810 # 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
1811 @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
1812 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
1813 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
1814
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
1815 open_fds = set()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1816
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
1817 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
1818 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
1819 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
1820 self.addCleanup(os.close, fds[1])
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1821 open_fds.update(fds)
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1822
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
1823 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
1824 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
1825 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
1826 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
1827 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
1828
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1829 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
1830 to_be_closed = open_fds - {fd}
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1831
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
1832 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
1833 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
1834 "fd to be closed passed")
66895
b066bd0b64e7 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66340
diff changeset
1835
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
1836 # 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
1837 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
1838 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
1839 [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
1840 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
1841 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
1842
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1843 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
1844 with io.open(os.devnull, "r+") as inout:
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1845 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
1846 stdout=inout, stdin=inout)
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1847 p.wait()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1848
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1849 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
1850 with io.open(os.devnull, "r+") as inout:
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1851 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
1852 stdout=inout, stderr=inout)
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1853 p.wait()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1854
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1855 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
1856 with io.open(os.devnull, "r+") as inout:
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1857 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
1858 stderr=inout, stdin=inout)
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1859 p.wait()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1860
66893
767420808a62 Issue #1731717: Fixed the problem where subprocess.wait() could cause an
Gregory P. Smith <greg@mad-scientist.com>
parents: 66889
diff changeset
1861 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
1862 # 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
1863 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
1864 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
1865 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
1866 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
1867 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
1868 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
1869 " 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
1870 stderr.decode('utf-8'))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1871
68680
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1872 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
1873 # 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
1874 # 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
1875 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
1876 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
1877 '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
1878 '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
1879 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
1880 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
1881 f = p.stdout
68981
72f6f5a29e7a Merge with 3.1
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68680 68980
diff changeset
1882 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
1883 try:
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1884 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
1885 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
1886 finally:
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1887 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
1888
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
1889 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
1890 # 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
1891 # 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
1892 # 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
1893 # 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
1894 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
1895 '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
1896 '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
1897 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
1898 stderr=subprocess.PIPE)
71913
f6b0c794495a Fix ResourceWarnings in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71910
diff changeset
1899 self.addCleanup(p.stdout.close)
f6b0c794495a Fix ResourceWarnings in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71910
diff changeset
1900 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
1901 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
1902 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
1903 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
1904 # 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
1905 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
1906
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1907 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
1908 # 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
1909 # 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
1910 # 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
1911 # 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
1912 # 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
1913 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
1914 '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
1915 '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
1916 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
1917 stderr=subprocess.PIPE)
71913
f6b0c794495a Fix ResourceWarnings in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71910
diff changeset
1918 self.addCleanup(p.stdout.close)
f6b0c794495a Fix ResourceWarnings in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71910
diff changeset
1919 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
1920 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
1921 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
1922 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
1923 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
1924 # 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
1925 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
1926
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1927 # 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
1928 # 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
1929 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
1930 with self.assertRaises(EnvironmentError) as 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
1931 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
1932 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
1933 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
1934 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
1935 # 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
1936 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
1937 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
1938
66895
b066bd0b64e7 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66340
diff changeset
1939
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
1940 @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
1941 class Win32ProcessTestCase(BaseTestCase):
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
1942
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1943 def test_startupinfo(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1944 # startupinfo argument
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1945 # 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
1946 # depend on win32all.
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1947 STARTF_USESHOWWINDOW = 1
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1948 SW_MAXIMIZE = 3
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1949 startupinfo = subprocess.STARTUPINFO()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1950 startupinfo.dwFlags = STARTF_USESHOWWINDOW
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1951 startupinfo.wShowWindow = SW_MAXIMIZE
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1952 # 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
1953 # 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
1954 # ignored
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1955 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
1956 startupinfo=startupinfo)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1957
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1958 def test_creationflags(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1959 # creationflags argument
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1960 CREATE_NEW_CONSOLE = 16
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1961 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
1962 subprocess.call(sys.executable +
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1963 ' -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
1964 creationflags=CREATE_NEW_CONSOLE)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1965
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1966 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
1967 # 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
1968 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
1969 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1970 "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
1971 preexec_fn=lambda: 1)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1972 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
1973 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1974 "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
1975 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1976 close_fds=True)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1977
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1978 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
1979 # close file descriptors
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1980 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
1981 "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
1982 close_fds=True)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1983 self.assertEqual(rc, 47)
41969
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41845
diff changeset
1984
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1985 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
1986 # 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
1987 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1988 newenv["FRUIT"] = "physalis"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1989 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
1990 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1991 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
1992 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
1993 self.assertIn(b"physalis", p.stdout.read())
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1994
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1995 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
1996 # 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
1997 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1998 newenv["FRUIT"] = "physalis"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1999 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
2000 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2001 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
2002 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
2003 self.assertIn(b"physalis", p.stdout.read())
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2004
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2005 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
2006 # 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
2007 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
2008 ' -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
2009 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
2010
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
2011 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
2012 # 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
2013 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
2014 import sys, time
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2015 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
2016 sys.stdout.flush()
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2017 time.sleep(30)
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2018 """],
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2019 stdin=subprocess.PIPE,
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2020 stdout=subprocess.PIPE,
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2021 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
2022 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
2023 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
2024 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
2025 # 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
2026 # sending any signal.
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
2027 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
2028 getattr(p, method)(*args)
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
2029 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
2030 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
2031 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
2032 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
2033
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
2034 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
2035 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
2036 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
2037 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
2038 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
2039 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
2040 """],
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
2041 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
2042 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
2043 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
2044 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
2045 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
2046 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
2047 # 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
2048 # 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
2049 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
2050 # 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
2051 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
2052 # 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
2053 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
2054 _, 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
2055 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
2056 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
2057 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
2058
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2059 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
2060 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
2061
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2062 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
2063 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
2064
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2065 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
2066 self._kill_process('terminate')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2067
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
2068 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
2069 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
2070
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
2071 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
2072 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
2073
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
2074 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
2075 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
2076
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
2077
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
2078 # The module says:
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
2079 # "NB This only works (and is only relevant) for UNIX."
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
2080 #
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
2081 # Actually, getoutput should work on any platform with an os.popen, but
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
2082 # I'll take the comment as given, and skip this suite.
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
2083 @unittest.skipUnless(os.name == 'posix', "only relevant for UNIX")
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2084 class CommandTests(unittest.TestCase):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2085 def test_getoutput(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2086 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
2087 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
2088 (0, 'xyzzy'))
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
2089
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2090 # 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
2091 # 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
2092 # 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
2093 dir = None
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2094 try:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2095 dir = tempfile.mkdtemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2096 name = os.path.join(dir, "foo")
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
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 status, output = subprocess.getstatusoutput('cat ' + name)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2099 self.assertNotEqual(status, 0)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2100 finally:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2101 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
2102 os.rmdir(dir)
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
2103
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
2104
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2105 @unittest.skipUnless(getattr(subprocess, '_has_poll', False),
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2106 "poll system call not supported")
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2107 class ProcessTestCaseNoPoll(ProcessTestCase):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2108 def setUp(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2109 subprocess._has_poll = False
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2110 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
2111
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2112 def tearDown(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2113 subprocess._has_poll = True
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
2114 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
2115
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
2116
59209
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2117 class HelperFunctionTests(unittest.TestCase):
59217
d1c62882c73b Merged revisions 78533 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59209
diff changeset
2118 @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
2119 def test_eintr_retry_call(self):
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2120 record_calls = []
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2121 def fake_os_func(*args):
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2122 record_calls.append(args)
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2123 if len(record_calls) == 2:
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2124 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
2125 return tuple(reversed(args))
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2126
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2127 self.assertEqual((999, 256),
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2128 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
2129 self.assertEqual([(256, 999)], record_calls)
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2130 # 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
2131 self.assertEqual((666,),
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2132 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
2133 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
2134
59207
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
2135
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
2136 @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
2137 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
2138
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
2139 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
2140 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
2141 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
2142 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
2143 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
2144 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
2145 )
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
2146 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
2147
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
2148 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
2149 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
2150 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
2151
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
2152 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
2153 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
2154 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
2155 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
2156 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
2157 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
2158 "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
2159 )
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
2160
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
2161 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
2162 # 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
2163 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
2164 "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
2165
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
2166 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
2167 # 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
2168 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
2169
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
2170 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
2171 # 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
2172 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
2173 "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
2174
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
2175 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
2176 # 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
2177 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
2178
66622
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2179
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
2180 class ContextManagerTests(BaseTestCase):
66622
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2181
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2182 def test_pipe(self):
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2183 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
2184 "import sys;"
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2185 "sys.stdout.write('stdout');"
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2186 "sys.stderr.write('stderr');"],
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2187 stdout=subprocess.PIPE,
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2188 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
2189 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
2190 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
2191
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2192 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
2193 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
2194
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2195 def test_returncode(self):
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2196 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
2197 "import sys; sys.exit(100)"]) as proc:
70049
b00a64a5cb93 merge - 7a3f3ad83676 Fixes Issue #12044.
Gregory P. Smith <greg@krypto.org>
parents: 69155
diff changeset
2198 pass
b00a64a5cb93 merge - 7a3f3ad83676 Fixes Issue #12044.
Gregory P. Smith <greg@krypto.org>
parents: 69155
diff changeset
2199 # __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
2200 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
2201
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2202 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
2203 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
2204 "import sys;"
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2205 "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
2206 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
2207 proc.communicate(b"context")
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2208 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
2209
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2210 def test_invalid_args(self):
81076
5d172190c37c fix test for subprocess (#16644)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80391 81075
diff changeset
2211 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
2212 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
2213 stdout=subprocess.PIPE,
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2214 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
2215 pass
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
2216
59209
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
2217
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2218 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
2219 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
2220 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
2221 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
2222 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
2223 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
2224 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
2225 CommandsWithSpaces,
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
2226 ContextManagerTests,
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
2227 )
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
2228
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
2229 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
2230 support.reap_children()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2231
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2232 if __name__ == "__main__":
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
2233 unittest.main()