diff options
| author | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2007-05-30 16:39:12 +0000 |
|---|---|---|
| committer | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2007-05-30 16:39:12 +0000 |
| commit | 1a89c87fd99db074efd8bcc694a0b0ec0dcf2a93 (patch) | |
| tree | 360e4ded0aa89cd6cdabe82f4a1195104cc40191 /pexpect | |
| parent | 6e8f6f759424fd794337fd5c485ff86bd15ff9bf (diff) | |
| download | pexpect-1a89c87fd99db074efd8bcc694a0b0ec0dcf2a93.tar.gz | |
Added expect_exact back in! For Andrew Stone.
Diffstat (limited to 'pexpect')
| -rw-r--r-- | pexpect/pexpect.py | 48 | ||||
| -rwxr-xr-x | pexpect/tests/test_expect.py | 39 |
2 files changed, 51 insertions, 36 deletions
diff --git a/pexpect/pexpect.py b/pexpect/pexpect.py index 9786282..ba822e3 100644 --- a/pexpect/pexpect.py +++ b/pexpect/pexpect.py @@ -1139,7 +1139,9 @@ class spawn (object): """This compiles a pattern-string or a list of pattern-strings. Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or a list of - those. Patterns may also be None which results in an empty list. + those. Patterns may also be None which results in an empty list (you + might do this if waiting for an EOF or TIMEOUT condition without + expecting any pattern). This is used by expect() when calling expect_list(). Thus expect() is nothing more than:: @@ -1184,13 +1186,14 @@ class spawn (object): def expect(self, pattern, timeout = -1, searchwindowsize=None): """This seeks through the stream until a pattern is matched. The - pattern is overloaded and may take several types including a list. The - pattern can be a StringType, EOF, a compiled re, or a list of those - types. Strings will be compiled to re types. This returns the index - into the pattern list. If the pattern was not a list this returns index - 0 on a successful match. This may raise exceptions for EOF or TIMEOUT. - To avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to the - pattern list. + pattern is overloaded and may take several types. The pattern can be a + StringType, EOF, a compiled re, or a list of any of those types. + Strings will be compiled to re types. This returns the index into the + pattern list. If the pattern was not a list this returns index 0 on a + successful match. This may raise exceptions for EOF or TIMEOUT. To + avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to the pattern + list. That will cause expect to match an EOF or TIMEOUT condition + instead of raising an exception. After a match is found the instance attributes 'before', 'after' and 'match' will be set. You can see all the data read before the match in @@ -1201,7 +1204,7 @@ class spawn (object): If timeout is -1 then timeout will be set to the self.timeout value. - Note: A list entry may be EOF or TIMEOUT instead of a string. This will + A list entry may be EOF or TIMEOUT instead of a string. This will catch these exceptions and return the index of the list entry instead of raising the exception. The attribute 'after' will be set to the exception type. The attribute 'match' will be None. This allows you to @@ -1487,16 +1490,27 @@ class spawn (object): raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the maxread member variable.') - def expect_exact (self, pattern_list, timeout = -1): + def expect_exact (self, pattern_list, timeout = -1, searchwindowsize=None): - """This method is no longer supported or allowed. It was too hard to - maintain and keep it up to date with expect_list. Few people used this - method. Most people favored reliability over speed. The implementation - is left in comments in case anyone needs to hack this feature back into - their copy. If someone wants to diff this with expect_list and make - them work nearly the same then I will consider adding this make in. """ + """This will escape all the regular expression meta chars for each + string in the pattern_list. This will only escape the strings, so you + can still pass in compiled regexes, EOF, or TIMEOUT. This function + otherwise behaves exactly like expect(). + + This is useful if you want to match an exact pattern string without + having regex meta characters interpreted. This simply calls re.escape() + on each string in the list. """ + + if pattern_list is None: + return [] + if type(pattern_list) is not types.ListType: + pattern_list = [pattern_list] + for i in range(0,len(pattern_list)): + if type(pattern_list[i]) is types.StringType: + pattern_list[i] = re.escape(pattern_list[i]) + return self.expect(pattern_list, timeout, searchwindowsize) +# raise ExceptionPexpect ('This method is no longer supported or allowed.') - raise ExceptionPexpect ('This method is no longer supported or allowed.') def setlog (self, fileobject): """This method is no longer supported or allowed. diff --git a/pexpect/tests/test_expect.py b/pexpect/tests/test_expect.py index 33d10b0..e0819c2 100755 --- a/pexpect/tests/test_expect.py +++ b/pexpect/tests/test_expect.py @@ -137,31 +137,32 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): p = pexpect.spawn('ls -l /bin') the_new_way = '' while 1: - i = p.expect (['\n', pexpect.EOF]) - the_new_way = the_new_way + p.before - if i == 1: - break + i = p.expect (['\n', pexpect.EOF]) + the_new_way = the_new_way + p.before + if i == 1: + break the_new_way = the_new_way[:-1] the_new_way = the_new_way.replace('\r','\n') # For some reason I get an extra newline under OS X evey once in a while. # I found it by looking through the hex_dump(). assert the_old_way == the_new_way, hex_dump(the_new_way) + "\n" + hex_dump(the_old_way) -# def test_expect_exact (self): -# the_old_way = commands.getoutput('ls -l /bin') -# -# p = pexpect.spawn('ls -l /bin') -# the_new_way = '' -# while 1: -# i = p.expect (['\n', pexpect.EOF]) -# the_new_way = the_new_way + p.before -# if i == 1: -# break -# the_new_way = the_new_way[:-1] -# the_new_way = the_new_way.replace('\r','\n') -# -# assert the_old_way == the_new_way -# + def test_expect_exact (self): + the_old_way = commands.getoutput('ls -l /bin') + p = pexpect.spawn('ls -l /bin') + the_new_way = '' + while 1: + i = p.expect_exact (['\n', pexpect.EOF]) + the_new_way = the_new_way + p.before + if i == 1: + break + the_new_way = the_new_way[:-1] + the_new_way = the_new_way.replace('\r','\n') + assert the_old_way == the_new_way + p = pexpect.spawn('echo hello.?world') + i = p.expect_exact('.?') + assert p.before == 'hello' and p.after == '.?' + def test_expect_eof (self): the_old_way = commands.getoutput('/bin/ls -l /bin') p = pexpect.spawn('/bin/ls -l /bin') |
