diff options
author | Joel Edwards <joeledwards@gmail.com> | 2013-12-10 05:46:47 -0700 |
---|---|---|
committer | Joel Edwards <joeledwards@gmail.com> | 2013-12-11 07:37:55 -0700 |
commit | b6f8d73985be36b5f2f5a02e64d4586c1aab0cdf (patch) | |
tree | cf607b6077514a042a8f65473e75805eca24ffa5 | |
parent | fd57bd0466040238cd18cf2068b739cce36e9d40 (diff) | |
download | pexpect-b6f8d73985be36b5f2f5a02e64d4586c1aab0cdf.tar.gz |
__init__.py (pexpect)
- updated the read_nonblocking method to take a new boolean coerce_result
argument
pxssh.py
- updated try_read_prompt to support python 2.x/3.x results from os.read
(string/binary respectively)
-rw-r--r-- | pexpect/__init__.py | 10 | ||||
-rw-r--r-- | pexpect/pxssh.py | 16 |
2 files changed, 19 insertions, 7 deletions
diff --git a/pexpect/__init__.py b/pexpect/__init__.py index ed5e1e4..e248ad3 100644 --- a/pexpect/__init__.py +++ b/pexpect/__init__.py @@ -843,7 +843,7 @@ class spawn(object): second_log.write(s) second_log.flush() - def read_nonblocking(self, size=1, timeout=-1): + def read_nonblocking(self, size=1, timeout=-1, coerce_result=True): '''This reads at most size characters from the child application. It includes a timeout. If the read does not complete within the timeout period then a TIMEOUT exception is raised. If the end of file is read @@ -914,8 +914,12 @@ class spawn(object): self.flag_eof = True raise EOF('End Of File (EOF). Empty string style platform.') - s = self._coerce_read_string(s) - self._log(s, 'read') + cs = self._coerce_read_string(s) + self._log(cs, 'read') + + if coerce_result: + s = cs + return s raise ExceptionPexpect('Reached an unexpected state.') diff --git a/pexpect/pxssh.py b/pexpect/pxssh.py index 65f2c57..e644a74 100644 --- a/pexpect/pxssh.py +++ b/pexpect/pxssh.py @@ -21,6 +21,7 @@ PEXPECT LICENSE ''' from pexpect import ExceptionPexpect, TIMEOUT, EOF, spawn +import struct import time import os @@ -158,15 +159,18 @@ class pxssh (spawn): # maximum time for reading the entire prompt total_timeout = timeout_multiplier * 3.0 - prompt = '' + prompt = None begin = time.time() expired = 0.0 timeout = first_char_timeout while expired < total_timeout: try: - c = self.read_nonblocking(size=1, timeout=timeout) - prompt += c # append acquired content + c = self.read_nonblocking(size=1, timeout=timeout, coerce_result=False) + if prompt is None: + prompt = c + else: + prompt += c expired = time.time() - begin # updated total time expired timeout = inter_char_timeout except TIMEOUT: @@ -174,7 +178,11 @@ class pxssh (spawn): # inter_char_timeout will drop us out of the loop quickly expired = total_timeout - return prompt + result = '' + if prompt is not None: + result = self._coerce_read_string(prompt) + + return result def sync_original_prompt (self, sync_multiplier=1.0): |