Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_popen2.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 228cfb6e2900 |
| children | 1ea03f86e081 |
| rev | line source |
|---|---|
|
10376
337733ce9593
Test for popen2 module, by Chris Tismer.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
|
41216
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
2 """Test script for popen2.py""" |
|
10376
337733ce9593
Test for popen2 module, by Chris Tismer.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 |
|
41690
6c4aa4611c88
Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41532
diff
changeset
|
4 import warnings |
|
6c4aa4611c88
Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41532
diff
changeset
|
5 warnings.filterwarnings("ignore", ".*popen2 module is deprecated.*", |
|
6c4aa4611c88
Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41532
diff
changeset
|
6 DeprecationWarning) |
|
6c4aa4611c88
Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41532
diff
changeset
|
7 warnings.filterwarnings("ignore", "os\.popen. is deprecated.*", |
|
6c4aa4611c88
Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41532
diff
changeset
|
8 DeprecationWarning) |
|
6c4aa4611c88
Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41532
diff
changeset
|
9 |
|
14236
58d7995ce795
Added os.popen2() and os.popen3() for non-Windows platforms.
Fred Drake <fdrake@acm.org>
parents:
13727
diff
changeset
|
10 import os |
|
16363
c29ca1980303
Checking in patch #103478 -- makes popen2 and fork1 tested on BeOS.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
14361
diff
changeset
|
11 import sys |
|
41216
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
12 import unittest |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
13 import popen2 |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
14 |
|
52355
228cfb6e2900
fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents:
52353
diff
changeset
|
15 from test.test_support import run_unittest, reap_children |
|
14236
58d7995ce795
Added os.popen2() and os.popen3() for non-Windows platforms.
Fred Drake <fdrake@acm.org>
parents:
13727
diff
changeset
|
16 |
|
41216
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
17 if sys.platform[:4] == 'beos' or sys.platform[:6] == 'atheos': |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
18 # Locks get messed up or something. Generally we're supposed |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
19 # to avoid mixing "posix" fork & exec with native threads, and |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
20 # they may be right about that after all. |
|
52355
228cfb6e2900
fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents:
52353
diff
changeset
|
21 raise unittest.SkipTest("popen2() doesn't work on " + sys.platform) |
|
10376
337733ce9593
Test for popen2 module, by Chris Tismer.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 |
|
41216
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
23 # if we don't have os.popen, check that |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
24 # we have os.fork. if not, skip the test |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
25 # (by raising an ImportError) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
26 try: |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
27 from os import popen |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
28 del popen |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
29 except ImportError: |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
30 from os import fork |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
31 del fork |
|
10376
337733ce9593
Test for popen2 module, by Chris Tismer.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 |
|
41216
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
33 class Popen2Test(unittest.TestCase): |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
34 cmd = "cat" |
|
14236
58d7995ce795
Added os.popen2() and os.popen3() for non-Windows platforms.
Fred Drake <fdrake@acm.org>
parents:
13727
diff
changeset
|
35 if os.name == "nt": |
|
58d7995ce795
Added os.popen2() and os.popen3() for non-Windows platforms.
Fred Drake <fdrake@acm.org>
parents:
13727
diff
changeset
|
36 cmd = "more" |
|
41216
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
37 teststr = "ab cd\n" |
|
14361
5c51c096de3b
The "more" cmd varies across Windows flavors, sometimes adding stray
Tim Peters <tim.peters@gmail.com>
parents:
14236
diff
changeset
|
38 # "more" doesn't act the same way across Windows flavors, |
|
5c51c096de3b
The "more" cmd varies across Windows flavors, sometimes adding stray
Tim Peters <tim.peters@gmail.com>
parents:
14236
diff
changeset
|
39 # sometimes adding an extra newline at the start or the |
|
5c51c096de3b
The "more" cmd varies across Windows flavors, sometimes adding stray
Tim Peters <tim.peters@gmail.com>
parents:
14236
diff
changeset
|
40 # end. So we strip whitespace off both ends for comparison. |
|
5c51c096de3b
The "more" cmd varies across Windows flavors, sometimes adding stray
Tim Peters <tim.peters@gmail.com>
parents:
14236
diff
changeset
|
41 expected = teststr.strip() |
|
41216
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
42 |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
43 def setUp(self): |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
44 popen2._cleanup() |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
45 # When the test runs, there shouldn't be any open pipes |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
46 self.assertFalse(popen2._active, "Active pipes when test starts" + |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
47 repr([c.cmd for c in popen2._active])) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
48 |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
49 def tearDown(self): |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
50 for inst in popen2._active: |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
51 inst.wait() |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
52 popen2._cleanup() |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
53 self.assertFalse(popen2._active, "_active not empty") |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41216
diff
changeset
|
54 reap_children() |
|
41216
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
55 |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
56 def validate_output(self, teststr, expected_out, r, w, e=None): |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
57 w.write(teststr) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
58 w.close() |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
59 got = r.read() |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
60 self.assertEquals(expected_out, got.strip(), "wrote %r read %r" % |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
61 (teststr, got)) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
62 |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
63 if e is not None: |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
64 got = e.read() |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
65 self.assertFalse(got, "unexpected %r on stderr" % got) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41216
diff
changeset
|
66 |
|
41216
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
67 def test_popen2(self): |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
68 r, w = popen2.popen2(self.cmd) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
69 self.validate_output(self.teststr, self.expected, r, w) |
|
14236
58d7995ce795
Added os.popen2() and os.popen3() for non-Windows platforms.
Fred Drake <fdrake@acm.org>
parents:
13727
diff
changeset
|
70 |
|
41216
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
71 def test_popen3(self): |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
72 if os.name == 'posix': |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
73 r, w, e = popen2.popen3([self.cmd]) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
74 self.validate_output(self.teststr, self.expected, r, w, e) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
75 |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
76 r, w, e = popen2.popen3(self.cmd) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
77 self.validate_output(self.teststr, self.expected, r, w, e) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
78 |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
79 def test_os_popen2(self): |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
80 # same test as test_popen2(), but using the os.popen*() API |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
81 w, r = os.popen2(self.cmd) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
82 self.validate_output(self.teststr, self.expected, r, w) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
83 |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
84 def test_os_popen3(self): |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
85 # same test as test_popen3(), but using the os.popen*() API |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
86 if os.name == 'posix': |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
87 w, r, e = os.popen3([self.cmd]) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
88 self.validate_output(self.teststr, self.expected, r, w, e) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
89 |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
90 w, r, e = os.popen3(self.cmd) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
91 self.validate_output(self.teststr, self.expected, r, w, e) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
92 |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41216
diff
changeset
|
93 |
|
41216
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
94 def test_main(): |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
95 run_unittest(Popen2Test) |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
96 |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
97 if __name__ == "__main__": |
|
8bb570ae4c45
Patch #1676994: Refactor test_popen2 to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38961
diff
changeset
|
98 test_main() |
