1999-03-11 21:26:23 +08:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""Test script for popen2.py
|
|
|
|
Christian Tismer
|
|
|
|
"""
|
|
|
|
|
2000-08-29 01:20:05 +08:00
|
|
|
import os
|
2001-01-31 02:35:32 +08:00
|
|
|
import sys
|
2002-07-24 03:04:11 +08:00
|
|
|
from test.test_support import TestSkipped
|
2000-08-29 01:20:05 +08:00
|
|
|
|
1999-03-11 21:26:23 +08:00
|
|
|
# popen2 contains its own testing routine
|
|
|
|
# which is especially useful to see if open files
|
2000-07-27 15:42:43 +08:00
|
|
|
# like stdin can be read successfully by a forked
|
1999-03-11 21:26:23 +08:00
|
|
|
# subprocess.
|
|
|
|
|
|
|
|
def main():
|
2000-08-29 01:20:05 +08:00
|
|
|
print "Test popen2 module:"
|
2002-06-11 14:22:31 +08:00
|
|
|
if (sys.platform[:4] == 'beos' or sys.platform[:6] == 'atheos') \
|
|
|
|
and __name__ != '__main__':
|
2001-01-31 02:35:32 +08:00
|
|
|
# Locks get messed up or something. Generally we're supposed
|
|
|
|
# to avoid mixing "posix" fork & exec with native threads, and
|
|
|
|
# they may be right about that after all.
|
2002-06-11 14:22:31 +08:00
|
|
|
raise TestSkipped, "popen2() doesn't work during import on " + sys.platform
|
2000-07-27 15:42:43 +08:00
|
|
|
try:
|
|
|
|
from os import popen
|
|
|
|
except ImportError:
|
|
|
|
# if we don't have os.popen, check that
|
|
|
|
# we have os.fork. if not, skip the test
|
|
|
|
# (by raising an ImportError)
|
|
|
|
from os import fork
|
1999-03-11 21:26:23 +08:00
|
|
|
import popen2
|
|
|
|
popen2._test()
|
|
|
|
|
|
|
|
|
2000-08-29 01:20:05 +08:00
|
|
|
def _test():
|
|
|
|
# same test as popen2._test(), but using the os.popen*() API
|
|
|
|
print "Testing os module:"
|
|
|
|
import popen2
|
|
|
|
cmd = "cat"
|
2000-09-02 04:38:55 +08:00
|
|
|
teststr = "ab cd\n"
|
2000-08-29 01:20:05 +08:00
|
|
|
if os.name == "nt":
|
|
|
|
cmd = "more"
|
2000-09-02 04:38:55 +08:00
|
|
|
# "more" doesn't act the same way across Windows flavors,
|
|
|
|
# sometimes adding an extra newline at the start or the
|
|
|
|
# end. So we strip whitespace off both ends for comparison.
|
|
|
|
expected = teststr.strip()
|
2000-08-29 01:20:05 +08:00
|
|
|
print "testing popen2..."
|
|
|
|
w, r = os.popen2(cmd)
|
|
|
|
w.write(teststr)
|
|
|
|
w.close()
|
2000-09-02 04:38:55 +08:00
|
|
|
got = r.read()
|
|
|
|
if got.strip() != expected:
|
|
|
|
raise ValueError("wrote %s read %s" % (`teststr`, `got`))
|
2000-08-29 01:20:05 +08:00
|
|
|
print "testing popen3..."
|
|
|
|
try:
|
|
|
|
w, r, e = os.popen3([cmd])
|
|
|
|
except:
|
|
|
|
w, r, e = os.popen3(cmd)
|
|
|
|
w.write(teststr)
|
|
|
|
w.close()
|
2000-09-02 04:38:55 +08:00
|
|
|
got = r.read()
|
|
|
|
if got.strip() != expected:
|
|
|
|
raise ValueError("wrote %s read %s" % (`teststr`, `got`))
|
|
|
|
got = e.read()
|
|
|
|
if got:
|
|
|
|
raise ValueError("unexected %s on stderr" % `got`)
|
2000-08-29 01:20:05 +08:00
|
|
|
for inst in popen2._active[:]:
|
|
|
|
inst.wait()
|
2000-09-02 04:38:55 +08:00
|
|
|
if popen2._active:
|
|
|
|
raise ValueError("_active not empty")
|
2000-08-29 01:20:05 +08:00
|
|
|
print "All OK"
|
|
|
|
|
|
|
|
main()
|
|
|
|
_test()
|