summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoractsofcreation <actsofcreation@656d521f-e311-0410-88e0-e7920216d269>2004-04-24 08:41:55 +0000
committeractsofcreation <actsofcreation@656d521f-e311-0410-88e0-e7920216d269>2004-04-24 08:41:55 +0000
commitbded0add6ac59c992b39565fbbaa3aa71395aac1 (patch)
treeacced206e95013fc346cfa7e02a2a2960a045116
parent25da75a253b334cbf09e666537b5c8be612cdfa5 (diff)
downloadpexpect-patch_877254.tar.gz
Applied patch 877254, implementing the expect_lookahead() methodpatch_877254
git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/branches/patch_877254@227 656d521f-e311-0410-88e0-e7920216d269
-rw-r--r--pexpect/pexpect.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/pexpect/pexpect.py b/pexpect/pexpect.py
index c313f46..b234273 100644
--- a/pexpect/pexpect.py
+++ b/pexpect/pexpect.py
@@ -695,6 +695,33 @@ class spawn:
self.match = None
self.buffer = ''
raise
+
+ def expect_lookahead(self,pattern, timeout = -1):
+ """
+ This function works exactly like the expect() function,
+ taking the same arguments, returning the same results,
+ and causing the same side effects except for one thing.
+ This function leaves all the data on the buffer. It does
+ not strip off everything up to the end of the match.
+ This is useful in situations where you have multiple possible
+ sequences that you may have to interact with, for which you
+ have pexpect proceedures, but you can't know which one applies
+ until you look to see which one is starting. The problem with
+ expect is that once you've looked to see which one is starting,
+ you have no way to use your existing proceedures, because expect
+ strips off of the buffer the very data your proceedures need to
+ get them going.
+ """
+
+ #Do a normal expect process
+ index = self.expect(pattern,timeout)
+ #Put back the part of the buffer stripped off
+ if ( self.match != None ):
+ self.buffer = self.before + self.match.group() + self.buffer
+ else:
+ self.buffer = self.before + self.buffer
+ #Return the index
+ return index
def expect_list(self, pattern_list, timeout = -1):
"""
@@ -739,6 +766,7 @@ class spawn:
except EOF:
self.before = incoming
self.after = EOF
+ self.match = None
if EOF in pattern_list:
#self.buffer = ''
return pattern_list.index(EOF)
@@ -747,6 +775,7 @@ class spawn:
except TIMEOUT:
self.before = incoming
self.after = TIMEOUT
+ self.match = None
if TIMEOUT in pattern_list:
#self.buffer = ''
return pattern_list.index(TIMEOUT)