diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-09-24 09:49:46 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-09-24 09:49:46 -0400 |
commit | ca4ac5cfe4fae041463674cd8ee40b62444693f8 (patch) | |
tree | 181e1fb0a5f35d25b14f58541d38d0bbebcf9853 /cmd2/utils.py | |
parent | 35550e048bde73b08fad28c2a8d844dcbdea7f35 (diff) | |
download | cmd2-git-ca4ac5cfe4fae041463674cd8ee40b62444693f8.tar.gz |
StdSim write methods now raise a TypeError exception if passed the wrong type
Also:
- Added explicit unit tests for StdSim to test_utils.py
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r-- | cmd2/utils.py | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index 83185603..bdb488cc 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -262,6 +262,8 @@ class StdSim(object): def write(self, b: bytes) -> None: """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))) self.byte_buf += b if self.echo: self.inner_stream.buffer.write(b) @@ -272,6 +274,8 @@ class StdSim(object): def write(self, s: str) -> None: """Add str to internal bytes buffer and if echo is True, echo contents to inner stream.""" + if not isinstance(s, str): + raise TypeError('write() argument must be str, not {}'.format(type(s))) b = s.encode() self.buffer.write(b) |