annotate Lib/test/test_subprocess.py @ 80272:ec00f8570c55

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