home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / TEST_POPEN2.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  1.7 KB  |  66 lines

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