annotate Lib/test/test_subprocess.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 715b3cbcb438
children e67620048d2f
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
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2 from test import test_support
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
59208
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
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
58856
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
11 import sysconfig
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
12
73922
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
13 try:
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
14 import resource
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
15 except ImportError:
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
16 resource = None
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
17
33723
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
33739
76355363101b New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents: 33738
diff changeset
30
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
31 try:
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
32 mkstemp = tempfile.mkstemp
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
33 except AttributeError:
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
34 # tempfile.mkstemp is not available
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
35 def mkstemp():
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
36 """Replacement for mkstemp, calling mktemp."""
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
37 fname = tempfile.mktemp()
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
38 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
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
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
41 class BaseTestCase(unittest.TestCase):
38961
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38916
diff changeset
42 def setUp(self):
38967
a21e420a597c Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38961
diff changeset
43 # Try to minimize the number of children we have so this test
a21e420a597c Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38961
diff changeset
44 # doesn't crash on some buildbots (Alphas in particular).
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
45 test_support.reap_children()
38961
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38916
diff changeset
46
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38916
diff changeset
47 def tearDown(self):
59339
d8b2fa88788b #2777: Enable test_send_signal, test_kill and test_terminate on all platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59338
diff changeset
48 for inst in subprocess._active:
d8b2fa88788b #2777: Enable test_send_signal, test_kill and test_terminate on all platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59338
diff changeset
49 inst.wait()
d8b2fa88788b #2777: Enable test_send_signal, test_kill and test_terminate on all platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59338
diff changeset
50 subprocess._cleanup()
d8b2fa88788b #2777: Enable test_send_signal, test_kill and test_terminate on all platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59338
diff changeset
51 self.assertFalse(subprocess._active, "subprocess._active not empty")
38961
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38916
diff changeset
52
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
53 def assertStderrEqual(self, stderr, expected, msg=None):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
54 # In a debug build, stuff like "[6580 refs]" is printed to stderr at
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
55 # shutdown time. That frustrates tests trying to check stderr produced
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
56 # from a spawned Python process.
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
57 actual = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
58 self.assertEqual(actual, expected, msg)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
59
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
60
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
61 class ProcessTestCase(BaseTestCase):
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
62
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
63 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
64 # call() function with sequence argument
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
65 rc = subprocess.call([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
66 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
67 self.assertEqual(rc, 47)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
68
34302
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
69 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
70 # 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
71 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
72 "import sys; sys.exit(0)"])
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
73 self.assertEqual(rc, 0)
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
74
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
75 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
76 # check_call() function with non-zero return code
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
77 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
78 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
79 "import sys; sys.exit(47)"])
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
80 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
81
49984
589cde614b5b rename the new check_call_output to check_output. its less ugly.
Gregory P. Smith <greg@mad-scientist.com>
parents: 49977
diff changeset
82 def test_check_output(self):
589cde614b5b rename the new check_call_output to check_output. its less ugly.
Gregory P. Smith <greg@mad-scientist.com>
parents: 49977
diff changeset
83 # check_output() function with zero return code
589cde614b5b rename the new check_call_output to check_output. its less ugly.
Gregory P. Smith <greg@mad-scientist.com>
parents: 49977
diff changeset
84 output = subprocess.check_output(
49977
4790b31d9257 Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents: 47338
diff changeset
85 [sys.executable, "-c", "print 'BDFL'"])
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55178
diff changeset
86 self.assertIn('BDFL', output)
49977
4790b31d9257 Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents: 47338
diff changeset
87
49984
589cde614b5b rename the new check_call_output to check_output. its less ugly.
Gregory P. Smith <greg@mad-scientist.com>
parents: 49977
diff changeset
88 def test_check_output_nonzero(self):
49977
4790b31d9257 Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents: 47338
diff changeset
89 # check_call() function with non-zero return code
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
90 with self.assertRaises(subprocess.CalledProcessError) as c:
49984
589cde614b5b rename the new check_call_output to check_output. its less ugly.
Gregory P. Smith <greg@mad-scientist.com>
parents: 49977
diff changeset
91 subprocess.check_output(
49977
4790b31d9257 Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents: 47338
diff changeset
92 [sys.executable, "-c", "import sys; sys.exit(5)"])
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
93 self.assertEqual(c.exception.returncode, 5)
49977
4790b31d9257 Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents: 47338
diff changeset
94
49984
589cde614b5b rename the new check_call_output to check_output. its less ugly.
Gregory P. Smith <greg@mad-scientist.com>
parents: 49977
diff changeset
95 def test_check_output_stderr(self):
589cde614b5b rename the new check_call_output to check_output. its less ugly.
Gregory P. Smith <greg@mad-scientist.com>
parents: 49977
diff changeset
96 # check_output() function stderr redirected to stdout
589cde614b5b rename the new check_call_output to check_output. its less ugly.
Gregory P. Smith <greg@mad-scientist.com>
parents: 49977
diff changeset
97 output = subprocess.check_output(
49977
4790b31d9257 Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents: 47338
diff changeset
98 [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
4790b31d9257 Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents: 47338
diff changeset
99 stderr=subprocess.STDOUT)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55178
diff changeset
100 self.assertIn('BDFL', output)
49977
4790b31d9257 Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents: 47338
diff changeset
101
49984
589cde614b5b rename the new check_call_output to check_output. its less ugly.
Gregory P. Smith <greg@mad-scientist.com>
parents: 49977
diff changeset
102 def test_check_output_stdout_arg(self):
589cde614b5b rename the new check_call_output to check_output. its less ugly.
Gregory P. Smith <greg@mad-scientist.com>
parents: 49977
diff changeset
103 # check_output() function stderr redirected to stdout
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
104 with self.assertRaises(ValueError) as c:
49984
589cde614b5b rename the new check_call_output to check_output. its less ugly.
Gregory P. Smith <greg@mad-scientist.com>
parents: 49977
diff changeset
105 output = subprocess.check_output(
49977
4790b31d9257 Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents: 47338
diff changeset
106 [sys.executable, "-c", "print 'will not be run'"],
4790b31d9257 Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents: 47338
diff changeset
107 stdout=sys.stdout)
4790b31d9257 Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents: 47338
diff changeset
108 self.fail("Expected ValueError when stdout arg supplied.")
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
109 self.assertIn('stdout', c.exception.args[0])
34302
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
110
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
111 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
112 # call() function with keyword args
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
113 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
114 newenv["FRUIT"] = "banana"
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
115 rc = subprocess.call([sys.executable, "-c",
59338
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
116 'import sys, os;'
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
117 'sys.exit(os.getenv("FRUIT")=="banana")'],
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
118 env=newenv)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
119 self.assertEqual(rc, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
120
70556
26ea0a46aadd Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69153
diff changeset
121 def test_invalid_args(self):
26ea0a46aadd Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69153
diff changeset
122 # Popen() called with invalid arguments should raise TypeError
26ea0a46aadd Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69153
diff changeset
123 # but Popen.__del__ should not complain (issue #12085)
70558
07b43607a905 Issue #12085: Fix test_subprocess for my previous commit
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70556
diff changeset
124 with test_support.captured_stderr() as s:
70556
26ea0a46aadd Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69153
diff changeset
125 self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
26ea0a46aadd Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69153
diff changeset
126 argcount = subprocess.Popen.__init__.__code__.co_argcount
26ea0a46aadd Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69153
diff changeset
127 too_many_args = [0] * (argcount + 1)
26ea0a46aadd Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69153
diff changeset
128 self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
26ea0a46aadd Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69153
diff changeset
129 self.assertEqual(s.getvalue(), '')
26ea0a46aadd Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69153
diff changeset
130
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
131 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
132 # .stdin is None when not redirected
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
133 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
134 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
135 self.addCleanup(p.stdout.close)
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
136 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
137 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
138 self.assertEqual(p.stdin, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
139
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
140 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
141 # .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
142 p = subprocess.Popen([sys.executable, "-c",
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
143 'print " this bit of output is from a '
e4f7d3b0c5d4 test_stdout_none(): Don't print "banana" to the screen in the middle
Tim Peters <tim.peters@gmail.com>
parents: 33736
diff changeset
144 'test of stdout in a different '
e4f7d3b0c5d4 test_stdout_none(): Don't print "banana" to the screen in the middle
Tim Peters <tim.peters@gmail.com>
parents: 33736
diff changeset
145 'process ..."'],
e4f7d3b0c5d4 test_stdout_none(): Don't print "banana" to the screen in the middle
Tim Peters <tim.peters@gmail.com>
parents: 33736
diff changeset
146 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
147 self.addCleanup(p.stdin.close)
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
148 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
149 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
150 self.assertEqual(p.stdout, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
151
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
152 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
153 # .stderr is None when not redirected
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
154 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
155 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
156 self.addCleanup(p.stdout.close)
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
157 self.addCleanup(p.stdin.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
158 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
159 self.assertEqual(p.stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
160
58856
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
161 def test_executable_with_cwd(self):
59506
6e37e127581b Revert r78830: realpath() should really be applied to sys.executable.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59503
diff changeset
162 python_dir = os.path.dirname(os.path.realpath(sys.executable))
58856
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
163 p = subprocess.Popen(["somethingyoudonthave", "-c",
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
164 "import sys; sys.exit(47)"],
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
165 executable=sys.executable, cwd=python_dir)
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
166 p.wait()
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
167 self.assertEqual(p.returncode, 47)
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
168
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
169 @unittest.skipIf(sysconfig.is_python_build(),
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
170 "need an installed Python. See #7774")
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
171 def test_executable_without_cwd(self):
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
172 # For a normal installation, it should work without 'cwd'
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
173 # argument. For test runs in the build directory, see #7774.
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
174 p = subprocess.Popen(["somethingyoudonthave", "-c",
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
175 "import sys; sys.exit(47)"],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
176 executable=sys.executable)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
177 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
178 self.assertEqual(p.returncode, 47)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
179
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
180 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
181 # stdin redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
182 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
183 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
184 stdin=subprocess.PIPE)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
185 p.stdin.write("pear")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
186 p.stdin.close()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
187 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
188 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
189
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
190 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
191 # stdin is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
192 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
193 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
194 os.write(d, "pear")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
195 os.lseek(d, 0, 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
196 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
197 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
198 stdin=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
199 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
200 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
201
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
202 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
203 # stdin is set to open file object
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
204 tf = tempfile.TemporaryFile()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
205 tf.write("pear")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
206 tf.seek(0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
207 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
208 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
209 stdin=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
210 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
211 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
212
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
213 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
214 # stdout redirection
33723
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.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
217 stdout=subprocess.PIPE)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
218 self.addCleanup(p.stdout.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
219 self.assertEqual(p.stdout.read(), "orange")
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_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
222 # stdout is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
223 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
224 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
225 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
226 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
227 stdout=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
228 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
229 os.lseek(d, 0, 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
230 self.assertEqual(os.read(d, 1024), "orange")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
231
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
232 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
233 # stdout is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
234 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
235 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
236 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
237 stdout=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
238 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
239 tf.seek(0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
240 self.assertEqual(tf.read(), "orange")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
241
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
242 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
243 # stderr redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
244 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
245 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
246 stderr=subprocess.PIPE)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
247 self.addCleanup(p.stderr.close)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
248 self.assertStderrEqual(p.stderr.read(), "strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
249
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
250 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
251 # stderr is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
252 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
253 d = tf.fileno()
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=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
257 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
258 os.lseek(d, 0, 0)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
259 self.assertStderrEqual(os.read(d, 1024), "strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
260
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
261 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
262 # stderr is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
263 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
264 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
265 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
266 stderr=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
267 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
268 tf.seek(0)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
269 self.assertStderrEqual(tf.read(), "strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
270
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
271 def test_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
272 # capture stdout and stderr to the same pipe
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
273 p = subprocess.Popen([sys.executable, "-c",
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
274 'import sys;'
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
275 'sys.stdout.write("apple");'
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
276 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
277 'sys.stderr.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
278 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
279 stderr=subprocess.STDOUT)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
280 self.addCleanup(p.stdout.close)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
281 self.assertStderrEqual(p.stdout.read(), "appleorange")
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_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
284 # 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
285 tf = tempfile.TemporaryFile()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
286 p = subprocess.Popen([sys.executable, "-c",
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
287 'import sys;'
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
288 'sys.stdout.write("apple");'
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
289 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
290 'sys.stderr.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
291 stdout=tf,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
292 stderr=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
293 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
294 tf.seek(0)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
295 self.assertStderrEqual(tf.read(), "appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
296
39791
5e502de0ceda Fixed bug #1531862: Do not close standard file descriptors in subprocess.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 39772
diff changeset
297 def test_stdout_filedes_of_stdout(self):
5e502de0ceda Fixed bug #1531862: Do not close standard file descriptors in subprocess.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 39772
diff changeset
298 # stdout is set to 1 (#1531862).
5e502de0ceda Fixed bug #1531862: Do not close standard file descriptors in subprocess.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 39772
diff changeset
299 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))"
5e502de0ceda Fixed bug #1531862: Do not close standard file descriptors in subprocess.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 39772
diff changeset
300 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
301 self.assertEqual(rc, 2)
39791
5e502de0ceda Fixed bug #1531862: Do not close standard file descriptors in subprocess.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 39772
diff changeset
302
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
303 def test_cwd(self):
44363
210c507dac99 Patch #1672 by Joseph Armbruster. Use tempdir() to get a temporary directory.
Guido van Rossum <guido@python.org>
parents: 41886
diff changeset
304 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
305 # 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
306 # 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
307 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
308 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
309 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
310 os.chdir(cwd)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
311 p = subprocess.Popen([sys.executable, "-c",
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
312 'import sys,os;'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
313 'sys.stdout.write(os.getcwd())'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
314 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
315 cwd=tmpdir)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
316 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
317 normcase = os.path.normcase
179e42ea2e9c normalize case when comparing directory names (problem reported by
Fredrik Lundh <fredrik@pythonware.com>
parents: 33739
diff changeset
318 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
319
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
320 def test_env(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
321 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
322 newenv["FRUIT"] = "orange"
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
323 p = subprocess.Popen([sys.executable, "-c",
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
324 'import sys,os;'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
325 'sys.stdout.write(os.getenv("FRUIT"))'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
326 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
327 env=newenv)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
328 self.addCleanup(p.stdout.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
329 self.assertEqual(p.stdout.read(), "orange")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
330
34671
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
331 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
332 p = subprocess.Popen([sys.executable, "-c",
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
333 'import sys;'
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
334 '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
335 stdin=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
336 p.communicate("pear")
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
337 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
338
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
339 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
340 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
341 '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
342 stdout=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
343 (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
344 self.assertEqual(stdout, "pineapple")
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
345 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
346
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
347 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
348 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
349 '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
350 stderr=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
351 (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
352 self.assertEqual(stdout, None)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
353 self.assertStderrEqual(stderr, "pineapple")
34671
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
354
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
355 def test_communicate(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
356 p = subprocess.Popen([sys.executable, "-c",
47338
273e8de34603 Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents: 47119
diff changeset
357 'import sys,os;'
273e8de34603 Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents: 47119
diff changeset
358 'sys.stderr.write("pineapple");'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
359 'sys.stdout.write(sys.stdin.read())'],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
360 stdin=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
361 stdout=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
362 stderr=subprocess.PIPE)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
363 self.addCleanup(p.stdout.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
364 self.addCleanup(p.stderr.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
365 self.addCleanup(p.stdin.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
366 (stdout, stderr) = p.communicate("banana")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
367 self.assertEqual(stdout, "banana")
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
368 self.assertStderrEqual(stderr, "pineapple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
369
47437
6404446e5aa3 Backport r62724 from trunk. Fixes issue 2791. subprocess.Popen.communicate
Gregory P. Smith <greg@mad-scientist.com>
parents: 40620
diff changeset
370 # This test is Linux specific for simplicity to at least have
6404446e5aa3 Backport r62724 from trunk. Fixes issue 2791. subprocess.Popen.communicate
Gregory P. Smith <greg@mad-scientist.com>
parents: 40620
diff changeset
371 # some coverage. It is not a platform specific bug.
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
372 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
373 "Linux specific")
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
374 # Test for the fd leak reported in http://bugs.python.org/issue2791.
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
375 def test_communicate_pipe_fd_leak(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
376 fd_directory = '/proc/%d/fd' % os.getpid()
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
377 num_fds_before_popen = len(os.listdir(fd_directory))
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
378 p = subprocess.Popen([sys.executable, "-c", "print()"],
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
379 stdout=subprocess.PIPE)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
380 p.communicate()
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
381 num_fds_after_communicate = len(os.listdir(fd_directory))
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
382 del p
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
383 num_fds_after_destruction = len(os.listdir(fd_directory))
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
384 self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
385 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
47437
6404446e5aa3 Backport r62724 from trunk. Fixes issue 2791. subprocess.Popen.communicate
Gregory P. Smith <greg@mad-scientist.com>
parents: 40620
diff changeset
386
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
387 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
388 # communicate() should return None if no redirection is active
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
389 p = subprocess.Popen([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
390 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
391 (stdout, stderr) = p.communicate()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
392 self.assertEqual(stdout, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
393 self.assertEqual(stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
394
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
395 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
396 # communicate() with writes larger than pipe_buf
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
397 # This test will probably deadlock rather than fail, if
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
398 # communicate() does not work properly.
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
399 x, y = os.pipe()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
400 if mswindows:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
401 pipe_buf = 512
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
402 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
403 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
404 os.close(x)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
405 os.close(y)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
406 p = subprocess.Popen([sys.executable, "-c",
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
407 'import sys,os;'
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
408 'sys.stdout.write(sys.stdin.read(47));'
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
409 'sys.stderr.write("xyz"*%d);'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
410 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
411 stdin=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
412 stdout=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
413 stderr=subprocess.PIPE)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
414 self.addCleanup(p.stdout.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
415 self.addCleanup(p.stderr.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
416 self.addCleanup(p.stdin.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
417 string_to_write = "abc"*pipe_buf
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
418 (stdout, stderr) = p.communicate(string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
419 self.assertEqual(stdout, string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
420
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
421 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
422 # stdin.write before communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
423 p = subprocess.Popen([sys.executable, "-c",
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
424 'import sys,os;'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
425 'sys.stdout.write(sys.stdin.read())'],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
426 stdin=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
427 stdout=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
428 stderr=subprocess.PIPE)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
429 self.addCleanup(p.stdout.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
430 self.addCleanup(p.stderr.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
431 self.addCleanup(p.stdin.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
432 p.stdin.write("banana")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
433 (stdout, stderr) = p.communicate("split")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
434 self.assertEqual(stdout, "bananasplit")
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
435 self.assertStderrEqual(stderr, "")
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
436
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
437 def test_universal_newlines(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
438 p = subprocess.Popen([sys.executable, "-c",
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
439 'import sys,os;' + SETBINARY +
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
440 'sys.stdout.write("line1\\n");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
441 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
442 'sys.stdout.write("line2\\r");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
443 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
444 'sys.stdout.write("line3\\r\\n");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
445 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
446 'sys.stdout.write("line4\\r");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
447 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
448 'sys.stdout.write("\\nline5");'
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
449 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
450 'sys.stdout.write("\\nline6");'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
451 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
452 universal_newlines=1)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
453 self.addCleanup(p.stdout.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
454 stdout = p.stdout.read()
38078
b1cbc605bec9 Try to fix breakage caused by patch #1479181, r45850
Neal Norwitz <nnorwitz@gmail.com>
parents: 38077
diff changeset
455 if hasattr(file, 'newlines'):
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
456 # Interpreter with universal newline support
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
457 self.assertEqual(stdout,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
458 "line1\nline2\nline3\nline4\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
459 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
460 # Interpreter without universal newline support
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
461 self.assertEqual(stdout,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
462 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
463
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
464 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
465 # universal newlines through communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
466 p = subprocess.Popen([sys.executable, "-c",
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
467 'import sys,os;' + SETBINARY +
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
468 'sys.stdout.write("line1\\n");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
469 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
470 'sys.stdout.write("line2\\r");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
471 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
472 'sys.stdout.write("line3\\r\\n");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
473 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
474 'sys.stdout.write("line4\\r");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
475 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
476 'sys.stdout.write("\\nline5");'
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
477 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
478 'sys.stdout.write("\\nline6");'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
479 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
480 universal_newlines=1)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
481 self.addCleanup(p.stdout.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
482 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
483 (stdout, stderr) = p.communicate()
38078
b1cbc605bec9 Try to fix breakage caused by patch #1479181, r45850
Neal Norwitz <nnorwitz@gmail.com>
parents: 38077
diff changeset
484 if hasattr(file, 'newlines'):
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
485 # Interpreter with universal newline support
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
486 self.assertEqual(stdout,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
487 "line1\nline2\nline3\nline4\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
488 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
489 # Interpreter without universal newline support
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
490 self.assertEqual(stdout,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
491 "line1\nline2\rline3\r\nline4\r\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
64815
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
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:
64815
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
498 max_handles = 2050 # too much for (at least some) Windows setups
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
499 handles = []
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
500 try:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
501 for i in range(max_handles):
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
502 try:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
503 handles.append(os.open(test_support.TESTFN,
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
504 os.O_WRONLY | os.O_CREAT))
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
505 except OSError as e:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
506 if e.errno != errno.EMFILE:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
507 raise
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
508 break
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
509 else:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
510 self.skipTest("failed to reach the file descriptor limit "
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
511 "(tried %d)" % max_handles)
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
512 # Close a couple of them (should be enough for a subprocess)
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
513 for i in range(10):
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
514 os.close(handles.pop())
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
515 # Loop creating some subprocesses. If one of them leaks some fds,
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
516 # the next loop iteration will fail by reaching the max fd limit.
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
517 for i in range(15):
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
518 p = subprocess.Popen([sys.executable, "-c",
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
519 "import sys;"
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
520 "sys.stdout.write(sys.stdin.read())"],
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
521 stdin=subprocess.PIPE,
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
522 stdout=subprocess.PIPE,
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
523 stderr=subprocess.PIPE)
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
524 data = p.communicate(b"lime")[0]
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
525 self.assertEqual(data, b"lime")
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
526 finally:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
527 for h in handles:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
528 os.close(h)
79569
715b3cbcb438 Clean up a test turd.
Mark Dickinson <mdickinson@enthought.com>
parents: 75526
diff changeset
529 test_support.unlink(test_support.TESTFN)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
530
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
531 def test_list2cmdline(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
532 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
533 '"a b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
534 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
535 'ab\\"c \\ d')
44790
210323e2a927 fix comment typos, use not arg instead of arg == "", add test coverage
Gregory P. Smith <greg@mad-scientist.com>
parents: 44363
diff changeset
536 self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
210323e2a927 fix comment typos, use not arg instead of arg == "", add test coverage
Gregory P. Smith <greg@mad-scientist.com>
parents: 44363
diff changeset
537 'ab\\"c " \\\\" d')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
538 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
539 'a\\\\\\b "de fg" h')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
540 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
541 'a\\\\\\"b c d')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
542 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
543 '"a\\\\b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
544 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
545 '"a\\\\b\\ c" d e')
40620
1409e1f273b5 Fix for bug #1634343: allow specifying empty arguments on Windows
Peter Astrand <astrand@lysator.liu.se>
parents: 39792
diff changeset
546 self.assertEqual(subprocess.list2cmdline(['ab', '']),
1409e1f273b5 Fix for bug #1634343: allow specifying empty arguments on Windows
Peter Astrand <astrand@lysator.liu.se>
parents: 39792
diff changeset
547 'ab ""')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
548
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
549
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
550 def test_poll(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
551 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
552 "-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
553 count = 0
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
554 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
555 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
556 count += 1
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
557 # 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
558 # 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
559 # 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
560 # didn't go around at least twice.
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
561 self.assertGreaterEqual(count, 2)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
562 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
563 self.assertEqual(p.poll(), 0)
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
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
566 def test_wait(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
567 p = subprocess.Popen([sys.executable,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
568 "-c", "import time; time.sleep(2)"])
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
569 self.assertEqual(p.wait(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
570 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
571 self.assertEqual(p.wait(), 0)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
572
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
573
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 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
575 # 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
576 # TypeError.
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
577 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
578 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
579
51637
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
580 def test_leaking_fds_on_error(self):
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
581 # see bug #5179: Popen leaks file descriptors to PIPEs if
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
582 # the child fails to execute; this will eventually exhaust
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
583 # the maximum number of open fds. 1024 seems a very common
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
584 # value for that limit, but Windows has 2048, so we loop
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
585 # 1024 times (each call leaked two fds).
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
586 for i in range(1024):
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
587 # Windows raises IOError. Others raise OSError.
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
588 with self.assertRaises(EnvironmentError) as c:
51637
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
589 subprocess.Popen(['nonexisting_i_hope'],
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
590 stdout=subprocess.PIPE,
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
591 stderr=subprocess.PIPE)
68435
0a19b3278fa2 #11490: EACCES can also mean command not found
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
592 # ignore errors that indicate the command was not found
0a19b3278fa2 #11490: EACCES can also mean command not found
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
593 if c.exception.errno not in (errno.ENOENT, errno.EACCES):
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
594 raise c.exception
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
595
63773
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
596 def test_handles_closed_on_exception(self):
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
597 # If CreateProcess exits with an error, ensure the
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
598 # duplicate output handles are released
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
599 ifhandle, ifname = mkstemp()
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
600 ofhandle, ofname = mkstemp()
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
601 efhandle, efname = mkstemp()
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
602 try:
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
603 subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
604 stderr=efhandle)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
605 except OSError:
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
606 os.close(ifhandle)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
607 os.remove(ifname)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
608 os.close(ofhandle)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
609 os.remove(ofname)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
610 os.close(efhandle)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
611 os.remove(efname)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
612 self.assertFalse(os.path.exists(ifname))
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
613 self.assertFalse(os.path.exists(ofname))
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
614 self.assertFalse(os.path.exists(efname))
51637
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
615
69153
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
616 def test_communicate_epipe(self):
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
617 # Issue 10963: communicate() should hide EPIPE
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
618 p = subprocess.Popen([sys.executable, "-c", 'pass'],
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
619 stdin=subprocess.PIPE,
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
620 stdout=subprocess.PIPE,
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
621 stderr=subprocess.PIPE)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
622 self.addCleanup(p.stdout.close)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
623 self.addCleanup(p.stderr.close)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
624 self.addCleanup(p.stdin.close)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
625 p.communicate("x" * 2**20)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
626
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
627 def test_communicate_epipe_only_stdin(self):
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
628 # Issue 10963: communicate() should hide EPIPE
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
629 p = subprocess.Popen([sys.executable, "-c", 'pass'],
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
630 stdin=subprocess.PIPE)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
631 self.addCleanup(p.stdin.close)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
632 time.sleep(2)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
633 p.communicate("x" * 2**20)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
634
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
635 # context manager
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
636 class _SuppressCoreFiles(object):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
637 """Try to prevent core files from being created."""
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
638 old_limit = None
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
639
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
640 def __enter__(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
641 """Try to save previous ulimit, then set it to (0, 0)."""
73922
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
642 if resource is not None:
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
643 try:
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
644 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
645 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
646 except (ValueError, resource.error):
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
647 pass
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
648
63152
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
649 if sys.platform == 'darwin':
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
650 # Check if the 'Crash Reporter' on OSX was configured
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
651 # in 'Developer' mode and warn that it will get triggered
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
652 # when it is.
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
653 #
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
654 # This assumes that this context manager is used in tests
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
655 # that might trigger the next manager.
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
656 value = subprocess.Popen(['/usr/bin/defaults', 'read',
63156
bdc069a1a721 Merged revisions 83079 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63097
diff changeset
657 'com.apple.CrashReporter', 'DialogType'],
bdc069a1a721 Merged revisions 83079 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63097
diff changeset
658 stdout=subprocess.PIPE).communicate()[0]
63152
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
659 if value.strip() == b'developer':
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
660 print "this tests triggers the Crash Reporter, that is intentional"
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
661 sys.stdout.flush()
63156
bdc069a1a721 Merged revisions 83079 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63097
diff changeset
662
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
663 def __exit__(self, *args):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
664 """Return core file behavior to default."""
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
665 if self.old_limit is None:
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
666 return
73922
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
667 if resource is not None:
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
668 try:
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
669 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
670 except (ValueError, resource.error):
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
671 pass
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
672
71235
8a4c9c154b5d Issue #12493: skip test_communicate_eintr() if signal.SIGALRM is missing
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71230
diff changeset
673 @unittest.skipUnless(hasattr(signal, 'SIGALRM'),
8a4c9c154b5d Issue #12493: skip test_communicate_eintr() if signal.SIGALRM is missing
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71230
diff changeset
674 "Requires signal.SIGALRM")
71230
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
675 def test_communicate_eintr(self):
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
676 # Issue #12493: communicate() should handle EINTR
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
677 def handler(signum, frame):
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
678 pass
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
679 old_handler = signal.signal(signal.SIGALRM, handler)
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
680 self.addCleanup(signal.signal, signal.SIGALRM, old_handler)
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
681
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
682 # the process is running for 2 seconds
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
683 args = [sys.executable, "-c", 'import time; time.sleep(2)']
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
684 for stream in ('stdout', 'stderr'):
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
685 kw = {stream: subprocess.PIPE}
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
686 with subprocess.Popen(args, **kw) as process:
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
687 signal.alarm(1)
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
688 # communicate() will be interrupted by SIGALRM
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
689 process.communicate()
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
690
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
691
59338
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
692 @unittest.skipIf(mswindows, "POSIX specific tests")
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
693 class POSIXProcessTestCase(BaseTestCase):
39341
53d0552a7a8c [Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents: 39084
diff changeset
694
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
695 def test_exceptions(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
696 # caught & re-raised exceptions
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
697 with self.assertRaises(OSError) as c:
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
698 p = subprocess.Popen([sys.executable, "-c", ""],
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
699 cwd="/this/path/does/not/exist")
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
700 # The attribute child_traceback should contain "os.chdir" somewhere.
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
701 self.assertIn("os.chdir", c.exception.child_traceback)
39346
bd50a42b7df0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39341
diff changeset
702
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
703 def test_run_abort(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
704 # returncode handles signal termination
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
705 with _SuppressCoreFiles():
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
706 p = subprocess.Popen([sys.executable, "-c",
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
707 "import os; os.abort()"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
708 p.wait()
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
709 self.assertEqual(-p.returncode, signal.SIGABRT)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
710
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
711 def test_preexec(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
712 # preexec function
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
713 p = subprocess.Popen([sys.executable, "-c",
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
714 "import sys, os;"
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
715 "sys.stdout.write(os.getenv('FRUIT'))"],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
716 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
717 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
718 self.addCleanup(p.stdout.close)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
719 self.assertEqual(p.stdout.read(), "apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
720
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
721 def test_args_string(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
722 # args is a string
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
723 f, fname = mkstemp()
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
724 os.write(f, "#!/bin/sh\n")
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
725 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
726 sys.executable)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
727 os.close(f)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
728 os.chmod(fname, 0o700)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
729 p = subprocess.Popen(fname)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
730 p.wait()
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
731 os.remove(fname)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
732 self.assertEqual(p.returncode, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
733
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
734 def test_invalid_args(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
735 # invalid arguments should raise ValueError
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
736 self.assertRaises(ValueError, subprocess.call,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
737 [sys.executable, "-c",
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
738 "import sys; sys.exit(47)"],
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
739 startupinfo=47)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
740 self.assertRaises(ValueError, subprocess.call,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
741 [sys.executable, "-c",
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
742 "import sys; sys.exit(47)"],
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
743 creationflags=47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
744
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
745 def test_shell_sequence(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
746 # Run command through the shell (sequence)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
747 newenv = os.environ.copy()
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
748 newenv["FRUIT"] = "apple"
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
749 p = subprocess.Popen(["echo $FRUIT"], shell=1,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
750 stdout=subprocess.PIPE,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
751 env=newenv)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
752 self.addCleanup(p.stdout.close)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
753 self.assertEqual(p.stdout.read().strip(), "apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
754
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
755 def test_shell_string(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
756 # Run command through the shell (string)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
757 newenv = os.environ.copy()
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
758 newenv["FRUIT"] = "apple"
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
759 p = subprocess.Popen("echo $FRUIT", shell=1,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
760 stdout=subprocess.PIPE,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
761 env=newenv)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
762 self.addCleanup(p.stdout.close)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
763 self.assertEqual(p.stdout.read().strip(), "apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
764
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
765 def test_call_string(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
766 # call() function with string argument on UNIX
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
767 f, fname = mkstemp()
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
768 os.write(f, "#!/bin/sh\n")
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
769 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
770 sys.executable)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
771 os.close(f)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
772 os.chmod(fname, 0700)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
773 rc = subprocess.call(fname)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
774 os.remove(fname)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
775 self.assertEqual(rc, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
776
63096
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
777 def test_specific_shell(self):
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
778 # Issue #9265: Incorrect name passed as arg[0].
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
779 shells = []
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
780 for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']:
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
781 for name in ['bash', 'ksh']:
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
782 sh = os.path.join(prefix, name)
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
783 if os.path.isfile(sh):
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
784 shells.append(sh)
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
785 if not shells: # Will probably work for any shell but csh.
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
786 self.skipTest("bash or ksh required for this test")
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
787 sh = '/bin/sh'
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
788 if os.path.isfile(sh) and not os.path.islink(sh):
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
789 # Test will fail if /bin/sh is a symlink to csh.
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
790 shells.append(sh)
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
791 for sh in shells:
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
792 p = subprocess.Popen("echo $0", executable=sh, shell=True,
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
793 stdout=subprocess.PIPE)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
794 self.addCleanup(p.stdout.close)
63096
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
795 self.assertEqual(p.stdout.read().strip(), sh)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
796
59435
71e4ab21346d Do not fail if returncode is 0 on send_signal/kill/terminate, for win32 platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59433
diff changeset
797 def _kill_process(self, method, *args):
59375
3197ecc0944a #2777: Handle fds more carefully to try to fix some x86-Linux failures (namely, neal bot and twisted bot).
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59352
diff changeset
798 # Do not inherit file handles from the parent.
3197ecc0944a #2777: Handle fds more carefully to try to fix some x86-Linux failures (namely, neal bot and twisted bot).
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59352
diff changeset
799 # It should fix failures on some platforms.
64854
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
800 p = subprocess.Popen([sys.executable, "-c", """if 1:
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
801 import sys, time
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
802 sys.stdout.write('x\\n')
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
803 sys.stdout.flush()
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
804 time.sleep(30)
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
805 """],
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
806 close_fds=True,
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
807 stdin=subprocess.PIPE,
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
808 stdout=subprocess.PIPE,
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
809 stderr=subprocess.PIPE)
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
810 # Wait for the interpreter to be completely initialized before
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
811 # sending any signal.
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
812 p.stdout.read(1)
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
813 getattr(p, method)(*args)
59435
71e4ab21346d Do not fail if returncode is 0 on send_signal/kill/terminate, for win32 platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59433
diff changeset
814 return p
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
815
75526
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
816 def _kill_dead_process(self, method, *args):
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
817 # Do not inherit file handles from the parent.
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
818 # It should fix failures on some platforms.
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
819 p = subprocess.Popen([sys.executable, "-c", """if 1:
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
820 import sys, time
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
821 sys.stdout.write('x\\n')
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
822 sys.stdout.flush()
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
823 """],
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
824 close_fds=True,
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
825 stdin=subprocess.PIPE,
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
826 stdout=subprocess.PIPE,
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
827 stderr=subprocess.PIPE)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
828 # Wait for the interpreter to be completely initialized before
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
829 # sending any signal.
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
830 p.stdout.read(1)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
831 # The process should end after this
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
832 time.sleep(1)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
833 # This shouldn't raise even though the child is now dead
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
834 getattr(p, method)(*args)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
835 p.communicate()
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
836
59435
71e4ab21346d Do not fail if returncode is 0 on send_signal/kill/terminate, for win32 platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59433
diff changeset
837 def test_send_signal(self):
71e4ab21346d Do not fail if returncode is 0 on send_signal/kill/terminate, for win32 platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59433
diff changeset
838 p = self._kill_process('send_signal', signal.SIGINT)
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
839 _, stderr = p.communicate()
59961
37da9bc46174 The SIGINT signal may happen earlier, during site.py initialization.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59957
diff changeset
840 self.assertIn('KeyboardInterrupt', stderr)
59957
289006dc46ee The standard error should be empty when the signal is killed, except on SIGINT.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59955
diff changeset
841 self.assertNotEqual(p.wait(), 0)
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
842
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
843 def test_kill(self):
59435
71e4ab21346d Do not fail if returncode is 0 on send_signal/kill/terminate, for win32 platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59433
diff changeset
844 p = self._kill_process('kill')
59957
289006dc46ee The standard error should be empty when the signal is killed, except on SIGINT.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59955
diff changeset
845 _, stderr = p.communicate()
289006dc46ee The standard error should be empty when the signal is killed, except on SIGINT.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59955
diff changeset
846 self.assertStderrEqual(stderr, '')
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
847 self.assertEqual(p.wait(), -signal.SIGKILL)
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
848
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
849 def test_terminate(self):
59435
71e4ab21346d Do not fail if returncode is 0 on send_signal/kill/terminate, for win32 platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59433
diff changeset
850 p = self._kill_process('terminate')
59957
289006dc46ee The standard error should be empty when the signal is killed, except on SIGINT.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59955
diff changeset
851 _, stderr = p.communicate()
289006dc46ee The standard error should be empty when the signal is killed, except on SIGINT.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59955
diff changeset
852 self.assertStderrEqual(stderr, '')
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
853 self.assertEqual(p.wait(), -signal.SIGTERM)
63097
3fdd9b43024c Merged revisions 82973 via svnmerge from
Stefan Krah <stefan@bytereef.org>
parents: 62403
diff changeset
854
75526
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
855 def test_send_signal_dead(self):
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
856 # Sending a signal to a dead process
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
857 self._kill_dead_process('send_signal', signal.SIGINT)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
858
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
859 def test_kill_dead(self):
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
860 # Killing a dead process
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
861 self._kill_dead_process('kill')
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
862
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
863 def test_terminate_dead(self):
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
864 # Terminating a dead process
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
865 self._kill_dead_process('terminate')
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
866
67312
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
867 def check_close_std_fds(self, fds):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
868 # Issue #9905: test that subprocess pipes still work properly with
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
869 # some standard fds closed
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
870 stdin = 0
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
871 newfds = []
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
872 for a in fds:
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
873 b = os.dup(a)
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
874 newfds.append(b)
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
875 if a == 0:
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
876 stdin = b
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
877 try:
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
878 for fd in fds:
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
879 os.close(fd)
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
880 out, err = subprocess.Popen([sys.executable, "-c",
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
881 'import sys;'
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
882 'sys.stdout.write("apple");'
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
883 'sys.stdout.flush();'
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
884 'sys.stderr.write("orange")'],
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
885 stdin=stdin,
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
886 stdout=subprocess.PIPE,
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
887 stderr=subprocess.PIPE).communicate()
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
888 err = test_support.strip_python_stderr(err)
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
889 self.assertEqual((out, err), (b'apple', b'orange'))
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
890 finally:
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
891 for b, a in zip(newfds, fds):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
892 os.dup2(b, a)
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
893 for b in newfds:
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
894 os.close(b)
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
895
67312
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
896 def test_close_fd_0(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
897 self.check_close_std_fds([0])
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
898
67312
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
899 def test_close_fd_1(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
900 self.check_close_std_fds([1])
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
901
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
902 def test_close_fd_2(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
903 self.check_close_std_fds([2])
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
904
67312
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
905 def test_close_fds_0_1(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
906 self.check_close_std_fds([0, 1])
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
907
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
908 def test_close_fds_0_2(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
909 self.check_close_std_fds([0, 2])
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
910
67312
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
911 def test_close_fds_1_2(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
912 self.check_close_std_fds([1, 2])
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
913
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
914 def test_close_fds_0_1_2(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
915 # Issue #10806: test that subprocess pipes still work properly with
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
916 # all standard fds closed.
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
917 self.check_close_std_fds([0, 1, 2])
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
918
71530
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
919 def check_swap_fds(self, stdin_no, stdout_no, stderr_no):
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
920 # open up some temporary files
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
921 temps = [mkstemp() for i in range(3)]
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
922 temp_fds = [fd for fd, fname in temps]
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
923 try:
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
924 # unlink the files -- we won't need to reopen them
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
925 for fd, fname in temps:
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
926 os.unlink(fname)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
927
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
928 # save a copy of the standard file descriptors
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
929 saved_fds = [os.dup(fd) for fd in range(3)]
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
930 try:
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
931 # duplicate the temp files over the standard fd's 0, 1, 2
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
932 for fd, temp_fd in enumerate(temp_fds):
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
933 os.dup2(temp_fd, fd)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
934
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
935 # write some data to what will become stdin, and rewind
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
936 os.write(stdin_no, b"STDIN")
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
937 os.lseek(stdin_no, 0, 0)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
938
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
939 # now use those files in the given order, so that subprocess
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
940 # has to rearrange them in the child
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
941 p = subprocess.Popen([sys.executable, "-c",
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
942 'import sys; got = sys.stdin.read();'
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
943 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'],
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
944 stdin=stdin_no,
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
945 stdout=stdout_no,
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
946 stderr=stderr_no)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
947 p.wait()
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
948
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
949 for fd in temp_fds:
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
950 os.lseek(fd, 0, 0)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
951
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
952 out = os.read(stdout_no, 1024)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
953 err = test_support.strip_python_stderr(os.read(stderr_no, 1024))
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
954 finally:
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
955 for std, saved in enumerate(saved_fds):
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
956 os.dup2(saved, std)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
957 os.close(saved)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
958
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
959 self.assertEqual(out, b"got STDIN")
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
960 self.assertEqual(err, b"err")
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
961
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
962 finally:
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
963 for fd in temp_fds:
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
964 os.close(fd)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
965
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
966 # When duping fds, if there arises a situation where one of the fds is
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
967 # either 0, 1 or 2, it is possible that it is overwritten (#12607).
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
968 # This tests all combinations of this.
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
969 def test_swap_fds(self):
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
970 self.check_swap_fds(0, 1, 2)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
971 self.check_swap_fds(0, 2, 1)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
972 self.check_swap_fds(1, 0, 2)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
973 self.check_swap_fds(1, 2, 0)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
974 self.check_swap_fds(2, 0, 1)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
975 self.check_swap_fds(2, 1, 0)
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
976
66894
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
977 def test_wait_when_sigchild_ignored(self):
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
978 # NOTE: sigchild_ignore.py may not be an effective test on all OSes.
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
979 sigchild_ignore = test_support.findfile("sigchild_ignore.py",
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
980 subdir="subprocessdata")
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
981 p = subprocess.Popen([sys.executable, sigchild_ignore],
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
982 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
983 stdout, stderr = p.communicate()
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
984 self.assertEqual(0, p.returncode, "sigchild_ignore.py exited"
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
985 " non-zero with this error:\n%s" % stderr)
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
986
71902
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
987 def test_zombie_fast_process_del(self):
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
988 # Issue #12650: on Unix, if Popen.__del__() was called before the
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
989 # process exited, it wouldn't be added to subprocess._active, and would
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
990 # remain a zombie.
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
991 # spawn a Popen, and delete its reference before it exits
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
992 p = subprocess.Popen([sys.executable, "-c",
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
993 'import sys, time;'
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
994 'time.sleep(0.2)'],
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
995 stdout=subprocess.PIPE,
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
996 stderr=subprocess.PIPE)
71915
e7fe0c05d722 Fix resource leaks in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71912
diff changeset
997 self.addCleanup(p.stdout.close)
e7fe0c05d722 Fix resource leaks in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71912
diff changeset
998 self.addCleanup(p.stderr.close)
71902
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
999 ident = id(p)
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1000 pid = p.pid
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1001 del p
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1002 # check that p is in the active processes list
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1003 self.assertIn(ident, [id(o) for o in subprocess._active])
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1004
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1005 def test_leak_fast_process_del_killed(self):
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1006 # Issue #12650: on Unix, if Popen.__del__() was called before the
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1007 # process exited, and the process got killed by a signal, it would never
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1008 # be removed from subprocess._active, which triggered a FD and memory
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1009 # leak.
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1010 # spawn a Popen, delete its reference and kill it
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1011 p = subprocess.Popen([sys.executable, "-c",
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1012 'import time;'
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1013 'time.sleep(3)'],
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1014 stdout=subprocess.PIPE,
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1015 stderr=subprocess.PIPE)
71915
e7fe0c05d722 Fix resource leaks in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71912
diff changeset
1016 self.addCleanup(p.stdout.close)
e7fe0c05d722 Fix resource leaks in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71912
diff changeset
1017 self.addCleanup(p.stderr.close)
71902
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1018 ident = id(p)
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1019 pid = p.pid
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1020 del p
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1021 os.kill(pid, signal.SIGKILL)
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1022 # check that p is in the active processes list
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1023 self.assertIn(ident, [id(o) for o in subprocess._active])
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1024
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1025 # let some time for the process to exit, and create a new Popen: this
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1026 # should trigger the wait() of p
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1027 time.sleep(0.2)
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1028 with self.assertRaises(EnvironmentError) as c:
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1029 with subprocess.Popen(['nonexisting_i_hope'],
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1030 stdout=subprocess.PIPE,
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1031 stderr=subprocess.PIPE) as proc:
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1032 pass
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1033 # p should have been wait()ed on, and removed from the _active list
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1034 self.assertRaises(OSError, os.waitpid, pid, 0)
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1035 self.assertNotIn(ident, [id(o) for o in subprocess._active])
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1036
72076
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1037 def test_pipe_cloexec(self):
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1038 # Issue 12786: check that the communication pipes' FDs are set CLOEXEC,
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1039 # and are not inherited by another child process.
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1040 p1 = subprocess.Popen([sys.executable, "-c",
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1041 'import os;'
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1042 'os.read(0, 1)'
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1043 ],
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1044 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1045 stderr=subprocess.PIPE)
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1046
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1047 p2 = subprocess.Popen([sys.executable, "-c", """if True:
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1048 import os, errno, sys
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1049 for fd in %r:
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1050 try:
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1051 os.close(fd)
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1052 except OSError as e:
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1053 if e.errno != errno.EBADF:
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1054 raise
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1055 else:
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1056 sys.exit(1)
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1057 sys.exit(0)
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1058 """ % [f.fileno() for f in (p1.stdin, p1.stdout,
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1059 p1.stderr)]
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1060 ],
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1061 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1062 stderr=subprocess.PIPE, close_fds=False)
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1063 p1.communicate('foo')
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1064 _, stderr = p2.communicate()
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1065
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1066 self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr))
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1067
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
1068
59338
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
1069 @unittest.skipUnless(mswindows, "Windows specific tests")
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
1070 class Win32ProcessTestCase(BaseTestCase):
59339
d8b2fa88788b #2777: Enable test_send_signal, test_kill and test_terminate on all platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59338
diff changeset
1071
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1072 def test_startupinfo(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1073 # startupinfo argument
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1074 # We uses hardcoded constants, because we do not want to
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1075 # depend on win32all.
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1076 STARTF_USESHOWWINDOW = 1
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1077 SW_MAXIMIZE = 3
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1078 startupinfo = subprocess.STARTUPINFO()
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1079 startupinfo.dwFlags = STARTF_USESHOWWINDOW
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1080 startupinfo.wShowWindow = SW_MAXIMIZE
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1081 # Since Python is a console process, it won't be affected
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1082 # by wShowWindow, but the argument should be silently
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1083 # ignored
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1084 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
1085 startupinfo=startupinfo)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1086
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1087 def test_creationflags(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1088 # creationflags argument
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1089 CREATE_NEW_CONSOLE = 16
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1090 sys.stderr.write(" a DOS box should flash briefly ...\n")
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1091 subprocess.call(sys.executable +
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1092 ' -c "import time; time.sleep(0.25)"',
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1093 creationflags=CREATE_NEW_CONSOLE)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1094
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1095 def test_invalid_args(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1096 # invalid arguments should raise ValueError
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1097 self.assertRaises(ValueError, subprocess.call,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1098 [sys.executable, "-c",
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1099 "import sys; sys.exit(47)"],
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1100 preexec_fn=lambda: 1)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1101 self.assertRaises(ValueError, subprocess.call,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1102 [sys.executable, "-c",
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1103 "import sys; sys.exit(47)"],
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1104 stdout=subprocess.PIPE,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1105 close_fds=True)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1106
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1107 def test_close_fds(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1108 # close file descriptors
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1109 rc = subprocess.call([sys.executable, "-c",
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1110 "import sys; sys.exit(47)"],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1111 close_fds=True)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1112 self.assertEqual(rc, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1113
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1114 def test_shell_sequence(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1115 # Run command through the shell (sequence)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1116 newenv = os.environ.copy()
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1117 newenv["FRUIT"] = "physalis"
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1118 p = subprocess.Popen(["set"], shell=1,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1119 stdout=subprocess.PIPE,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1120 env=newenv)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
1121 self.addCleanup(p.stdout.close)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1122 self.assertIn("physalis", p.stdout.read())
41886
ec3b8e94a355 Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents: 40619
diff changeset
1123
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1124 def test_shell_string(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1125 # Run command through the shell (string)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1126 newenv = os.environ.copy()
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1127 newenv["FRUIT"] = "physalis"
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1128 p = subprocess.Popen("set", shell=1,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1129 stdout=subprocess.PIPE,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1130 env=newenv)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
1131 self.addCleanup(p.stdout.close)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1132 self.assertIn("physalis", p.stdout.read())
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1133
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1134 def test_call_string(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1135 # call() function with string argument on Windows
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1136 rc = subprocess.call(sys.executable +
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1137 ' -c "import sys; sys.exit(47)"')
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1138 self.assertEqual(rc, 47)
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
1139
59435
71e4ab21346d Do not fail if returncode is 0 on send_signal/kill/terminate, for win32 platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59433
diff changeset
1140 def _kill_process(self, method, *args):
59441
ab7c87ed5cad #2777: Try hard to make Win7 buildbot happy...
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59435
diff changeset
1141 # Some win32 buildbot raises EOFError if stdin is inherited
64928
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1142 p = subprocess.Popen([sys.executable, "-c", """if 1:
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1143 import sys, time
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1144 sys.stdout.write('x\\n')
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1145 sys.stdout.flush()
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1146 time.sleep(30)
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1147 """],
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1148 stdin=subprocess.PIPE,
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1149 stdout=subprocess.PIPE,
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1150 stderr=subprocess.PIPE)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
1151 self.addCleanup(p.stdout.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
1152 self.addCleanup(p.stderr.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
1153 self.addCleanup(p.stdin.close)
64928
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1154 # Wait for the interpreter to be completely initialized before
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1155 # sending any signal.
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1156 p.stdout.read(1)
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1157 getattr(p, method)(*args)
59957
289006dc46ee The standard error should be empty when the signal is killed, except on SIGINT.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59955
diff changeset
1158 _, stderr = p.communicate()
289006dc46ee The standard error should be empty when the signal is killed, except on SIGINT.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59955
diff changeset
1159 self.assertStderrEqual(stderr, '')
64928
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1160 returncode = p.wait()
59463
a3f6178354f5 Replace the stderr logging with assertNotEqual(returncode, 0).
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59441
diff changeset
1161 self.assertNotEqual(returncode, 0)
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
1162
75526
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1163 def _kill_dead_process(self, method, *args):
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1164 p = subprocess.Popen([sys.executable, "-c", """if 1:
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1165 import sys, time
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1166 sys.stdout.write('x\\n')
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1167 sys.stdout.flush()
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1168 sys.exit(42)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1169 """],
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1170 stdin=subprocess.PIPE,
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1171 stdout=subprocess.PIPE,
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1172 stderr=subprocess.PIPE)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1173 self.addCleanup(p.stdout.close)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1174 self.addCleanup(p.stderr.close)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1175 self.addCleanup(p.stdin.close)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1176 # Wait for the interpreter to be completely initialized before
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1177 # sending any signal.
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1178 p.stdout.read(1)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1179 # The process should end after this
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1180 time.sleep(1)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1181 # This shouldn't raise even though the child is now dead
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1182 getattr(p, method)(*args)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1183 _, stderr = p.communicate()
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1184 self.assertStderrEqual(stderr, b'')
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1185 rc = p.wait()
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1186 self.assertEqual(rc, 42)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1187
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1188 def test_send_signal(self):
59435
71e4ab21346d Do not fail if returncode is 0 on send_signal/kill/terminate, for win32 platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59433
diff changeset
1189 self._kill_process('send_signal', signal.SIGTERM)
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
1190
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1191 def test_kill(self):
59435
71e4ab21346d Do not fail if returncode is 0 on send_signal/kill/terminate, for win32 platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59433
diff changeset
1192 self._kill_process('kill')
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
1193
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1194 def test_terminate(self):
59435
71e4ab21346d Do not fail if returncode is 0 on send_signal/kill/terminate, for win32 platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59433
diff changeset
1195 self._kill_process('terminate')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1196
75526
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1197 def test_send_signal_dead(self):
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1198 self._kill_dead_process('send_signal', signal.SIGTERM)
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1199
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1200 def test_kill_dead(self):
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1201 self._kill_dead_process('kill')
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1202
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1203 def test_terminate_dead(self):
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1204 self._kill_dead_process('terminate')
b6ec3b717f7e Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73922
diff changeset
1205
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
1206
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1207 @unittest.skipUnless(getattr(subprocess, '_has_poll', False),
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1208 "poll system call not supported")
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1209 class ProcessTestCaseNoPoll(ProcessTestCase):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1210 def setUp(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1211 subprocess._has_poll = False
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1212 ProcessTestCase.setUp(self)
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
1213
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1214 def tearDown(self):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1215 subprocess._has_poll = True
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1216 ProcessTestCase.tearDown(self)
55092
9808e38fde72 Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1217
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1218
59208
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1219 class HelperFunctionTests(unittest.TestCase):
59215
854b34a06896 Fix the new test on windows (skip it, its posix only)
Gregory P. Smith <greg@mad-scientist.com>
parents: 59206
diff changeset
1220 @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
59206
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
1221 def test_eintr_retry_call(self):
59208
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1222 record_calls = []
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1223 def fake_os_func(*args):
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1224 record_calls.append(args)
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1225 if len(record_calls) == 2:
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1226 raise OSError(errno.EINTR, "fake interrupted system call")
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1227 return tuple(reversed(args))
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1228
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1229 self.assertEqual((999, 256),
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1230 subprocess._eintr_retry_call(fake_os_func, 256, 999))
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1231 self.assertEqual([(256, 999)], record_calls)
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1232 # This time there will be an EINTR so it will loop once.
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1233 self.assertEqual((666,),
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1234 subprocess._eintr_retry_call(fake_os_func, 666))
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1235 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1236
63931
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1237 @unittest.skipUnless(mswindows, "mswindows only")
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1238 class CommandsWithSpaces (BaseTestCase):
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1239
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1240 def setUp(self):
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1241 super(CommandsWithSpaces, self).setUp()
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1242 f, fname = mkstemp(".py", "te st")
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1243 self.fname = fname.lower ()
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1244 os.write(f, b"import sys;"
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1245 b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1246 )
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1247 os.close(f)
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1248
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1249 def tearDown(self):
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1250 os.remove(self.fname)
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1251 super(CommandsWithSpaces, self).tearDown()
59218
f53fe33e03ea Merged revisions 78532 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59208
diff changeset
1252
63931
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1253 def with_spaces(self, *args, **kwargs):
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1254 kwargs['stdout'] = subprocess.PIPE
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1255 p = subprocess.Popen(*args, **kwargs)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
1256 self.addCleanup(p.stdout.close)
63931
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1257 self.assertEqual(
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1258 p.stdout.read ().decode("mbcs"),
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1259 "2 [%r, 'ab cd']" % self.fname
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1260 )
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1261
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1262 def test_shell_string_with_spaces(self):
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1263 # call() function with string argument with spaces on Windows
63958
068291d44fd7 Merged revisions 83983 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 63931
diff changeset
1264 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
068291d44fd7 Merged revisions 83983 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 63931
diff changeset
1265 "ab cd"), shell=1)
63931
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1266
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1267 def test_shell_sequence_with_spaces(self):
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1268 # call() function with sequence argument with spaces on Windows
63958
068291d44fd7 Merged revisions 83983 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 63931
diff changeset
1269 self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1)
63931
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1270
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1271 def test_noshell_string_with_spaces(self):
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1272 # call() function with string argument with spaces on Windows
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1273 self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1274 "ab cd"))
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1275
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1276 def test_noshell_sequence_with_spaces(self):
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1277 # call() function with sequence argument with spaces on Windows
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1278 self.with_spaces([sys.executable, self.fname, "ab cd"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1279
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1280 def test_main():
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1281 unit_tests = (ProcessTestCase,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1282 POSIXProcessTestCase,
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1283 Win32ProcessTestCase,
59206
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
1284 ProcessTestCaseNoPoll,
63931
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1285 HelperFunctionTests,
ab2d94931b0c #2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows
Tim Golden <mail@timgolden.me.uk>
parents: 63842
diff changeset
1286 CommandsWithSpaces)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1287
55092
9808e38fde72 Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1288 test_support.run_unittest(*unit_tests)
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
1289 test_support.reap_children()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1290
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1291 if __name__ == "__main__":
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1292 test_main()