def test_basic_pty(): try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name)) debug("Calling slave_open(%r)"%(slave_name,)) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'"%slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise TestSkipped, "Pseudo-terminals (seemingly) not functional." 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) s1 = os.read(master_fd, 1024) sys.stdout.write(normalize_output(s1)) debug("Writing chunked output") os.write(slave_fd, TEST_STRING_2[:5]) os.write(slave_fd, TEST_STRING_2[5:]) s2 = os.read(master_fd, 1024) sys.stdout.write(normalize_output(s2)) os.close(slave_fd) os.close(master_fd)
def test_basic_pty(): try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'" % (master_fd, slave_name)) debug("Calling slave_open(%r)" % (slave_name, )) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'" % slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise TestSkipped, "Pseudo-terminals (seemingly) not functional." if not os.isatty(slave_fd) and sys.platform not in fickle_isatty: raise TestFailed, "slave_fd is not a tty" # 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) sys.stdout.write(s1.replace("\r\n", "\n")) debug("Writing chunked output") os.write(slave_fd, TEST_STRING_2[:5]) os.write(slave_fd, TEST_STRING_2[5:]) s2 = os.read(master_fd, 1024) sys.stdout.write(s2.replace("\r\n", "\n")) os.close(slave_fd) os.close(master_fd)
def test_basic(self): try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'" % (master_fd, slave_name)) debug("Calling slave_open(%r)" % (slave_name, )) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'" % slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise unittest.SkipTest, "Pseudo-terminals (seemingly) not functional." self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty') # Solaris requires reading the fd before anything is returned. # My guess is that since we open and close the slave fd # in master_open(), we need to read the EOF. # Ensure the fd is non-blocking in case there's nothing to read. orig_flags = fcntl.fcntl(master_fd, fcntl.F_GETFL) fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags | os.O_NONBLOCK) try: s1 = os.read(master_fd, 1024) self.assertEqual('', s1) except OSError, e: if e.errno != errno.EAGAIN: raise
def test_basic(self): try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'" % (master_fd, slave_name)) debug("Calling slave_open(%r)" % (slave_name,)) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'" % slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise TestSkipped, "Pseudo-terminals (seemingly) not functional." self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty') # Solaris requires reading the fd before anything is returned. # My guess is that since we open and close the slave fd # in master_open(), we need to read the EOF. # Ensure the fd is non-blocking in case there's nothing to read. orig_flags = fcntl.fcntl(master_fd, fcntl.F_GETFL) fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags | os.O_NONBLOCK) try: s1 = os.read(master_fd, 1024) self.assertEquals('', s1) except OSError, e: if e.errno != errno.EAGAIN: raise
def test_basic_pty(): try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name)) debug("Calling slave_open(%r)"%(slave_name,)) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'"%slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise TestSkipped, "Pseudo-terminals (seemingly) not functional." if not os.isatty(slave_fd) and sys.platform not in fickle_isatty: raise TestFailed, "slave_fd is not a tty" # 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) sys.stdout.write(s1.replace("\r\n", "\n")) debug("Writing chunked output") os.write(slave_fd, TEST_STRING_2[:5]) os.write(slave_fd, TEST_STRING_2[5:]) s2 = os.read(master_fd, 1024) sys.stdout.write(s2.replace("\r\n", "\n")) os.close(slave_fd) os.close(master_fd)
def test_basic_pty(): try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'" % (master_fd, slave_name)) debug("Calling slave_open(%r)" % (slave_name, )) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'" % slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise TestSkipped, "Pseudo-terminals (seemingly) not functional." 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) s1 = os.read(master_fd, 1024) sys.stdout.write(normalize_output(s1)) debug("Writing chunked output") os.write(slave_fd, TEST_STRING_2[:5]) os.write(slave_fd, TEST_STRING_2[5:]) s2 = os.read(master_fd, 1024) sys.stdout.write(normalize_output(s2)) os.close(slave_fd) os.close(master_fd)
def test_basic(self): try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'" % (master_fd, slave_name)) debug("Calling slave_open(%r)" % (slave_name, )) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'" % slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise unittest.SkipTest( "Pseudo-terminals (seemingly) not functional.") self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty') # Solaris requires reading the fd before anything is returned. # My guess is that since we open and close the slave fd # in master_open(), we need to read the EOF. # Ensure the fd is non-blocking in case there's nothing to read. blocking = os.get_blocking(master_fd) try: os.set_blocking(master_fd, False) try: s1 = os.read(master_fd, 1024) self.assertEqual(b'', s1) except OSError as e: if e.errno != errno.EAGAIN: raise finally: # Restore the original flags. os.set_blocking(master_fd, blocking) debug("Writing to slave_fd") os.write(slave_fd, TEST_STRING_1) s1 = _readline(master_fd) self.assertEqual(b'I wish to buy a fish license.\n', normalize_output(s1)) debug("Writing chunked output") os.write(slave_fd, TEST_STRING_2[:5]) os.write(slave_fd, TEST_STRING_2[5:]) s2 = _readline(master_fd) self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2)) os.close(slave_fd) # closing master_fd can raise a SIGHUP if the process is # the session leader: we installed a SIGHUP signal handler # to ignore this signal. os.close(master_fd)
def test_basic(self): try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'" % (master_fd, slave_name)) debug("Calling slave_open(%r)" % (slave_name,)) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'" % slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise unittest.SkipTest("Pseudo-terminals (seemingly) not functional.") self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty') # Solaris requires reading the fd before anything is returned. # My guess is that since we open and close the slave fd # in master_open(), we need to read the EOF. # Ensure the fd is non-blocking in case there's nothing to read. blocking = os.get_blocking(master_fd) try: os.set_blocking(master_fd, False) try: s1 = os.read(master_fd, 1024) self.assertEqual(b'', s1) except OSError as e: if e.errno != errno.EAGAIN: raise finally: # Restore the original flags. os.set_blocking(master_fd, blocking) debug("Writing to slave_fd") os.write(slave_fd, TEST_STRING_1) s1 = _readline(master_fd) self.assertEqual(b'I wish to buy a fish license.\n', normalize_output(s1)) debug("Writing chunked output") os.write(slave_fd, TEST_STRING_2[:5]) os.write(slave_fd, TEST_STRING_2[5:]) s2 = _readline(master_fd) self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2)) os.close(slave_fd) os.close(master_fd)
def test_basic(self): try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'" % (master_fd, slave_name)) debug("Calling slave_open(%r)" % (slave_name, )) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'" % slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise unittest.SkipTest( "Pseudo-terminals (seemingly) not functional.") self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty') # Solaris requires reading the fd before anything is returned. # My guess is that since we open and close the slave fd # in master_open(), we need to read the EOF. # Ensure the fd is non-blocking in case there's nothing to read. orig_flags = fcntl.fcntl(master_fd, fcntl.F_GETFL) fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags | os.O_NONBLOCK) try: s1 = os.read(master_fd, 1024) self.assertEqual(b'', s1) except OSError as e: if e.errno != errno.EAGAIN: raise # Restore the original flags. fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags) debug("Writing to slave_fd") os.write(slave_fd, TEST_STRING_1) s1 = os.read(master_fd, 1024) self.assertEqual(b'I wish to buy a fish license.\n', normalize_output(s1)) debug("Writing chunked output") os.write(slave_fd, TEST_STRING_2[:5]) os.write(slave_fd, TEST_STRING_2[5:]) s2 = os.read(master_fd, 1024) self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2)) os.close(slave_fd) os.close(master_fd)
def start_serial(self,var): """Create a pseudo serial port pair, and start listening for data on the server port. The slave port name is declared in an environment variable var, so drvAsynSerialPortConfigure should connect to $(var)""" if self.started: self.diagnostic("Server already started") else: self.inq, self.outq = Queue.Queue(), Queue.Queue() self.started = True # create a pseudo serial port, open slave port so listening doesn't fail self.mfd,sname = pty.master_open() self.sfd = pty.slave_open(sname) # start to listen on the master port self.__daemon(self.__serial_in) # start to respond to any messages put on the outq self.__daemon(self.__serial_out) # start the worker thread self.__daemon(self.__process) # put the portname for the IOC to connect to in var os.environ[var] = sname
def start_serial(self, var): """Create a pseudo serial port pair, and start listening for data on the server port. The slave port name is declared in an environment variable var, so drvAsynSerialPortConfigure should connect to $(var)""" if self.started: self.diagnostic("Server already started") else: self.inq, self.outq = Queue.Queue(), Queue.Queue() self.started = True # create a pseudo serial port, open slave port so listening doesn't fail self.mfd, sname = pty.master_open() self.sfd = pty.slave_open(sname) # start to listen on the master port self.__daemon(self.__serial_in) # start to respond to any messages put on the outq self.__daemon(self.__serial_out) # start the worker thread self.__daemon(self.__process) # put the portname for the IOC to connect to in var os.environ[var] = sname
def test_basic(self): try: debug('Calling master_open()') master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'" % (master_fd, slave_name)) debug('Calling slave_open(%r)' % (slave_name, )) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'" % slave_fd) except OSError: raise unittest.SkipTest( 'Pseudo-terminals (seemingly) not functional.') self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty') blocking = os.get_blocking(master_fd) try: os.set_blocking(master_fd, False) try: s1 = os.read(master_fd, 1024) self.assertEqual(b'', s1) except OSError as e: if e.errno != errno.EAGAIN: raise finally: os.set_blocking(master_fd, blocking) debug('Writing to slave_fd') os.write(slave_fd, TEST_STRING_1) s1 = os.read(master_fd, 1024) self.assertEqual(b'I wish to buy a fish license.\n', normalize_output(s1)) debug('Writing chunked output') os.write(slave_fd, TEST_STRING_2[:5]) os.write(slave_fd, TEST_STRING_2[5:]) s2 = os.read(master_fd, 1024) self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2)) os.close(slave_fd) os.close(master_fd)
import pty, os, sys from test_support import verbose, TestFailed, TestSkipped 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): print msg else: def debug(msg): pass # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing # because pty code is not too portable. try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name)) debug("Calling slave_open(%s)"%`slave_name`) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'"%slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise TestSkipped, "Pseudo-terminals (seemingly) not functional." if not os.isatty(slave_fd): raise TestFailed, "slave_fd is not a tty" # 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) sys.stdout.write(s1.replace("\r\n", "\n"))
if verbose: def debug(msg): print msg else: def debug(msg): pass # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing # because pty code is not too portable. try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'" % (master_fd, slave_name)) debug("Calling slave_open(%s)" % ` slave_name `) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'" % slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise TestSkipped, "Pseudo-terminals (seemingly) not functional." if not os.isatty(slave_fd): raise TestFailed, "slave_fd is not a tty" # 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")
def setup_pty( self, _use_pty ): self.using_pty = _use_pty if _use_pty: ## The lower this number is the more responsive some commands ## may be ( printing prompt, ls ), but also the quicker others ## may timeout reading their output ( ping, ftp ) self.delay = 0.2 ## Hack to get pty name until I can figure out to get name ## of slave pty using pty.fork() I've tried everything ## including using all of the python src for pty.fork(). ## I'm probably trying to do something I can't do. However, ## there does seem to be a std call named ptsname() which ## returns the slave pty name i.e. /dev/pty/XX ## Assumption is, that between the dummy call to ## master_open is done and the pty.fork happens, we'll be ## the next pty entry after the one from pty.master_open() ## According to SysV docs it will look for the first ## unused, so this shouldn't be too bad besides its looks. ## Only have to make sure they're not already in use and ## if it is try the next one etc. self.master, pty_name = pty.master_open() dbg_print ( 'setup_pty: slave pty name is ' + pty_name ) self.pid, self.fd = pty.fork() ## pid and fd are of child shell self.outd = self.fd self.ind = self.fd self.errd = self.fd signal.signal( signal.SIGCHLD, self.sigchld_handler ) if self.pid == 0: ## In spawned shell process. NOTE: any 'print'ing done within here will corrupt vim. attrs = tty.tcgetattr( 1 ) attrs[ 6 ][ tty.VMIN ] = 1 attrs[ 6 ][ tty.VTIME ] = 0 attrs[ 0 ] = attrs[ 0 ] | tty.BRKINT attrs[ 0 ] = attrs[ 0 ] | tty.IGNBRK attrs[ 3 ] = attrs[ 3 ] & ~tty.ICANON & ~tty.ECHO tty.tcsetattr( 1, tty.TCSANOW, attrs ) try: args = [ self.sh ] if self.arg != '': args.append( self.arg ) os.execv( self.sh, args ) except: print( 'setup_pty: Couldn\'t start specified shell ' + self.sh ) else: try: attrs = tty.tcgetattr( 1 ) termios_keys = attrs[ 6 ] ## Get *real* key-sequence for standard input keys, i.e. EOF self.intr_key = termios_keys[ tty.VINTR ] self.eof_key = termios_keys[ tty.VEOF ] except: dbg_print ( 'setup_pty: tcgetattr failed' ) else: ## Use pipes on Win32. not as reliable/nice. works OK but with limitations. self.delay = 0.2 p = Popen( self.sh + ' ' + self.arg, shell = False, bufsize = 32, stdin = PIPE, stdout = PIPE, stderr = PIPE, creationflags = win32process.CREATE_NO_WINDOW | win32process.CREATE_NEW_PROCESS_GROUP ) ( self.stdin, self.stdout, self.stderr ) = ( p.stdin, p.stdout, p.stderr ) self.outd = self.stdout.fileno() self.ind = self.stdin.fileno () self.errd = self.stderr.fileno() self.pid = p.pid dbg_print ( 'setup_pty: self.outd: ' + str( self.outd ) ) dbg_print ( 'setup_pty: self.ind: ' + str( self.ind ) ) dbg_print ( 'setup_pty: self.errd: ' + str( self.errd ) ) dbg_print ( 'setup_pty: pid is ' + str( self.pid ) )
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): print msg else: def debug(msg): pass # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing # because pty code is not too portable. try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name)) debug("Calling slave_open(%s)"%`slave_name`) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'"%slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise TestSkipped, "Pseudo-terminals (seemingly) not functional." if not os.isatty(slave_fd): raise TestFailed, "slave_fd is not a tty" # 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")
def setup_pty( self, _use_pty ): self.using_pty = _use_pty if _use_pty: ## The lower this number is the more responsive some commands ## may be ( printing prompt, ls ), but also the quicker others ## may timeout reading their output ( ping, ftp ) self.delay = 0.1 ## Hack to get pty name until I can figure out to get name ## of slave pty using pty.fork( ) I've tried everything ## including using all of the python src for pty.fork( ). ## I'm probably trying to do something I can't do. However, ## there does seem to be a std call named ptsname( ) which ## returns the slave pty name i.e. /dev/pty/XX ## Assumption is, that between the dummy call to ## master_open is done and the pty.fork happens, we'll be ## the next pty entry after the one from pty.master_open( ) ## According to SysV docs it will look for the first ## unused, so this shouldn't be too bad besides its looks. ## Only have to make sure they're not already in use and ## if it is try the next one etc. self.master, pty_name = pty.master_open( ) dbg_print ( 'setup_pty: Dummy pty name is ' + pty_name ) ## On Linux pty name will be pts/X where X is between 0 and N ## On Solaris pty name will be ttypX where X is between 0 and N ## So for these cases it's safe to clip the trailing number. m = re.search( r'\d*$', pty_name ) self.cur_pty = int( m.group( 0 ) ) + 1 dbg_print ( 'setup_pty: Next pty num is ' + `self.cur_pty` ) ## TODO: Check to see if it's already in use, and keep ## bumping number until we find the one not in use entries = os.listdir( "/dev/pts" ) if entries: ## just in case, don't most *nix have /dev? while 1: if str( self.cur_pty ) in entries: self.cur_pty += 1 else: dbg_print ( 'setup_pty: Found a non-matching pty ' + `self.cur_pty` + ' using it' ) break else: dbg_print( "setup_pty: platform '" + sys.platform + "' doesn't seem to have pty entries in standard location(s)." ) self.pid, self.fd = pty.fork( ) self.outd = self.fd self.ind = self.fd self.errd = self.fd if self.pid == 0: ## In spawned shell process attrs = tty.tcgetattr( 1 ) attrs[6][tty.VMIN] = 1 attrs[6][tty.VTIME] = 0 attrs[0] = attrs[0] | tty.BRKINT attrs[3] = attrs[3] & ~tty.ICANON & ~tty.ECHO tty.tcsetattr( 1, tty.TCSANOW, attrs ) os.execv( self.sh, [ self.sh, self.arg ] ) else: attrs = tty.tcgetattr( 1 ) termios_keys = attrs[ 6 ] ## Get *real* key-sequence for standard input keys, i.e. EOF self.eof_key = termios_keys[ tty.VEOF ] self.eol_key = termios_keys[ tty.VEOL ] self.erase_key = termios_keys[ tty.VERASE ] self.intr_key = termios_keys[ tty.VINTR ] self.kill_key = termios_keys[ tty.VKILL ] self.susp_key = termios_keys[ tty.VSUSP ] else: ## Use pipes. not as reliable/nice. works OK but with limitations. ## Needed for Windows support. self.delay = 0.2 self.stdout, self.stdin, self.stderr = popen2.popen3( self.sh + " " + self.arg, bufsize = -1 ) self.outd = self.stdout.fileno( ) self.ind = self.stdin.fileno ( ) self.errd = self.stderr.fileno( ) self.intr_key = '' self.eof_key = ''
def setup_pty( self, _use_pty ): self.using_pty = _use_pty if _use_pty: ## The lower this number is the more responsive some commands ## may be ( printing prompt, ls ), but also the quicker others ## may timeout reading their output ( ping, ftp ) self.delay = 0.2 ## Hack to get pty name until I can figure out to get name ## of slave pty using pty.fork() I've tried everything ## including using all of the python src for pty.fork(). ## I'm probably trying to do something I can't do. However, ## there does seem to be a std call named ptsname() which ## returns the slave pty name i.e. /dev/pty/XX ## Assumption is, that between the dummy call to ## master_open is done and the pty.fork happens, we'll be ## the next pty entry after the one from pty.master_open() ## According to SysV docs it will look for the first ## unused, so this shouldn't be too bad besides its looks. ## Only have to make sure they're not already in use and ## if it is try the next one etc. self.master, pty_name = pty.master_open() dbg_print ( 'setup_pty: slave pty name is ' + pty_name ) self.pid, self.fd = pty.fork() self.outd = self.fd self.ind = self.fd self.errd = self.fd signal.signal( signal.SIGCHLD, self.sigchld_handler ) if self.pid == 0: ## In spawned shell process, NOTE: any 'print'ing done within ## here will corrupt vim. attrs = tty.tcgetattr( 1 ) attrs[ 6 ][ tty.VMIN ] = 1 attrs[ 6 ][ tty.VTIME ] = 0 attrs[ 0 ] = attrs[ 0 ] | tty.BRKINT attrs[ 0 ] = attrs[ 0 ] & tty.IGNBRK attrs[ 3 ] = attrs[ 3 ] & ~tty.ICANON & ~tty.ECHO tty.tcsetattr( 1, tty.TCSANOW, attrs ) try: if self.arg != '': os.execv( self.sh, [ self.sh, self.arg ] ) else: os.execv( self.sh, [ self.sh, ] ) except: print( 'setup_pty: Couldn\'t start specified shell ' + self.sh ) return else: try: attrs = tty.tcgetattr( 1 ) termios_keys = attrs[ 6 ] except: dbg_print ( 'setup_pty: tcgetattr failed' ) return ## Get *real* key-sequence for standard input keys, i.e. EOF self.eof_key = termios_keys[ tty.VEOF ] self.intr_key = termios_keys[ tty.VINTR ] else: ## Use pipes on Win32. not as reliable/nice. works OK but with limitations. self.delay = 0.2 self.stdout, self.stdin, self.stderr = popen2.popen3( self.sh + ' ' + self.arg, -1, 'b' ) self.outd = self.stdout.fileno() self.ind = self.stdin.fileno () self.errd = self.stderr.fileno() dbg_print ( 'setup_pty: self.outd: ' + str( self.outd ) ) dbg_print ( 'setup_pty: self.ind: ' + str( self.ind ) ) dbg_print ( 'setup_pty: self.errd: ' + str( self.errd ) ) self.eof_key = '' self.intr_key = '' windll.kernel32.AttachConsole( -1 ) ## ATTACH_PARENT_PROCESS self.pid = os.getpid()
import pty, os, sys from test_support import verbose, TestFailed, TestSkipped 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): print msg else: def debug(msg): pass # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing # because pty code is not too portable. try: debug("Calling master_open()") master_fd, slave_name = pty.master_open() debug("Got master_fd '%d', slave_name '%s'" % (master_fd, slave_name)) debug("Calling slave_open(%s)" % ` slave_name `) slave_fd = pty.slave_open(slave_name) debug("Got slave_fd '%d'" % slave_fd) except OSError: # " An optional feature could not be imported " ... ? raise TestSkipped, "Pseudo-terminals (seemingly) not functional." if not os.isatty(slave_fd): raise TestFailed, "slave_fd is not a tty" # 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)
def setup_pty( self, _use_pty ): self.using_pty = _use_pty if _use_pty: ## The lower this number is the more responsive some commands ## may be ( printing prompt, ls ), but also the quicker others ## may timeout reading their output ( ping, ftp ) self.delay = 0.1 ## Hack to get pty name until I can figure out to get name ## of slave pty using pty.fork() I've tried everything ## including using all of the python src for pty.fork(). ## I'm probably trying to do something I can't do. However, ## there does seem to be a std call named ptsname() which ## returns the slave pty name i.e. /dev/pty/XX ## Assumption is, that between the dummy call to ## master_open is done and the pty.fork happens, we'll be ## the next pty entry after the one from pty.master_open() ## According to SysV docs it will look for the first ## unused, so this shouldn't be too bad besides its looks. ## Only have to make sure they're not already in use and ## if it is try the next one etc. self.master, pty_name = pty.master_open() dbg_print ( 'setup_pty: slave pty name is ' + pty_name ) self.pid, self.fd = pty.fork() self.outd = self.fd self.ind = self.fd self.errd = self.fd signal.signal( signal.SIGCHLD, self.sigchld_handler ) if self.pid == 0: ## In spawned shell process, NOTE: any 'print'ing done within ## here will corrupt vim. attrs = tty.tcgetattr( 1 ) attrs[ 6 ][ tty.VMIN ] = 1 attrs[ 6 ][ tty.VTIME ] = 0 attrs[ 0 ] = attrs[ 0 ] | tty.BRKINT attrs[ 0 ] = attrs[ 0 ] & tty.IGNBRK attrs[ 3 ] = attrs[ 3 ] & ~tty.ICANON & ~tty.ECHO tty.tcsetattr( 1, tty.TCSANOW, attrs ) if self.arg != '': os.execv( self.sh, [ self.sh, self.arg ] ) else: os.execv( self.sh, [ self.sh, ] ) else: try: attrs = tty.tcgetattr( 1 ) termios_keys = attrs[ 6 ] except: dbg_print ( 'setup_pty: tcgetattr failed' ) return # Get *real* key-sequence for standard input keys, i.e. EOF self.eof_key = termios_keys[ tty.VEOF ] self.eol_key = termios_keys[ tty.VEOL ] self.erase_key = termios_keys[ tty.VERASE ] self.intr_key = termios_keys[ tty.VINTR ] self.kill_key = termios_keys[ tty.VKILL ] self.susp_key = termios_keys[ tty.VSUSP ] else: ## Use pipes on Win32. not as reliable/nice. works OK but with limitations. self.delay = 0.2 try: import win32pipe dbg_print ( 'setup_pty: using windows extensions' ) self.stdin, self.stdout, self.stderr = win32pipe.popen3( self.sh + " " + self.arg ) except ImportError: dbg_print ( 'setup_pty: not using windows extensions' ) self.stdout, self.stdin, self.stderr = popen2.popen3( self.sh + " " + self.arg, -1, 'b' ) self.outd = self.stdout.fileno() self.ind = self.stdin.fileno () self.errd = self.stderr.fileno() self.intr_key = '' self.eof_key = ''