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_PTY.PY < prev    next >
Encoding:
Python Source  |  2000-10-03  |  2.9 KB  |  92 lines

  1. import pty, os, sys, string
  2. from test_support import verbose, TestFailed, TestSkipped
  3.  
  4. TEST_STRING_1 = "I wish to buy a fish license."
  5. TEST_STRING_2 = "For my pet fish, Eric."
  6. TEST_STRING_3 = "And now for something completely different..."
  7. TEST_STRING_4 = "but you pronounce it throatwobbler mangrove."
  8.  
  9. if verbose:
  10.     def debug(msg):
  11.         print msg
  12. else:
  13.     def debug(msg):
  14.         pass
  15.  
  16. # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
  17. # because pty code is not too portable.
  18.  
  19. try:
  20.     debug("Calling master_open()")
  21.     master_fd, slave_name = pty.master_open()
  22.     debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name))
  23.     debug("Calling slave_open(%s)"%`slave_name`)
  24.     slave_fd = pty.slave_open(slave_name)
  25.     debug("Got slave_fd '%d'"%slave_fd)
  26. except OSError:
  27.     # " An optional feature could not be imported " ... ?
  28.     raise TestSkipped, "Pseudo-terminals (seemingly) not functional."
  29.  
  30. if not os.isatty(slave_fd):
  31.     raise TestFailed, "slave_fd is not a tty"
  32.  
  33. debug("Writing to slave_fd")
  34. os.write(slave_fd, TEST_STRING_1) # should check return value
  35. print os.read(master_fd, 1024)
  36.  
  37. os.write(slave_fd, TEST_STRING_2[:5])
  38. os.write(slave_fd, TEST_STRING_2[5:])
  39. print os.read(master_fd, 1024)
  40.  
  41. os.close(slave_fd)
  42. os.close(master_fd)
  43.  
  44. # basic pty passed.
  45.  
  46. debug("calling pty.fork()")
  47. pid, master_fd = pty.fork()
  48. if pid == pty.CHILD:
  49.     ## # Please uncomment these when os.isatty() is added.
  50.     ## if not os.isatty(1):
  51.     ##     debug("Child's fd 1 is not a tty?!")
  52.     ##     os._exit(3)
  53.     try:
  54.         debug("In child, calling os.setsid()")
  55.         os.setsid()
  56.     except OSError:
  57.         # Good, we already were session leader
  58.         debug("OSError was raised.")
  59.         pass
  60.     except AttributeError:
  61.         # Have pty, but not setsid() ?
  62.         debug("AttributeError was raised.")
  63.         pass
  64.     except:
  65.         # We don't want this error to propagate, escape the call to
  66.         # os._exit(), and cause very peculiar behavior in the calling
  67.         # regrtest.py !
  68.         debug("Some other error was raised.")
  69.         os._exit(1)
  70.     else:
  71.         debug("os.setsid() succeeded! (bad!)")
  72.         os._exit(2)
  73.     os._exit(4)
  74. else:
  75.     debug("Waiting for child (%d) to finish."%pid)
  76.     (pid, status) = os.waitpid(pid, 0)
  77.     debug("Child (%d) exited with status %d."%(pid, status))
  78.     if status / 256 == 1:
  79.         raise TestFailed, "Child raised an unexpected exception in os.setsid()"
  80.     elif status / 256 == 2:
  81.         raise TestFailed, "pty.fork() failed to make child a session leader."
  82.     elif status / 256 == 3:
  83.         raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
  84.     elif status / 256 <> 4:
  85.         raise TestFailed, "pty.fork() failed for unknown reasons:"
  86.         print os.read(master_fd, 65536)
  87.  
  88. os.close(master_fd)
  89.  
  90. # pty.fork() passed.
  91.  
  92.