Skip to content

Commit 06bd735

Browse files
authored
Merge pull request #564 from python-cmd2/stdsim_binary
StdSim improvements for handling binary data
2 parents b99c094 + 68d8de8 commit 06bd735

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

cmd2/utils.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,35 +268,49 @@ def write(self, b: bytes) -> None:
268268
if self.echo:
269269
self.inner_stream.buffer.write(b)
270270

271-
def __init__(self, inner_stream, echo: bool = False) -> None:
271+
def __init__(self, inner_stream, echo: bool = False,
272+
encoding: str='utf-8', errors: str='replace') -> None:
273+
"""
274+
Initializer
275+
:param inner_stream: the emulated stream
276+
:param echo: if True, then all input will be echoed to inner_stream
277+
:param encoding: codec for encoding/decoding strings (defaults to utf-8)
278+
:param errors: how to handle encoding/decoding errors (defaults to replace)
279+
"""
272280
self.buffer = self.ByteBuf(inner_stream, echo)
273281
self.inner_stream = inner_stream
282+
self.encoding = encoding
283+
self.errors = errors
274284

275285
def write(self, s: str) -> None:
276-
"""Add str to internal bytes buffer and if echo is True, echo contents to inner stream."""
286+
"""Add str to internal bytes buffer and if echo is True, echo contents to inner stream"""
277287
if not isinstance(s, str):
278288
raise TypeError('write() argument must be str, not {}'.format(type(s)))
279-
b = s.encode()
289+
b = s.encode(encoding=self.encoding, errors=self.errors)
280290
self.buffer.write(b)
281291

282292
def getvalue(self) -> str:
283-
"""Get the internal contents as a str.
293+
"""Get the internal contents as a str"""
294+
return self.buffer.byte_buf.decode(encoding=self.encoding, errors=self.errors)
284295

285-
:return string from the internal contents
286-
"""
287-
return self.buffer.byte_buf.decode()
296+
def getbytes(self) -> bytes:
297+
"""Get the internal contents as bytes"""
298+
return self.buffer.byte_buf
288299

289300
def read(self) -> str:
290-
"""Read from the internal contents as a str and then clear them out.
291-
292-
:return: string from the internal contents
293-
"""
301+
"""Read from the internal contents as a str and then clear them out"""
294302
result = self.getvalue()
295303
self.clear()
296304
return result
297305

306+
def readbytes(self) -> bytes:
307+
"""Read from the internal contents as bytes and then clear them out"""
308+
result = self.getbytes()
309+
self.clear()
310+
return result
311+
298312
def clear(self) -> None:
299-
"""Clear the internal contents."""
313+
"""Clear the internal contents"""
300314
self.buffer.byte_buf = b''
301315

302316
def __getattr__(self, item: str):

tests/test_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def test_stdsim_buffer_write_bytes(stdout_sim):
133133
b_str = b'Hello World'
134134
stdout_sim.buffer.write(b_str)
135135
assert stdout_sim.getvalue() == b_str.decode()
136+
assert stdout_sim.getbytes() == b_str
136137

137138
def test_stdsim_buffer_write_str(stdout_sim):
138139
my_str = 'Hello World'
@@ -148,6 +149,15 @@ def test_stdsim_read(stdout_sim):
148149
assert stdout_sim.read() == my_str
149150
assert stdout_sim.getvalue() == ''
150151

152+
def test_stdsim_read_bytes(stdout_sim):
153+
b_str = b'Hello World'
154+
stdout_sim.buffer.write(b_str)
155+
# getbytes() returns the value and leaves it unaffected internally
156+
assert stdout_sim.getbytes() == b_str
157+
# read_bytes() returns the value and then clears the internal buffer
158+
assert stdout_sim.readbytes() == b_str
159+
assert stdout_sim.getbytes() == b''
160+
151161
def test_stdsim_clear(stdout_sim):
152162
my_str = 'Hello World'
153163
stdout_sim.write(my_str)

0 commit comments

Comments
 (0)