annotate Lib/test/test_subprocess.py @ 66807:d28126ebd60d

Entries for datetime, callable, and collections.Counter.
author Raymond Hettinger <python@rcn.com>
date Thu, 09 Dec 2010 16:41:54 +0000
parents 2e1fcf28c914
children eab8f91f5754
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
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
6 import os
59207
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
7 import errno
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
8 import tempfile
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
9 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
10 import re
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
11 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
12 import warnings
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
13 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
14 import gc
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
15 except ImportError:
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
16 gc = None
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
17
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
18 mswindows = (sys.platform == "win32")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
19
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
20 #
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
21 # Depends on the following external programs: Python
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
22 #
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
23
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
24 if mswindows:
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
25 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
26 'os.O_BINARY);')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
27 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
28 SETBINARY = ''
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
29
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
30
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
31 try:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
32 mkstemp = tempfile.mkstemp
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
33 except AttributeError:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
34 # 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
35 def mkstemp():
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
36 """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
37 fname = tempfile.mktemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
38 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
39
33739
76355363101b New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents: 33738
diff changeset
40
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
41 class BaseTestCase(unittest.TestCase):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
42 def setUp(self):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
43 # 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
44 # 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
45 support.reap_children()
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
46
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
47 def tearDown(self):
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
48 for inst in subprocess._active:
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
49 inst.wait()
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
50 subprocess._cleanup()
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
51 self.assertFalse(subprocess._active, "subprocess._active not empty")
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
52
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
53 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
54 # 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
55 # 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
56 # 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
57 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
58 self.assertEqual(actual, expected, msg)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
59
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
60
66676
b37da60c9072 issue7213 + issue2320: Cause a DeprecationWarning if the close_fds argument is
Gregory P. Smith <greg@mad-scientist.com>
parents: 66622
diff changeset
61 class DeprecationWarningTests(BaseTestCase):
b37da60c9072 issue7213 + issue2320: Cause a DeprecationWarning if the close_fds argument is
Gregory P. Smith <greg@mad-scientist.com>
parents: 66622
diff changeset
62 def testCloseFdsWarning(self):
b37da60c9072 issue7213 + issue2320: Cause a DeprecationWarning if the close_fds argument is
Gregory P. Smith <greg@mad-scientist.com>
parents: 66622
diff changeset
63 quick_process = [sys.executable, "-c", "import sys; sys.exit(0)"]
66683
2e1fcf28c914 refactor the warning test.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66676
diff changeset
64 with warnings.catch_warnings(record=True) as warnlist:
2e1fcf28c914 refactor the warning test.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66676
diff changeset
65 warnings.simplefilter("always")
2e1fcf28c914 refactor the warning test.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66676
diff changeset
66 subprocess.call(quick_process, close_fds=True)
2e1fcf28c914 refactor the warning test.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66676
diff changeset
67 self.assertEqual([], warnlist)
2e1fcf28c914 refactor the warning test.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66676
diff changeset
68 subprocess.call(quick_process, close_fds=False)
2e1fcf28c914 refactor the warning test.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66676
diff changeset
69 self.assertEqual([], warnlist)
2e1fcf28c914 refactor the warning test.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66676
diff changeset
70 with self.assertWarns(DeprecationWarning) as wm:
2e1fcf28c914 refactor the warning test.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66676
diff changeset
71 subprocess.Popen(quick_process).wait()
2e1fcf28c914 refactor the warning test.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66676
diff changeset
72 self.assertEqual(1, len(wm.warnings))
2e1fcf28c914 refactor the warning test.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66676
diff changeset
73 self.assertIn('close_fds parameter was not specified',
2e1fcf28c914 refactor the warning test.
Gregory P. Smith <greg@mad-scientist.com>
parents: 66676
diff changeset
74 str(wm.warnings[0]))
66676
b37da60c9072 issue7213 + issue2320: Cause a DeprecationWarning if the close_fds argument is
Gregory P. Smith <greg@mad-scientist.com>
parents: 66622
diff changeset
75
b37da60c9072 issue7213 + issue2320: Cause a DeprecationWarning if the close_fds argument is
Gregory P. Smith <greg@mad-scientist.com>
parents: 66622
diff changeset
76
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
77 class ProcessTestCase(BaseTestCase):
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
78
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
79 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
80 # call() function with sequence argument
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
81 rc = subprocess.call([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
82 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
83 self.assertEqual(rc, 47)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
84
34302
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
85 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
86 # 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
87 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
88 "import sys; sys.exit(0)"])
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
89 self.assertEqual(rc, 0)
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
90
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
91 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
92 # 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
93 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
94 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
95 "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
96 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
97
50074
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
98 def test_check_output(self):
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
99 # 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
100 output = subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
101 [sys.executable, "-c", "print('BDFL')"])
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55626
diff changeset
102 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
103
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
104 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
105 # 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
106 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
107 subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
108 [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
109 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
110
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
111 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
112 # 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
113 output = subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
114 [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
115 stderr=subprocess.STDOUT)
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55626
diff changeset
116 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
117
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
118 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
119 # 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
120 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
121 output = subprocess.check_output(
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
122 [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
123 stdout=sys.stdout)
024a9897c651 Merged revisions 67511,67536-67537,67543 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49200
diff changeset
124 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
125 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
126
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
127 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
128 # call() function with keyword args
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
129 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
130 newenv["FRUIT"] = "banana"
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
131 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
132 'import sys, os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
133 '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
134 env=newenv)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
135 self.assertEqual(rc, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
136
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
137 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
138 # .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
139 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
140 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
141 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
142 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
143 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
144 self.assertEqual(p.stdin, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
145
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
146 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
147 # .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
148 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
149 '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
150 '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
151 '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
152 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
153 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
154 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
155 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
156 self.assertEqual(p.stdout, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
157
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
158 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
159 # .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
160 p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
161 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
162 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
163 self.addCleanup(p.stdin.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
164 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
165 self.assertEqual(p.stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
166
58931
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
167 def test_executable_with_cwd(self):
59507
a111024a0bcb Merged revisions 78833 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59504
diff changeset
168 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
169 p = subprocess.Popen(["somethingyoudonthave", "-c",
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
170 "import sys; sys.exit(47)"],
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
171 executable=sys.executable, cwd=python_dir)
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
172 p.wait()
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
173 self.assertEqual(p.returncode, 47)
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
174
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
175 @unittest.skipIf(sysconfig.is_python_build(),
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
176 "need an installed Python. See #7774")
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
177 def test_executable_without_cwd(self):
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
178 # 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
179 # 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
180 p = subprocess.Popen(["somethingyoudonthave", "-c",
4c0be37b46b0 Merged revisions 78136 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
181 "import sys; sys.exit(47)"],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
182 executable=sys.executable)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
183 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
184 self.assertEqual(p.returncode, 47)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
185
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
186 def test_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
187 # stdin redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
188 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
189 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
190 stdin=subprocess.PIPE)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
191 p.stdin.write(b"pear")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
192 p.stdin.close()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
193 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
194 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
195
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
196 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
197 # stdin is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
198 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
199 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
200 d = tf.fileno()
49200
bdb660dca74d Issue #3782: os.write() must not accept unicode strings
Antoine Pitrou <solipsis@pitrou.net>
parents: 48395
diff changeset
201 os.write(d, b"pear")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
202 os.lseek(d, 0, 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
203 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
204 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
205 stdin=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
206 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
207 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
208
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
209 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
210 # stdin is set to open file object
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
211 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
212 self.addCleanup(tf.close)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
213 tf.write(b"pear")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
214 tf.seek(0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
215 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
216 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
217 stdin=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
218 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
219 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
220
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
221 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
222 # stdout redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
223 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
224 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
225 stdout=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
226 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
227 self.assertEqual(p.stdout.read(), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
228
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
229 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
230 # stdout is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
231 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
232 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
233 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
234 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
235 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
236 stdout=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
237 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
238 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
239 self.assertEqual(os.read(d, 1024), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
240
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
241 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
242 # stdout is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
243 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
244 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
245 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
246 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
247 stdout=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
248 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
249 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
250 self.assertEqual(tf.read(), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
251
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
252 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
253 # stderr redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
254 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
255 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
256 stderr=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
257 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
258 self.assertStderrEqual(p.stderr.read(), b"strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
259
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
260 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
261 # stderr is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
262 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
263 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
264 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
265 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
266 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
267 stderr=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
268 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
269 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
270 self.assertStderrEqual(os.read(d, 1024), b"strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
271
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
272 def test_stderr_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
273 # stderr is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
274 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
275 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
276 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
277 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
278 stderr=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
279 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
280 tf.seek(0)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
281 self.assertStderrEqual(tf.read(), b"strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
282
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
283 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
284 # capture stdout and stderr to the same pipe
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
285 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
286 'import sys;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
287 '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
288 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
289 '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
290 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
291 stderr=subprocess.STDOUT)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
292 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
293 self.assertStderrEqual(p.stdout.read(), b"appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
294
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
295 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
296 # 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
297 tf = tempfile.TemporaryFile()
65799
0ee86fd902f1 close some more files
Benjamin Peterson <benjamin@python.org>
parents: 65415
diff changeset
298 self.addCleanup(tf.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
299 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
300 'import sys;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
301 '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
302 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
303 '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
304 stdout=tf,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
305 stderr=tf)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
306 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
307 tf.seek(0)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
308 self.assertStderrEqual(tf.read(), b"appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
309
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
310 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
311 # 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
312 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
313 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
314 self.assertEqual(rc, 2)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
315
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
316 def test_cwd(self):
44373
2750d531fdc0 Merged revisions 59565-59594 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
317 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
318 # 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
319 # 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
320 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
321 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
322 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
323 os.chdir(cwd)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
324 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
325 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
326 '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
327 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
328 cwd=tmpdir)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
329 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
330 normcase = os.path.normcase
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
331 self.assertEqual(normcase(p.stdout.read().decode("utf-8")),
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
332 normcase(tmpdir))
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
333
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
334 def test_env(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
335 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
336 newenv["FRUIT"] = "orange"
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
337 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
338 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
339 'sys.stdout.write(os.getenv("FRUIT"))'],
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
340 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
341 env=newenv)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
342 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
343 self.assertEqual(p.stdout.read(), b"orange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
344
34671
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
345 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
346 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
347 'import sys;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
348 '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
349 stdin=subprocess.PIPE)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
350 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
351 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
352
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
353 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
354 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
355 '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
356 stdout=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
357 (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
358 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
359 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
360
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
361 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
362 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
363 '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
364 stderr=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
365 (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
366 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
367 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
368
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
369 def test_communicate(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
370 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
371 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
372 '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
373 '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
374 stdin=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
375 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
376 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
377 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
378 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
379 self.addCleanup(p.stdin.close)
48395
bb65f9b76c1f #2683: Popen.communicate() argument must be bytes.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
380 (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
381 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
382 self.assertStderrEqual(stderr, b"pineapple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
383
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
384 # This test is Linux specific for simplicity to at least have
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
385 # some coverage. It is not a platform specific bug.
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
386 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
387 "Linux specific")
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
388 # 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
389 def test_communicate_pipe_fd_leak(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
390 fd_directory = '/proc/%d/fd' % os.getpid()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
391 num_fds_before_popen = len(os.listdir(fd_directory))
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
392 p = subprocess.Popen([sys.executable, "-c", "print()"],
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
393 stdout=subprocess.PIPE)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
394 p.communicate()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
395 num_fds_after_communicate = len(os.listdir(fd_directory))
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
396 del p
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
397 num_fds_after_destruction = len(os.listdir(fd_directory))
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
398 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
399 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
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
400
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
401 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
402 # communicate() should return None if no redirection is active
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
403 p = subprocess.Popen([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
404 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
405 (stdout, stderr) = p.communicate()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
406 self.assertEqual(stdout, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
407 self.assertEqual(stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
408
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
409 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
410 # communicate() with writes larger than pipe_buf
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
411 # This test will probably deadlock rather than fail, if
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
412 # communicate() does not work properly.
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
413 x, y = os.pipe()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
414 if mswindows:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
415 pipe_buf = 512
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
416 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
417 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
418 os.close(x)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
419 os.close(y)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
420 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
421 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
422 '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
423 '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
424 '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
425 stdin=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
426 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
427 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
428 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
429 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
430 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
431 string_to_write = b"abc"*pipe_buf
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
432 (stdout, stderr) = p.communicate(string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
433 self.assertEqual(stdout, string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
434
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
435 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
436 # stdin.write before communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
437 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
438 'import sys,os;'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
439 '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
440 stdin=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
441 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
442 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
443 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
444 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
445 self.addCleanup(p.stdin.close)
42927
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
446 p.stdin.write(b"banana")
f3b0ac4e684f More str/bytes goodness.
Guido van Rossum <guido@python.org>
parents: 42863
diff changeset
447 (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
448 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
449 self.assertStderrEqual(stderr, b"")
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
450
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
451 def test_universal_newlines(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
452 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
453 '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
454 'sys.stdout.write("line1\\n");'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
455 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
456 '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
457 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
458 'sys.stdout.write("line3\\r\\n");'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
459 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
460 'sys.stdout.write("line4\\r");'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
461 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
462 'sys.stdout.write("\\nline5");'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
463 'sys.stdout.flush();'
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("\\nline6");'],
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
465 stdout=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
466 universal_newlines=1)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
467 self.addCleanup(p.stdout.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
468 stdout = p.stdout.read()
41732
bbb099d4ec80 Make test_subprocess pass. The subprocess module now reads and writes
Guido van Rossum <guido@python.org>
parents: 40788
diff changeset
469 self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
470
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
471 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
472 # universal newlines through communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
473 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
474 '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
475 'sys.stdout.write("line1\\n");'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
476 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
477 '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
478 'sys.stdout.flush();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
479 'sys.stdout.write("line3\\r\\n");'
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("line4\\r");'
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();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
483 'sys.stdout.write("\\nline5");'
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();'
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
485 'sys.stdout.write("\\nline6");'],
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
486 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43684
diff changeset
487 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
488 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
489 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
490 (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
491 self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
492
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
493 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
494 # Make sure we leak no resources
64813
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
495 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
496 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
497 else:
64813
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
498 max_handles = 2050 # too much for (at least some) Windows setups
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
499 handles = []
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
500 try:
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
501 for i in range(max_handles):
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
502 try:
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
503 handles.append(os.open(support.TESTFN,
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
504 os.O_WRONLY | os.O_CREAT))
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
505 except OSError as e:
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
506 if e.errno != errno.EMFILE:
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
507 raise
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
508 break
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
509 else:
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
510 self.skipTest("failed to reach the file descriptor limit "
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
511 "(tried %d)" % max_handles)
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
512 # Close a couple of them (should be enough for a subprocess)
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
513 for i in range(10):
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
514 os.close(handles.pop())
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
515 # Loop creating some subprocesses. If one of them leaks some fds,
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
516 # the next loop iteration will fail by reaching the max fd limit.
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
517 for i in range(15):
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
518 p = subprocess.Popen([sys.executable, "-c",
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
519 "import sys;"
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
520 "sys.stdout.write(sys.stdin.read())"],
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
521 stdin=subprocess.PIPE,
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
522 stdout=subprocess.PIPE,
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
523 stderr=subprocess.PIPE)
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
524 data = p.communicate(b"lime")[0]
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
525 self.assertEqual(data, b"lime")
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
526 finally:
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
527 for h in handles:
4981cf0a9ca4 Issue #9895: speed up test_subprocess
Antoine Pitrou <solipsis@pitrou.net>
parents: 64808
diff changeset
528 os.close(h)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
529
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
530 def test_list2cmdline(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
531 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
532 '"a b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
533 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
534 'ab\\"c \\ d')
44810
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44373
diff changeset
535 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44373
diff changeset
536 'ab\\"c " \\\\" d')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
537 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
538 'a\\\\\\b "de fg" h')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
539 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
540 'a\\\\\\"b c d')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
541 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
542 '"a\\\\b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
543 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
544 '"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
545 self.assertEqual(subprocess.list2cmdline(['ab', '']),
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
546 'ab ""')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
547
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_poll(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
550 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
551 "-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
552 count = 0
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
553 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
554 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
555 count += 1
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
556 # 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
557 # 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
558 # 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
559 # 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
560 self.assertGreaterEqual(count, 2)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
561 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
562 self.assertEqual(p.poll(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
563
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
564
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
565 def test_wait(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
566 p = subprocess.Popen([sys.executable,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
567 "-c", "import time; time.sleep(2)"])
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
568 self.assertEqual(p.wait(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
569 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
570 self.assertEqual(p.wait(), 0)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
571
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
572
79d2ae0a62ef Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents: 33998
diff changeset
573 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
574 # 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
575 # TypeError.
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
576 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
577 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
578
41984
e420039368b7 The bufsize argument to Popen() should accept None meaning the default (0).
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
579 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
580 # 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
581 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
582 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
583 # 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
584 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
585 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
586
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
587 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
588 # 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
589 # 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
590 # 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
591 # 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
592 # 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
593 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
594 # 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
595 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
596 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
597 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
598 stderr=subprocess.PIPE)
64808
b49aee38779b Issue #9894: Do not hardcode ENOENT in test_subprocess.
Antoine Pitrou <solipsis@pitrou.net>
parents: 63956
diff changeset
599 if c.exception.errno != errno.ENOENT: # ignore "no such file"
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
600 raise c.exception
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
601
61793
441491572fce Issue #8780: Fix a regression introduced by r78946 in subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61693
diff changeset
602 def test_issue8780(self):
441491572fce Issue #8780: Fix a regression introduced by r78946 in subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61693
diff changeset
603 # Ensure that stdout is inherited from the parent
441491572fce Issue #8780: Fix a regression introduced by r78946 in subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61693
diff changeset
604 # if stdout=PIPE is not used
441491572fce Issue #8780: Fix a regression introduced by r78946 in subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61693
diff changeset
605 code = ';'.join((
441491572fce Issue #8780: Fix a regression introduced by r78946 in subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61693
diff changeset
606 'import subprocess, sys',
441491572fce Issue #8780: Fix a regression introduced by r78946 in subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61693
diff changeset
607 'retcode = subprocess.call('
441491572fce Issue #8780: Fix a regression introduced by r78946 in subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61693
diff changeset
608 "[sys.executable, '-c', 'print(\"Hello World!\")'])",
441491572fce Issue #8780: Fix a regression introduced by r78946 in subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61693
diff changeset
609 'assert retcode == 0'))
441491572fce Issue #8780: Fix a regression introduced by r78946 in subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61693
diff changeset
610 output = subprocess.check_output([sys.executable, '-c', code])
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66307
diff changeset
611 self.assertTrue(output.startswith(b'Hello World!'), ascii(output))
61793
441491572fce Issue #8780: Fix a regression introduced by r78946 in subprocess on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61693
diff changeset
612
63772
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
613 def test_handles_closed_on_exception(self):
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
614 # If CreateProcess exits with an error, ensure the
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
615 # duplicate output handles are released
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
616 ifhandle, ifname = mkstemp()
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
617 ofhandle, ofname = mkstemp()
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
618 efhandle, efname = mkstemp()
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
619 try:
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
620 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
621 stderr=efhandle)
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
622 except OSError:
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
623 os.close(ifhandle)
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
624 os.remove(ifname)
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
625 os.close(ofhandle)
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
626 os.remove(ofname)
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
627 os.close(efhandle)
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
628 os.remove(efname)
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
629 self.assertFalse(os.path.exists(ifname))
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
630 self.assertFalse(os.path.exists(ofname))
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
631 self.assertFalse(os.path.exists(efname))
ad292fce79f9 Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63727
diff changeset
632
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
633
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
634 # context manager
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
635 class _SuppressCoreFiles(object):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
636 """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
637 old_limit = None
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
638
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
639 def __enter__(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
640 """Try to save previous ulimit, then set it to (0, 0)."""
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
641 try:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
642 import resource
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
643 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
644 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
645 except (ImportError, ValueError, resource.error):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
646 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
647
63141
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
648 if sys.platform == 'darwin':
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
649 # 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
650 # 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
651 # when it is.
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
652 #
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
653 # 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
654 # that might trigger the next manager.
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
655 value = subprocess.Popen(['/usr/bin/defaults', 'read',
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
656 'com.apple.CrashReporter', 'DialogType'],
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
657 stdout=subprocess.PIPE).communicate()[0]
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
658 if value.strip() == b'developer':
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
659 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
660 "that is intentional", end='')
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
661 sys.stdout.flush()
2ad8e8ce46c6 Workaround for issue 4047: in some configurations of
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63094
diff changeset
662
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
663 def __exit__(self, *args):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
664 """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
665 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
666 return
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
667 try:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
668 import resource
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
669 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
670 except (ImportError, ValueError, resource.error):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
671 pass
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
672
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
673
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
674 @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
675 class POSIXProcessTestCase(BaseTestCase):
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
676
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
677 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
678 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
679 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
680 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
681 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
682 # 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
683 # 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
684 # 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
685 desired_exception = e
66307
c06ed25e69b8 add filename to ENOENT message #4925
Benjamin Peterson <benjamin@python.org>
parents: 65977
diff changeset
686 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
687 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
688 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
689 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
690
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
691 # 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
692 try:
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
693 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
694 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
695 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
696 # 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
697 # 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
698 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
699 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
700 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
701 self.fail("Expected OSError: %s" % desired_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
702
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
703 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
704 # 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
705 # 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
706 # 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
707 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
708 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
709
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
710 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
711 # 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
712 # 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
713 # 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
714 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
715 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
716 [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
717 "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
718 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
719 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
720 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
721 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
722 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
723 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
724 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
725 self.assertNotEqual(parent_pgid, child_pgid)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
726
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
727 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
728 # returncode handles signal termination
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
729 with _SuppressCoreFiles():
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
730 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
731 'import os; os.abort()'])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
732 p.wait()
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
733 self.assertEqual(-p.returncode, signal.SIGABRT)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
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 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
736 # 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
737 # 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
738 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
739 'import sys,os;'
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
740 '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
741 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
742 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
743 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
744 self.assertEqual(p.stdout.read(), b"apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
745
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
746 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
747 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
748 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
749 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
750 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
751 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
752 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
753 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
754 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
755 "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
756 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
757 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
758 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
759 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
760 "to the parent process.")
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
761
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
762 @unittest.skipUnless(gc, "Requires a gc module.")
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
763 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
764 # 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
765 # 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
766 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
767 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
768 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
769 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
770 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
771 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
772 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
773 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
774 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
775 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
776 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
777 "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
778
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
779 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
780 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
781 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
782 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
783 self.assertTrue(gc.isenabled(), "Popen left gc disabled.")
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
784
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
785 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
786 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
787 [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
788 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
789
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
790 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
791 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
792 [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
793 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
794 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
795 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
796 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
797 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
798 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
799
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
800 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
801 # args is a string
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
802 fd, fname = mkstemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
803 # 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
804 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
805 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
806 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
807 sys.executable)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
808 os.chmod(fname, 0o700)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
809 p = subprocess.Popen(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
810 p.wait()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
811 os.remove(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
812 self.assertEqual(p.returncode, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
813
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
814 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
815 # 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
816 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
817 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
818 "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
819 startupinfo=47)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
820 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
821 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
822 "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
823 creationflags=47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
824
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
825 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
826 # 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
827 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
828 newenv["FRUIT"] = "apple"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
829 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
830 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
831 env=newenv)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
832 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
833 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
834
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
835 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
836 # 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
837 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
838 newenv["FRUIT"] = "apple"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
839 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
840 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
841 env=newenv)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
842 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
843 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
844
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
845 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
846 # 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
847 fd, fname = mkstemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
848 # 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
849 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
850 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
851 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
852 sys.executable)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
853 os.chmod(fname, 0o700)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
854 rc = subprocess.call(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
855 os.remove(fname)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
856 self.assertEqual(rc, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
857
63094
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
858 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
859 # 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
860 shells = []
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
861 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
862 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
863 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
864 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
865 shells.append(sh)
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
866 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
867 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
868 sh = '/bin/sh'
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
869 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
870 # 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
871 shells.append(sh)
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
872 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
873 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
874 stdout=subprocess.PIPE)
65955
6e1ae323fde6 Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65799
diff changeset
875 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
876 self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
aa0b56dd14af Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62402
diff changeset
877
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
878 def _kill_process(self, method, *args):
59376
d9e6f318568f Merged revisions 78701 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59353
diff changeset
879 # 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
880 # 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
881 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
882 import sys, time
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
883 sys.stdout.write('x\\n')
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
884 sys.stdout.flush()
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
885 time.sleep(30)
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
886 """],
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
887 close_fds=True,
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
888 stdin=subprocess.PIPE,
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
889 stdout=subprocess.PIPE,
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
890 stderr=subprocess.PIPE)
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
891 # 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
892 # sending any signal.
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
893 p.stdout.read(1)
76fd68bba1a8 Try a more robust implementation of _kill_process
Antoine Pitrou <solipsis@pitrou.net>
parents: 64842
diff changeset
894 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
895 return p
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
896
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
897 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
898 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
899 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
900 self.assertIn(b'KeyboardInterrupt', stderr)
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
901 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
902
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
903 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
904 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
905 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
906 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
907 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
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_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
910 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
911 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
912 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
913 self.assertEqual(p.wait(), -signal.SIGTERM)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
914
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
915 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
916 def prepare():
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
917 raise ValueError("surrogate:\uDCff")
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
918
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
919 try:
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
920 subprocess.call(
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
921 [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
922 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
923 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
924 # 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
925 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
926 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
927 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
928 # _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
929 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
930 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
931 else:
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
932 self.fail("Expected ValueError or RuntimeError")
8428eaa2d3ad Issue #8467: Pure Python implementation of subprocess encodes the error message
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60057
diff changeset
933
60902
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
934 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
935 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
936 # 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
937 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
938 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
939 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
940 # 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
941 # 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
942 # 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
943 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
944 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
945 [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
946 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
947 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
948 self.assertEqual(stdout.decode('ascii'), ascii(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
949
dc8c8d54504e Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60896
diff changeset
950 # 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
951 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
952 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
953 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
954 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
955 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
956 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
957 [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
958 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
959 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
960 self.assertEqual(stdout.decode('ascii'), ascii(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
961
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
962 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
963 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
964 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
965 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
966
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
967 # 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
968 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
969 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
970
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
971 # 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
972 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
973 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
974 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
975 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
976
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61332
diff changeset
977 # 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
978 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
979 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
980 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
981 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
982
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
983
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
984 @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
985 class Win32ProcessTestCase(BaseTestCase):
59340
04955e7eb597 Merged revisions 78661-78662 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59216
diff changeset
986
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
987 def test_startupinfo(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
988 # startupinfo argument
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
989 # 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
990 # depend on win32all.
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
991 STARTF_USESHOWWINDOW = 1
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
992 SW_MAXIMIZE = 3
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
993 startupinfo = subprocess.STARTUPINFO()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
994 startupinfo.dwFlags = STARTF_USESHOWWINDOW
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
995 startupinfo.wShowWindow = SW_MAXIMIZE
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
996 # 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
997 # 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
998 # ignored
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
999 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
1000 startupinfo=startupinfo)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1001
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1002 def test_creationflags(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1003 # creationflags argument
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1004 CREATE_NEW_CONSOLE = 16
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1005 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
1006 subprocess.call(sys.executable +
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1007 ' -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
1008 creationflags=CREATE_NEW_CONSOLE)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1009
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1010 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
1011 # 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
1012 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
1013 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1014 "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
1015 preexec_fn=lambda: 1)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1016 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
1017 [sys.executable, "-c",
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1018 "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
1019 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1020 close_fds=True)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1021
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1022 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
1023 # close file descriptors
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1024 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
1025 "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
1026 close_fds=True)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1027 self.assertEqual(rc, 47)
41969
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41845
diff changeset
1028
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1029 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
1030 # 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
1031 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1032 newenv["FRUIT"] = "physalis"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1033 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
1034 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1035 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
1036 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
1037 self.assertIn(b"physalis", p.stdout.read())
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1038
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1039 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
1040 # 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
1041 newenv = os.environ.copy()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1042 newenv["FRUIT"] = "physalis"
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1043 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
1044 stdout=subprocess.PIPE,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1045 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
1046 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
1047 self.assertIn(b"physalis", p.stdout.read())
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1048
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1049 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
1050 # 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
1051 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
1052 ' -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
1053 self.assertEqual(rc, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1054
59471
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
1055 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
1056 # 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
1057 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
1058 import sys, time
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1059 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
1060 sys.stdout.flush()
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1061 time.sleep(30)
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1062 """],
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1063 stdin=subprocess.PIPE,
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1064 stdout=subprocess.PIPE,
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1065 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
1066 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
1067 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
1068 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
1069 # 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
1070 # sending any signal.
f6a0eb730a9b Make _kill_process more robust under Windows too (see issue #8432)
Antoine Pitrou <solipsis@pitrou.net>
parents: 64843
diff changeset
1071 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
1072 getattr(p, method)(*args)
60057
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1073 _, stderr = p.communicate()
bb2ca1b63321 Merged revisions 79344,79346,79350 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59723
diff changeset
1074 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
1075 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
1076 self.assertNotEqual(returncode, 0)
ac80884ddcc3 Merged revisions 78736,78759,78761,78767,78788-78789 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59398
diff changeset
1077
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1078 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
1079 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
1080
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1081 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
1082 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
1083
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1084 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
1085 self._kill_process('terminate')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1086
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1087
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
1088 # The module says:
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
1089 # "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
1090 #
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
1091 # 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
1092 # 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
1093 @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
1094 class CommandTests(unittest.TestCase):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1095 def test_getoutput(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1096 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
1097 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
1098 (0, 'xyzzy'))
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
1099
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1100 # 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
1101 # 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
1102 # 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
1103 dir = None
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1104 try:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1105 dir = tempfile.mkdtemp()
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1106 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
1107
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1108 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
1109 self.assertNotEqual(status, 0)
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1110 finally:
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1111 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
1112 os.rmdir(dir)
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
1113
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
1114
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1115 @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
1116 "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
1117 class ProcessTestCaseNoPoll(ProcessTestCase):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1118 def setUp(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1119 subprocess._has_poll = False
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1120 ProcessTestCase.setUp(self)
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
1121
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1122 def tearDown(self):
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1123 subprocess._has_poll = True
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1124 ProcessTestCase.tearDown(self)
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
1125
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
1126
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
1127 @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
1128 "_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
1129 class ProcessTestCasePOSIXPurePython(ProcessTestCase, POSIXProcessTestCase):
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1130 def setUp(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
1131 subprocess._posixsubprocess = None
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1132 ProcessTestCase.setUp(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
1133 POSIXProcessTestCase.setUp(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
1134
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1135 def tearDown(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
1136 subprocess._posixsubprocess = sys.modules['_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
1137 POSIXProcessTestCase.tearDown(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
1138 ProcessTestCase.tearDown(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
1139
768722b2ae0a * Replaces the internals of the subprocess module from fork through exec on
Gregory P. Smith <greg@mad-scientist.com>
parents: 59507
diff changeset
1140
59207
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1141 class HelperFunctionTests(unittest.TestCase):
59216
924ca295b146 Merged revisions 78532 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59207
diff changeset
1142 @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
59207
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1143 def test_eintr_retry_call(self):
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1144 record_calls = []
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1145 def fake_os_func(*args):
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1146 record_calls.append(args)
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1147 if len(record_calls) == 2:
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1148 raise OSError(errno.EINTR, "fake interrupted system call")
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1149 return tuple(reversed(args))
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1150
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1151 self.assertEqual((999, 256),
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1152 subprocess._eintr_retry_call(fake_os_func, 256, 999))
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1153 self.assertEqual([(256, 999)], record_calls)
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1154 # This time there will be an EINTR so it will loop once.
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1155 self.assertEqual((666,),
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1156 subprocess._eintr_retry_call(fake_os_func, 666))
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1157 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1158
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1159
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
1160 @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
1161 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
1162
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
1163 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
1164 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
1165 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
1166 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
1167 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
1168 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
1169 )
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
1170 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
1171
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
1172 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
1173 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
1174 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
1175
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
1176 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
1177 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
1178 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
1179 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
1180 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
1181 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
1182 "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
1183 )
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
1184
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
1185 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
1186 # 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
1187 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
1188 "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
1189
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
1190 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
1191 # 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
1192 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
1193
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
1194 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
1195 # 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
1196 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
1197 "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
1198
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
1199 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
1200 # 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
1201 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
1202
66622
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1203
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1204 class ContextManagerTests(ProcessTestCase):
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1205
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1206 def test_pipe(self):
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1207 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
1208 "import sys;"
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1209 "sys.stdout.write('stdout');"
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1210 "sys.stderr.write('stderr');"],
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1211 stdout=subprocess.PIPE,
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1212 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
1213 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
1214 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
1215
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1216 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
1217 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
1218
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1219 def test_returncode(self):
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1220 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
1221 "import sys; sys.exit(100)"]) as proc:
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1222 proc.wait()
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1223 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
1224
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1225 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
1226 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
1227 "import sys;"
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1228 "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
1229 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
1230 proc.communicate(b"context")
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1231 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
1232
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1233 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
1234 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
1235 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
1236 stdout=subprocess.PIPE,
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1237 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
1238 pass
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1239
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1240 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
1241 raise c.exception
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1242
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1243
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1244 def test_main():
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1245 unit_tests = (ProcessTestCase,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1246 POSIXProcessTestCase,
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1247 Win32ProcessTestCase,
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
1248 ProcessTestCasePOSIXPurePython,
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1249 CommandTests,
59207
55778855cd25 Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59193
diff changeset
1250 ProcessTestCaseNoPoll,
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
1251 HelperFunctionTests,
66622
e02021fbfe22 Fix #10554. Added context manager support to Popen objects.
Brian Curtin <brian.curtin@gmail.com>
parents: 66310
diff changeset
1252 CommandsWithSpaces,
66676
b37da60c9072 issue7213 + issue2320: Cause a DeprecationWarning if the close_fds argument is
Gregory P. Smith <greg@mad-scientist.com>
parents: 66622
diff changeset
1253 ContextManagerTests,
b37da60c9072 issue7213 + issue2320: Cause a DeprecationWarning if the close_fds argument is
Gregory P. Smith <greg@mad-scientist.com>
parents: 66622
diff changeset
1254 DeprecationWarningTests)
59193
01c58cef82d4 Recorded merge of revisions 78508-78509 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58931
diff changeset
1255
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
1256 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
1257 support.reap_children()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1258
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1259 if __name__ == "__main__":
47331
0525cd5b2582 The commands module has been removed. The getoutput() and getstatusoutput()
Brett Cannon <bcannon@gmail.com>
parents: 47303
diff changeset
1260 test_main()