Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_subprocess.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 15d8b2db2be4 |
| children | 12469f20656b |
| 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 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
75 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
|
76 # call() function with keyword args |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
77 newenv = os.environ.copy() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
78 newenv["FRUIT"] = "banana" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
79 rc = subprocess.call([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
80 'import sys, os;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
81 'sys.exit(os.getenv("FRUIT")=="banana")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
82 env=newenv) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
83 self.assertEqual(rc, 1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
84 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
85 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
|
86 # .stdin is None when not redirected |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
87 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
88 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
89 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
90 self.assertEqual(p.stdin, None) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
91 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
92 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
|
93 # .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
|
94 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
|
95 '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
|
96 '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
|
97 '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
|
98 stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
99 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
100 self.assertEqual(p.stdout, None) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
101 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
102 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
|
103 # .stderr is None when not redirected |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
104 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
105 stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
106 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
107 self.assertEqual(p.stderr, None) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
108 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
109 def test_executable(self): |
| 33732 | 110 p = subprocess.Popen(["somethingyoudonthave", |
| 111 "-c", "import sys; sys.exit(47)"], | |
| 112 executable=sys.executable) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
113 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
114 self.assertEqual(p.returncode, 47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
115 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
116 def test_stdin_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
|
117 # stdin redirection |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
118 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
119 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
120 stdin=subprocess.PIPE) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
121 p.stdin.write("pear") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
122 p.stdin.close() |
|
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.returncode, 1) |
|
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_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
|
127 # stdin is set to open file descriptor |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
128 tf = tempfile.TemporaryFile() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
129 d = tf.fileno() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
130 os.write(d, "pear") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
131 os.lseek(d, 0, 0) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
132 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
133 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
134 stdin=d) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
135 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
136 self.assertEqual(p.returncode, 1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
137 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
138 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
|
139 # stdin is set to open file object |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
140 tf = tempfile.TemporaryFile() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
141 tf.write("pear") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
142 tf.seek(0) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
143 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
144 'import sys; sys.exit(sys.stdin.read() == "pear")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
145 stdin=tf) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
146 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
147 self.assertEqual(p.returncode, 1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
148 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
149 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
|
150 # stdout redirection |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
151 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
152 'import sys; sys.stdout.write("orange")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
153 stdout=subprocess.PIPE) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
154 self.assertEqual(p.stdout.read(), "orange") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
155 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
156 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
|
157 # stdout is set to open file descriptor |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
158 tf = tempfile.TemporaryFile() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
159 d = tf.fileno() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
160 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
161 'import sys; sys.stdout.write("orange")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
162 stdout=d) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
163 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
164 os.lseek(d, 0, 0) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
165 self.assertEqual(os.read(d, 1024), "orange") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
166 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
167 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
|
168 # stdout is set to open file object |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
169 tf = tempfile.TemporaryFile() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
170 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
171 'import sys; sys.stdout.write("orange")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
172 stdout=tf) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
173 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
174 tf.seek(0) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
175 self.assertEqual(tf.read(), "orange") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
176 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
177 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
|
178 # stderr redirection |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
179 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
180 'import sys; sys.stderr.write("strawberry")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
181 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
|
182 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
|
183 "strawberry") |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
184 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
185 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
|
186 # stderr is set to open file descriptor |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
187 tf = tempfile.TemporaryFile() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
188 d = tf.fileno() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
189 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
190 'import sys; sys.stderr.write("strawberry")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
191 stderr=d) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
192 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
193 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
|
194 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
|
195 "strawberry") |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
196 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
197 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
|
198 # stderr is set to open file object |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
199 tf = tempfile.TemporaryFile() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
200 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
201 'import sys; sys.stderr.write("strawberry")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
202 stderr=tf) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
203 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
204 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
|
205 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
|
206 "strawberry") |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
207 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
208 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
|
209 # capture stdout and stderr to the same pipe |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
210 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
211 'import sys;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
212 'sys.stdout.write("apple");' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
213 'sys.stdout.flush();' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
214 'sys.stderr.write("orange")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
215 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
216 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
|
217 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
|
218 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
|
219 self.assertEqual(stripped, "appleorange") |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
220 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
221 def test_stdout_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
|
222 # 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
|
223 tf = tempfile.TemporaryFile() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
224 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
225 'import sys;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
226 'sys.stdout.write("apple");' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
227 'sys.stdout.flush();' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
228 'sys.stderr.write("orange")'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
229 stdout=tf, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
230 stderr=tf) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
231 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
232 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
|
233 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
|
234 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
|
235 self.assertEqual(stripped, "appleorange") |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
236 |
|
39793
363ef804a096
Fixed subprocess bug #1531862 again, after removing tests
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
39772
diff
changeset
|
237 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
|
238 # stdout is set to 1 (#1531862). |
|
363ef804a096
Fixed subprocess bug #1531862 again, after removing tests
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
39772
diff
changeset
|
239 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
|
240 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
|
241 self.assertEquals(rc, 2) |
|
363ef804a096
Fixed subprocess bug #1531862 again, after removing tests
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
39772
diff
changeset
|
242 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
243 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
|
244 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
|
245 # 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
|
246 # 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
|
247 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
|
248 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
|
249 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
|
250 os.chdir(cwd) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
251 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
252 'import sys,os;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
253 'sys.stdout.write(os.getcwd())'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
254 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
255 cwd=tmpdir) |
|
33742
179e42ea2e9c
normalize case when comparing directory names (problem reported by
Fredrik Lundh <fredrik@pythonware.com>
parents:
33739
diff
changeset
|
256 normcase = os.path.normcase |
|
179e42ea2e9c
normalize case when comparing directory names (problem reported by
Fredrik Lundh <fredrik@pythonware.com>
parents:
33739
diff
changeset
|
257 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir)) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
258 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
259 def test_env(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
260 newenv = os.environ.copy() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
261 newenv["FRUIT"] = "orange" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
262 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
263 'import sys,os;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
264 'sys.stdout.write(os.getenv("FRUIT"))'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
265 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
266 env=newenv) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
267 self.assertEqual(p.stdout.read(), "orange") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
268 |
|
34671
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
269 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
|
270 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
|
271 '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
|
272 stdin=subprocess.PIPE) |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
273 p.communicate("pear") |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
274 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
|
275 |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
276 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
|
277 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
|
278 '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
|
279 stdout=subprocess.PIPE) |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
280 (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
|
281 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
|
282 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
|
283 |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
284 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
|
285 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
|
286 '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
|
287 stderr=subprocess.PIPE) |
|
db7a8310b19a
Added three more testcases: Using communicate with only one of
Peter Astrand <astrand@lysator.liu.se>
parents:
34302
diff
changeset
|
288 (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
|
289 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
|
290 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
|
291 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
292 def test_communicate(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
293 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
|
294 '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
|
295 'sys.stderr.write("pineapple");' |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
296 'sys.stdout.write(sys.stdin.read())'], |
| 33732 | 297 stdin=subprocess.PIPE, |
| 298 stdout=subprocess.PIPE, | |
| 299 stderr=subprocess.PIPE) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
300 (stdout, stderr) = p.communicate("banana") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
301 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
|
302 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
|
303 "pineapple") |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
304 |
|
47338
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
305 # 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
|
306 # 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
|
307 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
|
308 # 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
|
309 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
|
310 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
|
311 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
|
312 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
|
313 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
|
314 p.communicate() |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
315 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
|
316 del p |
|
273e8de34603
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
Gregory P. Smith <greg@mad-scientist.com>
parents:
47119
diff
changeset
|
317 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
|
318 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
|
319 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
|
320 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
321 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
|
322 # communicate() should return None if no redirection is active |
| 33732 | 323 p = subprocess.Popen([sys.executable, "-c", |
| 324 "import sys; sys.exit(47)"]) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
325 (stdout, stderr) = p.communicate() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
326 self.assertEqual(stdout, None) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
327 self.assertEqual(stderr, None) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
328 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
329 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
|
330 # communicate() with writes larger than pipe_buf |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
331 # This test will probably deadlock rather than fail, if |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
332 # communicate() does not work properly. |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
333 x, y = os.pipe() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
334 if mswindows: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
335 pipe_buf = 512 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
336 else: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
337 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
338 os.close(x) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
339 os.close(y) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
340 p = subprocess.Popen([sys.executable, "-c", |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
341 'import sys,os;' |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
342 'sys.stdout.write(sys.stdin.read(47));' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
343 'sys.stderr.write("xyz"*%d);' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
344 'sys.stdout.write(sys.stdin.read())' % pipe_buf], |
| 33732 | 345 stdin=subprocess.PIPE, |
| 346 stdout=subprocess.PIPE, | |
| 347 stderr=subprocess.PIPE) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
348 string_to_write = "abc"*pipe_buf |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
349 (stdout, stderr) = p.communicate(string_to_write) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
350 self.assertEqual(stdout, string_to_write) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
351 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
352 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
|
353 # stdin.write before communicate() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
354 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
355 'import sys,os;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
356 'sys.stdout.write(sys.stdin.read())'], |
| 33732 | 357 stdin=subprocess.PIPE, |
| 358 stdout=subprocess.PIPE, | |
| 359 stderr=subprocess.PIPE) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
360 p.stdin.write("banana") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
361 (stdout, stderr) = p.communicate("split") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
362 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
|
363 self.assertEqual(remove_stderr_debug_decorations(stderr), "") |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
364 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
365 def test_universal_newlines(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
366 p = subprocess.Popen([sys.executable, "-c", |
| 33732 | 367 'import sys,os;' + SETBINARY + |
| 368 'sys.stdout.write("line1\\n");' | |
| 369 'sys.stdout.flush();' | |
| 370 'sys.stdout.write("line2\\r");' | |
| 371 'sys.stdout.flush();' | |
| 372 'sys.stdout.write("line3\\r\\n");' | |
| 373 'sys.stdout.flush();' | |
| 374 'sys.stdout.write("line4\\r");' | |
| 375 'sys.stdout.flush();' | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
376 'sys.stdout.write("\\nline5");' |
| 33732 | 377 'sys.stdout.flush();' |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
378 'sys.stdout.write("\\nline6");'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
379 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
380 universal_newlines=1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
381 stdout = p.stdout.read() |
|
38078
b1cbc605bec9
Try to fix breakage caused by patch #1479181, r45850
Neal Norwitz <nnorwitz@gmail.com>
parents:
38077
diff
changeset
|
382 if hasattr(file, 'newlines'): |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
383 # Interpreter with universal newline support |
| 33732 | 384 self.assertEqual(stdout, |
| 385 "line1\nline2\nline3\nline4\nline5\nline6") | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
386 else: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
387 # Interpreter without universal newline support |
| 33732 | 388 self.assertEqual(stdout, |
| 389 "line1\nline2\rline3\r\nline4\r\nline5\nline6") | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
390 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
391 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
|
392 # universal newlines through communicate() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
393 p = subprocess.Popen([sys.executable, "-c", |
| 33732 | 394 'import sys,os;' + SETBINARY + |
| 395 'sys.stdout.write("line1\\n");' | |
| 396 'sys.stdout.flush();' | |
| 397 'sys.stdout.write("line2\\r");' | |
| 398 'sys.stdout.flush();' | |
| 399 'sys.stdout.write("line3\\r\\n");' | |
| 400 'sys.stdout.flush();' | |
| 401 'sys.stdout.write("line4\\r");' | |
| 402 'sys.stdout.flush();' | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
403 'sys.stdout.write("\\nline5");' |
| 33732 | 404 'sys.stdout.flush();' |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
405 'sys.stdout.write("\\nline6");'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
406 stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
407 universal_newlines=1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
408 (stdout, stderr) = p.communicate() |
|
38078
b1cbc605bec9
Try to fix breakage caused by patch #1479181, r45850
Neal Norwitz <nnorwitz@gmail.com>
parents:
38077
diff
changeset
|
409 if hasattr(file, 'newlines'): |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
410 # Interpreter with universal newline support |
| 33732 | 411 self.assertEqual(stdout, |
| 412 "line1\nline2\nline3\nline4\nline5\nline6") | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
413 else: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
414 # Interpreter without universal newline support |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
415 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
|
416 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
417 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
|
418 # 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
|
419 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
|
420 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
|
421 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
|
422 else: |
|
34818
4eaab38566c9
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34695
diff
changeset
|
423 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
|
424 for i in range(max_handles): |
| 33732 | 425 p = subprocess.Popen([sys.executable, "-c", |
| 426 "import sys;sys.stdout.write(sys.stdin.read())"], | |
| 427 stdin=subprocess.PIPE, | |
| 428 stdout=subprocess.PIPE, | |
| 429 stderr=subprocess.PIPE) | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
430 data = p.communicate("lime")[0] |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
431 self.assertEqual(data, "lime") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
432 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
433 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
434 def test_list2cmdline(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
435 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
436 '"a b c" d e') |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
437 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
438 '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
|
439 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
|
440 'ab\\"c " \\\\" d') |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
441 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']), |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
442 'a\\\\\\b "de fg" h') |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
443 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']), |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
444 'a\\\\\\"b c d') |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
445 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']), |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
446 '"a\\\\b c" d e') |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
447 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']), |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
448 '"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
|
449 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
|
450 '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
|
451 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
|
452 'echo "foo|bar"') |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
453 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
454 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
455 def test_poll(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
456 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
|
457 "-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
|
458 count = 0 |
|
a4f747e72138
Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents:
33737
diff
changeset
|
459 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
|
460 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
|
461 count += 1 |
|
a4f747e72138
Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents:
33737
diff
changeset
|
462 # 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
|
463 # 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
|
464 # 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
|
465 # didn't go around at least twice. |
|
a4f747e72138
Kill several problems at once: test_poll() failed sometimes for me.
Tim Peters <tim.peters@gmail.com>
parents:
33737
diff
changeset
|
466 self.assert_(count >= 2) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
467 # Subsequent invocations should just return the returncode |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
468 self.assertEqual(p.poll(), 0) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
469 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
470 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
471 def test_wait(self): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
472 p = subprocess.Popen([sys.executable, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
473 "-c", "import time; time.sleep(2)"]) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
474 self.assertEqual(p.wait(), 0) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
475 # Subsequent invocations should just return the returncode |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
476 self.assertEqual(p.wait(), 0) |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
477 |
|
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
|
478 |
|
79d2ae0a62ef
Raise TypeError if bufsize argument is not an integer. Patch 1071755, slightly modified.
Peter Astrand <astrand@lysator.liu.se>
parents:
33998
diff
changeset
|
479 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
|
480 # 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
|
481 # 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
|
482 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
|
483 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
|
484 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
|
485 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
|
486 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
|
487 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
|
488 |
|
51637
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
489 def test_leaking_fds_on_error(self): |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
490 # see bug #5179: Popen leaks file descriptors to PIPEs if |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
491 # the child fails to execute; this will eventually exhaust |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
492 # the maximum number of open fds. 1024 seems a very common |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
493 # value for that limit, but Windows has 2048, so we loop |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
494 # 1024 times (each call leaked two fds). |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
495 for i in range(1024): |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
496 try: |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
497 subprocess.Popen(['nonexisting_i_hope'], |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
498 stdout=subprocess.PIPE, |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
499 stderr=subprocess.PIPE) |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
500 # Windows raises IOError |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
501 except (IOError, OSError), err: |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
502 if err.errno != 2: # ignore "no such file" |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
503 raise |
|
15d8b2db2be4
Merged revisions 69620 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47338
diff
changeset
|
504 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
505 # |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
506 # POSIX tests |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
507 # |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
508 if not mswindows: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
509 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
|
510 # catched & re-raised exceptions |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
511 try: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
512 p = subprocess.Popen([sys.executable, "-c", ""], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
513 cwd="/this/path/does/not/exist") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
514 except OSError, e: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
515 # The attribute child_traceback should contain "os.chdir" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
516 # somewhere. |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
517 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
518 else: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
519 self.fail("Expected OSError") |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
520 |
|
39341
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
521 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
|
522 """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
|
523 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
|
524 """ |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
525 try: |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
526 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
|
527 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
|
528 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
|
529 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
|
530 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
|
531 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
|
532 |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
533 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
|
534 """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
|
535 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
|
536 return |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
537 try: |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
538 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
|
539 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
|
540 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
|
541 return |
|
39346
bd50a42b7df0
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39341
diff
changeset
|
542 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
543 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
|
544 # 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
|
545 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
|
546 try: |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
547 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
|
548 "-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
|
549 finally: |
|
53d0552a7a8c
[Patch #1520905] Attempt to suppress core file created by test_subprocess.py.
Andrew M. Kuchling <amk@amk.ca>
parents:
39084
diff
changeset
|
550 self._unsuppress_core_files(old_limit) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
551 p.wait() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
552 self.assertEqual(-p.returncode, signal.SIGABRT) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
553 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
554 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
|
555 # preexec function |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
556 p = subprocess.Popen([sys.executable, "-c", |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
557 'import sys,os;' \ |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
558 'sys.stdout.write(os.getenv("FRUIT"))'], |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
559 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
560 preexec_fn=lambda: os.putenv("FRUIT", "apple")) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
561 self.assertEqual(p.stdout.read(), "apple") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
562 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
563 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
|
564 # args is a string |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
565 f, fname = self.mkstemp() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
566 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
|
567 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 33732 | 568 sys.executable) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
569 os.close(f) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
570 os.chmod(fname, 0700) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
571 p = subprocess.Popen(fname) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
572 p.wait() |
|
33998
f5311303a357
Remove tempfile after use in test_call_string.
Peter Astrand <astrand@lysator.liu.se>
parents:
33980
diff
changeset
|
573 os.remove(fname) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
574 self.assertEqual(p.returncode, 47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
575 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
576 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
|
577 # invalid arguments should raise ValueError |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
578 self.assertRaises(ValueError, subprocess.call, |
| 33732 | 579 [sys.executable, |
| 580 "-c", "import sys; sys.exit(47)"], | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
581 startupinfo=47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
582 self.assertRaises(ValueError, subprocess.call, |
| 33732 | 583 [sys.executable, |
| 584 "-c", "import sys; sys.exit(47)"], | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
585 creationflags=47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
586 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
587 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
|
588 # Run command through the shell (sequence) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
589 newenv = os.environ.copy() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
590 newenv["FRUIT"] = "apple" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
591 p = subprocess.Popen(["echo $FRUIT"], shell=1, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
592 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
593 env=newenv) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
594 self.assertEqual(p.stdout.read().strip(), "apple") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
595 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
596 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
|
597 # Run command through the shell (string) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
598 newenv = os.environ.copy() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
599 newenv["FRUIT"] = "apple" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
600 p = subprocess.Popen("echo $FRUIT", shell=1, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
601 stdout=subprocess.PIPE, |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
602 env=newenv) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
603 self.assertEqual(p.stdout.read().strip(), "apple") |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
604 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
605 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
|
606 # call() function with string argument on UNIX |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
607 f, fname = self.mkstemp() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
608 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
|
609 os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % |
| 33732 | 610 sys.executable) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
611 os.close(f) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
612 os.chmod(fname, 0700) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
613 rc = subprocess.call(fname) |
|
33998
f5311303a357
Remove tempfile after use in test_call_string.
Peter Astrand <astrand@lysator.liu.se>
parents:
33980
diff
changeset
|
614 os.remove(fname) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
615 self.assertEqual(rc, 47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
616 |
|
46706
c8aa220c6cee
Disabled some unit tests for the upcoming release. See #2777
Christian Heimes <christian@cheimes.de>
parents:
46479
diff
changeset
|
617 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
|
618 p = subprocess.Popen([sys.executable, |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
619 "-c", "input()"]) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
620 |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
621 self.assert_(p.poll() is None, p.poll()) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
622 p.send_signal(signal.SIGINT) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
623 self.assertNotEqual(p.wait(), 0) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
624 |
|
46706
c8aa220c6cee
Disabled some unit tests for the upcoming release. See #2777
Christian Heimes <christian@cheimes.de>
parents:
46479
diff
changeset
|
625 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
|
626 p = subprocess.Popen([sys.executable, |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
627 "-c", "input()"]) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
628 |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
629 self.assert_(p.poll() is None, p.poll()) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
630 p.kill() |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
631 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
|
632 |
|
46706
c8aa220c6cee
Disabled some unit tests for the upcoming release. See #2777
Christian Heimes <christian@cheimes.de>
parents:
46479
diff
changeset
|
633 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
|
634 p = subprocess.Popen([sys.executable, |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
635 "-c", "input()"]) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
636 |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
637 self.assert_(p.poll() is None, p.poll()) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
638 p.terminate() |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
639 self.assertEqual(p.wait(), -signal.SIGTERM) |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
640 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
641 # |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
642 # Windows tests |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
643 # |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
644 if mswindows: |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
645 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
|
646 # startupinfo argument |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
647 # We uses hardcoded constants, because we do not want to |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
648 # depend on win32all. |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
649 STARTF_USESHOWWINDOW = 1 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
650 SW_MAXIMIZE = 3 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
651 startupinfo = subprocess.STARTUPINFO() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
652 startupinfo.dwFlags = STARTF_USESHOWWINDOW |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
653 startupinfo.wShowWindow = SW_MAXIMIZE |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
654 # 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
|
655 # by wShowWindow, but the argument should be silently |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
656 # ignored |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
657 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
|
658 startupinfo=startupinfo) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
659 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
660 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
|
661 # creationflags argument |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
662 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
|
663 sys.stderr.write(" a DOS box should flash briefly ...\n") |
| 33732 | 664 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
|
665 ' -c "import time; time.sleep(0.25)"', |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
666 creationflags=CREATE_NEW_CONSOLE) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
667 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
668 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
|
669 # invalid arguments should raise ValueError |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
670 self.assertRaises(ValueError, subprocess.call, |
| 33732 | 671 [sys.executable, |
| 672 "-c", "import sys; sys.exit(47)"], | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
673 preexec_fn=lambda: 1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
674 self.assertRaises(ValueError, subprocess.call, |
| 33732 | 675 [sys.executable, |
| 676 "-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
|
677 stdout=subprocess.PIPE, |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
678 close_fds=True) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
679 |
|
41886
ec3b8e94a355
Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents:
40619
diff
changeset
|
680 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
|
681 # close file descriptors |
|
ec3b8e94a355
Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents:
40619
diff
changeset
|
682 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
|
683 "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
|
684 close_fds=True) |
|
ec3b8e94a355
Applied patch 1669481, slightly modified: Support close_fds on Win32
Peter Astrand <astrand@lysator.liu.se>
parents:
40619
diff
changeset
|
685 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
|
686 |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
687 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
|
688 # Run command through the shell (sequence) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
689 newenv = os.environ.copy() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
690 newenv["FRUIT"] = "physalis" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
691 p = subprocess.Popen(["set"], shell=1, |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
692 stdout=subprocess.PIPE, |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
693 env=newenv) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
694 self.assertNotEqual(p.stdout.read().find("physalis"), -1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
695 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
696 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
|
697 # Run command through the shell (string) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
698 newenv = os.environ.copy() |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
699 newenv["FRUIT"] = "physalis" |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
700 p = subprocess.Popen("set", shell=1, |
|
33731
53c468d0955b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33723
diff
changeset
|
701 stdout=subprocess.PIPE, |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
702 env=newenv) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
703 self.assertNotEqual(p.stdout.read().find("physalis"), -1) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
704 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
705 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
|
706 # call() function with string argument on Windows |
| 33732 | 707 rc = subprocess.call(sys.executable + |
| 708 ' -c "import sys; sys.exit(47)"') | |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
709 self.assertEqual(rc, 47) |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
710 |
|
46706
c8aa220c6cee
Disabled some unit tests for the upcoming release. See #2777
Christian Heimes <christian@cheimes.de>
parents:
46479
diff
changeset
|
711 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
|
712 p = subprocess.Popen([sys.executable, |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
713 "-c", "input()"]) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
714 |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
715 self.assert_(p.poll() is None, p.poll()) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
716 p.send_signal(signal.SIGTERM) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
717 self.assertNotEqual(p.wait(), 0) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
718 |
|
46706
c8aa220c6cee
Disabled some unit tests for the upcoming release. See #2777
Christian Heimes <christian@cheimes.de>
parents:
46479
diff
changeset
|
719 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
|
720 p = subprocess.Popen([sys.executable, |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
721 "-c", "input()"]) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
722 |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
723 self.assert_(p.poll() is None, p.poll()) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
724 p.kill() |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
725 self.assertNotEqual(p.wait(), 0) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
726 |
|
46706
c8aa220c6cee
Disabled some unit tests for the upcoming release. See #2777
Christian Heimes <christian@cheimes.de>
parents:
46479
diff
changeset
|
727 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
|
728 p = subprocess.Popen([sys.executable, |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
729 "-c", "input()"]) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
730 |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
731 self.assert_(p.poll() is None, p.poll()) |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
732 p.terminate() |
|
e16593da4898
Added kill, terminate and send_signal to subprocess.Popen
Christian Heimes <christian@cheimes.de>
parents:
44801
diff
changeset
|
733 self.assertNotEqual(p.wait(), 0) |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
734 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
735 def test_main(): |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
736 test_support.run_unittest(ProcessTestCase) |
|
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
|
737 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
|
738 test_support.reap_children() |
|
33723
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
739 |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
740 if __name__ == "__main__": |
|
76641824cf05
Added Peter Astrand's subprocess module.
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
741 test_main() |
