Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_subprocess.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 17891566a478 |
| children | d1e2f794d020 |
| 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 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
7 import tempfile |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
8 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
|
9 import re |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
10 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
11 mswindows = (sys.platform == "win32") |
|
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 # |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
14 # Depends on the following external programs: Python |
|
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 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
17 if mswindows: |
| 33732 | 18 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), ' |
| 19 'os.O_BINARY);') | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
20 else: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
21 SETBINARY = '' |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
22 |
|
33739
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
23 # In a debug build, stuff like "[6580 refs]" is printed to stderr at |
|
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
24 # shutdown time. That frustrates tests trying to check stderr produced |
|
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
25 # from a spawned Python process. |
|
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
26 def remove_stderr_debug_decorations(stderr): |
|
33757
8a5061bcd417
remove_stderr_debug_decorations(): Always try the substitution. Else
Tim Peters <tim.peters@gmail.com>
parents:
33743
diff
changeset
|
27 return re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr) |
|
33739
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
28 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
29 class ProcessTestCase(unittest.TestCase): |
|
38961
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38916
diff
changeset
|
30 def setUp(self): |
|
38967
a21e420a597c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38961
diff
changeset
|
31 # 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
|
32 # doesn't crash on some buildbots (Alphas in particular). |
|
39055
3b38ab9c4b03
Make it possible to run test_subprocess.py with Python 2.2, which lacks test_support.reap_children().
Peter Astrand <astrand@lysator.liu.se>
parents:
38967
diff
changeset
|
33 if hasattr(test_support, "reap_children"): |
|
3b38ab9c4b03
Make it possible to run test_subprocess.py with Python 2.2, which lacks test_support.reap_children().
Peter Astrand <astrand@lysator.liu.se>
parents:
38967
diff
changeset
|
34 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
|
35 |
|
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38916
diff
changeset
|
36 def tearDown(self): |
|
38967
a21e420a597c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38961
diff
changeset
|
37 # 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
|
38 # doesn't crash on some buildbots (Alphas in particular). |
|
39055
3b38ab9c4b03
Make it possible to run test_subprocess.py with Python 2.2, which lacks test_support.reap_children().
Peter Astrand <astrand@lysator.liu.se>
parents:
38967
diff
changeset
|
39 if hasattr(test_support, "reap_children"): |
|
3b38ab9c4b03
Make it possible to run test_subprocess.py with Python 2.2, which lacks test_support.reap_children().
Peter Astrand <astrand@lysator.liu.se>
parents:
38967
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 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
42 def mkstemp(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
43 """wrapper for mkstemp, calling mktemp if mkstemp is not available""" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
44 if hasattr(tempfile, "mkstemp"): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
45 return tempfile.mkstemp() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
46 else: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
47 fname = tempfile.mktemp() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
48 return os.open(fname, os.O_RDWR|os.O_CREAT), fname |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
49 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
50 # |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
51 # Generic tests |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
52 # |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
53 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
|
54 # call() function with sequence argument |
| 33732 | 55 rc = subprocess.call([sys.executable, "-c", |
| 56 "import sys; sys.exit(47)"]) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
57 self.assertEqual(rc, 47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
58 |
|
34302
998fb1e1c8e9
New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents:
34052
diff
changeset
|
59 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
|
60 # 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
|
61 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
|
62 "import sys; sys.exit(0)"]) |
|
998fb1e1c8e9
New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents:
34052
diff
changeset
|
63 self.assertEqual(rc, 0) |
|
998fb1e1c8e9
New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents:
34052
diff
changeset
|
64 |
|
998fb1e1c8e9
New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents:
34052
diff
changeset
|
65 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
|
66 # check_call() function with non-zero return code |
|
998fb1e1c8e9
New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents:
34052
diff
changeset
|
67 try: |
|
998fb1e1c8e9
New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents:
34052
diff
changeset
|
68 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
|
69 "import sys; sys.exit(47)"]) |
|
998fb1e1c8e9
New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents:
34052
diff
changeset
|
70 except subprocess.CalledProcessError, e: |
|
39084
9f2c3de74856
Bug #1223937: CalledProcessError.errno -> CalledProcessError.returncode.
Peter Astrand <astrand@lysator.liu.se>
parents:
39055
diff
changeset
|
71 self.assertEqual(e.returncode, 47) |
|
34302
998fb1e1c8e9
New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents:
34052
diff
changeset
|
72 else: |
|
998fb1e1c8e9
New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents:
34052
diff
changeset
|
73 self.fail("Expected CalledProcessError") |
|
998fb1e1c8e9
New subprocess utility function: check_call. Closes #1071764.
Peter Astrand <astrand@lysator.liu.se>
parents:
34052
diff
changeset
|
74 |
|
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
|
75 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
|
76 # 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
|
77 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
|
78 [sys.executable, "-c", "print 'BDFL'"]) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55178
diff
changeset
|
79 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
|
80 |
|
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
|
81 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
|
82 # check_call() function with non-zero return code |
|
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
|
83 try: |
|
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
|
84 subprocess.check_output( |
|
49977
4790b31d9257
Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents:
47338
diff
changeset
|
85 [sys.executable, "-c", "import sys; sys.exit(5)"]) |
|
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
|
86 except subprocess.CalledProcessError, e: |
|
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 self.assertEqual(e.returncode, 5) |
|
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
|
88 else: |
|
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 self.fail("Expected CalledProcessError") |
|
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
|
90 |
|
49984
589cde614b5b
rename the new check_call_output to check_output. its less ugly.
Gregory P. Smith <greg@mad-scientist.com>
parents:
49977
diff
changeset
|
91 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
|
92 # 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
|
93 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
|
94 [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
|
95 stderr=subprocess.STDOUT) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55178
diff
changeset
|
96 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
|
97 |
|
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
|
98 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
|
99 # check_output() function stderr redirected to stdout |
|
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
|
100 try: |
|
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
|
101 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
|
102 [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
|
103 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
|
104 except ValueError, e: |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55178
diff
changeset
|
105 self.assertIn('stdout', e.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
|
106 else: |
|
4790b31d9257
Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents:
47338
diff
changeset
|
107 self.fail("Expected ValueError when stdout arg supplied.") |
|
4790b31d9257
Adds a subprocess.check_call_output() function to return the output from a
Gregory P. Smith <greg@mad-scientist.com>
parents:
47338
diff
changeset
|
108 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
109 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
|
110 # call() function with keyword args |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
111 newenv = os.environ.copy() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
112 newenv["FRUIT"] = "banana" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
113 rc = subprocess.call([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
114 'import sys, os;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
115 'sys.exit(os.getenv("FRUIT")=="banana")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
116 env=newenv) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
117 self.assertEqual(rc, 1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
118 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
119 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
|
120 # .stdin is None when not redirected |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
121 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
122 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
123 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
124 self.assertEqual(p.stdin, None) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
125 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
126 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
|
127 # .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
|
128 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
|
129 '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
|
130 '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
|
131 '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
|
132 stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
133 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
134 self.assertEqual(p.stdout, None) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
135 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
136 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
|
137 # .stderr is None when not redirected |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
138 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
139 stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
140 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
141 self.assertEqual(p.stderr, None) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
142 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
143 def test_executable(self): |
| 33732 | 144 p = subprocess.Popen(["somethingyoudonthave", |
| 145 "-c", "import sys; sys.exit(47)"], | |
| 146 executable=sys.executable) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
147 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
148 self.assertEqual(p.returncode, 47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
149 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
150 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
|
151 # stdin redirection |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
152 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
153 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
154 stdin=subprocess.PIPE) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
155 p.stdin.write("pear") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
156 p.stdin.close() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
157 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
158 self.assertEqual(p.returncode, 1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
159 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
160 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
|
161 # stdin is set to open file descriptor |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
162 tf = tempfile.TemporaryFile() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
163 d = tf.fileno() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
164 os.write(d, "pear") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
165 os.lseek(d, 0, 0) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
166 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
167 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
168 stdin=d) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
169 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
170 self.assertEqual(p.returncode, 1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
171 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
172 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
|
173 # stdin is set to open file object |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
174 tf = tempfile.TemporaryFile() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
175 tf.write("pear") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
176 tf.seek(0) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
177 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
178 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
179 stdin=tf) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
180 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
181 self.assertEqual(p.returncode, 1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
182 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
183 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
|
184 # stdout redirection |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
185 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
186 'import sys; sys.stdout.write("orange")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
187 stdout=subprocess.PIPE) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
188 self.assertEqual(p.stdout.read(), "orange") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
189 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
190 def test_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
|
191 # stdout is set to open file descriptor |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
192 tf = tempfile.TemporaryFile() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
193 d = tf.fileno() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
194 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=d) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
197 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
198 os.lseek(d, 0, 0) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
199 self.assertEqual(os.read(d, 1024), "orange") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
200 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
201 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
|
202 # stdout is set to open file object |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
203 tf = tempfile.TemporaryFile() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
204 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
205 'import sys; sys.stdout.write("orange")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
206 stdout=tf) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
207 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
208 tf.seek(0) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
209 self.assertEqual(tf.read(), "orange") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
210 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
211 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
|
212 # stderr redirection |
|
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.stderr.write("strawberry")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
215 stderr=subprocess.PIPE) |
|
33739
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
216 self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()), |
|
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
217 "strawberry") |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
218 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
219 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
|
220 # stderr is set to open file descriptor |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
221 tf = tempfile.TemporaryFile() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
222 d = tf.fileno() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
223 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
224 'import sys; sys.stderr.write("strawberry")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
225 stderr=d) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
226 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
227 os.lseek(d, 0, 0) |
|
33739
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
228 self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)), |
|
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
229 "strawberry") |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
230 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
231 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
|
232 # stderr is set to open file object |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
233 tf = tempfile.TemporaryFile() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
234 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
235 'import sys; sys.stderr.write("strawberry")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
236 stderr=tf) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
237 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
238 tf.seek(0) |
|
33739
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
239 self.assertEqual(remove_stderr_debug_decorations(tf.read()), |
|
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
240 "strawberry") |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
241 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
242 def test_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
|
243 # capture stdout and stderr to the same pipe |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
244 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
245 'import sys;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
246 'sys.stdout.write("apple");' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
247 'sys.stdout.flush();' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
248 'sys.stderr.write("orange")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
249 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
250 stderr=subprocess.STDOUT) |
|
33739
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
251 output = p.stdout.read() |
|
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
252 stripped = remove_stderr_debug_decorations(output) |
|
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
253 self.assertEqual(stripped, "appleorange") |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
254 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
255 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
|
256 # 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
|
257 tf = tempfile.TemporaryFile() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
258 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
259 'import sys;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
260 'sys.stdout.write("apple");' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
261 'sys.stdout.flush();' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
262 'sys.stderr.write("orange")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
263 stdout=tf, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
264 stderr=tf) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
265 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
266 tf.seek(0) |
|
33739
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
267 output = tf.read() |
|
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
268 stripped = remove_stderr_debug_decorations(output) |
|
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
269 self.assertEqual(stripped, "appleorange") |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
270 |
|
39793
363ef804a096
Fixed subprocess bug #1531862 again, after removing tests
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
39772
diff
changeset
|
271 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
|
272 # stdout is set to 1 (#1531862). |
|
363ef804a096
Fixed subprocess bug #1531862 again, after removing tests
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
39772
diff
changeset
|
273 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
|
274 rc = subprocess.call([sys.executable, "-c", cmd], stdout=1) |
|
363ef804a096
Fixed subprocess bug #1531862 again, after removing tests
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
39772
diff
changeset
|
275 self.assertEquals(rc, 2) |
|
363ef804a096
Fixed subprocess bug #1531862 again, after removing tests
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
39772
diff
changeset
|
276 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
277 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
|
278 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
|
279 # 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
|
280 # 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
|
281 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
|
282 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
|
283 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
|
284 os.chdir(cwd) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
285 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
286 'import sys,os;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
287 'sys.stdout.write(os.getcwd())'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
288 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
289 cwd=tmpdir) |
|
33742
179e42ea2e9c
normalize case when comparing directory names (problem reported by
Fredrik Lundh <fredrik@pythonware.com>
parents:
33739
diff
changeset
|
290 normcase = os.path.normcase |
|
179e42ea2e9c
normalize case when comparing directory names (problem reported by
Fredrik Lundh <fredrik@pythonware.com>
parents:
33739
diff
changeset
|
291 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir)) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
292 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
293 def test_env(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
294 newenv = os.environ.copy() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
295 newenv["FRUIT"] = "orange" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
296 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
297 'import sys,os;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
298 'sys.stdout.write(os.getenv("FRUIT"))'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
299 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
300 env=newenv) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
301 self.assertEqual(p.stdout.read(), "orange") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
302 |
|
34671
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
303 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
|
304 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
|
305 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
306 stdin=subprocess.PIPE) |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
307 p.communicate("pear") |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
308 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
|
309 |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
310 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
|
311 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
|
312 '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
|
313 stdout=subprocess.PIPE) |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
314 (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
|
315 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
|
316 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
|
317 |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
318 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
|
319 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
|
320 '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
|
321 stderr=subprocess.PIPE) |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
322 (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
|
323 self.assertEqual(stdout, None) |
|
47338
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
324 self.assertEqual(remove_stderr_debug_decorations(stderr), "pineapple") |
|
34671
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
325 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
326 def test_communicate(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
327 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
|
328 '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
|
329 'sys.stderr.write("pineapple");' |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
330 'sys.stdout.write(sys.stdin.read())'], |
| 33732 | 331 stdin=subprocess.PIPE, |
| 332 stdout=subprocess.PIPE, | |
| 333 stderr=subprocess.PIPE) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
334 (stdout, stderr) = p.communicate("banana") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
335 self.assertEqual(stdout, "banana") |
|
33739
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
336 self.assertEqual(remove_stderr_debug_decorations(stderr), |
|
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
337 "pineapple") |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
338 |
|
47338
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
339 # 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
|
340 # some coverage. It is not a platform specific bug. |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
341 if os.path.isdir('/proc/%d/fd' % os.getpid()): |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
342 # Test for the fd leak reported in http://bugs.python.org/issue2791. |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
343 def test_communicate_pipe_fd_leak(self): |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
344 fd_directory = '/proc/%d/fd' % os.getpid() |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
345 num_fds_before_popen = len(os.listdir(fd_directory)) |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
346 p = subprocess.Popen([sys.executable, '-c', 'print()'], |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
347 stdout=subprocess.PIPE) |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
348 p.communicate() |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
349 num_fds_after_communicate = len(os.listdir(fd_directory)) |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
350 del p |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
351 num_fds_after_destruction = len(os.listdir(fd_directory)) |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
352 self.assertEqual(num_fds_before_popen, num_fds_after_destruction) |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
353 self.assertEqual(num_fds_before_popen, num_fds_after_communicate) |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
354 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
355 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
|
356 # communicate() should return None if no redirection is active |
| 33732 | 357 p = subprocess.Popen([sys.executable, "-c", |
| 358 "import sys; sys.exit(47)"]) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
359 (stdout, stderr) = p.communicate() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
360 self.assertEqual(stdout, None) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
361 self.assertEqual(stderr, None) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
362 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
363 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
|
364 # communicate() with writes larger than pipe_buf |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
365 # This test will probably deadlock rather than fail, if |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
366 # communicate() does not work properly. |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
367 x, y = os.pipe() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
368 if mswindows: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
369 pipe_buf = 512 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
370 else: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
371 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
372 os.close(x) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
373 os.close(y) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
374 p = subprocess.Popen([sys.executable, "-c", |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
375 'import sys,os;' |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
376 'sys.stdout.write(sys.stdin.read(47));' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
377 'sys.stderr.write("xyz"*%d);' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
378 'sys.stdout.write(sys.stdin.read())' % pipe_buf], |
| 33732 | 379 stdin=subprocess.PIPE, |
| 380 stdout=subprocess.PIPE, | |
| 381 stderr=subprocess.PIPE) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
382 string_to_write = "abc"*pipe_buf |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
383 (stdout, stderr) = p.communicate(string_to_write) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
384 self.assertEqual(stdout, string_to_write) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
385 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
386 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
|
387 # stdin.write before communicate() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
388 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
389 'import sys,os;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
390 'sys.stdout.write(sys.stdin.read())'], |
| 33732 | 391 stdin=subprocess.PIPE, |
| 392 stdout=subprocess.PIPE, | |
| 393 stderr=subprocess.PIPE) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
394 p.stdin.write("banana") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
395 (stdout, stderr) = p.communicate("split") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
396 self.assertEqual(stdout, "bananasplit") |
|
33739
76355363101b
New helper remove_stderr_debug_decorations(). This test passes in a
Tim Peters <tim.peters@gmail.com>
parents:
33738
diff
changeset
|
397 self.assertEqual(remove_stderr_debug_decorations(stderr), "") |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
398 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
399 def test_universal_newlines(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
400 p = subprocess.Popen([sys.executable, "-c", |
| 33732 | 401 'import sys,os;' + SETBINARY + |
| 402 'sys.stdout.write("line1\\n");' | |
| 403 'sys.stdout.flush();' | |
| 404 'sys.stdout.write("line2\\r");' | |
| 405 'sys.stdout.flush();' | |
| 406 'sys.stdout.write("line3\\r\\n");' | |
| 407 'sys.stdout.flush();' | |
| 408 'sys.stdout.write("line4\\r");' | |
| 409 'sys.stdout.flush();' | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
410 'sys.stdout.write("\\nline5");' |
| 33732 | 411 'sys.stdout.flush();' |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
412 'sys.stdout.write("\\nline6");'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
413 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
414 universal_newlines=1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
415 stdout = p.stdout.read() |
|
38078
b1cbc605bec9
Try to fix breakage caused by patch #1479181, r45850
Neal Norwitz <nnorwitz@gmail.com>
parents:
38077
diff
changeset
|
416 if hasattr(file, 'newlines'): |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
417 # Interpreter with universal newline support |
| 33732 | 418 self.assertEqual(stdout, |
| 419 "line1\nline2\nline3\nline4\nline5\nline6") | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
420 else: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
421 # Interpreter without universal newline support |
| 33732 | 422 self.assertEqual(stdout, |
| 423 "line1\nline2\rline3\r\nline4\r\nline5\nline6") | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
424 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
425 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
|
426 # universal newlines through communicate() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
427 p = subprocess.Popen([sys.executable, "-c", |
| 33732 | 428 'import sys,os;' + SETBINARY + |
| 429 'sys.stdout.write("line1\\n");' | |
| 430 'sys.stdout.flush();' | |
| 431 'sys.stdout.write("line2\\r");' | |
| 432 'sys.stdout.flush();' | |
| 433 'sys.stdout.write("line3\\r\\n");' | |
| 434 'sys.stdout.flush();' | |
| 435 'sys.stdout.write("line4\\r");' | |
| 436 'sys.stdout.flush();' | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
437 'sys.stdout.write("\\nline5");' |
| 33732 | 438 'sys.stdout.flush();' |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
439 'sys.stdout.write("\\nline6");'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
440 stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
441 universal_newlines=1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
442 (stdout, stderr) = p.communicate() |
|
38078
b1cbc605bec9
Try to fix breakage caused by patch #1479181, r45850
Neal Norwitz <nnorwitz@gmail.com>
parents:
38077
diff
changeset
|
443 if hasattr(file, 'newlines'): |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
444 # Interpreter with universal newline support |
| 33732 | 445 self.assertEqual(stdout, |
| 446 "line1\nline2\nline3\nline4\nline5\nline6") | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
447 else: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
448 # Interpreter without universal newline support |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
449 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
450 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
451 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
|
452 # Make sure we leak no resources |
|
38916
78816c5cff05
Make it possible to run test_subprocess.py on Python 2.2, which lacks test_support.is_resource_enabled.
Peter Astrand <astrand@lysator.liu.se>
parents:
38078
diff
changeset
|
453 if not hasattr(test_support, "is_resource_enabled") \ |
|
78816c5cff05
Make it possible to run test_subprocess.py on Python 2.2, which lacks test_support.is_resource_enabled.
Peter Astrand <astrand@lysator.liu.se>
parents:
38078
diff
changeset
|
454 or test_support.is_resource_enabled("subprocess") and 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
|
455 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
|
456 else: |
|
34818
4eaab38566c9
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34695
diff
changeset
|
457 max_handles = 65 |
|
33743
4683983e84b3
Don't spend quite as much time looking for leaks on Windows, where
Fredrik Lundh <fredrik@pythonware.com>
parents:
33742
diff
changeset
|
458 for i in range(max_handles): |
| 33732 | 459 p = subprocess.Popen([sys.executable, "-c", |
| 460 "import sys;sys.stdout.write(sys.stdin.read())"], | |
| 461 stdin=subprocess.PIPE, | |
| 462 stdout=subprocess.PIPE, | |
| 463 stderr=subprocess.PIPE) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
464 data = p.communicate("lime")[0] |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
465 self.assertEqual(data, "lime") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
466 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
467 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
468 def test_list2cmdline(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
469 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
470 '"a b c" d e') |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
471 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
472 '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
|
473 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
|
474 'ab\\"c " \\\\" d') |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
475 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']), |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
476 'a\\\\\\b "de fg" h') |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
477 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']), |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
478 'a\\\\\\"b c d') |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
479 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']), |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
480 '"a\\\\b c" d e') |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
481 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']), |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
482 '"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
|
483 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
|
484 'ab ""') |
|
44801
c23892f5a69f
Fix issue 1300: Quote command line arguments that contain a '|' character in
Gregory P. Smith <greg@mad-scientist.com>
parents:
44790
diff
changeset
|
485 self.assertEqual(subprocess.list2cmdline(['echo', 'foo|bar']), |
|
c23892f5a69f
Fix issue 1300: Quote command line arguments that contain a '|' character in
Gregory P. Smith <greg@mad-scientist.com>
parents:
44790
diff
changeset
|
486 'echo "foo|bar"') |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
487 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
488 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
489 def test_poll(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
490 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
|
491 "-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
|
492 count = 0 |
|
a4f747e72138
Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents:
33737
diff
changeset
|
493 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
|
494 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
|
495 count += 1 |
|
a4f747e72138
Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents:
33737
diff
changeset
|
496 # 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
|
497 # 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
|
498 # 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
|
499 # didn't go around at least twice. |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51636
diff
changeset
|
500 self.assertTrue(count >= 2) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
501 # Subsequent invocations should just return the returncode |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
502 self.assertEqual(p.poll(), 0) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
503 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
504 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
505 def test_wait(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
506 p = subprocess.Popen([sys.executable, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
507 "-c", "import time; time.sleep(2)"]) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
508 self.assertEqual(p.wait(), 0) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
509 # Subsequent invocations should just return the returncode |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
510 self.assertEqual(p.wait(), 0) |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
511 |
|
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
|
512 |
|
79d2ae0a62ef
Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents:
33998
diff
changeset
|
513 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
|
514 # 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
|
515 # TypeError. |
|
79d2ae0a62ef
Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents:
33998
diff
changeset
|
516 try: |
|
79d2ae0a62ef
Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents:
33998
diff
changeset
|
517 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
|
518 except TypeError: |
|
79d2ae0a62ef
Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents:
33998
diff
changeset
|
519 pass |
|
79d2ae0a62ef
Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents:
33998
diff
changeset
|
520 else: |
|
79d2ae0a62ef
Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents:
33998
diff
changeset
|
521 self.fail("Expected TypeError") |
|
79d2ae0a62ef
Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents:
33998
diff
changeset
|
522 |
|
51636
556990bb4f0a
#5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents:
49984
diff
changeset
|
523 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
|
524 # 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
|
525 # 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
|
526 # 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
|
527 # 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
|
528 # 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
|
529 for i in range(1024): |
|
556990bb4f0a
#5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents:
49984
diff
changeset
|
530 try: |
|
556990bb4f0a
#5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents:
49984
diff
changeset
|
531 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
|
532 stdout=subprocess.PIPE, |
|
556990bb4f0a
#5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents:
49984
diff
changeset
|
533 stderr=subprocess.PIPE) |
|
556990bb4f0a
#5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents:
49984
diff
changeset
|
534 # Windows raises IOError |
|
556990bb4f0a
#5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents:
49984
diff
changeset
|
535 except (IOError, OSError), err: |
|
556990bb4f0a
#5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents:
49984
diff
changeset
|
536 if err.errno != 2: # ignore "no such file" |
|
556990bb4f0a
#5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents:
49984
diff
changeset
|
537 raise |
|
556990bb4f0a
#5179: don't leak PIPE fds when child execution fails.
Georg Brandl <georg@python.org>
parents:
49984
diff
changeset
|
538 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
539 # |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
540 # POSIX tests |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
541 # |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
542 if not mswindows: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
543 def test_exceptions(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
|
544 # catched & re-raised exceptions |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
545 try: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
546 p = subprocess.Popen([sys.executable, "-c", ""], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
547 cwd="/this/path/does/not/exist") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
548 except OSError, e: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
549 # The attribute child_traceback should contain "os.chdir" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
550 # somewhere. |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
551 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
552 else: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
553 self.fail("Expected OSError") |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
554 |
|
39341
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
555 def _suppress_core_files(self): |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
556 """Try to prevent core files from being created. |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
557 Returns previous ulimit if successful, else None. |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
558 """ |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
559 try: |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
560 import resource |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
561 old_limit = resource.getrlimit(resource.RLIMIT_CORE) |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
562 resource.setrlimit(resource.RLIMIT_CORE, (0,0)) |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
563 return old_limit |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
564 except (ImportError, ValueError, resource.error): |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
565 return None |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
566 |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
567 def _unsuppress_core_files(self, old_limit): |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
568 """Return core file behavior to default.""" |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
569 if old_limit is None: |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
570 return |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
571 try: |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
572 import resource |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
573 resource.setrlimit(resource.RLIMIT_CORE, old_limit) |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
574 except (ImportError, ValueError, resource.error): |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
575 return |
|
39346
bd50a42b7df0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39341
diff
changeset
|
576 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
577 def test_run_abort(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
|
578 # returncode handles signal termination |
|
39341
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
579 old_limit = self._suppress_core_files() |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
580 try: |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
581 p = subprocess.Popen([sys.executable, |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
582 "-c", "import os; os.abort()"]) |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
583 finally: |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
584 self._unsuppress_core_files(old_limit) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
585 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
586 self.assertEqual(-p.returncode, signal.SIGABRT) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
587 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
588 def test_preexec(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
|
589 # preexec function |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
590 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
591 'import sys,os;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
592 'sys.stdout.write(os.getenv("FRUIT"))'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
593 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
594 preexec_fn=lambda: os.putenv("FRUIT", "apple")) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
595 self.assertEqual(p.stdout.read(), "apple") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
596 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
597 def test_args_string(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
|
598 # args is a string |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
599 f, fname = self.mkstemp() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
600 os.write(f, "#!/bin/sh\n") |
|
47119
6b7977314c02
fix issue2381: test_subprocess fails if your sys.executable is on a
Gregory P. Smith <greg@mad-scientist.com>
parents:
46706
diff
changeset
|
601 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 33732 | 602 sys.executable) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
603 os.close(f) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
604 os.chmod(fname, 0700) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
605 p = subprocess.Popen(fname) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
606 p.wait() |
|
33998
f5311303a357
Remove tempfile after use in test_call_string.
Peter Astrand <astrand@lysator.liu.se>
parents:
33980
diff
changeset
|
607 os.remove(fname) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
608 self.assertEqual(p.returncode, 47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
609 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
610 def test_invalid_args(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
|
611 # invalid arguments should raise ValueError |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
612 self.assertRaises(ValueError, subprocess.call, |
| 33732 | 613 [sys.executable, |
| 614 "-c", "import sys; sys.exit(47)"], | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
615 startupinfo=47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
616 self.assertRaises(ValueError, subprocess.call, |
| 33732 | 617 [sys.executable, |
| 618 "-c", "import sys; sys.exit(47)"], | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
619 creationflags=47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
620 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
621 def test_shell_sequence(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
|
622 # Run command through the shell (sequence) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
623 newenv = os.environ.copy() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
624 newenv["FRUIT"] = "apple" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
625 p = subprocess.Popen(["echo $FRUIT"], shell=1, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
626 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
627 env=newenv) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
628 self.assertEqual(p.stdout.read().strip(), "apple") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
629 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
630 def test_shell_string(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
|
631 # Run command through the shell (string) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
632 newenv = os.environ.copy() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
633 newenv["FRUIT"] = "apple" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
634 p = subprocess.Popen("echo $FRUIT", shell=1, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
635 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
636 env=newenv) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
637 self.assertEqual(p.stdout.read().strip(), "apple") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
638 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
639 def test_call_string(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
|
640 # call() function with string argument on UNIX |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
641 f, fname = self.mkstemp() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
642 os.write(f, "#!/bin/sh\n") |
|
47119
6b7977314c02
fix issue2381: test_subprocess fails if your sys.executable is on a
Gregory P. Smith <greg@mad-scientist.com>
parents:
46706
diff
changeset
|
643 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 33732 | 644 sys.executable) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
645 os.close(f) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
646 os.chmod(fname, 0700) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
647 rc = subprocess.call(fname) |
|
33998
f5311303a357
Remove tempfile after use in test_call_string.
Peter Astrand <astrand@lysator.liu.se>
parents:
33980
diff
changeset
|
648 os.remove(fname) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
649 self.assertEqual(rc, 47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
650 |
|
46706
c8aa220c6cee
Disabled some unit tests for the upcoming release. See #2777
Christian Heimes <christian@cheimes.de>
parents:
46479
diff
changeset
|
651 def DISABLED_test_send_signal(self): |
|
46479
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
652 p = subprocess.Popen([sys.executable, |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
653 "-c", "input()"]) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
654 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51636
diff
changeset
|
655 self.assertTrue(p.poll() is None, p.poll()) |
|
46479
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
656 p.send_signal(signal.SIGINT) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
657 self.assertNotEqual(p.wait(), 0) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
658 |
|
46706
c8aa220c6cee
Disabled some unit tests for the upcoming release. See #2777
Christian Heimes <christian@cheimes.de>
parents:
46479
diff
changeset
|
659 def DISABLED_test_kill(self): |
|
46479
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
660 p = subprocess.Popen([sys.executable, |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
661 "-c", "input()"]) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
662 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51636
diff
changeset
|
663 self.assertTrue(p.poll() is None, p.poll()) |
|
46479
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
664 p.kill() |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
665 self.assertEqual(p.wait(), -signal.SIGKILL) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
666 |
|
46706
c8aa220c6cee
Disabled some unit tests for the upcoming release. See #2777
Christian Heimes <christian@cheimes.de>
parents:
46479
diff
changeset
|
667 def DISABLED_test_terminate(self): |
|
46479
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
668 p = subprocess.Popen([sys.executable, |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
669 "-c", "input()"]) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
670 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51636
diff
changeset
|
671 self.assertTrue(p.poll() is None, p.poll()) |
|
46479
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
672 p.terminate() |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
673 self.assertEqual(p.wait(), -signal.SIGTERM) |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
674 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
675 # |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
676 # Windows tests |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
677 # |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
678 if mswindows: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
679 def test_startupinfo(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
|
680 # startupinfo argument |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
681 # We uses hardcoded constants, because we do not want to |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
682 # depend on win32all. |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
683 STARTF_USESHOWWINDOW = 1 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
684 SW_MAXIMIZE = 3 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
685 startupinfo = subprocess.STARTUPINFO() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
686 startupinfo.dwFlags = STARTF_USESHOWWINDOW |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
687 startupinfo.wShowWindow = SW_MAXIMIZE |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
688 # Since Python is a console process, it won't be affected |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
689 # by wShowWindow, but the argument should be silently |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
690 # ignored |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
691 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
692 startupinfo=startupinfo) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
693 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
694 def test_creationflags(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
|
695 # creationflags argument |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
696 CREATE_NEW_CONSOLE = 16 |
|
33736
a804280e34f9
Windows test_creationflags() test: print msg to stderr informing the
Tim Peters <tim.peters@gmail.com>
parents:
33734
diff
changeset
|
697 sys.stderr.write(" a DOS box should flash briefly ...\n") |
| 33732 | 698 subprocess.call(sys.executable + |
|
33736
a804280e34f9
Windows test_creationflags() test: print msg to stderr informing the
Tim Peters <tim.peters@gmail.com>
parents:
33734
diff
changeset
|
699 ' -c "import time; time.sleep(0.25)"', |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
700 creationflags=CREATE_NEW_CONSOLE) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
701 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
702 def test_invalid_args(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
|
703 # invalid arguments should raise ValueError |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
704 self.assertRaises(ValueError, subprocess.call, |
| 33732 | 705 [sys.executable, |
| 706 "-c", "import sys; sys.exit(47)"], | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
707 preexec_fn=lambda: 1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
708 self.assertRaises(ValueError, subprocess.call, |
| 33732 | 709 [sys.executable, |
| 710 "-c", "import sys; sys.exit(47)"], | |
|
41886
ec3b8e94a355
Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents:
40619
diff
changeset
|
711 stdout=subprocess.PIPE, |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
712 close_fds=True) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
713 |
|
41886
ec3b8e94a355
Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents:
40619
diff
changeset
|
714 def test_close_fds(self): |
|
ec3b8e94a355
Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents:
40619
diff
changeset
|
715 # close file descriptors |
|
ec3b8e94a355
Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents:
40619
diff
changeset
|
716 rc = subprocess.call([sys.executable, "-c", |
|
ec3b8e94a355
Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents:
40619
diff
changeset
|
717 "import sys; sys.exit(47)"], |
|
ec3b8e94a355
Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents:
40619
diff
changeset
|
718 close_fds=True) |
|
ec3b8e94a355
Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents:
40619
diff
changeset
|
719 self.assertEqual(rc, 47) |
|
ec3b8e94a355
Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents:
40619
diff
changeset
|
720 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
721 def test_shell_sequence(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
|
722 # Run command through the shell (sequence) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
723 newenv = os.environ.copy() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
724 newenv["FRUIT"] = "physalis" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
725 p = subprocess.Popen(["set"], shell=1, |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
726 stdout=subprocess.PIPE, |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
727 env=newenv) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
728 self.assertNotEqual(p.stdout.read().find("physalis"), -1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
729 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
730 def test_shell_string(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
|
731 # Run command through the shell (string) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
732 newenv = os.environ.copy() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
733 newenv["FRUIT"] = "physalis" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
734 p = subprocess.Popen("set", shell=1, |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
735 stdout=subprocess.PIPE, |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
736 env=newenv) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
737 self.assertNotEqual(p.stdout.read().find("physalis"), -1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
738 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
739 def test_call_string(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
|
740 # call() function with string argument on Windows |
| 33732 | 741 rc = subprocess.call(sys.executable + |
| 742 ' -c "import sys; sys.exit(47)"') | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
743 self.assertEqual(rc, 47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
744 |
|
46706
c8aa220c6cee
Disabled some unit tests for the upcoming release. See #2777
Christian Heimes <christian@cheimes.de>
parents:
46479
diff
changeset
|
745 def DISABLED_test_send_signal(self): |
|
46479
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
746 p = subprocess.Popen([sys.executable, |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
747 "-c", "input()"]) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
748 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51636
diff
changeset
|
749 self.assertTrue(p.poll() is None, p.poll()) |
|
46479
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
750 p.send_signal(signal.SIGTERM) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
751 self.assertNotEqual(p.wait(), 0) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
752 |
|
46706
c8aa220c6cee
Disabled some unit tests for the upcoming release. See #2777
Christian Heimes <christian@cheimes.de>
parents:
46479
diff
changeset
|
753 def DISABLED_test_kill(self): |
|
46479
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
754 p = subprocess.Popen([sys.executable, |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
755 "-c", "input()"]) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
756 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51636
diff
changeset
|
757 self.assertTrue(p.poll() is None, p.poll()) |
|
46479
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
758 p.kill() |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
759 self.assertNotEqual(p.wait(), 0) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
760 |
|
46706
c8aa220c6cee
Disabled some unit tests for the upcoming release. See #2777
Christian Heimes <christian@cheimes.de>
parents:
46479
diff
changeset
|
761 def DISABLED_test_terminate(self): |
|
46479
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
762 p = subprocess.Popen([sys.executable, |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
763 "-c", "input()"]) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
764 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51636
diff
changeset
|
765 self.assertTrue(p.poll() is None, p.poll()) |
|
46479
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
766 p.terminate() |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
767 self.assertNotEqual(p.wait(), 0) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
768 |
|
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
|
769 |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
770 unit_tests = [ProcessTestCase] |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
771 |
|
55178
5609352fea12
#6416: Fix compilation of the select module on Windows, as well as test_subprocess:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
55092
diff
changeset
|
772 if getattr(subprocess, '_has_poll', False): |
|
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
|
773 class ProcessTestCaseNoPoll(ProcessTestCase): |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
774 def setUp(self): |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
775 subprocess._has_poll = False |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
776 ProcessTestCase.setUp(self) |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
777 |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
778 def tearDown(self): |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
779 subprocess._has_poll = True |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
780 ProcessTestCase.tearDown(self) |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
781 |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
782 unit_tests.append(ProcessTestCaseNoPoll) |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
783 |
|
9808e38fde72
Use select.poll() in subprocess, when available, rather than select() so that
Gregory P. Smith <greg@mad-scientist.com>
parents:
55002
diff
changeset
|
784 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
785 def test_main(): |
|
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
|
786 test_support.run_unittest(*unit_tests) |
|
39055
3b38ab9c4b03
Make it possible to run test_subprocess.py with Python 2.2, which lacks test_support.reap_children().
Peter Astrand <astrand@lysator.liu.se>
parents:
38967
diff
changeset
|
787 if hasattr(test_support, "reap_children"): |
|
3b38ab9c4b03
Make it possible to run test_subprocess.py with Python 2.2, which lacks test_support.reap_children().
Peter Astrand <astrand@lysator.liu.se>
parents:
38967
diff
changeset
|
788 test_support.reap_children() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
789 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
790 if __name__ == "__main__": |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
791 test_main() |
