annotate Lib/test/test_subprocess.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 41b1fe5a75a6
children 481f5d9ef577 f452d7d5470d
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
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46710
diff changeset
2 from test import support
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
3 import subprocess
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
4 import sys
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
5 import signal
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
6 import io
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
7 import os
59209
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
8 import errno
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
9 import tempfile
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
10 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
11 import re
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
12 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
13 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
14 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
15 import shutil
73925
6040c248a199 we're always going to have gc
Benjamin Peterson <benjamin@python.org>
parents: 73923
diff changeset
16 import gc
73923
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
17
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
18 try:
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
19 import resource
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
20 except ImportError:
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
21 resource = None
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
22
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
23 mswindows = (sys.platform == "win32")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
24
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
25 #
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
26 # Depends on the following external programs: Python
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 if mswindows:
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
30 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
31 'os.O_BINARY);')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
32 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
33 SETBINARY = ''
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
34
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
35
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
36 try:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
37 mkstemp = tempfile.mkstemp
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
38 except AttributeError:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
39 # 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
40 def mkstemp():
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
41 """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
42 fname = tempfile.mktemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
43 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
44
33739
76355363101b New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents: 33738
diff changeset
45
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
46 class BaseTestCase(unittest.TestCase):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
47 def setUp(self):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
48 # 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
49 # 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
50 support.reap_children()
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
51
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
52 def tearDown(self):
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
53 for inst in subprocess._active:
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
54 inst.wait()
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
55 subprocess._cleanup()
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
56 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
57
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
58 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
59 # 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
60 # 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
61 # 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
62 actual = support.strip_python_stderr(stderr)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
63 self.assertEqual(actual, expected, msg)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
64
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
65
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
66 class ProcessTestCase(BaseTestCase):
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
67
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
68 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
69 # call() function with sequence argument
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
70 rc = subprocess.call([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
71 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
72 self.assertEqual(rc, 47)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
73
34302
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
74 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
75 # 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
76 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
77 "import sys; sys.exit(0)"])
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
78 self.assertEqual(rc, 0)
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
79
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
80 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
81 # 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
82 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
83 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
84 "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
85 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
86
50074
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
87 def test_check_output(self):
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
88 # 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
89 output = subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
90 [sys.executable, "-c", "print('BDFL')"])
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55626
diff changeset
91 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
92
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
93 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
94 # 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
95 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
96 subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
97 [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
98 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
99
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
100 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
101 # 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
102 output = subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
103 [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
104 stderr=subprocess.STDOUT)
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_stdout_arg(self):
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
108 # 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
109 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
110 output = 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", "print('will not be run')"],
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
112 stdout=sys.stdout)
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
113 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
114 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
115
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
116 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
117 # call() function with keyword args
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
118 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
119 newenv["FRUIT"] = "banana"
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
120 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
121 'import sys, os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
122 '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
123 env=newenv)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
124 self.assertEqual(rc, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
125
70554
0f2714e49583 Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70465
diff changeset
126 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
127 # 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
128 # 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
129 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
130 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
131 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
132 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
133 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
134 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
135
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
136 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
137 # .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
138 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
139 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
140 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
141 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
142 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
143 self.assertEqual(p.stdin, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
144
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
145 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
146 # .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
147 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
148 '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
149 '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
150 '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
151 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
152 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
153 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
154 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
155 self.assertEqual(p.stdout, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
156
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
157 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
158 # .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
159 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
160 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
161 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
162 self.addCleanup(p.stdin.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
163 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
164 self.assertEqual(p.stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
165
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
166 def test_executable_with_cwd(self):
59507
a111024a0bcb Merged revisions 78833 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59504
diff changeset
167 python_dir = os.path.dirname(os.path.realpath(sys.executable))
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
168 p = subprocess.Popen(["somethingyoudonthave", "-c",
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
169 "import sys; sys.exit(47)"],
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
170 executable=sys.executable, cwd=python_dir)
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
171 p.wait()
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
172 self.assertEqual(p.returncode, 47)
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
173
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
174 @unittest.skipIf(sysconfig.is_python_build(),
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
175 "need an installed Python. See #7774")
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
176 def test_executable_without_cwd(self):
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
177 # 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
178 # argument. For test runs in the build directory, see #7774.
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
179 p = subprocess.Popen(["somethingyoudonthave", "-c",
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
180 "import sys; sys.exit(47)"],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
181 executable=sys.executable)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
182 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
183 self.assertEqual(p.returncode, 47)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
184
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
185 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
186 # stdin redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
187 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
188 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
189 stdin=subprocess.PIPE)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
190 p.stdin.write(b"pear")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
191 p.stdin.close()
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.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
194
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
195 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
196 # stdin is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
197 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
198 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
199 d = tf.fileno()
49200
bdb660dca74d Issue #3782: os.write() must not accept unicode strings
Antoine Pitrou <solipsis@pitrou.net>
parents: 48395
diff changeset
200 os.write(d, b"pear")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
201 os.lseek(d, 0, 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
202 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
203 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
204 stdin=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
205 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
206 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
207
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
208 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
209 # stdin is set to open file object
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
210 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
211 self.addCleanup(tf.close)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
212 tf.write(b"pear")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
213 tf.seek(0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
214 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
215 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
216 stdin=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
217 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
218 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
219
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
220 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
221 # stdout redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
222 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
223 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
224 stdout=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
225 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
226 self.assertEqual(p.stdout.read(), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
227
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
228 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
229 # stdout is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
230 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
231 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
232 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
233 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
234 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
235 stdout=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
236 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
237 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
238 self.assertEqual(os.read(d, 1024), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
239
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
240 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
241 # stdout is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
242 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
243 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
244 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
245 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
246 stdout=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
247 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
248 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
249 self.assertEqual(tf.read(), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
250
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
251 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
252 # stderr redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
253 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
254 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
255 stderr=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
256 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
257 self.assertStderrEqual(p.stderr.read(), b"strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
258
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
259 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
260 # stderr is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
261 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
262 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
263 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
264 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
265 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
266 stderr=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
267 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
268 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
269 self.assertStderrEqual(os.read(d, 1024), b"strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
270
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
271 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
272 # stderr is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
273 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
274 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
275 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
276 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
277 stderr=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
278 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
279 tf.seek(0)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
280 self.assertStderrEqual(tf.read(), b"strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
281
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
282 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
283 # capture stdout and stderr to the same pipe
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
284 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
285 'import sys;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
286 '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
287 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
288 '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
289 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
290 stderr=subprocess.STDOUT)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
291 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
292 self.assertStderrEqual(p.stdout.read(), b"appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
293
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
294 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
295 # 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
296 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
297 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
298 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
299 'import sys;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
300 '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
301 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
302 '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
303 stdout=tf,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
304 stderr=tf)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
305 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
306 tf.seek(0)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
307 self.assertStderrEqual(tf.read(), b"appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
308
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
309 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
310 # 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
311 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
312 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
313 self.assertEqual(rc, 2)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
314
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
315 def test_cwd(self):
44373
2750d531fdc0 Merged revisions 59565-59594 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
316 tmpdir = tempfile.gettempdir()
33980
4005f79d281a Use os.chdir/os.getcwd instead of os.path.realpath, to support Tru64
Peter Astrand <astrand@lysator.liu.se>
parents: 33814
diff changeset
317 # We cannot use os.path.realpath to canonicalize the path,
4005f79d281a Use os.chdir/os.getcwd instead of os.path.realpath, to support Tru64
Peter Astrand <astrand@lysator.liu.se>
parents: 33814
diff changeset
318 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
4005f79d281a Use os.chdir/os.getcwd instead of os.path.realpath, to support Tru64
Peter Astrand <astrand@lysator.liu.se>
parents: 33814
diff changeset
319 cwd = os.getcwd()
4005f79d281a Use os.chdir/os.getcwd instead of os.path.realpath, to support Tru64
Peter Astrand <astrand@lysator.liu.se>
parents: 33814
diff changeset
320 os.chdir(tmpdir)
4005f79d281a Use os.chdir/os.getcwd instead of os.path.realpath, to support Tru64
Peter Astrand <astrand@lysator.liu.se>
parents: 33814
diff changeset
321 tmpdir = os.getcwd()
4005f79d281a Use os.chdir/os.getcwd instead of os.path.realpath, to support Tru64
Peter Astrand <astrand@lysator.liu.se>
parents: 33814
diff changeset
322 os.chdir(cwd)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
323 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
324 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
325 'sys.stdout.write(os.getcwd())'],
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
326 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
327 cwd=tmpdir)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
328 self.addCleanup(p.stdout.close)
33742
179e42ea2e9c normalize case when comparing directory names (problem reported by
Fredrik Lundh <fredrik@pythonware.com>
parents: 33739
diff changeset
329 normcase = os.path.normcase
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
330 self.assertEqual(normcase(p.stdout.read().decode("utf-8")),
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
331 normcase(tmpdir))
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
332
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
333 def test_env(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
334 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
335 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
336 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
337 '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
338 '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
339 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
340 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
341 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
342 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
343
70929
52c5f80122dd Issue #12383: skip test_empty_env() of subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70926
diff changeset
344 # 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
345 # Python
52c5f80122dd Issue #12383: skip test_empty_env() of subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70926
diff changeset
346 @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
347 '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
348 @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
349 '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
350 '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
351 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
352 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
353 '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
354 '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
355 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
356 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
357 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
358 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
359 (b"[]",
da3af4b131d7 Issue #12383: fix test_empty_env() of subprocess on Mac OS X
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70923
diff changeset
360 # 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
361 # environment
da3af4b131d7 Issue #12383: fix test_empty_env() of subprocess on Mac OS X
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70923
diff changeset
362 b"['__CF_USER_TEXT_ENCODING']"))
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
363
34671
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
364 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
365 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
366 'import sys;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
367 '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
368 stdin=subprocess.PIPE)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
369 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
370 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
371
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
372 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
373 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
374 '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
375 stdout=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
376 (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
377 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
378 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
379
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
380 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
381 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
382 '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
383 stderr=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
384 (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
385 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
386 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
387
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
388 def test_communicate(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
389 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
390 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
391 '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
392 '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
393 stdin=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
394 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
395 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
396 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
397 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
398 self.addCleanup(p.stdin.close)
48395
bb65f9b76c1f #2683: Popen.communicate() argument must be bytes.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
399 (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
400 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
401 self.assertStderrEqual(stderr, b"pineapple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
402
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
403 # 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
404 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
405 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
406 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
407 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
408 options = {}
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
409 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
410 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
411 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
412 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
413 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
414 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
415 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
416 continue
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
417 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
418 p.communicate()
38d66980187e Issue #10763: subprocess.communicate() closes stdout and stderr if both are
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66896
diff changeset
419 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
420 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
421 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
422 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
423 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
424 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
425
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
426 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
427 # communicate() should return None if no redirection is active
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
428 p = subprocess.Popen([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
429 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
430 (stdout, stderr) = p.communicate()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
431 self.assertEqual(stdout, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
432 self.assertEqual(stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
433
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
434 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
435 # communicate() with writes larger than pipe_buf
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
436 # This test will probably deadlock rather than fail, if
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
437 # communicate() does not work properly.
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
438 x, y = os.pipe()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
439 if mswindows:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
440 pipe_buf = 512
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
441 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
442 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
443 os.close(x)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
444 os.close(y)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
445 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
446 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
447 'sys.stdout.write(sys.stdin.read(47));'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
448 'sys.stderr.write("xyz"*%d);'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
449 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
450 stdin=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
451 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
452 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
453 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
454 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
455 self.addCleanup(p.stdin.close)
41732
bbb099d4ec80 Make test_subprocess pass. The subprocess module now reads and writes
Guido van Rossum <guido@python.org>
parents: 40788
diff changeset
456 string_to_write = b"abc"*pipe_buf
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
457 (stdout, stderr) = p.communicate(string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
458 self.assertEqual(stdout, string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
459
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
460 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
461 # stdin.write before communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
462 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
463 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
464 '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
465 stdin=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
466 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
467 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
468 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
469 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
470 self.addCleanup(p.stdin.close)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
471 p.stdin.write(b"banana")
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
472 (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
473 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
474 self.assertStderrEqual(stderr, b"")
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
475
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
476 def test_universal_newlines(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
477 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
478 'import sys,os;' + SETBINARY +
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
479 'sys.stdout.write(sys.stdin.readline());'
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
480 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
481 'sys.stdout.write("line2\\n");'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
482 'sys.stdout.flush();'
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
483 'sys.stdout.write(sys.stdin.read());'
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
484 'sys.stdout.flush();'
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
485 'sys.stdout.write("line4\\n");'
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
486 'sys.stdout.flush();'
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
487 'sys.stdout.write("line5\\r\\n");'
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
488 'sys.stdout.flush();'
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
489 'sys.stdout.write("line6\\r");'
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
490 'sys.stdout.flush();'
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
491 'sys.stdout.write("\\nline7");'
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
492 'sys.stdout.flush();'
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
493 'sys.stdout.write("\\nline8");'],
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
494 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
495 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
496 universal_newlines=1)
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
497 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
498 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
499 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
500 p.stdin.close()
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
501 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
502 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
503 "line2\n")
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
504 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
505 "line3\n")
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
506 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
507 "line4\nline5\nline6\nline7\nline8")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
508
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
509 def test_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
510 # universal newlines through communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
511 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
512 'import sys,os;' + SETBINARY +
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
513 'sys.stdout.write("line2\\n");'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
514 'sys.stdout.flush();'
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
515 'sys.stdout.write("line4\\n");'
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
516 'sys.stdout.flush();'
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
517 'sys.stdout.write("line5\\r\\n");'
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
518 'sys.stdout.flush();'
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
519 'sys.stdout.write("line6\\r");'
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
520 'sys.stdout.flush();'
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
521 'sys.stdout.write("\\nline7");'
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
522 'sys.stdout.flush();'
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
523 'sys.stdout.write("\\nline8");'],
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
524 stderr=subprocess.PIPE,
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
525 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
526 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
527 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
528 self.addCleanup(p.stderr.close)
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
529 # BUG: can't give a non-empty stdin because it breaks both the
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
530 # select- and poll-based communicate() implementations.
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
531 (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
532 self.assertEqual(stdout,
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
533 "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
534
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
535 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
536 # 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
537 p = subprocess.Popen([sys.executable, "-c",
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
538 'import sys,os;' + SETBINARY + '''\nif True:
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
539 s = sys.stdin.readline()
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
540 assert s == "line1\\n", repr(s)
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
541 s = sys.stdin.read()
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
542 assert s == "line3\\n", repr(s)
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
543 '''],
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
544 stdin=subprocess.PIPE,
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
545 universal_newlines=1)
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
546 (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
547 self.assertEqual(p.returncode, 0)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
548
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
549 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
550 # Make sure we leak no resources
64814
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
551 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
552 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
553 else:
64814
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
554 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
555 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
556 tmpdir = tempfile.mkdtemp()
64814
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
557 try:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
558 for i in range(max_handles):
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
559 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
560 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
561 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
562 except OSError as e:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
563 if e.errno != errno.EMFILE:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
564 raise
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
565 break
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
566 else:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
567 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
568 "(tried %d)" % max_handles)
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
569 # 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
570 for i in range(10):
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
571 os.close(handles.pop())
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
572 # 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
573 # 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
574 for i in range(15):
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
575 p = subprocess.Popen([sys.executable, "-c",
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
576 "import sys;"
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
577 "sys.stdout.write(sys.stdin.read())"],
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
578 stdin=subprocess.PIPE,
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
579 stdout=subprocess.PIPE,
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
580 stderr=subprocess.PIPE)
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
581 data = p.communicate(b"lime")[0]
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
582 self.assertEqual(data, b"lime")
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
583 finally:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
584 for h in handles:
77425f2d7d76 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64810
diff changeset
585 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
586 shutil.rmtree(tmpdir)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
587
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
588 def test_list2cmdline(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
589 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
590 '"a b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
591 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
592 'ab\\"c \\ d')
44810
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44373
diff changeset
593 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44373
diff changeset
594 'ab\\"c " \\\\" d')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
595 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
596 'a\\\\\\b "de fg" h')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
597 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
598 'a\\\\\\"b c d')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
599 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
600 '"a\\\\b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
601 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
602 '"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
603 self.assertEqual(subprocess.list2cmdline(['ab', '']),
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
604 'ab ""')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
605
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
606
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
607 def test_poll(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
608 p = subprocess.Popen([sys.executable,
33738
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
609 "-c", "import time; time.sleep(1)"])
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
610 count = 0
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
611 while p.poll() is None:
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
612 time.sleep(0.1)
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
613 count += 1
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
614 # We expect that the poll loop probably went around about 10 times,
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
615 # but, based on system scheduling we can't control, it's possible
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
616 # poll() never returned None. It "should be" very rare that it
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
617 # didn't go around at least twice.
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
618 self.assertGreaterEqual(count, 2)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
619 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
620 self.assertEqual(p.poll(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
621
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
622
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
623 def test_wait(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
624 p = subprocess.Popen([sys.executable,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
625 "-c", "import time; time.sleep(2)"])
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
626 self.assertEqual(p.wait(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
627 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
628 self.assertEqual(p.wait(), 0)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
629
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
630
79d2ae0a62ef Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents: 33998
diff changeset
631 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
632 # 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
633 # TypeError.
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
634 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
635 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
636
41984
e420039368b7 The bufsize argument to Popen() should accept None meaning the default (0).
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
637 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
638 # 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
639 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
640 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
641 # 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
642 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
643 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
644
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
645 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
646 # 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
647 # 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
648 # 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
649 # 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
650 # 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
651 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
652 # 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
653 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
654 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
655 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
656 stderr=subprocess.PIPE)
68433
67f4ef6094ed Merge fix for #11490 from 3.1.
R David Murray <rdmurray@bitdance.com>
parents: 68257 68432
diff changeset
657 # ignore errors that indicate the command was not found
68437
f65aad1226b7 Fix whitespace
R David Murray <rdmurray@bitdance.com>
parents: 68433
diff changeset
658 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
659 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
660
61798
73416b1bbcd5 Issue #8780: Only backport the new test, the fix is not needed
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61014
diff changeset
661 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
662 # 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
663 # 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
664 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
665 '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
666 '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
667 "[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
668 '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
669 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
670 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
671
63774
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
672 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
673 # 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
674 # 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
675 ifhandle, ifname = mkstemp()
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
676 ofhandle, ofname = mkstemp()
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
677 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
678 try:
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
679 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
680 stderr=efhandle)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
681 except OSError:
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
682 os.close(ifhandle)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
683 os.remove(ifname)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
684 os.close(ofhandle)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
685 os.remove(ofname)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
686 os.close(efhandle)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
687 os.remove(efname)
bb2608f73eb7 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63142
diff changeset
688 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
689 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
690 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
691
69154
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
692 def test_communicate_epipe(self):
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
693 # 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
694 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
695 stdin=subprocess.PIPE,
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
696 stdout=subprocess.PIPE,
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
697 stderr=subprocess.PIPE)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
698 self.addCleanup(p.stdout.close)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
699 self.addCleanup(p.stderr.close)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
700 self.addCleanup(p.stdin.close)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
701 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
702
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
703 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
704 # 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
705 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
706 stdin=subprocess.PIPE)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
707 self.addCleanup(p.stdin.close)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
708 time.sleep(2)
158495d49f58 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68980
diff changeset
709 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
710
71233
807921ba241d Issue #12493: skip test_communicate_eintr() if signal.SIGALRM is missing
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71228
diff changeset
711 @unittest.skipUnless(hasattr(signal, 'SIGALRM'),
807921ba241d Issue #12493: skip test_communicate_eintr() if signal.SIGALRM is missing
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71228
diff changeset
712 "Requires signal.SIGALRM")
71228
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
713 def test_communicate_eintr(self):
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
714 # Issue #12493: communicate() should handle EINTR
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
715 def handler(signum, frame):
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
716 pass
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
717 old_handler = signal.signal(signal.SIGALRM, handler)
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
718 self.addCleanup(signal.signal, signal.SIGALRM, old_handler)
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
719
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
720 # the process is running for 2 seconds
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
721 args = [sys.executable, "-c", 'import time; time.sleep(2)']
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
722 for stream in ('stdout', 'stderr'):
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
723 kw = {stream: subprocess.PIPE}
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
724 with subprocess.Popen(args, **kw) as process:
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
725 signal.alarm(1)
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
726 # communicate() will be interrupted by SIGALRM
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
727 process.communicate()
dcfacc2d93b4 Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70929
diff changeset
728
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
729
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
730 # context manager
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
731 class _SuppressCoreFiles(object):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
732 """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
733 old_limit = None
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
734
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
735 def __enter__(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
736 """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
737 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
738 try:
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
739 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
740 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
741 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
742 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
743
63141
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
744 if sys.platform == 'darwin':
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
745 # 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
746 # 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
747 # when it is.
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
748 #
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
749 # 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
750 # that might trigger the next manager.
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
751 value = subprocess.Popen(['/usr/bin/defaults', 'read',
63142
5a698a152f30 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63095
diff changeset
752 'com.apple.CrashReporter', 'DialogType'],
5a698a152f30 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63095
diff changeset
753 stdout=subprocess.PIPE).communicate()[0]
63141
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
754 if value.strip() == b'developer':
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
755 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
756 "that is intentional", end='')
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
757 sys.stdout.flush()
63142
5a698a152f30 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63095
diff changeset
758
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
759 def __exit__(self, *args):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
760 """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
761 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
762 return
73923
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
763 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
764 try:
245f6094ccba you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 71913
diff changeset
765 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
766 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
767 pass
63142
5a698a152f30 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63095
diff changeset
768
5a698a152f30 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63095
diff changeset
769
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
770 @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
771 class POSIXProcessTestCase(BaseTestCase):
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
772
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
773 def test_exceptions(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
774 nonexistent_dir = "/_this/pa.th/does/not/exist"
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
775 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
776 os.chdir(nonexistent_dir)
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
777 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
778 # 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
779 # 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
780 # 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
781 desired_exception = e
66307
c06ed25e69b8 add filename to ENOENT message #4925
Benjamin Peterson <benjamin@python.org>
parents: 65977
diff changeset
782 desired_exception.strerror += ': ' + repr(sys.executable)
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
783 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
784 self.fail("chdir to nonexistant directory %s succeeded." %
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
785 nonexistent_dir)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
786
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
787 # Error in the child re-raised in the parent.
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
788 try:
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
789 p = subprocess.Popen([sys.executable, "-c", ""],
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
790 cwd=nonexistent_dir)
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
791 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
792 # 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
793 # 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
794 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
795 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
796 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
797 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
798
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
799 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
800 # 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
801 # 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
802 # 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
803 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
804 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
805
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
806 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
807 # 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
808 # 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
809 # 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
810 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
811 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
812 [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
813 "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
814 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
815 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
816 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
817 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
818 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
819 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
820 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
821 self.assertNotEqual(parent_pgid, child_pgid)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
822
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
823 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
824 # returncode handles signal termination
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
825 with _SuppressCoreFiles():
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
826 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
827 'import os; os.abort()'])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
828 p.wait()
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
829 self.assertEqual(-p.returncode, signal.SIGABRT)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
830
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
831 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
832 # 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
833 # 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
834 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
835 'import sys,os;'
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
836 '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
837 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
838 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
839 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
840 self.assertEqual(p.stdout.read(), b"apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
841
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
842 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
843 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
844 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
845 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
846 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
847 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
848 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
849 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
850 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
851 "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
852 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
853 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
854 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
855 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
856 "to the parent process.")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
857
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
858 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
859 # 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
860 # 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
861 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
862 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
863 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
864 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
865 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
866 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
867 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
868 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
869 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
870 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
871 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
872 "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
873
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
874 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
875 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
876 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
877 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
878 self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
879
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
880 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
881 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
882 [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
883 preexec_fn=lambda: None)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
884
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
885 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
886 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
887 [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
888 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
889 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
890 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
891 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
892 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
893 gc.disable()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
894
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
895 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
896 # args is a string
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
897 fd, fname = mkstemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
898 # 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
899 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
900 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
901 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
902 sys.executable)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
903 os.chmod(fname, 0o700)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
904 p = subprocess.Popen(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
905 p.wait()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
906 os.remove(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
907 self.assertEqual(p.returncode, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
908
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
909 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
910 # 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
911 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
912 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
913 "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
914 startupinfo=47)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
915 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
916 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
917 "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
918 creationflags=47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
919
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
920 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
921 # 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
922 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
923 newenv["FRUIT"] = "apple"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
924 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
925 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
926 env=newenv)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
927 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
928 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
929
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
930 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
931 # 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
932 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
933 newenv["FRUIT"] = "apple"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
934 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
935 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
936 env=newenv)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
937 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
938 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
939
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
940 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
941 # 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
942 fd, fname = mkstemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
943 # 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
944 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
945 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
946 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
947 sys.executable)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
948 os.chmod(fname, 0o700)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
949 rc = subprocess.call(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
950 os.remove(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
951 self.assertEqual(rc, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
952
63094
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
953 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
954 # 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
955 shells = []
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
956 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
957 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
958 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
959 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
960 shells.append(sh)
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
961 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
962 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
963 sh = '/bin/sh'
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
964 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
965 # 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
966 shells.append(sh)
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
967 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
968 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
969 stdout=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
970 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
971 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
972
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
973 def _kill_process(self, method, *args):
59376
d9e6f318568f Merged revisions 78701 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59353
diff changeset
974 # 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
975 # 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
976 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
977 import sys, time
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
978 sys.stdout.write('x\\n')
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
979 sys.stdout.flush()
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
980 time.sleep(30)
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
981 """],
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
982 close_fds=True,
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
983 stdin=subprocess.PIPE,
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
984 stdout=subprocess.PIPE,
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
985 stderr=subprocess.PIPE)
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
986 # 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
987 # sending any signal.
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
988 p.stdout.read(1)
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
989 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
990 return p
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
991
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
992 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
993 # 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
994 # 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
995 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
996 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
997 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
998 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
999 """],
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
1000 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
1001 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
1002 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
1003 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
1004 # 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
1005 # 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
1006 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
1007 # 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
1008 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
1009 # 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
1010 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
1011 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
1012
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
1013 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
1014 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
1015 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1016 self.assertIn(b'KeyboardInterrupt', stderr)
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
1017 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
1018
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1019 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
1020 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
1021 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1022 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
1023 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
1024
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1025 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
1026 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
1027 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1028 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
1029 self.assertEqual(p.wait(), -signal.SIGTERM)
63095
ad66eff9800b Merged revisions 82971 via svnmerge from
Stefan Krah <stefan@bytereef.org>
parents: 62404
diff changeset
1030
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
1031 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
1032 # 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
1033 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
1034
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
1035 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
1036 # 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
1037 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
1038
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
1039 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
1040 # 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
1041 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
1042
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1043 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
1044 # 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
1045 # 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
1046 stdin = 0
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1047 newfds = []
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1048 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
1049 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
1050 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
1051 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
1052 stdin = b
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1053 try:
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1054 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
1055 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
1056 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
1057 'import sys;'
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1058 '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
1059 '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
1060 '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
1061 stdin=stdin,
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1062 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
1063 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
1064 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
1065 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
1066 finally:
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1067 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
1068 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
1069 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
1070 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
1071
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1072 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
1073 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
1074
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1075 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
1076 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
1077
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1078 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
1079 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
1080
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1081 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
1082 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
1083
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1084 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
1085 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
1086
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1087 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
1088 self.check_close_std_fds([1, 2])
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
1089
67310
a66a6e42538f Issue #10806, issue #9905: Fix subprocess pipes when some of the standard
Antoine Pitrou <solipsis@pitrou.net>
parents: 67303
diff changeset
1090 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
1091 # 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
1092 # 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
1093 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
1094
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1095 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
1096 # 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
1097 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
1098 try:
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1099 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
1100
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1101 # 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
1102 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
1103 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
1104
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1105 # 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
1106 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
1107 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
1108
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1109 # 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
1110 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
1111 try:
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1112 # 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
1113 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
1114 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
1115
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1116 # 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
1117 # 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
1118 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
1119 '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
1120 '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
1121 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
1122 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
1123 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
1124 p.wait()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1125 finally:
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1126 # 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
1127 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
1128 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
1129 os.close(saved)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
1130
67327
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1131 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
1132 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
1133
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1134 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
1135 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
1136 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
1137 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
1138
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1139 finally:
a7c499266a9f Add a subprocess test of remapping standard file descriptors (issue #1187).
Antoine Pitrou <solipsis@pitrou.net>
parents: 67310
diff changeset
1140 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
1141 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
1142
71524
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1143 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
1144 # 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
1145 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
1146 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
1147 try:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1148 # 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
1149 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
1150 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
1151
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1152 # 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
1153 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
1154 try:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1155 # 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
1156 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
1157 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
1158
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1159 # 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
1160 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
1161 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
1162
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1163 # 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
1164 # 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
1165 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
1166 '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
1167 '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
1168 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
1169 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
1170 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
1171 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
1172
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1173 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
1174 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
1175
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1176 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
1177 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
1178 finally:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1179 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
1180 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
1181 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
1182
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1183 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
1184 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
1185
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1186 finally:
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1187 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
1188 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
1189
1140b32747c9 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71485
diff changeset
1190 # 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
1191 # 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
1192 # 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
1193 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
1194 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
1195 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
1196 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
1197 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
1198 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
1199 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
1200
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1201 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
1202 def prepare():
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1203 raise ValueError("surrogate:\uDCff")
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1204
60896
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1205 try:
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1206 subprocess.call(
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1207 [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
1208 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
1209 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
1210 # 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
1211 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
1212 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
1213 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
1214 # _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
1215 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
1216 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
1217 else:
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
1218 self.fail("Expected ValueError or RuntimeError")
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1219
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1220 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
1221 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
1222 # 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
1223 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
1224 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
1225 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
1226 # 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
1227 # 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
1228 # 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
1229 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
1230 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
1231 [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
1232 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
1233 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
1234 self.assertEqual(stdout.decode('ascii'), ascii(value))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1235
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
1236 # 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
1237 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
1238 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
1239 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
1240 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
1241 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
1242 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
1243 [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
1244 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
1245 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
1246 self.assertEqual(stdout.decode('ascii'), ascii(value))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1247
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
1248 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
1249 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
1250 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
1251 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
1252
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1253 # 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
1254 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
1255 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
1256
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
1257 # 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
1258 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
1259 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
1260 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
1261 self.assertEqual(exitcode, 0)
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1262
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
1263 # 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
1264 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
1265 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
1266 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
1267 self.assertEqual(exitcode, 0)
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1268
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
1269 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
1270 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
1271 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
1272
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
1273 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
1274 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
1275 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
1276
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
1277 self.addCleanup(p1.communicate, b'')
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1278
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
1279 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
1280 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
1281
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
1282 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
1283 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
1284 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
1285 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
1286
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
1287 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
1288 "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
1289 "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
1290 (unwanted_fds, result_fds & unwanted_fds))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1291
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
1292 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
1293 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
1294 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
1295
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
1296 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
1297 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
1298
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
1299 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
1300 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
1301 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
1302
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
1303 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
1304 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
1305 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
1306
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
1307 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
1308 self.addCleanup(p2.wait)
74575
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1309 def kill_p1():
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1310 try:
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1311 p1.terminate()
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1312 except ProcessLookupError:
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1313 pass
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1314 def kill_p2():
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1315 try:
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1316 p2.terminate()
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1317 except ProcessLookupError:
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1318 pass
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1319 self.addCleanup(kill_p1)
6b436e31e1de Fix a unittest error seen on NetBSD 5.
Gregory P. Smith <greg@krypto.org>
parents: 74563
diff changeset
1320 self.addCleanup(kill_p2)
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1321
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
1322 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
1323 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
1324
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
1325 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
1326
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
1327 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
1328 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
1329
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
1330 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
1331 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
1332
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
1333 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
1334 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
1335
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
1336 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
1337 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
1338 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
1339
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
1340 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
1341 # 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
1342 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
1343 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
1344 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
1345 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
1346
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
1347 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
1348 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
1349 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
1350 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
1351
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
1352 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
1353 "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
1354
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
1355 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
1356 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
1357 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
1358 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
1359
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
1360 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
1361 "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
1362 self.assertIn(1, remaining_fds, "Subprocess failed")
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1363
74563
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1364 # 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
1365 # 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
1366 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
1367 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
1368 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
1369 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
1370 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
1371 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
1372
61aa484a3e54 Fixes issue #8052: The posix subprocess module's close_fds behavior was
Gregory P. Smith <greg@krypto.org>
parents: 73925
diff changeset
1373 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
1374 "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
1375 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
1376
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
1377 # 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
1378 # 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
1379 # 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
1380 # 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
1381 @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
1382 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
1383 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
1384
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
1385 open_fds = set()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1386
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
1387 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
1388 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
1389 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
1390 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
1391 open_fds.update(fds)
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1392
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
1393 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
1394 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
1395 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
1396 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
1397 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
1398
59b43dc34158 Issue #6559: fix the subprocess.Popen pass_fds implementation. Add a unittest.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66868
diff changeset
1399 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
1400 to_be_closed = open_fds - {fd}
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1401
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
1402 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
1403 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
1404 "fd to be closed passed")
66895
b066bd0b64e7 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66340
diff changeset
1405
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
1406 # 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
1407 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
1408 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
1409 [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
1410 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
1411 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
1412
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1413 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
1414 with io.open(os.devnull, "r+") as inout:
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1415 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
1416 stdout=inout, stdin=inout)
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1417 p.wait()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1418
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1419 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
1420 with io.open(os.devnull, "r+") as inout:
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1421 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
1422 stdout=inout, stderr=inout)
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1423 p.wait()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1424
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1425 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
1426 with io.open(os.devnull, "r+") as inout:
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1427 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
1428 stderr=inout, stdin=inout)
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1429 p.wait()
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1430
66893
767420808a62 Issue #1731717: Fixed the problem where subprocess.wait() could cause an
Gregory P. Smith <greg@mad-scientist.com>
parents: 66889
diff changeset
1431 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
1432 # 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
1433 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
1434 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
1435 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
1436 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
1437 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
1438 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
1439 " non-zero with this error:\n%s" %
12381f19e2ad Fix "BytesWarning: str() on a bytes instance"
Gregory P. Smith <greg@mad-scientist.com>
parents: 66893
diff changeset
1440 stderr.decode('utf8'))
67311
e9a51d463579 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66897
diff changeset
1441
68680
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1442 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
1443 # 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
1444 # 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
1445 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
1446 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
1447 '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
1448 '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
1449 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
1450 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
1451 f = p.stdout
68981
72f6f5a29e7a Merge with 3.1
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68680 68980
diff changeset
1452 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
1453 try:
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1454 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
1455 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
1456 finally:
7451da272111 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 68554 68679
diff changeset
1457 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
1458
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
1459 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
1460 # 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
1461 # 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
1462 # 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
1463 # 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
1464 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
1465 '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
1466 '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
1467 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
1468 stderr=subprocess.PIPE)
71913
f6b0c794495a Fix ResourceWarnings in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71910
diff changeset
1469 self.addCleanup(p.stdout.close)
f6b0c794495a Fix ResourceWarnings in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71910
diff changeset
1470 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
1471 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
1472 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
1473 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
1474 # 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
1475 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
1476
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1477 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
1478 # 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
1479 # 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
1480 # 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
1481 # 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
1482 # 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
1483 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
1484 '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
1485 '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
1486 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
1487 stderr=subprocess.PIPE)
71913
f6b0c794495a Fix ResourceWarnings in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71910
diff changeset
1488 self.addCleanup(p.stdout.close)
f6b0c794495a Fix ResourceWarnings in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71910
diff changeset
1489 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
1490 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
1491 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
1492 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
1493 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
1494 # 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
1495 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
1496
8ca7f109ce79 Issue #12650: Fix a race condition where a subprocess.Popen could leak
Charles-François Natali <neologix@free.fr>
parents: 71524
diff changeset
1497 # 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
1498 # 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
1499 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
1500 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
1501 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
1502 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
1503 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
1504 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
1505 # 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
1506 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
1507 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
1508
66895
b066bd0b64e7 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 66340
diff changeset
1509
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
1510 @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
1511 class Win32ProcessTestCase(BaseTestCase):
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
1512
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1513 def test_startupinfo(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1514 # startupinfo argument
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1515 # 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
1516 # depend on win32all.
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1517 STARTF_USESHOWWINDOW = 1
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1518 SW_MAXIMIZE = 3
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1519 startupinfo = subprocess.STARTUPINFO()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1520 startupinfo.dwFlags = STARTF_USESHOWWINDOW
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1521 startupinfo.wShowWindow = SW_MAXIMIZE
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1522 # 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
1523 # 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
1524 # ignored
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1525 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
1526 startupinfo=startupinfo)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1527
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1528 def test_creationflags(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1529 # creationflags argument
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1530 CREATE_NEW_CONSOLE = 16
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1531 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
1532 subprocess.call(sys.executable +
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1533 ' -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
1534 creationflags=CREATE_NEW_CONSOLE)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1535
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1536 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
1537 # 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
1538 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
1539 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1540 "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
1541 preexec_fn=lambda: 1)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1542 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
1543 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1544 "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
1545 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1546 close_fds=True)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1547
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1548 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
1549 # close file descriptors
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1550 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
1551 "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
1552 close_fds=True)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1553 self.assertEqual(rc, 47)
41969
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41845
diff changeset
1554
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1555 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
1556 # 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
1557 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1558 newenv["FRUIT"] = "physalis"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1559 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
1560 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1561 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
1562 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
1563 self.assertIn(b"physalis", p.stdout.read())
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1564
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1565 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
1566 # 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
1567 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1568 newenv["FRUIT"] = "physalis"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1569 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
1570 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1571 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
1572 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
1573 self.assertIn(b"physalis", p.stdout.read())
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1574
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1575 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
1576 # 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
1577 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
1578 ' -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
1579 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
1580
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
1581 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
1582 # 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
1583 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
1584 import sys, time
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1585 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
1586 sys.stdout.flush()
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1587 time.sleep(30)
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1588 """],
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1589 stdin=subprocess.PIPE,
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1590 stdout=subprocess.PIPE,
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1591 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
1592 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
1593 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
1594 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
1595 # 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
1596 # sending any signal.
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1597 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
1598 getattr(p, method)(*args)
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1599 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1600 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
1601 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
1602 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
1603
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
1604 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
1605 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
1606 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
1607 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
1608 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
1609 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
1610 """],
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
1611 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
1612 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
1613 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
1614 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
1615 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
1616 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
1617 # 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
1618 # 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
1619 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
1620 # 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
1621 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
1622 # 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
1623 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
1624 _, 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
1625 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
1626 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
1627 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
1628
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1629 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
1630 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
1631
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1632 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
1633 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
1634
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1635 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
1636 self._kill_process('terminate')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1637
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
1638 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
1639 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
1640
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
1641 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
1642 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
1643
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
1644 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
1645 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
1646
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
1647
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
1648 # The module says:
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
1649 # "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
1650 #
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
1651 # 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
1652 # 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
1653 @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
1654 class CommandTests(unittest.TestCase):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1655 def test_getoutput(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1656 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
1657 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
1658 (0, 'xyzzy'))
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
1659
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1660 # 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
1661 # 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
1662 # 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
1663 dir = None
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1664 try:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1665 dir = tempfile.mkdtemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1666 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
1667
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1668 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
1669 self.assertNotEqual(status, 0)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1670 finally:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1671 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
1672 os.rmdir(dir)
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
1673
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
1674
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1675 @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
1676 "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
1677 class ProcessTestCaseNoPoll(ProcessTestCase):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1678 def setUp(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1679 subprocess._has_poll = False
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1680 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
1681
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1682 def tearDown(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1683 subprocess._has_poll = True
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1684 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
1685
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
1686
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
1687 @unittest.skipUnless(getattr(subprocess, '_posixsubprocess', 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
1688 "_posixsubprocess extension module not found.")
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1689 class ProcessTestCasePOSIXPurePython(ProcessTestCase, POSIXProcessTestCase):
70465
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1690 @classmethod
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1691 def setUpClass(cls):
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1692 global subprocess
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1693 assert subprocess._posixsubprocess
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1694 # Reimport subprocess while forcing _posixsubprocess to not exist.
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1695 with support.check_warnings(('.*_posixsubprocess .* not being used.*',
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1696 RuntimeWarning)):
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1697 subprocess = support.import_fresh_module(
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1698 'subprocess', blocked=['_posixsubprocess'])
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1699 assert not subprocess._posixsubprocess
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
1700
70465
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1701 @classmethod
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1702 def tearDownClass(cls):
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1703 global subprocess
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1704 # Reimport subprocess as it should be, restoring order to the universe.
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1705 subprocess = support.import_fresh_module('subprocess')
4f248dd34dd9 Fix ProcessTestCasePOSIXPurePython to test the module from import when
Gregory P. Smith <greg@krypto.org>
parents: 70049
diff changeset
1706 assert subprocess._posixsubprocess
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
1707
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
1708
59209
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
1709 class HelperFunctionTests(unittest.TestCase):
59217
d1c62882c73b Merged revisions 78533 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59209
diff changeset
1710 @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
1711 def test_eintr_retry_call(self):
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
1712 record_calls = []
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
1713 def fake_os_func(*args):
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
1714 record_calls.append(args)
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
1715 if len(record_calls) == 2:
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
1716 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
1717 return tuple(reversed(args))
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
1718
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
1719 self.assertEqual((999, 256),
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
1720 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
1721 self.assertEqual([(256, 999)], record_calls)
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
1722 # 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
1723 self.assertEqual((666,),
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
1724 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
1725 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
1726
59207
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1727
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
1728 @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
1729 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
1730
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
1731 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
1732 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
1733 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
1734 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
1735 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
1736 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
1737 )
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
1738 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
1739
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
1740 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
1741 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
1742 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
1743
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
1744 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
1745 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
1746 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
1747 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
1748 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
1749 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
1750 "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
1751 )
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
1752
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
1753 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
1754 # 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
1755 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
1756 "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
1757
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
1758 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
1759 # 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
1760 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
1761
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
1762 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
1763 # 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
1764 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
1765 "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
1766
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
1767 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
1768 # 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
1769 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
1770
66622
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1771
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
1772 class ContextManagerTests(BaseTestCase):
66622
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1773
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1774 def test_pipe(self):
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1775 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
1776 "import sys;"
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1777 "sys.stdout.write('stdout');"
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1778 "sys.stderr.write('stderr');"],
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1779 stdout=subprocess.PIPE,
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1780 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
1781 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
1782 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
1783
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1784 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
1785 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
1786
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1787 def test_returncode(self):
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1788 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
1789 "import sys; sys.exit(100)"]) as proc:
70049
b00a64a5cb93 merge - 7a3f3ad83676 Fixes Issue #12044.
Gregory P. Smith <greg@krypto.org>
parents: 69155
diff changeset
1790 pass
b00a64a5cb93 merge - 7a3f3ad83676 Fixes Issue #12044.
Gregory P. Smith <greg@krypto.org>
parents: 69155
diff changeset
1791 # __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
1792 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
1793
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1794 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
1795 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
1796 "import sys;"
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1797 "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
1798 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
1799 proc.communicate(b"context")
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1800 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
1801
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1802 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
1803 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
1804 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
1805 stdout=subprocess.PIPE,
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1806 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
1807 pass
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1808
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1809 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
1810 raise c.exception
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1811
59209
1f158238fa76 Merged revisions 78524 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55626
diff changeset
1812
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1813 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
1814 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
1815 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
1816 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
1817 ProcessTestCasePOSIXPurePython,
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
1818 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
1819 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
1820 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
1821 CommandsWithSpaces,
71485
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
1822 ContextManagerTests,
5cc536fbd7c1 Issue #12591: Improve support of "universal newlines" in the subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 71233
diff changeset
1823 )
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
1824
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
1825 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
1826 support.reap_children()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1827
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1828 if __name__ == "__main__":
68526
ad2bd8d338b0 Add unittests demonstrating issue #11432.
Gregory P. Smith <greg@krypto.org>
parents: 68492
diff changeset
1829 unittest.main()