diff options
author | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2003-07-20 18:47:09 +0000 |
---|---|---|
committer | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2003-07-20 18:47:09 +0000 |
commit | a00f3c1d0a0bfc4898b355c2facfb78ff2400b0f (patch) | |
tree | b8c66f8ee16968aa065a4f85654b796fe3e5128a | |
parent | ecc44fabf7ad6bb030b8f21a1afc5292d8796771 (diff) | |
download | pexpect-a00f3c1d0a0bfc4898b355c2facfb78ff2400b0f.tar.gz |
Changes to support multibyte read.
git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@204 656d521f-e311-0410-88e0-e7920216d269
-rw-r--r-- | pexpect/pexpect.py | 57 | ||||
-rwxr-xr-x | pexpect/tests/test_filedescriptor.py | 10 |
2 files changed, 61 insertions, 6 deletions
diff --git a/pexpect/pexpect.py b/pexpect/pexpect.py index bc48ca9..ba4da0b 100644 --- a/pexpect/pexpect.py +++ b/pexpect/pexpect.py @@ -106,6 +106,10 @@ class spawn: self.STDOUT_FILENO = sys.stdout.fileno() self.STDERR_FILENO = sys.stderr.fileno() + self.stdin = sys.stdin + self.stdout = sys.stdout + self.stderr = sys.stderr + ### IMPLEMENT THIS FEATURE!!! #self.maxbuffersize = 10000 # anything before maxsearchsize point is preserved, but not searched. @@ -124,6 +128,11 @@ class spawn: self.name = '' # File-like object. self.flag_eof = 0 + + # Add the ability to read more than one byte from a TTY at a time. + self.buffer = '' + self.maxread = 1 # Maximum to read at a time + # If command is an int type then it must represent an open file descriptor. if type (command) == type(0): try: # Command is an int, so now check if it is a file descriptor. @@ -235,8 +244,18 @@ class spawn: raise ExceptionPexpect ('This file descriptor cannot be closed because it was not created by spawn. The original creator is responsible for closing it.') self.flush() os.close (self.child_fd) +<<<<<<< pexpect.py +<<<<<<< pexpect.py + + pid, status = os.waitpid(self.pid) + +======= + os.waitpid (self.pid, 0) +>>>>>>> 1.79 +======= if wait: os.waitpid (self.pid, 0) +>>>>>>> 1.80 self.child_fd = -1 self.__child_fd_owner = None @@ -266,6 +285,11 @@ class spawn: """ self.log_file = fileobject + def setmaxread (self, maxread): + """This sets the maximum number of bytes to read from a TTY at one time. + """ + self.maxread = maxread + def read_nonblocking (self, size = -1, timeout = None): """ This reads at most size characters from the child application. @@ -618,10 +642,11 @@ class spawn: pattern_list = [pattern_list] try: - incoming = '' + #ED# incoming = '' + incoming = self.buffer while 1: # Keep reading until exception or return. - c = self.read_nonblocking (1, timeout) - incoming = incoming + c + #ED# c = self.read_nonblocking (1, timeout) + #ED# incoming = incoming + c # Sequence through the list of patterns and look for a match. index = -1 @@ -633,12 +658,17 @@ class spawn: if match_index >= 0: self.before = incoming [ : match_index] self.after = incoming [match_index : ] + self.buffer = incoming [match_index + len(str_target):] self.match = None return index + c = self.read_nonblocking (self.maxread, timeout) + incoming = incoming + c + except EOF: if EOF in pattern_list: self.before = incoming self.after = EOF + self.buffer = '' return pattern_list.index(EOF) else: raise @@ -646,6 +676,7 @@ class spawn: if TIMEOUT in pattern_list: self.before = incoming self.after = TIMEOUT + self.buffer = '' return pattern_list.index(TIMEOUT) else: raise @@ -653,6 +684,7 @@ class spawn: self.before = incoming self.after = None self.match = None + self.buffer = '' raise def expect_list(self, pattern_list, timeout = -1): @@ -669,10 +701,11 @@ class spawn: timeout = self.timeout try: - incoming = '' + #ED# incoming = '' + incoming = self.buffer while 1: # Keep reading until exception or return. - c = self.read_nonblocking (1, timeout) - incoming = incoming + c + #ED# c = self.read_nonblocking (1, timeout) + #ED# incoming = incoming + c # Sequence through the list of patterns and look for a match. index = -1 @@ -685,11 +718,17 @@ class spawn: self.before = incoming[ : match.start()] self.after = incoming[match.start() : ] self.match = match + self.buffer = incoming[match.end() : ] return index + # Read more data + c = self.read_nonblocking (self.maxread, timeout) + incoming = incoming + c + except EOF: if EOF in pattern_list: self.before = incoming self.after = EOF + self.buffer = '' return pattern_list.index(EOF) else: raise @@ -697,6 +736,7 @@ class spawn: if TIMEOUT in pattern_list: self.before = incoming self.after = TIMEOUT + self.buffer = '' return pattern_list.index(TIMEOUT) else: raise @@ -704,6 +744,7 @@ class spawn: self.before = incoming self.after = None self.match = None + self.buffer = '' raise def getwinsize(self): @@ -751,6 +792,10 @@ class spawn: This simply echos the child stdout and child stderr to the real stdout and it echos the real stdin to the child stdin. """ + # Flush the buffer. + self.stdout.write (self.buffer) + self.buffer = '' + self.stdout.flush() mode = tty.tcgetattr(self.STDIN_FILENO) tty.setraw(self.STDIN_FILENO) try: diff --git a/pexpect/tests/test_filedescriptor.py b/pexpect/tests/test_filedescriptor.py index 5509dc9..1634d2e 100755 --- a/pexpect/tests/test_filedescriptor.py +++ b/pexpect/tests/test_filedescriptor.py @@ -13,6 +13,16 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase): s.expect (pexpect.EOF) assert s.before == ' END\n' + + def test_setmaxread (self): + fd = os.open ('TESTDATA.txt', os.O_RDONLY) + s = pexpect.spawn (fd) + s.setmaxread(100) + s.expect('2') + s.expect ('This is the end of test data:') + s.expect (pexpect.EOF) + assert s.before == ' END\n' + def test_fd_isalive (self): fd = os.open ('TESTDATA.txt', os.O_RDONLY) s = pexpect.spawn (fd) |