1999-03-11 09:26:23 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""Test script for popen2.py
|
|
|
|
Christian Tismer
|
|
|
|
"""
|
|
|
|
|
2000-08-28 14:20:05 -03:00
|
|
|
import os
|
2001-01-30 14:35:32 -04:00
|
|
|
import sys
|
2002-07-23 16:04:11 -03:00
|
|
|
from test.test_support import TestSkipped
|
2000-08-28 14:20:05 -03:00
|
|
|
|
1999-03-11 09:26:23 -04:00
|
|
|
# popen2 contains its own testing routine
|
|
|
|
# which is especially useful to see if open files
|
2000-07-27 04:42:43 -03:00
|
|
|
# like stdin can be read successfully by a forked
|
1999-03-11 09:26:23 -04:00
|
|
|
# subprocess.
|
|
|
|
|
|
|
|
def main():
|
2000-08-28 14:20:05 -03:00
|
|
|
print "Test popen2 module:"
|
2002-06-11 03:22:31 -03:00
|
|
|
if (sys.platform[:4] == 'beos' or sys.platform[:6] == 'atheos') \
|
|
|
|
and __name__ != '__main__':
|
2001-01-30 14:35:32 -04: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 03:22:31 -03:00
|
|
|
raise TestSkipped, "popen2() doesn't work during import on " + sys.platform
|
2000-07-27 04:42:43 -03: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 09:26:23 -04:00
|
|
|
import popen2
|
|
|
|
popen2._test()
|
|
|
|
|
|
|
|
|
2000-08-28 14:20:05 -03:00
|
|
|
def _test():
|
|
|
|
# same test as popen2._test(), but using the os.popen*() API
|
|
|
|
print "Testing os module:"
|
|
|
|
import popen2
|
|
|
|
cmd = "cat"
|
2000-09-01 17:38:55 -03:00
|
|
|
teststr = "ab cd\n"
|
2000-08-28 14:20:05 -03:00
|
|
|
if os.name == "nt":
|
|
|
|
cmd = "more"
|
2000-09-01 17:38:55 -03: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-28 14:20:05 -03:00
|
|
|
print "testing popen2..."
|
|
|
|
w, r = os.popen2(cmd)
|
|
|
|
w.write(teststr)
|
|
|
|
w.close()
|
2000-09-01 17:38:55 -03:00
|
|
|
got = r.read()
|
|
|
|
if got.strip() != expected:
|
2004-02-12 13:35:32 -04:00
|
|
|
raise ValueError("wrote %r read %r" % (teststr, got))
|
2000-08-28 14:20:05 -03: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-01 17:38:55 -03:00
|
|
|
got = r.read()
|
|
|
|
if got.strip() != expected:
|
2004-02-12 13:35:32 -04:00
|
|
|
raise ValueError("wrote %r read %r" % (teststr, got))
|
2000-09-01 17:38:55 -03:00
|
|
|
got = e.read()
|
|
|
|
if got:
|
2004-02-12 13:35:32 -04:00
|
|
|
raise ValueError("unexected %r on stderr" % (got,))
|
2000-08-28 14:20:05 -03:00
|
|
|
for inst in popen2._active[:]:
|
|
|
|
inst.wait()
|
2000-09-01 17:38:55 -03:00
|
|
|
if popen2._active:
|
|
|
|
raise ValueError("_active not empty")
|
2000-08-28 14:20:05 -03:00
|
|
|
print "All OK"
|
|
|
|
|
|
|
|
main()
|
|
|
|
_test()
|