annotate Lib/test/test_subprocess.py @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents 8cc4be648ab4
children 430d50e3ac53
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
59206
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
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
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
13 mswindows = (sys.platform == "win32")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
14
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
15 #
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
16 # Depends on the following external programs: Python
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
17 #
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
18
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
19 if mswindows:
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
20 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
21 'os.O_BINARY);')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
22 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
23 SETBINARY = ''
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
24
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
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
26 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
27 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
28 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
29 # 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
30 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
31 """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
32 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
33 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
34
33739
76355363101b New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents: 33738
diff changeset
35
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
36 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
37 def setUp(self):
38967
a21e420a597c Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38961
diff changeset
38 # 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
39 # 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
40 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
41
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
42 def tearDown(self):
d8b2fa88788b #2777: Enable test_send_signal, test_kill and test_terminate on all platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59338
diff changeset
43 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
44 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
45 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
46 self.assertFalse(subprocess._active, "subprocess._active not empty")
d8b2fa88788b #2777: Enable test_send_signal, test_kill and test_terminate on all platforms.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59338
diff changeset
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
48 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
49 # 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
50 # 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
51 # 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
52 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
53 self.assertEqual(actual, expected, msg)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
54
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
55
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
56 class ProcessTestCase(BaseTestCase):
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
57
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
58 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
59 # call() function with sequence argument
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
60 rc = subprocess.call([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
61 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
62 self.assertEqual(rc, 47)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
63
34302
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
64 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
65 # 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
66 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
67 "import sys; sys.exit(0)"])
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
68 self.assertEqual(rc, 0)
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
69
998fb1e1c8e9 New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents: 34052
diff changeset
70 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
71 # 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
72 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
73 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
74 "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
75 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
76
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
77 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
78 # 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
79 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
80 [sys.executable, "-c", "print 'BDFL'"])
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55178
diff changeset
81 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
82
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
83 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
84 # 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
85 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
86 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
87 [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
88 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
89
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
90 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
91 # 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
92 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
93 [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
94 stderr=subprocess.STDOUT)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55178
diff changeset
95 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
96
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
97 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
98 # 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
99 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
100 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
101 [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
102 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
103 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
104 self.assertIn('stdout', c.exception.args[0])
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
105
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
106 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
107 # call() function with keyword args
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
108 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
109 newenv["FRUIT"] = "banana"
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
110 rc = subprocess.call([sys.executable, "-c",
59338
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
111 'import sys, os;'
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
112 'sys.exit(os.getenv("FRUIT")=="banana")'],
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
113 env=newenv)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
114 self.assertEqual(rc, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
115
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
116 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
117 # .stdin is None when not redirected
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
118 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
119 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
120 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
121 self.assertEqual(p.stdin, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
122
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
123 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
124 # .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
125 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
126 '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
127 '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
128 '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
129 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
130 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
131 self.assertEqual(p.stdout, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
132
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
133 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
134 # .stderr is None when not redirected
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
135 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
136 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
137 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
138 self.assertEqual(p.stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
139
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
140 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
141 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
142 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
143 "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
144 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
145 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
146 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
147
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
148 @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
149 "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
150 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
151 # 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
152 # 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
153 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
154 "import sys; sys.exit(47)"],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
155 executable=sys.executable)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
156 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
157 self.assertEqual(p.returncode, 47)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
158
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
159 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
160 # stdin redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
161 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
162 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
163 stdin=subprocess.PIPE)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
164 p.stdin.write("pear")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
165 p.stdin.close()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
166 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
167 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
168
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
169 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
170 # stdin is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
171 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
172 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
173 os.write(d, "pear")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
174 os.lseek(d, 0, 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
175 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
176 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
177 stdin=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
178 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
179 self.assertEqual(p.returncode, 1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
180
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
181 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
182 # stdin is set to open file object
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
183 tf = tempfile.TemporaryFile()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
184 tf.write("pear")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
185 tf.seek(0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
186 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
187 'import sys; sys.exit(sys.stdin.read() == "pear")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
188 stdin=tf)
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, 1)
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_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
193 # stdout 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.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
196 stdout=subprocess.PIPE)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
197 self.assertEqual(p.stdout.read(), "orange")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
198
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
199 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
200 # stdout is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
201 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
202 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
203 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
204 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
205 stdout=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
206 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
207 os.lseek(d, 0, 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
208 self.assertEqual(os.read(d, 1024), "orange")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
209
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
210 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
211 # stdout is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
212 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
213 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
214 'import sys; sys.stdout.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
215 stdout=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
216 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
217 tf.seek(0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
218 self.assertEqual(tf.read(), "orange")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
219
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
220 def test_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
221 # stderr redirection
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
222 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
223 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
224 stderr=subprocess.PIPE)
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
225 self.assertStderrEqual(p.stderr.read(), "strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
226
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
227 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
228 # stderr is set to open file descriptor
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
229 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
230 d = tf.fileno()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
231 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
232 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
233 stderr=d)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
234 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
235 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
236 self.assertStderrEqual(os.read(d, 1024), "strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
237
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
238 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
239 # stderr is set to open file object
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
240 tf = tempfile.TemporaryFile()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
241 p = subprocess.Popen([sys.executable, "-c",
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
242 'import sys; sys.stderr.write("strawberry")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
243 stderr=tf)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
244 p.wait()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
245 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
246 self.assertStderrEqual(tf.read(), "strawberry")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
247
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
248 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
249 # capture stdout and stderr to the same pipe
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
250 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
251 '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
252 '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
253 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
254 'sys.stderr.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
255 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
256 stderr=subprocess.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
257 self.assertStderrEqual(p.stdout.read(), "appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
258
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
259 def test_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
260 # 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
261 tf = tempfile.TemporaryFile()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
262 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
263 '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
264 '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
265 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
266 'sys.stderr.write("orange")'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
267 stdout=tf,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
268 stderr=tf)
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 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
271 self.assertStderrEqual(tf.read(), "appleorange")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
272
39793
363ef804a096 Fixed subprocess bug #1531862 again, after removing tests
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 39772
diff changeset
273 def test_stdout_filedes_of_stdout(self):
363ef804a096 Fixed subprocess bug #1531862 again, after removing tests
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 39772
diff changeset
274 # stdout is set to 1 (#1531862).
363ef804a096 Fixed subprocess bug #1531862 again, after removing tests
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 39772
diff changeset
275 cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))"
363ef804a096 Fixed subprocess bug #1531862 again, after removing tests
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 39772
diff changeset
276 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
277 self.assertEqual(rc, 2)
39793
363ef804a096 Fixed subprocess bug #1531862 again, after removing tests
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 39772
diff changeset
278
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
279 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
280 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
281 # 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
282 # 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
283 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
284 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
285 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
286 os.chdir(cwd)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
287 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
288 'import sys,os;'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
289 'sys.stdout.write(os.getcwd())'],
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 cwd=tmpdir)
33742
179e42ea2e9c normalize case when comparing directory names (problem reported by
Fredrik Lundh <fredrik@pythonware.com>
parents: 33739
diff changeset
292 normcase = os.path.normcase
179e42ea2e9c normalize case when comparing directory names (problem reported by
Fredrik Lundh <fredrik@pythonware.com>
parents: 33739
diff changeset
293 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
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_env(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
296 newenv = os.environ.copy()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
297 newenv["FRUIT"] = "orange"
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,os;'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
300 'sys.stdout.write(os.getenv("FRUIT"))'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
301 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
302 env=newenv)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
303 self.assertEqual(p.stdout.read(), "orange")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
304
34671
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
305 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
306 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
307 '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
308 '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
309 stdin=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
310 p.communicate("pear")
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
311 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
312
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
313 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
314 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
315 '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
316 stdout=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
317 (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
318 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
319 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
320
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
321 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
322 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
323 '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
324 stderr=subprocess.PIPE)
db7a8310b19a Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents: 34302
diff changeset
325 (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
326 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
327 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
328
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
329 def test_communicate(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
330 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
331 '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
332 'sys.stderr.write("pineapple");'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
333 'sys.stdout.write(sys.stdin.read())'],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
334 stdin=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
335 stdout=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
336 stderr=subprocess.PIPE)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
337 (stdout, stderr) = p.communicate("banana")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
338 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
339 self.assertStderrEqual(stderr, "pineapple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
340
47338
273e8de34603 Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents: 47119
diff changeset
341 # This test is Linux specific for simplicity to at least have
273e8de34603 Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents: 47119
diff changeset
342 # 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
343 @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
344 "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
345 # 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
346 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
347 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
348 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
349 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
350 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
351 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
352 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
353 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
354 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
355 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
356 self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
47338
273e8de34603 Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents: 47119
diff changeset
357
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
358 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
359 # communicate() should return None if no redirection is active
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
360 p = subprocess.Popen([sys.executable, "-c",
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
361 "import sys; sys.exit(47)"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
362 (stdout, stderr) = p.communicate()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
363 self.assertEqual(stdout, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
364 self.assertEqual(stderr, None)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
365
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
366 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
367 # communicate() with writes larger than pipe_buf
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
368 # This test will probably deadlock rather than fail, if
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
369 # communicate() does not work properly.
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
370 x, y = os.pipe()
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
371 if mswindows:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
372 pipe_buf = 512
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
373 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
374 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
375 os.close(x)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
376 os.close(y)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
377 p = subprocess.Popen([sys.executable, "-c",
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
378 '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
379 '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
380 'sys.stderr.write("xyz"*%d);'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
381 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
382 stdin=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
383 stdout=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
384 stderr=subprocess.PIPE)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
385 string_to_write = "abc"*pipe_buf
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
386 (stdout, stderr) = p.communicate(string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
387 self.assertEqual(stdout, string_to_write)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
388
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
389 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
390 # stdin.write before communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
391 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
392 'import sys,os;'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
393 'sys.stdout.write(sys.stdin.read())'],
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
394 stdin=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
395 stdout=subprocess.PIPE,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
396 stderr=subprocess.PIPE)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
397 p.stdin.write("banana")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
398 (stdout, stderr) = p.communicate("split")
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
399 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
400 self.assertStderrEqual(stderr, "")
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
401
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
402 def test_universal_newlines(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
403 p = subprocess.Popen([sys.executable, "-c",
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
404 'import sys,os;' + SETBINARY +
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
405 'sys.stdout.write("line1\\n");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
406 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
407 'sys.stdout.write("line2\\r");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
408 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
409 'sys.stdout.write("line3\\r\\n");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
410 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
411 'sys.stdout.write("line4\\r");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
412 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
413 'sys.stdout.write("\\nline5");'
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
414 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
415 'sys.stdout.write("\\nline6");'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
416 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
417 universal_newlines=1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
418 stdout = p.stdout.read()
38078
b1cbc605bec9 Try to fix breakage caused by patch #1479181, r45850
Neal Norwitz <nnorwitz@gmail.com>
parents: 38077
diff changeset
419 if hasattr(file, 'newlines'):
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
420 # Interpreter with universal newline support
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
421 self.assertEqual(stdout,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
422 "line1\nline2\nline3\nline4\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
423 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
424 # Interpreter without universal newline support
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
425 self.assertEqual(stdout,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
426 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
427
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
428 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
429 # universal newlines through communicate()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
430 p = subprocess.Popen([sys.executable, "-c",
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
431 'import sys,os;' + SETBINARY +
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
432 'sys.stdout.write("line1\\n");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
433 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
434 'sys.stdout.write("line2\\r");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
435 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
436 'sys.stdout.write("line3\\r\\n");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
437 'sys.stdout.flush();'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
438 'sys.stdout.write("line4\\r");'
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
439 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
440 'sys.stdout.write("\\nline5");'
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
441 'sys.stdout.flush();'
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
442 'sys.stdout.write("\\nline6");'],
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
443 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
444 universal_newlines=1)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
445 (stdout, stderr) = p.communicate()
38078
b1cbc605bec9 Try to fix breakage caused by patch #1479181, r45850
Neal Norwitz <nnorwitz@gmail.com>
parents: 38077
diff changeset
446 if hasattr(file, 'newlines'):
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
447 # Interpreter with universal newline support
33732
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
448 self.assertEqual(stdout,
d9404db29bc6 Wrap long lines.
Tim Peters <tim.peters@gmail.com>
parents: 33731
diff changeset
449 "line1\nline2\nline3\nline4\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
450 else:
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
451 # 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
452 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
453 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
454
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
455 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
456 # Make sure we leak no resources
64815
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
457 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
458 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
459 else:
64815
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
460 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
461 handles = []
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
462 try:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
463 for i in range(max_handles):
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
464 try:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
465 handles.append(os.open(test_support.TESTFN,
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
466 os.O_WRONLY | os.O_CREAT))
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
467 except OSError as e:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
468 if e.errno != errno.EMFILE:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
469 raise
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
470 break
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
471 else:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
472 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
473 "(tried %d)" % max_handles)
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
474 # 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
475 for i in range(10):
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
476 os.close(handles.pop())
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
477 # 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
478 # 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
479 for i in range(15):
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
480 p = subprocess.Popen([sys.executable, "-c",
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
481 "import sys;"
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
482 "sys.stdout.write(sys.stdin.read())"],
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
483 stdin=subprocess.PIPE,
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
484 stdout=subprocess.PIPE,
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
485 stderr=subprocess.PIPE)
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
486 data = p.communicate(b"lime")[0]
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
487 self.assertEqual(data, b"lime")
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
488 finally:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
489 for h in handles:
04b45d451059 Merged revisions 84883 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64809
diff changeset
490 os.close(h)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
491
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
492 def test_list2cmdline(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
493 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
494 '"a b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
495 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
496 '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
497 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
498 'ab\\"c " \\\\" d')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
499 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
500 'a\\\\\\b "de fg" h')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
501 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
502 'a\\\\\\"b c d')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
503 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
504 '"a\\\\b c" d e')
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
505 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
506 '"a\\\\b\\ c" d e')
40619
2df80e716e5c Fix for bug #1634343: allow specifying empty arguments on Windows
Peter Astrand <astrand@lysator.liu.se>
parents: 39793
diff changeset
507 self.assertEqual(subprocess.list2cmdline(['ab', '']),
2df80e716e5c Fix for bug #1634343: allow specifying empty arguments on Windows
Peter Astrand <astrand@lysator.liu.se>
parents: 39793
diff changeset
508 'ab ""')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
509
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
510
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
511 def test_poll(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
512 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
513 "-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
514 count = 0
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
515 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
516 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
517 count += 1
a4f747e72138 Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents: 33737
diff changeset
518 # 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
519 # 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
520 # 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
521 # 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
522 self.assertGreaterEqual(count, 2)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
523 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
524 self.assertEqual(p.poll(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
525
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
526
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
527 def test_wait(self):
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
528 p = subprocess.Popen([sys.executable,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
529 "-c", "import time; time.sleep(2)"])
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
530 self.assertEqual(p.wait(), 0)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
531 # Subsequent invocations should just return the returncode
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
532 self.assertEqual(p.wait(), 0)
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
533
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
534
79d2ae0a62ef Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents: 33998
diff changeset
535 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
536 # 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
537 # 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
538 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
539 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
540
51636
556990bb4f0a #5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents: 49984
diff changeset
541 def test_leaking_fds_on_error(self):
556990bb4f0a #5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents: 49984
diff changeset
542 # see bug #5179: Popen leaks file descriptors to PIPEs if
556990bb4f0a #5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents: 49984
diff changeset
543 # the child fails to execute; this will eventually exhaust
556990bb4f0a #5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents: 49984
diff changeset
544 # the maximum number of open fds. 1024 seems a very common
556990bb4f0a #5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents: 49984
diff changeset
545 # value for that limit, but Windows has 2048, so we loop
556990bb4f0a #5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents: 49984
diff changeset
546 # 1024 times (each call leaked two fds).
556990bb4f0a #5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents: 49984
diff changeset
547 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
548 # 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
549 with self.assertRaises(EnvironmentError) as c:
51636
556990bb4f0a #5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents: 49984
diff changeset
550 subprocess.Popen(['nonexisting_i_hope'],
556990bb4f0a #5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents: 49984
diff changeset
551 stdout=subprocess.PIPE,
556990bb4f0a #5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents: 49984
diff changeset
552 stderr=subprocess.PIPE)
64809
4aa2a20ac335 Merged revisions 84878 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63958
diff changeset
553 if c.exception.errno != errno.ENOENT: # ignore "no such file"
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
554 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
555
63773
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
556 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
557 # 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
558 # 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
559 ifhandle, ifname = mkstemp()
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
560 ofhandle, ofname = mkstemp()
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
561 efhandle, efname = mkstemp()
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
562 try:
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
563 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
564 stderr=efhandle)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
565 except OSError:
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
566 os.close(ifhandle)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
567 os.remove(ifname)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
568 os.close(ofhandle)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
569 os.remove(ofname)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
570 os.close(efhandle)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
571 os.remove(efname)
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
572 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
573 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
574 self.assertFalse(os.path.exists(efname))
2c10510a1aef Issue #3210: Ensure stdio handles are closed if CreateProcess fails
Tim Golden <mail@timgolden.me.uk>
parents: 63152
diff changeset
575
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
576
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
577 # 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
578 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
579 """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
580 old_limit = None
51636
556990bb4f0a #5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents: 49984
diff changeset
581
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
582 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
583 """Try to save previous ulimit, then set it to (0, 0)."""
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
584 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
585 import resource
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
586 self.old_limit = resource.getrlimit(resource.RLIMIT_CORE)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
587 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
588 except (ImportError, ValueError, resource.error):
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 pass
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
590
63152
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
591 if sys.platform == 'darwin':
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
592 # 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
593 # 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
594 # when it is.
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
595 #
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
596 # 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
597 # that might trigger the next manager.
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
598 value = subprocess.Popen(['/usr/bin/defaults', 'read',
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
599 'com.apple.CrashReporter', 'DialogType'],
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
600 stdout=subprocess.PIPE).communicate()[0]
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
601 if value.strip() == b'developer':
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
602 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
603 sys.stdout.flush()
996ee9e03816 Merged revisions 83067 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 63096
diff changeset
604
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
605 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
606 """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
607 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
608 return
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
609 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
610 import resource
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
611 resource.setrlimit(resource.RLIMIT_CORE, self.old_limit)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
612 except (ImportError, ValueError, resource.error):
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
613 pass
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
614
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
615
59338
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
616 @unittest.skipIf(mswindows, "POSIX specific tests")
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
617 class POSIXProcessTestCase(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
618
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
619 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
620 # 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
621 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
622 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
623 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
624 # 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
625 self.assertIn("os.chdir", c.exception.child_traceback)
39346
bd50a42b7df0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39341
diff changeset
626
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
627 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
628 # 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
629 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
630 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
631 "import os; os.abort()"])
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
632 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
633 self.assertEqual(-p.returncode, signal.SIGABRT)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
634
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
635 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
636 # 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
637 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
638 "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
639 "sys.stdout.write(os.getenv('FRUIT'))"],
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
640 stdout=subprocess.PIPE,
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
641 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
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
642 self.assertEqual(p.stdout.read(), "apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
643
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
644 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
645 # 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
646 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
647 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
648 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
649 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
650 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
651 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
652 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
653 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
654 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
655 self.assertEqual(p.returncode, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
656
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
657 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
658 # 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
659 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
660 [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
661 "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
662 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
663 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
664 [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
665 "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
666 creationflags=47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
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 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
669 # 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
670 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
671 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
672 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
673 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
674 env=newenv)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
675 self.assertEqual(p.stdout.read().strip(), "apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
676
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
677 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
678 # 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
679 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
680 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
681 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
682 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
683 env=newenv)
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
684 self.assertEqual(p.stdout.read().strip(), "apple")
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
685
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
686 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
687 # 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
688 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
689 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
690 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
691 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
692 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
693 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
694 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
695 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
696 self.assertEqual(rc, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
697
63096
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
698 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
699 # 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
700 shells = []
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
701 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
702 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
703 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
704 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
705 shells.append(sh)
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
706 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
707 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
708 sh = '/bin/sh'
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
709 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
710 # 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
711 shells.append(sh)
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
712 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
713 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
714 stdout=subprocess.PIPE)
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
715 self.assertEqual(p.stdout.read().strip(), sh)
354546846c29 Issue #9265: Incorrect name passed as arg[0] when shell=True
Stefan Krah <stefan@bytereef.org>
parents: 62401
diff changeset
716
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
717 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
718 # 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
719 # 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
720 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
721 import sys, time
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
722 sys.stdout.write('x\\n')
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
723 sys.stdout.flush()
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
724 time.sleep(30)
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
725 """],
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
726 close_fds=True,
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
727 stdin=subprocess.PIPE,
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
728 stdout=subprocess.PIPE,
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
729 stderr=subprocess.PIPE)
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
730 # 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
731 # sending any signal.
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
732 p.stdout.read(1)
6bd276f5d1bc Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64815
diff changeset
733 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
734 return p
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
735
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
736 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
737 p = self._kill_process('send_signal', signal.SIGINT)
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
738 _, 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
739 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
740 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
741
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 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
743 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
744 _, 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
745 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
746 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
747
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
748 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
749 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
750 _, 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
751 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
752 self.assertEqual(p.wait(), -signal.SIGTERM)
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
753
33731
53c468d0955b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33723
diff changeset
754
59338
ce8be64b34a8 Cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59215
diff changeset
755 @unittest.skipUnless(mswindows, "Windows specific tests")
59955
b214ca8eb6bd Silence test_subprocess.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59506
diff changeset
756 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
757
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
758 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
759 # 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
760 # 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
761 # 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
762 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
763 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
764 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
765 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
766 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
767 # 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
768 # 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
769 # 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
770 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
771 startupinfo=startupinfo)
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
772
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
773 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
774 # 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
775 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
776 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
777 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
778 ' -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
779 creationflags=CREATE_NEW_CONSOLE)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
780
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
781 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
782 # 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
783 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
784 [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
785 "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
786 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
787 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
788 [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
789 "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
790 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
791 close_fds=True)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
792
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
793 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
794 # 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
795 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
796 "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
797 close_fds=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
798 self.assertEqual(rc, 47)
41886
ec3b8e94a355 Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents: 40619
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_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
801 # 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
802 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
803 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
804 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
805 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
806 env=newenv)
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 self.assertIn("physalis", p.stdout.read())
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
808
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
809 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
810 # 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
811 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
812 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
813 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
814 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
815 env=newenv)
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 self.assertIn("physalis", p.stdout.read())
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
817
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
818 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
819 # 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
820 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
821 ' -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
822 self.assertEqual(rc, 47)
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
823
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
824 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
825 # 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
826 p = subprocess.Popen([sys.executable, "-c", """if 1:
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
827 import sys, time
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
828 sys.stdout.write('x\\n')
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
829 sys.stdout.flush()
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
830 time.sleep(30)
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
831 """],
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
832 stdin=subprocess.PIPE,
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
833 stdout=subprocess.PIPE,
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
834 stderr=subprocess.PIPE)
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
835 # 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
836 # sending any signal.
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
837 p.stdout.read(1)
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
838 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
839 _, 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
840 self.assertStderrEqual(stderr, '')
64928
8cc4be648ab4 Merged revisions 84997 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64854
diff changeset
841 returncode = p.wait()
59463
a3f6178354f5 Replace the stderr logging with assertNotEqual(returncode, 0).
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59441
diff changeset
842 self.assertNotEqual(returncode, 0)
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
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_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
845 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
846
59191
679f52b8aef4 Clean test_subprocess: use assertRaises, skipIf, skipUnless helpers and a custom helper assertStderrEqual.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58856
diff changeset
847 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
848 self._kill_process('kill')
46479
e16593da4898 Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents: 44801
diff changeset
849
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
850 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
851 self._kill_process('terminate')
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
852
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
853
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
854 @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
855 "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
856 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
857 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
858 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
859 ProcessTestCase.setUp(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
860
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
861 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
862 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
863 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
864
9808e38fde72 Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
865
59206
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
866 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
867 @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
868 def test_eintr_retry_call(self):
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
869 record_calls = []
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
870 def fake_os_func(*args):
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
871 record_calls.append(args)
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
872 if len(record_calls) == 2:
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
873 raise OSError(errno.EINTR, "fake interrupted system call")
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
874 return tuple(reversed(args))
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
875
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
876 self.assertEqual((999, 256),
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
877 subprocess._eintr_retry_call(fake_os_func, 256, 999))
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
878 self.assertEqual([(256, 999)], record_calls)
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
879 # This time there will be an EINTR so it will loop once.
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
880 self.assertEqual((666,),
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
881 subprocess._eintr_retry_call(fake_os_func, 666))
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
882 self.assertEqual([(256, 999), (666,), (666,)], record_calls)
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
883
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
884
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
885 @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
886 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
887
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
888 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
889 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
890 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
891 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
892 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
893 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
894 )
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
895 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
896
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
897 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
898 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
899 super(CommandsWithSpaces, self).tearDown()
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
900
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
901 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
902 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
903 p = subprocess.Popen(*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
904 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
905 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
906 "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
907 )
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
908
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
909 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
910 # 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
911 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
912 "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
913
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
914 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
915 # 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
916 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
917
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
918 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
919 # 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
920 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
921 "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
922
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
923 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
924 # 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
925 self.with_spaces([sys.executable, self.fname, "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
926
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
927 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
928 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
929 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
930 Win32ProcessTestCase,
59206
6e664bcc958d Issue #1068268: The subprocess module now handles EINTR in internal
Gregory P. Smith <greg@mad-scientist.com>
parents: 59192
diff changeset
931 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
932 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
933 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
934
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
935 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
936 test_support.reap_children()
33723
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
937
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
938 if __name__ == "__main__":
76641824cf05 Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
939 test_main()