From fd7332f59e003a8ee658647f883d4b03018c5e5b Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Mon, 15 Jan 2018 16:43:10 -0500 Subject: optimize pty buffering and searching Python strings are slow and expensive as buffers because they're immutable; replace the output buffer with a StringIO/BytesIO object see: https://github.com/pexpect/pexpect/issues/438 --- tests/test_expect.py | 8 ++++++++ tests/test_performance.py | 6 ++++++ 2 files changed, 14 insertions(+) (limited to 'tests') diff --git a/tests/test_expect.py b/tests/test_expect.py index dcf059b..ec55d43 100755 --- a/tests/test_expect.py +++ b/tests/test_expect.py @@ -400,6 +400,14 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): else: self.fail ('Expected an EOF exception.') + def test_buffer_interface(self): + p = pexpect.spawn('cat', timeout=5) + p.sendline (b'Hello') + p.expect (b'Hello') + assert len(p.buffer) + p.buffer = b'Testing' + p.sendeof () + def _before_after(self, p): p.timeout = 5 diff --git a/tests/test_performance.py b/tests/test_performance.py index 7be0cf6..63778af 100755 --- a/tests/test_performance.py +++ b/tests/test_performance.py @@ -23,6 +23,7 @@ from __future__ import print_function import unittest, time, sys import platform import pexpect +import re from . import PexpectTestCase # This isn't exactly a unit test, but it fits in nicely with the rest of the tests. @@ -101,6 +102,11 @@ class PerformanceTestCase (PexpectTestCase.PexpectTestCase): self.faster_range(100000) print("100000 calls to faster_range:", (time.time() - start_time)) + def test_large_stdout_stream(self): + e = pexpect.spawn('openssl rand -base64 {}'.format(1024*1024*25), searchwindowsize=1000) + resp = e.expect(['Password:', pexpect.EOF, pexpect.TIMEOUT]) + assert resp == 1 # index 1 == EOF + if __name__ == "__main__": unittest.main() -- cgit v1.2.1