annotate Lib/test/test_subprocess.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 677cf4a55a9c
children 61d6b34af419
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
80371
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
61 class PopenTestException(Exception):
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
62 pass
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
63
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
64
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
65 class PopenExecuteChildRaises(subprocess.Popen):
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
66 """Popen subclass for testing cleanup of subprocess.PIPE filehandles when
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
67 _execute_child fails.
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
68 """
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
69 def _execute_child(self, *args, **kwargs):
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
70 raise PopenTestException("Forced Exception for Test")
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
71
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
72
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
73 class ProcessTestCase(BaseTestCase):
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
74
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
75 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
76 # call() function with sequence argument
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
77 rc = subprocess.call([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
78 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
79 self.assertEqual(rc, 47)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
80
34302
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
81 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
82 # 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
83 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
84 "import sys; sys.exit(0)"])
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
85 self.assertEqual(rc, 0)
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
86
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
87 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
88 # 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
89 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
90 subprocess.check_call([sys.executable, "-c",
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
91 "import sys; sys.exit(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
92 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
93
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
94 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
95 # 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
96 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
97 [sys.executable, "-c", "print 'BDFL'"])
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55178
diff changeset
98 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
99
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
100 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
101 # 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
102 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
103 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
104 [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
105 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
106
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
107 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
108 # 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
109 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
110 [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
111 stderr=subprocess.STDOUT)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55178
diff changeset
112 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
113
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
114 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
115 # 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
116 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
117 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
118 [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
119 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
120 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
121 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
122
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
123 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
124 # call() function with keyword args
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
125 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
126 newenv["FRUIT"] = "banana"
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
127 rc = subprocess.call([sys.executable, "-c",
59338
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
128 'import sys, os;'
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
129 'sys.exit(os.getenv("FRUIT")=="banana")'],
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
130 env=newenv)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
131 self.assertEqual(rc, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
132
70556
26ea0a46aadd Close #12085: Fix an attribute error in subprocess.Popen destructor if the
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69153
diff changeset
133 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
134 # 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
135 # 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
136 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
137 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
138 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
139 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
140 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
141 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
142
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
143 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
144 # .stdin is None when not redirected
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
145 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
146 stdout=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.stdout.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.stdin, 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_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
153 # .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
154 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
155 '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
156 '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
157 '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
158 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
159 self.addCleanup(p.stdin.close)
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
160 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
161 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
162 self.assertEqual(p.stdout, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
163
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
164 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
165 # .stderr is None when not redirected
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
166 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
167 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
168 self.addCleanup(p.stdout.close)
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
169 self.addCleanup(p.stdin.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
170 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
171 self.assertEqual(p.stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
172
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
173 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
174 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
175 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
176 "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
177 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
178 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
179 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
180
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
181 @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
182 "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
183 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
184 # 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
185 # 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
186 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
187 "import sys; sys.exit(47)"],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
188 executable=sys.executable)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
189 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
190 self.assertEqual(p.returncode, 47)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
191
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
192 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
193 # stdin redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
194 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
195 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
196 stdin=subprocess.PIPE)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
197 p.stdin.write("pear")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
198 p.stdin.close()
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_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
203 # stdin is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
204 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
205 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
206 os.write(d, "pear")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
207 os.lseek(d, 0, 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
208 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
209 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
210 stdin=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
211 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
212 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
213
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
214 def test_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
215 # stdin is set to open file object
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
216 tf = tempfile.TemporaryFile()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
217 tf.write("pear")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
218 tf.seek(0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
219 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
220 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
221 stdin=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
222 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
223 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
224
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
225 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
226 # stdout redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
227 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
228 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
229 stdout=subprocess.PIPE)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
230 self.addCleanup(p.stdout.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
231 self.assertEqual(p.stdout.read(), "orange")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
232
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
233 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
234 # stdout is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
235 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
236 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
237 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
238 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
239 stdout=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
240 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
241 os.lseek(d, 0, 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
242 self.assertEqual(os.read(d, 1024), "orange")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
243
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
244 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
245 # stdout is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
246 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
247 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
248 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
249 stdout=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
250 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
251 tf.seek(0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
252 self.assertEqual(tf.read(), "orange")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
253
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
254 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
255 # stderr redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
256 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
257 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
258 stderr=subprocess.PIPE)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
259 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
260 self.assertStderrEqual(p.stderr.read(), "strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
261
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
262 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
263 # stderr is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
264 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
265 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
266 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
267 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
268 stderr=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
269 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
270 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
271 self.assertStderrEqual(os.read(d, 1024), "strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
272
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
273 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
274 # stderr is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
275 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
276 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
277 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
278 stderr=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
279 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
280 tf.seek(0)
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(tf.read(), "strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
282
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
283 def test_stdout_stderr_pipe(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
284 # capture stdout and stderr to the same pipe
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
285 p = subprocess.Popen([sys.executable, "-c",
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
286 '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
287 '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
288 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
289 'sys.stderr.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
290 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
291 stderr=subprocess.STDOUT)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
292 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
293 self.assertStderrEqual(p.stdout.read(), "appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
294
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
295 def test_stdout_stderr_file(self):
33733
39a13e39d24e Experience with Zope2's tests showed it's a Bad Idea to make unittest
Tim Peters <tim.peters@gmail.com>
parents: 33732
diff changeset
296 # capture stdout and stderr to the same open file
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
297 tf = tempfile.TemporaryFile()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
298 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
299 '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
300 '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
301 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
302 'sys.stderr.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
303 stdout=tf,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
304 stderr=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
305 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
306 tf.seek(0)
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
307 self.assertStderrEqual(tf.read(), "appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
308
39791
5e502de0ceda Fixed bug #1531862: Do not close standard file descriptors in subprocess.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 39772
diff changeset
309 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
310 # 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
311 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
312 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
313 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
314
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
315 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
316 tmpdir = tempfile.gettempdir()
33980
4005f79d281a Use os.chdir/os.getcwd instead of os.path.realpath, to support Tru64
Peter Astrand <astrand@lysator.liu.se>
parents: 33814
diff changeset
317 # We cannot use os.path.realpath to canonicalize the path,
4005f79d281a Use os.chdir/os.getcwd instead of os.path.realpath, to support Tru64
Peter Astrand <astrand@lysator.liu.se>
parents: 33814
diff changeset
318 # since it doesn't expand Tru64 {memb} strings. See bug 1063571.
4005f79d281a Use os.chdir/os.getcwd instead of os.path.realpath, to support Tru64
Peter Astrand <astrand@lysator.liu.se>
parents: 33814
diff changeset
319 cwd = os.getcwd()
4005f79d281a Use os.chdir/os.getcwd instead of os.path.realpath, to support Tru64
Peter Astrand <astrand@lysator.liu.se>
parents: 33814
diff changeset
320 os.chdir(tmpdir)
4005f79d281a Use os.chdir/os.getcwd instead of os.path.realpath, to support Tru64
Peter Astrand <astrand@lysator.liu.se>
parents: 33814
diff changeset
321 tmpdir = os.getcwd()
4005f79d281a Use os.chdir/os.getcwd instead of os.path.realpath, to support Tru64
Peter Astrand <astrand@lysator.liu.se>
parents: 33814
diff changeset
322 os.chdir(cwd)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
323 p = subprocess.Popen([sys.executable, "-c",
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.getcwd())'],
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 cwd=tmpdir)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
328 self.addCleanup(p.stdout.close)
33742
179e42ea2e9c normalize case when comparing directory names (problem reported by
Fredrik Lundh <fredrik@pythonware.com>
parents: 33739
diff changeset
329 normcase = os.path.normcase
179e42ea2e9c normalize case when comparing directory names (problem reported by
Fredrik Lundh <fredrik@pythonware.com>
parents: 33739
diff changeset
330 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
331
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
332 def test_env(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
333 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
334 newenv["FRUIT"] = "orange"
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
335 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
336 'import sys,os;'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
337 'sys.stdout.write(os.getenv("FRUIT"))'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
338 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
339 env=newenv)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
340 self.addCleanup(p.stdout.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
341 self.assertEqual(p.stdout.read(), "orange")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
342
34671
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
343 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
344 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
345 '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
346 '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
347 stdin=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
348 p.communicate("pear")
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
349 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
350
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
351 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
352 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
353 '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
354 stdout=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
355 (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
356 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
357 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
358
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
359 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
360 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
361 '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
362 stderr=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
363 (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
364 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
365 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
366
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
367 def test_communicate(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
368 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
369 '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
370 'sys.stderr.write("pineapple");'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
371 'sys.stdout.write(sys.stdin.read())'],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
372 stdin=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
373 stdout=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
374 stderr=subprocess.PIPE)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
375 self.addCleanup(p.stdout.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
376 self.addCleanup(p.stderr.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
377 self.addCleanup(p.stdin.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
378 (stdout, stderr) = p.communicate("banana")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
379 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
380 self.assertStderrEqual(stderr, "pineapple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
381
47437
6404446e5aa3 Backport r62724 from trunk. Fixes issue 2791. subprocess.Popen.communicate
Gregory P. Smith <greg@mad-scientist.com>
parents: 40620
diff changeset
382 # 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
383 # 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
384 @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
385 "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
386 # 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
387 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
388 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
389 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
390 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
391 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
392 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
393 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
394 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
395 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
396 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
397 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
398
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
399 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
400 # communicate() should return None if no redirection is active
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
401 p = subprocess.Popen([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
402 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
403 (stdout, stderr) = p.communicate()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
404 self.assertEqual(stdout, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
405 self.assertEqual(stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
406
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
407 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
408 # communicate() with writes larger than pipe_buf
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
409 # This test will probably deadlock rather than fail, if
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
410 # communicate() does not work properly.
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
411 x, y = os.pipe()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
412 if mswindows:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
413 pipe_buf = 512
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
414 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
415 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
416 os.close(x)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
417 os.close(y)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
418 p = subprocess.Popen([sys.executable, "-c",
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
419 '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
420 '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
421 'sys.stderr.write("xyz"*%d);'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
422 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
423 stdin=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
424 stdout=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
425 stderr=subprocess.PIPE)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
426 self.addCleanup(p.stdout.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
427 self.addCleanup(p.stderr.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
428 self.addCleanup(p.stdin.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
429 string_to_write = "abc"*pipe_buf
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
430 (stdout, stderr) = p.communicate(string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
431 self.assertEqual(stdout, string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
432
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
433 def test_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
434 # stdin.write before communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
435 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
436 'import sys,os;'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
437 'sys.stdout.write(sys.stdin.read())'],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
438 stdin=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
439 stdout=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
440 stderr=subprocess.PIPE)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
441 self.addCleanup(p.stdout.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
442 self.addCleanup(p.stderr.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
443 self.addCleanup(p.stdin.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
444 p.stdin.write("banana")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
445 (stdout, stderr) = p.communicate("split")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
446 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
447 self.assertStderrEqual(stderr, "")
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
448
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
449 def test_universal_newlines(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
450 p = subprocess.Popen([sys.executable, "-c",
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
451 'import sys,os;' + SETBINARY +
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
452 'sys.stdout.write("line1\\n");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
453 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
454 'sys.stdout.write("line2\\r");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
455 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
456 'sys.stdout.write("line3\\r\\n");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
457 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
458 'sys.stdout.write("line4\\r");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
459 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
460 'sys.stdout.write("\\nline5");'
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
461 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
462 'sys.stdout.write("\\nline6");'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
463 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
464 universal_newlines=1)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
465 self.addCleanup(p.stdout.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
466 stdout = p.stdout.read()
38078
b1cbc605bec9 Try to fix breakage caused by patch #1479181, r45850
Neal Norwitz <nnorwitz@gmail.com>
parents: 38077
diff changeset
467 if hasattr(file, 'newlines'):
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
468 # Interpreter with universal newline support
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
469 self.assertEqual(stdout,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
470 "line1\nline2\nline3\nline4\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
471 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
472 # Interpreter without universal newline support
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
473 self.assertEqual(stdout,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
474 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
475
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
476 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
477 # universal newlines through communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
478 p = subprocess.Popen([sys.executable, "-c",
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
479 'import sys,os;' + SETBINARY +
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
480 'sys.stdout.write("line1\\n");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
481 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
482 'sys.stdout.write("line2\\r");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
483 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
484 'sys.stdout.write("line3\\r\\n");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
485 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
486 'sys.stdout.write("line4\\r");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
487 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
488 'sys.stdout.write("\\nline5");'
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
489 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
490 'sys.stdout.write("\\nline6");'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
491 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
492 universal_newlines=1)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
493 self.addCleanup(p.stdout.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
494 self.addCleanup(p.stderr.close)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
495 (stdout, stderr) = p.communicate()
38078
b1cbc605bec9 Try to fix breakage caused by patch #1479181, r45850
Neal Norwitz <nnorwitz@gmail.com>
parents: 38077
diff changeset
496 if hasattr(file, 'newlines'):
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
497 # Interpreter with universal newline support
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
498 self.assertEqual(stdout,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
499 "line1\nline2\nline3\nline4\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
500 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
501 # 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
502 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
503 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
504
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
505 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
506 # Make sure we leak no resources
64815
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
507 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
508 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
509 else:
64815
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
510 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
511 handles = []
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
512 try:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
513 for i in range(max_handles):
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
514 try:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
515 handles.append(os.open(test_support.TESTFN,
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
516 os.O_WRONLY | os.O_CREAT))
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
517 except OSError as e:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
518 if e.errno != errno.EMFILE:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
519 raise
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
520 break
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
521 else:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
522 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
523 "(tried %d)" % max_handles)
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
524 # 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
525 for i in range(10):
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
526 os.close(handles.pop())
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
527 # 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
528 # 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
529 for i in range(15):
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
530 p = subprocess.Popen([sys.executable, "-c",
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
531 "import sys;"
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
532 "sys.stdout.write(sys.stdin.read())"],
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
533 stdin=subprocess.PIPE,
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
534 stdout=subprocess.PIPE,
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
535 stderr=subprocess.PIPE)
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
536 data = p.communicate(b"lime")[0]
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
537 self.assertEqual(data, b"lime")
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
538 finally:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
539 for h in handles:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
540 os.close(h)
79569
715b3cbcb438 Clean up a test turd.
Mark Dickinson <mdickinson@enthought.com>
parents: 75526
diff changeset
541 test_support.unlink(test_support.TESTFN)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
542
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
543 def test_list2cmdline(self):
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')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
546 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
547 '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
548 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
549 'ab\\"c " \\\\" d')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
550 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
551 'a\\\\\\b "de fg" h')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
552 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
553 'a\\\\\\"b c d')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
554 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
555 '"a\\\\b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
556 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
557 '"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
558 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
559 'ab ""')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
560
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
561
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
562 def test_poll(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
563 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
564 "-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
565 count = 0
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
566 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
567 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
568 count += 1
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
569 # 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
570 # 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
571 # 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
572 # 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
573 self.assertGreaterEqual(count, 2)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
574 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
575 self.assertEqual(p.poll(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
576
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
577
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
578 def test_wait(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
579 p = subprocess.Popen([sys.executable,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
580 "-c", "import time; time.sleep(2)"])
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
581 self.assertEqual(p.wait(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
582 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
583 self.assertEqual(p.wait(), 0)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
584
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
585
79d2ae0a62ef Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents: 33998
diff changeset
586 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
587 # 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
588 # 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
589 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
590 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
591
51637
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
592 def test_leaking_fds_on_error(self):
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
593 # 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
594 # 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
595 # 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
596 # 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
597 # 1024 times (each call leaked two fds).
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
598 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
599 # 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
600 with self.assertRaises(EnvironmentError) as c:
51637
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
601 subprocess.Popen(['nonexisting_i_hope'],
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
602 stdout=subprocess.PIPE,
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
603 stderr=subprocess.PIPE)
68435
0a19b3278fa2 #11490: EACCES can also mean command not found
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
604 # 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
605 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
606 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
607
63773
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
608 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
609 # 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
610 # 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
611 ifhandle, ifname = mkstemp()
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
612 ofhandle, ofname = mkstemp()
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
613 efhandle, efname = mkstemp()
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
614 try:
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
615 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
616 stderr=efhandle)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
617 except OSError:
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
618 os.close(ifhandle)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
619 os.remove(ifname)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
620 os.close(ofhandle)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
621 os.remove(ofname)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
622 os.close(efhandle)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
623 os.remove(efname)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
624 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
625 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
626 self.assertFalse(os.path.exists(efname))
51637
15d8b2db2be4 Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47338
diff changeset
627
69153
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
628 def test_communicate_epipe(self):
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
629 # 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
630 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
631 stdin=subprocess.PIPE,
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
632 stdout=subprocess.PIPE,
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
633 stderr=subprocess.PIPE)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
634 self.addCleanup(p.stdout.close)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
635 self.addCleanup(p.stderr.close)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
636 self.addCleanup(p.stdin.close)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
637 p.communicate("x" * 2**20)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
638
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
639 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
640 # 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
641 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
642 stdin=subprocess.PIPE)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
643 self.addCleanup(p.stdin.close)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
644 time.sleep(2)
c10d55c51d81 Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 68435
diff changeset
645 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
646
80371
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
647 # This test is Linux-ish specific for simplicity to at least have
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
648 # some coverage. It is not a platform specific bug.
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
649 @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
650 "Linux specific")
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
651 def test_failed_child_execute_fd_leak(self):
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
652 """Test for the fork() failure fd leak reported in issue16327."""
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
653 fd_directory = '/proc/%d/fd' % os.getpid()
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
654 fds_before_popen = os.listdir(fd_directory)
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
655 with self.assertRaises(PopenTestException):
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
656 PopenExecuteChildRaises(
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
657 [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE,
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
658 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
659
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
660 # NOTE: This test doesn't verify that the real _execute_child
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
661 # does not close the file descriptors itself on the way out
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
662 # during an exception. Code inspection has confirmed that.
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
663
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
664 fds_after_exception = os.listdir(fd_directory)
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
665 self.assertEqual(fds_before_popen, fds_after_exception)
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
666
e67620048d2f Fixes issue #16327: The subprocess module no longer leaks file descriptors
Gregory P. Smith <greg@krypto.org>
parents: 79569
diff changeset
667
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
668 # 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
669 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
670 """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
671 old_limit = None
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
672
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
673 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
674 """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
675 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
676 try:
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
677 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
678 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
679 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
680 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
681
63152
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
682 if sys.platform == 'darwin':
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
683 # 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
684 # 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
685 # when it is.
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
686 #
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
687 # 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
688 # that might trigger the next manager.
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
689 value = subprocess.Popen(['/usr/bin/defaults', 'read',
63156
bdc069a1a721 Merged revisions 83079 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63097
diff changeset
690 'com.apple.CrashReporter', 'DialogType'],
bdc069a1a721 Merged revisions 83079 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63097
diff changeset
691 stdout=subprocess.PIPE).communicate()[0]
63152
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
692 if value.strip() == b'developer':
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
693 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
694 sys.stdout.flush()
63156
bdc069a1a721 Merged revisions 83079 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63097
diff changeset
695
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
696 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
697 """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
698 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
699 return
73922
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
700 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
701 try:
fd5f43737546 you can't get resource.error if you can't import resource
Benjamin Peterson <benjamin@python.org>
parents: 72076
diff changeset
702 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
703 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
704 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
705
71235
8a4c9c154b5d Issue #12493: skip test_communicate_eintr() if signal.SIGALRM is missing
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71230
diff changeset
706 @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
707 "Requires signal.SIGALRM")
71230
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
708 def test_communicate_eintr(self):
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
709 # Issue #12493: communicate() should handle EINTR
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
710 def handler(signum, frame):
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
711 pass
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
712 old_handler = signal.signal(signal.SIGALRM, handler)
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
713 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
714
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
715 # the process is running for 2 seconds
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
716 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
717 for stream in ('stdout', 'stderr'):
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
718 kw = {stream: subprocess.PIPE}
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
719 with subprocess.Popen(args, **kw) as process:
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
720 signal.alarm(1)
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
721 # communicate() will be interrupted by SIGALRM
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
722 process.communicate()
6a28ccde2f1b Issue #12493: subprocess: communicate() handles EINTR
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70558
diff changeset
723
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
724
59338
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
725 @unittest.skipIf(mswindows, "POSIX specific tests")
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
726 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
727
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
728 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
729 # 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
730 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
731 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
732 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
733 # 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
734 self.assertIn("os.chdir", c.exception.child_traceback)
39346
bd50a42b7df0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39341
diff changeset
735
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
736 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
737 # 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
738 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
739 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
740 "import os; os.abort()"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
741 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
742 self.assertEqual(-p.returncode, signal.SIGABRT)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
743
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
744 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
745 # 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
746 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
747 "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
748 "sys.stdout.write(os.getenv('FRUIT'))"],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
749 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
750 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
751 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
752 self.assertEqual(p.stdout.read(), "apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
753
80389
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
754 class _TestExecuteChildPopen(subprocess.Popen):
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
755 """Used to test behavior at the end of _execute_child."""
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
756 def __init__(self, testcase, *args, **kwargs):
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
757 self._testcase = testcase
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
758 subprocess.Popen.__init__(self, *args, **kwargs)
80382
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
759
80389
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
760 def _execute_child(
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
761 self, args, executable, preexec_fn, close_fds, cwd, env,
80382
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
762 universal_newlines, startupinfo, creationflags, shell,
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
763 p2cread, p2cwrite,
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
764 c2pread, c2pwrite,
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
765 errread, errwrite):
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
766 try:
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
767 subprocess.Popen._execute_child(
80389
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
768 self, args, executable, preexec_fn, close_fds,
80382
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
769 cwd, env, universal_newlines,
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
770 startupinfo, creationflags, shell,
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
771 p2cread, p2cwrite,
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
772 c2pread, c2pwrite,
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
773 errread, errwrite)
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
774 finally:
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
775 # Open a bunch of file descriptors and verify that
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
776 # none of them are the same as the ones the Popen
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
777 # instance is using for stdin/stdout/stderr.
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
778 devzero_fds = [os.open("/dev/zero", os.O_RDONLY)
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
779 for _ in range(8)]
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
780 try:
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
781 for fd in devzero_fds:
80389
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
782 self._testcase.assertNotIn(
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
783 fd, (p2cwrite, c2pread, errread))
80382
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
784 finally:
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
785 map(os.close, devzero_fds)
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
786
80389
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
787 @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
788 def test_preexec_errpipe_does_not_double_close_pipes(self):
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
789 """Issue16140: Don't double close pipes on preexec error."""
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
790
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
791 def raise_it():
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
792 raise RuntimeError("force the _execute_child() errpipe_data path.")
80382
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
793
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
794 with self.assertRaises(RuntimeError):
80389
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
795 self._TestExecuteChildPopen(
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
796 self, [sys.executable, "-c", "pass"],
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
797 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
f02123b1b446 Refactor test_preexec_errpipe to not create an uncollectable reference cycle.
Gregory P. Smith <greg@krypto.org>
parents: 80383
diff changeset
798 stderr=subprocess.PIPE, preexec_fn=raise_it)
80382
2bdd984a55ac Fix issue #16140 bug that the fix to issue #16327 added - don't double
Gregory P. Smith <greg@krypto.org>
parents: 80371
diff changeset
799
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
800 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
801 # 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
802 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
803 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
804 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
805 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
806 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
807 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
808 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
809 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
810 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
811 self.assertEqual(p.returncode, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
812
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
813 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
814 # 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
815 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
816 [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
817 "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
818 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
819 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
820 [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
821 "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
822 creationflags=47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
823
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
824 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
825 # 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
826 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
827 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
828 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
829 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
830 env=newenv)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
831 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
832 self.assertEqual(p.stdout.read().strip(), "apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
833
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
834 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
835 # 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
836 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
837 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
838 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
839 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
840 env=newenv)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
841 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
842 self.assertEqual(p.stdout.read().strip(), "apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
843
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
844 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
845 # 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
846 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
847 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
848 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
849 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
850 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
851 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
852 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
853 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
854 self.assertEqual(rc, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
855
63096
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
856 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
857 # 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
858 shells = []
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
859 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
860 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
861 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
862 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
863 shells.append(sh)
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
864 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
865 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
866 sh = '/bin/sh'
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
867 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
868 # 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
869 shells.append(sh)
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
870 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
871 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
872 stdout=subprocess.PIPE)
65956
430d50e3ac53 Merged revisions 86185 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 64928
diff changeset
873 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
874 self.assertEqual(p.stdout.read().strip(), sh)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
875
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
876 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
877 # 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
878 # 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
879 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
880 import sys, time
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
881 sys.stdout.write('x\\n')
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
882 sys.stdout.flush()
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
883 time.sleep(30)
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
884 """],
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
885 close_fds=True,
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
886 stdin=subprocess.PIPE,
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
887 stdout=subprocess.PIPE,
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
888 stderr=subprocess.PIPE)
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
889 # 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
890 # sending any signal.
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
891 p.stdout.read(1)
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
892 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
893 return p
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
894
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
895 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
896 # 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
897 # 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
898 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
899 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
900 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
901 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
902 """],
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
903 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
904 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
905 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
906 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
907 # 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
908 # 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
909 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
910 # 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
911 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
912 # 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
913 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
914 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
915
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
916 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
917 p = self._kill_process('send_signal', signal.SIGINT)
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
918 _, 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
919 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
920 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
921
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
922 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
923 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
924 _, 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
925 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
926 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
927
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
928 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
929 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
930 _, 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
931 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
932 self.assertEqual(p.wait(), -signal.SIGTERM)
63097
3fdd9b43024c Merged revisions 82973 via svnmerge from
Stefan Krah <stefan@bytereef.org>
parents: 62403
diff changeset
933
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
934 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
935 # 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
936 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
937
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
938 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
939 # 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
940 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
941
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
942 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
943 # 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
944 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
945
67312
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
946 def check_close_std_fds(self, fds):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
947 # 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
948 # some standard fds closed
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
949 stdin = 0
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
950 newfds = []
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
951 for a in fds:
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
952 b = os.dup(a)
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
953 newfds.append(b)
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
954 if a == 0:
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
955 stdin = b
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
956 try:
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
957 for fd in fds:
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
958 os.close(fd)
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
959 out, err = subprocess.Popen([sys.executable, "-c",
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
960 'import sys;'
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
961 'sys.stdout.write("apple");'
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
962 'sys.stdout.flush();'
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
963 'sys.stderr.write("orange")'],
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
964 stdin=stdin,
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
965 stdout=subprocess.PIPE,
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
966 stderr=subprocess.PIPE).communicate()
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
967 err = test_support.strip_python_stderr(err)
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
968 self.assertEqual((out, err), (b'apple', b'orange'))
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
969 finally:
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
970 for b, a in zip(newfds, fds):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
971 os.dup2(b, a)
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
972 for b in newfds:
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
973 os.close(b)
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
974
67312
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
975 def test_close_fd_0(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
976 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
977
67312
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
978 def test_close_fd_1(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
979 self.check_close_std_fds([1])
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
980
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
981 def test_close_fd_2(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
982 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
983
67312
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
984 def test_close_fds_0_1(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
985 self.check_close_std_fds([0, 1])
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
986
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
987 def test_close_fds_0_2(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
988 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
989
67312
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
990 def test_close_fds_1_2(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
991 self.check_close_std_fds([1, 2])
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
992
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
993 def test_close_fds_0_1_2(self):
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
994 # 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
995 # all standard fds closed.
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66894
diff changeset
996 self.check_close_std_fds([0, 1, 2])
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
997
71530
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
998 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
999 # 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
1000 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
1001 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
1002 try:
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
1003 # 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
1004 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
1005 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
1006
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
1007 # 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
1008 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
1009 try:
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
1010 # 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
1011 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
1012 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
1013
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
1014 # 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
1015 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
1016 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
1017
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
1018 # 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
1019 # 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
1020 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
1021 '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
1022 '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
1023 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
1024 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
1025 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
1026 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
1027
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
1028 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
1029 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
1030
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
1031 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
1032 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
1033 finally:
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
1034 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
1035 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
1036 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
1037
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
1038 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
1039 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
1040
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
1041 finally:
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
1042 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
1043 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
1044
6119440bae30 Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 71235
diff changeset
1045 # 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
1046 # 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
1047 # 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
1048 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
1049 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
1050 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
1051 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
1052 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
1053 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
1054 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
1055
66894
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
1056 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
1057 # 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
1058 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
1059 subdir="subprocessdata")
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
1060 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
1061 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
1062 stdout, stderr = p.communicate()
072ee3f743c2 Merged revisions 87233 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 65978
diff changeset
1063 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
1064 " 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
1065
71902
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1066 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
1067 # 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
1068 # 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
1069 # remain a zombie.
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1070 # 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
1071 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
1072 'import sys, time;'
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1073 'time.sleep(0.2)'],
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1074 stdout=subprocess.PIPE,
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1075 stderr=subprocess.PIPE)
71915
e7fe0c05d722 Fix resource leaks in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71912
diff changeset
1076 self.addCleanup(p.stdout.close)
e7fe0c05d722 Fix resource leaks in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71912
diff changeset
1077 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
1078 ident = id(p)
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1079 pid = p.pid
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1080 del p
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1081 # 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
1082 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
1083
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1084 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
1085 # 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
1086 # 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
1087 # 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
1088 # leak.
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1089 # 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
1090 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
1091 'import time;'
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1092 'time.sleep(3)'],
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1093 stdout=subprocess.PIPE,
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1094 stderr=subprocess.PIPE)
71915
e7fe0c05d722 Fix resource leaks in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71912
diff changeset
1095 self.addCleanup(p.stdout.close)
e7fe0c05d722 Fix resource leaks in test_subprocess.
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 71912
diff changeset
1096 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
1097 ident = id(p)
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1098 pid = p.pid
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1099 del p
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1100 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
1101 # 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
1102 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
1103
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1104 # 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
1105 # 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
1106 time.sleep(0.2)
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1107 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
1108 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
1109 stdout=subprocess.PIPE,
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1110 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
1111 pass
7d358379c5fb Issue #12650: only run the tests on Unix.
Charles-François Natali <neologix@free.fr>
parents: 71899
diff changeset
1112 # 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
1113 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
1114 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
1115
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
1116 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
1117 # 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
1118 # 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
1119 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
1120 '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
1121 '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
1122 ],
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1123 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
1124 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
1125
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1126 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
1127 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
1128 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
1129 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
1130 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
1131 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
1132 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
1133 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
1134 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
1135 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
1136 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
1137 """ % [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
1138 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
1139 ],
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1140 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
1141 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
1142 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
1143 _, 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
1144
498b03a55297 Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
Charles-François Natali <neologix@free.fr>
parents: 71915
diff changeset
1145 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
1146
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
1147
59338
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
1148 @unittest.skipUnless(mswindows, "Windows specific tests")
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
1149 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
1150
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
1151 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
1152 # 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
1153 # 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
1154 # 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
1155 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
1156 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
1157 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
1158 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
1159 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
1160 # 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
1161 # 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
1162 # 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
1163 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
1164 startupinfo=startupinfo)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1165
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
1166 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
1167 # 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
1168 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
1169 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
1170 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
1171 ' -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
1172 creationflags=CREATE_NEW_CONSOLE)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1173
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
1174 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
1175 # 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
1176 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
1177 [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
1178 "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
1179 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
1180 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
1181 [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
1182 "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
1183 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
1184 close_fds=True)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1185
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
1186 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
1187 # 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
1188 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
1189 "import sys; sys.exit(47)"],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1190 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
1191 self.assertEqual(rc, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1192
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
1193 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
1194 # 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
1195 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
1196 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
1197 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
1198 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
1199 env=newenv)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
1200 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
1201 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
1202
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
1203 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
1204 # 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
1205 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
1206 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
1207 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
1208 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
1209 env=newenv)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
1210 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
1211 self.assertIn("physalis", p.stdout.read())
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1212
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
1213 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
1214 # 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
1215 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
1216 ' -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
1217 self.assertEqual(rc, 47)
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
1218
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
1219 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
1220 # 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
1221 p = subprocess.Popen([sys.executable, "-c", """if 1:
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1222 import sys, time
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1223 sys.stdout.write('x\\n')
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1224 sys.stdout.flush()
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1225 time.sleep(30)
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1226 """],
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1227 stdin=subprocess.PIPE,
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1228 stdout=subprocess.PIPE,
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1229 stderr=subprocess.PIPE)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
1230 self.addCleanup(p.stdout.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
1231 self.addCleanup(p.stderr.close)
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
1232 self.addCleanup(p.stdin.close)
64928
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1233 # 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
1234 # sending any signal.
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1235 p.stdout.read(1)
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1236 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
1237 _, 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
1238 self.assertStderrEqual(stderr, '')
64928
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
1239 returncode = p.wait()
59463
a3f6178354f5 Replace the stderr logging with assertNotEqual(returncode, 0).
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59441
diff changeset
1240 self.assertNotEqual(returncode, 0)
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
1241
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
1242 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
1243 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
1244 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
1245 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
1246 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
1247 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
1248 """],
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
1249 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
1250 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
1251 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
1252 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
1253 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
1254 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
1255 # 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
1256 # 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
1257 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
1258 # 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
1259 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
1260 # 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
1261 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
1262 _, 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
1263 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
1264 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
1265 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
1266
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
1267 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
1268 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
1269
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
1270 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
1271 self._kill_process('kill')
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
1272
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
1273 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
1274 self._kill_process('terminate')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1275
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
1276 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
1277 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
1278
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
1279 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
1280 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
1281
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
1282 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
1283 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
1284
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
1285
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
1286 @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
1287 "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
1288 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
1289 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
1290 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
1291 ProcessTestCase.setUp(self)
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
1292
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
1293 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
1294 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
1295 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
1296
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1297
59208
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1298 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
1299 @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
1300 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
1301 record_calls = []
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1302 def fake_os_func(*args):
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1303 record_calls.append(args)
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1304 if len(record_calls) == 2:
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1305 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
1306 return tuple(reversed(args))
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1307
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1308 self.assertEqual((999, 256),
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1309 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
1310 self.assertEqual([(256, 999)], record_calls)
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1311 # 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
1312 self.assertEqual((666,),
12469f20656b Merged revisions 78523 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 51637
diff changeset
1313 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
1314 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
1315
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
1316 @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
1317 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
1318
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
1319 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
1320 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
1321 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
1322 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
1323 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
1324 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
1325 )
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
1326 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
1327
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
1328 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
1329 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
1330 super(CommandsWithSpaces, self).tearDown()
59218
f53fe33e03ea Merged revisions 78532 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59208
diff changeset
1331
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
1332 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
1333 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
1334 p = subprocess.Popen(*args, **kwargs)
65978
239ffee5edae Merged revisions 86207 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65956
diff changeset
1335 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
1336 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
1337 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
1338 "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
1339 )
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
1340
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
1341 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
1342 # 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
1343 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
1344 "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
1345
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
1346 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
1347 # 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
1348 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
1349
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
1350 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
1351 # 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
1352 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
1353 "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
1354
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
1355 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
1356 # 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
1357 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
1358
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1359 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
1360 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
1361 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
1362 Win32ProcessTestCase,
59206
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
1363 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
1364 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
1365 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
1366
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
1367 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
1368 test_support.reap_children()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1369
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1370 if __name__ == "__main__":
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1371 test_main()