Skip to content

Commit b97763a

Browse files
committed
Fixed error where an inner stream like StringIO doesn't have a buffer attribute.
1 parent 9b1bfab commit b97763a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

cmd2/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,24 @@ class StdSim(object):
255255
"""
256256
class ByteBuf(object):
257257
"""Inner class which stores an actual bytes buffer and does the actual output if echo is enabled."""
258-
def __init__(self, inner_stream, echo: bool = False) -> None:
258+
def __init__(self, inner_stream, echo: bool = False,
259+
encoding: str='utf-8', errors: str='replace') -> None:
259260
self.byte_buf = b''
260261
self.inner_stream = inner_stream
261262
self.echo = echo
263+
self.encoding = encoding
264+
self.errors = errors
262265

263266
def write(self, b: bytes) -> None:
264267
"""Add bytes to internal bytes buffer and if echo is True, echo contents to inner stream."""
265268
if not isinstance(b, bytes):
266269
raise TypeError('a bytes-like object is required, not {}'.format(type(b)))
267270
self.byte_buf += b
268271
if self.echo:
269-
self.inner_stream.buffer.write(b)
272+
if hasattr(self.inner_stream, 'buffer'):
273+
self.inner_stream.buffer.write(b)
274+
else:
275+
self.inner_stream.write(b.decode(encoding=self.encoding, errors=self.errors))
270276

271277
def __init__(self, inner_stream, echo: bool = False,
272278
encoding: str='utf-8', errors: str='replace') -> None:

0 commit comments

Comments
 (0)