@@ -261,28 +261,10 @@ def natural_sort(list_to_sort: Iterable[str]) -> List[str]:
261261
262262
263263class StdSim (object ):
264- """Class to simulate behavior of sys.stdout or sys.stderr.
265-
264+ """
265+ Class to simulate behavior of sys.stdout or sys.stderr.
266266 Stores contents in internal buffer and optionally echos to the inner stream it is simulating.
267267 """
268- class ByteBuf (object ):
269- """Inner class which stores an actual bytes buffer and does the actual output if echo is enabled."""
270- def __init__ (self , inner_stream , echo : bool = False ,
271- encoding : str = 'utf-8' , errors : str = 'replace' ) -> None :
272- self .byte_buf = b''
273- self .inner_stream = inner_stream
274- self .echo = echo
275- self .encoding = encoding
276- self .errors = errors
277-
278- def write (self , b : bytes ) -> None :
279- """Add bytes to internal bytes buffer and if echo is True, echo contents to inner stream."""
280- if not isinstance (b , bytes ):
281- raise TypeError ('a bytes-like object is required, not {}' .format (type (b )))
282- self .byte_buf += b
283- if self .echo :
284- self .inner_stream .buffer .write (b )
285-
286268 def __init__ (self , inner_stream , echo : bool = False ,
287269 encoding : str = 'utf-8' , errors : str = 'replace' ) -> None :
288270 """
@@ -292,17 +274,20 @@ def __init__(self, inner_stream, echo: bool = False,
292274 :param encoding: codec for encoding/decoding strings (defaults to utf-8)
293275 :param errors: how to handle encoding/decoding errors (defaults to replace)
294276 """
295- self .buffer = self .ByteBuf (inner_stream , echo )
296277 self .inner_stream = inner_stream
297278 self .echo = echo
298279 self .encoding = encoding
299280 self .errors = errors
281+ self .__store_output = True
282+ self .buffer = ByteBuf (self )
300283
301284 def write (self , s : str ) -> None :
302285 """Add str to internal bytes buffer and if echo is True, echo contents to inner stream"""
303286 if not isinstance (s , str ):
304287 raise TypeError ('write() argument must be str, not {}' .format (type (s )))
305- self .buffer .byte_buf += s .encode (encoding = self .encoding , errors = self .errors )
288+
289+ if self .__store_output :
290+ self .buffer .byte_buf += s .encode (encoding = self .encoding , errors = self .errors )
306291 if self .echo :
307292 self .inner_stream .write (s )
308293
@@ -330,13 +315,42 @@ def clear(self) -> None:
330315 """Clear the internal contents"""
331316 self .buffer .byte_buf = b''
332317
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+
333329 def __getattr__ (self , item : str ):
334330 if item in self .__dict__ :
335331 return self .__dict__ [item ]
336332 else :
337333 return getattr (self .inner_stream , item )
338334
339335
336+ class ByteBuf (object ):
337+ """
338+ Used by StdSim to write binary data and stores the actual bytes written
339+ """
340+ def __init__ (self , std_sim_instance : StdSim ) -> None :
341+ self .byte_buf = b''
342+ self .std_sim_instance = std_sim_instance
343+
344+ def write (self , b : bytes ) -> None :
345+ """Add bytes to internal bytes buffer and if echo is True, echo contents to inner stream."""
346+ if not isinstance (b , bytes ):
347+ raise TypeError ('a bytes-like object is required, not {}' .format (type (b )))
348+ if self .std_sim_instance .get_store_output ():
349+ self .byte_buf += b
350+ if self .std_sim_instance .echo :
351+ self .std_sim_instance .inner_stream .buffer .write (b )
352+
353+
340354def unquote_redirection_tokens (args : List [str ]) -> None :
341355 """
342356 Unquote redirection tokens in a list of command-line arguments
0 commit comments