''' * Project : Screenipy * Author : Pranjal Joshi * Created : 07/05/2021 * Description : Class for supressing stdout & stderr ''' import os, sys class SuppressOutput: def __init__(self,suppress_stdout=False,suppress_stderr=False): self.suppress_stdout = suppress_stdout self.suppress_stderr = suppress_stderr self._stdout = None self._stderr = None def __enter__(self): devnull = open(os.devnull, "w") if self.suppress_stdout: self._stdout = sys.stdout sys.stdout = devnull if self.suppress_stderr: self._stderr = sys.stderr sys.stderr = devnull def __exit__(self, *args): if self.suppress_stdout: sys.stdout = self._stdout if self.suppress_stderr: sys.stderr = self._stderr