Skip to content

Commit 2ea9658

Browse files
committed
Replaced StdSim.__store_output with StdSim.pause_storage
1 parent c2d1de6 commit 2ea9658

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

cmd2/pyscript_bridge.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr
3434
3535
In some cases, the data member may contain everything needed for a command and storing stdout
3636
and stderr might just be a duplication of data that wastes memory. In that case, the StdSim can
37-
be told not to store output with its set_store_output() method.
37+
be told not to store output with its pause_storage member. While this member is True, any output
38+
sent to StdSim won't be saved in its buffer.
3839
3940
The code would look like this:
4041
if isinstance(self.stdout, StdSim):
41-
self.stdout.set_store_output(False)
42+
self.stdout.pause_storage = True
4243
4344
if isinstance(sys.stderr, StdSim):
44-
sys.stderr.set_store_output(False)
45+
sys.stderr.pause_storage = True
4546
4647
See StdSim class in utils.py for more information
4748

cmd2/utils.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,15 @@ def __init__(self, inner_stream, echo: bool = False,
278278
self.echo = echo
279279
self.encoding = encoding
280280
self.errors = errors
281-
self.__store_output = True
281+
self.pause_storage = False
282282
self.buffer = ByteBuf(self)
283283

284284
def write(self, s: str) -> None:
285285
"""Add str to internal bytes buffer and if echo is True, echo contents to inner stream"""
286286
if not isinstance(s, str):
287287
raise TypeError('write() argument must be str, not {}'.format(type(s)))
288288

289-
if self.__store_output:
289+
if not self.pause_storage:
290290
self.buffer.byte_buf += s.encode(encoding=self.encoding, errors=self.errors)
291291
if self.echo:
292292
self.inner_stream.write(s)
@@ -315,17 +315,6 @@ def clear(self) -> None:
315315
"""Clear the internal contents"""
316316
self.buffer.byte_buf = b''
317317

318-
def get_store_output(self) -> bool:
319-
return self.__store_output
320-
321-
def set_store_output(self, store_output: bool) -> None:
322-
"""
323-
Set whether output should be saved in buffer.byte_buf
324-
:param store_output: Store output if True, otherwise do not and clear the buffer
325-
"""
326-
self.__store_output = self.buffer.store_output = store_output
327-
self.clear()
328-
329318
def __getattr__(self, item: str):
330319
if item in self.__dict__:
331320
return self.__dict__[item]
@@ -345,7 +334,7 @@ def write(self, b: bytes) -> None:
345334
"""Add bytes to internal bytes buffer and if echo is True, echo contents to inner stream."""
346335
if not isinstance(b, bytes):
347336
raise TypeError('a bytes-like object is required, not {}'.format(type(b)))
348-
if self.std_sim_instance.get_store_output():
337+
if not self.std_sim_instance.pause_storage:
349338
self.byte_buf += b
350339
if self.std_sim_instance.echo:
351340
self.std_sim_instance.inner_stream.buffer.write(b)

tests/test_utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,25 @@ def test_stdsim_getattr_noexist(stdout_sim):
194194
# Here the StdSim getattr is allowing us to access methods defined by the inner stream
195195
assert not stdout_sim.isatty()
196196

197+
def test_stdsim_pause_storage(stdout_sim):
198+
# Test pausing storage for string data
199+
my_str = 'Hello World'
200+
201+
stdout_sim.pause_storage = False
202+
stdout_sim.write(my_str)
203+
assert stdout_sim.read() == my_str
204+
205+
stdout_sim.pause_storage = True
206+
stdout_sim.write(my_str)
207+
assert stdout_sim.read() == ''
208+
209+
# Test pausing storage for binary data
210+
b_str = b'Hello World'
211+
212+
stdout_sim.pause_storage = False
213+
stdout_sim.buffer.write(b_str)
214+
assert stdout_sim.readbytes() == b_str
215+
216+
stdout_sim.pause_storage = True
217+
stdout_sim.buffer.write(b_str)
218+
assert stdout_sim.getbytes() == b''

0 commit comments

Comments
 (0)