Skip to content

Commit c7f7757

Browse files
authored
Merge pull request #619 from python-cmd2/string_stream
Fixed buffering issue when echoing StdSim data
2 parents fe324f2 + 9596181 commit c7f7757

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.9.8 (TBD, 2019)
2+
* Bug Fixes
3+
* Fixed issue with echoing strings in StdSim. Because they were being sent to a binary buffer, line buffering
4+
was being ignored.
5+
16
## 0.9.7 (January 08, 2019)
27
* Bug Fixes
38
* Fixed bug when user chooses a zero or negative index when calling ``Cmd.select()``

cmd2/utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,7 @@ def write(self, b: bytes) -> None:
281281
raise TypeError('a bytes-like object is required, not {}'.format(type(b)))
282282
self.byte_buf += b
283283
if self.echo:
284-
if hasattr(self.inner_stream, 'buffer'):
285-
self.inner_stream.buffer.write(b)
286-
else:
287-
self.inner_stream.write(b.decode(encoding=self.encoding, errors=self.errors))
284+
self.inner_stream.buffer.write(b)
288285

289286
def __init__(self, inner_stream, echo: bool = False,
290287
encoding: str='utf-8', errors: str='replace') -> None:
@@ -297,15 +294,17 @@ def __init__(self, inner_stream, echo: bool = False,
297294
"""
298295
self.buffer = self.ByteBuf(inner_stream, echo)
299296
self.inner_stream = inner_stream
297+
self.echo = echo
300298
self.encoding = encoding
301299
self.errors = errors
302300

303301
def write(self, s: str) -> None:
304302
"""Add str to internal bytes buffer and if echo is True, echo contents to inner stream"""
305303
if not isinstance(s, str):
306304
raise TypeError('write() argument must be str, not {}'.format(type(s)))
307-
b = s.encode(encoding=self.encoding, errors=self.errors)
308-
self.buffer.write(b)
305+
self.buffer.byte_buf += s.encode(encoding=self.encoding, errors=self.errors)
306+
if self.echo:
307+
self.inner_stream.write(s)
309308

310309
def getvalue(self) -> str:
311310
"""Get the internal contents as a str"""

tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def test_quot_string_if_needed_no():
122122

123123
@pytest.fixture
124124
def stdout_sim():
125-
stdsim = cu.StdSim(sys.stdout)
125+
stdsim = cu.StdSim(sys.stdout, echo=True)
126126
return stdsim
127127

128128
@pytest.fixture

0 commit comments

Comments
 (0)