diff options
author | Thomas Kluyver <takowl@gmail.com> | 2014-07-03 17:26:42 -0700 |
---|---|---|
committer | Thomas Kluyver <takowl@gmail.com> | 2014-09-21 12:06:51 -0700 |
commit | 9b2571c7390b0329e97fe44a61e3b260a772cb3f (patch) | |
tree | 7d725fd3c96a66432d91901b70353b9b67e9b144 | |
parent | 1ced5524cd740ef12c0851ac116af6568cc08aac (diff) | |
download | pexpect-9b2571c7390b0329e97fe44a61e3b260a772cb3f.tar.gz |
Expose async parameter for expect_exact
-rw-r--r-- | pexpect/__init__.py | 21 | ||||
-rw-r--r-- | pexpect/expect.py | 2 | ||||
-rw-r--r-- | tests/test_async.py | 15 |
3 files changed, 26 insertions, 12 deletions
diff --git a/pexpect/__init__.py b/pexpect/__init__.py index 2f16acd..9c447f7 100644 --- a/pexpect/__init__.py +++ b/pexpect/__init__.py @@ -1466,7 +1466,8 @@ class spawn(object): return self.expect_list(compiled_pattern_list, timeout, searchwindowsize, async) - def expect_list(self, pattern_list, timeout=-1, searchwindowsize=-1, async=False): + def expect_list(self, pattern_list, timeout=-1, searchwindowsize=-1, + async=False): '''This takes a list of compiled regular expressions and returns the index into the pattern_list that matched the child output. The list may @@ -1477,17 +1478,18 @@ class spawn(object): the expect() method. This is called by expect(). If timeout==-1 then the self.timeout value is used. If searchwindowsize==-1 then the self.searchwindowsize value is used. ''' + if timeout == -1: + timeout = self.timeout exp = Expecter(self, searcher_re(pattern_list), searchwindowsize) if async: from .async import expect_async - if timeout == -1: - timeout = self.timeout return expect_async(exp, timeout) else: return exp.expect_loop(timeout) - def expect_exact(self, pattern_list, timeout=-1, searchwindowsize=-1): + def expect_exact(self, pattern_list, timeout=-1, searchwindowsize=-1, + async=False): '''This is similar to expect(), but uses plain string matching instead of compiled regular expressions in 'pattern_list'. The 'pattern_list' @@ -1500,6 +1502,8 @@ class spawn(object): This method is also useful when you don't want to have to worry about escaping regular expression characters that you want to match.''' + if timeout == -1: + timeout = self.timeout if (isinstance(pattern_list, self.allowed_string_types) or pattern_list in (TIMEOUT, EOF)): @@ -1517,10 +1521,13 @@ class spawn(object): except TypeError: self._pattern_type_err(pattern_list) pattern_list = [prepare_pattern(p) for p in pattern_list] + exp = Expecter(self, searcher_string(pattern_list), searchwindowsize) - return exp.expect_loop(timeout) - return self.expect_loop(searcher_string(pattern_list), - timeout, searchwindowsize) + if async: + from .async import expect_async + return expect_async(exp, timeout) + else: + return exp.expect_loop(timeout) def expect_loop(self, searcher, timeout=-1, searchwindowsize=-1): '''This is the common loop used inside expect. The 'searcher' should be diff --git a/pexpect/expect.py b/pexpect/expect.py index a87948c..b8da406 100644 --- a/pexpect/expect.py +++ b/pexpect/expect.py @@ -77,8 +77,6 @@ class Expecter(object): spawn = self.spawn from . import EOF, TIMEOUT - if timeout == -1: - timeout = self.spawn.timeout if timeout is not None: end_time = time.time() + timeout diff --git a/tests/test_async.py b/tests/test_async.py index bb945a4..ce75572 100644 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -3,14 +3,17 @@ try: except ImportError: asyncio = None -import pexpect +import sys import unittest +import pexpect +from .PexpectTestCase import PexpectTestCase + def run(coro): return asyncio.get_event_loop().run_until_complete(coro) @unittest.skipIf(asyncio is None, "Requires asyncio") -class AsyncTests(unittest.TestCase): +class AsyncTests(PexpectTestCase): def test_simple_expect(self): p = pexpect.spawn('cat') p.sendline('Hello asyncio') @@ -39,4 +42,10 @@ class AsyncTests(unittest.TestCase): p.sendeof() coro = p.expect('Blah', async=True) with self.assertRaises(pexpect.EOF): - run(coro)
\ No newline at end of file + run(coro) + + def test_expect_exact(self): + p = pexpect.spawn('%s list100.py' % sys.executable) + assert run(p.expect_exact(b'5', async=True)) == 0 + assert run(p.expect_exact(['wpeok', b'11'], async=True)) == 1 + assert run(p.expect_exact([b'foo', pexpect.EOF], async=True)) == 1 |