mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									053ae3502c
								
							
						
					
					
						commit
						b0dbeef1c4
					
				
					 1 changed files with 37 additions and 25 deletions
				
			
		| 
						 | 
					@ -1,10 +1,8 @@
 | 
				
			||||||
import pty, os, sys
 | 
					import pty, os, sys
 | 
				
			||||||
from test_support import verbose, TestFailed, TestSkipped
 | 
					from test_support import verbose, TestFailed, TestSkipped
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEST_STRING_1 = "I wish to buy a fish license."
 | 
					TEST_STRING_1 = "I wish to buy a fish license.\n"
 | 
				
			||||||
TEST_STRING_2 = "For my pet fish, Eric."
 | 
					TEST_STRING_2 = "For my pet fish, Eric.\n"
 | 
				
			||||||
TEST_STRING_3 = "And now for something completely different..."
 | 
					 | 
				
			||||||
TEST_STRING_4 = "but you pronounce it throatwobbler mangrove."
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
if verbose:
 | 
					if verbose:
 | 
				
			||||||
    def debug(msg):
 | 
					    def debug(msg):
 | 
				
			||||||
| 
						 | 
					@ -30,13 +28,23 @@ def debug(msg):
 | 
				
			||||||
if not os.isatty(slave_fd):
 | 
					if not os.isatty(slave_fd):
 | 
				
			||||||
    raise TestFailed, "slave_fd is not a tty"
 | 
					    raise TestFailed, "slave_fd is not a tty"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
debug("Writing to slave_fd")
 | 
					# IRIX apparently turns \n into \r\n. Allow that, but avoid allowing other
 | 
				
			||||||
os.write(slave_fd, TEST_STRING_1) # should check return value
 | 
					# differences (like extra whitespace, trailing garbage, etc.)
 | 
				
			||||||
print os.read(master_fd, 1024)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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])
 | 
				
			||||||
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(slave_fd)
 | 
				
			||||||
os.close(master_fd)
 | 
					os.close(master_fd)
 | 
				
			||||||
| 
						 | 
					@ -46,26 +54,30 @@ def debug(msg):
 | 
				
			||||||
debug("calling pty.fork()")
 | 
					debug("calling pty.fork()")
 | 
				
			||||||
pid, master_fd = pty.fork()
 | 
					pid, master_fd = pty.fork()
 | 
				
			||||||
if pid == pty.CHILD:
 | 
					if pid == pty.CHILD:
 | 
				
			||||||
    ## # Please uncomment these when os.isatty() is added.
 | 
					    # stdout should be connected to a tty.
 | 
				
			||||||
    ## if not os.isatty(1):
 | 
					    if not os.isatty(1):
 | 
				
			||||||
    ##     debug("Child's fd 1 is not a tty?!")
 | 
					        debug("Child's fd 1 is not a tty?!")
 | 
				
			||||||
    ##     os._exit(3)
 | 
					        os._exit(3)
 | 
				
			||||||
    try:
 | 
					
 | 
				
			||||||
 | 
					    # After pty.fork(), the child should already be a session leader.
 | 
				
			||||||
 | 
					    # (on those systems that have that concept.) 
 | 
				
			||||||
    debug("In child, calling os.setsid()")
 | 
					    debug("In child, calling os.setsid()")
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
        os.setsid()
 | 
					        os.setsid()
 | 
				
			||||||
    except OSError:
 | 
					    except OSError:
 | 
				
			||||||
        # Good, we already were session leader
 | 
					        # Good, we already were session leader
 | 
				
			||||||
        debug("OSError was raised.")
 | 
					        debug("Good: OSError was raised.")
 | 
				
			||||||
        pass
 | 
					        pass
 | 
				
			||||||
    except AttributeError:
 | 
					    except AttributeError:
 | 
				
			||||||
        # Have pty, but not setsid() ?
 | 
					        # Have pty, but not setsid() ?
 | 
				
			||||||
        debug("AttributeError was raised.")
 | 
					        debug("No setsid() available ?")
 | 
				
			||||||
        pass
 | 
					        pass
 | 
				
			||||||
    except:
 | 
					    except:
 | 
				
			||||||
        # We don't want this error to propagate, escape the call to
 | 
					        # We don't want this error to propagate, escaping the call to
 | 
				
			||||||
        # os._exit(), and cause very peculiar behavior in the calling
 | 
					        # os._exit() and causing very peculiar behavior in the calling
 | 
				
			||||||
        # regrtest.py !
 | 
					        # regrtest.py !
 | 
				
			||||||
        debug("Some other error was raised.")
 | 
					        # Note: could add traceback printing here.
 | 
				
			||||||
 | 
					        debug("An unexpected error was raised.")
 | 
				
			||||||
        os._exit(1)
 | 
					        os._exit(1)
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        debug("os.setsid() succeeded! (bad!)")
 | 
					        debug("os.setsid() succeeded! (bad!)")
 | 
				
			||||||
| 
						 | 
					@ -74,16 +86,16 @@ def debug(msg):
 | 
				
			||||||
else:
 | 
					else:
 | 
				
			||||||
    debug("Waiting for child (%d) to finish."%pid)
 | 
					    debug("Waiting for child (%d) to finish."%pid)
 | 
				
			||||||
    (pid, status) = os.waitpid(pid, 0)
 | 
					    (pid, status) = os.waitpid(pid, 0)
 | 
				
			||||||
    debug("Child (%d) exited with status %d."%(pid, status))
 | 
					    res = status / 256
 | 
				
			||||||
    if status / 256 == 1:
 | 
					    debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
 | 
				
			||||||
 | 
					    if res == 1:
 | 
				
			||||||
        raise TestFailed, "Child raised an unexpected exception in os.setsid()"
 | 
					        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."
 | 
					        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"
 | 
					        raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
 | 
				
			||||||
    elif status / 256 != 4:
 | 
					    elif res != 4:
 | 
				
			||||||
        raise TestFailed, "pty.fork() failed for unknown reasons:"
 | 
					        raise TestFailed, "pty.fork() failed for unknown reasons."
 | 
				
			||||||
        print os.read(master_fd, 65536)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
os.close(master_fd)
 | 
					os.close(master_fd)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue