diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-10-09 18:30:13 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-10-09 18:30:13 -0400 |
commit | b97763a4465618a49aa3befac98bbfefaff60979 (patch) | |
tree | 4e650b6f9339ce1256bfa64a2192dfecdf241671 | |
parent | 9b1bfab6e2a908d119e46bcc70e98d94739ab4a4 (diff) | |
download | cmd2-git-b97763a4465618a49aa3befac98bbfefaff60979.tar.gz |
Fixed error where an inner stream like StringIO doesn't have a buffer attribute.
-rw-r--r-- | cmd2/utils.py | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index 501733a9..d4a3db2f 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -255,10 +255,13 @@ class StdSim(object): """ class ByteBuf(object): """Inner class which stores an actual bytes buffer and does the actual output if echo is enabled.""" - def __init__(self, inner_stream, echo: bool = False) -> None: + def __init__(self, inner_stream, echo: bool = False, + encoding: str='utf-8', errors: str='replace') -> None: self.byte_buf = b'' self.inner_stream = inner_stream self.echo = echo + self.encoding = encoding + self.errors = errors def write(self, b: bytes) -> None: """Add bytes to internal bytes buffer and if echo is True, echo contents to inner stream.""" @@ -266,7 +269,10 @@ class StdSim(object): raise TypeError('a bytes-like object is required, not {}'.format(type(b))) self.byte_buf += b if self.echo: - self.inner_stream.buffer.write(b) + if hasattr(self.inner_stream, 'buffer'): + self.inner_stream.buffer.write(b) + else: + self.inner_stream.write(b.decode(encoding=self.encoding, errors=self.errors)) def __init__(self, inner_stream, echo: bool = False, encoding: str='utf-8', errors: str='replace') -> None: |