home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_popen2.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  2.0 KB  |  73 lines

  1. #! /usr/bin/env python
  2. """Test script for popen2.py
  3.    Christian Tismer
  4. """
  5.  
  6. import os
  7. import sys
  8. from test_support import TestSkipped
  9.  
  10. # popen2 contains its own testing routine
  11. # which is especially useful to see if open files
  12. # like stdin can be read successfully by a forked
  13. # subprocess.
  14.  
  15. def main():
  16.     print "Test popen2 module:"
  17.     if sys.platform[:4] == 'beos' and __name__ != '__main__':
  18.         #  Locks get messed up or something.  Generally we're supposed
  19.         #  to avoid mixing "posix" fork & exec with native threads, and
  20.         #  they may be right about that after all.
  21.         raise TestSkipped, "popen2() doesn't work during import on BeOS"
  22.     try:
  23.         from os import popen
  24.     except ImportError:
  25.         # if we don't have os.popen, check that
  26.         # we have os.fork.  if not, skip the test
  27.         # (by raising an ImportError)
  28.         from os import fork
  29.     import popen2
  30.     popen2._test()
  31.  
  32.  
  33. def _test():
  34.     # same test as popen2._test(), but using the os.popen*() API
  35.     print "Testing os module:"
  36.     import popen2
  37.     cmd  = "cat"
  38.     teststr = "ab cd\n"
  39.     if os.name == "nt":
  40.         cmd = "more"
  41.     # "more" doesn't act the same way across Windows flavors,
  42.     # sometimes adding an extra newline at the start or the
  43.     # end.  So we strip whitespace off both ends for comparison.
  44.     expected = teststr.strip()
  45.     print "testing popen2..."
  46.     w, r = os.popen2(cmd)
  47.     w.write(teststr)
  48.     w.close()
  49.     got = r.read()
  50.     if got.strip() != expected:
  51.         raise ValueError("wrote %s read %s" % (`teststr`, `got`))
  52.     print "testing popen3..."
  53.     try:
  54.         w, r, e = os.popen3([cmd])
  55.     except:
  56.         w, r, e = os.popen3(cmd)
  57.     w.write(teststr)
  58.     w.close()
  59.     got = r.read()
  60.     if got.strip() != expected:
  61.         raise ValueError("wrote %s read %s" % (`teststr`, `got`))
  62.     got = e.read()
  63.     if got:
  64.         raise ValueError("unexected %s on stderr" % `got`)
  65.     for inst in popen2._active[:]:
  66.         inst.wait()
  67.     if popen2._active:
  68.         raise ValueError("_active not empty")
  69.     print "All OK"
  70.  
  71. main()
  72. _test()
  73.