Skip to content

Commit 639bd9b

Browse files
committed
Made encode and error class members of StdSim
1 parent 8c47a4a commit 639bd9b

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

cmd2/utils.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -268,38 +268,38 @@ 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 stream this sits on top of
276+
:param echo: if True, then all contents 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

275-
def write(self, s: str, encoding: str = 'utf-8') -> None:
276-
"""
277-
Add str to internal bytes buffer and if echo is True, echo contents to inner stream.
278-
:param: s: string to write
279-
:param encoding: how to encode the string (defaults to utf-8)
280-
"""
285+
def write(self, s: str) -> None:
286+
"""Add str to internal bytes buffer and if echo is True, echo contents to inner stream"""
281287
if not isinstance(s, str):
282288
raise TypeError('write() argument must be str, not {}'.format(type(s)))
283-
b = s.encode(encoding=encoding)
289+
b = s.encode(encoding=self.encoding, errors=self.errors)
284290
self.buffer.write(b)
285291

286-
def getvalue(self, encoding: str = 'utf-8') -> str:
287-
"""
288-
Get the internal contents as a str
289-
:param encoding: how to decode the bytes (defaults to utf-8)
290-
"""
291-
return self.buffer.byte_buf.decode(encoding=encoding, errors='replace')
292+
def getvalue(self) -> str:
293+
"""Get the internal contents as a str"""
294+
return self.buffer.byte_buf.decode(encoding=self.encoding, errors=self.errors)
292295

293296
def getbytes(self) -> bytes:
294297
"""Get the internal contents as bytes"""
295298
return self.buffer.byte_buf
296299

297-
def read(self, encoding: str = 'utf-8') -> str:
298-
"""
299-
Read from the internal contents as a str and then clear them out
300-
:param encoding: how to decode the bytes (defaults to utf-8)
301-
"""
302-
result = self.getvalue(encoding)
300+
def read(self) -> str:
301+
"""Read from the internal contents as a str and then clear them out"""
302+
result = self.getvalue()
303303
self.clear()
304304
return result
305305

0 commit comments

Comments
 (0)