0001 #! /usr/bin/env python 0002 """Test script for popen2.py 0003 Christian Tismer 0004 """ 0005 0006 import os 0007 import sys 0008 from test.test_support import TestSkipped 0009 0010 # popen2 contains its own testing routine 0011 # which is especially useful to see if open files 0012 # like stdin can be read successfully by a forked 0013 # subprocess. 0014 0015 def main(): 0016 print "Test popen2 module:" 0017 if (sys.platform[:4] == 'beos' or sys.platform[:6] == 'atheos') \ 0018 and __name__ != '__main__': 0019 # Locks get messed up or something. Generally we're supposed 0020 # to avoid mixing "posix" fork & exec with native threads, and 0021 # they may be right about that after all. 0022 raise TestSkipped, "popen2() doesn't work during import on " + sys.platform 0023 try: 0024 from os import popen 0025 except ImportError: 0026 # if we don't have os.popen, check that 0027 # we have os.fork. if not, skip the test 0028 # (by raising an ImportError) 0029 from os import fork 0030 import popen2 0031 popen2._test() 0032 0033 0034 def _test(): 0035 # same test as popen2._test(), but using the os.popen*() API 0036 print "Testing os module:" 0037 import popen2 0038 cmd = "cat" 0039 teststr = "ab cd\n" 0040 if os.name == "nt": 0041 cmd = "more" 0042 # "more" doesn't act the same way across Windows flavors, 0043 # sometimes adding an extra newline at the start or the 0044 # end. So we strip whitespace off both ends for comparison. 0045 expected = teststr.strip() 0046 print "testing popen2..." 0047 w, r = os.popen2(cmd) 0048 w.write(teststr) 0049 w.close() 0050 got = r.read() 0051 if got.strip() != expected: 0052 raise ValueError("wrote %r read %r" % (teststr, got)) 0053 print "testing popen3..." 0054 try: 0055 w, r, e = os.popen3([cmd]) 0056 except: 0057 w, r, e = os.popen3(cmd) 0058 w.write(teststr) 0059 w.close() 0060 got = r.read() 0061 if got.strip() != expected: 0062 raise ValueError("wrote %r read %r" % (teststr, got)) 0063 got = e.read() 0064 if got: 0065 raise ValueError("unexected %r on stderr" % (got,)) 0066 for inst in popen2._active[:]: 0067 inst.wait() 0068 if popen2._active: 0069 raise ValueError("_active not empty") 0070 print "All OK" 0071 0072 main() 0073 _test() 0074
Generated by PyXR 0.9.4