summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-03-09 19:38:22 -0500
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-03-09 19:38:22 -0500
commit2ea9658fae7ce882bfc3266415fb634c79b4db09 (patch)
tree0b402c1b18115c35d1ae8a9653d9594d280a2dac
parentc2d1de68d247668f55523582b35849d8c61b2144 (diff)
downloadcmd2-git-2ea9658fae7ce882bfc3266415fb634c79b4db09.tar.gz
Replaced StdSim.__store_output with StdSim.pause_storage
-rw-r--r--cmd2/pyscript_bridge.py7
-rw-r--r--cmd2/utils.py17
-rw-r--r--tests/test_utils.py22
3 files changed, 29 insertions, 17 deletions
diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py
index d2e52a30..f3ce841d 100644
--- a/cmd2/pyscript_bridge.py
+++ b/cmd2/pyscript_bridge.py
@@ -34,14 +34,15 @@ class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr
In some cases, the data member may contain everything needed for a command and storing stdout
and stderr might just be a duplication of data that wastes memory. In that case, the StdSim can
- be told not to store output with its set_store_output() method.
+ be told not to store output with its pause_storage member. While this member is True, any output
+ sent to StdSim won't be saved in its buffer.
The code would look like this:
if isinstance(self.stdout, StdSim):
- self.stdout.set_store_output(False)
+ self.stdout.pause_storage = True
if isinstance(sys.stderr, StdSim):
- sys.stderr.set_store_output(False)
+ sys.stderr.pause_storage = True
See StdSim class in utils.py for more information
diff --git a/cmd2/utils.py b/cmd2/utils.py
index ca68bff7..a8760a65 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -278,7 +278,7 @@ class StdSim(object):
self.echo = echo
self.encoding = encoding
self.errors = errors
- self.__store_output = True
+ self.pause_storage = False
self.buffer = ByteBuf(self)
def write(self, s: str) -> None:
@@ -286,7 +286,7 @@ class StdSim(object):
if not isinstance(s, str):
raise TypeError('write() argument must be str, not {}'.format(type(s)))
- if self.__store_output:
+ if not self.pause_storage:
self.buffer.byte_buf += s.encode(encoding=self.encoding, errors=self.errors)
if self.echo:
self.inner_stream.write(s)
@@ -315,17 +315,6 @@ class StdSim(object):
"""Clear the internal contents"""
self.buffer.byte_buf = b''
- def get_store_output(self) -> bool:
- return self.__store_output
-
- def set_store_output(self, store_output: bool) -> None:
- """
- Set whether output should be saved in buffer.byte_buf
- :param store_output: Store output if True, otherwise do not and clear the buffer
- """
- self.__store_output = self.buffer.store_output = store_output
- self.clear()
-
def __getattr__(self, item: str):
if item in self.__dict__:
return self.__dict__[item]
@@ -345,7 +334,7 @@ class ByteBuf(object):
"""Add bytes to internal bytes buffer and if echo is True, echo contents to inner stream."""
if not isinstance(b, bytes):
raise TypeError('a bytes-like object is required, not {}'.format(type(b)))
- if self.std_sim_instance.get_store_output():
+ if not self.std_sim_instance.pause_storage:
self.byte_buf += b
if self.std_sim_instance.echo:
self.std_sim_instance.inner_stream.buffer.write(b)
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 75d4479a..307f69da 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -194,3 +194,25 @@ def test_stdsim_getattr_noexist(stdout_sim):
# Here the StdSim getattr is allowing us to access methods defined by the inner stream
assert not stdout_sim.isatty()
+def test_stdsim_pause_storage(stdout_sim):
+ # Test pausing storage for string data
+ my_str = 'Hello World'
+
+ stdout_sim.pause_storage = False
+ stdout_sim.write(my_str)
+ assert stdout_sim.read() == my_str
+
+ stdout_sim.pause_storage = True
+ stdout_sim.write(my_str)
+ assert stdout_sim.read() == ''
+
+ # Test pausing storage for binary data
+ b_str = b'Hello World'
+
+ stdout_sim.pause_storage = False
+ stdout_sim.buffer.write(b_str)
+ assert stdout_sim.readbytes() == b_str
+
+ stdout_sim.pause_storage = True
+ stdout_sim.buffer.write(b_str)
+ assert stdout_sim.getbytes() == b''