Allow the process of reading back what we wrote to a pty to transform
linefeeds into carriagereturn-linefeeds (which is apparently what IRIX
does.) Also add some comments, an extra test and reorganize it a bit.
diff --git a/Lib/test/test_pty.py b/Lib/test/test_pty.py
index 2a2d818..9b95754 100644
--- a/Lib/test/test_pty.py
+++ b/Lib/test/test_pty.py
@@ -1,10 +1,8 @@
 import pty, os, sys
 from test_support import verbose, TestFailed, TestSkipped
 
-TEST_STRING_1 = "I wish to buy a fish license."
-TEST_STRING_2 = "For my pet fish, Eric."
-TEST_STRING_3 = "And now for something completely different..."
-TEST_STRING_4 = "but you pronounce it throatwobbler mangrove."
+TEST_STRING_1 = "I wish to buy a fish license.\n"
+TEST_STRING_2 = "For my pet fish, Eric.\n"
 
 if verbose:
     def debug(msg):
@@ -30,13 +28,23 @@
 if not os.isatty(slave_fd):
     raise TestFailed, "slave_fd is not a tty"
 
-debug("Writing to slave_fd")
-os.write(slave_fd, TEST_STRING_1) # should check return value
-print os.read(master_fd, 1024)
+# IRIX apparently turns \n into \r\n. Allow that, but avoid allowing other
+# differences (like extra whitespace, trailing garbage, etc.)
 
+debug("Writing to slave_fd")
+os.write(slave_fd, TEST_STRING_1)
+s1 = os.read(master_fd, 1024)
+if s1[-2:] == "\r\n":
+    s1 = s1[:-2] + "\n"
+sys.stdout.write(s1)
+
+debug("Writing chunked output")
 os.write(slave_fd, TEST_STRING_2[:5])
 os.write(slave_fd, TEST_STRING_2[5:])
-print os.read(master_fd, 1024)
+s2 = os.read(master_fd, 1024)
+if s2[-2:] == "\r\n":
+    s2 = s2[:-2] + "\n"
+sys.stdout.write(s2)
 
 os.close(slave_fd)
 os.close(master_fd)
@@ -46,26 +54,30 @@
 debug("calling pty.fork()")
 pid, master_fd = pty.fork()
 if pid == pty.CHILD:
-    ## # Please uncomment these when os.isatty() is added.
-    ## if not os.isatty(1):
-    ##     debug("Child's fd 1 is not a tty?!")
-    ##     os._exit(3)
+    # stdout should be connected to a tty.
+    if not os.isatty(1):
+        debug("Child's fd 1 is not a tty?!")
+        os._exit(3)
+
+    # After pty.fork(), the child should already be a session leader.
+    # (on those systems that have that concept.) 
+    debug("In child, calling os.setsid()")
     try:
-        debug("In child, calling os.setsid()")
         os.setsid()
     except OSError:
         # Good, we already were session leader
-        debug("OSError was raised.")
+        debug("Good: OSError was raised.")
         pass
     except AttributeError:
         # Have pty, but not setsid() ?
-        debug("AttributeError was raised.")
+        debug("No setsid() available ?")
         pass
     except:
-        # We don't want this error to propagate, escape the call to
-        # os._exit(), and cause very peculiar behavior in the calling
+        # We don't want this error to propagate, escaping the call to
+        # os._exit() and causing very peculiar behavior in the calling
         # regrtest.py !
-        debug("Some other error was raised.")
+        # Note: could add traceback printing here.
+        debug("An unexpected error was raised.")
         os._exit(1)
     else:
         debug("os.setsid() succeeded! (bad!)")
@@ -74,16 +86,16 @@
 else:
     debug("Waiting for child (%d) to finish."%pid)
     (pid, status) = os.waitpid(pid, 0)
-    debug("Child (%d) exited with status %d."%(pid, status))
-    if status / 256 == 1:
+    res = status / 256
+    debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
+    if res == 1:
         raise TestFailed, "Child raised an unexpected exception in os.setsid()"
-    elif status / 256 == 2:
+    elif res == 2:
         raise TestFailed, "pty.fork() failed to make child a session leader."
-    elif status / 256 == 3:
+    elif res == 3:
         raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
-    elif status / 256 != 4:
-        raise TestFailed, "pty.fork() failed for unknown reasons:"
-        print os.read(master_fd, 65536)
+    elif res != 4:
+        raise TestFailed, "pty.fork() failed for unknown reasons."
 
 os.close(master_fd)