--- pty.py.orig 2008-01-30 16:49:04.325764000 -0500 +++ pty.py 2008-02-17 15:08:47.352141000 -0500 @@ -6,6 +6,16 @@ # UNIX Environment. Chapter 19. # Author: Steen Lumholt -- with additions by Guido. +# This version modified by Fergus Henderson : +# see pty.py.patch for the changes: +# - spawn() now waits for the invoked process, and returns the value +# returned from os.waitpid(). +# - fixed a bug in the _copy() loop that caused it to spin using 100% CPU +# rather than blocking. +# TODO(fergus): once this patch is incorporated in the upstream python +# distribution, remove this file and use python's built-in 'pty' module +# instead. + from select import select import os import tty @@ -136,15 +146,21 @@ Copies pty master -> standard output (master_read) standard input -> pty master (stdin_read)""" + fds = [master_fd, STDIN_FILENO] while 1: - rfds, wfds, xfds = select( - [master_fd, STDIN_FILENO], [], []) + rfds, wfds, xfds = select(fds, [], []) if master_fd in rfds: data = master_read(master_fd) - os.write(STDOUT_FILENO, data) + if len(data) == 0: # Reached EOF. + fds.remove(master_fd) + else: + os.write(STDOUT_FILENO, data) if STDIN_FILENO in rfds: data = stdin_read(STDIN_FILENO) - _writen(master_fd, data) + if len(data) == 0: # Reached EOF. + fds.remove(STDIN_FILENO) + else: + _writen(master_fd, data) def spawn(argv, master_read=_read, stdin_read=_read): """Create a spawned process.""" @@ -166,3 +182,5 @@ tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode) os.close(master_fd) + (_pid, status) = os.waitpid(pid, 0) + return status