@@ -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"""
0 commit comments