2023-07-28 18:45:18 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2024-01-15 15:52:10 +00:00
|
|
|
import sys, traceback
|
|
|
|
|
|
|
|
# Avoid "LookupError: unknown encoding: ascii" when open() called in a destructor
|
|
|
|
outnull_file = open(os.devnull, "w")
|
|
|
|
errnull_file = open(os.devnull, "w")
|
2023-07-28 18:45:18 +00:00
|
|
|
|
|
|
|
class suppress_stdout_stderr(object):
|
2023-11-02 19:30:55 +00:00
|
|
|
# NOTE: these must be "saved" here to avoid exceptions when using
|
|
|
|
# this context manager inside of a __del__ method
|
|
|
|
sys = sys
|
|
|
|
os = os
|
|
|
|
|
2023-11-03 17:02:15 +00:00
|
|
|
def __init__(self, disable: bool = True):
|
|
|
|
self.disable = disable
|
|
|
|
|
2023-07-28 18:45:18 +00:00
|
|
|
# Oddly enough this works better than the contextlib version
|
|
|
|
def __enter__(self):
|
2023-11-03 17:02:15 +00:00
|
|
|
if self.disable:
|
|
|
|
return self
|
|
|
|
|
2023-12-12 01:44:51 +00:00
|
|
|
# Check if sys.stdout and sys.stderr have fileno method
|
|
|
|
if not hasattr(self.sys.stdout, 'fileno') or not hasattr(self.sys.stderr, 'fileno'):
|
|
|
|
return self # Return the instance without making changes
|
|
|
|
|
2023-11-02 19:30:55 +00:00
|
|
|
self.old_stdout_fileno_undup = self.sys.stdout.fileno()
|
|
|
|
self.old_stderr_fileno_undup = self.sys.stderr.fileno()
|
2023-07-28 18:45:18 +00:00
|
|
|
|
2023-12-12 01:44:51 +00:00
|
|
|
self.old_stdout_fileno = self.os.dup(self.old_stdout_fileno_undup)
|
|
|
|
self.old_stderr_fileno = self.os.dup(self.old_stderr_fileno_undup)
|
2023-07-28 18:45:18 +00:00
|
|
|
|
2023-11-02 19:30:55 +00:00
|
|
|
self.old_stdout = self.sys.stdout
|
|
|
|
self.old_stderr = self.sys.stderr
|
2023-07-28 18:45:18 +00:00
|
|
|
|
2024-01-15 15:52:10 +00:00
|
|
|
self.os.dup2(outnull_file.fileno(), self.old_stdout_fileno_undup)
|
|
|
|
self.os.dup2(errnull_file.fileno(), self.old_stderr_fileno_undup)
|
2023-07-28 18:45:18 +00:00
|
|
|
|
2024-01-15 15:52:10 +00:00
|
|
|
self.sys.stdout = outnull_file
|
|
|
|
self.sys.stderr = errnull_file
|
2023-07-28 18:45:18 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, *_):
|
2023-11-03 17:02:15 +00:00
|
|
|
if self.disable:
|
|
|
|
return
|
|
|
|
|
2023-12-12 01:44:51 +00:00
|
|
|
# Check if sys.stdout and sys.stderr have fileno method
|
|
|
|
if hasattr(self.sys.stdout, 'fileno') and hasattr(self.sys.stderr, 'fileno'):
|
|
|
|
self.sys.stdout = self.old_stdout
|
|
|
|
self.sys.stderr = self.old_stderr
|
2023-07-28 18:45:18 +00:00
|
|
|
|
2023-12-12 01:44:51 +00:00
|
|
|
self.os.dup2(self.old_stdout_fileno, self.old_stdout_fileno_undup)
|
|
|
|
self.os.dup2(self.old_stderr_fileno, self.old_stderr_fileno_undup)
|
2023-07-28 18:45:18 +00:00
|
|
|
|
2023-12-12 01:44:51 +00:00
|
|
|
self.os.close(self.old_stdout_fileno)
|
|
|
|
self.os.close(self.old_stderr_fileno)
|